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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
|
/* Authors:
* Petr Vobornik <pvoborni@redhat.com>
*
* Copyright (C) 2012 Red Hat
* see file 'COPYING' for use and warranty information
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
define(['dojo/_base/declare',
'dojo/router',
'dojo/_base/lang',
'dojo/_base/array',
'dojo/io-query',
'dojo/topic',
'../entities',
'../facets',
'../ipa' //TODO: remove dependancy
],
function(declare, router, lang, array, ioquery, topic, entities, facets, IPA) {
/**
* Class navigation
* This component keeps menu and routes in sync. It signalizes
* other components to display facet by sending 'show-facet' event.
* Other components can use navigate_to_* methods to change currently
* displayed facet. This change can be canceled in 'facet-change'
* event handler.
*/
var navigation = declare(null, {
/**
* Holds references to register route handlers.
* Can be used for unregistering routes.
* @type Array
*/
route_handlers: [],
/**
* Prefix of all routes for this navigation. Useful for multiple
* navigation objects in one application.
* @type String
*/
route_prefix: '',
/**
* Variations of entity routes
*/
entity_routes: [
'/e/:entity/:facet/:pkeys/*args',
'/e/:entity/:facet//*args',
'/e/:entity/:facet/:pkeys',
'/e/:entity/:facet',
'/e/:entity'
],
/**
* Variations of simple page routes
*/
page_routes: [
'/p/:page/*args',
'/p/:page'
],
/**
* Used during facet changing. Set it to true in 'facet-change'
* event handler to stop the change.
* @type Boolean
*/
canceled: false,
/**
* Flag to indicate that next hash change should not invoke showing a
* facet.
* Main purpose: updating hash.
* @type Boolen
*/
ignore_next: false,
/**
* Register a route-handler pair to a dojo.router
* Handler will be run in context of this object
*
* @param {string|array} route or routes to register
* @param {function} handler to be associated with the route(s)
*/
register_route: function(route, handler) {
// TODO: add multiple routes for one handler
route = this.route_prefix + route;
this.route_handlers.push(router.register(route, lang.hitch(this, handler)));
},
/**
* Initializates router
* - registers handlers
*/
init_router: function() {
// entity pages
array.forEach(this.entity_routes, function(route) {
this.register_route(route, this.entity_route_handler);
}, this);
// special pages
this.register_route(this.page_routes, this.page_route_handler);
},
/**
* Handler for entity routes
* Shouldn't be invoked directly.
*/
entity_route_handler: function(event) {
if (this.check_clear_ignore()) return;
var entity_name = event.params.entity;
var facet_name = event.params.facet;
var pkeys = this._decode_pkeys(event.params.pkeys || '');
var args = ioquery.queryToObject(event.params.args || '');
args.pkeys = pkeys;
// set new facet state
//var entity = entities.get(entity_name);
var entity = IPA.get_entity(entity_name); // TODO: replace with prev line
var facet = entity.get_facet(facet_name);
facet.set_state(args);
this.show_facet(facet);
},
/**
* General facet route handler
* Shouldn't be invoked directly.
*/
page_route_handler: function(event) {
if (this.check_clear_ignore()) return;
var facet_name = event.params.page;
var args = ioquery.queryToObject(event.params.args || '');
// // Find menu item
// var items = this.menu.items.query({ page: facet_name });
//
// // Select menu item
// if (items.total > 0) {
// this.menu.select(items[items.total-1]);
// }
// set new facet state
var facet = facets.get(facet_name);
facet.set_state(args);
this.show_facet(facet);
},
/**
* Used for switching to entitie's facets. Current target facet
* state is used as params (pkeys, args) when none of pkeys and args
* are used (useful for switching to previous page with keeping the context).
*/
navigate_to_entity_facet: function(entity_name, facet_name, pkeys, args) {
//var entity = entities.get(entity_name);
var entity = IPA.get_entity(entity_name); // TODO: replace with prev line
var facet = entity.get_facet(facet_name);
if (!facet) return false; // TODO: maybe replace with exception
// Use current state if none supplied
if (!pkeys && !args) {
args = facet.get_state();
}
args = args || {};
// Facets may be nested and require more pkeys than supplied.
args.pkeys = facet.get_pkeys(pkeys);
var hash = this._create_entity_facet_hash(facet, args);
return this.navigate_to_hash(hash, facet);
},
/**
* Navigate to other facet.
*/
navigate_to_facet: function(facet_name, args) {
// TODO: uncoment when `facets` are implemented
// var facet = facets.get(facet_name);
// if (!args) args = facet.get_args();
// var hash = this._create_facet_hash(facet, { args: args });
// return this.navigate_to_hash(hash, facet);
},
/**
* Low level function.
*
* Public usage should be limited reinitializing canceled navigations.
*/
navigate_to_hash: function(hash, facet) {
this.canceled = false;
topic.publish('facet-change', { facet: facet, hash: hash });
if (this.canceled) {
topic.publish('facet-change-canceled', { facet: facet, hash : hash });
return false;
}
this.update_hash(hash, false);
return true;
},
/**
* Changes hash to supplied
*
* @param {String} Hash to set
* @param {Boolean} Whether to suppress following hash change handler
*/
update_hash: function(hash, ignore_change) {
this.ignore_next = !!ignore_change;
router.go(hash);
},
/**
* Returns and resets `ignore_next` property.
*/
check_clear_ignore: function() {
var ignore = this.ignore_next;
this.ignore_next = false;
return ignore;
},
/**
* Creates from facet state appropriate hash.
*/
_create_entity_facet_hash: function(facet, state) {
state = lang.clone(state);
var entity_name = facet.entity.name;
var pkeys = this._encode_pkeys(state.pkeys || []);
delete state.pkeys;
var args = ioquery.objectToQuery(state || {});
var path = [this.route_prefix, 'e', entity_name, facet.name];
if (!IPA.is_empty(args)) path.push(pkeys, args);
else if (!IPA.is_empty(pkeys)) path.push(pkeys);
var hash = path.join('/');
return hash;
},
/**
* Creates hash of general facet.
*/
_create_facet_hash: function(facet, state) {
var args = ioquery.objectToQuery(state.args || {});
var path = [this.route_prefix, 'p', facet.name];
if (!IPA.is_empty(args)) path.push(args);
var hash = path.join('/');
return hash;
},
/**
* Creates hash from supplied facet and state.
*
* @param {facet} facet
* @param {Object} state
*/
create_hash: function(facet, state) {
if (facet.entity) return this._create_entity_facet_hash(facet, state);
else return this._create_facet_hash(facet, state);
},
/**
* Tells other component to show given facet.
*/
show_facet: function(facet) {
topic.publish('facet-show', {
facet: facet
});
},
/**
* URI Encodes array items and delimits them by '&'
* Example: ['foo ', 'bar'] => 'foo%20&bar'
*/
_encode_pkeys: function(pkeys) {
var ret = [];
array.forEach(pkeys, function(pkey) {
ret.push(encodeURIComponent(pkey));
});
return ret.join('&');
},
/**
* Splits strings by '&' and return an array of URI decoded parts.
* Example: 'foo%20&bar' => ['foo ', 'bar']
*/
_decode_pkeys: function(str) {
var keys = str.split('&');
for (var i=0; i<keys.length; i++) {
keys[i] = decodeURIComponent(keys[i]);
}
return keys;
},
/**
* Starts routing
*/
startup: function() {
router.startup();
},
constructor: function(spec) {
spec = spec || {};
this.init_router();
}
});
return navigation;
});
|