Remoto - VFS: preferences.js Source File
Remoto - VFS
preferences.js
Go to the documentation of this file.
1 
2 define( [
3  'remoto!stdlib:js/include/objectRegistry.js',
4  'include/modal/modal',
5  ],
6  function(objectRegistry,modal)
7  {
8  'use strict';
9 
25  function preferences()
26  {
27  this._vars = {};
28  this._objectLoader = null;
29  }
30 
31  preferences.prototype = {
32 
42  update: function( p )
43  {
44  //console.log("updating preferences!");
45  //console.log(p);
46  //console.log(this._vars);
47  //return;
48 
49  if (!p)
50  {
51  console.warn("Received null preference update. Returning.");
52  return;
53  }
54 
55  p = p.attributes ? p.attributes : p;
56 
57  for (var v in p)
58  {
59  if (v === "style")
60  {
61  var oldStyle = this._vars[v];
62  if (p[v].value !== oldStyle)
63  {
64  $("link[userStyle=true]").remove();
65  if (p[v].value)
66  $("head").append("<link type='text/css' rel='stylesheet' href='"+p[v].value+"' userStyle='true' />");
67  }
68  }
69 
70  if (v === "javascript")
71  {
72  $("script[userJS=true]").remove();
73  if (p[v].value)
74  $("head").append("<script src='"+p[v].value+"' userJS='true' />");
75  }
76 
77  this._vars[v] = p[v].value;
78  }
79 
80  //console.log(this._vars);
81  },
82 
93  applyDiff: function( diff, user )
94  {
95  //console.log("applyDiff preferences!");
96  this.update(diff);
97  },
98 
109  applySubscription: function(data, metadata)
110  {
111  //console.log("applySubscription preferences!");
112  //console.log(arguments,this._vars);
113 
114  if (data.base)
115  {
116  if (!this._objectLoader)
117  this._objectLoader = new objectRegistry.objectLoader( this._vars["preferencesBase"] );
118  return this._objectLoader.fetchDefinition( data.base, data, this.update.bind(this) );
119  }
120  else
121  return this.update(data);
122  },
123 
134  apply: function( value )
135  {
136  var v;
137  var r = value;
138  var changed = false;
139  var p;
140 
141  //var d = new Date();
142  //variables["datetime"] = d.toLocaleString();
143 
144  for (var variable in this._vars)
145  {
146  p = new RegExp("#"+variable+"#","g");
147  v = r;
148  r = r.replace(p, variables[variable]);
149  if ( r != v )
150  changed = true;
151  }
152 
153  return changed ? r : false;
154  },
155 
167  fetch: function( v,_default )
168  {
169  if (v in this._vars)
170  return this._vars[v];
171  else
172  return _default;
173  },
174 
185  set: function( n, v )
186  {
187  this._vars[n] = v;
188  },
189 
198  reset: function()
199  {
200  this._vars = {};
201  },
202 
211  applyRequestSuccess: function(command,path,data,metadata)
212  {
213  if (command === "rm")
214  {
215  setTimeout( function()
216  {
217  console.log("Preferences deleted.");
218  modal.cancelAllModals();
219  //$(window).trigger("logout");
220  $(window).trigger("reload");
221  }, 0 );
222  }
223  else
224  console.error("Preferences request success",arguments);
225  }
226  }
227 
228  return new preferences();
229  }
230 );
231 
232 
233 
234 
235 
236 
237 
238 
fetch(v, _default)
applySubscription(data, metadata)
setter value
a setter DOCME
setter user
a setter DOCME
getter path
a getter DOCME
applyRequestSuccess()
If an adminstrator has deleted his own preferences, this function will be called on success.
apply(value)
applyDiff(diff, user)
update(dirty)
Process the _queue and apply the settings.
metadata(paths)