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.

234 lines
7.4 KiB

2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. This file is part of Ext JS 3.4
  3. Copyright (c) 2011-2013 Sencha Inc
  4. Contact: http://www.sencha.com/contact
  5. GNU General Public License Usage
  6. This file may be used under the terms of the GNU General Public License version 3.0 as
  7. published by the Free Software Foundation and appearing in the file LICENSE included in the
  8. packaging of this file.
  9. Please review the following information to ensure the GNU General Public License version 3.0
  10. requirements will be met: http://www.gnu.org/copyleft/gpl.html.
  11. If you are unsure which license is appropriate for your use, please contact the sales department
  12. at http://www.sencha.com/contact.
  13. Build date: 2013-04-03 15:07:25
  14. */
  15. /**
  16. * @class Ext.History
  17. * @extends Ext.util.Observable
  18. * History management component that allows you to register arbitrary tokens that signify application
  19. * history state on navigation actions. You can then handle the history {@link #change} event in order
  20. * to reset your application UI to the appropriate state when the user navigates forward or backward through
  21. * the browser history stack.
  22. * @singleton
  23. */
  24. Ext.History = (function () {
  25. var iframe, hiddenField;
  26. var ready = false;
  27. var currentToken;
  28. function getHash() {
  29. var href = location.href, i = href.indexOf("#"),
  30. hash = i >= 0 ? href.substr(i + 1) : null;
  31. if (Ext.isGecko) {
  32. hash = decodeURIComponent(hash);
  33. }
  34. return hash;
  35. }
  36. function doSave() {
  37. hiddenField.value = currentToken;
  38. }
  39. function handleStateChange(token) {
  40. currentToken = token;
  41. Ext.History.fireEvent('change', token);
  42. }
  43. function updateIFrame (token) {
  44. var html = ['<html><body><div id="state">',Ext.util.Format.htmlEncode(token),'</div></body></html>'].join('');
  45. try {
  46. var doc = iframe.contentWindow.document;
  47. doc.open();
  48. doc.write(html);
  49. doc.close();
  50. return true;
  51. } catch (e) {
  52. return false;
  53. }
  54. }
  55. function checkIFrame() {
  56. if (!iframe.contentWindow || !iframe.contentWindow.document) {
  57. setTimeout(checkIFrame, 10);
  58. return;
  59. }
  60. var doc = iframe.contentWindow.document;
  61. var elem = doc.getElementById("state");
  62. var token = elem ? elem.innerText : null;
  63. var hash = getHash();
  64. setInterval(function () {
  65. doc = iframe.contentWindow.document;
  66. elem = doc.getElementById("state");
  67. var newtoken = elem ? elem.innerText : null;
  68. var newHash = getHash();
  69. if (newtoken !== token) {
  70. token = newtoken;
  71. handleStateChange(token);
  72. location.hash = token;
  73. hash = token;
  74. doSave();
  75. } else if (newHash !== hash) {
  76. hash = newHash;
  77. updateIFrame(newHash);
  78. }
  79. }, 50);
  80. ready = true;
  81. Ext.History.fireEvent('ready', Ext.History);
  82. }
  83. function startUp() {
  84. currentToken = hiddenField.value ? hiddenField.value : getHash();
  85. if (Ext.isIE) {
  86. checkIFrame();
  87. } else {
  88. var hash = getHash();
  89. setInterval(function () {
  90. var newHash = getHash();
  91. if (newHash !== hash) {
  92. hash = newHash;
  93. handleStateChange(hash);
  94. doSave();
  95. }
  96. }, 50);
  97. ready = true;
  98. Ext.History.fireEvent('ready', Ext.History);
  99. }
  100. }
  101. return {
  102. /**
  103. * The id of the hidden field required for storing the current history token.
  104. * @type String
  105. * @property
  106. */
  107. fieldId: 'x-history-field',
  108. /**
  109. * The id of the iframe required by IE to manage the history stack.
  110. * @type String
  111. * @property
  112. */
  113. iframeId: 'x-history-frame',
  114. events:{},
  115. /**
  116. * Initialize the global History instance.
  117. * @param {Boolean} onReady (optional) A callback function that will be called once the history
  118. * component is fully initialized.
  119. * @param {Object} scope (optional) The scope (<code>this</code> reference) in which the callback is executed. Defaults to the browser window.
  120. */
  121. init: function (onReady, scope) {
  122. if(ready) {
  123. Ext.callback(onReady, scope, [this]);
  124. return;
  125. }
  126. if(!Ext.isReady){
  127. Ext.onReady(function(){
  128. Ext.History.init(onReady, scope);
  129. });
  130. return;
  131. }
  132. hiddenField = Ext.getDom(Ext.History.fieldId);
  133. if (Ext.isIE) {
  134. iframe = Ext.getDom(Ext.History.iframeId);
  135. }
  136. this.addEvents(
  137. /**
  138. * @event ready
  139. * Fires when the Ext.History singleton has been initialized and is ready for use.
  140. * @param {Ext.History} The Ext.History singleton.
  141. */
  142. 'ready',
  143. /**
  144. * @event change
  145. * Fires when navigation back or forwards within the local page's history occurs.
  146. * @param {String} token An identifier associated with the page state at that point in its history.
  147. */
  148. 'change'
  149. );
  150. if(onReady){
  151. this.on('ready', onReady, scope, {single:true});
  152. }
  153. startUp();
  154. },
  155. /**
  156. * Add a new token to the history stack. This can be any arbitrary value, although it would
  157. * commonly be the concatenation of a component id and another id marking the specifc history
  158. * state of that component. Example usage:
  159. * <pre><code>
  160. // Handle tab changes on a TabPanel
  161. tabPanel.on('tabchange', function(tabPanel, tab){
  162. Ext.History.add(tabPanel.id + ':' + tab.id);
  163. });
  164. </code></pre>
  165. * @param {String} token The value that defines a particular application-specific history state
  166. * @param {Boolean} preventDuplicates When true, if the passed token matches the current token
  167. * it will not save a new history step. Set to false if the same state can be saved more than once
  168. * at the same history stack location (defaults to true).
  169. */
  170. add: function (token, preventDup) {
  171. if(preventDup !== false){
  172. if(this.getToken() == token){
  173. return true;
  174. }
  175. }
  176. if (Ext.isIE) {
  177. return updateIFrame(token);
  178. } else {
  179. location.hash = token;
  180. return true;
  181. }
  182. },
  183. /**
  184. * Programmatically steps back one step in browser history (equivalent to the user pressing the Back button).
  185. */
  186. back: function(){
  187. history.go(-1);
  188. },
  189. /**
  190. * Programmatically steps forward one step in browser history (equivalent to the user pressing the Forward button).
  191. */
  192. forward: function(){
  193. history.go(1);
  194. },
  195. /**
  196. * Retrieves the currently-active history token.
  197. * @return {String} The token
  198. */
  199. getToken: function() {
  200. return ready ? currentToken : getHash();
  201. }
  202. };
  203. })();
  204. Ext.apply(Ext.History, new Ext.util.Observable());