From 9fc300ff8b152249bd56c6c08e5008a04778e90b Mon Sep 17 00:00:00 2001 From: Mike Taylor Date: Wed, 26 Mar 2014 17:06:16 +0000 Subject: [PATCH] Move that various specific-widget-type functions outside the scope of the generic widget object. Involves a lot of tedious mucking about with var that=this, but it's worth it because it makes each individual widget type its own thing, ready to moved out into a separate file. --- src/mkws-widgets.js | 116 +++++++++++++++++++++++++++++++-------------------- 1 file changed, 70 insertions(+), 46 deletions(-) diff --git a/src/mkws-widgets.js b/src/mkws-widgets.js index 377419e..9ba58bb 100644 --- a/src/mkws-widgets.js +++ b/src/mkws-widgets.js @@ -6,6 +6,15 @@ function widget($, team, type, node) { node: node }; + function log(s) { + team.log(s); + } + that.log = log; + + that.toString = function() { + return '[Widget ' + team.name() + ':' + type + ']'; + } + mkws.registerWidgetType('Targets', promoteTargets); mkws.registerWidgetType('Stat', promoteStat); mkws.registerWidgetType('Termlists', promoteTermlists); @@ -15,29 +24,27 @@ function widget($, team, type, node) { mkws.registerWidgetType('Sort', promoteSort); mkws.registerWidgetType('Perpage', promotePerpage); - var M = mkws.M; - var promote = mkws.promotionFunction(type); if (promote) { - promote(); + promote.call(that); log("made " + type + " widget(node=" + node + ")"); } else { log("made UNENCAPSULATED widget(type=" + type + ", node=" + node + ")"); } return that; +} - function log(s) { - team.log(s); - } - // Functions follow for promoting the regular widget object into // widgets of specific types. These could be moved outside of the // widget object, or even into their own source files. function promoteTargets() { - team.queue("targets").subscribe(function(data) { + var that = this; + var M = mkws.M; + + this.team.queue("targets").subscribe(function(data) { var table ='' + '' + '' + @@ -55,17 +62,20 @@ function widget($, team, type, node) { } table += '
' + M('Target ID') + '' + M('Hits') + '
'; - var subnode = $(node).children('.mkwsBytarget'); + var subnode = $(that.node).children('.mkwsBytarget'); subnode.html(table); }); } function promoteStat() { - team.queue("stat").subscribe(function(data) { - if (node.length === 0) alert("huh?!"); + var that = this; + var M = mkws.M; - $(node).html('' + M('Status info') + '' + + this.team.queue("stat").subscribe(function(data) { + if (that.node.length === 0) alert("huh?!"); + + $(that.node).html('' + M('Status info') + '' + ' -- ' + '' + M('Active clients') + ': ' + data.activeclients + '/' + data.clients + '' + ' -- ' + @@ -75,8 +85,11 @@ function widget($, team, type, node) { function promoteTermlists() { - team.queue("termlists").subscribe(function(data) { - if (!node) { + var that = this; + var M = mkws.M; + + this.team.queue("termlists").subscribe(function(data) { + if (!that.node) { alert("termlists event when there are no termlists"); return; } @@ -84,12 +97,12 @@ function widget($, team, type, node) { // no facets: this should never happen if (!mkws_config.facets || mkws_config.facets.length == 0) { alert("onTerm called even though we have no facets: " + $.toJSON(data)); - $(node).hide(); + $(that.node).hide(); return; } // display if we first got results - $(node).show(); + $(that.node).show(); var acc = []; acc.push('
' + M('Termlists') + '
'); @@ -107,10 +120,11 @@ function widget($, team, type, node) { } } - $(node).html(acc.join('')); + $(that.node).html(acc.join('')); function addSingleFacet(acc, caption, data, max, pzIndex) { - acc.push('
'); + var teamName = that.team.name(); + acc.push('
'); acc.push('
' + M(caption) + '
'); for (var i = 0; i < data.length && i < max; i++) { acc.push('
'); @@ -119,11 +133,11 @@ function widget($, team, type, node) { if (!pzIndex) { // Special case: target selection acc.push('target_id='+data[i].id+' '); - if (!team.targetFiltered(data[i].id)) { - action = 'mkws.limitTarget(\'' + team.name() + '\', this.getAttribute(\'target_id\'),this.firstChild.nodeValue)'; + if (!that.team.targetFiltered(data[i].id)) { + action = 'mkws.limitTarget(\'' + teamName + '\', this.getAttribute(\'target_id\'),this.firstChild.nodeValue)'; } } else { - action = 'mkws.limitQuery(\'' + team.name() + '\', \'' + pzIndex + '\', this.firstChild.nodeValue)'; + action = 'mkws.limitQuery(\'' + teamName + '\', \'' + pzIndex + '\', this.firstChild.nodeValue)'; } acc.push('onclick="' + action + ';return false;">' + data[i].name + '' + ' ' + data[i].freq + ''); @@ -136,10 +150,14 @@ function widget($, team, type, node) { function promotePager() { - team.queue("pager").subscribe(function(data) { - $(node).html(drawPager(data)) + var that = this; + var M = mkws.M; + + this.team.queue("pager").subscribe(function(data) { + $(that.node).html(drawPager(data)) function drawPager(data) { + var teamName = that.team.name(); var s = '
' + M('Displaying') + ': ' + (data.start + 1) + ' ' + M('to') + ' ' + (data.start + data.num) + ' ' + M('of') + ' ' + data.merged + ' (' + M('found') + ': ' @@ -147,8 +165,8 @@ function widget($, team, type, node) { //client indexes pages from 1 but pz2 from 0 var onsides = 6; - var pages = Math.ceil(team.totalRecordCount() / team.perpage()); - var currentPage = team.currentPage(); + var pages = Math.ceil(that.team.totalRecordCount() / that.team.perpage()); + var currentPage = that.team.currentPage(); var firstClkbl = (currentPage - onsides > 0) ? currentPage - onsides @@ -160,7 +178,7 @@ function widget($, team, type, node) { var prev = '<< ' + M('Prev') + ' | '; if (currentPage > 1) - prev = '' + prev = '' +'<< ' + M('Prev') + ' | '; var middle = ''; @@ -169,13 +187,13 @@ function widget($, team, type, node) { if(i == currentPage) numLabel = '' + i + ''; - middle += ' ' + middle += ' ' + numLabel + ' '; } var next = ' | ' + M('Next') + ' >>'; if (pages - currentPage > 0) - next = ' | ' + next = ' | ' + M('Next') + ' >>'; var predots = ''; @@ -196,7 +214,10 @@ function widget($, team, type, node) { function promoteRecords() { - team.queue("records").subscribe(function(data) { + var that = this; + var team = this.team; + + this.team.queue("records").subscribe(function(data) { var html = []; for (var i = 0; i < data.hits.length; i++) { var hit = data.hits[i]; @@ -210,7 +231,7 @@ function widget($, team, type, node) { html.push(team.renderDetails(team.currentRecordData())); } } - $(node).html(html.join('')); + $(that.node).html(html.join('')); function renderSummary(hit) { @@ -224,8 +245,12 @@ function widget($, team, type, node) { function promoteNavi() { - team.queue("navi").subscribe(function() { - var filters = team.filters(); + var that = this; + var teamName = this.team.name(); + var M = mkws.M; + + this.team.queue("navi").subscribe(function() { + var filters = that.team.filters(); var text = ""; for (var i in filters) { @@ -234,16 +259,16 @@ function widget($, team, type, node) { } var filter = filters[i]; if (filter.id) { - text += M('source') + ': ' + filter.name + ''; } else { - text += M(filter.field) + ': ' + filter.value + ''; } } - $(node).html(text); + $(that.node).html(text); }); } @@ -252,11 +277,11 @@ function widget($, team, type, node) { // It seems this and the Perpage widget doen't need to // subscribe to anything, since they produce events rather // than consuming them. - $(node).change(function () { - team.set_sortOrder($(node).val()); - if (team.submitted()) { - team.resetPage(); - team.reShow(); + $(this.node).change(function () { + this.team.set_sortOrder($(node).val()); + if (this.team.submitted()) { + this.team.resetPage(); + this.team.reShow(); } return false; }); @@ -264,13 +289,12 @@ function widget($, team, type, node) { function promotePerpage() { - $(node).change(function() { - team.set_perpage($(node).val()); - if (team.submitted()) { - team.resetPage(); - team.reShow(); + $(this.node).change(function() { + this.team.set_perpage($(node).val()); + if (this.team.submitted()) { + this.team.resetPage(); + this.team.reShow(); } return false; }); } -} -- 1.7.10.4