Break some long lines.
[mkws-moved-to-github.git] / src / mkws-team.js
index 39941d6..c26ab04 100644 (file)
@@ -28,24 +28,28 @@ function team($, teamName) {
     var m_paz; // will be initialised below
     var m_template = {};
 
-    that.name = function() { return m_teamName; }
-    that.submitted = function() { return m_submitted; }
-    that.perpage = function() { return m_perpage; }
-    that.totalRecordCount = function() { return m_totalRecordCount; }
-    that.currentPage = function() { return m_currentPage; }
-    that.currentRecordId = function() { return m_currentRecordId; }
-    that.currentRecordData = function() { return m_currentRecordData; }
-    that.filters = function() { return m_filters; }
-
+    that.toString = function() { return '[Team ' + teamName + ']'; };
+
+    // Accessor methods for individual widgets: readers
+    that.name = function() { return m_teamName; };
+    that.submitted = function() { return m_submitted; };
+    that.perpage = function() { return m_perpage; };
+    that.totalRecordCount = function() { return m_totalRecordCount; };
+    that.currentPage = function() { return m_currentPage; };
+    that.currentRecordId = function() { return m_currentRecordId; };
+    that.currentRecordData = function() { return m_currentRecordData; };
+    that.filters = function() { return m_filters; };
+
+    // Accessor methods for individual widgets: writers
     that.set_sortOrder = function(val) { m_sortOrder = val };
     that.set_perpage = function(val) { m_perpage = val };
 
 
-    var log = function (s) {
+    function log(s) {
        var now = $.now();
-       var timestamp = ((now - m_logTime.start)/1000).toFixed(3) + " (+" + ((now - m_logTime.last)/1000).toFixed(3) + ") "
+       var timestamp = (((now - m_logTime.start)/1000).toFixed(3) + " (+" +
+                        ((now - m_logTime.last)/1000).toFixed(3) + ") ");
        m_logTime.last = now;
-
        mkws.log(m_teamName + ": " + timestamp + s);
     }
     that.log = log;
@@ -73,34 +77,28 @@ function team($, teamName) {
                    });
 
 
-    //
     // pz2.js event handlers:
-    //
     function onInit() {
        log("init");
        m_paz.stat();
        m_paz.bytarget();
     }
 
-
     function onBytarget(data) {
        log("target");
        queue("targets").publish(data);
     }
 
-
     function onStat(data) {
        log("stat");
        queue("stat").publish(data);
     }
 
-
     function onTerm(data) {
        log("term");
        queue("termlists").publish(data);
     }
 
-
     function onShow(data, teamName) {
        log("show");
        m_totalRecordCount = data.merged;
@@ -108,14 +106,12 @@ function team($, teamName) {
        queue("records").publish(data);
     }
 
-
     function onRecord(data, args, teamName) {
        log("record");
        // FIXME: record is async!!
        clearTimeout(m_paz.recordTimer);
-       // ##### restrict to current team
-       var detRecordDiv = document.getElementById(recordDetailsId(data.recid[0]));
-       if (detRecordDiv) {
+       var detRecordDiv = findnode(recordDetailsId(data.recid[0]));
+       if (detRecordDiv.length) {
            // in case on_show was faster to redraw element
            return;
        }
@@ -126,7 +122,7 @@ function team($, teamName) {
     }
 
 
-    // Used by promoteRecords() and onRecord()
+    // Used by the Records widget and onRecord()
     function recordElementId(s) {
        return 'mkwsRec_' + s.replace(/[^a-z0-9]/ig, '_');
     }
@@ -136,7 +132,6 @@ function team($, teamName) {
     function recordDetailsId(s) {
        return 'mkwsDet_' + s.replace(/[^a-z0-9]/ig, '_');
     }
-    that.recordElementId = recordElementId;
 
 
     that.targetFiltered = function(id) {
@@ -147,100 +142,86 @@ function team($, teamName) {
            }
        }
        return false;
-    }
-
-
-    ////////////////////////////////////////////////////////////////////////////////
-    ////////////////////////////////////////////////////////////////////////////////
-
-
-    // when search button pressed
-    function onFormSubmitEventHandler()
-    {
-       var val = findnode('.mkwsQuery').val();
-       newSearch(val);
-       return false;
-    }
-
-
-    function newSearch(query, sortOrder, targets)
-    {
-       log("newSearch: " + query);
-
-       if (mkws_config.use_service_proxy && !mkws.authenticated) {
-           alert("searching before authentication");
-           return;
-       }
-
-       m_filters = []
-       triggerSearch(query, sortOrder, targets);
-       switchView('records'); // In case it's configured to start off as hidden
-       m_submitted = true;
-    }
+    };
 
 
-    // limit by target functions
-    that.limitTarget  = function (id, name)
+    that.limitTarget = function(id, name)
     {
        log("limitTarget(id=" + id + ", name=" + name + ")");
        m_filters.push({ id: id, name: name });
        triggerSearch();
        return false;
-    }
+    };
 
 
-    // limit the query after clicking the facet
-    that.limitQuery = function (field, value)
+    that.limitQuery = function(field, value)
     {
        log("limitQuery(field=" + field + ", value=" + value + ")");
        m_filters.push({ field: field, value: value });
        triggerSearch();
        return false;
-    }
+    };
 
 
-    that.delimitTarget = function (id)
+    that.delimitTarget = function(id)
     {
        log("delimitTarget(id=" + id + ")");
-       var newFilters = [];
-       for (var i in m_filters) {
-           var filter = m_filters[i];
-           if (filter.id) {
-               log("delimitTarget() removing filter " + $.toJSON(filter));
-           } else {
-               log("delimitTarget() keeping filter " + $.toJSON(filter));
-               newFilters.push(filter);
-           }
-       }
-       m_filters = newFilters;
-
+       removeMatchingFilters(function(f) { return f.id });
        triggerSearch();
        return false;
-    }
+    };
 
 
-    that.delimitQuery = function (field, value)
+    that.delimitQuery = function(field, value)
     {
        log("delimitQuery(field=" + field + ", value=" + value + ")");
+       removeMatchingFilters(function(f) { return f.field && field == f.field && value == f.value });
+       triggerSearch();
+       return false;
+    };
+
+
+    function removeMatchingFilters(matchFn) {
        var newFilters = [];
        for (var i in m_filters) {
            var filter = m_filters[i];
-           if (filter.field &&
-               field == filter.field &&
-               value == filter.value) {
-               log("delimitQuery() removing filter " + $.toJSON(filter));
+           if (matchFn(filter)) {
+               log("removeMatchingFilters() removing filter " + $.toJSON(filter));
            } else {
-               log("delimitQuery() keeping filter " + $.toJSON(filter));
+               log("removeMatchingFilters() keeping filter " + $.toJSON(filter));
                newFilters.push(filter);
            }
        }
        m_filters = newFilters;
-
-       triggerSearch();
-       return false;
     }
 
 
+    that.showPage = function(pageNum)
+    {
+       m_currentPage = pageNum;
+       m_paz.showPage(m_currentPage - 1);
+    };
+
+
+    that.pagerNext = function() {
+       if (m_totalRecordCount - m_perpage*m_currentPage > 0) {
+            m_paz.showNext();
+            m_currentPage++;
+       }
+    };
+
+
+    that.pagerPrev = function() {
+       if (m_paz.showPrev() != false)
+            m_currentPage--;
+    };
+
+
+    that.reShow = function() {
+       m_paz.show(0, m_perpage, m_sortOrder);
+    };
+
+
     function resetPage()
     {
        m_currentPage = 1;
@@ -249,7 +230,23 @@ function team($, teamName) {
     that.resetPage = resetPage;
 
 
-    function triggerSearch (query, sortOrder, targets)
+    function newSearch(query, sortOrder, targets)
+    {
+       log("newSearch: " + query);
+
+       if (mkws_config.use_service_proxy && !mkws.authenticated) {
+           alert("searching before authentication");
+           return;
+       }
+
+       m_filters = []
+       triggerSearch(query, sortOrder, targets);
+       switchView('records'); // In case it's configured to start off as hidden
+       m_submitted = true;
+    }
+
+
+    function triggerSearch(query, sortOrder, targets)
     {
        resetPage();
        queue("navi").publish();
@@ -291,7 +288,8 @@ function team($, teamName) {
            params.limit = pp2limit;
        }
 
-       log("triggerSearch(" + m_query + "): filters = " + $.toJSON(m_filters) + ", pp2filter = " + pp2filter + ", params = " + $.toJSON(params));
+       log("triggerSearch(" + m_query + "): filters = " + $.toJSON(m_filters) + ", " +
+           "pp2filter = " + pp2filter + ", params = " + $.toJSON(params));
 
        // We can use: params.torusquery = "udb=NAME"
        // Note: that won't work when running against raw pazpar2
@@ -299,34 +297,6 @@ function team($, teamName) {
     }
 
 
-    that.reShow = function() {
-       m_paz.show(0, m_perpage, m_sortOrder);
-    }
-
-
-
-    that.showPage = function (pageNum)
-    {
-       m_currentPage = pageNum;
-       m_paz.showPage(m_currentPage - 1);
-    }
-
-
-    // simple paging functions
-    that.pagerNext = function () {
-       if (m_totalRecordCount - m_perpage*m_currentPage > 0) {
-            m_paz.showNext();
-            m_currentPage++;
-       }
-    }
-
-
-    that.pagerPrev = function () {
-       if (m_paz.showPrev() != false)
-            m_currentPage--;
-    }
-
-
     // switching view between targets and records
     function switchView(view) {
        var targets = findnode('.mkwsTargets');
@@ -362,7 +332,7 @@ function team($, teamName) {
 
 
     // detailed record drawing
-    that.showDetails = function (recId) {
+    that.showDetails = function(recId) {
        var oldRecordId = m_currentRecordId;
        m_currentRecordId = recId;
 
@@ -382,7 +352,7 @@ function team($, teamName) {
        // request the record
        log("showDetails() requesting record '" + recId + "'");
        m_paz.record(recId);
-    }
+    };
 
 
     /*
@@ -443,7 +413,11 @@ function team($, teamName) {
 
        mkwsHtmlSwitch();
 
-       findnode('.mkwsSearchForm').submit(onFormSubmitEventHandler);
+       findnode('.mkwsSearchForm').submit(function() {
+           var val = findnode('.mkwsQuery').val();
+           newSearch(val);
+           return false;
+       });
 
        // on first page, hide the termlist
        $(document).ready(function() { findnode(".mkwsTermlists").hide(); });
@@ -602,7 +576,7 @@ function team($, teamName) {
        log(s);
 
        newSearch(query, sortOrder, targets);
-    }
+    };
 
 
     // This function is taken from a StackOverflow answer
@@ -642,7 +616,9 @@ function team($, teamName) {
            return s + '.mkwsTeam_' + teamName;
        }).join(',');
 
-       return $(selector);
+       var node = $(selector);
+       //log('findnode(' + selector + ') found ' + node.length + ' nodes');
+       return node;
     }
 
 
@@ -650,7 +626,8 @@ function team($, teamName) {
     {
        var template = loadTemplate("Record");
        var details = template(data);
-       return '<div class="details mkwsTeam_' + m_teamName + '" id="' + recordDetailsId(data.recid[0]) + '">' + details + '</div>';
+       return '<div class="details mkwsTeam_' + m_teamName + '" ' +
+           'id="' + recordDetailsId(data.recid[0]) + '">' + details + '</div>';
     }
     that.renderDetails = renderDetails;
 
@@ -780,23 +757,10 @@ function team($, teamName) {
            };
        }
        return queues[id];
-    }
+    };
     that.queue = queue;
 
+    mkwsHtmlAll()
 
-    // main
-    (function() {
-       try {
-           mkwsHtmlAll()
-       }
-
-       catch (e) {
-           mkws_config.error = e.message;
-           // alert(e.message);
-       }
-    })();
-
-    // Bizarrely, 'that' is just an empty hash. All its state is in
-    // the closure variables defined earlier in this function.
     return that;
 };