forked from pwalczyszyn/jqmNavigator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjqmNavigator-min.js
19 lines (19 loc) · 5.44 KB
/
jqmNavigator-min.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
//////////////////////////////////////////////////////////////////////////////////////
//
// Copyright 2012 Piotr Walczyszyn (http://outof.me | @pwalczyszyn)
// Copyright 2013 Jonathan Nunez ([email protected])
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
//////////////////////////////////////////////////////////////////////////////////////
(function(root,factory){if(typeof define==="function"&&define.amd){define(["jquery"],factory)}else{factory(jQuery)}}(this,function($){$(document).bind("mobileinit",function(){$.mobile.linkBindingEnabled=false;$.mobile.ajaxEnabled=false;$.mobile.hashListeningEnabled=false;$.mobile.pushStateEnabled=false;$.mobile.autoInitializePage=false;$.extend($.mobile,{jqmNavigator:{VERSION:"1.4.0",_containers:[],defaultPageContainer:null,currentView:function(options){var containerViews=this._getPageContainerViews(options);if(containerViews.views.length>0){return containerViews.views[containerViews.views.length-1]}else{return false}},pushView:function(view,options,endCallback){var containerViews=this._getPageContainerViews(options);containerViews.views.push(view);containerViews.pageContainer.append(view.el);view.render();if(endCallback){view.$el.one("pageshow",function(){endCallback()})}if(!$.mobile.firstPage){view.$el.attr("data-role","page");if(!$.mobile.autoInitializePage){$.mobile.initializePage()}}else{containerViews.pageContainer.pagecontainer("change",view.$el,$.extend({role:"page",changeHash:false},options))}},popView:function(options){var containerViews=this._getPageContainerViews(options);if(containerViews.views.length>1){var fromView=containerViews.views.pop();var toView=containerViews.views[containerViews.views.length-1];fromView.$el.one("pagehide",function(){fromView.remove()});containerViews.pageContainer.pagecontainer("change",toView.$el,$.extend({role:"page",reverse:true,changeHash:false},options));return true}else{return false}},popBackNViews:function(N,options){var containerViews=this._getPageContainerViews(options);if(containerViews.views.length>1){if(containerViews.views.length-N>=1){var fromView=containerViews.views.pop();var toView=containerViews.views[containerViews.views.length-N];var removedViews=containerViews.views.splice((containerViews.views.length-N)+1,containerViews.views.length-1);fromView.$el.one("pagehide",function(){removedViews.forEach(function(item){item.remove()},this);fromView.remove()});containerViews.pageContainer.pagecontainer("change",toView.$el,$.extend({role:"page",reverse:true,changeHash:false},options))}else{console.log("Cannot pop first view or below, you can replace it instead!")}}else{console.log("Cannot pop first view, you can replace it instead!")}},popToFirst:function(options){var containerViews=this._getPageContainerViews(options);if(containerViews.views.length>1){var fromView=containerViews.views[containerViews.views.length-1];var toView=containerViews.views[0];var removedViews=containerViews.views.splice(1,containerViews.views.length-1);fromView.$el.one("pagehide",function(){removedViews.forEach(function(item){item.remove()},this)});containerViews.pageContainer.pagecontainer("change",toView.$el,$.extend({role:"page",reverse:true,changeHash:false},options))}else{console.log("Cannot pop first view, you can replace it instead!")}},replaceView:function(view,options,endCallback){var containerViews=this._getPageContainerViews(options);if(containerViews.views.length>=1){var fromView=containerViews.views.pop();fromView.$el.one("pagehide",function(){fromView.remove()});containerViews.views.push(view);containerViews.pageContainer.append(view.el);view.render();if(endCallback){view.$el.one("pageshow",function(){endCallback()})}containerViews.pageContainer.pagecontainer("change",view.$el,$.extend({role:"page",changeHash:false},options))}},replaceAll:function(view,options){var containerViews=this._getPageContainerViews(options);if(containerViews.views.length>=1){var fromView=containerViews.views[containerViews.views.length-1];var removedViews=containerViews.views.splice(0,containerViews.views.length);fromView.$el.one("pagehide",function(){removedViews.forEach(function(item){item.remove()},this)});containerViews.views.push(view);containerViews.pageContainer.append(view.el);view.render();containerViews.pageContainer.pagecontainer("change",view.$el,$.extend({role:"page",changeHash:false},options))}},_getPageContainerViews:function(options){var pageContainer=options&&options.pageContainer?options.pageContainer:$.mobile.pageContainer||this.defaultPageContainer||$("body"),result;this._containers.some(function(item){if(item.pageContainer[0]===pageContainer[0]){result=item;return true}return false},this);if(!result){result={pageContainer:pageContainer,views:[]};this._containers.push(result)}return result},getViews:function jqmNavigator_getViews(pageContainer){var views=null,pc=pageContainer?pageContainer[0]:($.mobile.pageContainer?$.mobile.pageContainer[0]:null);this._containers.some(function(item){if(item.pageContainer[0]===pc){views=item.views;return true}return false},this);return views}}})})}));