PageRenderTime 26ms CodeModel.GetById 35ms RepoModel.GetById 0ms app.codeStats 0ms

/upload/talk/symfony2-intro/jquery.history.js

https://github.com/chrux/chrux.github.com
JavaScript | 113 lines | 93 code | 6 blank | 14 comment | 14 complexity | e34fe9dc5c2b54f3298f1308dca148c2 MD5 | raw file
  1. /*
  2. * jQuery history plugin
  3. *
  4. * sample page: http://www.serpere.info/jquery-history-plugin/samples/
  5. *
  6. * Copyright (c) 2006-2009 Taku Sano (Mikage Sawatari)
  7. * Copyright (c) 2010 Takayuki Miwa
  8. * Licensed under the MIT License:
  9. * http://www.opensource.org/licenses/mit-license.php
  10. *
  11. * Modified by Lincoln Cooper to add Safari support and only call the callback once during initialization
  12. * for msie when no initial hash supplied.
  13. */
  14. (function($) {
  15. var locationWrapper = {
  16. put: function(hash, win) {
  17. (win || window).location.hash = encodeURIComponent(hash);
  18. },
  19. get: function(win) {
  20. var hash = ((win || window).location.hash).replace(/^#/, '');
  21. return $.browser.fx ? hash : decodeURIComponent(hash);
  22. }
  23. };
  24. var iframeWrapper = {
  25. id: "__jQuery_history",
  26. init: function() {
  27. var html = '<iframe id="'+ this.id +'" style="display:none" src="javascript:false;" />';
  28. $("body").prepend(html);
  29. return this;
  30. },
  31. _document: function() {
  32. return $("#"+ this.id)[0].contentWindow.document;
  33. },
  34. put: function(hash) {
  35. var doc = this._document();
  36. doc.open();
  37. doc.close();
  38. locationWrapper.put(hash, doc);
  39. },
  40. get: function() {
  41. return locationWrapper.get(this._document());
  42. }
  43. };
  44. // public base interface
  45. var _ = {
  46. appState: undefined,
  47. callback: undefined,
  48. init: function(callback) {},
  49. check: function() {},
  50. load: function(hash) {}
  51. };
  52. $.history = _;
  53. var SimpleImpl = {
  54. init: function(callback) {
  55. _.callback = callback;
  56. var current_hash = locationWrapper.get();
  57. _.appState = current_hash;
  58. _.callback(current_hash);
  59. setInterval(_.check, 100);
  60. },
  61. check: function() {
  62. var current_hash = locationWrapper.get();
  63. if(current_hash != _.appState) {
  64. _.appState = current_hash;
  65. _.callback(current_hash);
  66. }
  67. },
  68. load: function(hash) {
  69. if(hash != _.appState) {
  70. locationWrapper.put(hash);
  71. _.appState = hash;
  72. _.callback(hash);
  73. }
  74. }
  75. };
  76. var IframeImpl = {
  77. init: function(callback) {
  78. _.callback = callback;
  79. var current_hash = locationWrapper.get();
  80. _.appState = current_hash;
  81. iframeWrapper.init().put(current_hash);
  82. _.callback(current_hash);
  83. setInterval(_.check, 100);
  84. },
  85. check: function() {
  86. var current_hash = iframeWrapper.get();
  87. if(current_hash != _.appState) {
  88. locationWrapper.put(current_hash);
  89. _.appState = current_hash;
  90. _.callback(current_hash);
  91. }
  92. },
  93. load: function(hash) {
  94. if(hash != _.appState) {
  95. locationWrapper.put(hash);
  96. iframeWrapper.put(hash);
  97. _.appState = hash;
  98. _.callback(hash);
  99. }
  100. }
  101. };
  102. if($.browser.msie && ($.browser.version < 8 || document.documentMode < 8)) {
  103. $.extend(_, IframeImpl);
  104. } else {
  105. $.extend(_, SimpleImpl);
  106. }
  107. })(jQuery);