X-Git-Url: http://lists.indexdata.dk/cgi-bin?a=blobdiff_plain;f=tools%2Fhtdocs%2Fmkws.js;h=1292b5f675f8fac8760b9199794739ccd632a034;hb=098b75175b5a1e665a3c11f8e09b49169a8ddeb5;hp=39038fe0f3e7bcbe827139fce816a9a0188e02e0;hpb=4db61068d161a703356b64b539148d8173a7aaf3;p=mkws-moved-to-github.git
diff --git a/tools/htdocs/mkws.js b/tools/htdocs/mkws.js
index 39038fe..1292b5f 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,67 @@ 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, type, node) {
+ var that = {
+ team: team,
+ type: type,
+ node: node
+ };
+
+ // ### More to do here, surely: e.g. wiring into the team
+ mkws.debug("made widget(team=" + team + ", type=" + type + ", node=" + node);
+
+ 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 +212,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 +268,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);
}
@@ -265,13 +291,14 @@ function team($, teamName) {
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) {
@@ -347,11 +374,13 @@ function team($, teamName) {
for (var i = 0; i < data.length && i < max; i++) {
acc.push('
');
acc.push(' 0)
? m_curPage - onsides
@@ -654,9 +679,9 @@ function team($, teamName) {
? firstClkbl + 2*onsides
: pages;
- var prev = '<< ' + M('Prev') + ' | ';
+ var prev = '<< ' + M('Prev') + ' | ';
if (m_curPage > 1)
- prev = ''
+ prev = ''
+'<< ' + M('Prev') + ' | ';
var middle = '';
@@ -665,13 +690,13 @@ function team($, teamName) {
if(i == m_curPage)
numLabel = '' + i + '';
- middle += ' '
+ middle += ' '
+ numLabel + ' ';
}
- var next = ' | ' + M('Next') + ' >>';
+ var next = ' | ' + M('Next') + ' >>';
if (pages - m_curPage > 0)
- next = ' | '
+ next = ' | '
+ M('Next') + ' >>';
var predots = '';
@@ -689,7 +714,7 @@ function team($, teamName) {
}
- mkws.showPage = function (pageNum)
+ that.showPage = function (pageNum)
{
m_curPage = pageNum;
m_paz.showPage(m_curPage - 1);
@@ -697,15 +722,15 @@ function team($, teamName) {
// simple paging functions
- mkws.pagerNext = function () {
- if (m_totalRec - m_recPerPage*m_curPage > 0) {
+ that.pagerNext = function () {
+ if (m_totalRec - m_perpage*m_curPage > 0) {
m_paz.showNext();
m_curPage++;
}
}
- mkws.pagerPrev = function () {
+ that.pagerPrev = function () {
if (m_paz.showPrev() != false)
m_curPage--;
}
@@ -751,7 +776,7 @@ function team($, teamName) {
m_curDetRecId = recId;
// remove current detailed view if any
- var detRecordDiv = document.getElementById('mkwsDet_'+oldRecId);
+ var detRecordDiv = document.getElementById('mkwsDet_' + m_teamName + '_' + oldRecId);
// lovin DOM!
if (detRecordDiv)
detRecordDiv.parentNode.removeChild(detRecordDiv);
@@ -772,23 +797,29 @@ function team($, teamName) {
{
var template = loadTemplate("Record");
var details = template(data);
- return '
' + details + '
';
+ return '
' + details + '
';
}
function loadTemplate(name)
{
- var template = mkws['template' + name];
+ var template = m_template[name];
if (template === undefined) {
- var source = $("#mkwsTemplate" + name).html();
+ // Fall back to generic template if there is no team-specific one
+ var node = $(".mkwsTemplate_" + name + ".mkwsTeam_" + m_teamName)
+ if (!node.length) {
+ node = $(".mkwsTemplate_" + name + ".mkwsTeam_ALL")
+ }
+
+ var source = node.html();
if (!source) {
source = defaultTemplate(name);
}
template = Handlebars.compile(source);
debug("compiled template '" + name + "'");
- mkws['template' + name] = template;
+ m_template[name] = template;
}
return template;
@@ -826,10 +857,10 @@ function team($, teamName) {
{{/if}}\
{{#if md-electronic-url}}\
\
@@ -884,19 +916,14 @@ 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) {
- $(node).html('\
+ mkws.handle_node_with_team($('.mkwsSearch.mkwsTeam_' + m_teamName),
+ function(tname) {
+ this.html('\
');
- });
});
debug("HTML records");
@@ -914,13 +941,13 @@ function team($, teamName) {
\
\
\
- \
+ \
\
\
- \
- \
- \
- \
+ \
+ \
+ \
+ \
\
\
\
@@ -931,9 +958,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,12 +984,21 @@ function team($, teamName) {
$(document).ready(function() { mkws.resize_page() });
}
- domReady();
+ var node;
+ node = $('.mkwsSearchForm.mkwsTeam_' + m_teamName);
+ if (node.length)
+ 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);
+ var container = $(".mkwsMOTDContainer.mkwsTeam_" + m_teamName);
if (motd.length && container.length) {
// Move the MOTD from the provided element down into the container
motd.appendTo(container);
@@ -1014,7 +1052,7 @@ function team($, teamName) {
function mkws_html_sort() {
debug("HTML sort, m_sort = '" + m_sort + "'");
- var sort_html = '