From: Jason Skomorowski Date: Mon, 9 Jun 2014 14:01:02 +0000 (-0400) Subject: Initial templatification + Records widget X-Git-Tag: 1.0.0~324^2~67 X-Git-Url: http://sru.miketaylor.org.uk/cgi-bin?a=commitdiff_plain;h=86229ffbb4ef43e4f028c1f59fa93dc5b2285770;p=mkws-moved-to-github.git Initial templatification + Records widget --- diff --git a/examples/htdocs/two-teams.html b/examples/htdocs/two-teams.html index 006f11b..694522e 100644 --- a/examples/htdocs/two-teams.html +++ b/examples/htdocs/two-teams.html @@ -9,7 +9,7 @@ - + diff --git a/src/Makefile b/src/Makefile index 82cf254..8e8e48d 100644 --- a/src/Makefile +++ b/src/Makefile @@ -11,14 +11,15 @@ PP2_URL = http://git.indexdata.com/?p=pazpar2.git;a=blob_plain;f=js/${PP2_FILE}; JQUERY_UI_URL = http://code.jquery.com/ui/1.10.3/jquery-ui.js VERSION = $(shell tr -d '\012' < VERSION) -COMPONENTS = mkws-handlebars.js \ +COMPONENTS = mkws-handlebars.js\ mkws-core.js mkws-team.js mkws-filter.js \ + mkws-templates.js\ mkws-widget.js mkws-widget-main.js mkws-widget-termlists.js \ mkws-widget-authname.js mkws-widget-categories.js mkws-widget-log.js \ mkws-widget-record.js mkws-widget-reference.js mkws-widget-builder.js GENERATED = ${HANDLEBARS_FILE} ${JQUERY_FILE} ${JQUERY_JSON_FILE} ${PP2_FILE} \ - mkws.js mkws.min.js mkws-complete.js mkws-complete.min.js + mkws.js mkws.min.js mkws-complete.js mkws-complete.min.js mkws-templates.js INSTALLABLE = mkws-jquery.js VERSION NEWS $(GENERATED) @@ -93,6 +94,9 @@ mkws.js: $(COMPONENTS) Makefile cat $(COMPONENTS) > $@ chmod 444 $@ +mkws-templates.js: + handlebars -n mkws.defaultTemplates *.templates/*.handlebars -f mkws-templates.js + distclean: clean rm -f *.orig *.bak *.rej diff --git a/src/mkws-core.js b/src/mkws-core.js index 7091291..7b776ab 100644 --- a/src/mkws-core.js +++ b/src/mkws-core.js @@ -16,6 +16,7 @@ var mkws = { // initial value allows jQuery popup to use logging. teams: {}, widgetType2function: {}, + defaultTemplates: {}, locale_lang: { "de": { @@ -178,95 +179,6 @@ mkws.objectInheritingFrom = function(o) { } -mkws.defaultTemplate = function(name) { - if (name === 'Record') { - return '\ -
\ - \ - \ - \ - \ - {{#if md-date}}\ - \ - \ - \ - \ - {{/if}}\ - {{#if md-author}}\ - \ - \ - \ - \ - {{/if}}\ - {{#if md-electronic-url}}\ - \ - \ - \ - \ - {{/if}}\ - {{#mkws-if-any location having="md-subject"}}\ - \ - \ - \ - \ - {{/mkws-if-any}}\ - \ - \ - \ - \ -
{{mkws-translate "Title"}}\ - {{md-title}}\ - {{#if md-title-remainder}}\ - ({{md-title-remainder}})\ - {{/if}}\ - {{#if md-title-responsibility}}\ - {{md-title-responsibility}}\ - {{/if}}\ -
{{mkws-translate "Date"}}{{md-date}}
{{mkws-translate "Author"}}{{md-author}}
{{mkws-translate "Links"}}\ - {{#each md-electronic-url}}\ - Link{{mkws-index1}}\ - {{/each}}\ -
{{mkws-translate "Subject"}}\ - {{#mkws-first location having="md-subject"}}\ - {{#if md-subject}}\ - {{#mkws-commaList md-subject}}\ - {{this}}{{/mkws-commaList}}\ - {{/if}}\ - {{/mkws-first}}\ -
{{mkws-translate "Locations"}}\ - {{#mkws-commaList location}}\ - {{mkws-attr "@name"}}{{/mkws-commaList}}\ -
\ -'; - } else if (name === "Summary") { - return '\ -\ - {{md-title}}\ -\ -{{#if md-title-remainder}}\ - {{md-title-remainder}}\ -{{/if}}\ -{{#if md-title-responsibility}}\ - {{md-title-responsibility}}\ -{{/if}}\ -'; - } else if (name === "Image") { - return '\ - \ - {{#mkws-first md-thumburl}}\ - {{../md-title}}\ - {{/mkws-first}}\ -
\ -
\ -'; - } - - var s = "There is no default '" + name +"' template!"; - alert(s); - return s; -}; - - // The following functions are dispatchers for team methods that // are called from the UI using a team-name rather than implicit // context. diff --git a/src/mkws-team.js b/src/mkws-team.js index 25a24d6..d209e43 100644 --- a/src/mkws-team.js +++ b/src/mkws-team.js @@ -388,32 +388,34 @@ function team($, teamName) { function loadTemplate(name) { var template = m_template[name]; - - if (template === undefined) { - // Fall back to generic template if there is no team-specific one + if (template === undefined && Handlebars.compile) { var source; var node = $(".mkwsTemplate_" + name + " .mkwsTeam_" + that.name()); if (node && node.length < 1) { node = $(".mkwsTemplate_" + name); } - if (node) { - source = node.html(); - } - - if (!source) { - source = m_templateText[name]; - } - if (!source) { - source = mkws.defaultTemplate(name); + if (node) source = node.html(); + if (!source) source = m_templateText[name]; + if (source) { + template = Handlebars.compile(source); + log("compiled template '" + name + "'"); } - - template = Handlebars.compile(source); - log("compiled template '" + name + "'"); + } + //if (template === undefined) template = mkws_templatesbyteam[m_teamName][name]; + if (template === undefined && Handlebars.templates) { + template = Handlebars.templates[name]; + } + if (template === undefined && mkws.defaultTemplates) { + template = mkws.defaultTemplates[name]; + } + if (template) { m_template[name] = template; + return template; + } + else { + alert("Missing MKWS template for " + name); + } } - - return template; - } that.loadTemplate = loadTemplate; diff --git a/src/mkws-widget-main.js b/src/mkws-widget-main.js index 3761752..03eba2e 100644 --- a/src/mkws-widget-main.js +++ b/src/mkws-widget-main.js @@ -116,28 +116,25 @@ mkws.registerWidgetType('Records', function() { 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]; that.team.queue("record").publish(hit); - var divId = team.recordElementId(hit.recid[0]); - html.push('
', renderSummary(hit), '
'); + hit.detailLinkId = team.recordElementId(hit.recid[0]); + hit.detailClick = "mkws.showDetails('" + team.name() + "', '" + hit.recid[0] + "');return false;" + hit.containerClass = "mkwsSummary mkwsTeam_" + team.name(); + hit.containerClass += " " + hit.detailLinkId; // ### At some point, we may be able to move the // m_currentRecordId and m_currentRecordData members // from the team object into this widget. if (hit.recid == team.currentRecordId()) { - if (team.currentRecordData()) - html.push(team.renderDetails(team.currentRecordData())); + if (team.currentRecordData()) { + hit.renderedDetails = team.renderDetails(team.currentRecordData()); + console.log(hit.renderedDetails); + } } } - that.node.html(html.join('')); - - function renderSummary(hit) { - var template = team.loadTemplate(that.config.template || "Summary"); - hit._id = team.recordElementId(hit.recid[0]); - hit._onclick = "mkws.showDetails('" + team.name() + "', '" + hit.recid[0] + "');return false;" - return template(hit); - } + var template = team.loadTemplate(that.config.template || "Records"); + that.node.html(template({"hits": data.hits})); }); that.autosearch(); diff --git a/src/mkws-widget-main.templates/Image.handlebars b/src/mkws-widget-main.templates/Image.handlebars new file mode 100644 index 0000000..02bf01d --- /dev/null +++ b/src/mkws-widget-main.templates/Image.handlebars @@ -0,0 +1,6 @@ + + {{#mkws-first md-thumburl}} + {{../md-title}} + {{/mkws-first}} +
+
diff --git a/src/mkws-widget-main.templates/Record.handlebars b/src/mkws-widget-main.templates/Record.handlebars new file mode 100644 index 0000000..f8bf951 --- /dev/null +++ b/src/mkws-widget-main.templates/Record.handlebars @@ -0,0 +1,59 @@ +{{! +Full record display. +}} + + + + + + {{#if md-date}} + + + + + {{/if}} + {{#if md-author}} + + + + + {{/if}} + {{#if md-electronic-url}} + + + + + {{/if}} + {{#mkws-if-any location having="md-subject"}} + + + + + {{/mkws-if-any}} + + + + +
{{mkws-translate "Title"}} + {{md-title}} + {{#if md-title-remainder}} + ({{md-title-remainder}}) + {{/if}} + {{#if md-title-responsibility}} + {{md-title-responsibility}} + {{/if}} +
{{mkws-translate "Date"}}{{md-date}}
{{mkws-translate "Author"}}{{md-author}}
{{mkws-translate "Links"}} + {{#each md-electronic-url}} + Link{{mkws-index1}} + {{/each}} +
{{mkws-translate "Subject"}} + {{#mkws-first location having="md-subject"}} + {{#if md-subject}} + {{#mkws-commaList md-subject}} + {{this}}{{/mkws-commaList}} + {{/if}} + {{/mkws-first}} +
{{mkws-translate "Locations"}} + {{#mkws-commaList location}} + {{mkws-attr "@name"}}{{/mkws-commaList}} +
diff --git a/src/mkws-widget-main.templates/Records.handlebars b/src/mkws-widget-main.templates/Records.handlebars new file mode 100644 index 0000000..03ba9a2 --- /dev/null +++ b/src/mkws-widget-main.templates/Records.handlebars @@ -0,0 +1,26 @@ +{{! +Records from a search. + +hits: + containerClass - class attribute for same + detailLinkId - id for the element triggering detail display + detailClick - a click event handler for details + renderedDetails - active record details rendered from the Record template + md-* - metadata fields passed through from backend +}} +{{#each hits}} +
+ + {{md-title}} + + {{#if md-title-remainder}} + {{md-title-remainder}} + {{/if}} + {{#if md-title-responsibility}} + {{md-title-responsibility}} + {{/if}} + {{#if renderedDetails}} + {{{renderedDetails}}} + {{/if}} +
+{{/each}}