From 2cd5b4594c10ad8a487a7eb7531e1feaa2a05025 Mon Sep 17 00:00:00 2001 From: Lebbeous Fogle-Weekley Date: Tue, 25 Sep 2012 16:02:26 -0400 Subject: [PATCH] IN / NOT IN for filter somewhat working, but doesn't save/load yet Signed-off-by: Lebbeous Fogle-Weekley --- .../web/js/dojo/openils/widget/PCrudFilterPane.js | 177 +++++++++++++++++---- .../js/dojo/openils/widget/nls/PCrudFilterPane.js | 5 +- 2 files changed, 149 insertions(+), 33 deletions(-) diff --git a/Open-ILS/web/js/dojo/openils/widget/PCrudFilterPane.js b/Open-ILS/web/js/dojo/openils/widget/PCrudFilterPane.js index e19099ae09..ef24865896 100644 --- a/Open-ILS/web/js/dojo/openils/widget/PCrudFilterPane.js +++ b/Open-ILS/web/js/dojo/openils/widget/PCrudFilterPane.js @@ -101,6 +101,18 @@ if (!dojo._hasResource['openils.widget.PCrudFilterPane']) { "param_count": 1, "strict": true }, { + "name": "in", + "label": pcFilterLocaleStrings.OPERATOR_IN, + "param_count": null, /* arbitrary number, special */ + "strict": true, + "minimal": true + }, { + "name": "not in", + "label": pcFilterLocaleStrings.OPERATOR_NOT_IN, + "param_count": null, /* arbitrary number, special */ + "strict": true, + "minimal": true + }, { "name": "between", "label": pcFilterLocaleStrings.OPERATOR_BETWEEN, "param_count": 2, @@ -153,7 +165,7 @@ if (!dojo._hasResource['openils.widget.PCrudFilterPane']) { var ops = openils.Util.objectProperties(clause); var op = ops.pop(); - var matches = op.match(/^not (\w+)$/); + var matches = op.match(/^not [lb].+$/); /* "not in" needs no change */ if (matches) { clause[matches[1]] = clause[op]; delete clause[op]; @@ -162,6 +174,30 @@ if (!dojo._hasResource['openils.widget.PCrudFilterPane']) { return false; } + /* Given a value, add it to selector options if it's not already there, + * and select it. */ + function _add_or_at_least_select(value, selector) { + var found = false; + + for (var i = 0; i < selector.options.length; i++) { + var option = selector.options[i]; + if (option.value == value) { + found = true; + option.selected = true; + } + } + + if (!found) { + dojo.create( + "option", { + "innerHTML": value, + "value": value, + "selected": "selected" + }, selector + ); + } + } + /* This is not the dijit per se. Search further in this file for * "dojo.declare" for the beginning of the dijit. * @@ -472,12 +508,12 @@ if (!dojo._hasResource['openils.widget.PCrudFilterPane']) { }; this._create_value_slot = function(use_element) { + var how = {"innerHTML": "-"}; + if (use_element) - this.value_slot = dojo.create( - "span", {"innerHTML": "-"}, use_element - ); + this.value_slot = dojo.create("span", how, use_element); else - this.value_slot = dojo.create("td",{"innerHTML":"-"},this.tr); + this.value_slot = dojo.create("td", how, this.tr); }; this._create_remover = function(use_element) { @@ -497,7 +533,10 @@ if (!dojo._hasResource['openils.widget.PCrudFilterPane']) { this._clear_value_slot = function() { if (this.value_widgets) { this.value_widgets.forEach( - function(autowidg) { autowidg.widget.destroy(); } + function(autowidg) { + if (autowidg.widget) + autowidg.widget.destroy(); + } ); delete this.value_widgets; } @@ -513,29 +552,100 @@ if (!dojo._hasResource['openils.widget.PCrudFilterPane']) { this.value_widgets = []; - var param_count = this.operator_selector.item.param_count; - - /* This is where find and deploy custom widget builders. */ + /* This is where find custom widget builders to deploy shortly. */ var widget_builder_key = this.selected_field_fm_class + ":" + this.selected_field_fm_field; var constr = this.filter_row_manager.widget_builders[widget_builder_key] || openils.widget.AutoFieldWidget; - for (var i = 0; i < param_count; i++) { - var widg = new constr({ - "fmClass": this.selected_field_fm_class, - "fmField": this.selected_field_fm_field, - "noDisablePkey": true, - "parentNode": dojo.create("span", {}, this.value_slot), - "dijitArgs": {"scrollOnFocus": false} - }); + /* How many value widgets do we need for this operator? */ + var param_count = + this.operator_selector.store.getValue( + this.operator_selector.item, "param_count" + ); - widg.build(); - this.value_widgets.push(widg); + if (param_count === null) { + /* When param_count is null, we invoke the special case of + * preparing widgets for building a dynamic set of values. + * All other cases are handled by the else branch. */ + this._build_set_value_widgets(constr); + } else { + for (var i = 0; i < param_count; i++) { + this.value_widgets.push( + this._build_one_value_widget(constr) + ); + } } }; + this._build_set_value_widgets = function(constr) { + var value_widget = dojo.create( + "select", { + "multiple": "multiple", + "size": 4, + "style": { + "width": "6em", + "verticalAlign": "middle", + "margin": "0 0.75em" + } + }, + this.value_slot + ); + var entry_widget = this._build_one_value_widget(constr); + var adder = dojo.create( + "a", { + "href": "javascript:void(0);", + "style": {"verticalAlign": "middle", "margin": "0 0.75em"}, + "innerHTML": "[+]", /* XXX i18n? */ + "onclick": dojo.hitch(this, function() { + _add_or_at_least_select( + this._value_of_widget(entry_widget), + value_widget + ); + entry_widget.widget.attr("value", ""); /* clear */ + }) + }, this.value_slot + ); + this.value_widgets.push(value_widget); + }; + + + /* Create just one value widget (used by higher-level functions + * that worry about how many are needed). */ + this._build_one_value_widget = function(constr) { + var widg = new constr({ + "fmClass": this.selected_field_fm_class, + "fmField": this.selected_field_fm_field, + "noDisablePkey": true, + "parentNode": dojo.create( + "span", { + "style": {"verticalAlign": "middle"} + }, this.value_slot + ), + "dijitArgs": {"scrollOnFocus": false} + }); + + widg.build(); + return widg; + }; + + this._value_of_widget = function(widg) { + if (!widg.widget) /* widg is