1
- /*! angular-breadcrumb - v0.4.1
1
+ /*! angular-breadcrumb - v0.5.0
2
2
* http://ncuillery.github.io/angular-breadcrumb
3
- * Copyright (c) 2015 Nicolas Cuillery; Licensed MIT */
3
+ * Copyright (c) 2016 Nicolas Cuillery; Licensed MIT */
4
4
5
5
( function ( window , angular , undefined ) {
6
6
'use strict' ;
@@ -19,12 +19,24 @@ function parseStateRef(ref) {
19
19
return { state : parsed [ 1 ] , paramExpr : parsed [ 3 ] || null } ;
20
20
}
21
21
22
+ var $registeredListeners = { } ;
23
+ function registerListenerOnce ( tag , $rootScope , event , fn ) {
24
+ var deregisterListenerFn = $registeredListeners [ tag ] ;
25
+ if ( deregisterListenerFn !== undefined ) {
26
+ deregisterListenerFn ( ) ;
27
+ }
28
+ deregisterListenerFn = $rootScope . $on ( event , fn ) ;
29
+ $registeredListeners [ tag ] = deregisterListenerFn ;
30
+ }
31
+
22
32
function $Breadcrumb ( ) {
23
33
24
34
var $$options = {
25
35
prefixStateName : null ,
26
36
template : 'bootstrap3' ,
27
37
templateUrl : null ,
38
+ templateLast : 'default' ,
39
+ templateLastUrl : null ,
28
40
includeAbstract : false
29
41
} ;
30
42
@@ -37,7 +49,7 @@ function $Breadcrumb() {
37
49
var $lastViewScope = $rootScope ;
38
50
39
51
// Early catch of $viewContentLoaded event
40
- $rootScope . $on ( '$viewContentLoaded' , function ( event ) {
52
+ registerListenerOnce ( '$Breadcrumb.$viewContentLoaded' , $rootScope , '$viewContentLoaded' , function ( event ) {
41
53
// With nested views, the event occur several times, in "wrong" order
42
54
if ( ! event . targetScope . ncyBreadcrumbIgnore &&
43
55
isAOlderThanB ( event . targetScope . $id , $lastViewScope . $id ) ) {
@@ -80,6 +92,7 @@ function $Breadcrumb() {
80
92
}
81
93
82
94
conf . ncyBreadcrumbLink = $state . href ( ref . state , parentParams || $stateParams || { } ) ;
95
+ conf . ncyBreadcrumbStateRef = stateRef ;
83
96
chain . unshift ( conf ) ;
84
97
}
85
98
} ;
@@ -119,6 +132,22 @@ function $Breadcrumb() {
119
132
return $$options . templateUrl ;
120
133
} ,
121
134
135
+ getTemplateLast : function ( templates ) {
136
+ if ( $$options . templateLastUrl ) {
137
+ // templateUrl takes precedence over template
138
+ return null ;
139
+ } else if ( templates [ $$options . templateLast ] ) {
140
+ // Predefined templates (default)
141
+ return templates [ $$options . templateLast ] ;
142
+ } else {
143
+ return $$options . templateLast ;
144
+ }
145
+ } ,
146
+
147
+ getTemplateLastUrl : function ( ) {
148
+ return $$options . templateLastUrl ;
149
+ } ,
150
+
122
151
getStatesChain : function ( exitOnFirst ) { // Deliberately undocumented param, see getLastStep
123
152
var chain = [ ] ;
124
153
@@ -154,6 +183,7 @@ var getExpression = function(interpolationFunction) {
154
183
if ( interpolationFunction . expressions ) {
155
184
return interpolationFunction . expressions ;
156
185
} else {
186
+ // Workaround for Angular 1.2.x
157
187
var expressions = [ ] ;
158
188
angular . forEach ( interpolationFunction . parts , function ( part ) {
159
189
if ( angular . isFunction ( part ) ) {
@@ -210,7 +240,7 @@ function BreadcrumbDirective($interpolate, $breadcrumb, $rootScope) {
210
240
var renderBreadcrumb = function ( ) {
211
241
deregisterWatchers ( labelWatchers ) ;
212
242
labelWatchers = [ ] ;
213
-
243
+
214
244
var viewScope = $breadcrumb . $getLastViewScope ( ) ;
215
245
scope . steps = $breadcrumb . getStatesChain ( ) ;
216
246
angular . forEach ( scope . steps , function ( step ) {
@@ -225,7 +255,7 @@ function BreadcrumbDirective($interpolate, $breadcrumb, $rootScope) {
225
255
} ) ;
226
256
} ;
227
257
228
- $rootScope . $on ( '$viewContentLoaded' , function ( event ) {
258
+ registerListenerOnce ( 'BreadcrumbDirective.$viewContentLoaded' , $rootScope , '$viewContentLoaded' , function ( event ) {
229
259
if ( ! event . targetScope . ncyBreadcrumbIgnore ) {
230
260
renderBreadcrumb ( ) ;
231
261
}
@@ -240,14 +270,20 @@ function BreadcrumbDirective($interpolate, $breadcrumb, $rootScope) {
240
270
BreadcrumbDirective . $inject = [ '$interpolate' , '$breadcrumb' , '$rootScope' ] ;
241
271
242
272
function BreadcrumbLastDirective ( $interpolate , $breadcrumb , $rootScope ) {
273
+ var $$templates = {
274
+ 'default' : '{{ncyBreadcrumbLabel}}'
275
+ } ;
243
276
244
277
return {
245
278
restrict : 'A' ,
246
279
scope : { } ,
247
- template : '{{ncyBreadcrumbLabel}}' ,
280
+ template : $breadcrumb . getTemplateLast ( $$templates ) ,
281
+ templateUrl : $breadcrumb . getTemplateLastUrl ( ) ,
248
282
compile : function ( cElement , cAttrs ) {
249
283
250
284
// Override the default template if ncyBreadcrumbLast has a value
285
+ // This should likely be removed in a future version since global
286
+ // templating is now available for ncyBreadcrumbLast
251
287
var template = cElement . attr ( cAttrs . $attr . ncyBreadcrumbLast ) ;
252
288
if ( template ) {
253
289
cElement . html ( template ) ;
@@ -260,7 +296,7 @@ function BreadcrumbLastDirective($interpolate, $breadcrumb, $rootScope) {
260
296
var renderLabel = function ( ) {
261
297
deregisterWatchers ( labelWatchers ) ;
262
298
labelWatchers = [ ] ;
263
-
299
+
264
300
var viewScope = $breadcrumb . $getLastViewScope ( ) ;
265
301
var lastStep = $breadcrumb . getLastStep ( ) ;
266
302
if ( lastStep ) {
@@ -277,7 +313,7 @@ function BreadcrumbLastDirective($interpolate, $breadcrumb, $rootScope) {
277
313
}
278
314
} ;
279
315
280
- $rootScope . $on ( '$viewContentLoaded' , function ( event ) {
316
+ registerListenerOnce ( 'BreadcrumbLastDirective.$viewContentLoaded' , $rootScope , '$viewContentLoaded' , function ( event ) {
281
317
if ( ! event . targetScope . ncyBreadcrumbIgnore ) {
282
318
renderLabel ( ) ;
283
319
}
@@ -306,13 +342,13 @@ function BreadcrumbTextDirective($interpolate, $breadcrumb, $rootScope) {
306
342
if ( template ) {
307
343
cElement . html ( template ) ;
308
344
}
309
-
345
+
310
346
var separator = cElement . attr ( cAttrs . $attr . ncyBreadcrumbTextSeparator ) || ' / ' ;
311
347
312
348
return {
313
349
post : function postLink ( scope ) {
314
350
var labelWatchers = [ ] ;
315
-
351
+
316
352
var registerWatchersText = function ( labelWatcherArray , interpolationFunction , viewScope ) {
317
353
angular . forEach ( getExpression ( interpolationFunction ) , function ( expression ) {
318
354
var watcher = viewScope . $watch ( expression , function ( newValue , oldValue ) {
@@ -327,7 +363,7 @@ function BreadcrumbTextDirective($interpolate, $breadcrumb, $rootScope) {
327
363
var renderLabel = function ( ) {
328
364
deregisterWatchers ( labelWatchers ) ;
329
365
labelWatchers = [ ] ;
330
-
366
+
331
367
var viewScope = $breadcrumb . $getLastViewScope ( ) ;
332
368
var steps = $breadcrumb . getStatesChain ( ) ;
333
369
var combinedLabels = [ ] ;
@@ -341,11 +377,11 @@ function BreadcrumbTextDirective($interpolate, $breadcrumb, $rootScope) {
341
377
combinedLabels . push ( step . name ) ;
342
378
}
343
379
} ) ;
344
-
380
+
345
381
scope . ncyBreadcrumbChain = combinedLabels . join ( separator ) ;
346
382
} ;
347
383
348
- $rootScope . $on ( '$viewContentLoaded' , function ( event ) {
384
+ registerListenerOnce ( 'BreadcrumbTextDirective.$viewContentLoaded' , $rootScope , '$viewContentLoaded' , function ( event ) {
349
385
if ( ! event . targetScope . ncyBreadcrumbIgnore ) {
350
386
renderLabel ( ) ;
351
387
}
0 commit comments