/** * @license dotJEM Angular Routing * (c) 2012-2013 dotJEM (Jens Melgaard) * License: MIT * * @module angular-routing */ var dotjem;(function(e,t,r,n){"use strict";function a(e,t){return w(new(w(function(){},{prototype:e})),t)}function o(e){return g(e)?e:e.$fullname||e.fullname}function i(e){if(v(e))return function(t,r){return t.invoke(e,e,r)};if(b(e)){var t=e[e.length-1];return function(r,n){return r.invoke(e,t,n)}}return null}function u(e,t,r){var n=C(e||{});return n.$all=C(e||{}),n.$path=C(t||{}),n.$search=C(r||{}),n}function c(e,t){var r=[];return E(e,function(e,t){r.push(s(t,!0)+(e===!0?"":"="+s(e,!0)))}),r.length?t+r.join("&"):""}function s(e,t){return encodeURIComponent(e).replace(/%40/gi,"@").replace(/%3A/gi,":").replace(/%24/g,"$").replace(/%2C/gi,",").replace(/%20/g,t?"%20":"+")}function l(e){return e.replace(_,"\\$&")}function f(){function e(e){var t={handler:{}};return d(e.to)&&(t.to=e.to),d(e.from)&&(t.from=e.from),d(e.handler)&&(t.handler=e.handler),d(e.before)&&m(t.handler.before)&&(t.handler.before=e.before),d(e.between)&&m(t.handler.between)&&(t.handler.between=e.between),d(e.after)&&m(t.handler.after)&&(t.handler.after=e.after),t}function t(e,t){var r=V.validateTarget(e),n=V.validateTarget(t);if(!r||!n){if(r)throw Error("Invalid transition - to: '"+t+"'.");if(n)throw Error("Invalid transition - from: '"+e+"'.");throw Error("Invalid transition - from: '"+e+"', to: '"+t+"'.")}}function r(e){for(var t=n,r=e.split("."),a=r[0]===x?1:0;r.length>a;a++)r[a]in t.children||(t.children[r[a]]={children:{},targets:{}}),t=t.children[r[a]];return t}var n={children:{},targets:{}};this.onEnter=function(t,r){if($(r)){var n=e(r);this.transition(n.from||"*",t,n.handler)}else(v(r)||b(r))&&this.transition("*",t,r)},this.onExit=function(t,r){if($(r)){var n=e(r);this.transition(t,n.to||"*",n.handler)}else(v(r)||b(r))&&this.transition(t,"*",r)},this.transition=function(e,n,a){var u,c=this;if(b(e))E(e,function(e){c.transition(e,n,a)});else if(b(n))E(n,function(t){c.transition(e,t,a)});else{if(e=o(e),n=o(n),n===e&&-1===n.indexOf("*"))return this;t(e,n),i(a)&&(a={between:a}),u=r(e),n in u.targets||(u.targets[n]=[]),a.name=e+" -> "+n,u.targets[n].push(a)}return this},this.$get=["$q","$injector",function(e,t){function r(e,r){function n(n,a,o){var c;E(u,function(u){d(c=n(u))&&i(c)(t,{$to:r,$from:e,$transition:a,$view:o})})}var a=s(o(e)),u=c(a,o(r));return{before:function(e,t){n(function(e){return e.before},e,t)},between:function(e,t){n(function(e){return e.between},e,t)},after:function(e,t){n(function(e){return e.after},e,t)}}}function a(e){return e[0]===x&&e.splice(0,1),e}function u(e,t){for(var r,n,o=a(e.split(".")).reverse(),i=a(t.split(".")).reverse();;){if(r=o.pop(),n=i.pop(),"*"===n||"*"===r)return!0;if(r!==n)return!1;if(!d(r)||!d(n))return!0}return!0}function c(e,t){var r=[];return E(e,function(e){E(e.targets,function(e,n){u(n,t)&&E(e,function(e){r.push(e)})})}),r}function s(e){var t=n,r=e.split("."),a=[],o=r[0]===x?1:0;do{if("*"in t.children&&a.push(t.children["*"]),!(r[o]in t.children))break;t=t.children[r[o]],a.push(t)}while(o++0))throw Error(T.regexConverterNotValid);t=e}return r=RegExp(t,n),{parse:function(e){var t=r.test(e);return{accept:t,value:t?r.exec(e):null}},format:function(e){var t=""+e,n=r.test(t);if(!n)throw Error(T.valueCouldNotBeMatchedByRegex);return t}}}),this.convert("",function(){return function(){return!0}}),this.$get=["$rootScope","$location","$q","$injector","$routeParams","$browser",function(t,r,i,u,s,l){function f(e,t,r){var n=a(e,{self:a(e.self,{params:w({},r,t),searchParams:r,pathParams:t})});return n}function h(e){var t,n;return E(p,function(a){!n&&(t=a.match(e))&&(n=f(a,t,r.search()))}),n||p[null]&&f(p[null],{},{})}function d(){var e=h(r.path()),a=v.current,o=e?e.self:n;if(!m&&o&&a&&angular.equals(o.pathParams,a.pathParams)&&!o.reloadOnSearch)a.params=o.params,a.searchParams=o.searchParams,a.pathParams=o.pathParams,C(o.params,s),t.$broadcast(O.ROUTE_UPDATE,a);else if(e||a){m=!1;var c=t.$broadcast(O.ROUTE_CHANGE_START,o,a);if(!c.defaultPrevented){v.current=o,e&&e.redirect(r,o);var l=i.when(o);o&&E(b,function(e){l=l.then(function(){var t=u.invoke(e,o,{$next:o});return i.when(t)})}),l.then(function(){o===v.current&&(e&&angular.copy(o.params,s),t.$broadcast(O.ROUTE_CHANGE_SUCCESS,o,a))},function(e){o===v.current&&t.$broadcast(O.ROUTE_CHANGE_ERROR,o,a,e)})}}}var m=!1,v={routes:p,html5Mode:function(){return e.html5Mode()},hashPrefix:function(){return e.hashPrefix()},reload:function(){m=!0,t.$evalAsync(d)},change:function(e){var t=e.params||{},n=o(e.route,t),a=r.path(n).search(t||{});e.replace&&a.replace()},format:function(t,r){var r=r||{},n=o(t,r)+c(r,"?");return e.html5Mode()&&(n=(l.baseHref()+n).replace(/\/\//g,"/")),n}};return t.$on(O.LOCATION_CHANGE,d),v}]}];angular.module("dotjem.routing").provider("$route",R).value("$routeParams",{}),angular.module("dotjem.routing").provider("$stateTransition",f);var A=["$routeProvider","$stateTransitionProvider",function(e,t){var r=new I(e,t),n=r.createState(x,{}),a=new N(n);new k,this.state=function(e,t){V.validateName(e);var n=a.lookup(e,1);return n.add(r.createState(e,t,n)),this},this.$get=["$rootScope","$q","$injector","$route","$view","$stateTransition","$location","$scroll","$resolve",function(e,t,r,i,c,s,l,f,p){function h(t){d(t)?g(t)||$(t)?(b=o(t),0!==b.indexOf(x)&&(b=x+"."+b)):t&&(b=n.fullname):b=C.fullname,e.$evalAsync(function(){m({state:C,params:y.params,fource:b})})}function m(n){_.ended||_.abort();var u=_=S.next(function(e){S=e});if(u=u.execute(G.initializeContext(o(n.state),n.params,a)).execute(G.createEmitter(s)).execute(G.buildChanges(b)).execute(G.createTransition(m)).execute(function(){b=null}).execute(G.raiseUpdate(e)).execute(G.updateRoute(i,n.updateroute)).execute(G.beginTransaction(c,r)).execute(G.before()).execute(function(t){e.$broadcast(O.STATE_CHANGE_START,t.toState,y.current).defaultPrevented&&t.abort()}),!u.ended){var l,h=!1,d={},v=t.when("");E(u.changed.array,function(e){v=v.then(function(){return(h=h||e.isChanged)&&p.clear(e.state.resolve),p.all(e.state.resolve,d,{$to:u.toState,$from:y.current})}).then(function(t){u.completePrep(e.state.fullname,d=w({},d,t)),l=e.state.scrollTo})}),v.then(function(){u.execute(G.between(e)).execute(function(t){C=t.to;var r=y.current;y.params=t.params,y.current=t.toState,t.transaction.commit(),e.$broadcast(O.STATE_CHANGE_SUCCESS,t.toState,r)}).execute(G.after(f,l)).complete()},function(t){u.execute(function(r){e.$broadcast(O.STATE_CHANGE_ERROR,r.toState,y.current,t),r.abort()})})}}var v=new H(i),b=null,C=n,y={root:n,current:w(n.self,{$params:u()}),params:u(),"goto":function(e,t){m({state:e,params:u(t),updateroute:!0})},lookup:function(e){return a.resolve(C,e,!0)},reload:h,url:function(e,t){return e=d(e)?a.resolve(C,o(e),!1):C,v.buildUrl(y.current,e,t)},is:function(e){return C.is(o(e))},isActive:function(e){return C.isActive(o(e))}};e.$on(O.ROUTE_CHANGE_SUCCESS,function(){var e=i.current;e?e.state&&m({state:e.state,params:u(e.params,e.pathParams,e.searchParams)}):m({state:n,params:u()})}),e.$on(O.ROUTE_UPDATE,function(){var t=i.current,r=u(t.params,t.pathParams,t.searchParams);y.params=r,y.current.$params=r,e.$broadcast(O.STATE_UPDATE,y.current)});var S=new B(y,function(){},n).complete(),_=S;return y}]}];angular.module("dotjem.routing").provider("$state",A);var P=[function(){this.$get=["$q","$injector",function(e,t){var r={},n={};return r.push=function(e,t){n[e]=t},r.clear=function(e){m(e)&&(n={}),g(e)?delete n[e]:$(e)?angular.forEach(e,function(e,t){r.clear(t)}):b(e)&&angular.forEach(e,function(e){r.clear(e)})},r.all=function(r,a,o){var u=[],c=[],s=e.defer();return angular.forEach(r,function(e,r){var l;c.push(r);try{r in n||(g(e)?n[r]=angular.isString(e):null!=(l=i(e))&&(n[r]=l(t,w({},a,o)))),u.push(n[r])}catch(f){s.reject("Could not resolve "+r)}}),e.all(u).then(function(e){var t={};angular.forEach(e,function(e,r){t[c[r]]=e}),s.resolve(t)},function(e){s.reject(e)}),s.promise},r}]}];angular.module("dotjem.routing").provider("$resolve",P),angular.module("dotjem.routing").provider("$template",p),angular.module("dotjem.routing").provider("$view",h);var j=[function(){this.$get=["$window","$rootScope","$anchorScroll","$injector",function(e,t,r,a){function o(r){return u.$current=r,"top"===r?(e.scrollTo(0,0),n):(t.$broadcast("$scrollPositionChanged",r),n)}var u=function(e){var t;m(e)?r():g(e)?o(e):null!==(t=i(e))&&o(t(a))};return u.$current="top",u}]}];angular.module("dotjem.routing").provider("$scroll",j);var U=function(){function e(e,t,r,n){this._name=e,this._fullname=t,this._parent=n,this._children={},this._self=r,this._self.$fullname=t,this._reloadOnOptional=!d(r.reloadOnSearch)||r.reloadOnSearch,this._scrollTo="top",n&&d(n.scrollTo)&&(this._scrollTo=n.scrollTo),d(this._self.scrollTo)&&(this._scrollTo=this._self.scrollTo)}return Object.defineProperty(e.prototype,"children",{get:function(){return this._children},enumerable:!0,configurable:!0}),Object.defineProperty(e.prototype,"fullname",{get:function(){return this._fullname},enumerable:!0,configurable:!0}),Object.defineProperty(e.prototype,"name",{get:function(){return this._name},enumerable:!0,configurable:!0}),Object.defineProperty(e.prototype,"reloadOnOptional",{get:function(){return this._reloadOnOptional},set:function(e){this._reloadOnOptional=e},enumerable:!0,configurable:!0}),Object.defineProperty(e.prototype,"self",{get:function(){return C(this._self)},enumerable:!0,configurable:!0}),Object.defineProperty(e.prototype,"parent",{get:function(){return this._parent},enumerable:!0,configurable:!0}),Object.defineProperty(e.prototype,"route",{get:function(){return this._route},set:function(e){if(m(e))throw Error(T.routeCannotBeUndefined);this._route=e},enumerable:!0,configurable:!0}),Object.defineProperty(e.prototype,"root",{get:function(){return null===this.parent?this:this._parent.root},enumerable:!0,configurable:!0}),Object.defineProperty(e.prototype,"scrollTo",{get:function(){return this._scrollTo},enumerable:!0,configurable:!0}),Object.defineProperty(e.prototype,"views",{get:function(){return this.self.views},enumerable:!0,configurable:!0}),Object.defineProperty(e.prototype,"resolve",{get:function(){return this.self.resolve},enumerable:!0,configurable:!0}),e.prototype.add=function(e){return this._children[e.name]=e,this},e.prototype.resolveRoute=function(){return d(this.route)?this.route.route:d(this.parent)?this.parent.resolveRoute():""},e.prototype.is=function(e){return this.fullname===e||this.fullname===x+"."+e},e.prototype.isActive=function(e){return this.is(e)?!0:this.parent&&this.parent.isActive(e)||!1},e}(),N=function(){function e(e){this.root=e,this.nameRegex=RegExp("^("+l(x)+"\\.)?\\w+(\\.\\w+)*$"),this.siblingRegex=RegExp("^\\$node\\(([-+]?\\d+)\\)$"),this.indexRegex=RegExp("^\\[(-?\\d+)\\]$")}return e.prototype.lookup=function(e,t){for(var r=this.root,n=e.split("."),a=n[0]===x?1:0,t=d(t)?t:0;n.length-t>a;a++){if(!(n[a]in r.children))throw Error("Could not locate '"+n[a]+"' under '"+r.fullname+"'.");r=r.children[n[a]]}return r},e.prototype.resolve=function(e,t,r){var n,a=this,o=this.siblingRegex.exec(t),i=e;if(o?i=this.selectSibling(Number(o[1]),i):this.nameRegex.test(t)?i=this.lookup(t):(n=t.split("/"),E(n,function(t){i=a.select(e,t,i)})),i===this.root)throw Error(T.expressionOutOfBounds);return i&&r?C(i.self):i},e.prototype.selectSibling=function(e,t){var r=[],n=0;for(E(t.parent.children,function(e){r.push(e),t.fullname===e.fullname&&(n=r.length-1)});0>e;)e+=r.length;return e=(n+e)%r.length,r[e]},e.prototype.select=function(e,t,r){if("."===t){if(e!==r)throw Error(T.invalidBrowserPathExpression);return r}if(".."===t){if(m(r.parent))throw Error(T.expressionOutOfBounds);return r.parent}if(""===t){if(e!==r)throw Error(T.invalidBrowserPathExpression);return this.root}var n=this.indexRegex.exec(t);if(n){var a=Number(n[1]),o=[];if(E(r.children,function(e){o.push(e)}),Math.abs(a)>=o.length)throw Error(T.expressionOutOfBounds);return 0>a?o[o.length+a]:o[a]}if(t in r.children)return r.children[t];throw Error(T.couldNotFindStateForPath+": "+t)},e}(),k=function(){function e(){}return e.prototype.buildStateArray=function(e,t){function r(){var e={};return a.route&&E(a.route.params,function(r,n){e[n]=t[n]}),e}var n=[],a=e;do n.push({state:a,params:r()});while(a=a.parent);return n},e.prototype.compare=function(e,t,r,n,a){for(var o,i,u=this.buildStateArray(e,r||{}),c=this.buildStateArray(t,n),s=Math.max(u.length,c.length),l=!1,f=!y(r,n),p=0;s>p;p++)o=u[u.length-p-1],i=c[c.length-p-1],m(i)?c[0].isChanged=l=!0:i.isChanged=m(o)||a===i.state.fullname||i.state.fullname!==o.state.fullname||!y(i.params,o.params)?l=!0:!1;return l=l||c[0].state.reloadOnOptional&&f,{array:c.reverse(),stateChanges:l,paramChanges:f}},e}(),I=function(){function e(e,t){this.routes=e,this.transitions=t}return e.prototype.createRoute=function(e,t,r,n){var a=t||"";return""!==a&&"/"===a[a.length-1]&&(a=a.substr(0,a.length-1)),"/"!==e[0]&&""!==e&&(a+="/"),a+=e,this.routes.when(a,{state:r,reloadOnSearch:n})},e.prototype.createState=function(e,t,r){var n=this,a=e.split(".").pop();d(r)&&(e=r.fullname+"."+a);var o=new U(a,e,t,r);return o.reloadOnOptional=!d(t.reloadOnSearch)||t.reloadOnSearch,d(t.route)&&(o.route=this.createRoute(t.route,r.resolveRoute(),o.fullname,o.reloadOnOptional).$route),d(t.onEnter)&&this.transitions.onEnter(o.fullname,t.onEnter),d(t.onExit)&&this.transitions.onExit(o.fullname,t.onExit),d(t.children)&&E(t.children,function(e,t){o.add(n.createState(o.fullname+"."+t,e,o))}),o},e}(),V=function(){function e(){}return e.nameValidation=/^\w+(\.\w+)*?$/,e.targetValidation=/^\$?\w+(\.\w+)*(\.[*])?$/,e.validateName=function(t){if(!e.nameValidation.test(t)||t===x)throw Error("Invalid name: '"+t+"'.")},e.validateTarget=function(t){return"*"===t||e.targetValidation.test(t)?!0:!1},e}(),H=function(){function e(e){this.route=e}return e.prototype.buildUrl=function(e,t,r){var n=e;if(!t.route)throw Error("Can't build url for a state that doesn't have a url defined.");var a={},o=n&&n.$params.$all||{};return E(t.route.params,function(e,t){t in o&&(a[t]=o[t])}),this.route.format(t.route.route,w(a,r||{}))},e}(),G={initializeContext:function(e,t,r){return function(n){var a=r.resolve(n.from,e,!1);n.to=a,n.params=t,n.toState=w({},a.self,{$params:t})}},createEmitter:function(e){return function(t){t.emit=e.find(t.$state.current,t.toState)}},buildChanges:function(e){return function(t){t.changed=(new k).compare(t.from,t.to,t.$state.params,t.params,e)}},createTransition:function(e){return function(t){var r={canceled:!1,cancel:function(){r.canceled=!0},"goto":function(t,n){r.canceled=!0,e({state:t,params:{all:n},updateroute:!0})}};t.transition=r}},raiseUpdate:function(e){return function(t){var r=t.changed,n=t.$state;r.stateChanges||(r.paramChanges&&(n.params=t.params,n.current.$params=t.params,e.$broadcast("$stateUpdate",n.current)),t.abort())}},updateRoute:function(e,t){return function(r){var n=r.to.route;if(t&&n){var a={},o=r.$state.params.$all;E(n.params,function(e,t){t in o&&(a[t]=o[t])});var i=w(a,r.params.$all);e.change(w({},n,{params:i})),r.abort()}}},before:function(){return function(e){e.emit.before(e.transition,e.transaction),e.transition.canceled&&e.abort()}},between:function(e){return function(t){t.emit.between(t.transition,t.transaction),t.transition.canceled&&(e.$broadcast("$stateChangeAborted",t.toState,t.$state.current),t.abort())}},after:function(e,t){return function(r){r.transition.canceled||(r.transition.cancel=function(){throw Error("Can't cancel transition in after handler")},r.emit.after(r.transition,r.transaction),e(t))}},beginTransaction:function(e,t){return function(r){r.transaction=e.beginUpdate(),r.transaction.clear();var n=!1;E(r.changed.array,function(e){n=n||e.isChanged,E(e.state.views,function(a,o){var u,c;(u=a.sticky)&&((c=i(u))?u=c(t,{$to:r.toState,$from:r.$state.current}):g(u)||(u=e.state.fullname)),n||a.force||d(u)?r.prepUpdate(e.state.fullname,o,a.template,a.controller,u):r.prepCreate(e.state.fullname,o,a.template,a.controller)})})}}},B=function(){function e(e,t,r){this.aborted=!1,this.completed=!1,this._prep={},this._$state=e,this.to=r,this.onComplete=t}return Object.defineProperty(e.prototype,"$state",{get:function(){return this._$state},enumerable:!0,configurable:!0}),Object.defineProperty(e.prototype,"ended",{get:function(){return this.aborted||this.completed},enumerable:!0,configurable:!0}),e.prototype.next=function(t){this.ended||this.abort();var r=new e(this.$state,t);return r.previous=this,r.from=this.to,this.previous=null,r},e.prototype.execute=function(e){return!this.ended&&(e(this),this.aborted)?this.previous:this},e.prototype.complete=function(){return this.ended||(this.onComplete(this),this.completed=!0),this},e.prototype.abort=function(){return this.ended||(this.aborted=!0,this.transaction&&!this.transaction.completed&&this.transaction.cancel()),this},e.prototype.prepUpdate=function(e,t,r,n,a){var o=this._prep[e]=this._prep[e]||{};o[t]=this.transaction.prepUpdate(t,r,n,a)},e.prototype.prepCreate=function(e,t,r,n){var a=this._prep[e]=this._prep[e]||{};a[t]=this.transaction.prepCreate(t,r,n)},e.prototype.completePrep=function(e,t){E(this._prep[e],function(e){e(t)})},e}(),q=["$state","$compile","$controller","$view","$animate","$template",function(e,t,r,a,o,i){return{restrict:"ECA",terminal:!0,priority:1e3,transclude:"element",compile:function(e,u,c){return function(e,u,s){function l(){d($)&&($.remove(),$=n,u.contents().show())}function f(){null!==y&&y.then(function(e){u.contents().hide(),u.append($=angular.element(e))})}function p(e){m&&(m.$destroy(),m=null),g&&(e?o.leave(g):g.remove(),g=null)}function h(n){var i,s=a.get(b);if(s&&s.template){if(s.version===C)return;C=s.version,i=s.controller,s.template.then(function(a){var l=e.$new();c(l,function(e){p(n),e.html(a),n?o.enter(e,null,u):u.after(e);var c,f=t(e.contents());m=l,g=e,i&&(c=w({},s.locals),c.$scope=m,i=r(i,c),e.data("$ngControllerController",i),e.children().data("$ngControllerController",i)),f(m),m.$emit("$viewContentLoaded"),m.$eval(E)})})}else C=-1,p(n)}var m,g,$,b=s.jemView||s.name,E=s.onload||"",C=-1,y=s.loader&&i.get(s.loader)||null;e.$on(O.VIEW_UPDATE,function(e,t){t===b&&h(!0)}),e.$on(O.VIEW_REFRESH,function(e,t,r){t===b&&(v(m.refresh)?m.refresh(r):m.$broadcast("$refresh",t,r))}),e.$on("$viewPrep",function(e,t,r){t===b&&"update"===r.type?f():"cancel"===r.type&&l()}),h(!1)}}}}];angular.module("dotjem.routing").directive("jemView",q);var D=["$scroll","$timeout",function(e,t){return{restrict:"AC",terminal:!1,link:function(r,n,a){function o(e){e===i&&t(function(){n[0].scrollIntoView()},u)}var i=a.jemAnchor||a.id,u=d(a.delay)?Number(a.delay):1;r.$on("$scrollPositionChanged",function(e,t){o(t)}),o(e.$current)}}}];angular.module("dotjem.routing").directive("jemAnchor",D),angular.module("dotjem.routing").directive("id",D);var F=["$state","$route",function(e,t){return{restrict:"AC",link:function(r,n,a){function o(){var t=r.$eval(a.sref),o=r.$eval(a.params),i=e.url(t,o);c||(i="#"+s+i),n.attr(l[u],i)}function i(){var t=r.$eval(a.sref);e.isActive(t)?n.addClass(a.activeClass):n.removeClass(a.activeClass)}var u=n[0].tagName.toLowerCase(),c=t.html5Mode(),s=t.hashPrefix(),l={a:"href",form:"action"},f=S;d(a.activeClass)&&(f=i),r.$on(O.STATE_CHANGE_SUCCESS,f),u in l?(a.$observe("params",o),a.$observe("sref",o)):n.click(function(){r.$apply(function(){e.goto(r.sref,r.params)})})}}}];angular.module("dotjem.routing").directive("sref",F),r.State=U,r.StateBrowser=N,r.StateComparer=k,r.StateFactory=I,r.StateRules=V,r.StateUrlBuilder=H,r.RootName=x})(window,document,dotjem||(dotjem={}));