Permalink
Browse files

Updating async func and updating demo regarding async

  • Loading branch information...
1 parent 7d86a44 commit 8fefc3a283c281505113f128f66e39559f2b383d @finnsson committed Feb 20, 2013
Showing with 171 additions and 23 deletions.
  1. +2 −2 dist/pager.min.js
  2. +50 −16 pager.js
  3. +21 −0 pagerjs.com/demo/demo.js
  4. +1 −0 pagerjs.com/demo/index.html
  5. +95 −3 pagerjs.com/demo/page/async.html
  6. +2 −2 pagerjs.com/demo/pager.min.js
View
4 dist/pager.min.js
@@ -1,4 +1,4 @@
-/*! pager.js - v0.7.0 - 2013-02-17
+/*! pager.js - v0.7.0 - 2013-02-20
* http://oscar.finnsson.nu/pagerjs/
* Copyright (c) 2013 Oscar Finnsson; Licensed MIT */
-(function(e){var t=e.History,n=function(n,r){"use strict";var i=function(e,t){return function(){var n=arguments;return r.computed(function(){return e.apply(t,n)})}},s={};s.page=null,s.now=function(){return Date.now?Date.now():(new Date).valueOf()},s.extendWithPage=function(e){var t=new s.Page;e.$__page__=t,s.page=t},s.navigationFailed=r.observable(),s.onBindingError=n.Callbacks(),s.onSourceError=n.Callbacks(),s.showChild=function(e){var t=e&&e.length===1&&e[0]===""?[]:e;s.page.showPage(t)},s.getParentPage=function(e){while(e){if(e.$page&&e.$page.val("urlToggle")!=="none")return e.$page;if(e.$data&&e.$data.$__page__)return e.$data.$__page__;e=e.$parentContext}return null};var o=function(e){e.substring(0,s.Href.hash.length)===s.Href.hash&&(e=e.slice(s.Href.hash.length));var t=u(e);s.showChild(t)};s.navigate=function(e){s.useHTML5history?s.Href5.history.pushState(null,null,e):location.hash=e};var u=function(e){return n.map(e.replace(/\+/g," ").split("/"),decodeURIComponent)},a={};a.value=r.utils.unwrapObservable,a.arrayValue=function(e){return n.map(e,function(e){return a.value(e)})};var f=function(e){var t,n={},r=/([^&=]+)=?([^&]*)/g;while(t=r.exec(e))n[t[1]]=t[2];return n},l=function(e){if(!e)return{name:null,params:{}};var t=e.split("?"),n=t[0],r=t[1],i={};return r&&(i=f(r)),{name:n,params:i}};s.ChildManager=function(e,t){this.currentChildO=r.observable(null);var i=this;this.page=t,this.timeStamp=s.now(),this.hideChild=function(){i.currentChild&&i.currentChild.getId()!=="start"&&(i.currentChild.hidePage(function(){}),i.currentChild=null,i.currentChildO(null))},this.showChild=function(t){var r=t.length===0;this.timeStamp=s.now();var o=this.timeStamp,u=i.currentChild;i.currentChild=null;var f=!1,c=l(t[0]),h=c.name,p=null;n.each(e(),function(e,t){if(!f){var n=t.getId(),r=t.getRole();if(n===h||(h===""||h==null)&&(n==="start"||r==="start"))f=!0,i.currentChild=t;n==="?"&&(p=t)}});var d=!1,v=i,m=function(e,t){if(!f){var n=t.getId(),r=t.getValue().modal;if(r){if(n===h||(h===""||h==null)&&n==="start")f=!0,i.currentChild=t,d=!0;n==="?"&&!p&&(p=t,d=!0)}}};while(!i.currentChild&&v.page.parentPage&&!v.page.getValue().modal){var g=v.page.parentPage.children;n.each(g(),m),i.currentChild||(v=v.page.parentPage.childManager)}!i.currentChild&&p&&!r&&(i.currentChild=p),i.currentChild&&(i.currentChildO(i.currentChild),d?i.currentChild.currentParentPage(i.page):i.currentChild.currentParentPage(null));var y=function(){s.navigationFailed&&s.navigationFailed({page:i.page,route:t}),i.page.getValue().navigationFailed&&a.value(i.page.getValue().navigationFailed)(i.page,t)},b=function(){var e=a.value(i.currentChild.getValue().guard);e?e(i.currentChild,t,function(){i.timeStamp===o&&i.currentChild.showPage(t.slice(1),c,t[0])},u):i.currentChild.showPage(t.slice(1),c,t[0])};u&&u===i.currentChild?b():u?u.hidePage(function(){i.currentChild?b():y()}):i.currentChild?b():y()}},s.Page=function(e,t,n,i,o){this.element=e,this.valueAccessor=t,this.allBindingsAccessor=n,this.viewModel=i,this.bindingContext=o,this.children=r.observableArray([]),this.childManager=new s.ChildManager(this.children,this),this.parentPage=null,this.currentId=null,this.getCurrentId=r.observable(),this.ctx=null,this.currentParentPage=r.observable(null),this.isVisible=r.observable(!1),this.originalRoute=r.observable(null),this.route=null};var c=s.Page.prototype;c.val=function(e){return a.value(this.getValue()[e])},c.currentChildPage=function(){return this.childManager.currentChildO},c.find=function(e){var t=a.value(e),r=this;if(t.substring(0,1)==="/")r=s.page,t=t.slice(1);else while(t.substring(0,3)==="../")r=r.currentParentPage&&r.currentParentPage()?r.currentParentPage():r.parentPage,t=t.slice(3);var i=u(t);return n.each(i,function(e,t){r=r.child(t)()}),r},c.find$=function(e){return i(this.find,this)(e)},c.path=function(e){var t=this,r=a.value(e);if(r&&typeof r=="object"&&r.path&&r.params&&!(r instanceof s.Page)){var i=r.path,o=r.params;return t.path(i)+"?"+n.param(o)}var u;if(r==null||r==="")u=t;else{if(!(r instanceof s.Page)){if(r.substring(0,1)==="/")return s.page.getFullRoute()()+r.substring(1);var f=0;while(r.substring(0,3)==="../")f++,r=r.slice(3);var l=t.getFullRoute()(),c=l.slice(0,l.length-f).join("/"),h=(c===""?"":c+"/")+r;return s.useHTML5history?n("base").attr("href")+h:s.Href.hash+h}u=r}var p=u.getFullRoute()().join("/");return s.useHTML5history?n("base").attr("href")+p:s.Href.hash+p},c.path$=function(e){return i(this.path,this)(e)},c.async=function(e,t,n){var r=this;return function(){var i=e();i.done(function(){s.navigate(r.path(t))}),i.fail(function(){s.navigate(r.path(n))})}},c.showPage=function(e,t,n){var r=this,i=r.currentId,s=r.pageRoute?r.pageRoute.params:null,o=r.isVisible();r.currentId=t?t.name||"":"",r.getCurrentId(r.currentId),r.isVisible(!0),n&&r.originalRoute(n),r.route=e,r.pageRoute=t,o?(r.getId()==="?"&&i!==r.currentId&&r.show(),t&&s!==t.params&&r.setParams()):(r.setParams(),r.show()),r.childManager.showChild(e)},c.setParams=function(){if(this.pageRoute&&this.pageRoute.params){var e=this.pageRoute.params,t=this.ctx,i=this.val("params")||{};n.isArray(i)?n.each(i,function(n,i){var s=e[i];t[i]?t[i](s):t[i]=r.observable(s)}):n.each(i,function(n,i){var s=e[n],o;s==null?o=a.value(i):o=s,t[n]?t[n](o):t[n]=r.observable(o)})}if(this.pageRoute){var s=this.getValue().nameParam;s&&(typeof s=="string"?this.ctx[s]?this.ctx[s](this.currentId):this.ctx[s]=r.observable(this.currentId):s(this.currentId))}},c.hidePage=function(e){var t=this;"show"!==t.val("urlToggle")?(t.hideElementWrapper(e),t.childManager.hideChild()):e&&e()};var h=function(e){try{r.applyBindingsToDescendants(e.childBindingContext,e.element)}catch(t){var n=e.val("onBindingError");n&&n(e.e),s.onBindingError.fire({page:e,error:t})}};c.init=function(){var e=this,t=e.val("urlToggle"),i=e.val("id");i!=="?"&&e.getCurrentId(i);var s=r.utils.domData.get(e.element,"__ko_pagerjsBindingData");if(s)return{controlsDescendantBindings:!0};r.utils.domData.set(e.element,"__ko_pagerjsBindingData",e),r.utils.domNodeDisposal.addDisposeCallback(e.element,function(){var t=e.val("beforeRemove");t&&t({page:e}),e.parentPage&&e.parentPage.children.remove(e);var n=e.val("afterRemove");n&&n({page:e})});var o=e.getValue();t!=="none"&&(e.parentPage=e.getParentPage(),e.parentPage.children.push(this),e.hideElement()),e.val("source")&&e.loadSource(e.val("source")),e.ctx=null;if(o.withOnShow)e.ctx={},e.childBindingContext=e.bindingContext.createChildContext(e.ctx),r.utils.extend(e.childBindingContext,{$page:this});else{var u=o["with"]||e.bindingContext.$observableData||e.viewModel;e.ctx=a.value(u),e.augmentContext();if(r.isObservable(u)){var f=r.observable(e.ctx);e.childBindingContext=e.bindingContext.createChildContext(f),r.utils.extend(e.childBindingContext,{$page:this,$observableData:u}),h(e),u.subscribe(function(){f(a.value(u))})}else e.childBindingContext=e.bindingContext.createChildContext(e.ctx),r.utils.extend(e.childBindingContext,{$page:this,$observableData:undefined}),h(e)}if(t!=="none")e.parentPage.route&&e.parentPage.route[0]===e.getId()&&setTimeout(function(){e.parentPage.showPage(e.parentPage.route)},0);else{var l=function(){n(e.element).is(":visible")&&e.showPage([])};setTimeout(l,0),e.getParentPage().isVisible.subscribe(function(e){e&&setTimeout(l,0)})}var c=e.getValue().bind;return r.isObservable(c)&&c(e),{controlsDescendantBindings:!0}},c.augmentContext=function(){var e=this,t=e.val("params");t&&(n.isArray(t)?n.each(t,function(t,n){e.ctx[n]||(e.ctx[n]=r.observable())}):n.each(t,function(n,i){e.ctx[n]||(r.isObservable(i)?e.ctx[n]=i:i===null?(t[n]=r.observable(null),e.ctx[n]=r.observable(null)):e.ctx[n]=r.observable(i))})),this.val("vars")&&n.each(this.val("vars"),function(t,n){r.isObservable(n)?e.ctx[t]=n:e.ctx[t]=r.observable(n)});var i=this.getValue().nameParam;i&&typeof i=="string"&&(e.ctx[i]=r.observable(null)),this.setParams()},c.getValue=function(){return this.valueAccessor?a.value(this.valueAccessor()):{}},c.getParentPage=function(){return s.getParentPage(this.bindingContext)},c.getId=function(){return this.val("id")},c.id=function(){var e=this.getCurrentId();return e==null||e===""?this.getId():e},c.sourceUrl=function(e){var t=this;return this.getId()==="?"?r.computed(function(){var n;return t.val("deep")?n=[t.currentId].concat(t.route).join("/"):n=t.currentId,a.value(e).replace("{1}",n)}):r.computed(function(){return a.value(e)})},c.loadWithOnShow=function(){var e=this;if(!e.withOnShowLoaded||e.val("sourceCache")!==!0)e.withOnShowLoaded=!0,e.val("withOnShow")(function(t){var n=e.bindingContext.createChildContext(t);e.ctx=t,e.childBindingContext=n,e.augmentContext(),r.utils.extend(n,{$page:e}),h(e)},e)},c.loadSource=function(e){var t=this.getValue(),i=this,o=this.element,u=null,f=t.loader||s.loader;if(t.frame==="iframe"){var l=n("iframe",n(o));l.length===0&&(l=n("<iframe></iframe>"),n(o).append(l)),f&&(u=a.value(f)(i,l),u.load()),l.one("load",function(){u&&u.unload(),t.sourceLoaded&&t.sourceLoaded(i)}),r.applyBindingsToNode(l[0],{attr:{src:this.sourceUrl(e)}})}else{f&&(u=a.value(f)(i,i.element),u.load());var c=function(){u&&u.unload(),i.val("withOnShow")?i.val("withOnShow")&&i.loadWithOnShow():h(i),t.sourceLoaded&&t.sourceLoaded(i),i.route&&i.childManager.showChild(i.route)};if(typeof a.value(e)=="string"){var p=a.value(this.sourceUrl(e));d(o,p,function(){c()},i)}else{var v=n(o).children();a.value(e)(this,function(){n.each(v,function(e,t){r.utils.domNodeDisposal.removeNode(t)}),c()})}}};var p=/<script\b[^<]*(?:(?!<\/script>)<[^<]*)*<\/script>/gi,d=function(e,t,i,o){var u,a,f,l=n(e),c=t.indexOf(" ");c>=0&&(u=t.slice(c,t.length),t=t.slice(0,c));var h=jQuery.ajax({url:t,type:"GET",dataType:"html",complete:function(e,t){i&&l.each(i,f||[e.responseText,t,e])}}).done(function(e){f=arguments,n.each(l.children(),function(e,t){r.utils.domNodeDisposal.removeNode(t)}),l.html(u?jQuery("<div>").append(e.replace(p,"")).find(u):e)});return h.fail(function(){o.val("onSourceError")&&o.val("onSourceError")({url:t,page:o,xhrPromise:h}),s.onSourceError.fire({url:t,page:o,xhrPromise:h})}),l};c.show=function(t){var n=this.element,r=this;r.showElementWrapper(t),r.val("title")&&(e.document.title=r.val("title"));if(r.val("sourceOnShow")){if(!r.val("sourceCache")||!n.__pagerLoaded__||typeof r.val("sourceCache")=="number"&&n.__pagerLoaded__+r.val("sourceCache")*1e3<s.now())n.__pagerLoaded__=s.now(),r.loadSource(r.val("sourceOnShow"))}else r.val("withOnShow")&&r.loadWithOnShow()},c.showElementWrapper=function(e){var t=this;t.val("beforeShow")&&t.val("beforeShow")(this),t.showElement(e),t.val("scrollToTop")&&t.element.scrollIntoView(),t.val("afterShow")&&t.val("afterShow")(this)},c.showElement=function(e){this.val("showElement")?this.val("showElement")(this,e):this.val("fx")?s.fx[this.val("fx")].showElement(this,e):s.showElement?s.showElement(this,e):n(this.element).show(e)},c.hideElementWrapper=function(e){this.isVisible(!1),this.val("beforeHide")&&this.val("beforeHide")(this),this.hideElement(e),this.val("afterHide")&&this.val("afterHide")(this)},c.hideElement=function(e){this.val("hideElement")?this.val("hideElement")(this,e):this.val("fx")?s.fx[this.val("fx")].hideElement(this,e):s.hideElement?s.hideElement(this,e):(n(this.element).hide(),e&&e())},c.getFullRoute=function(){return this._fullRoute?this._fullRoute:(this._fullRoute=r.computed(function(){var e=null;return this.currentParentPage&&this.currentParentPage()?(e=this.currentParentPage().getFullRoute()().slice(0),e.push(this.originalRoute()||this.getId()),e):this.parentPage?(e=this.parentPage.getFullRoute()().slice(0),e.push(this.originalRoute()||this.getId()),e):[]},this),this._fullRoute)},c.getRole=function(){return this.val("role")||"next"},c.nullObject=new s.Page,c.nullObject.children=r.observableArray([]),c.child=function(e){var t=this;return t._child==null&&(t._child={}),t._child[e]||(t._child[e]=r.computed(function(){var t=n.grep(this.children(),function(t){return t.id()===e})[0];return t||this.nullObject},this)),t._child[e]},r.bindingHandlers.page={init:function(e,t,n,r,i){var o=null;return a.value(t())instanceof s.Page?(o=a.value(t()),o.element=e,o.allBindingsAccessor==null&&(o.allBindingsAccessor=n),o.viewModel==null&&(o.viewModel=r),o.bindingContext==null&&(o.bindingContext=i)):o=new s.Page(e,t,n,r,i),o.init()}},s.useHTML5history=!1,s.rootURI="/",s.Href=function(e,t,n,i,s){this.element=e,this.bindingContext=s,this.path=r.observable(),this.pageOrRelativePath=r.observable(t)};var v=s.Href.prototype;return v.getParentPage=function(){return s.getParentPage(this.bindingContext)},v.init=function(){var e=this,t=e.getParentPage();e.path=r.computed(function(){var n=a.value(e.pageOrRelativePath()());return t.path(n)})},s.Href.hash="#",v.bind=function(){r.applyBindingsToNode(this.element,{attr:{href:this.path}})},v.update=function(e){this.pageOrRelativePath(e)},s.Href5=function(e,t,n,r,i){s.Href.apply(this,arguments)},s.Href5.prototype=new s.Href,s.Href5.history=e.history,s.Href5.prototype.bind=function(){var e=this;r.applyBindingsToNode(e.element,{attr:{href:e.path},click:function(){var t=e.path();if(t===""||t==="/")t=n("base").attr("href");s.Href5.history.pushState(null,null,t)}})},r.bindingHandlers["page-hash"]={init:function(e,t,n,r,i){var o=new s.Href(e,t,n,r,i);o.init(),o.bind(),e.__ko__page=o},update:function(e,t){e.__ko__page.update(t)}},r.bindingHandlers["page-href5"]={init:function(e,t,n,r,i){var o=new s.Href5(e,t,n,r,i);o.init(),o.bind(),e.__ko__page=o},update:function(e,t){e.__ko__page.update(t)}},r.bindingHandlers["page-href"]={init:function(e,t,n,r,i){var o=s.useHTML5history?s.Href5:s.Href,u=new o(e,t,n,r,i);u.init(),u.bind(),e.__ko__page=u},update:function(e,t){e.__ko__page.update(t)}},s.fx={},s.fx.cssAsync=function(e){return{showElement:function(t,r){var i=n(t.element);i.addClass(e),i.show();var s=setInterval(function(){clearInterval(s),i.addClass(e+"-in")},10),o=setInterval(function(){clearInterval(o),r&&r()},300)},hideElement:function(t,r){var i=n(t.element);if(!t.pageHiddenOnce)t.pageHiddenOnce=!0,i.hide();else{i.removeClass(e+"-in");var s=setInterval(function(){clearInterval(s),r&&r(),i.hide()},300)}}}},s.fx.zoom=s.fx.cssAsync("pagerjs-fx-zoom"),s.fx.flip=s.fx.cssAsync("pagerjs-fx-flip"),s.fx.popout=s.fx.cssAsync("pagerjs-fx-popout-modal"),s.fx.jQuerySync=function(e,t){return{showElement:function(t,r){e.call(n(t.element),300,r)},hideElement:function(e,r){t.call(n(e.element),300,function(){n(e.element).hide()}),r&&r()}}},s.fx.slide=s.fx.jQuerySync(n.fn.slideDown,n.fn.slideUp),s.fx.fade=s.fx.jQuerySync(n.fn.fadeIn,n.fn.fadeOut),s.startHistoryJs=function(r){r&&s.Href5.history.pushState(null,null,r),t.Adapter.bind(e,"statechange",function(){var e=n("base").attr("href"),r=t.getState().url.replace(e,"");o(r)}),t.Adapter.bind(e,"anchorchange",function(){o(location.hash)}),o(t.getState().url.replace(t.getRootUrl(),""))},s.startHashChange=function(t){t&&(e.location.hash=s.Href.hash+t),n(e).hashchange(function(){o(e.location.hash)}),n(e).hashchange()},s.start=function(t){t&&(e.location.hash=s.Href.hash+t);var r=function(){o(e.location.hash)};n(e).bind("hashchange",r),r()},s},r=e.define;typeof r=="function"&&typeof r.amd=="object"&&r.amd?r(["knockout","jquery"],function(e){return n($,e)}):e.pager=n($,ko)})(window);
+(function(e){var t=e.History,n=function(n,r){"use strict";var i=function(e,t){return function(){var n=arguments;return r.computed(function(){return e.apply(t,n)})}},s={};s.page=null,s.now=function(){return Date.now?Date.now():(new Date).valueOf()},s.extendWithPage=function(e){var t=new s.Page;e.$__page__=t,s.page=t},s.navigationFailed=r.observable(),s.onBindingError=n.Callbacks(),s.onSourceError=n.Callbacks(),s.showChild=function(e){var t=e&&e.length===1&&e[0]===""?[]:e;s.page.showPage(t)},s.getParentPage=function(e){while(e){if(e.$page&&e.$page.val("urlToggle")!=="none")return e.$page;if(e.$data&&e.$data.$__page__)return e.$data.$__page__;e=e.$parentContext}return null};var o=null,u=null,a=function(e){u&&u.reject({cancel:!0}),o=null,e.substring(0,s.Href.hash.length)===s.Href.hash&&(e=e.slice(s.Href.hash.length));var t=f(e);s.showChild(t)};s.navigate=function(e){s.useHTML5history?s.Href5.history.pushState(null,null,e):location.hash=e};var f=function(e){return n.map(e.replace(/\+/g," ").split("/"),decodeURIComponent)},l={};l.value=r.utils.unwrapObservable,l.arrayValue=function(e){return n.map(e,function(e){return l.value(e)})};var c=function(e){var t,n={},r=/([^&=]+)=?([^&]*)/g;while(t=r.exec(e))n[t[1]]=t[2];return n},h=function(e){if(!e)return{name:null,params:{}};var t=e.split("?"),n=t[0],r=t[1],i={};return r&&(i=c(r)),{name:n,params:i}};s.ChildManager=function(e,t){this.currentChildO=r.observable(null);var i=this;this.page=t,this.timeStamp=s.now(),this.hideChild=function(){i.currentChild&&i.currentChild.getId()!=="start"&&(i.currentChild.hidePage(function(){}),i.currentChild=null,i.currentChildO(null))},this.showChild=function(t){var r=t.length===0;this.timeStamp=s.now();var o=this.timeStamp,u=i.currentChild;i.currentChild=null;var a=!1,f=h(t[0]),c=f.name,p=null;n.each(e(),function(e,t){if(!a){var n=t.getId(),r=t.getRole();if(n===c||(c===""||c==null)&&(n==="start"||r==="start"))a=!0,i.currentChild=t;n==="?"&&(p=t)}});var d=!1,v=i,m=function(e,t){if(!a){var n=t.getId(),r=t.getValue().modal;if(r){if(n===c||(c===""||c==null)&&n==="start")a=!0,i.currentChild=t,d=!0;n==="?"&&!p&&(p=t,d=!0)}}};while(!i.currentChild&&v.page.parentPage&&!v.page.getValue().modal){var g=v.page.parentPage.children;n.each(g(),m),i.currentChild||(v=v.page.parentPage.childManager)}!i.currentChild&&p&&!r&&(i.currentChild=p),i.currentChild&&(i.currentChildO(i.currentChild),d?i.currentChild.currentParentPage(i.page):i.currentChild.currentParentPage(null));var y=function(){s.navigationFailed&&s.navigationFailed({page:i.page,route:t}),i.page.getValue().navigationFailed&&l.value(i.page.getValue().navigationFailed)(i.page,t)},b=function(){var e=l.value(i.currentChild.getValue().guard);e?e(i.currentChild,t,function(){i.timeStamp===o&&i.currentChild.showPage(t.slice(1),f,t[0])},u):i.currentChild.showPage(t.slice(1),f,t[0])};u&&u===i.currentChild?b():u?u.hidePage(function(){i.currentChild?b():y()}):i.currentChild?b():y()}},s.Page=function(e,t,n,i,o){this.element=e,this.valueAccessor=t,this.allBindingsAccessor=n,this.viewModel=i,this.bindingContext=o,this.children=r.observableArray([]),this.childManager=new s.ChildManager(this.children,this),this.parentPage=null,this.currentId=null,this.getCurrentId=r.observable(),this.ctx=null,this.currentParentPage=r.observable(null),this.isVisible=r.observable(!1),this.originalRoute=r.observable(null),this.route=null};var p=s.Page.prototype;p.val=function(e){return l.value(this.getValue()[e])},p.currentChildPage=function(){return this.childManager.currentChildO},p.find=function(e){var t=l.value(e),r=this;if(t.substring(0,1)==="/")r=s.page,t=t.slice(1);else while(t.substring(0,3)==="../")r=r.currentParentPage&&r.currentParentPage()?r.currentParentPage():r.parentPage,t=t.slice(3);var i=f(t);return n.each(i,function(e,t){r=r.child(t)()}),r},p.find$=function(e){return i(this.find,this)(e)},p.path=function(e){var t=this,r=l.value(e);if(r&&typeof r=="object"&&r.path&&r.params&&!(r instanceof s.Page)){var i=r.path,o=r.params;return t.path(i)+"?"+n.param(o)}var u;if(r==null||r==="")u=t;else{if(!(r instanceof s.Page)){if(r.substring(0,1)==="/")return s.page.getFullRoute()()+r.substring(1);var a=0;while(r.substring(0,3)==="../")a++,r=r.slice(3);var f=t.getFullRoute()(),c=f.slice(0,f.length-a).join("/"),h=(c===""?"":c+"/")+r;return s.useHTML5history?n("base").attr("href")+h:s.Href.hash+h}u=r}var p=u.getFullRoute()().join("/");return s.useHTML5history?n("base").attr("href")+p:s.Href.hash+p},p.path$=function(e){return i(this.path,this)(e)},p.async=function(e,t,n,r){var i=this;return function(){u&&u.reject({cancel:!0});var a=e();u=a,r&&r(a.state());var f=Math.random();o=f,a.done(function(){r&&r(a.state()),f===o&&s.navigate(i.path(t))}),a.fail(function(e){r&&r(a.state());var t=e&&e.cancel;f===o&&!t&&n&&s.navigate(i.path(n))})}},p.showPage=function(e,t,n){var r=this,i=r.currentId,s=r.pageRoute?r.pageRoute.params:null,o=r.isVisible();r.currentId=t?t.name||"":"",r.getCurrentId(r.currentId),r.isVisible(!0),n&&r.originalRoute(n),r.route=e,r.pageRoute=t,o?(r.getId()==="?"&&i!==r.currentId&&r.show(),t&&s!==t.params&&r.setParams()):(r.setParams(),r.show()),r.childManager.showChild(e)},p.setParams=function(){if(this.pageRoute&&this.pageRoute.params){var e=this.pageRoute.params,t=this.ctx,i=this.val("params")||{};n.isArray(i)?n.each(i,function(n,i){var s=e[i];t[i]?t[i](s):t[i]=r.observable(s)}):n.each(i,function(n,i){var s=e[n],o;s==null?o=l.value(i):o=s,t[n]?t[n](o):t[n]=r.observable(o)})}if(this.pageRoute){var s=this.getValue().nameParam;s&&(typeof s=="string"?this.ctx[s]?this.ctx[s](this.currentId):this.ctx[s]=r.observable(this.currentId):s(this.currentId))}},p.hidePage=function(e){var t=this;"show"!==t.val("urlToggle")?(t.hideElementWrapper(e),t.childManager.hideChild()):e&&e()};var d=function(e){try{r.applyBindingsToDescendants(e.childBindingContext,e.element)}catch(t){var n=e.val("onBindingError");n&&n(e.e),s.onBindingError.fire({page:e,error:t})}};p.init=function(){var e=this,t=e.val("urlToggle"),i=e.val("id");i!=="?"&&e.getCurrentId(i);var s=r.utils.domData.get(e.element,"__ko_pagerjsBindingData");if(s)return{controlsDescendantBindings:!0};r.utils.domData.set(e.element,"__ko_pagerjsBindingData",e),r.utils.domNodeDisposal.addDisposeCallback(e.element,function(){var t=e.val("beforeRemove");t&&t({page:e}),e.parentPage&&e.parentPage.children.remove(e);var n=e.val("afterRemove");n&&n({page:e})});var o=e.getValue();t!=="none"&&(e.parentPage=e.getParentPage(),e.parentPage.children.push(this),e.hideElement()),e.val("source")&&e.loadSource(e.val("source")),e.ctx=null;if(o.withOnShow)e.ctx={},e.childBindingContext=e.bindingContext.createChildContext(e.ctx),r.utils.extend(e.childBindingContext,{$page:this});else{var u=o["with"]||e.bindingContext.$observableData||e.viewModel;e.ctx=l.value(u),e.augmentContext();if(r.isObservable(u)){var a=r.observable(e.ctx);e.childBindingContext=e.bindingContext.createChildContext(a),r.utils.extend(e.childBindingContext,{$page:this,$observableData:u}),d(e),u.subscribe(function(){a(l.value(u))})}else e.childBindingContext=e.bindingContext.createChildContext(e.ctx),r.utils.extend(e.childBindingContext,{$page:this,$observableData:undefined}),d(e)}if(t!=="none")e.parentPage.route&&e.parentPage.route[0]===e.getId()&&setTimeout(function(){e.parentPage.showPage(e.parentPage.route)},0);else{var f=function(){n(e.element).is(":visible")&&e.showPage([])};setTimeout(f,0),e.getParentPage().isVisible.subscribe(function(e){e&&setTimeout(f,0)})}var c=e.getValue().bind;return r.isObservable(c)&&c(e),{controlsDescendantBindings:!0}},p.augmentContext=function(){var e=this,t=e.val("params");t&&(n.isArray(t)?n.each(t,function(t,n){e.ctx[n]||(e.ctx[n]=r.observable())}):n.each(t,function(n,i){e.ctx[n]||(r.isObservable(i)?e.ctx[n]=i:i===null?(t[n]=r.observable(null),e.ctx[n]=r.observable(null)):e.ctx[n]=r.observable(i))})),this.val("vars")&&n.each(this.val("vars"),function(t,n){r.isObservable(n)?e.ctx[t]=n:e.ctx[t]=r.observable(n)});var i=this.getValue().nameParam;i&&typeof i=="string"&&(e.ctx[i]=r.observable(null)),this.setParams()},p.getValue=function(){return this.valueAccessor?l.value(this.valueAccessor()):{}},p.getParentPage=function(){return s.getParentPage(this.bindingContext)},p.getId=function(){return this.val("id")},p.id=function(){var e=this.getCurrentId();return e==null||e===""?this.getId():e},p.sourceUrl=function(e){var t=this;return this.getId()==="?"?r.computed(function(){var n;return t.val("deep")?n=[t.currentId].concat(t.route).join("/"):n=t.currentId,l.value(e).replace("{1}",n)}):r.computed(function(){return l.value(e)})},p.loadWithOnShow=function(){var e=this;if(!e.withOnShowLoaded||e.val("sourceCache")!==!0)e.withOnShowLoaded=!0,e.val("withOnShow")(function(t){var n=e.bindingContext.createChildContext(t);e.ctx=t,e.childBindingContext=n,e.augmentContext(),r.utils.extend(n,{$page:e}),d(e)},e)},p.loadSource=function(e){var t=this.getValue(),i=this,o=this.element,u=null,a=t.loader||s.loader;if(t.frame==="iframe"){var f=n("iframe",n(o));f.length===0&&(f=n("<iframe></iframe>"),n(o).append(f)),a&&(u=l.value(a)(i,f),u.load()),f.one("load",function(){u&&u.unload(),t.sourceLoaded&&t.sourceLoaded(i)}),r.applyBindingsToNode(f[0],{attr:{src:this.sourceUrl(e)}})}else{a&&(u=l.value(a)(i,i.element),u.load());var c=function(){u&&u.unload(),i.val("withOnShow")?i.val("withOnShow")&&i.loadWithOnShow():d(i),t.sourceLoaded&&t.sourceLoaded(i),i.route&&i.childManager.showChild(i.route)};if(typeof l.value(e)=="string"){var h=l.value(this.sourceUrl(e));m(o,h,function(){c()},i)}else{var p=n(o).children();l.value(e)(this,function(){n.each(p,function(e,t){r.utils.domNodeDisposal.removeNode(t)}),c()})}}};var v=/<script\b[^<]*(?:(?!<\/script>)<[^<]*)*<\/script>/gi,m=function(e,t,i,o){var u,a,f,l=n(e),c=t.indexOf(" ");c>=0&&(u=t.slice(c,t.length),t=t.slice(0,c));var h=jQuery.ajax({url:t,type:"GET",dataType:"html",complete:function(e,t){i&&l.each(i,f||[e.responseText,t,e])}}).done(function(e){f=arguments,n.each(l.children(),function(e,t){r.utils.domNodeDisposal.removeNode(t)}),l.html(u?jQuery("<div>").append(e.replace(v,"")).find(u):e)});return h.fail(function(){o.val("onSourceError")&&o.val("onSourceError")({url:t,page:o,xhrPromise:h}),s.onSourceError.fire({url:t,page:o,xhrPromise:h})}),l};p.show=function(t){var n=this.element,r=this;r.showElementWrapper(t),r.val("title")&&(e.document.title=r.val("title"));if(r.val("sourceOnShow")){if(!r.val("sourceCache")||!n.__pagerLoaded__||typeof r.val("sourceCache")=="number"&&n.__pagerLoaded__+r.val("sourceCache")*1e3<s.now())n.__pagerLoaded__=s.now(),r.loadSource(r.val("sourceOnShow"))}else r.val("withOnShow")&&r.loadWithOnShow()},p.showElementWrapper=function(e){var t=this;t.val("beforeShow")&&t.val("beforeShow")(this),t.showElement(e),t.val("scrollToTop")&&t.element.scrollIntoView(),t.val("afterShow")&&t.val("afterShow")(this)},p.showElement=function(e){this.val("showElement")?this.val("showElement")(this,e):this.val("fx")?s.fx[this.val("fx")].showElement(this,e):s.showElement?s.showElement(this,e):n(this.element).show(e)},p.hideElementWrapper=function(e){this.isVisible(!1),this.val("beforeHide")&&this.val("beforeHide")(this),this.hideElement(e),this.val("afterHide")&&this.val("afterHide")(this)},p.hideElement=function(e){this.val("hideElement")?this.val("hideElement")(this,e):this.val("fx")?s.fx[this.val("fx")].hideElement(this,e):s.hideElement?s.hideElement(this,e):(n(this.element).hide(),e&&e())},p.getFullRoute=function(){return this._fullRoute?this._fullRoute:(this._fullRoute=r.computed(function(){var e=null;return this.currentParentPage&&this.currentParentPage()?(e=this.currentParentPage().getFullRoute()().slice(0),e.push(this.originalRoute()||this.getId()),e):this.parentPage?(e=this.parentPage.getFullRoute()().slice(0),e.push(this.originalRoute()||this.getId()),e):[]},this),this._fullRoute)},p.getRole=function(){return this.val("role")||"next"},p.nullObject=new s.Page,p.nullObject.children=r.observableArray([]),p.child=function(e){var t=this;return t._child==null&&(t._child={}),t._child[e]||(t._child[e]=r.computed(function(){var t=n.grep(this.children(),function(t){return t.id()===e})[0];return t||this.nullObject},this)),t._child[e]},r.bindingHandlers.page={init:function(e,t,n,r,i){var o=null;return l.value(t())instanceof s.Page?(o=l.value(t()),o.element=e,o.allBindingsAccessor==null&&(o.allBindingsAccessor=n),o.viewModel==null&&(o.viewModel=r),o.bindingContext==null&&(o.bindingContext=i)):o=new s.Page(e,t,n,r,i),o.init()}},s.useHTML5history=!1,s.rootURI="/",s.Href=function(e,t,n,i,s){this.element=e,this.bindingContext=s,this.path=r.observable(),this.pageOrRelativePath=r.observable(t)};var g=s.Href.prototype;return g.getParentPage=function(){return s.getParentPage(this.bindingContext)},g.init=function(){var e=this,t=e.getParentPage();e.path=r.computed(function(){var n=l.value(e.pageOrRelativePath()());return t.path(n)})},s.Href.hash="#",g.bind=function(){r.applyBindingsToNode(this.element,{attr:{href:this.path}})},g.update=function(e){this.pageOrRelativePath(e)},s.Href5=function(e,t,n,r,i){s.Href.apply(this,arguments)},s.Href5.prototype=new s.Href,s.Href5.history=e.history,s.Href5.prototype.bind=function(){var e=this;r.applyBindingsToNode(e.element,{attr:{href:e.path},click:function(){var t=e.path();if(t===""||t==="/")t=n("base").attr("href");s.Href5.history.pushState(null,null,t)}})},r.bindingHandlers["page-hash"]={init:function(e,t,n,r,i){var o=new s.Href(e,t,n,r,i);o.init(),o.bind(),e.__ko__page=o},update:function(e,t){e.__ko__page.update(t)}},r.bindingHandlers["page-href5"]={init:function(e,t,n,r,i){var o=new s.Href5(e,t,n,r,i);o.init(),o.bind(),e.__ko__page=o},update:function(e,t){e.__ko__page.update(t)}},r.bindingHandlers["page-href"]={init:function(e,t,n,r,i){var o=s.useHTML5history?s.Href5:s.Href,u=new o(e,t,n,r,i);u.init(),u.bind(),e.__ko__page=u},update:function(e,t){e.__ko__page.update(t)}},s.fx={},s.fx.cssAsync=function(e){return{showElement:function(t,r){var i=n(t.element);i.addClass(e),i.show();var s=setInterval(function(){clearInterval(s),i.addClass(e+"-in")},10),o=setInterval(function(){clearInterval(o),r&&r()},300)},hideElement:function(t,r){var i=n(t.element);if(!t.pageHiddenOnce)t.pageHiddenOnce=!0,i.hide();else{i.removeClass(e+"-in");var s=setInterval(function(){clearInterval(s),r&&r(),i.hide()},300)}}}},s.fx.zoom=s.fx.cssAsync("pagerjs-fx-zoom"),s.fx.flip=s.fx.cssAsync("pagerjs-fx-flip"),s.fx.popout=s.fx.cssAsync("pagerjs-fx-popout-modal"),s.fx.jQuerySync=function(e,t){return{showElement:function(t,r){e.call(n(t.element),300,r)},hideElement:function(e,r){t.call(n(e.element),300,function(){n(e.element).hide()}),r&&r()}}},s.fx.slide=s.fx.jQuerySync(n.fn.slideDown,n.fn.slideUp),s.fx.fade=s.fx.jQuerySync(n.fn.fadeIn,n.fn.fadeOut),s.startHistoryJs=function(r){r&&s.Href5.history.pushState(null,null,r),t.Adapter.bind(e,"statechange",function(){var e=n("base").attr("href"),r=t.getState().url.replace(e,"");a(r)}),t.Adapter.bind(e,"anchorchange",function(){a(location.hash)}),a(t.getState().url.replace(t.getRootUrl(),""))},s.startHashChange=function(t){t&&(e.location.hash=s.Href.hash+t),n(e).hashchange(function(){a(e.location.hash)}),n(e).hashchange()},s.start=function(t){t&&(e.location.hash=s.Href.hash+t);var r=function(){a(e.location.hash)};n(e).bind("hashchange",r),r()},s},r=e.define;typeof r=="function"&&typeof r.amd=="object"&&r.amd?r(["knockout","jquery"],function(e){return n($,e)}):e.pager=n($,ko)})(window);
View
66 pager.js
@@ -95,13 +95,24 @@
return null;
};
+ // set this to a random value in order to verify that the navigation should happen
+ // Is cleaned after every goTo.
+ var goToKey = null;
+
+ var currentAsyncDeferred = null;
+
/**
*
* Takes a complete, working, path as parameter. *Not* a route, relative route or Page-object.
*
* @param {String} path
*/
var goTo = function (path) {
+ // reject any async navigation in progress
+ if(currentAsyncDeferred) {
+ currentAsyncDeferred.reject({cancel: true});
+ }
+ goToKey = null;
// strip # (or #!/)
if (path.substring(0, pager.Href.hash.length) === pager.Href.hash) {
path = path.slice(pager.Href.hash.length);
@@ -524,20 +535,43 @@
/**
*
- * @param {Function} fn should return a $.Promise.
+ * @param {Function} fn should return a $.Deferred (NOT a promise since async should be able to reject it).
* @param {String/Object} ok route (e.g. '/some/path' or '../some/path'). Should not contain '#!/'.
* @param {String/Object} notOk route (e.g. '/some/path' or '../some/path'). Should not contain '#!/'.
* @return {Function}
*/
- p.async = function (fn, ok, notOk) {
+ p.async = function (fn, ok, notOk, state) {
var me = this;
return function () {
+ if(currentAsyncDeferred) {
+ currentAsyncDeferred.reject({cancel: true});
+ }
var result = fn();
+ currentAsyncDeferred = result;
+ if(state) {
+ state(result.state());
+ }
+ var key = Math.random();
+ goToKey = key;
+
result.done(function () {
- pager.navigate(me.path(ok));
+ if(state) {
+ state(result.state());
+ }
+ if (key === goToKey) {
+ pager.navigate(me.path(ok));
+ }
});
- result.fail(function () {
- pager.navigate(me.path(notOk));
+ result.fail(function (data) {
+ if(state) {
+ state(result.state());
+ }
+ var cancel = data && data.cancel;
+ if (key === goToKey) {
+ if (!cancel && notOk) {
+ pager.navigate(me.path(notOk));
+ }
+ }
});
};
};
@@ -690,18 +724,18 @@
ko.utils.domNodeDisposal.addDisposeCallback(m.element, function () {
// then remove this Page-instance
var beforeRemove = m.val('beforeRemove');
- if(beforeRemove) {
+ if (beforeRemove) {
beforeRemove({
- page: m
+ page:m
});
}
if (m.parentPage) {
m.parentPage.children.remove(m);
}
var afterRemove = m.val('afterRemove');
- if(afterRemove) {
+ if (afterRemove) {
afterRemove({
- page: m
+ page:m
});
}
});
@@ -776,7 +810,7 @@
}
// Bind the page to the config property `bind` if it exists
var bind = m.getValue()['bind'];
- if(ko.isObservable(bind)) {
+ if (ko.isObservable(bind)) {
bind(m);
}
@@ -975,7 +1009,7 @@
} else { // should be a method
var childrenToRemove = $(element).children();
_ko.value(source)(this, function () {
- $.each(childrenToRemove, function(i,c) {
+ $.each(childrenToRemove, function (i, c) {
ko.utils.domNodeDisposal.removeNode(c);
});
onLoad();
@@ -1083,7 +1117,7 @@
me.val('beforeShow')(this);
}
me.showElement(callback);
- if(me.val('scrollToTop')) {
+ if (me.val('scrollToTop')) {
me.element.scrollIntoView();
}
if (me.val('afterShow')) {
@@ -1222,16 +1256,16 @@
ko.bindingHandlers.page = {
init:function (element, valueAccessor, allBindingsAccessor, viewModel, bindingContext) {
var page = null;
- if(_ko.value(valueAccessor()) instanceof pager.Page) {
+ if (_ko.value(valueAccessor()) instanceof pager.Page) {
page = _ko.value(valueAccessor());
page.element = element;
- if(page.allBindingsAccessor == null) {
+ if (page.allBindingsAccessor == null) {
page.allBindingsAccessor = allBindingsAccessor;
}
- if(page.viewModel == null) {
+ if (page.viewModel == null) {
page.viewModel = viewModel;
}
- if(page.bindingContext == null) {
+ if (page.bindingContext == null) {
page.bindingContext = bindingContext;
}
} else {
View
21 pagerjs.com/demo/demo.js
@@ -75,6 +75,27 @@ requirejs(['jquery', 'knockout', 'underscore', 'pager', 'bootstrap', 'hashchange
};
var viewModel = {
+ page: {
+ async: {
+ // wait 2 secs before returning ok
+ wait2: function() {
+ var d = $.Deferred();
+ setTimeout(function() {
+ d.resolve();
+ }, 2000);
+ return d;
+ },
+ wait2Fail: function() {
+ var d = $.Deferred();
+ setTimeout(function() {
+ d.reject();
+ }, 2000);
+ return d;
+ },
+ okIsLoading:ko.observable(),
+ notOkIsLoading: ko.observable()
+ }
+ },
question: ko.observable('How many roads must a man walk down before you can call him a man?'),
closePage: function(page) {
return function() {
View
1 pagerjs.com/demo/index.html
@@ -101,6 +101,7 @@
<div data-bind="page: {id: 'deep_navigation_with_wildcards', title: 'Deep Navigation with Wildcards', scrollToTop: true, sourceCache: true, sourceOnShow: 'page/deep_navigation_with_wildcards.html'}"></div>
<div data-bind="page: {id: 'custom_js_when_navigating', title: 'Custom JS when Navigating', desc: 'reacting to navigation', scrollToTop: true, sourceCache: true, sourceOnShow: 'page/custom_js_when_navigating.html'}"></div>
<div data-bind="page: {id: 'failed_navigation', title: 'Failed Navigation', scrollToTop: true, sourceCache: true, sourceOnShow: 'page/failed_navigation.html'}"></div>
+ <div data-bind="page: {id: 'async', with: page.async, title: 'Async', desc: 'handling asynchronous navigation', scrollToTop: true, sourceCache: true, sourceOnShow: 'page/async.html'}"></div>
<div data-bind="page: {id: 'guards', title: 'Guards', desc: 'conditions when navigating to a page is ok', scrollToTop: true, sourceCache: true, sourceOnShow: 'page/guards.html'}"></div>
<div data-bind="page: {id: '?', title: '404 - Single Page Not Found', scrollToTop: true, sourceCache: true, sourceOnShow: 'page/404.html'}"></div>
</div>
View
98 pagerjs.com/demo/page/async.html
@@ -2,11 +2,103 @@
<h1>Async Click</h1>
<p class="lead">
- The web is
+ The web is asynchronous. Being able to handle this in a SPA is important.
+ Using <code>$page.async(Function,String/Page,[String/Page],[Observable])</code> makes it trivial
+ in your SPA.
</p>
+
+ <p class="lead">
+ The function <code>$page.async</code> got some interesting conditions.
+ </p>
+ <ul>
+ <li>
+ The first argument should be a function that returns a <code>$.Deferred</code>, <b>NOT</b>
+ a <code>$.Promise</code>. The reason is that the global navigation handler must be able to
+ <code>#reject</code> the deferred. Other listeners to the deferred should be able to handle
+ that it is fed with the data <code>{cancel: true}</code>.
+ </li>
+ <li>
+ The second argument should be a path (String), a page or an observable path or page.
+ This argument will be used to resolve the navigation path if the deferred is resolved.
+ </li>
+ <li>
+ The third argument is optional and can be <code>null</code> or <code>undefined</code>.
+ If it is defined it should have the same properties and the second argument (i.e. being
+ a path or page). It will decide which navigation to use if the deferred is rejected.
+ </li>
+ <li>
+ The fourth and final argument is also optional.
+ It should be an observable. The observables value will be updated to the state of
+ the deferred object returned by the first argument.
+ When the async function is called it will be set to <code>pending</code>.
+ If the deferred is resolved it will be set to <code>resolved</code>.
+ If the deferred is rejected it will be set to <code>rejected</code>.
+ See <a href="http://api.jquery.com/deferred.state/">deferred.state()</a> in the
+ jQuery documentation.
+ </li>
+ </ul>
</header>
+<p>
+ In this example we got two buttons (one will be resolve and one will be rejected).
+ For brevity only one buttons model and markup is presented.
+</p>
+
+<pre data-bind="prettyprint: {}" class="prettyprint linenums">
+viewModel = {
+ // wait 2 secs before returning ok
+ wait2: function() {
+ var d = $.Deferred();
+ setTimeout(function() {
+ d.resolve();
+ }, 2000);
+ return d;
+ },
+ okIsLoading:ko.observable()
+};
+</pre>
+
+<p>and the markup</p>
+
+<pre data-bind="prettyprint: {}" class="prettyprint linenums">
+&lt;a class="btn" data-bind="click: $page.async(wait2,'ok','notok',okIsLoading)"&gt;
+ Show OK page after 2 secs
+ &lt;!-- ko if: (okIsLoading() == 'pending') --&gt;
+ &lt;img src="small-ajax-loader.gif"/&gt;
+ &lt;!-- /ko --&gt;
+&lt;/a&gt;
+</pre>
-<pre data-bind="prettyprint: {}" class="prettyprint linenums">
- </pre>
+<p>
+ Click on both buttons.
+</p>
+<a class="btn" data-bind="click: $page.async(wait2,'ok','notok',okIsLoading)">Show OK page after 2 secs
+ <!-- ko if: (okIsLoading() == 'pending') -->
+ <img src="small-ajax-loader.gif"/>
+ <!-- /ko -->
+</a>
+<br/>
+<br/>
+<a class="btn" data-bind="click: $page.async(wait2Fail,'ok','notok',notOkIsLoading)">Show NOT OK page after 2 secs
+ <!-- ko if: (notOkIsLoading() == 'pending') -->
+ <img src="small-ajax-loader.gif"/>
+ <!-- /ko -->
+</a>
+<br/>
+
+<div data-bind="page: {id: 'ok'}" class="well">
+ <h3>OK Page</h3>
+
+ <p>
+ The hash is now changed to <code>#!/navigation/async/ok</code>.
+ </p>
+</div>
+
+<div data-bind="page: {id: 'notok'}" class="well">
+ <h3>Not OK Page</h3>
+
+ <p>
+ The hash is now changed to <code>#!/navigation/async/notok</code>.
+ </p>
+</div>
View
4 pagerjs.com/demo/pager.min.js
@@ -1,4 +1,4 @@
-/*! pager.js - v0.7.0 - 2013-02-17
+/*! pager.js - v0.7.0 - 2013-02-20
* http://oscar.finnsson.nu/pagerjs/
* Copyright (c) 2013 Oscar Finnsson; Licensed MIT */
-(function(e){var t=e.History,n=function(n,r){"use strict";var i=function(e,t){return function(){var n=arguments;return r.computed(function(){return e.apply(t,n)})}},s={};s.page=null,s.now=function(){return Date.now?Date.now():(new Date).valueOf()},s.extendWithPage=function(e){var t=new s.Page;e.$__page__=t,s.page=t},s.navigationFailed=r.observable(),s.onBindingError=n.Callbacks(),s.onSourceError=n.Callbacks(),s.showChild=function(e){var t=e&&e.length===1&&e[0]===""?[]:e;s.page.showPage(t)},s.getParentPage=function(e){while(e){if(e.$page&&e.$page.val("urlToggle")!=="none")return e.$page;if(e.$data&&e.$data.$__page__)return e.$data.$__page__;e=e.$parentContext}return null};var o=function(e){e.substring(0,s.Href.hash.length)===s.Href.hash&&(e=e.slice(s.Href.hash.length));var t=u(e);s.showChild(t)};s.navigate=function(e){s.useHTML5history?s.Href5.history.pushState(null,null,e):location.hash=e};var u=function(e){return n.map(e.replace(/\+/g," ").split("/"),decodeURIComponent)},a={};a.value=r.utils.unwrapObservable,a.arrayValue=function(e){return n.map(e,function(e){return a.value(e)})};var f=function(e){var t,n={},r=/([^&=]+)=?([^&]*)/g;while(t=r.exec(e))n[t[1]]=t[2];return n},l=function(e){if(!e)return{name:null,params:{}};var t=e.split("?"),n=t[0],r=t[1],i={};return r&&(i=f(r)),{name:n,params:i}};s.ChildManager=function(e,t){this.currentChildO=r.observable(null);var i=this;this.page=t,this.timeStamp=s.now(),this.hideChild=function(){i.currentChild&&i.currentChild.getId()!=="start"&&(i.currentChild.hidePage(function(){}),i.currentChild=null,i.currentChildO(null))},this.showChild=function(t){var r=t.length===0;this.timeStamp=s.now();var o=this.timeStamp,u=i.currentChild;i.currentChild=null;var f=!1,c=l(t[0]),h=c.name,p=null;n.each(e(),function(e,t){if(!f){var n=t.getId(),r=t.getRole();if(n===h||(h===""||h==null)&&(n==="start"||r==="start"))f=!0,i.currentChild=t;n==="?"&&(p=t)}});var d=!1,v=i,m=function(e,t){if(!f){var n=t.getId(),r=t.getValue().modal;if(r){if(n===h||(h===""||h==null)&&n==="start")f=!0,i.currentChild=t,d=!0;n==="?"&&!p&&(p=t,d=!0)}}};while(!i.currentChild&&v.page.parentPage&&!v.page.getValue().modal){var g=v.page.parentPage.children;n.each(g(),m),i.currentChild||(v=v.page.parentPage.childManager)}!i.currentChild&&p&&!r&&(i.currentChild=p),i.currentChild&&(i.currentChildO(i.currentChild),d?i.currentChild.currentParentPage(i.page):i.currentChild.currentParentPage(null));var y=function(){s.navigationFailed&&s.navigationFailed({page:i.page,route:t}),i.page.getValue().navigationFailed&&a.value(i.page.getValue().navigationFailed)(i.page,t)},b=function(){var e=a.value(i.currentChild.getValue().guard);e?e(i.currentChild,t,function(){i.timeStamp===o&&i.currentChild.showPage(t.slice(1),c,t[0])},u):i.currentChild.showPage(t.slice(1),c,t[0])};u&&u===i.currentChild?b():u?u.hidePage(function(){i.currentChild?b():y()}):i.currentChild?b():y()}},s.Page=function(e,t,n,i,o){this.element=e,this.valueAccessor=t,this.allBindingsAccessor=n,this.viewModel=i,this.bindingContext=o,this.children=r.observableArray([]),this.childManager=new s.ChildManager(this.children,this),this.parentPage=null,this.currentId=null,this.getCurrentId=r.observable(),this.ctx=null,this.currentParentPage=r.observable(null),this.isVisible=r.observable(!1),this.originalRoute=r.observable(null),this.route=null};var c=s.Page.prototype;c.val=function(e){return a.value(this.getValue()[e])},c.currentChildPage=function(){return this.childManager.currentChildO},c.find=function(e){var t=a.value(e),r=this;if(t.substring(0,1)==="/")r=s.page,t=t.slice(1);else while(t.substring(0,3)==="../")r=r.currentParentPage&&r.currentParentPage()?r.currentParentPage():r.parentPage,t=t.slice(3);var i=u(t);return n.each(i,function(e,t){r=r.child(t)()}),r},c.find$=function(e){return i(this.find,this)(e)},c.path=function(e){var t=this,r=a.value(e);if(r&&typeof r=="object"&&r.path&&r.params&&!(r instanceof s.Page)){var i=r.path,o=r.params;return t.path(i)+"?"+n.param(o)}var u;if(r==null||r==="")u=t;else{if(!(r instanceof s.Page)){if(r.substring(0,1)==="/")return s.page.getFullRoute()()+r.substring(1);var f=0;while(r.substring(0,3)==="../")f++,r=r.slice(3);var l=t.getFullRoute()(),c=l.slice(0,l.length-f).join("/"),h=(c===""?"":c+"/")+r;return s.useHTML5history?n("base").attr("href")+h:s.Href.hash+h}u=r}var p=u.getFullRoute()().join("/");return s.useHTML5history?n("base").attr("href")+p:s.Href.hash+p},c.path$=function(e){return i(this.path,this)(e)},c.async=function(e,t,n){var r=this;return function(){var i=e();i.done(function(){s.navigate(r.path(t))}),i.fail(function(){s.navigate(r.path(n))})}},c.showPage=function(e,t,n){var r=this,i=r.currentId,s=r.pageRoute?r.pageRoute.params:null,o=r.isVisible();r.currentId=t?t.name||"":"",r.getCurrentId(r.currentId),r.isVisible(!0),n&&r.originalRoute(n),r.route=e,r.pageRoute=t,o?(r.getId()==="?"&&i!==r.currentId&&r.show(),t&&s!==t.params&&r.setParams()):(r.setParams(),r.show()),r.childManager.showChild(e)},c.setParams=function(){if(this.pageRoute&&this.pageRoute.params){var e=this.pageRoute.params,t=this.ctx,i=this.val("params")||{};n.isArray(i)?n.each(i,function(n,i){var s=e[i];t[i]?t[i](s):t[i]=r.observable(s)}):n.each(i,function(n,i){var s=e[n],o;s==null?o=a.value(i):o=s,t[n]?t[n](o):t[n]=r.observable(o)})}if(this.pageRoute){var s=this.getValue().nameParam;s&&(typeof s=="string"?this.ctx[s]?this.ctx[s](this.currentId):this.ctx[s]=r.observable(this.currentId):s(this.currentId))}},c.hidePage=function(e){var t=this;"show"!==t.val("urlToggle")?(t.hideElementWrapper(e),t.childManager.hideChild()):e&&e()};var h=function(e){try{r.applyBindingsToDescendants(e.childBindingContext,e.element)}catch(t){var n=e.val("onBindingError");n&&n(e.e),s.onBindingError.fire({page:e,error:t})}};c.init=function(){var e=this,t=e.val("urlToggle"),i=e.val("id");i!=="?"&&e.getCurrentId(i);var s=r.utils.domData.get(e.element,"__ko_pagerjsBindingData");if(s)return{controlsDescendantBindings:!0};r.utils.domData.set(e.element,"__ko_pagerjsBindingData",e),r.utils.domNodeDisposal.addDisposeCallback(e.element,function(){var t=e.val("beforeRemove");t&&t({page:e}),e.parentPage&&e.parentPage.children.remove(e);var n=e.val("afterRemove");n&&n({page:e})});var o=e.getValue();t!=="none"&&(e.parentPage=e.getParentPage(),e.parentPage.children.push(this),e.hideElement()),e.val("source")&&e.loadSource(e.val("source")),e.ctx=null;if(o.withOnShow)e.ctx={},e.childBindingContext=e.bindingContext.createChildContext(e.ctx),r.utils.extend(e.childBindingContext,{$page:this});else{var u=o["with"]||e.bindingContext.$observableData||e.viewModel;e.ctx=a.value(u),e.augmentContext();if(r.isObservable(u)){var f=r.observable(e.ctx);e.childBindingContext=e.bindingContext.createChildContext(f),r.utils.extend(e.childBindingContext,{$page:this,$observableData:u}),h(e),u.subscribe(function(){f(a.value(u))})}else e.childBindingContext=e.bindingContext.createChildContext(e.ctx),r.utils.extend(e.childBindingContext,{$page:this,$observableData:undefined}),h(e)}if(t!=="none")e.parentPage.route&&e.parentPage.route[0]===e.getId()&&setTimeout(function(){e.parentPage.showPage(e.parentPage.route)},0);else{var l=function(){n(e.element).is(":visible")&&e.showPage([])};setTimeout(l,0),e.getParentPage().isVisible.subscribe(function(e){e&&setTimeout(l,0)})}var c=e.getValue().bind;return r.isObservable(c)&&c(e),{controlsDescendantBindings:!0}},c.augmentContext=function(){var e=this,t=e.val("params");t&&(n.isArray(t)?n.each(t,function(t,n){e.ctx[n]||(e.ctx[n]=r.observable())}):n.each(t,function(n,i){e.ctx[n]||(r.isObservable(i)?e.ctx[n]=i:i===null?(t[n]=r.observable(null),e.ctx[n]=r.observable(null)):e.ctx[n]=r.observable(i))})),this.val("vars")&&n.each(this.val("vars"),function(t,n){r.isObservable(n)?e.ctx[t]=n:e.ctx[t]=r.observable(n)});var i=this.getValue().nameParam;i&&typeof i=="string"&&(e.ctx[i]=r.observable(null)),this.setParams()},c.getValue=function(){return this.valueAccessor?a.value(this.valueAccessor()):{}},c.getParentPage=function(){return s.getParentPage(this.bindingContext)},c.getId=function(){return this.val("id")},c.id=function(){var e=this.getCurrentId();return e==null||e===""?this.getId():e},c.sourceUrl=function(e){var t=this;return this.getId()==="?"?r.computed(function(){var n;return t.val("deep")?n=[t.currentId].concat(t.route).join("/"):n=t.currentId,a.value(e).replace("{1}",n)}):r.computed(function(){return a.value(e)})},c.loadWithOnShow=function(){var e=this;if(!e.withOnShowLoaded||e.val("sourceCache")!==!0)e.withOnShowLoaded=!0,e.val("withOnShow")(function(t){var n=e.bindingContext.createChildContext(t);e.ctx=t,e.childBindingContext=n,e.augmentContext(),r.utils.extend(n,{$page:e}),h(e)},e)},c.loadSource=function(e){var t=this.getValue(),i=this,o=this.element,u=null,f=t.loader||s.loader;if(t.frame==="iframe"){var l=n("iframe",n(o));l.length===0&&(l=n("<iframe></iframe>"),n(o).append(l)),f&&(u=a.value(f)(i,l),u.load()),l.one("load",function(){u&&u.unload(),t.sourceLoaded&&t.sourceLoaded(i)}),r.applyBindingsToNode(l[0],{attr:{src:this.sourceUrl(e)}})}else{f&&(u=a.value(f)(i,i.element),u.load());var c=function(){u&&u.unload(),i.val("withOnShow")?i.val("withOnShow")&&i.loadWithOnShow():h(i),t.sourceLoaded&&t.sourceLoaded(i),i.route&&i.childManager.showChild(i.route)};if(typeof a.value(e)=="string"){var p=a.value(this.sourceUrl(e));d(o,p,function(){c()},i)}else{var v=n(o).children();a.value(e)(this,function(){n.each(v,function(e,t){r.utils.domNodeDisposal.removeNode(t)}),c()})}}};var p=/<script\b[^<]*(?:(?!<\/script>)<[^<]*)*<\/script>/gi,d=function(e,t,i,o){var u,a,f,l=n(e),c=t.indexOf(" ");c>=0&&(u=t.slice(c,t.length),t=t.slice(0,c));var h=jQuery.ajax({url:t,type:"GET",dataType:"html",complete:function(e,t){i&&l.each(i,f||[e.responseText,t,e])}}).done(function(e){f=arguments,n.each(l.children(),function(e,t){r.utils.domNodeDisposal.removeNode(t)}),l.html(u?jQuery("<div>").append(e.replace(p,"")).find(u):e)});return h.fail(function(){o.val("onSourceError")&&o.val("onSourceError")({url:t,page:o,xhrPromise:h}),s.onSourceError.fire({url:t,page:o,xhrPromise:h})}),l};c.show=function(t){var n=this.element,r=this;r.showElementWrapper(t),r.val("title")&&(e.document.title=r.val("title"));if(r.val("sourceOnShow")){if(!r.val("sourceCache")||!n.__pagerLoaded__||typeof r.val("sourceCache")=="number"&&n.__pagerLoaded__+r.val("sourceCache")*1e3<s.now())n.__pagerLoaded__=s.now(),r.loadSource(r.val("sourceOnShow"))}else r.val("withOnShow")&&r.loadWithOnShow()},c.showElementWrapper=function(e){var t=this;t.val("beforeShow")&&t.val("beforeShow")(this),t.showElement(e),t.val("scrollToTop")&&t.element.scrollIntoView(),t.val("afterShow")&&t.val("afterShow")(this)},c.showElement=function(e){this.val("showElement")?this.val("showElement")(this,e):this.val("fx")?s.fx[this.val("fx")].showElement(this,e):s.showElement?s.showElement(this,e):n(this.element).show(e)},c.hideElementWrapper=function(e){this.isVisible(!1),this.val("beforeHide")&&this.val("beforeHide")(this),this.hideElement(e),this.val("afterHide")&&this.val("afterHide")(this)},c.hideElement=function(e){this.val("hideElement")?this.val("hideElement")(this,e):this.val("fx")?s.fx[this.val("fx")].hideElement(this,e):s.hideElement?s.hideElement(this,e):(n(this.element).hide(),e&&e())},c.getFullRoute=function(){return this._fullRoute?this._fullRoute:(this._fullRoute=r.computed(function(){var e=null;return this.currentParentPage&&this.currentParentPage()?(e=this.currentParentPage().getFullRoute()().slice(0),e.push(this.originalRoute()||this.getId()),e):this.parentPage?(e=this.parentPage.getFullRoute()().slice(0),e.push(this.originalRoute()||this.getId()),e):[]},this),this._fullRoute)},c.getRole=function(){return this.val("role")||"next"},c.nullObject=new s.Page,c.nullObject.children=r.observableArray([]),c.child=function(e){var t=this;return t._child==null&&(t._child={}),t._child[e]||(t._child[e]=r.computed(function(){var t=n.grep(this.children(),function(t){return t.id()===e})[0];return t||this.nullObject},this)),t._child[e]},r.bindingHandlers.page={init:function(e,t,n,r,i){var o=null;return a.value(t())instanceof s.Page?(o=a.value(t()),o.element=e,o.allBindingsAccessor==null&&(o.allBindingsAccessor=n),o.viewModel==null&&(o.viewModel=r),o.bindingContext==null&&(o.bindingContext=i)):o=new s.Page(e,t,n,r,i),o.init()}},s.useHTML5history=!1,s.rootURI="/",s.Href=function(e,t,n,i,s){this.element=e,this.bindingContext=s,this.path=r.observable(),this.pageOrRelativePath=r.observable(t)};var v=s.Href.prototype;return v.getParentPage=function(){return s.getParentPage(this.bindingContext)},v.init=function(){var e=this,t=e.getParentPage();e.path=r.computed(function(){var n=a.value(e.pageOrRelativePath()());return t.path(n)})},s.Href.hash="#",v.bind=function(){r.applyBindingsToNode(this.element,{attr:{href:this.path}})},v.update=function(e){this.pageOrRelativePath(e)},s.Href5=function(e,t,n,r,i){s.Href.apply(this,arguments)},s.Href5.prototype=new s.Href,s.Href5.history=e.history,s.Href5.prototype.bind=function(){var e=this;r.applyBindingsToNode(e.element,{attr:{href:e.path},click:function(){var t=e.path();if(t===""||t==="/")t=n("base").attr("href");s.Href5.history.pushState(null,null,t)}})},r.bindingHandlers["page-hash"]={init:function(e,t,n,r,i){var o=new s.Href(e,t,n,r,i);o.init(),o.bind(),e.__ko__page=o},update:function(e,t){e.__ko__page.update(t)}},r.bindingHandlers["page-href5"]={init:function(e,t,n,r,i){var o=new s.Href5(e,t,n,r,i);o.init(),o.bind(),e.__ko__page=o},update:function(e,t){e.__ko__page.update(t)}},r.bindingHandlers["page-href"]={init:function(e,t,n,r,i){var o=s.useHTML5history?s.Href5:s.Href,u=new o(e,t,n,r,i);u.init(),u.bind(),e.__ko__page=u},update:function(e,t){e.__ko__page.update(t)}},s.fx={},s.fx.cssAsync=function(e){return{showElement:function(t,r){var i=n(t.element);i.addClass(e),i.show();var s=setInterval(function(){clearInterval(s),i.addClass(e+"-in")},10),o=setInterval(function(){clearInterval(o),r&&r()},300)},hideElement:function(t,r){var i=n(t.element);if(!t.pageHiddenOnce)t.pageHiddenOnce=!0,i.hide();else{i.removeClass(e+"-in");var s=setInterval(function(){clearInterval(s),r&&r(),i.hide()},300)}}}},s.fx.zoom=s.fx.cssAsync("pagerjs-fx-zoom"),s.fx.flip=s.fx.cssAsync("pagerjs-fx-flip"),s.fx.popout=s.fx.cssAsync("pagerjs-fx-popout-modal"),s.fx.jQuerySync=function(e,t){return{showElement:function(t,r){e.call(n(t.element),300,r)},hideElement:function(e,r){t.call(n(e.element),300,function(){n(e.element).hide()}),r&&r()}}},s.fx.slide=s.fx.jQuerySync(n.fn.slideDown,n.fn.slideUp),s.fx.fade=s.fx.jQuerySync(n.fn.fadeIn,n.fn.fadeOut),s.startHistoryJs=function(r){r&&s.Href5.history.pushState(null,null,r),t.Adapter.bind(e,"statechange",function(){var e=n("base").attr("href"),r=t.getState().url.replace(e,"");o(r)}),t.Adapter.bind(e,"anchorchange",function(){o(location.hash)}),o(t.getState().url.replace(t.getRootUrl(),""))},s.startHashChange=function(t){t&&(e.location.hash=s.Href.hash+t),n(e).hashchange(function(){o(e.location.hash)}),n(e).hashchange()},s.start=function(t){t&&(e.location.hash=s.Href.hash+t);var r=function(){o(e.location.hash)};n(e).bind("hashchange",r),r()},s},r=e.define;typeof r=="function"&&typeof r.amd=="object"&&r.amd?r(["knockout","jquery"],function(e){return n($,e)}):e.pager=n($,ko)})(window);
+(function(e){var t=e.History,n=function(n,r){"use strict";var i=function(e,t){return function(){var n=arguments;return r.computed(function(){return e.apply(t,n)})}},s={};s.page=null,s.now=function(){return Date.now?Date.now():(new Date).valueOf()},s.extendWithPage=function(e){var t=new s.Page;e.$__page__=t,s.page=t},s.navigationFailed=r.observable(),s.onBindingError=n.Callbacks(),s.onSourceError=n.Callbacks(),s.showChild=function(e){var t=e&&e.length===1&&e[0]===""?[]:e;s.page.showPage(t)},s.getParentPage=function(e){while(e){if(e.$page&&e.$page.val("urlToggle")!=="none")return e.$page;if(e.$data&&e.$data.$__page__)return e.$data.$__page__;e=e.$parentContext}return null};var o=null,u=null,a=function(e){u&&u.reject({cancel:!0}),o=null,e.substring(0,s.Href.hash.length)===s.Href.hash&&(e=e.slice(s.Href.hash.length));var t=f(e);s.showChild(t)};s.navigate=function(e){s.useHTML5history?s.Href5.history.pushState(null,null,e):location.hash=e};var f=function(e){return n.map(e.replace(/\+/g," ").split("/"),decodeURIComponent)},l={};l.value=r.utils.unwrapObservable,l.arrayValue=function(e){return n.map(e,function(e){return l.value(e)})};var c=function(e){var t,n={},r=/([^&=]+)=?([^&]*)/g;while(t=r.exec(e))n[t[1]]=t[2];return n},h=function(e){if(!e)return{name:null,params:{}};var t=e.split("?"),n=t[0],r=t[1],i={};return r&&(i=c(r)),{name:n,params:i}};s.ChildManager=function(e,t){this.currentChildO=r.observable(null);var i=this;this.page=t,this.timeStamp=s.now(),this.hideChild=function(){i.currentChild&&i.currentChild.getId()!=="start"&&(i.currentChild.hidePage(function(){}),i.currentChild=null,i.currentChildO(null))},this.showChild=function(t){var r=t.length===0;this.timeStamp=s.now();var o=this.timeStamp,u=i.currentChild;i.currentChild=null;var a=!1,f=h(t[0]),c=f.name,p=null;n.each(e(),function(e,t){if(!a){var n=t.getId(),r=t.getRole();if(n===c||(c===""||c==null)&&(n==="start"||r==="start"))a=!0,i.currentChild=t;n==="?"&&(p=t)}});var d=!1,v=i,m=function(e,t){if(!a){var n=t.getId(),r=t.getValue().modal;if(r){if(n===c||(c===""||c==null)&&n==="start")a=!0,i.currentChild=t,d=!0;n==="?"&&!p&&(p=t,d=!0)}}};while(!i.currentChild&&v.page.parentPage&&!v.page.getValue().modal){var g=v.page.parentPage.children;n.each(g(),m),i.currentChild||(v=v.page.parentPage.childManager)}!i.currentChild&&p&&!r&&(i.currentChild=p),i.currentChild&&(i.currentChildO(i.currentChild),d?i.currentChild.currentParentPage(i.page):i.currentChild.currentParentPage(null));var y=function(){s.navigationFailed&&s.navigationFailed({page:i.page,route:t}),i.page.getValue().navigationFailed&&l.value(i.page.getValue().navigationFailed)(i.page,t)},b=function(){var e=l.value(i.currentChild.getValue().guard);e?e(i.currentChild,t,function(){i.timeStamp===o&&i.currentChild.showPage(t.slice(1),f,t[0])},u):i.currentChild.showPage(t.slice(1),f,t[0])};u&&u===i.currentChild?b():u?u.hidePage(function(){i.currentChild?b():y()}):i.currentChild?b():y()}},s.Page=function(e,t,n,i,o){this.element=e,this.valueAccessor=t,this.allBindingsAccessor=n,this.viewModel=i,this.bindingContext=o,this.children=r.observableArray([]),this.childManager=new s.ChildManager(this.children,this),this.parentPage=null,this.currentId=null,this.getCurrentId=r.observable(),this.ctx=null,this.currentParentPage=r.observable(null),this.isVisible=r.observable(!1),this.originalRoute=r.observable(null),this.route=null};var p=s.Page.prototype;p.val=function(e){return l.value(this.getValue()[e])},p.currentChildPage=function(){return this.childManager.currentChildO},p.find=function(e){var t=l.value(e),r=this;if(t.substring(0,1)==="/")r=s.page,t=t.slice(1);else while(t.substring(0,3)==="../")r=r.currentParentPage&&r.currentParentPage()?r.currentParentPage():r.parentPage,t=t.slice(3);var i=f(t);return n.each(i,function(e,t){r=r.child(t)()}),r},p.find$=function(e){return i(this.find,this)(e)},p.path=function(e){var t=this,r=l.value(e);if(r&&typeof r=="object"&&r.path&&r.params&&!(r instanceof s.Page)){var i=r.path,o=r.params;return t.path(i)+"?"+n.param(o)}var u;if(r==null||r==="")u=t;else{if(!(r instanceof s.Page)){if(r.substring(0,1)==="/")return s.page.getFullRoute()()+r.substring(1);var a=0;while(r.substring(0,3)==="../")a++,r=r.slice(3);var f=t.getFullRoute()(),c=f.slice(0,f.length-a).join("/"),h=(c===""?"":c+"/")+r;return s.useHTML5history?n("base").attr("href")+h:s.Href.hash+h}u=r}var p=u.getFullRoute()().join("/");return s.useHTML5history?n("base").attr("href")+p:s.Href.hash+p},p.path$=function(e){return i(this.path,this)(e)},p.async=function(e,t,n,r){var i=this;return function(){u&&u.reject({cancel:!0});var a=e();u=a,r&&r(a.state());var f=Math.random();o=f,a.done(function(){r&&r(a.state()),f===o&&s.navigate(i.path(t))}),a.fail(function(e){r&&r(a.state());var t=e&&e.cancel;f===o&&!t&&n&&s.navigate(i.path(n))})}},p.showPage=function(e,t,n){var r=this,i=r.currentId,s=r.pageRoute?r.pageRoute.params:null,o=r.isVisible();r.currentId=t?t.name||"":"",r.getCurrentId(r.currentId),r.isVisible(!0),n&&r.originalRoute(n),r.route=e,r.pageRoute=t,o?(r.getId()==="?"&&i!==r.currentId&&r.show(),t&&s!==t.params&&r.setParams()):(r.setParams(),r.show()),r.childManager.showChild(e)},p.setParams=function(){if(this.pageRoute&&this.pageRoute.params){var e=this.pageRoute.params,t=this.ctx,i=this.val("params")||{};n.isArray(i)?n.each(i,function(n,i){var s=e[i];t[i]?t[i](s):t[i]=r.observable(s)}):n.each(i,function(n,i){var s=e[n],o;s==null?o=l.value(i):o=s,t[n]?t[n](o):t[n]=r.observable(o)})}if(this.pageRoute){var s=this.getValue().nameParam;s&&(typeof s=="string"?this.ctx[s]?this.ctx[s](this.currentId):this.ctx[s]=r.observable(this.currentId):s(this.currentId))}},p.hidePage=function(e){var t=this;"show"!==t.val("urlToggle")?(t.hideElementWrapper(e),t.childManager.hideChild()):e&&e()};var d=function(e){try{r.applyBindingsToDescendants(e.childBindingContext,e.element)}catch(t){var n=e.val("onBindingError");n&&n(e.e),s.onBindingError.fire({page:e,error:t})}};p.init=function(){var e=this,t=e.val("urlToggle"),i=e.val("id");i!=="?"&&e.getCurrentId(i);var s=r.utils.domData.get(e.element,"__ko_pagerjsBindingData");if(s)return{controlsDescendantBindings:!0};r.utils.domData.set(e.element,"__ko_pagerjsBindingData",e),r.utils.domNodeDisposal.addDisposeCallback(e.element,function(){var t=e.val("beforeRemove");t&&t({page:e}),e.parentPage&&e.parentPage.children.remove(e);var n=e.val("afterRemove");n&&n({page:e})});var o=e.getValue();t!=="none"&&(e.parentPage=e.getParentPage(),e.parentPage.children.push(this),e.hideElement()),e.val("source")&&e.loadSource(e.val("source")),e.ctx=null;if(o.withOnShow)e.ctx={},e.childBindingContext=e.bindingContext.createChildContext(e.ctx),r.utils.extend(e.childBindingContext,{$page:this});else{var u=o["with"]||e.bindingContext.$observableData||e.viewModel;e.ctx=l.value(u),e.augmentContext();if(r.isObservable(u)){var a=r.observable(e.ctx);e.childBindingContext=e.bindingContext.createChildContext(a),r.utils.extend(e.childBindingContext,{$page:this,$observableData:u}),d(e),u.subscribe(function(){a(l.value(u))})}else e.childBindingContext=e.bindingContext.createChildContext(e.ctx),r.utils.extend(e.childBindingContext,{$page:this,$observableData:undefined}),d(e)}if(t!=="none")e.parentPage.route&&e.parentPage.route[0]===e.getId()&&setTimeout(function(){e.parentPage.showPage(e.parentPage.route)},0);else{var f=function(){n(e.element).is(":visible")&&e.showPage([])};setTimeout(f,0),e.getParentPage().isVisible.subscribe(function(e){e&&setTimeout(f,0)})}var c=e.getValue().bind;return r.isObservable(c)&&c(e),{controlsDescendantBindings:!0}},p.augmentContext=function(){var e=this,t=e.val("params");t&&(n.isArray(t)?n.each(t,function(t,n){e.ctx[n]||(e.ctx[n]=r.observable())}):n.each(t,function(n,i){e.ctx[n]||(r.isObservable(i)?e.ctx[n]=i:i===null?(t[n]=r.observable(null),e.ctx[n]=r.observable(null)):e.ctx[n]=r.observable(i))})),this.val("vars")&&n.each(this.val("vars"),function(t,n){r.isObservable(n)?e.ctx[t]=n:e.ctx[t]=r.observable(n)});var i=this.getValue().nameParam;i&&typeof i=="string"&&(e.ctx[i]=r.observable(null)),this.setParams()},p.getValue=function(){return this.valueAccessor?l.value(this.valueAccessor()):{}},p.getParentPage=function(){return s.getParentPage(this.bindingContext)},p.getId=function(){return this.val("id")},p.id=function(){var e=this.getCurrentId();return e==null||e===""?this.getId():e},p.sourceUrl=function(e){var t=this;return this.getId()==="?"?r.computed(function(){var n;return t.val("deep")?n=[t.currentId].concat(t.route).join("/"):n=t.currentId,l.value(e).replace("{1}",n)}):r.computed(function(){return l.value(e)})},p.loadWithOnShow=function(){var e=this;if(!e.withOnShowLoaded||e.val("sourceCache")!==!0)e.withOnShowLoaded=!0,e.val("withOnShow")(function(t){var n=e.bindingContext.createChildContext(t);e.ctx=t,e.childBindingContext=n,e.augmentContext(),r.utils.extend(n,{$page:e}),d(e)},e)},p.loadSource=function(e){var t=this.getValue(),i=this,o=this.element,u=null,a=t.loader||s.loader;if(t.frame==="iframe"){var f=n("iframe",n(o));f.length===0&&(f=n("<iframe></iframe>"),n(o).append(f)),a&&(u=l.value(a)(i,f),u.load()),f.one("load",function(){u&&u.unload(),t.sourceLoaded&&t.sourceLoaded(i)}),r.applyBindingsToNode(f[0],{attr:{src:this.sourceUrl(e)}})}else{a&&(u=l.value(a)(i,i.element),u.load());var c=function(){u&&u.unload(),i.val("withOnShow")?i.val("withOnShow")&&i.loadWithOnShow():d(i),t.sourceLoaded&&t.sourceLoaded(i),i.route&&i.childManager.showChild(i.route)};if(typeof l.value(e)=="string"){var h=l.value(this.sourceUrl(e));m(o,h,function(){c()},i)}else{var p=n(o).children();l.value(e)(this,function(){n.each(p,function(e,t){r.utils.domNodeDisposal.removeNode(t)}),c()})}}};var v=/<script\b[^<]*(?:(?!<\/script>)<[^<]*)*<\/script>/gi,m=function(e,t,i,o){var u,a,f,l=n(e),c=t.indexOf(" ");c>=0&&(u=t.slice(c,t.length),t=t.slice(0,c));var h=jQuery.ajax({url:t,type:"GET",dataType:"html",complete:function(e,t){i&&l.each(i,f||[e.responseText,t,e])}}).done(function(e){f=arguments,n.each(l.children(),function(e,t){r.utils.domNodeDisposal.removeNode(t)}),l.html(u?jQuery("<div>").append(e.replace(v,"")).find(u):e)});return h.fail(function(){o.val("onSourceError")&&o.val("onSourceError")({url:t,page:o,xhrPromise:h}),s.onSourceError.fire({url:t,page:o,xhrPromise:h})}),l};p.show=function(t){var n=this.element,r=this;r.showElementWrapper(t),r.val("title")&&(e.document.title=r.val("title"));if(r.val("sourceOnShow")){if(!r.val("sourceCache")||!n.__pagerLoaded__||typeof r.val("sourceCache")=="number"&&n.__pagerLoaded__+r.val("sourceCache")*1e3<s.now())n.__pagerLoaded__=s.now(),r.loadSource(r.val("sourceOnShow"))}else r.val("withOnShow")&&r.loadWithOnShow()},p.showElementWrapper=function(e){var t=this;t.val("beforeShow")&&t.val("beforeShow")(this),t.showElement(e),t.val("scrollToTop")&&t.element.scrollIntoView(),t.val("afterShow")&&t.val("afterShow")(this)},p.showElement=function(e){this.val("showElement")?this.val("showElement")(this,e):this.val("fx")?s.fx[this.val("fx")].showElement(this,e):s.showElement?s.showElement(this,e):n(this.element).show(e)},p.hideElementWrapper=function(e){this.isVisible(!1),this.val("beforeHide")&&this.val("beforeHide")(this),this.hideElement(e),this.val("afterHide")&&this.val("afterHide")(this)},p.hideElement=function(e){this.val("hideElement")?this.val("hideElement")(this,e):this.val("fx")?s.fx[this.val("fx")].hideElement(this,e):s.hideElement?s.hideElement(this,e):(n(this.element).hide(),e&&e())},p.getFullRoute=function(){return this._fullRoute?this._fullRoute:(this._fullRoute=r.computed(function(){var e=null;return this.currentParentPage&&this.currentParentPage()?(e=this.currentParentPage().getFullRoute()().slice(0),e.push(this.originalRoute()||this.getId()),e):this.parentPage?(e=this.parentPage.getFullRoute()().slice(0),e.push(this.originalRoute()||this.getId()),e):[]},this),this._fullRoute)},p.getRole=function(){return this.val("role")||"next"},p.nullObject=new s.Page,p.nullObject.children=r.observableArray([]),p.child=function(e){var t=this;return t._child==null&&(t._child={}),t._child[e]||(t._child[e]=r.computed(function(){var t=n.grep(this.children(),function(t){return t.id()===e})[0];return t||this.nullObject},this)),t._child[e]},r.bindingHandlers.page={init:function(e,t,n,r,i){var o=null;return l.value(t())instanceof s.Page?(o=l.value(t()),o.element=e,o.allBindingsAccessor==null&&(o.allBindingsAccessor=n),o.viewModel==null&&(o.viewModel=r),o.bindingContext==null&&(o.bindingContext=i)):o=new s.Page(e,t,n,r,i),o.init()}},s.useHTML5history=!1,s.rootURI="/",s.Href=function(e,t,n,i,s){this.element=e,this.bindingContext=s,this.path=r.observable(),this.pageOrRelativePath=r.observable(t)};var g=s.Href.prototype;return g.getParentPage=function(){return s.getParentPage(this.bindingContext)},g.init=function(){var e=this,t=e.getParentPage();e.path=r.computed(function(){var n=l.value(e.pageOrRelativePath()());return t.path(n)})},s.Href.hash="#",g.bind=function(){r.applyBindingsToNode(this.element,{attr:{href:this.path}})},g.update=function(e){this.pageOrRelativePath(e)},s.Href5=function(e,t,n,r,i){s.Href.apply(this,arguments)},s.Href5.prototype=new s.Href,s.Href5.history=e.history,s.Href5.prototype.bind=function(){var e=this;r.applyBindingsToNode(e.element,{attr:{href:e.path},click:function(){var t=e.path();if(t===""||t==="/")t=n("base").attr("href");s.Href5.history.pushState(null,null,t)}})},r.bindingHandlers["page-hash"]={init:function(e,t,n,r,i){var o=new s.Href(e,t,n,r,i);o.init(),o.bind(),e.__ko__page=o},update:function(e,t){e.__ko__page.update(t)}},r.bindingHandlers["page-href5"]={init:function(e,t,n,r,i){var o=new s.Href5(e,t,n,r,i);o.init(),o.bind(),e.__ko__page=o},update:function(e,t){e.__ko__page.update(t)}},r.bindingHandlers["page-href"]={init:function(e,t,n,r,i){var o=s.useHTML5history?s.Href5:s.Href,u=new o(e,t,n,r,i);u.init(),u.bind(),e.__ko__page=u},update:function(e,t){e.__ko__page.update(t)}},s.fx={},s.fx.cssAsync=function(e){return{showElement:function(t,r){var i=n(t.element);i.addClass(e),i.show();var s=setInterval(function(){clearInterval(s),i.addClass(e+"-in")},10),o=setInterval(function(){clearInterval(o),r&&r()},300)},hideElement:function(t,r){var i=n(t.element);if(!t.pageHiddenOnce)t.pageHiddenOnce=!0,i.hide();else{i.removeClass(e+"-in");var s=setInterval(function(){clearInterval(s),r&&r(),i.hide()},300)}}}},s.fx.zoom=s.fx.cssAsync("pagerjs-fx-zoom"),s.fx.flip=s.fx.cssAsync("pagerjs-fx-flip"),s.fx.popout=s.fx.cssAsync("pagerjs-fx-popout-modal"),s.fx.jQuerySync=function(e,t){return{showElement:function(t,r){e.call(n(t.element),300,r)},hideElement:function(e,r){t.call(n(e.element),300,function(){n(e.element).hide()}),r&&r()}}},s.fx.slide=s.fx.jQuerySync(n.fn.slideDown,n.fn.slideUp),s.fx.fade=s.fx.jQuerySync(n.fn.fadeIn,n.fn.fadeOut),s.startHistoryJs=function(r){r&&s.Href5.history.pushState(null,null,r),t.Adapter.bind(e,"statechange",function(){var e=n("base").attr("href"),r=t.getState().url.replace(e,"");a(r)}),t.Adapter.bind(e,"anchorchange",function(){a(location.hash)}),a(t.getState().url.replace(t.getRootUrl(),""))},s.startHashChange=function(t){t&&(e.location.hash=s.Href.hash+t),n(e).hashchange(function(){a(e.location.hash)}),n(e).hashchange()},s.start=function(t){t&&(e.location.hash=s.Href.hash+t);var r=function(){a(e.location.hash)};n(e).bind("hashchange",r),r()},s},r=e.define;typeof r=="function"&&typeof r.amd=="object"&&r.amd?r(["knockout","jquery"],function(e){return n($,e)}):e.pager=n($,ko)})(window);

0 comments on commit 8fefc3a

Please sign in to comment.