add search.js
authorJason Etheridge <jason@esilibrary.com>
Tue, 1 Oct 2013 20:16:19 +0000 (16:16 -0400)
committerJason Etheridge <jason@esilibrary.com>
Tue, 1 Oct 2013 21:09:58 +0000 (17:09 -0400)
Open-ILS/xul/staff_client/server/patron/search.js [new file with mode: 0644]

diff --git a/Open-ILS/xul/staff_client/server/patron/search.js b/Open-ILS/xul/staff_client/server/patron/search.js
new file mode 100644 (file)
index 0000000..716b4b4
--- /dev/null
@@ -0,0 +1,455 @@
+/** FIXME: kludge to allow for testing with stock clients **/
+
+function my_init() {
+    try {
+        if (xul_param('id')||xul_param('barcode')) {
+            $('uber_deck').selectedIndex = 2;
+            patron_init();
+        } else {
+            $('uber_deck').selectedIndex = 1;
+            search_init();
+        }
+    } catch(E) {
+        alert('Error in my_init(): ' + E);
+    }
+}
+
+function default_focus() {
+    if (xul_param('id')||xul_param('barcode')) {
+        patron_default_focus();
+    } else {
+        search_default_focus();
+    }
+}
+
+/******/
+
+function search_init() {
+    try {
+        if (typeof JSAN == 'undefined') {
+            throw( "The JSAN library object is missing.");
+        }
+        JSAN.errorLevel = "die"; // none, warn, or die
+        JSAN.addRepository('oils://remote/xul/server/');
+        JSAN.use('util.error'); g.error = new util.error();
+        g.error.sdump('D_TRACE','my_init() for patron/search.xul');
+
+        JSAN.use('OpenILS.data');
+        g.data = new OpenILS.data();
+        g.data.stash_retrieve();
+
+        JSAN.use('util.file');
+        JSAN.use('util.widgets');
+        JSAN.use('util.functional');
+        JSAN.use('patron.search_result');
+
+        try {
+            window.xulG.set_tab_name(
+                xul_param('perm_editor')
+                ? 'Patron Search (for Perm Editor)'
+                : 'Patron Search (for Check Out)'
+            );
+        } catch(E) {}
+
+        search_ui_setup();
+        g.search = new patron.search_result();
+        g.search.init({});
+        $('patron_list_actions').appendChild(
+            g.search.list.render_list_actions()
+        );
+        g.search.list.set_list_actions();
+        search_default_focus();
+
+    } catch(E) {
+        alert('Error in patron/search.xul: ' + E);
+    }
+}
+
+function handle_enter(ev) {
+    try {
+        if (ev.target.tagName != 'textbox') {
+            return;
+        }
+        if (ev.keyCode == 13 /* enter */
+        || ev.keyCode == 77 /* enter on a mac */) {
+            submit();
+        }
+    } catch(E) {
+        dump('Error in patron/search.xul, handle_enter(): ' + E + '\n');
+    }
+}
+
+function update_search_summary_view() {
+    try {
+        var patrons = g.search.list.retrieve_selection_retrieval_data();
+        if (patrons.length > 0) {
+            $('iframe').setAttribute('src','data:,Loading...');
+            setTimeout(
+                function() {
+                    $('iframe').setAttribute(
+                        'src',
+                        urls.EG_PATRON_SEARCH_SUMMARY+'?au_id='+patrons[0]
+                    );
+                }, 0
+            );
+        }
+    } catch(E) {
+        dump('Error in patron/search.xul, update_search_summary_view(): ' + E + '\n');
+    }
+}
+
+function swap_view(ev) {
+    try {
+        var idx = $('deck').selectedIndex;
+        if (idx == 0) {
+            $('deck').selectedIndex = 1;
+            $('patron_list').focus();
+            update_search_summary_view();
+        } else {
+            $('deck').selectedIndex = 0;
+            search_default_focus();
+        }
+    } catch(E) {
+        dump('Error in patron/search.xul, swap_view(): ' + E + '\n');
+    }
+}
+
+function handle_merge(ev) {
+    try {
+        JSAN.use('patron.util');
+        if (
+            patron.util.merge(
+                g.search.list.retrieve_selection_retrieval_data()
+            )
+        ) {
+            util.widgets.dispatch('command','cmd_submit');
+        }
+    } catch(E) {
+        dump('Error in patron/search.xul, handle_merge: ' + E + '\n');
+    }
+}
+
+function search_ui_setup() {
+    try {
+        dump('entering patron/search.xul, ui_setup()\n');
+
+        $('cmd_swap_view').setAttribute('disabled','true');
+        $('cmd_retrieve').setAttribute('disabled','true');
+        $('cmd_merge').setAttribute('disabled','true');
+
+        $('cmd_swap_view').addEventListener(
+            'command',
+            swap_view,
+            false
+        );
+
+        $('cmd_clear').addEventListener(
+            'command',
+            clear_form,
+            false
+        );
+
+        $('cmd_submit').addEventListener(
+            'command',
+            submit,
+            false
+        );
+
+        $('cmd_retrieve').addEventListener(
+            'command',
+            function(ev) { retrieve_patrons(); },
+            false
+        );
+
+        $('cmd_merge').addEventListener(
+            'command',
+            handle_merge,
+            false
+        );
+
+        var nl = document.getElementsByTagName('textbox');
+        for (var i = 0; i < nl.length; i++) {
+            nl[i].addEventListener(
+                'keypress',
+                handle_enter,
+                false
+            );
+        }
+
+        util.widgets.remove_children('search_ou');
+        if (! $('search_ou').hasAttribute('value')) {
+            $('search_ou').setAttribute('value',g.data.tree.aou.id());
+        }
+
+        var search_ou_ml = util.widgets.make_menulist(
+            util.functional.map_list( g.data.list.my_aou,
+                function(el,idx) {
+                    return [ el.name(), el.id() ]
+                }
+            ).sort(
+                function(a,b) {
+                    if (a[1] < b[1]) return -1;
+                    if (a[1] > b[1]) return 1;
+                    return 0;
+                }
+            ),
+            $('search_ou').getAttribute('value')
+        );
+        search_ou_ml.addEventListener(
+            'command',
+            function(ev) {
+                search_ou_ml.parentNode.setAttribute(
+                    'value',
+                    ev.target.value
+                );
+                oils_persist(search_ou_ml.parentNode);
+            },
+            false
+        );
+        search_ou_ml.setAttribute('id','search_ou_ml');
+        $('search_ou').appendChild(search_ou_ml);
+
+        util.widgets.remove_children('profile');
+        var profile_ml = util.widgets.make_menulist(
+            [
+                ['','']
+            ].concat(
+                util.functional.map_list(
+                    g.data.list.pgt,
+                    function(el,idx) {
+                        return [ el.name(), el.id() ]
+                    }
+                ).sort(
+                    function(a,b) {
+                        if (a[0] < b[0]) return -1;
+                        if (a[0] > b[0]) return 1;
+                        return 0;
+                    }
+                )
+            ),
+            $('profile').getAttribute('value')
+        );
+        profile_ml.addEventListener(
+                'command',
+                function(ev) {
+                    profile_ml.parentNode.setAttribute(
+                        'value',
+                        ev.target.value
+                    );
+                    oils_persist(profile_ml.parentNode);
+                },
+                false
+        );
+        profile_ml.setAttribute('id','profile_ml');
+        $('profile').appendChild(profile_ml);
+
+    } catch(E) {
+        dump('Error in patron/search.xul, ui_setup(): ' + E + '\n');
+    }
+}
+
+function search_default_focus() {
+    setTimeout(
+        function() {
+            try {
+                dump('entering patron/search.xul, default_focus()\n');
+                var field = xul_param('focus')||'card';
+                // HACK for while we're symlinking barcode_entry.xul
+                // to allow testing with stock clients
+                if (! location.href.match(/barcode_entry/)) {
+                    field = 'family_name';
+                }
+                $(field).select();
+                $(field).focus();
+            } catch(E) {
+                dump('Error in patron/search.xul, default_focus(): ' + E + '\n');
+            }
+        }, 0
+    );
+}
+
+function clear_form() {
+    try {
+        var render_list = document.getElementsByAttribute('group','*');
+        for (var i = 0; i < render_list.length; i++) {
+            var node = render_list[i];
+            var id = node.id;
+            if (id == 'inactive') {
+                // no-op, sticky
+            } else if (id == 'profile'
+            || id == 'search_ou') {
+                // no-op, sticky
+            } else {
+                render_list[i].value = '';
+            }
+        }
+        search_default_focus();
+    } catch(E) {
+        dump('Error in patron/search.xul, clear_form(): ' + E + '\n');
+    }
+}
+
+var _submitting = false;
+function submit(ev) {
+    if (_submitting) {
+        dump('submit() in patron/search.xul while already submitting\n');
+        return;
+    }
+    _submitting = true;
+    try {
+        var query = {};
+        var render_list = document.getElementsByAttribute('group','*');
+        for (var i = 0; i < render_list.length; i++) {
+            var node = render_list[i];
+            var id = node.id;
+            var value;
+            if (id == 'inactive') {
+                value = node.getAttribute('checked');
+            } else if (id == 'profile'
+            || id == 'search_ou') {
+                value  = node.getAttribute('value');
+            } else {
+                value = node.value.replace(/^\s+/,'').replace(/[\\\s]+$/,'');
+                switch(id) {
+                    case 'family_name' :
+                    case 'first_given_name' :
+                    case 'second_given_name' :
+                        value = value.replace(/^[\d\s]+/g,'').replace(/[\d\s]+$/g,'')
+                    break;
+                }
+            }
+            if (value != '') {
+                query[id] = value;
+            }
+        }
+        search(query,undefined,undefined);
+    } catch(E) {
+        dump('Error in patron/search.xul, submit(): ' + E + '\n');
+    }
+}
+
+function handle_search_select(ev){
+    var select_count = g.search.list.retrieve_selection_retrieval_data().length;
+    $('cmd_swap_view').setAttribute('disabled','true');
+    $('cmd_retrieve').setAttribute('disabled','true');
+    $('cmd_merge').setAttribute('disabled','true');
+    if (select_count > 0) {
+        $('cmd_swap_view').setAttribute('disabled','false');
+        $('cmd_retrieve').setAttribute('disabled','false');
+    }
+    if (select_count > 1) {
+        $('cmd_merge').setAttribute('disabled','false');
+    }
+    if (_submitting) { return; }
+    if ($('deck').selectedIndex == 1) {
+        update_search_summary_view();
+    } else {
+        swap_view();
+    }
+}
+
+function want_auto_retrieve() {
+    if ($('auto').getAttribute('checked') == 'true') {
+        return true;
+    }
+    if ($('auto_via_barcode').getAttribute('checked') == 'true'
+        && $('card').value != ''
+    ) {
+        return true;
+    }
+    return false;
+}
+
+function want_tab_replace() {
+    if ($('replace_tab').getAttribute('checked') == 'true') {
+        return true;
+    }
+    if ($('replace_tab_via_barcode').getAttribute('checked') == 'true'
+        && $('card').value != ''
+    ) {
+        return true;
+    }
+    return false;
+}
+
+function search(query,limit,sort) {
+    try {
+        g.search.search_limit = limit;
+        g.search.search_sort = sort;
+        g.search.on_select =  handle_search_select;
+        g.search.on_dblclick = function(ev) { retrieve_patrons() };
+        g.search.on_finished = function(results) { _submitting = false; }
+        var patron_parts = [];
+        if ($('retrieve_library_card').getAttribute('checked') == 'true') {
+            patron_parts.push('card');
+        }
+        if ($('retrieve_mailing_address').getAttribute('checked') == 'true') {
+            patron_parts.push('mailing_address');
+        }
+        if ($('retrieve_billing_address').getAttribute('checked') == 'true') {
+            patron_parts.push('billing_address');
+        }
+        g.search.patron_parts = patron_parts;
+        g.search.skip_render_of_single_result = want_tab_replace();
+        if (want_auto_retrieve()) {
+            g.search.single_result_callback =
+                function(id) { retrieve_patrons([id]); }
+        } else {
+            g.search.single_result_callback = null;
+        }
+        g.search.list.clear();
+        $('patron_list').focus();
+        g.search.search(query);
+    } catch(E) {
+        dump('Error in patron/search.xul, search(): ' + E + '\n');
+    }
+}
+
+function retrieve_patrons(patrons) {
+    if (!patrons) {
+        patrons = g.search.list.retrieve_selection_retrieval_data();
+    }
+    var current_tab_index = typeof xulG.get_idx == 'function'
+        ? xulG.get_idx()
+        : -1; // Just want to test this code without requiring a
+              // local staff client upgrade. get_idx is needed if
+              // we want to replace a tab that does not have focus
+    for (
+        var i = want_tab_replace()
+        ? 1 // open new tabs before replacing current tab
+        : 0;
+        i < patrons.length;
+        i++
+    ) {
+        xulG.new_tab(
+            urls.XUL_PATRON_DISPLAY,{
+                'nofocus' : current_tab_index == -1
+                && want_tab_replace()
+            },{
+                'id':patrons[i],
+                'interface' : xul_param('perm_editor')
+                ? 'perms'
+                : 'checkout'
+            }
+        );
+    }
+    if (want_tab_replace()) {
+        var tab_params = {};
+        if (current_tab_index != -1) {
+            tab_params.index = current_tab_index;
+        }
+        xulG.set_tab(
+            urls.XUL_PATRON_DISPLAY,
+            tab_params,{
+                'id':patrons[0],
+                'interface' : xul_param('perm_editor')
+                ? 'perms'
+                : 'checkout'
+            }
+        );
+    } else {
+        if (current_tab_index == -1) {
+            search_default_focus();
+        }
+    }
+}