PageRenderTime 109ms CodeModel.GetById 26ms RepoModel.GetById 1ms app.codeStats 0ms

/assets/vendor/cc-royalslider-9.2.0/royalslider/dev-js-files/modules/jquery.rs.deeplinking.js

https://bitbucket.org/tredius/tredius.bitbucket.org
JavaScript | 282 lines | 156 code | 58 blank | 68 comment | 30 complexity | bc64422542769707043ca93cfc6cf97b MD5 | raw file
  1. (function($) {
  2. /**
  3. *
  4. * RoyalSlider Deep Linking Module
  5. * @version 1.0.1 + jQuery hashchange plugin v1.3 Copyright (c) 2010 Ben Alman:
  6. *
  7. * 1.0.1:
  8. * - Increased timeout duration before hash changes to 750ms to avoid reloading animation.
  9. */
  10. $.extend($.rsProto, {
  11. _initDeeplinking: function() {
  12. var self = this,
  13. isBlocked,
  14. hashTimeout,
  15. hashChangeTimeout;
  16. self._hashDefaults = {
  17. enabled: false,
  18. change: false,
  19. prefix: ''
  20. };
  21. self.st.deeplinking = $.extend({}, self._hashDefaults, self.st.deeplinking);
  22. if(self.st.deeplinking.enabled) {
  23. var hashChange = self.st.deeplinking.change;
  24. var prefix = '#' + self.st.deeplinking.prefix;
  25. var getSlideIdByHash = function() {
  26. var hash = window.location.hash;
  27. if(hash) {
  28. hash = parseInt( hash.substring(prefix.length), 10 );
  29. if(hash >= 0) {
  30. return hash - 1;
  31. }
  32. }
  33. return -1;
  34. }
  35. var id = getSlideIdByHash();
  36. if(id !== -1) {
  37. self.st.startSlideId = id;
  38. }
  39. if(hashChange) {
  40. $(window).on('hashchange.rs', function(e){
  41. if(!isBlocked) {
  42. var id = getSlideIdByHash();
  43. if(id < 0)
  44. id = 0;
  45. else if(id > self.numSlides - 1)
  46. id = self.numSlides - 1;
  47. self.goTo( id );
  48. }
  49. });
  50. }
  51. self.ev.on('rsAfterSlideChange', function() {
  52. if(hashTimeout) {
  53. clearTimeout(hashTimeout);
  54. }
  55. if(hashChangeTimeout) {
  56. clearTimeout(hashChangeTimeout);
  57. }
  58. hashChangeTimeout = setTimeout(function() {
  59. isBlocked = true;
  60. window.location.hash = prefix + (self.currSlideId + 1);
  61. hashTimeout = setTimeout(function() {
  62. isBlocked = false;
  63. hashTimeout = 0;
  64. }, 60);
  65. }, 750);
  66. });
  67. }
  68. }
  69. });
  70. $.rsModules.deeplinking = $.rsProto._initDeeplinking;
  71. })(jQuery);
  72. /*!
  73. * jQuery hashchange event - v1.3 - 7/21/2010
  74. * http://benalman.com/projects/jquery-hashchange-plugin/
  75. *
  76. * Copyright (c) 2010 "Cowboy" Ben Alman
  77. * Dual licensed under the MIT and GPL licenses.
  78. * http://benalman.com/about/license/
  79. */
  80. (function($,window,undefined){
  81. '$:nomunge'; // Used by YUI compressor.
  82. // Reused string.
  83. var str_hashchange = 'hashchange',
  84. // Method / object references.
  85. doc = document,
  86. fake_onhashchange,
  87. special = $.event.special,
  88. // Does the browser support window.onhashchange? Note that IE8 running in
  89. // IE7 compatibility mode reports true for 'onhashchange' in window, even
  90. // though the event isn't supported, so also test document.documentMode.
  91. doc_mode = doc.documentMode,
  92. supports_onhashchange = 'on' + str_hashchange in window && ( doc_mode === undefined || doc_mode > 7 );
  93. // Get location.hash (or what you'd expect location.hash to be) sans any
  94. // leading #. Thanks for making this necessary, Firefox!
  95. function get_fragment( url ) {
  96. url = url || location.href;
  97. return '#' + url.replace( /^[^#]*#?(.*)$/, '$1' );
  98. };
  99. $.fn[ str_hashchange ] = function( fn ) {
  100. return fn ? this.bind( str_hashchange, fn ) : this.trigger( str_hashchange );
  101. };
  102. $.fn[ str_hashchange ].delay = 50;
  103. special[ str_hashchange ] = $.extend( special[ str_hashchange ], {
  104. // Called only when the first 'hashchange' event is bound to window.
  105. setup: function() {
  106. // If window.onhashchange is supported natively, there's nothing to do..
  107. if ( supports_onhashchange ) { return false; }
  108. // Otherwise, we need to create our own. And we don't want to call this
  109. // until the user binds to the event, just in case they never do, since it
  110. // will create a polling loop and possibly even a hidden Iframe.
  111. $( fake_onhashchange.start );
  112. },
  113. // Called only when the last 'hashchange' event is unbound from window.
  114. teardown: function() {
  115. // If window.onhashchange is supported natively, there's nothing to do..
  116. if ( supports_onhashchange ) { return false; }
  117. // Otherwise, we need to stop ours (if possible).
  118. $( fake_onhashchange.stop );
  119. }
  120. });
  121. // fake_onhashchange does all the work of triggering the window.onhashchange
  122. // event for browsers that don't natively support it, including creating a
  123. // polling loop to watch for hash changes and in IE 6/7 creating a hidden
  124. // Iframe to enable back and forward.
  125. fake_onhashchange = (function(){
  126. var self = {},
  127. timeout_id,
  128. // Remember the initial hash so it doesn't get triggered immediately.
  129. last_hash = get_fragment(),
  130. fn_retval = function(val){ return val; },
  131. history_set = fn_retval,
  132. history_get = fn_retval;
  133. // Start the polling loop.
  134. self.start = function() {
  135. timeout_id || poll();
  136. };
  137. // Stop the polling loop.
  138. self.stop = function() {
  139. timeout_id && clearTimeout( timeout_id );
  140. timeout_id = undefined;
  141. };
  142. // This polling loop checks every $.fn.hashchange.delay milliseconds to see
  143. // if location.hash has changed, and triggers the 'hashchange' event on
  144. // window when necessary.
  145. function poll() {
  146. var hash = get_fragment(),
  147. history_hash = history_get( last_hash );
  148. if ( hash !== last_hash ) {
  149. history_set( last_hash = hash, history_hash );
  150. $(window).trigger( str_hashchange );
  151. } else if ( history_hash !== last_hash ) {
  152. location.href = location.href.replace( /#.*/, '' ) + history_hash;
  153. }
  154. timeout_id = setTimeout( poll, $.fn[ str_hashchange ].delay );
  155. };
  156. $.browser.msie && !supports_onhashchange && (function(){
  157. // Not only do IE6/7 need the "magical" Iframe treatment, but so does IE8
  158. // when running in "IE7 compatibility" mode.
  159. var iframe,
  160. iframe_src;
  161. // When the event is bound and polling starts in IE 6/7, create a hidden
  162. // Iframe for history handling.
  163. self.start = function(){
  164. if ( !iframe ) {
  165. iframe_src = $.fn[ str_hashchange ].src;
  166. iframe_src = iframe_src && iframe_src + get_fragment();
  167. // Create hidden Iframe. Attempt to make Iframe as hidden as possible
  168. // by using techniques from http://www.paciellogroup.com/blog/?p=604.
  169. iframe = $('<iframe tabindex="-1" title="empty"/>').hide()
  170. // When Iframe has completely loaded, initialize the history and
  171. // start polling.
  172. .one( 'load', function(){
  173. iframe_src || history_set( get_fragment() );
  174. poll();
  175. })
  176. // Load Iframe src if specified, otherwise nothing.
  177. .attr( 'src', iframe_src || 'javascript:0' )
  178. // Append Iframe after the end of the body to prevent unnecessary
  179. // initial page scrolling (yes, this works).
  180. .insertAfter( 'body' )[0].contentWindow;
  181. // Whenever `document.title` changes, update the Iframe's title to
  182. // prettify the back/next history menu entries. Since IE sometimes
  183. // errors with "Unspecified error" the very first time this is set
  184. // (yes, very useful) wrap this with a try/catch block.
  185. doc.onpropertychange = function(){
  186. try {
  187. if ( event.propertyName === 'title' ) {
  188. iframe.document.title = doc.title;
  189. }
  190. } catch(e) {}
  191. };
  192. }
  193. };
  194. // Override the "stop" method since an IE6/7 Iframe was created. Even
  195. // if there are no longer any bound event handlers, the polling loop
  196. // is still necessary for back/next to work at all!
  197. self.stop = fn_retval;
  198. // Get history by looking at the hidden Iframe's location.hash.
  199. history_get = function() {
  200. return get_fragment( iframe.location.href );
  201. };
  202. // Set a new history item by opening and then closing the Iframe
  203. // document, *then* setting its location.hash. If document.domain has
  204. // been set, update that as well.
  205. history_set = function( hash, history_hash ) {
  206. var iframe_doc = iframe.document,
  207. domain = $.fn[ str_hashchange ].domain;
  208. if ( hash !== history_hash ) {
  209. // Update Iframe with any initial `document.title` that might be set.
  210. iframe_doc.title = doc.title;
  211. // Opening the Iframe's document after it has been closed is what
  212. // actually adds a history entry.
  213. iframe_doc.open();
  214. // Set document.domain for the Iframe document as well, if necessary.
  215. domain && iframe_doc.write( '<script>document.domain="' + domain + '"</script>' );
  216. iframe_doc.close();
  217. // Update the Iframe's hash, for great justice.
  218. iframe.location.hash = hash;
  219. }
  220. };
  221. })();
  222. return self;
  223. })();
  224. })(jQuery,this);