diff --git a/Gruntfile.js b/Gruntfile.js index de6fffb..c1803cd 100644 --- a/Gruntfile.js +++ b/Gruntfile.js @@ -27,21 +27,21 @@ module.exports = function(grunt) { }, karma: { - once: { + angular115: { options: { keepalive: true, - configFile: 'karma.conf.js', + configFile: 'karma-angular-1.1.5.conf.js', autoWatch: false, singleRun: true } }, - keep: { + angular120: { options: { keepalive: true, - configFile: 'karma.conf.js', - autoWatch: true, - singleRun: false - } + configFile: 'karma-angular-1.2.0rc1.conf.js', + autoWatch: false, + singleRun: true + } } }, diff --git a/bower.json b/bower.json index c1f6582..b25693c 100644 --- a/bower.json +++ b/bower.json @@ -1,6 +1,6 @@ { "name": "angular-route-segment", - "version": "1.1.0", + "version": "1.2.0", "main": "build/angular-route-segment.min.js", "ignore": [ "**/.*", diff --git a/build/angular-route-segment.js b/build/angular-route-segment.js index f0b843f..be9f336 100644 --- a/build/angular-route-segment.js +++ b/build/angular-route-segment.js @@ -1,5 +1,5 @@ /** - * angular-route-segment v1.1.0 + * angular-route-segment v1.2.0 * https://angular-route-segment.com * @author Artem Chivchalov * @license MIT License http://opensource.org/licenses/MIT @@ -212,19 +212,30 @@ angular.module( 'route-segment', [] ).provider( '$routeSegment', // if we went back to the same state as we were before resolving new segment resolvingSemaphoreChain[i] = newSegment.name; else - updates.push(updateSegment(i, newSegment)); + updates.push({index: i, newSegment: newSegment}); } } - $q.all(updates).then(function(result) { + var curSegmentPromise = $q.when(); - $routeSegment.name = segmentName; - $routeSegment.$routeParams = angular.copy($routeParams); + if(updates.length > 0) { + for(var i=0; i segmentNameChain.length) { @@ -234,7 +245,8 @@ angular.module( 'route-segment', [] ).provider( '$routeSegment', for(var i=segmentNameChain.length; i < oldLength; i++) updateSegment(i, null); } - }); + }) + } @@ -264,7 +276,7 @@ angular.module( 'route-segment', [] ).provider( '$routeSegment', } resolvingSemaphoreChain[index] = segment.name; - + if(segment.params.untilResolved) { return resolve(index, segment.name, segment.params.untilResolved) .then(function(result) { @@ -354,6 +366,14 @@ angular.module( 'route-segment', [] ).provider( '$routeSegment', } function broadcast(index) { + + $routeSegment.$routeParams = angular.copy($routeParams); + + $routeSegment.name = ''; + for(var i=0; i<$routeSegment.chain.length; i++) + $routeSegment.name += $routeSegment.chain[i].name+"."; + $routeSegment.name = $routeSegment.name.substr(0, $routeSegment.name.length-1); + $rootScope.$broadcast( 'routeSegmentChange', { index: index, segment: $routeSegment.chain[index] || null } ); @@ -393,20 +413,9 @@ angular.module( 'route-segment', [] ).provider( '$routeSegment', })(angular);;'use strict'; /** - * The directive app:view is more powerful replacement of built-in ng:view. It allows views to be nested, where each - * following view in the chain corresponds to the next route segment (see $routeSegment service). - * - * Sample: - *
- *

Section

- *
Nothing selected
- *
- * - * Initial contents of an element with app:view will display if corresponding route segment doesn't exist. - * - * View resolving are depends on route segment params: - * - `template` define contents of the view - * - `controller` is attached to view contents when compiled and linked + * appViewSegment directive + * It is based on ngView directive code: + * https://github.com/angular/angular.js/blob/master/src/ngRoute/directive/ngView.js */ (function(angular) { @@ -427,7 +436,7 @@ angular.module( 'route-segment', [] ).provider( '$routeSegment', return function($scope) { - var currentScope, currentElement, onloadExp = tAttrs.onload || '', animate, + var currentScope, currentElement, currentSegment, onloadExp = tAttrs.onload || '', animate, viewSegmentIndex = parseInt(tAttrs.appViewSegment); try { @@ -447,7 +456,7 @@ angular.module( 'route-segment', [] ).provider( '$routeSegment', // Watching for the specified route segment and updating contents $scope.$on('routeSegmentChange', function(event, args) { - if(args.index == viewSegmentIndex) + if(args.index == viewSegmentIndex && currentSegment != args.segment) update(args.segment); }); @@ -467,6 +476,8 @@ angular.module( 'route-segment', [] ).provider( '$routeSegment', function update(segment) { + currentSegment = segment; + if(isDefault) { isDefault = false; tElement.replaceWith(anchor); @@ -484,36 +495,31 @@ angular.module( 'route-segment', [] ).provider( '$routeSegment', var locals = angular.extend({}, segment.locals), template = locals && locals.$template; - if (template) { - - clearContent(); - - currentElement = tElement.clone(); - currentElement.html(template); - animate.enter( currentElement, null, anchor ); + clearContent(); - var link = $compile(currentElement, false, 499), controller; + currentElement = tElement.clone(); + currentElement.html(template ? template : defaultContent); + animate.enter( currentElement, null, anchor ); - currentScope = $scope.$new(); - if (segment.params.controller) { - locals.$scope = currentScope; - controller = $controller(segment.params.controller, locals); - if(segment.params.controllerAs) - currentScope[segment.params.controllerAs] = controller; - currentElement.data('$ngControllerController', controller); - currentElement.children().data('$ngControllerController', controller); - } + var link = $compile(currentElement, false, 499), controller; - link(currentScope); - currentScope.$emit('$viewContentLoaded'); - currentScope.$eval(onloadExp); - } else { - clearContent(); + currentScope = $scope.$new(); + if (segment.params.controller) { + locals.$scope = currentScope; + controller = $controller(segment.params.controller, locals); + if(segment.params.controllerAs) + currentScope[segment.params.controllerAs] = controller; + currentElement.data('$ngControllerController', controller); + currentElement.children().data('$ngControllerController', controller); } + + link(currentScope); + currentScope.$emit('$viewContentLoaded'); + currentScope.$eval(onloadExp); } } } } }]); -})(angular); \ No newline at end of file +})(angular); diff --git a/build/angular-route-segment.min.js b/build/angular-route-segment.min.js index 5d154f9..931bbb6 100644 --- a/build/angular-route-segment.min.js +++ b/build/angular-route-segment.min.js @@ -1,7 +1,7 @@ /** - * angular-route-segment v1.1.0 + * angular-route-segment v1.2.0 * https://angular-route-segment.com * @author Artem Chivchalov * @license MIT License http://opensource.org/licenses/MIT */ -"use strict";!function(a){a.module("route-segment",[]).provider("$routeSegment",["$routeProvider",function(b){function c(a){return a.replace(/([\:\-\_]+(.))/g,function(a,b,c,d){return d?c.toUpperCase():c})}function d(a,b){if(!a)throw new Error("Invalid pointer segment");var e;return{segment:function(b,d){return a[c(b)]={params:d},e=b,this},within:function(b){var g;if(b=b||e,g=a[c(b)])void 0==g.children&&(g.children={});else{if(f.strictMode)throw new Error("Cannot get into unknown `"+b+"` segment");g=a[c(b)]={params:{},children:{}}}return d(g.children,this)},up:function(){return b},root:function(){return h}}}var e=this,f=e.options={autoLoadTemplates:!1,strictMode:!1},g=this.segments={},h=d(g,null);e.when=function(a,c){return b.when(a,{segment:c}),this},a.extend(e,h),this.$get=["$rootScope","$q","$http","$templateCache","$route","$routeParams","$injector",function(b,d,e,h,i,j,k){function l(b){var c=!1;return b.params.dependencies&&a.forEach(b.params.dependencies,function(b){a.equals(q.$routeParams[b],j[b])||(c=!0)}),c}function m(a,b){return q.chain[a]&&q.chain[a].clearWatcher&&q.chain[a].clearWatcher(),b?(r[a]=b.name,b.params.untilResolved?n(a,b.name,b.params.untilResolved).then(function(c){return void 0!=c.success&&o(a),n(a,b.name,b.params)}):n(a,b.name,b.params)):(r[a]=null,o(a),void 0)}function n(c,g,i){var j=a.extend({},i.resolve);return a.forEach(j,function(b,c){j[c]=a.isString(b)?k.get(b):k.invoke(b)}),i.template&&(j.$template=i.template),f.autoLoadTemplates&&i.templateUrl&&(j.$template=e.get(i.templateUrl,{cache:h}).then(function(a){return a.data})),d.all(j).then(function(e){if(r[c]!=g)return d.reject();if(q.chain[c]={name:g,params:i,locals:e,reload:function(){m(c,this).then(function(a){void 0!=a.success&&o(c)})}},i.watcher){var f=function(){if(!a.isFunction(i.watcher))throw new Error("Watcher is not a function in segment `"+g+"`");return k.invoke(i.watcher,{},{segment:q.chain[c]})},h=f();q.chain[c].clearWatcher=b.$watch(f,function(a){a!=h&&(h=a,q.chain[c].reload())})}return{success:c}},function(b){if(i.resolveFailed){var e={error:function(){return d.when(b)}};return n(c,g,a.extend({resolve:e},i.resolveFailed))}throw new Error("Resolving failed with a reason `"+b+"`, but no `resolveFailed` "+"provided for segment `"+g+"`")})}function o(a){b.$broadcast("routeSegmentChange",{index:a,segment:q.chain[a]||null})}function p(a,b){if(!b)return null;if(a>=b.length)return null;for(var d,e=g,f=0;a>=f;f++)d=b[f],void 0!=e[c(d)]&&(e=e[c(d)]),a>f&&(e=e.children);return{name:d,params:e.params}}var q={name:"",$routeParams:a.copy(j),chain:[],startsWith:function(a){var b=new RegExp("^"+a);return b.test(q.name)},contains:function(a){for(var b=0;bg.length){var d=q.chain.length,e=q.chain.length-g.length;q.chain.splice(-e,e);for(var c=g.length;d>c;c++)m(c,null)}})}}),q}]}])}(angular),function(a){a.module("view-segment",["route-segment"]).directive("appViewSegment",["$route","$compile","$controller","$routeParams","$routeSegment","$q","$injector",function(b,c,d,e,f,g,h){return{restrict:"ECA",priority:500,compile:function(b,e){var g=b.html(),i=!0,j=a.element(document.createComment(" view-segment "));return b.prepend(j),function(k){function l(){o&&(p.leave(o),o=null),n&&(n.$destroy(),n=null)}function m(e){if(i&&(i=!1,b.replaceWith(j)),!e)return l(),o=b.clone(),o.html(g),p.enter(o,null,j),c(o,!1,499)(k),void 0;var f=a.extend({},e.locals),h=f&&f.$template;if(h){l(),o=b.clone(),o.html(h),p.enter(o,null,j);var m,r=c(o,!1,499);n=k.$new(),e.params.controller&&(f.$scope=n,m=d(e.params.controller,f),e.params.controllerAs&&(n[e.params.controllerAs]=m),o.data("$ngControllerController",m),o.children().data("$ngControllerController",m)),r(n),n.$emit("$viewContentLoaded"),n.$eval(q)}else l()}var n,o,p,q=e.onload||"",r=parseInt(e.appViewSegment);try{var s=h.get("$animator");p=s(k,e)}catch(t){}try{p=h.get("$animate")}catch(t){}f.chain[r]&&m(f.chain[r]),k.$on("routeSegmentChange",function(a,b){b.index==r&&m(b.segment)})}}}}])}(angular); \ No newline at end of file +"use strict";!function(a){a.module("route-segment",[]).provider("$routeSegment",["$routeProvider",function(b){function c(a){return a.replace(/([\:\-\_]+(.))/g,function(a,b,c,d){return d?c.toUpperCase():c})}function d(a,b){if(!a)throw new Error("Invalid pointer segment");var e;return{segment:function(b,d){return a[c(b)]={params:d},e=b,this},within:function(b){var g;if(b=b||e,g=a[c(b)])void 0==g.children&&(g.children={});else{if(f.strictMode)throw new Error("Cannot get into unknown `"+b+"` segment");g=a[c(b)]={params:{},children:{}}}return d(g.children,this)},up:function(){return b},root:function(){return h}}}var e=this,f=e.options={autoLoadTemplates:!1,strictMode:!1},g=this.segments={},h=d(g,null);e.when=function(a,c){return b.when(a,{segment:c}),this},a.extend(e,h),this.$get=["$rootScope","$q","$http","$templateCache","$route","$routeParams","$injector",function(b,d,e,h,i,j,k){function l(b){var c=!1;return b.params.dependencies&&a.forEach(b.params.dependencies,function(b){a.equals(q.$routeParams[b],j[b])||(c=!0)}),c}function m(a,b){return q.chain[a]&&q.chain[a].clearWatcher&&q.chain[a].clearWatcher(),b?(r[a]=b.name,b.params.untilResolved?n(a,b.name,b.params.untilResolved).then(function(c){return void 0!=c.success&&o(a),n(a,b.name,b.params)}):n(a,b.name,b.params)):(r[a]=null,o(a),void 0)}function n(c,g,i){var j=a.extend({},i.resolve);return a.forEach(j,function(b,c){j[c]=a.isString(b)?k.get(b):k.invoke(b)}),i.template&&(j.$template=i.template),f.autoLoadTemplates&&i.templateUrl&&(j.$template=e.get(i.templateUrl,{cache:h}).then(function(a){return a.data})),d.all(j).then(function(e){if(r[c]!=g)return d.reject();if(q.chain[c]={name:g,params:i,locals:e,reload:function(){m(c,this).then(function(a){void 0!=a.success&&o(c)})}},i.watcher){var f=function(){if(!a.isFunction(i.watcher))throw new Error("Watcher is not a function in segment `"+g+"`");return k.invoke(i.watcher,{},{segment:q.chain[c]})},h=f();q.chain[c].clearWatcher=b.$watch(f,function(a){a!=h&&(h=a,q.chain[c].reload())})}return{success:c}},function(b){if(i.resolveFailed){var e={error:function(){return d.when(b)}};return n(c,g,a.extend({resolve:e},i.resolveFailed))}throw new Error("Resolving failed with a reason `"+b+"`, but no `resolveFailed` "+"provided for segment `"+g+"`")})}function o(c){q.$routeParams=a.copy(j),q.name="";for(var d=0;d=b.length)return null;for(var d,e=g,f=0;a>=f;f++)d=b[f],void 0!=e[c(d)]&&(e=e[c(d)]),a>f&&(e=e.children);return{name:d,params:e.params}}var q={name:"",$routeParams:a.copy(j),chain:[],startsWith:function(a){var b=new RegExp("^"+a);return b.test(q.name)},contains:function(a){for(var b=0;b0)for(var h=0;hf.length){var a=q.chain.length,b=q.chain.length-f.length;q.chain.splice(-b,b);for(var c=f.length;a>c;c++)m(c,null)}})}}),q}]}])}(angular),function(a){a.module("view-segment",["route-segment"]).directive("appViewSegment",["$route","$compile","$controller","$routeParams","$routeSegment","$q","$injector",function(b,c,d,e,f,g,h){return{restrict:"ECA",priority:500,compile:function(b,e){var g=b.html(),i=!0,j=a.element(document.createComment(" view-segment "));return b.prepend(j),function(k){function l(){o&&(q.leave(o),o=null),n&&(n.$destroy(),n=null)}function m(e){if(p=e,i&&(i=!1,b.replaceWith(j)),!e)return l(),o=b.clone(),o.html(g),q.enter(o,null,j),c(o,!1,499)(k),void 0;var f=a.extend({},e.locals),h=f&&f.$template;l(),o=b.clone(),o.html(h?h:g),q.enter(o,null,j);var m,s=c(o,!1,499);n=k.$new(),e.params.controller&&(f.$scope=n,m=d(e.params.controller,f),e.params.controllerAs&&(n[e.params.controllerAs]=m),o.data("$ngControllerController",m),o.children().data("$ngControllerController",m)),s(n),n.$emit("$viewContentLoaded"),n.$eval(r)}var n,o,p,q,r=e.onload||"",s=parseInt(e.appViewSegment);try{var t=h.get("$animator");q=t(k,e)}catch(u){}try{q=h.get("$animate")}catch(u){}f.chain[s]&&m(f.chain[s]),k.$on("routeSegmentChange",function(a,b){b.index==s&&p!=b.segment&&m(b.segment)})}}}}])}(angular); \ No newline at end of file