Forked mumble-django project from https://bitbucket.org/Svedrin/mumble-django
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.

215 lines
6.8 KiB

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