Web Interface For Lite Argus Control modules
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

303 lines
9.6 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*!
  2. * Ext JS Library 3.3.1
  3. * Copyright(c) 2006-2010 Sencha Inc.
  4. * licensing@sencha.com
  5. * http://www.sencha.com/license
  6. */
  7. /**
  8. * @class Ext.state.Provider
  9. * Abstract base class for state provider implementations. This class provides methods
  10. * for encoding and decoding <b>typed</b> variables including dates and defines the
  11. * Provider interface.
  12. */
  13. Ext.state.Provider = Ext.extend(Ext.util.Observable, {
  14. constructor : function(){
  15. /**
  16. * @event statechange
  17. * Fires when a state change occurs.
  18. * @param {Provider} this This state provider
  19. * @param {String} key The state key which was changed
  20. * @param {String} value The encoded value for the state
  21. */
  22. this.addEvents("statechange");
  23. this.state = {};
  24. Ext.state.Provider.superclass.constructor.call(this);
  25. },
  26. /**
  27. * Returns the current value for a key
  28. * @param {String} name The key name
  29. * @param {Mixed} defaultValue A default value to return if the key's value is not found
  30. * @return {Mixed} The state data
  31. */
  32. get : function(name, defaultValue){
  33. return typeof this.state[name] == "undefined" ?
  34. defaultValue : this.state[name];
  35. },
  36. /**
  37. * Clears a value from the state
  38. * @param {String} name The key name
  39. */
  40. clear : function(name){
  41. delete this.state[name];
  42. this.fireEvent("statechange", this, name, null);
  43. },
  44. /**
  45. * Sets the value for a key
  46. * @param {String} name The key name
  47. * @param {Mixed} value The value to set
  48. */
  49. set : function(name, value){
  50. this.state[name] = value;
  51. this.fireEvent("statechange", this, name, value);
  52. },
  53. /**
  54. * Decodes a string previously encoded with {@link #encodeValue}.
  55. * @param {String} value The value to decode
  56. * @return {Mixed} The decoded value
  57. */
  58. decodeValue : function(cookie){
  59. /**
  60. * a -> Array
  61. * n -> Number
  62. * d -> Date
  63. * b -> Boolean
  64. * s -> String
  65. * o -> Object
  66. * -> Empty (null)
  67. */
  68. var re = /^(a|n|d|b|s|o|e)\:(.*)$/,
  69. matches = re.exec(unescape(cookie)),
  70. all,
  71. type,
  72. v,
  73. kv;
  74. if(!matches || !matches[1]){
  75. return; // non state cookie
  76. }
  77. type = matches[1];
  78. v = matches[2];
  79. switch(type){
  80. case 'e':
  81. return null;
  82. case 'n':
  83. return parseFloat(v);
  84. case 'd':
  85. return new Date(Date.parse(v));
  86. case 'b':
  87. return (v == '1');
  88. case 'a':
  89. all = [];
  90. if(v != ''){
  91. Ext.each(v.split('^'), function(val){
  92. all.push(this.decodeValue(val));
  93. }, this);
  94. }
  95. return all;
  96. case 'o':
  97. all = {};
  98. if(v != ''){
  99. Ext.each(v.split('^'), function(val){
  100. kv = val.split('=');
  101. all[kv[0]] = this.decodeValue(kv[1]);
  102. }, this);
  103. }
  104. return all;
  105. default:
  106. return v;
  107. }
  108. },
  109. /**
  110. * Encodes a value including type information. Decode with {@link #decodeValue}.
  111. * @param {Mixed} value The value to encode
  112. * @return {String} The encoded value
  113. */
  114. encodeValue : function(v){
  115. var enc,
  116. flat = '',
  117. i = 0,
  118. len,
  119. key;
  120. if(v == null){
  121. return 'e:1';
  122. }else if(typeof v == 'number'){
  123. enc = 'n:' + v;
  124. }else if(typeof v == 'boolean'){
  125. enc = 'b:' + (v ? '1' : '0');
  126. }else if(Ext.isDate(v)){
  127. enc = 'd:' + v.toGMTString();
  128. }else if(Ext.isArray(v)){
  129. for(len = v.length; i < len; i++){
  130. flat += this.encodeValue(v[i]);
  131. if(i != len - 1){
  132. flat += '^';
  133. }
  134. }
  135. enc = 'a:' + flat;
  136. }else if(typeof v == 'object'){
  137. for(key in v){
  138. if(typeof v[key] != 'function' && v[key] !== undefined){
  139. flat += key + '=' + this.encodeValue(v[key]) + '^';
  140. }
  141. }
  142. enc = 'o:' + flat.substring(0, flat.length-1);
  143. }else{
  144. enc = 's:' + v;
  145. }
  146. return escape(enc);
  147. }
  148. });
  149. /**
  150. * @class Ext.state.Manager
  151. * This is the global state manager. By default all components that are "state aware" check this class
  152. * for state information if you don't pass them a custom state provider. In order for this class
  153. * to be useful, it must be initialized with a provider when your application initializes. Example usage:
  154. <pre><code>
  155. // in your initialization function
  156. init : function(){
  157. Ext.state.Manager.setProvider(new Ext.state.CookieProvider());
  158. var win = new Window(...);
  159. win.restoreState();
  160. }
  161. </code></pre>
  162. * @singleton
  163. */
  164. Ext.state.Manager = function(){
  165. var provider = new Ext.state.Provider();
  166. return {
  167. /**
  168. * Configures the default state provider for your application
  169. * @param {Provider} stateProvider The state provider to set
  170. */
  171. setProvider : function(stateProvider){
  172. provider = stateProvider;
  173. },
  174. /**
  175. * Returns the current value for a key
  176. * @param {String} name The key name
  177. * @param {Mixed} defaultValue The default value to return if the key lookup does not match
  178. * @return {Mixed} The state data
  179. */
  180. get : function(key, defaultValue){
  181. return provider.get(key, defaultValue);
  182. },
  183. /**
  184. * Sets the value for a key
  185. * @param {String} name The key name
  186. * @param {Mixed} value The state data
  187. */
  188. set : function(key, value){
  189. provider.set(key, value);
  190. },
  191. /**
  192. * Clears a value from the state
  193. * @param {String} name The key name
  194. */
  195. clear : function(key){
  196. provider.clear(key);
  197. },
  198. /**
  199. * Gets the currently configured state provider
  200. * @return {Provider} The state provider
  201. */
  202. getProvider : function(){
  203. return provider;
  204. }
  205. };
  206. }();
  207. /**
  208. * @class Ext.state.CookieProvider
  209. * @extends Ext.state.Provider
  210. * The default Provider implementation which saves state via cookies.
  211. * <br />Usage:
  212. <pre><code>
  213. var cp = new Ext.state.CookieProvider({
  214. path: "/cgi-bin/",
  215. expires: new Date(new Date().getTime()+(1000*60*60*24*30)), //30 days
  216. domain: "extjs.com"
  217. });
  218. Ext.state.Manager.setProvider(cp);
  219. </code></pre>
  220. * @cfg {String} path The path for which the cookie is active (defaults to root '/' which makes it active for all pages in the site)
  221. * @cfg {Date} expires The cookie expiration date (defaults to 7 days from now)
  222. * @cfg {String} domain The domain to save the cookie for. Note that you cannot specify a different domain than
  223. * your page is on, but you can specify a sub-domain, or simply the domain itself like 'extjs.com' to include
  224. * all sub-domains if you need to access cookies across different sub-domains (defaults to null which uses the same
  225. * domain the page is running on including the 'www' like 'www.extjs.com')
  226. * @cfg {Boolean} secure True if the site is using SSL (defaults to false)
  227. * @constructor
  228. * Create a new CookieProvider
  229. * @param {Object} config The configuration object
  230. */
  231. Ext.state.CookieProvider = Ext.extend(Ext.state.Provider, {
  232. constructor : function(config){
  233. Ext.state.CookieProvider.superclass.constructor.call(this);
  234. this.path = "/";
  235. this.expires = new Date(new Date().getTime()+(1000*60*60*24*7)); //7 days
  236. this.domain = null;
  237. this.secure = false;
  238. Ext.apply(this, config);
  239. this.state = this.readCookies();
  240. },
  241. // private
  242. set : function(name, value){
  243. if(typeof value == "undefined" || value === null){
  244. this.clear(name);
  245. return;
  246. }
  247. this.setCookie(name, value);
  248. Ext.state.CookieProvider.superclass.set.call(this, name, value);
  249. },
  250. // private
  251. clear : function(name){
  252. this.clearCookie(name);
  253. Ext.state.CookieProvider.superclass.clear.call(this, name);
  254. },
  255. // private
  256. readCookies : function(){
  257. var cookies = {},
  258. c = document.cookie + ";",
  259. re = /\s?(.*?)=(.*?);/g,
  260. matches,
  261. name,
  262. value;
  263. while((matches = re.exec(c)) != null){
  264. name = matches[1];
  265. value = matches[2];
  266. if(name && name.substring(0,3) == "ys-"){
  267. cookies[name.substr(3)] = this.decodeValue(value);
  268. }
  269. }
  270. return cookies;
  271. },
  272. // private
  273. setCookie : function(name, value){
  274. document.cookie = "ys-"+ name + "=" + this.encodeValue(value) +
  275. ((this.expires == null) ? "" : ("; expires=" + this.expires.toGMTString())) +
  276. ((this.path == null) ? "" : ("; path=" + this.path)) +
  277. ((this.domain == null) ? "" : ("; domain=" + this.domain)) +
  278. ((this.secure == true) ? "; secure" : "");
  279. },
  280. // private
  281. clearCookie : function(name){
  282. document.cookie = "ys-" + name + "=null; expires=Thu, 01-Jan-70 00:00:01 GMT" +
  283. ((this.path == null) ? "" : ("; path=" + this.path)) +
  284. ((this.domain == null) ? "" : ("; domain=" + this.domain)) +
  285. ((this.secure == true) ? "; secure" : "");
  286. }
  287. });