/* Authors: * Petr Vobornik * * Copyright (C) 2014 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 . */ define([ 'dojo/_base/declare', 'dojo/_base/lang', 'dojo/_base/array', 'dojo/io-query', '../reg', '../util' ], function(declare, lang, array, ioquery, reg, util) { /** * Routing mechanism * @class navigation.routing * @singleton */ var routing = { /** * Router instance * @property {navigation.Router} */ router: null, /** * Map of router handlers * @property {Object} */ route_handlers: {}, /** * Map of hash creators * @property {Object} */ hash_creators: {}, /** * Facet name to hash creator map * * - Key: facet name * - Value: hash creator * * @type {Object} */ hc_facet_map: {}, /** * Hash creator priority queue * * First item == highest priority * * @type {Array} */ hc_queue: [], /** * Map of navigators * @type {Object} */ navigators: {}, /** * Add hash creator at the beginning of hash creator queue * @param {navigation.routing.HashCreator} hash_creator * @param {Number} [position] */ add_hash_creator: function(hash_creator, position) { if (position !== undefined) { this.hc_queue.splice(position, 0, hash_creator); } else { this.hc_queue.unshift(hash_creator); } }, /** * Add hash creator to hash creator map * @param {string} facet_name * @param {navigation.routing.HashCreator} hash_creator */ assign_hash_creator: function (facet_name, hash_creator) { this.hc_facet_map[facet_name] = hash_creator; }, /** * Get hash creator for given facet * * Lookup priority: * * - facet -> hash creator map * - hash creator queue * * @param {facets.Facet} facet [description] * @return {navigation.routing.HashCreator} */ get_hash_creator: function(facet) { var name = facet.name; var hc = this.hc_facet_map[name]; if (!hc) { for (var i=0, l=this.hc_queue.length; i ['foo ', 'bar'] */ _decode_pkeys: function(str) { if (!str) return []; var keys = str.split('&'); for (var i=0; i 'foo%20&bar' */ _encode_pkeys: function(pkeys) { var ret = []; array.forEach(pkeys, function(pkey) { ret.push(encodeURIComponent(pkey)); }); return ret.join('&'); } }); /** * Navigate to other facet. * * @class navigation.routing.Navigator */ routing.Navigator = declare([], { name: 'generic', navigate: function(facet_name, args) { var facet = reg.facet.get(facet_name); if (!facet) { routing.router._error('Unknown facet', 'navigation', { facet: facet_name}); return false; } if (!args) args = facet.get_state(); return routing.navigate_to_facet(facet, args); } }); /** * Used for switching to entities' 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). * * @class navigation.routing.EntityNavigator * @extends navigation.routing.Navigator */ routing.EntityNavigator = declare([routing.Navigator], { name: 'entity', navigate: function(entity_name, facet_name, pkeys, args) { var entity = reg.entity.get(entity_name); if (!entity) { routing.router._error('Unknown entity', 'navigation', { entity: entity_name}); return false; } var facet = entity.get_facet(facet_name); if (!facet) { routing.router._error('Unknown facet', 'navigation', { facet: facet_name}); return false; } // 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); return routing.navigate_to_facet(facet, args); } }); /** * Init routing * * Sets default routes, handlers, hash creators and navigators * * @param {navigation.Router} router */ routing.init = function(router) { if (router) this.router = router; var generic_hc = new routing.HashCreator(); var entity_hc = new routing.EntityHashCreator(); var generic_rh = new routing.RouteHandler(); var entity_rh = new routing.EntityRouteHandler(); var generic_n = new routing.Navigator(); var entity_n = new routing.EntityNavigator(); this.add_hash_creator(generic_hc); this.add_hash_creator(entity_hc); this.add_route(this.page_routes, generic_rh); this.add_route(this.entity_routes, entity_rh); this.add_navigator(generic_n); this.add_navigator(entity_n); }; return routing; });