Filter loading: gracefully skip unknown fields, remove inital empty row
authorLebbeous Fogle-Weekley <lebbeous@esilibrary.com>
Wed, 12 Sep 2012 18:02:49 +0000 (14:02 -0400)
committerLebbeous Fogle-Weekley <lebbeous@esilibrary.com>
Fri, 21 Sep 2012 15:07:01 +0000 (11:07 -0400)
Signed-off-by: Lebbeous Fogle-Weekley <lebbeous@esilibrary.com>
Open-ILS/web/js/dojo/openils/widget/PCrudFilterPane.js

index 08a2641..4b071a6 100644 (file)
@@ -251,20 +251,58 @@ if (!dojo._hasResource['openils.widget.PCrudFilterPane']) {
             return result;
         };
 
-        this.add_row = function(initializer) {
+        this._validate_initializer = function(initializer, onsuccess) {
+            this.field_store.fetchItemByIdentity({
+                "identity": initializer.field,
+                "onItem": dojo.hitch(this, function(item) {
+                    if (item) {
+                        onsuccess();
+                    } else {
+                        console.debug(
+                            "skipping initializer for field " +
+                            initializer.field + " not present here"
+                        );
+                    }
+                })
+            });
+        };
+
+        this._proceed_add_row = function(initializer) {
+            var row_id_list = openils.Util.objectProperties(this.rows);
+
+            /* Kill initial empty row when adding pre-initialized rows. */
+            if (row_id_list.length == 1 && initializer) {
+                var existing_row_id = row_id_list.shift();
+                if (this.rows[existing_row_id].is_unset())
+                    this.remove_row(existing_row_id, true /* no_apply */);
+            }
+
             this.hide_empty_placeholder();
             var row_id = this.row_index++;
             this.rows[row_id] = new PCrudFilterRow(this, row_id, initializer);
         };
 
-        this.remove_row = function(row_id) {
+        this.add_row = function(initializer) {
+            if (initializer) {
+                this._validate_initializer(
+                    initializer,
+                    dojo.hitch(this, function() {
+                        this._proceed_add_row(initializer);
+                    })
+                );
+            } else {
+                this._proceed_add_row(initializer);
+            }
+        };
+
+        this.remove_row = function(row_id, no_apply) {
             this.rows[row_id].destroy();
             delete this.rows[row_id];
 
             if (openils.Util.objectProperties(this.rows).length < 1)
                 this.show_empty_placeholder();
 
-            if (this.compact)
+            if (this.compact && !no_apply)
                 this.do_apply();
         };
 
@@ -572,7 +610,6 @@ if (!dojo._hasResource['openils.widget.PCrudFilterPane']) {
         };
 
         this.initialize = function(initializer) {
-            console.log("initializer is "  + dojo.toJson(initializer));
             this.field_selector.attr("value", initializer.field);
             this.operator_selector.attr("value", initializer.operator);
 
@@ -588,6 +625,10 @@ if (!dojo._hasResource['openils.widget.PCrudFilterPane']) {
             }
         };
 
+        this.is_unset = function() {
+            return !Boolean(this.field_selector.attr("value"));
+        };
+
         this._init.apply(this, arguments);
     }