From 306f380258354bae4d40758aec42f9f1c34ae2e7 Mon Sep 17 00:00:00 2001 From: Petr Vobornik Date: Mon, 21 May 2012 13:46:14 +0200 Subject: Refactored action list and control buttons to use shared list of actions This is a first step for implementing action panels which will also use the shared list of actions. This effor changes the way how action list and control buttons are defined. First all actions are defined on facet level - attribute 'actions' in spec file. Implementation of action list widget is not specified on facet level. It is left in facet header. A list of action names used in action list can be now specified in facet spec in 'header_actions' attribute. Control buttons use similar concept. Facet by default is using control_buttons_widget. Details and search facet are defining their own default actions (refresh/add/remove/update/reset). Additional buttons can be defined as array of action names on facet level in control_buttons attribute. state_evaluators and state_listeners were united. They are called state_evaluators but they uses state_listener concept, they are attached to an event. For former state_evaluator the event is post_load. They are defined in spec in state attribute. State object purpose is to aggregate states from all state evaluators. It offers changed event to which can other objects subscribe. It also has summary evaluator which evaluation conditions. Summary evaluator creates summary status with human readable description. It can be used by facet header. https://fedorahosted.org/freeipa/ticket/2248 --- install/ui/facet.js | 547 +++++++++++++++++++++++++++++++++++----------------- 1 file changed, 368 insertions(+), 179 deletions(-) (limited to 'install/ui/facet.js') diff --git a/install/ui/facet.js b/install/ui/facet.js index 31a9df73..ef8a0624 100644 --- a/install/ui/facet.js +++ b/install/ui/facet.js @@ -28,6 +28,8 @@ IPA.facet = function(spec, no_init) { spec = spec || {}; + spec.state = spec.state || {}; + $.extend(spec.state, { factory: IPA.state }); var that = {}; @@ -42,7 +44,10 @@ IPA.facet = function(spec, no_init) { that.disable_breadcrumb = spec.disable_breadcrumb; that.disable_facet_tabs = spec.disable_facet_tabs; - that.action_list = spec.action_list; + that.action_state = IPA.build(spec.state); + that.actions = IPA.build({ actions: spec.actions }, IPA.action_holder_builder); + + that.header_actions = spec.header_actions; that.header = spec.header || IPA.facet_header({ facet: that }); that._needs_update = spec.needs_update; @@ -299,16 +304,17 @@ IPA.facet = function(spec, no_init) { that.init_facet = function() { + that.action_state.init(that); + that.actions.init(that); + that.header.init(); + var buttons_spec = { factory: IPA.control_buttons_widget, name: 'control-buttons', - 'class': 'control-buttons' + 'class': 'control-buttons', + buttons: spec.control_buttons }; - if (spec.control_buttons) { - $.extend(buttons_spec, spec.control_buttons); - } - that.control_buttons = IPA.build(buttons_spec); that.control_buttons.init(that); }; @@ -335,9 +341,9 @@ IPA.facet_header = function(spec) { that.facet = spec.facet; - var init = function() { + that.init = function() { - if (that.facet.action_list) { + if (that.facet.header_actions) { var widget_builder = IPA.widget_builder({ widget_options: { @@ -345,10 +351,17 @@ IPA.facet_header = function(spec) { } }); - that.action_list = widget_builder.build_widget(that.facet.action_list); - that.action_list.init(); + var widget = { + factory: IPA.action_list_widget, + actions: that.facet.header_actions + }; + + that.action_list = widget_builder.build_widget(widget); + that.action_list.init(that.facet); } + that.facet.action_state.changed.attach(that.update_summary); + that.title_widget = IPA.facet_title(); }; @@ -587,17 +600,15 @@ IPA.facet_header = function(spec) { } } } + }; - if (that.action_list) { - that.action_list.update(result); - - var state = that.action_list.state; - var icon_tooltip = that.action_list.state_evaluator.get_description(); - if (state.length > 0) { - var css_class = state.join(' '); - that.title_widget.set_class(css_class); - that.title_widget.set_icon_tooltip(icon_tooltip); - } + that.update_summary = function() { + var summary = that.facet.action_state.summary(); + + if (summary.state.length > 0) { + var css_class = summary.state.join(' '); + that.title_widget.set_class(css_class); + that.title_widget.set_icon_tooltip(summary.description); } that.adjust_elements(); @@ -645,8 +656,6 @@ IPA.facet_header = function(spec) { that.load(); }; - init(); - return that; }; @@ -801,7 +810,7 @@ IPA.table_facet = function(spec, no_init) { that.table.pagination_control.css('visibility', 'visible'); - that.post_load.notify(); + that.post_load.notify([data], that); that.clear_expired_flag(); }; @@ -1245,19 +1254,79 @@ IPA.action = function(spec) { that.name = spec.name; that.label = spec.label; + + that.enabled = spec.enabled !== undefined ? spec.enabled : true; that.enable_cond = spec.enable_cond || []; that.disable_cond = spec.disable_cond || []; + that.enabled_changed = IPA.observer(); + + that.visible = spec.visible !== undefined ? spec.visible : true; + that.show_cond = spec.show_cond || []; + that.hide_cond = spec.hide_cond || []; + that.visible_changed = IPA.observer(); + that.handler = spec.handler; - that.needs_confirm = spec.needs_confirm !== undefined ? spec.needs_confirm : true; + + that.needs_confirm = spec.needs_confirm !== undefined ? spec.needs_confirm : false; that.confirm_msg = spec.confirm_msg || IPA.messages.actions.confirm; - that.execute = function(facet, on_success, on_error) { + + that.execute_action = function(facet, on_success, on_error) { if (that.handler) { that.handler(facet, on_success, on_error); } }; + that.execute = function(facet, on_success, on_error) { + + if (!that.enabled || !that.visible) return; + + if (that.needs_confirm) { + + var confirmed = false; + + if (that.confirm_dialog) { + + var dialog = IPA.build(that.confirm_dialog); + confirmed = dialog.confirm(that.facet); + } else { + var msg = that.get_confirm_message(); + confirmed = IPA.confirm(msg); + } + + if (!confirmed) return; + } + + that.execute_action(facet, on_success, on_error); + }; + + that.get_confirm_message = function() { + return that.confirm_msg; + }; + + that.set_enabled = function(enabled) { + + var old = that.enabled; + + that.enabled = enabled; + + if (old !== that.enabled) { + that.enabled_changed.notify([that.enabled], that); + } + }; + + that.set_visible = function(visible) { + + var old = that.visible; + + that.visible = visible; + + if (old !== that.visible) { + that.visible_changed.notify([that.visible], that); + } + }; + return that; }; @@ -1269,32 +1338,189 @@ IPA.action_builder = function(spec) { return that; }; -IPA.state_evaluator = function(spec) { + +IPA.action_holder = function(spec) { spec = spec || {}; var that = {}; - that.evaluate = function() { - that.state = []; - return that.state; + that.actions = $.ordered_map(); + + that.init = function(facet) { + + var i, action, actions; + + that.facet = facet; + actions = IPA.build(spec.actions, IPA.action_builder) || []; + + for (i=0; i 0) { that.state.push('item-selected'); } - that.state_changed.notify(); + that.notify_on_change(old_state); }; return that; }; -IPA.self_service_state_listener = function(spec) { +IPA.self_service_state_evaluator = function(spec) { spec = spec || {}; spec.event = spec.event || 'post_load'; - var that = IPA.state_listener(spec); + var that = IPA.state_evaluator(spec); + that.name = spec.name || 'self_service_state_evaluator'; that.on_event = function() { + + var old_state = that.state; that.state = []; var self_service = IPA.nav.name === 'self-service'; @@ -1382,7 +1619,7 @@ IPA.self_service_state_listener = function(spec) { that.state.push('self-service'); } - that.state_changed.notify(); + that.notify_on_change(old_state); }; return that; @@ -1400,17 +1637,22 @@ IPA.action_button_widget = function(spec) { that.href = spec.href || that.name; that.icon = spec.icon; - that.needs_confirm = spec.needs_confirm !== undefined ? spec.needs_confirm : false; - that.confirm_msg = spec.confirm_msg || IPA.messages.actions.confirm; - that.confirm_dialog = spec.confirm_dialog; + that.action_name = spec.action || that.name; - that.action = IPA.build(spec.action, IPA.action_builder); that.enabled = spec.enabled !== undefined ? spec.enabled : true; that.visible = spec.visible !== undefined ? spec.visible : true; that.show_cond = spec.show_cond || []; that.hide_cond = spec.hide_cond || []; + that.init = function(facet) { + + that.facet = facet; + that.action = that.facet.actions.get(that.action_name); + that.action.enabled_changed.attach(that.set_enabled); + that.action.visible_changed.attach(that.set_visible); + }; + that.create = function(container) { that.widget_create(container); @@ -1426,42 +1668,15 @@ IPA.action_button_widget = function(spec) { } }).appendTo(container); - that.set_enabled(that.enabled); - that.set_visible(that.visible); + that.set_enabled(that.action.enabled); + that.set_visible(that.action.visible); }; that.on_click = function() { if (!that.enabled) return; - if (that.needs_confirm) { - - var confirmed = false; - - if (that.confirm_dialog) { - - var dialog = IPA.build(that.confirm_dialog); - confirmed = dialog.confirm(that.facet); - } else { - var msg = that.get_confirm_message(); - confirmed = IPA.confirm(msg); - } - - if (!confirmed) return; - } - - that.execute_action(); - }; - - that.get_confirm_message = function() { - return that.confirm_msg; - }; - - that.execute_action = function() { - - if (that.action) { - that.action.execute(that.facet); - } + that.action.execute(that.facet); }; that.set_enabled = function(enabled) { @@ -1508,27 +1723,16 @@ IPA.control_buttons_widget = function(spec) { var that = IPA.widget(spec); that.buttons = IPA.build(spec.buttons, IPA.action_button_widget_builder) || []; - that.state_listeners = IPA.build(spec.state_listeners, IPA.state_listener_builder) || []; - that.state = []; that.init = function(facet) { var i; - for (i=0; i