Excise hard dependency on openils.widget.Search, any dependency on fieldmapper
authorLebbeous Fogle-Weekley <lebbeous@esilibrary.com>
Thu, 26 Jan 2012 22:41:22 +0000 (17:41 -0500)
committerLebbeous Fogle-Weekley <lebbeous@esilibrary.com>
Thu, 26 Jan 2012 22:48:16 +0000 (17:48 -0500)
Signed-off-by: Lebbeous Fogle-Weekley <lebbeous@esilibrary.com>
Open-ILS/src/templates/opac/parts/js.tt2
Open-ILS/web/js/dojo/openils/AutoSuggestStore.js
Open-ILS/web/js/dojo/openils/widget/AutoSuggest.js

index 0b34e5a..6473bd5 100644 (file)
 [%- END %]
 [%- IF want_dojo; -%]
 <script type="text/javascript">
-    var locale = "[% ctx.locale %]";
      var djConfig = {
-         locale: locale.toLowerCase().replace("_", "-"),    /* XXX ? */
+         locale: "[% ctx.locale | lower | replace('_', '-') %]",
          parseOnLoad: true,
          isDebug: false
      }, lang, bidi;
 </script>
 <script type="text/javascript" src="[% ctx.media_prefix %]/js/dojo/dojo/dojo.js?[% ctx.eg_cache_hash %]"></script>
 <script type="text/javascript" src="[% ctx.media_prefix %]/js/dojo/opensrf/opensrf.js?[% ctx.eg_cache_hash %]"></script>
