| 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266 |
1
1
1
5
1
1
1
18
2
2
1
1
18
18
18
18
3
15
18
17
17
2
2
17
17
1
8
8
8
8
16
7
7
7
1
7
7
7
9
16
8
8
2
8
7
8
5
8
| 'use strict';
;require.register("views/common/breadcrumbs_view", function (exports, require, module) {
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
var App = require('app');
/**
* @param {string} labelBindingPath
* @returns {string}
* @private
*/
function _getLabelPathWithoutApp(labelBindingPath) {
return labelBindingPath.startsWith('App.') ? labelBindingPath.replace('App.', '') : labelBindingPath;
}
/**
* @param {string} stateName
* @returns {string}
* @private
*/
function _formatLabel(stateName) {
return stateName.capitalize().replace(/([a-z])([A-Z])/g, '$1 $2');
}
/**
* Don't create instances directly
* Only <code>App.BreadcrumbsView</code>-instance will create them
*
* @type {Em.Object}
*/
App.BreadcrumbItem = Em.Object.extend({
/**
* String shown as breadcrumb
*
* @type {string}
*/
label: '',
/**
* Path to variable that will be used as breadcrumb
* If <code>labelBindingPath</code> is <code>'App.router.somePath'</code>, its value will be used
*
* @type {string}
*/
labelBindingPath: '',
/**
* View shown as breadcrumb.
* If provied, <code>itemView</code> supersedes <code>label</code> and <code>labelBindingPath</code>.
*
* @type {object}
*/
itemView: null,
/**
* Determines if breadcrumb is disabled
*
* @type {boolean}
*/
disabled: false,
/**
* Check if current breadcrumb is last
*
* @type {boolean}
*/
isLast: false,
/**
* Invoke this action when click on breadcrumb item
* If provided, <code>action</code> supersedes <code>route</code>.
*
* @type {Function}
*/
action: null,
/**
* Move user to this route when click on breadcrumb item (don't add prefix <code>main</code>)
* This is used if an action is not defined.
*
* @type {string}
*/
route: '',
/**
* Hook executed before transition
* may be overridden when needed
*
* @type {Function}
*/
beforeTransition: Em.K,
/**
* Hook executed after transition
* may be overridden when needed
*
* @type {Function}
*/
afterTransition: Em.K,
/**
* Label shown on the page
* Result of <code>createLabel</code> execution
*
* @type {string}
*/
formattedLabel: '',
/**
* Hook for label formatting
* It's executed after <code>label</code> or <code>labelBindingPath</code> is processed
*
* @param {string} label
* @returns {string}
*/
labelPostFormat: function labelPostFormat(label) {
return label;
},
transition: function transition() {
var action = this.get('action');
if (action) {
return action();
} else {
return App.router.route('main/' + this.get('route'));
}
},
/**
* Generate <code>formattedLabel</code> shown on the page
*
* @method createLabel
*/
createLabel: function createLabel() {
var label = this.get('label');
var labelBindingPath = this.get('labelBindingPath');
var formattedLabel = void 0;
if (labelBindingPath) {
formattedLabel = Ember.Handlebars.Utils.escapeExpression(App.get(_getLabelPathWithoutApp(labelBindingPath)));
} else {
formattedLabel = label;
}
this.set('formattedLabel', this.labelPostFormat(formattedLabel));
},
/**
* If <code>labelBindingPath</code> is provided, <code>createLabel</code> should observe value in path <code>${labelBindingPath}</code>
*
* @returns {*}
*/
init: function init() {
var labelBindingPath = this.get('labelBindingPath');
if (labelBindingPath) {
labelBindingPath = 'App.' + _getLabelPathWithoutApp(labelBindingPath);
this.addObserver(labelBindingPath, this, 'createLabel');
}
this.createLabel();
return this._super.apply(this, arguments);
}
});
/**
* Usage:
* <code>{{view App.BreadcrumbsView}}</code>
*
* @type {Em.View}
*/
App.BreadcrumbsView = Em.View.extend({
templateName: require('templates/common/breadcrumbs'),
/**
* List of the breadcrumbs
* It's updated if <code>App.router.currentState</code> is changed. This happens when user is moved from one page to another
*
* @type {BreadcrumbItem[]}
*/
items: function () {
var currentState = App.get('router.currentState');
var items = [];
var wizardStepRegex = /^step[0-9]?/;
while (currentState) {
if (currentState.breadcrumbs !== undefined) {
// breadcrumbs should be defined and be not null or any other falsie-value
Eif (currentState.breadcrumbs) {
var _currentState$breadcr = currentState.breadcrumbs,
label = _currentState$breadcr.label,
labelBindingPath = _currentState$breadcr.labelBindingPath,
route = _currentState$breadcr.route,
disabled = _currentState$breadcr.disabled;
// generate label if it isn't provided
if (!label && !labelBindingPath) {
currentState.breadcrumbs.label = _formatLabel(currentState.name);
}
// generate route if it isn't provided and breadcrumb is not disabled
Eif (!route && !disabled) {
currentState.breadcrumbs.route = currentState.absoluteRoute(App.router).replace('/main/', '');
}
items.pushObject(currentState.breadcrumbs);
}
} else {
// generate breadcrumb if it is not defined
// breadcrumbs of wizard step such as "Step #" should be ignored
Iif (currentState.name && !['root', 'index'].contains(currentState.name) && !wizardStepRegex.test(currentState.name)) {
items.pushObject({ label: _formatLabel(currentState.name) });
}
}
currentState = currentState.get('parentState');
}
items.reverse();
items.slice(1).forEach(function (item) {
return item.label = Ember.Handlebars.Utils.escapeExpression(item.label);
});
items = items.map(function (item) {
return App.BreadcrumbItem.extend(item).create();
});
if (items.length) {
items.get('lastObject').setProperties({
disabled: true,
isLast: true
});
}
return items;
}.property('App.router.currentState'),
/**
* Move user to the route described in the breadcrumb item
* <code>beforeTransition</code> hook is executed
* <code>afterTransition</code> hook is executed
*
* @param {{context: App.BreadcrumbItem}} event
* @returns {*}
*/
moveTo: function moveTo(event) {
var item = event.context;
if (!item || item.get('disabled')) {
return;
}
Em.tryInvoke(item, 'beforeTransition');
Em.tryInvoke(item, 'transition');
Em.tryInvoke(item, 'afterTransition');
}
});
}); |