Provider2.html 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  5. <title>The source code</title>
  6. <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
  7. <script type="text/javascript" src="../resources/prettify/prettify.js"></script>
  8. <style type="text/css">
  9. .highlight { display: block; background-color: #ddd; }
  10. </style>
  11. <script type="text/javascript">
  12. function highlight() {
  13. document.getElementById(location.hash.replace(/#/, "")).className = "highlight";
  14. }
  15. </script>
  16. </head>
  17. <body onload="prettyPrint(); highlight();">
  18. <pre class="prettyprint lang-js"><span id='Ext-state-Provider'>/**
  19. </span> * @class Ext.state.Provider
  20. * &lt;p&gt;Abstract base class for state provider implementations. The provider is responsible
  21. * for setting values and extracting values to/from the underlying storage source. The
  22. * storage source can vary and the details should be implemented in a subclass. For example
  23. * a provider could use a server side database or the browser localstorage where supported.&lt;/p&gt;
  24. *
  25. * &lt;p&gt;This class provides methods for encoding and decoding &lt;b&gt;typed&lt;/b&gt; variables including
  26. * dates and defines the Provider interface. By default these methods put the value and the
  27. * type information into a delimited string that can be stored. These should be overridden in
  28. * a subclass if you want to change the format of the encoded value and subsequent decoding.&lt;/p&gt;
  29. */
  30. Ext.define('Ext.state.Provider', {
  31. mixins: {
  32. observable: 'Ext.util.Observable'
  33. },
  34. <span id='Ext-state-Provider-cfg-prefix'> /**
  35. </span> * @cfg {String} prefix A string to prefix to items stored in the underlying state store.
  36. * Defaults to &lt;tt&gt;'ext-'&lt;/tt&gt;
  37. */
  38. prefix: 'ext-',
  39. constructor : function(config){
  40. config = config || {};
  41. var me = this;
  42. Ext.apply(me, config);
  43. <span id='Ext-state-Provider-event-statechange'> /**
  44. </span> * @event statechange
  45. * Fires when a state change occurs.
  46. * @param {Ext.state.Provider} this This state provider
  47. * @param {String} key The state key which was changed
  48. * @param {String} value The encoded value for the state
  49. */
  50. me.addEvents(&quot;statechange&quot;);
  51. me.state = {};
  52. me.mixins.observable.constructor.call(me);
  53. },
  54. <span id='Ext-state-Provider-method-get'> /**
  55. </span> * Returns the current value for a key
  56. * @param {String} name The key name
  57. * @param {Object} defaultValue A default value to return if the key's value is not found
  58. * @return {Object} The state data
  59. */
  60. get : function(name, defaultValue){
  61. return typeof this.state[name] == &quot;undefined&quot; ?
  62. defaultValue : this.state[name];
  63. },
  64. <span id='Ext-state-Provider-method-clear'> /**
  65. </span> * Clears a value from the state
  66. * @param {String} name The key name
  67. */
  68. clear : function(name){
  69. var me = this;
  70. delete me.state[name];
  71. me.fireEvent(&quot;statechange&quot;, me, name, null);
  72. },
  73. <span id='Ext-state-Provider-method-set'> /**
  74. </span> * Sets the value for a key
  75. * @param {String} name The key name
  76. * @param {Object} value The value to set
  77. */
  78. set : function(name, value){
  79. var me = this;
  80. me.state[name] = value;
  81. me.fireEvent(&quot;statechange&quot;, me, name, value);
  82. },
  83. <span id='Ext-state-Provider-method-decodeValue'> /**
  84. </span> * Decodes a string previously encoded with {@link #encodeValue}.
  85. * @param {String} value The value to decode
  86. * @return {Object} The decoded value
  87. */
  88. decodeValue : function(value){
  89. // a -&gt; Array
  90. // n -&gt; Number
  91. // d -&gt; Date
  92. // b -&gt; Boolean
  93. // s -&gt; String
  94. // o -&gt; Object
  95. // -&gt; Empty (null)
  96. var me = this,
  97. re = /^(a|n|d|b|s|o|e)\:(.*)$/,
  98. matches = re.exec(unescape(value)),
  99. all,
  100. type,
  101. keyValue,
  102. values,
  103. vLen,
  104. v;
  105. if(!matches || !matches[1]){
  106. return; // non state
  107. }
  108. type = matches[1];
  109. value = matches[2];
  110. switch (type) {
  111. case 'e':
  112. return null;
  113. case 'n':
  114. return parseFloat(value);
  115. case 'd':
  116. return new Date(Date.parse(value));
  117. case 'b':
  118. return (value == '1');
  119. case 'a':
  120. all = [];
  121. if(value != ''){
  122. values = value.split('^');
  123. vLen = values.length;
  124. for (v = 0; v &lt; vLen; v++) {
  125. value = values[v];
  126. all.push(me.decodeValue(value));
  127. }
  128. }
  129. return all;
  130. case 'o':
  131. all = {};
  132. if(value != ''){
  133. values = value.split('^');
  134. vLen = values.length;
  135. for (v = 0; v &lt; vLen; v++) {
  136. value = values[v];
  137. keyValue = value.split('=');
  138. all[keyValue[0]] = me.decodeValue(keyValue[1]);
  139. }
  140. }
  141. return all;
  142. default:
  143. return value;
  144. }
  145. },
  146. <span id='Ext-state-Provider-method-encodeValue'> /**
  147. </span> * Encodes a value including type information. Decode with {@link #decodeValue}.
  148. * @param {Object} value The value to encode
  149. * @return {String} The encoded value
  150. */
  151. encodeValue : function(value){
  152. var flat = '',
  153. i = 0,
  154. enc,
  155. len,
  156. key;
  157. if (value == null) {
  158. return 'e:1';
  159. } else if(typeof value == 'number') {
  160. enc = 'n:' + value;
  161. } else if(typeof value == 'boolean') {
  162. enc = 'b:' + (value ? '1' : '0');
  163. } else if(Ext.isDate(value)) {
  164. enc = 'd:' + value.toGMTString();
  165. } else if(Ext.isArray(value)) {
  166. for (len = value.length; i &lt; len; i++) {
  167. flat += this.encodeValue(value[i]);
  168. if (i != len - 1) {
  169. flat += '^';
  170. }
  171. }
  172. enc = 'a:' + flat;
  173. } else if (typeof value == 'object') {
  174. for (key in value) {
  175. if (typeof value[key] != 'function' &amp;&amp; value[key] !== undefined) {
  176. flat += key + '=' + this.encodeValue(value[key]) + '^';
  177. }
  178. }
  179. enc = 'o:' + flat.substring(0, flat.length-1);
  180. } else {
  181. enc = 's:' + value;
  182. }
  183. return escape(enc);
  184. }
  185. });</pre>
  186. </body>
  187. </html>