+[%# See whether we can get away with killing this line - LFW %]<script type="text/javascript" src="[% ctx.media_prefix %]/js/dojo/opensrf/opensrf_xhr.js?[% ctx.eg_cache_hash %]"></script>
 <script type="text/javascript" src="[% ctx.media_prefix %]/js/dojo/dojo/openils_dojo.js?[% ctx.eg_cache_hash %]"></script>
 
 [% IF use_autosuggest.enabled == "t"; %]
 <script type="text/javascript">
-dojo.require("fieldmapper.dojoData");
-dojo.require("openils.widget.AutoSuggest");
+    dojo.require("openils.widget.AutoSuggest");
 </script>
 [% END; # use_autosuggest %]
 
index dab271f..97f4de6 100644 (file)
@@ -40,41 +40,90 @@ if (!dojo._hasResource["openils.AutoSuggestStore"]) {
         "constructor": function(/* object */ args) {
             dojo.mixin(this, args); /* XXX very sloppy */
             this._current_items = {};
+            this._setup_config_metabib_caches();
         },
 
-        "_label_field": function(field_id) {
-            /* It seems to be possible (well, I think I got it to happen once)
-             * to catch openils.widget.Searcher while it's still loading.
-             * So let's be gentle with it when we ask for its cached cmc and
-             * cmf objects.  */
-            if (!_cmf_cache) {
-                try {
-                    _cmf_cache = openils.widget.Searcher._cache.obj.cmf;
-                } catch (E) {
-                    console.log("o.w.Searcher's cmf cache not ready:" + E);
-                    return field_id;
-                }
+        "_setup_config_metabib_cache": function(key, field_list, oncomplete) {
+            var self = this;
+
+            if (this.cm_cache[key]) return;
+
+            var cookie = dojo.cookie("OILS_AS" + key);
+            if (cookie) {
+                this.cm_cache[key] = dojo.fromJson(cookie);
+                return oncomplete();
             }
 
-            if (!_cmc_cache) {
-                try {
-                    _cmc_cache = openils.widget.Searcher._cache.obj.cmc;
-                } catch (E) {
-                    console.log("o.w.Searcher's cmc cache not ready:" + E);
-                    return _cmf_cache[field_id].label;
-                }
+            /* now try to get it from open-ils.searcher */
+            try {
+                /* openils.widget.Searcher may not even be loaded;
+                 * that's ok; just try. */
+                this.cm_cache[key] =
+                    openils.widget.Searcher._cache.obj[key];
+                /* Don't try to set a cookie here; o.w.Searcher has
+                 * tried and failed. */
+            } catch (E) {
+                void(0);
             }
 
-            var mfield = _cmf_cache[field_id];
-            var mclass = _cmc_cache[mfield.field_class];
-            return mfield.label + " (" + mclass.label + ")";
+            if (this.cm_cache[key]) return oncomplete();
+
+            /* now try talking to fielder ourselves, and cache the result */
+            (new OpenSRF.ClientSession("open-ils.fielder"))({
+                "method": "open-ils.fielder." + key + ".atomic",
+                "params": [
+                    {"query": {"id": {"!=": null}, "fields": field_list}}
+                ],
+                "async": true,
+                "oncomplete": function(r) {
+                    /* XXX check for failure? */
+                    var result_arr = r.recv().content();
+
+                    self.cm_cache[key] = {};
+                    dojo.forEach(
+                        result_arr,
+                        function(o) { self.cm_cache[key][o.id] = o; }
+                    );
+                    dojo.cookie(
+                        "OILS_AS" + key, dojo.toJson(self.cm_cache[key])
+                    );
+                    console.log("finished fetching " + key + " from fielder");
+                    oncomplete();
+                }
+            }).send();
+        },
+
+        "_setup_config_metabib_caches": function() {
+            var self = this;
+
+            this.cm_cache = {};
+
+            var field_lists = {
+                "cmf": ["id", "field_class", "name", "label"],
+                "cmc": ["id", "name", "label"]
+            };
+            var class_list = openils.Util.objectProperties(field_lists);
+
+            var _is_done = function(k) { return Boolean(self.cm_cache[k]); };
+
+            dojo.forEach(
+                class_list, function(key) {
+                    self._setup_config_metabib_cache(
+                        key, field_lists[key], function() {
+                            if (dojo.every(class_list, _is_done)) {
+                                self.cm_cache._is_done = true;
+                            }
+                        }
+                    )
+                }
+            );
         },
 
         "_prepare_match_for_display": function(match, field) {
             return (
                 "<div class='oils_AS_match'><div class='oils_AS_match_term'>" +
                 match + "</div><div class='oils_AS_match_field'>" +
-                this._label_field(field) + "</div></div>"
+                this.get_field_label(field) + "</div></div>"
             );
         },
 
@@ -108,6 +157,12 @@ if (!dojo._hasResource["openils.AutoSuggestStore"]) {
             return "/opac/extras/autosuggest?" + params.join("&");
         },
 
+        "get_field_label": function(field_id) {
+            var mfield = this.cm_cache.cmf[field_id];
+            var mclass = this.cm_cache.cmc[mfield.field_class];
+            return mfield.label + " (" + mclass.label + ")";
+        },
+
         /* *** Begin dojo.data.api.Read methods *** */
 
         "getValue": function(
@@ -207,6 +262,12 @@ if (!dojo._hasResource["openils.AutoSuggestStore"]) {
             //  callback to the *req* object for the caller's use, but
             //  the one we provide does nothing but issue an alert().
 
+            if (!this.cm_cache._is_done) {
+                if (typeof req.onComplete == "function")
+                    req.onComplete.call(callback_scope, [], req);
+                return;
+            }
+
             this._current_items = {};
 
             var callback_scope = req.scope || dojo.global;
index c4c025d..d57d898 100644 (file)
@@ -3,7 +3,6 @@ if (!dojo._hasResource["openils.widget.AutoSuggest"]) {
     dojo._hasResource["openils.widget.AutoSuggest"] = true;
 
     dojo.require("dijit.form.ComboBox");
-    dojo.require("openils.widget.Searcher");
     dojo.require("openils.AutoSuggestStore");
 
     dojo.declare(
@@ -23,19 +22,15 @@ if (!dojo._hasResource["openils.widget.AutoSuggest"]) {
             "store_args": {},
 
             "_update_search_type_selector": function(id) {  /* cmf id */
-                /* Fail somewhat gracefully if a race condition, which I'm not
-                 * /certain/ actually exists, lets us get here before
-                 * openils.widget.Search has fully initialized. */
-                var f;
-                try {
-                    f = openils.widget.Searcher._cache.obj.cmf[id];
-                } catch (E) {
-                    console.log(
-                        "o.w.Searcher couldn't help us with field #" + id
+                if (!this.store.cm_cache) {
+                    console.warn(
+                        "can't update search type selector; " +
+                        "store doesn't have config.metabib_* caches available"
                     );
                     return;
                 }
 
+                var f = this.store.cm_cache.cmf[id];
                 var selector = this.type_selector;
                 var search_class = f.field_class + "|" + f.name;
                 var exact = dojo.indexOf(