Permalink
Browse files

split out navigation objects into their own files

  • Loading branch information...
1 parent 1437867 commit 62b6daef7999610cc969ab31b16c2b00b29ed12e @johnbender johnbender committed Dec 18, 2012
Showing with 414 additions and 388 deletions.
  1. +2 −0 js/index.php
  2. +129 −0 js/navigation/history.js
  3. +5 −386 js/navigation/navigate.js
  4. +276 −0 js/navigation/navigator.js
  5. +2 −2 js/navigation/path.js
View
@@ -22,6 +22,8 @@
'widgets/loader.js',
'navigation/events/navigate.js',
'navigation/path.js',
+ 'navigation/history.js',
+ 'navigation/navigator.js',
'navigation/navigate.js',
'jquery.mobile.navigation.js',
'jquery.mobile.transition.js',
@@ -0,0 +1,129 @@
+//>>excludeStart("jqmBuildExclude", pragmas.jqmBuildExclude);
+//>>description: History Manager
+//>>label: AJAX Navigation System
+//>>group: Navigation
+define([ "jquery", "./path" ], function( $ ) {
+//>>excludeEnd("jqmBuildExclude");
+
+(function( $ ) {
+ var path = $.mobile.path;
+
+ $.History = function( stack, index, initialDestination ) {
+ this.stack = stack || [];
+ this.activeIndex = index || 0;
+ this.initialDst = initialDestination || path.parseLocation().hash.replace( /^#/, "" );
+ };
+
+ $.extend($.History.prototype, {
+ getActive: function() {
+ return this.stack[ this.activeIndex ];
+ },
+
+ getLast: function() {
+ return this.stack[ this.previousIndex ];
+ },
+
+ getNext: function() {
+ return this.stack[ this.activeIndex + 1 ];
+ },
+
+ getPrev: function() {
+ return this.stack[ this.activeIndex - 1 ];
+ },
+
+ // addNew is used whenever a new page is added
+ add: function( url, data ){
+ data = data || {};
+
+ //if there's forward history, wipe it
+ if ( this.getNext() ) {
+ this.clearForward();
+ }
+
+ // if the hash is included in the data make sure the shape
+ // is consistent for comparison
+ if( data.hash && data.hash.indexOf( "#" ) === -1) {
+ data.hash = "#" + data.hash;
+ }
+
+ data.url = url;
+ this.stack.push( data );
+ this.activeIndex = this.stack.length - 1;
+ },
+
+ //wipe urls ahead of active index
+ clearForward: function() {
+ this.stack = this.stack.slice( 0, this.activeIndex + 1 );
+ },
+
+ find: function( url, stack, earlyReturn ) {
+ stack = stack || this.stack;
+
+ var entry, i, length = stack.length, index;
+
+ for ( i = 0; i < length; i++ ) {
+ entry = stack[i];
+
+ if ( decodeURIComponent(url) === decodeURIComponent(entry.url) ||
+ decodeURIComponent(url) === decodeURIComponent(entry.hash) ) {
+ index = i;
+
+ if( earlyReturn ) {
+ return index;
+ }
+ }
+ }
+
+ return index;
+ },
+
+ closest: function( url ) {
+ var closest, a = this.activeIndex;
+
+ // First, take the slice of the history stack before the current index and search
+ // for a url match. If one is found, we'll avoid avoid looking through forward history
+ // NOTE the preference for backward history movement is driven by the fact that
+ // most mobile browsers only have a dedicated back button, and users rarely use
+ // the forward button in desktop browser anyhow
+ closest = this.find( url, this.stack.slice(0, a) );
+
+ // If nothing was found in backward history check forward. The `true`
+ // value passed as the third parameter causes the find method to break
+ // on the first match in the forward history slice. The starting index
+ // of the slice must then be added to the result to get the element index
+ // in the original history stack :( :(
+ //
+ // TODO this is hyper confusing and should be cleaned up (ugh so bad)
+ if( closest === undefined ) {
+ closest = this.find( url, this.stack.slice(a), true );
+ closest = closest === undefined ? closest : closest + a;
+ }
+
+ return closest;
+ },
+
+ direct: function( opts ) {
+ var newActiveIndex = this.closest( opts.url ), a = this.activeIndex;
+
+ // save new page index, null check to prevent falsey 0 result
+ // record the previous index for reference
+ if( newActiveIndex !== undefined ) {
+ this.activeIndex = newActiveIndex;
+ this.previousIndex = a;
+ }
+
+ // invoke callbacks where appropriate
+ //
+ // TODO this is also convoluted and confusing
+ if ( newActiveIndex < a ) {
+ ( opts.present || opts.back || $.noop )( this.getActive(), 'back' );
+ } else if ( newActiveIndex > a ) {
+ ( opts.present || opts.forward || $.noop )( this.getActive(), 'forward' );
+ } else if ( newActiveIndex === undefined && opts.missing ){
+ opts.missing( this.getActive() );
+ }
+ }
+ });
+
+})( jQuery );
+});
Oops, something went wrong.

0 comments on commit 62b6dae

Please sign in to comment.