From 86898065b5e1d60168e2daff050853729b34f1ce Mon Sep 17 00:00:00 2001 From: Petr Vobornik Date: Fri, 16 May 2014 18:22:56 +0200 Subject: [PATCH] webui: generic routing Router is not able to create hash from facet state for custom routes/facets. This patch refactors router methods into providers. It allows to create additional route handlers, navigators and hash creators. These providers are mapped to facets and therefore it's possible to create router hash for any facet without any logic in the facet itself. Reviewed-By: Endi Sukma Dewata --- .../ui/src/freeipa/Application_controller.js | 18 +- install/ui/src/freeipa/navigation.js | 31 +- install/ui/src/freeipa/navigation/Router.js | 247 +-------- install/ui/src/freeipa/navigation/routing.js | 505 ++++++++++++++++++ 4 files changed, 540 insertions(+), 261 deletions(-) create mode 100644 install/ui/src/freeipa/navigation/routing.js diff --git a/install/ui/src/freeipa/Application_controller.js b/install/ui/src/freeipa/Application_controller.js index 5a0b67b80..9ed0bc6e9 100644 --- a/install/ui/src/freeipa/Application_controller.js +++ b/install/ui/src/freeipa/Application_controller.js @@ -35,10 +35,11 @@ define([ './reg', './navigation/Menu', './navigation/Router', + './navigation/routing', './navigation/menu_spec' ], function(declare, lang, array, Deferred, on, topic, query, dom_class, auth, - JSON, App_widget, FacetContainer, IPA, reg, Menu, Router, menu_spec) { + JSON, App_widget, FacetContainer, IPA, reg, Menu, Router, routing, menu_spec) { /** * Application controller @@ -78,6 +79,7 @@ define([ init: function() { this.menu = new Menu(); this.router = new Router(); + routing.init(this.router); var body_node = query('body')[0]; this.app_widget = new App_widget(); @@ -181,7 +183,7 @@ define([ if (IPA.is_selfservice) { this.on_profile(); } else { - this.router.navigate_to_entity_facet('user', 'search'); + routing.navigate(routing.default_path); } }, @@ -219,7 +221,7 @@ define([ }, on_profile: function() { - this.router.navigate_to_entity_facet('user', 'details', [IPA.whoami.uid[0]]); + routing.navigate(['entity', 'user', 'details', [IPA.whoami.uid[0]]]); }, on_logout: function(event) { @@ -287,8 +289,7 @@ define([ on_facet_state_changed: function(event) { if (event.facet === this.current_facet) { - var hash = this.router.create_hash(event.facet, event.state); - this.router.update_hash(hash, true); + routing.update_hash(event.facet, event.state); } }, @@ -405,14 +406,15 @@ define([ if (!child) { if(menu_item.entity) { // entity pages - this.router.navigate_to_entity_facet( + routing.navigate([ + 'entity', menu_item.entity, menu_item.facet, menu_item.pkeys, - menu_item.args); + menu_item.args]); } else if (menu_item.facet) { // concrete facets - this.router.navigate_to_facet(menu_item.facet, menu_item.args); + routing.navigate(['generic', menu_item.facet, menu_item.args]); } else { // categories, select first posible child, it may be the last var children = this.menu.query({parent: menu_item.name }); diff --git a/install/ui/src/freeipa/navigation.js b/install/ui/src/freeipa/navigation.js index 7cf309f15..105e867cb 100644 --- a/install/ui/src/freeipa/navigation.js +++ b/install/ui/src/freeipa/navigation.js @@ -21,16 +21,11 @@ define([ 'dojo/_base/lang', - './app_container', - './ipa' + './navigation/routing' ], - function(lang, app_container, IPA) { + function(lang, routing) { - var get_router = function() { - return app_container.app.router; - }; - var navigation = { /** * Navigation tells application to show certain facet. @@ -88,7 +83,6 @@ define([ */ show: function(arg1, arg2, arg3) { - var nav = get_router(); var params = {}; this.set_params(params, arg1); @@ -98,19 +92,20 @@ define([ var facet = params.facet; if (typeof facet === 'string') { - nav.navigate_to_facet(facet, params.args); + return routing.navigate(['generic', facet, params.args]); } if (!facet) throw 'Argument exception: missing facet'; if (facet && facet.entity) { - return nav.navigate_to_entity_facet( + return routing.navigate([ + 'entity', facet.entity.name, facet.name, params.pkeys, - params.args); + params.args]); } else { - return nav.navigate_to_facet(facet.name, params.args); + return routing.navigate(['generic', facet.name, params.args]); } }, @@ -128,14 +123,12 @@ define([ * @param {Object|facet.facet|string|Function} arg3 */ show_entity: function(entity_name, arg1, arg2, arg3) { - var nav = get_router(); var params = {}; - this.set_params(params, arg1); this.set_params(params, arg2); this.set_params(params, arg3); - return nav.navigate_to_entity_facet(entity_name, params.facet, - params.pkeys, params.args); + return routing.navigate(['entity', entity_name, params.facet, + params.pkeys, params.args]); }, /** @@ -150,8 +143,7 @@ define([ * notification purposes */ show_generic: function(hash, facet) { - var nav = get_router(); - nav.navigate_to_hash(hash, facet); + routing.router.navigate_to_hash(hash, facet); }, /** @@ -159,8 +151,7 @@ define([ * @method show_default */ show_default: function() { - // TODO: make configurable - return this.show_entity('user', 'search'); + routing.navigate(routing.default_path); } }; return navigation; diff --git a/install/ui/src/freeipa/navigation/Router.js b/install/ui/src/freeipa/navigation/Router.js index a3b2a6791..a65c60fd3 100644 --- a/install/ui/src/freeipa/navigation/Router.js +++ b/install/ui/src/freeipa/navigation/Router.js @@ -20,14 +20,10 @@ define(['dojo/_base/declare', 'dojo/_base/lang', - 'dojo/_base/array', 'dojo/Evented', - 'dojo/io-query', - 'dojo/router', - '../ipa', - '../reg' + 'dojo/router' ], - function(declare, lang, array, Evented, ioquery, router, IPA, reg) { + function(declare, lang, Evented, router) { /** * Router @@ -55,27 +51,6 @@ define(['dojo/_base/declare', */ route_prefix: '', - /** - * Variations of entity routes - * @property {Array.} - */ - 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 - * @property {Array.} - */ - page_routes: [ - '/p/:page/*args', - '/p/:page' - ], - /** * Used during facet changing. Set it to true in 'facet-change' * event handler to stop the change. @@ -100,145 +75,22 @@ define(['dojo/_base/declare', * @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))); + + if (route instanceof Array) { + for (var i=0, l=route.length; i '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) { - - if (!str) return []; - - var keys = str.split('&'); - for (var i=0; i + * + * 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.routes, generic_rh); + this.add_route(this.entity_routes, entity_rh); + this.add_navigator(generic_n); + this.add_navigator(entity_n); +}; + +return routing; + +}); \ No newline at end of file