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
This commit is contained in:
Petr Vobornik 2012-05-21 13:46:14 +02:00
parent c799f6a0bf
commit 306f380258
5 changed files with 702 additions and 413 deletions

View File

@ -231,57 +231,34 @@ IPA.details_facet = function(spec, no_init) {
spec = spec || {}; spec = spec || {};
spec.name = spec.name || 'details'; spec.name = spec.name || 'details';
spec.control_buttons = spec.control_buttons || {};
var cb = spec.control_buttons; spec.actions = spec.actions || [];
cb.factory = cb.factory || IPA.control_buttons_widget; spec.actions.unshift(
cb.buttons = cb.buttons || []; IPA.refresh_action,
cb.state_listeners = cb.state_listeners || []; IPA.reset_action,
cb.buttons.unshift( IPA.update_action);
spec.control_buttons = spec.control_buttons || [];
spec.control_buttons.unshift(
{ {
name: 'refresh', name: 'refresh',
label: IPA.messages.buttons.refresh, label: IPA.messages.buttons.refresh,
icon: 'reset-icon', icon: 'reset-icon'
action: {
handler: function(facet) {
facet.refresh();
}
}
}, },
{ {
name: 'reset', name: 'reset',
label: IPA.messages.buttons.reset, label: IPA.messages.buttons.reset,
icon: 'reset-icon', icon: 'reset-icon'
needs_confirm: true,
action: {
enable_cond: ['dirty'],
handler: function(facet) {
facet.reset();
}
}
}, },
{ {
name: 'update', name: 'update',
label: IPA.messages.buttons.update, label: IPA.messages.buttons.update,
icon: 'update-icon', icon: 'update-icon'
action: { });
enable_cond: ['dirty'],
handler: function(facet) {
if (!facet.validate()) {
facet.show_validation_error();
return;
}
facet.update(); spec.state = spec.state || {};
} spec.state.evaluators = spec.state.evaluators || [];
} spec.state.evaluators.push(IPA.dirty_state_evaluator);
}
);
cb.state_listeners.push(
{
factory: IPA.dirty_state_listener
}
);
var that = IPA.facet(spec, true); var that = IPA.facet(spec, true);
@ -476,7 +453,7 @@ IPA.details_facet = function(spec, no_init) {
field.load(data.result.result); field.load(data.result.result);
} }
that.policies.post_load(data); that.policies.post_load(data);
that.post_load.notify(); that.post_load.notify([data], that);
that.clear_expired_flag(); that.clear_expired_flag();
}; };
@ -719,7 +696,8 @@ IPA.details_facet = function(spec, no_init) {
var widget_builder = IPA.widget_builder({ var widget_builder = IPA.widget_builder({
widget_options: { widget_options: {
entity: that.entity entity: that.entity,
facet: that
} }
}); });
var field_builder = IPA.field_builder({ var field_builder = IPA.field_builder({
@ -872,38 +850,107 @@ IPA.command_builder = function() {
return that; return that;
}(); }();
IPA.select_action = function(spec) {
spec = spec || {};
spec.name = spec.name || 'select_action';
spec.label = spec.label || '-- select action --';
var that = IPA.action(spec);
that.execute_action = function(facet) {
};
return that;
};
IPA.refresh_action = function(spec) {
spec = spec || {};
spec.name = spec.name || 'refresh';
spec.label = spec.label || IPA.messages.buttons.refresh;
var that = IPA.action(spec);
that.execute_action = function(facet) {
facet.refresh();
};
return that;
};
IPA.reset_action = function(spec) {
spec = spec || {};
spec.name = spec.name || 'reset';
spec.label = spec.label || IPA.messages.buttons.reset;
spec.enable_cond = spec.enable_cond || ['dirty'];
var that = IPA.action(spec);
that.execute_action = function(facet) {
facet.reset();
};
return that;
};
IPA.update_action = function(spec) {
spec = spec || {};
spec.name = spec.name || 'update';
spec.label = spec.label || IPA.messages.buttons.update;
spec.needs_confirm = spec.needs_confirm !== undefined ? spec.needs_confirm : true;
spec.enable_cond = spec.enable_cond || ['dirty'];
var that = IPA.action(spec);
that.execute_action = function(facet) {
if (!facet.validate()) {
facet.show_validation_error();
return;
}
facet.update();
};
return that;
};
IPA.boolean_state_evaluator = function(spec) { IPA.boolean_state_evaluator = function(spec) {
spec = spec || {}; spec = spec || {};
spec.event = spec.event || 'post_load';
var that = IPA.state_evaluator(spec); var that = IPA.state_evaluator(spec);
that.name = spec.name || 'boolean_state_evaluator';
that.field = spec.field; that.field = spec.field;
that.true_state = spec.true_state || that.field_name + '-true'; that.true_state = spec.true_state || that.field_name + '-true';
that.false_state = spec.false_state || that.field_name + '-false'; that.false_state = spec.false_state || that.field_name + '-false';
that.true_desc = spec.true_desc || IPA.messages['true'];
that.false_desc = spec.false_desc || IPA.messages['false'];
that.invert_value = spec.invert_value; that.invert_value = spec.invert_value;
that.parser = IPA.build({ that.parser = IPA.build({
factory: spec.parser || IPA.boolean_formatter, factory: spec.parser || IPA.boolean_formatter,
invert_value: that.invert_value invert_value: that.invert_value
}); });
that.evaluate = function(record) { that.on_event = function(data) {
var old_state = that.state;
var record = data.result.result;
that.state = []; that.state = [];
var value = that.parser.parse(record[that.field]); var value = that.parser.parse(record[that.field]);
if (value === true) { if (value === true) {
that.state.push(that.true_state); that.state.push(that.true_state);
that.description = that.true_desc;
} else { } else {
that.state.push(that.false_state); that.state.push(that.false_state);
that.description = that.false_desc;
} }
return that.state; that.notify_on_change(old_state);
}; };
return that; return that;
@ -912,10 +959,9 @@ IPA.boolean_state_evaluator = function(spec) {
IPA.enable_state_evaluator = function(spec) { IPA.enable_state_evaluator = function(spec) {
spec = spec || {}; spec = spec || {};
spec.name = spec.name || 'enable_state_evaluator';
spec.true_state = spec.true_state || 'enabled'; spec.true_state = spec.true_state || 'enabled';
spec.false_state = spec.false_state || 'disabled'; spec.false_state = spec.false_state || 'disabled';
spec.true_desc = spec.true_desc || IPA.messages.status.enabled;
spec.false_desc = spec.false_desc || IPA.messages.status.disabled;
var that = IPA.boolean_state_evaluator(spec); var that = IPA.boolean_state_evaluator(spec);
@ -931,7 +977,7 @@ IPA.object_action = function(spec) {
that.method = spec.method; that.method = spec.method;
that.confirm_msg = spec.confirm_msg || IPA.messages.actions.confirm; 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) {
var entity_name = facet.entity.name; var entity_name = facet.entity.name;
var pkey = IPA.nav.get_state(entity_name+'-pkey'); var pkey = IPA.nav.get_state(entity_name+'-pkey');
@ -1021,3 +1067,22 @@ IPA.delete_action = function(spec) {
return that; return that;
}; };
IPA.enabled_summary_cond = function() {
return {
pos: ['enabled'],
neg: [],
description: IPA.messages.status.enabled,
state: ['enabled']
};
};
IPA.disabled_summary_cond = function() {
return {
pos: [],
neg: ['enabled'],
description: IPA.messages.status.disabled,
state: ['disabled']
};
};

View File

@ -28,6 +28,8 @@
IPA.facet = function(spec, no_init) { IPA.facet = function(spec, no_init) {
spec = spec || {}; spec = spec || {};
spec.state = spec.state || {};
$.extend(spec.state, { factory: IPA.state });
var that = {}; var that = {};
@ -42,7 +44,10 @@ IPA.facet = function(spec, no_init) {
that.disable_breadcrumb = spec.disable_breadcrumb; that.disable_breadcrumb = spec.disable_breadcrumb;
that.disable_facet_tabs = spec.disable_facet_tabs; 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.header = spec.header || IPA.facet_header({ facet: that });
that._needs_update = spec.needs_update; that._needs_update = spec.needs_update;
@ -299,16 +304,17 @@ IPA.facet = function(spec, no_init) {
that.init_facet = function() { that.init_facet = function() {
that.action_state.init(that);
that.actions.init(that);
that.header.init();
var buttons_spec = { var buttons_spec = {
factory: IPA.control_buttons_widget, factory: IPA.control_buttons_widget,
name: 'control-buttons', 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 = IPA.build(buttons_spec);
that.control_buttons.init(that); that.control_buttons.init(that);
}; };
@ -335,9 +341,9 @@ IPA.facet_header = function(spec) {
that.facet = spec.facet; 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({ var widget_builder = IPA.widget_builder({
widget_options: { widget_options: {
@ -345,10 +351,17 @@ IPA.facet_header = function(spec) {
} }
}); });
that.action_list = widget_builder.build_widget(that.facet.action_list); var widget = {
that.action_list.init(); 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(); that.title_widget = IPA.facet_title();
}; };
@ -587,17 +600,15 @@ IPA.facet_header = function(spec) {
} }
} }
} }
};
if (that.action_list) { that.update_summary = function() {
that.action_list.update(result); var summary = that.facet.action_state.summary();
var state = that.action_list.state; if (summary.state.length > 0) {
var icon_tooltip = that.action_list.state_evaluator.get_description(); var css_class = summary.state.join(' ');
if (state.length > 0) { that.title_widget.set_class(css_class);
var css_class = state.join(' '); that.title_widget.set_icon_tooltip(summary.description);
that.title_widget.set_class(css_class);
that.title_widget.set_icon_tooltip(icon_tooltip);
}
} }
that.adjust_elements(); that.adjust_elements();
@ -645,8 +656,6 @@ IPA.facet_header = function(spec) {
that.load(); that.load();
}; };
init();
return that; return that;
}; };
@ -801,7 +810,7 @@ IPA.table_facet = function(spec, no_init) {
that.table.pagination_control.css('visibility', 'visible'); that.table.pagination_control.css('visibility', 'visible');
that.post_load.notify(); that.post_load.notify([data], that);
that.clear_expired_flag(); that.clear_expired_flag();
}; };
@ -1245,194 +1254,33 @@ IPA.action = function(spec) {
that.name = spec.name; that.name = spec.name;
that.label = spec.label; that.label = spec.label;
that.enabled = spec.enabled !== undefined ? spec.enabled : true;
that.enable_cond = spec.enable_cond || []; that.enable_cond = spec.enable_cond || [];
that.disable_cond = spec.disable_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.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.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) { if (that.handler) {
that.handler(facet, on_success, on_error); that.handler(facet, on_success, on_error);
} }
}; };
return that; that.execute = function(facet, on_success, on_error) {
};
IPA.action_builder = function(spec) { if (!that.enabled || !that.visible) return;
spec = spec || {};
spec.factory = spec.factory || IPA.action;
var that = IPA.builder(spec);
return that;
};
IPA.state_evaluator = function(spec) {
spec = spec || {};
var that = {};
that.evaluate = function() {
that.state = [];
return that.state;
};
that.get_description = function() {
return that.description || '';
};
return that;
};
IPA.state_listener = function(spec) {
spec = spec || {};
var that = {};
that.event_name = spec.event;
that.state_changed = IPA.observer();
that.state = [];
that.init = function(facet) {
if (that.event_name && facet[that.event_name]) {
facet[that.event_name].attach(that.on_event);
}
};
that.on_event = function() {
that.state_changed.notify();
};
that.get_description = function() {
return that.description || '';
};
return that;
};
IPA.state_listener_builder = function(spec) {
spec = spec || {};
spec.factory = spec.factory || IPA.state_listener;
var that = IPA.builder(spec);
return that;
};
IPA.dirty_state_listener = function(spec) {
spec = spec || {};
spec.event = spec.event || 'dirty_changed';
var that = IPA.state_listener(spec);
that.on_event = function(dirty) {
that.state = [];
if (dirty) {
that.state.push('dirty');
}
that.state_changed.notify();
};
return that;
};
IPA.selected_state_listener = function(spec) {
spec = spec || {};
spec.event = spec.event || 'select_changed';
var that = IPA.state_listener(spec);
that.on_event = function(selected) {
that.state = [];
if (selected && selected.length > 0) {
that.state.push('item-selected');
}
that.state_changed.notify();
};
return that;
};
IPA.self_service_state_listener = function(spec) {
spec = spec || {};
spec.event = spec.event || 'post_load';
var that = IPA.state_listener(spec);
that.on_event = function() {
that.state = [];
var self_service = IPA.nav.name === 'self-service';
if (self_service) {
that.state.push('self-service');
}
that.state_changed.notify();
};
return that;
};
IPA.action_button_widget = function(spec) {
spec = spec || {};
var that = IPA.widget(spec);
that.name = spec.name;
that.label = spec.label;
that.tooltip = spec.tooltip;
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 = 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.create = function(container) {
that.widget_create(container);
that.button_element = IPA.action_button({
name: that.name,
href: that.href,
label: that.label,
icon: that.icon,
click: function() {
that.on_click();
return false;
}
}).appendTo(container);
that.set_enabled(that.enabled);
that.set_visible(that.visible);
};
that.on_click = function() {
if (!that.enabled) return;
if (that.needs_confirm) { if (that.needs_confirm) {
@ -1450,20 +1298,387 @@ IPA.action_button_widget = function(spec) {
if (!confirmed) return; if (!confirmed) return;
} }
that.execute_action(); that.execute_action(facet, on_success, on_error);
}; };
that.get_confirm_message = function() { that.get_confirm_message = function() {
return that.confirm_msg; return that.confirm_msg;
}; };
that.execute_action = function() { that.set_enabled = function(enabled) {
if (that.action) { var old = that.enabled;
that.action.execute(that.facet);
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;
};
IPA.action_builder = function(spec) {
spec = spec || {};
spec.factory = spec.factory || IPA.action;
var that = IPA.builder(spec);
return that;
};
IPA.action_holder = function(spec) {
spec = spec || {};
var that = {};
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<actions.length; i++) {
action = actions[i];
that.actions.put(action.name, action);
}
that.facet.action_state.changed.attach(that.state_changed);
that.facet.post_load.attach(that.on_load);
};
that.state_changed = function(state) {
var actions, action, i, enabled, visible;
actions = that.actions.values;
for (i=0; i<actions.length; i++) {
action = actions[i];
enabled = IPA.eval_cond(action.enable_cond, action.disable_cond, state);
visible = IPA.eval_cond(action.show_cond, action.hide_cond, state);
action.set_enabled(enabled);
action.set_visible(visible);
}
};
that.get = function(name) {
return that.actions.get(name);
};
that.add = function(action) {
that.actions.put(action.name, action);
};
that.on_load = function() {
var state = that.facet.action_state.get();
that.state_changed(state);
};
return that;
};
IPA.action_holder_builder = function(spec) {
spec = spec || {};
spec.factory = spec.factory || IPA.action_holder;
var that = IPA.builder(spec);
return that;
};
IPA.state = function(spec) {
spec = spec || {};
spec.summary_evaluator = spec.summary_evaluator || IPA.summary_evaluator;
var that = {};
that.state = $.ordered_map();
//when state changes. Params: state, Context: this
that.changed = IPA.observer();
that.evaluators = IPA.build(spec.evaluators, IPA.state_evaluator_builder) || [];
that.summary_evaluator = IPA.build(spec.summary_evaluator);
that.summary_conditions = spec.summary_conditions || [];
that.init = function(facet) {
var i, evaluator;
that.facet = facet;
for (i=0; i<that.evaluators.length; i++) {
evaluator = that.evaluators[i];
evaluator.init(facet);
evaluator.changed.attach(that.on_eval_changed);
}
};
that.on_eval_changed = function() {
var evaluator = this;
that.state.put(evaluator.name, evaluator.state);
that.notify();
};
that.get = function() {
var state, i;
state = [];
var values = that.state.values;
for (i=0; i<values.length; i++) {
$.merge(state, values[i]);
}
return state;
};
that.summary = function() {
var summary = that.summary_evaluator.evaluate(that);
return summary;
};
that.notify = function(state) {
state = state || that.get();
that.changed.notify([state], that);
};
return that;
};
IPA.summary_evaluator = function(spec) {
spec = spec || {};
var that = {};
that.evaluate = function(state) {
var conds, cond, i, summary, state_a;
conds = state.summary_conditions;
state_a = state.get();
for (i=0; i<conds.length; i++) {
cond = conds[i];
if (IPA.eval_cond(cond.pos, cond.neg, state_a)) {
summary = {
state: cond.state,
description: cond.description
};
break;
}
}
summary = summary || {
state: state_a,
description: ''
};
return summary;
};
return that;
};
IPA.state_evaluator = function(spec) {
spec = spec || {};
var that = {};
that.name = spec.name || 'state_evaluator';
that.event_name = spec.event;
//when state changes. Params: state, Context: this
that.changed = IPA.observer();
that.state = [];
that.init = function(facet) {
if (that.event_name && facet[that.event_name]) {
facet[that.event_name].attach(that.on_event);
}
};
that.on_event = function() {
};
that.notify_on_change = function(old_state) {
if (IPA.array_diff(that.state, old_state)) {
that.changed.notify([that.state], that);
}
};
return that;
};
IPA.state_evaluator_builder = function(spec) {
spec = spec || {};
spec.factory = spec.factory || IPA.state_evaluator;
var that = IPA.builder(spec);
return that;
};
IPA.dirty_state_evaluator = function(spec) {
spec = spec || {};
spec.event = spec.event || 'dirty_changed';
var that = IPA.state_evaluator(spec);
that.name = spec.name || 'dirty_state_evaluator';
that.on_event = function(dirty) {
var old_state = that.state;
that.state = [];
if (dirty) {
that.state.push('dirty');
}
that.notify_on_change(old_state);
};
return that;
};
IPA.selected_state_evaluator = function(spec) {
spec = spec || {};
spec.event = spec.event || 'select_changed';
var that = IPA.state_evaluator(spec);
that.name = spec.name || 'selected_state_evaluator';
that.on_event = function(selected) {
var old_state = that.state;
that.state = [];
if (selected && selected.length > 0) {
that.state.push('item-selected');
}
that.notify_on_change(old_state);
};
return that;
};
IPA.self_service_state_evaluator = function(spec) {
spec = spec || {};
spec.event = spec.event || 'post_load';
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';
if (self_service) {
that.state.push('self-service');
}
that.notify_on_change(old_state);
};
return that;
};
IPA.action_button_widget = function(spec) {
spec = spec || {};
var that = IPA.widget(spec);
that.name = spec.name;
that.label = spec.label;
that.tooltip = spec.tooltip;
that.href = spec.href || that.name;
that.icon = spec.icon;
that.action_name = spec.action || that.name;
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);
that.button_element = IPA.action_button({
name: that.name,
href: that.href,
label: that.label,
icon: that.icon,
click: function() {
that.on_click();
return false;
}
}).appendTo(container);
that.set_enabled(that.action.enabled);
that.set_visible(that.action.visible);
};
that.on_click = function() {
if (!that.enabled) return;
that.action.execute(that.facet);
};
that.set_enabled = function(enabled) { that.set_enabled = function(enabled) {
that.enabled = enabled; that.enabled = enabled;
@ -1508,27 +1723,16 @@ IPA.control_buttons_widget = function(spec) {
var that = IPA.widget(spec); var that = IPA.widget(spec);
that.buttons = IPA.build(spec.buttons, IPA.action_button_widget_builder) || []; 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) { that.init = function(facet) {
var i; var i;
for (i=0; i<that.state_listeners.length; i++) {
var listener = that.state_listeners[i];
listener.init(facet);
listener.state_changed.attach(that.on_state_change);
}
for (i=0; i<that.buttons.length; i++) { for (i=0; i<that.buttons.length; i++) {
var button = that.buttons[i]; var button = that.buttons[i];
button.facet = facet; button.init(facet);
} }
that.on_state_change();
}; };
that.create = function(container) { that.create = function(container) {
@ -1544,36 +1748,6 @@ IPA.control_buttons_widget = function(spec) {
} }
}; };
that.on_state_change = function() {
that.get_state();
that.reevaluate();
};
that.get_state = function() {
that.state = [];
for (var i=0; i<that.state_listeners.length; i++) {
var listener = that.state_listeners[i];
that.state.push.apply(that.state, listener.state);
}
};
that.reevaluate = function() {
for (var i=0; i<that.buttons.length; i++) {
var button = that.buttons[i];
var action = button.action;
var enabled = IPA.eval_cond(action.enable_cond, action.disable_cond, that.state);
var visible = IPA.eval_cond(button.show_cond, button.hide_cond, that.state);
button.set_enabled(enabled);
button.set_visible(visible);
}
};
return that; return that;
}; };
@ -1626,21 +1800,51 @@ IPA.action_list_widget = function(spec) {
var that = IPA.composite_widget(spec); var that = IPA.composite_widget(spec);
that.actions = IPA.build(spec.actions, IPA.action_builder) || []; that.action_names = spec.actions || [];
that.state_evaluator = IPA.build(spec.state_evaluator); that.actions = $.ordered_map();
that.state = [];
that.init = function(facet) {
var options, actions, action, name, i;
that.facet = facet;
that.init = function() {
that.action_select = that.widgets.get_widget('action'); that.action_select = that.widgets.get_widget('action');
that.apply_button = that.widgets.get_widget('apply'); that.apply_button = that.widgets.get_widget('apply');
that.action_select.value_changed.attach(that.on_action_change); that.action_select.value_changed.attach(that.on_action_change);
that.apply_button.click = that.on_apply; that.apply_button.click = that.on_apply;
var options = []; for (i=0; i<that.action_names.length; i++) {
name = that.action_names[i];
action = that.facet.actions.get(name);
that.add_action(action, true);
}
for (var i=0; i< that.actions.length; i++) { that.init_options();
var action = that.actions[i]; };
that.add_action = function(action, batch) {
that.actions.put(action.name, action);
action.enabled_changed.attach(that.action_enabled_changed);
action.visible_changed.attach(that.action_visible_changed);
if(!batch) {
that.init_options();
that.recreate_options();
that.select_first_enabled();
}
};
that.init_options = function() {
var options, actions, action, i;
options = [];
actions = that.actions.values;
for (i=0; i< actions.length; i++) {
action = actions[i];
options.push({ options.push({
label: action.label, label: action.label,
value: action.name value: action.name
@ -1650,71 +1854,62 @@ IPA.action_list_widget = function(spec) {
that.action_select.options = options; that.action_select.options = options;
}; };
that.recreate_options = function() {
that.action_select.create_options();
};
that.on_action_change = function() { that.on_action_change = function() {
var selected = that.action_select.save()[0]; var action = that.get_selected();
var action = that.get_action(selected); that.apply_button.set_enabled(action.enabled);
var enabled = that.action_enabled(action);
that.apply_button.set_enabled(enabled);
}; };
that.on_apply = function() { that.on_apply = function() {
var selected = that.action_select.save()[0];
var action = that.get_action(selected);
var enabled = that.action_enabled(action);
var facet = that.entity.get_facet();
if (enabled) { var action = that.get_selected();
action.execute(facet,
if (action.enabled) {
action.execute(that.facet,
that.on_action_success, that.on_action_success,
that.on_action_error); that.on_action_error);
} }
}; };
that.on_action_success = function() { that.on_action_success = function() {
var facet = that.entity.get_facet();
facet.refresh(); that.facet.refresh();
}; };
that.on_action_error = function() { that.on_action_error = function() {
var facet = that.entity.get_facet();
facet.refresh(); that.facet.refresh();
}; };
that.update = function(result) { that.action_enabled_changed = function(enabled) {
var action = this;
var selected_action = that.get_selected();
that.action_select.set_options_enabled(action.enabled, [action.name]);
that.get_state(result); if (!action.enabled && action === selected_action) {
var disabled = that.get_disabled(); that.select_first_enabled();
that.action_select.enable_options();
that.action_select.disable_options(disabled);
that.select_first_enabled();
};
that.get_state = function(result) {
if (that.state_evaluator) {
that.state = that.state_evaluator.evaluate(result);
} }
}; };
that.get_action = function(name) { that.get_selected = function() {
var selected = that.action_select.save()[0];
for (var i=0; i< that.actions.length; i++) { var action = that.actions.get(selected);
var action = that.actions[i]; return action;
if (action.name === name) {
return action;
}
}
return null;
}; };
that.get_disabled = function() { that.get_disabled = function() {
var disabled = []; var disabled = [];
var actions = that.action.values;
for (var i=0; i< that.actions.length; i++) { for (var i=0; i< actions.length; i++) {
var action = that.actions[i]; var action = actions[i];
if (!that.action_enabled(action)) { if (!that.action.enabled) {
disabled.push(action.name); disabled.push(action.name);
} }
} }
@ -1722,21 +1917,15 @@ IPA.action_list_widget = function(spec) {
return disabled; return disabled;
}; };
that.action_enabled = function(action) {
var enabled = IPA.eval_cond(action.enable_cond,
action.disable_cond,
that.state);
return enabled;
};
that.select_first_enabled = function() { that.select_first_enabled = function() {
var first = that.actions[0].name; var actions = that.actions.values;
for (var i=0; i< that.actions.length; i++) { var first = actions[0].name;
var action = that.actions[i];
if (that.action_enabled(action)) { for (var i=0; i< actions.length; i++) {
var action = actions[i];
if (action.enabled) {
first = action.name; first = action.name;
break; break;
} }

View File

@ -36,10 +36,17 @@ jQuery.ordered_map = jQuery.fn.ordered_map = function() {
}; };
that.put = function(key, value) { that.put = function(key, value) {
that.keys.push(key);
that.values.push(value); var i = that.get_key_index(key);
if (i >= 0) {
that.values[i] = value;
} else {
that.keys.push(key);
that.values.push(value);
that.length = that.keys.length;
}
that.map[key] = value; that.map[key] = value;
that.length = that.keys.length;
}; };
that.remove = function(key) { that.remove = function(key) {

View File

@ -37,58 +37,35 @@ IPA.search_facet = function(spec, no_init) {
spec.disable_facet_tabs = spec.disable_facet_tabs =
spec.disable_facet_tabs === undefined ? true : spec.disable_facet_tabs; spec.disable_facet_tabs === undefined ? true : spec.disable_facet_tabs;
spec.control_buttons = spec.control_buttons || {}; spec.actions = spec.actions || [];
spec.actions.unshift(
IPA.refresh_action,
IPA.batch_remove_action,
IPA.add_action);
var cb = spec.control_buttons; spec.control_buttons = spec.control_buttons || [];
cb.factory = cb.factory || IPA.control_buttons_widget; spec.control_buttons.unshift(
cb.buttons = cb.buttons || [];
cb.state_listeners = cb.state_listeners || [];
cb.buttons.unshift(
{ {
name: 'refresh', name: 'refresh',
label: IPA.messages.buttons.refresh, label: IPA.messages.buttons.refresh,
icon: 'reset-icon', icon: 'reset-icon'
action: {
handler: function(facet) {
facet.refresh();
}
}
}, },
{ {
name: 'remove', name: 'remove',
label: IPA.messages.buttons.remove, label: IPA.messages.buttons.remove,
icon: 'remove-icon', icon: 'remove-icon'
needs_confirm: true,
hide_cond: ['self-service'],
action: {
enable_cond: ['item-selected'],
handler: function(facet) {
facet.show_remove_dialog();
}
}
}, },
{ {
name: 'add', name: 'add',
label: IPA.messages.buttons.add, label: IPA.messages.buttons.add,
icon: 'add-icon', icon: 'add-icon'
hide_cond: ['self-service'], });
action: {
handler: function(facet) {
facet.show_add_dialog();
}
}
}
);
cb.state_listeners.push(
{
factory: IPA.selected_state_listener
},
{
factory: IPA.self_service_state_listener
}
);
//TODO: make buttons invisible on self-service. Currently regression. spec.state = spec.state || {};
spec.state.evaluators = spec.state.evaluators || [];
spec.state.evaluators.push(
IPA.selected_state_evaluator,
IPA.self_service_state_evaluator);
var that = IPA.table_facet(spec, true); var that = IPA.table_facet(spec, true);
@ -392,6 +369,39 @@ IPA.nested_search_facet = function(spec) {
return that; return that;
}; };
IPA.batch_remove_action = function(spec) {
spec = spec || {};
spec.name = spec.name || 'remove';
spec.label = spec.label || IPA.messages.buttons.remove;
spec.enable_cond = spec.enable_cond || ['item-selected'];
spec.hide_cond = spec.hide_cond || ['self-service'];
var that = IPA.action(spec);
that.execute_action = function(facet) {
facet.show_remove_dialog();
};
return that;
};
IPA.add_action = function(spec) {
spec = spec || {};
spec.name = spec.name || 'add';
spec.label = spec.label || IPA.messages.buttons.add;
spec.hide_cond = spec.hide_cond || ['self-service'];
var that = IPA.action(spec);
that.execute_action = function(facet) {
facet.show_add_dialog();
};
return that;
};
/* /*
* Calls entity's disable command for each selected item in a batch. * Calls entity's disable command for each selected item in a batch.
* Usable in table facets. * Usable in table facets.

View File

@ -839,29 +839,35 @@ IPA.select_widget = function(spec) {
container.addClass('select-widget'); container.addClass('select-widget');
var select = $('<select/>', { that.select = $('<select/>', {
name: that.name name: that.name,
change: function() {
that.value_changed.notify([], that);
return false;
}
}).appendTo(container); }).appendTo(container);
that.create_options();
if (that.undo) {
that.create_undo(container);
}
that.create_error_link(container);
};
that.create_options = function() {
that.select.empty();
for (var i=0; i<that.options.length; i++) { for (var i=0; i<that.options.length; i++) {
var option = that.options[i]; var option = that.options[i];
$('<option/>', { $('<option/>', {
text: option.label, text: option.label,
value: option.value value: option.value
}).appendTo(select); }).appendTo(that.select);
} }
if (that.undo) {
that.create_undo(container);
}
that.select = $('select[name="'+that.name+'"]', that.container);
that.select.change(function() {
that.value_changed.notify([], that);
});
that.create_error_link(container);
}; };
that.save = function() { that.save = function() {
@ -891,17 +897,29 @@ IPA.select_widget = function(spec) {
$('option', that.select).attr('selected', ''); $('option', that.select).attr('selected', '');
}; };
that.enable_options = function() { that.set_options_enabled = function(enabled, options) {
$('option', that.select).attr('disabled', '');
var html_value = enabled ? '' : 'disabled';
if (!options) {
$('option', that.select).attr('disabled', html_value);
} else {
for (var i=0; i<options.length;i++) {
var value = options[i];
var option = $('option[value="'+value+'"]', that.select);
option.attr('disabled', html_value);
}
}
};
that.enable_options = function(options) {
that.set_options_enabled(true, options);
}; };
that.disable_options = function(options) { that.disable_options = function(options) {
for (var i=0; i<options.length;i++) { that.set_options_enabled(false, options);
var value = options[i];
var option = $('option[value="'+value+'"]', that.select);
option.attr('disabled', 'disabled');
}
}; };
// methods that should be invoked by subclasses // methods that should be invoked by subclasses