X-Git-Url: http://sru.miketaylor.org.uk/?a=blobdiff_plain;f=tools%2Fhtdocs%2Fmkws.js;h=7c11d114aa50aca215aae6574f34f90db39381bd;hb=b37b485f27da9717619c8847ce3f30294a67b12e;hp=b694bdea25947b53bb809cda92a5e2eda6f04df2;hpb=62dfac04f5b76c782dc631c5b2b7e307e72ed8bb;p=mkws-moved-to-github.git
diff --git a/tools/htdocs/mkws.js b/tools/htdocs/mkws.js
index b694bde..7c11d11 100644
--- a/tools/htdocs/mkws.js
+++ b/tools/htdocs/mkws.js
@@ -65,20 +65,19 @@ Handlebars.registerHelper('commaList', function(items, options) {
});
+Handlebars.registerHelper('index1', function(obj) {
+ return obj.data.index + 1;
+});
-// Some functions are visible to be called from outside code, namely
-// generated HTML: that.switchView(), showDetails(), limitTarget(),
-// limitQuery(), delimitTarget(), delimitQuery(), pagerPrev(),
-// pagerNext(), showPage(). Also mkws.M() is made available for the
-// Handlebars helper 'translate'
-// Set up global mkws object. Contains a hash of team objects,
-// indexed by windowid.
+// Set up global mkws object. Contains truly global state such as SP
+// authentication, and a hash of team objects, indexed by windowid.
+//
var mkws = {
authenticated: false,
- debug_function: undefined, // will be set during initialisation
- debug_level: undefined, // will be initialised from mkws_config
+ debug_level: 1, // Will be overridden from mkws_config, but
+ // initial value allows jQuery popup to use logging.
paz: undefined, // will be set up during initialisation
teams: {},
locale_lang: {
@@ -143,22 +142,65 @@ var mkws = {
};
+// The following PubSub code is modified from the jQuery manual:
+// https://api.jquery.com/jQuery.Callbacks/
+//
+// Use as:
+// mkws.queue("eventName").subscribe(function(param1, param2 ...) { ... });
+// mkws.queue("eventName").publish(arg1, arg2, ...);
+
+(function() {
+ var queues = {};
+ mkws.queue = function(id) {
+ if (!queues[id]) {
+ var callbacks = $.Callbacks();
+ queues[id] = {
+ publish: callbacks.fire,
+ subscribe: callbacks.add,
+ unsubscribe: callbacks.remove
+ };
+ }
+ return queues[id];
+ }
+}());
+
+
// Define empty mkws_config for simple applications that don't define it.
if (mkws_config == null || typeof mkws_config != 'object') {
var mkws_config = {};
}
-// wrapper for jQuery lib
+// Factory function for widget objects.
+function widget($, team, node) {
+ var that = {
+ team: team,
+ node: node
+ };
+
+ // ### More to do here, surely
+
+ return that;
+}
+
+
+// Factory function for team objects. As much as possible, this uses
+// only member variables (prefixed "m_") and inner functions with
+// private scope. Some functions are visibl as member-functions to be
+// called from outside code -- specifically, from generated
+// HTML. These functions are that.switchView(), showDetails(),
+// limitTarget(), limitQuery(), delimitTarget(), delimitQuery(),
+// pagerPrev(), pagerNext(), showPage().
+//
function team($, teamName) {
var that = {};
var m_teamName = teamName;
var m_submitted = false;
var m_query; // initially undefined
- var m_sort = 'relevance';
+ var m_sort; // will be set below
+ var m_perpage; // will be set below
var m_filters = [];
var m_totalRec = 0;
- var m_recPerPage = 20;
var m_curPage = 1;
var m_curDetRecId = '';
var m_curDetRecData = null;
@@ -168,35 +210,17 @@ function team($, teamName) {
"last": $.now()
};
var m_paz; // will be initialised below
+ var m_template = {};
- // if (console && console.log) // disabled, will fail in IE8
- // console.log("run team(" + (teamName ? teamName : "") + ")");
-
-
- // Needs to be defined inside team() so it can see m_debug_time
- // ### member access won't work: there is only one instance of this function
- mkws.debug_function = function (string) {
- if (!mkws.debug_level)
- return;
-
- if (typeof console === "undefined" || typeof console.log === "undefined") { /* ARGH!!! old IE */
- return;
- }
-
+ var debug = function (s) {
var now = $.now();
var timestamp = ((now - m_debug_time.start)/1000).toFixed(3) + " (+" + ((now - m_debug_time.last)/1000).toFixed(3) + ") "
m_debug_time.last = now;
- // you need to disable use strict at the top of the file!!!
- if (mkws.debug_level >= 3) {
- console.log(timestamp + arguments.callee.caller);
- } else if (mkws.debug_level >= 2) {
- console.log(timestamp + ">>> called from function " + arguments.callee.caller.name + ' <<<');
- }
- console.log(m_teamName + ": " + timestamp + string);
+ mkws.debug(m_teamName + ": " + timestamp + s);
}
- var debug = mkws.debug_function; // local alias
+
debug("start running MKWS");
m_sort = mkws_config.sort_default;
@@ -242,7 +266,7 @@ function team($, teamName) {
"onrecord": my_onrecord });
if (!isNaN(parseInt(mkws_config.perpage_default))) {
- m_recPerPage = parseInt(mkws_config.perpage_default);
+ m_perpage = parseInt(mkws_config.perpage_default);
}
@@ -259,24 +283,20 @@ function team($, teamName) {
function my_onshow(data, teamName) {
debug("show");
m_totalRec = data.merged;
- // move it out
- var pager = document.getElementById("mkwsPager");
- if (pager) {
- pager.innerHTML = "";
- pager.innerHTML +='
';
- drawPager(pager);
+
+ var pager = $(".mkwsPager.mkwsTeam_" + m_teamName);
+ if (pager.length) {
+ pager.html(drawPager(data))
}
- // navi
var results = $(".mkwsRecords.mkwsTeam_" + m_teamName);
+ if (!results.length)
+ return;
var html = [];
for (var i = 0; i < data.hits.length; i++) {
var hit = data.hits[i];
- html.push('
',
+ html.push('
',
renderSummary(hit),
'
');
if (hit.recid == m_curDetRecId) {
@@ -352,11 +372,13 @@ function team($, teamName) {
for (var i = 0; i < data.length && i < max; i++) {
acc.push('
\
@@ -884,17 +914,15 @@ function team($, teamName) {
if (mkws_config.show_lang)
mkws_html_lang();
- // For some reason, doing this programmatically results in
- // document.mkwsSearchForm.mkwsQuery being undefined, hence the raw HTML.
debug("HTML search form");
// ### There is only one match here by design: fix not to bother looping
$('.mkwsSearch.mkwsTeam_' + m_teamName).each(function (i, obj) {
var node = this;
- mkws.handle_node_with_team(node, function(tname) {
+ mkws.handle_node_with_team(this, function(tname) {
$(node).html('\
');
});
});
@@ -914,13 +942,13 @@ function team($, teamName) {
\
\
\
- \
+ \
\
\
- \
- \
- \
- \
+ \
+ \
+ \
+ \
\
\
\
@@ -931,9 +959,10 @@ function team($, teamName) {
');
}
- if ($("#mkwsRanking").length) {
+ var node = $(".mkwsRanking.mkwsTeam_" + m_teamName);
+ if (node.length) {
var ranking_data = '';
- ranking_data += '';
- $("#mkwsRanking").html(ranking_data);
+ node.html(ranking_data);
}
mkws_html_switch();
+ // ### Should not be in the team code, since window size is global
if (mkws_config.responsive_design_width) {
// Responsive web design - change layout on the fly based on
// current screen width. Required for mobile devices.
@@ -955,15 +985,26 @@ function team($, teamName) {
$(document).ready(function() { mkws.resize_page() });
}
- debug("before domReady()");
- domReady();
- debug("after domReady()");
+ $('.mkwsSearchForm.mkwsTeam_' + m_teamName).each(function (i, obj) {
+ debug("adding search-forms for team '" + m_teamName + "'");
+ var node = this;
+ mkws.handle_node_with_team(this, function(tname) {
+ debug("adding search-form '" + tname + "' for team '" + m_teamName + "'");
+ $(node).submit(onFormSubmitEventHandler);
+ });
+ });
+
+ node = $('.mkwsSort.mkwsTeam_' + m_teamName);
+ if (node.length)
+ node.change(onSelectDdChange);
+ node = $('.mkwsPerpage.mkwsTeam_' + m_teamName);
+ if (node.length)
+ node.change(onSelectDdChange);
// on first page, hide the termlist
$(document).ready(function() { $(".mkwsTermlists.mkwsTeam_" + m_teamName).hide(); });
var motd = $(".mkwsMOTD.mkwsTeam_" + m_teamName);
var container = $(".mkwsMOTDContainer.mkwsTeam_" + m_teamName);
- debug("for team '" + m_teamName + "', motd=" + motd + "(" + motd.length + "), container=" + container + "(" + container.length + ")");
if (motd.length && container.length) {
// Move the MOTD from the provided element down into the container
motd.appendTo(container);
@@ -1017,7 +1058,7 @@ function team($, teamName) {
function mkws_html_sort() {
debug("HTML sort, m_sort = '" + m_sort + "'");
- var sort_html = '