Initial templatification + Records widget
authorJason Skomorowski <jason@indexdata.com>
Mon, 9 Jun 2014 14:01:02 +0000 (10:01 -0400)
committerJason Skomorowski <jason@indexdata.com>
Mon, 9 Jun 2014 14:57:37 +0000 (10:57 -0400)
examples/htdocs/two-teams.html
src/Makefile
src/mkws-core.js
src/mkws-team.js
src/mkws-widget-main.js
src/mkws-widget-main.templates/Image.handlebars [new file with mode: 0644]
src/mkws-widget-main.templates/Record.handlebars [new file with mode: 0644]
src/mkws-widget-main.templates/Records.handlebars [new file with mode: 0644]

index 006f11b..694522e 100644 (file)
@@ -9,7 +9,7 @@
     <script type="text/javascript" src="http://mkws.indexdata.com/jquery.json-2.4.js"></script>
     <script type="text/javascript" src="http://mkws.indexdata.com/handlebars-v1.1.2.js"></script>
     <script type="text/javascript" src="http://mkws.indexdata.com/pazpar2/js/pz2.js"></script>
-    <script type="text/javascript" src="http://mkws.indexdata.com/mkws.js"></script>
+    <script type="text/javascript" src="http://mkws.local/src/mkws.js"></script>
   </head>
   <body>
     <table width="100%" border="1">
index 82cf254..8e8e48d 100644 (file)
@@ -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
 
index 7091291..7b776ab 100644 (file)
@@ -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 '\
-<table>\
-  <tr>\
-    <th>{{mkws-translate "Title"}}</th>\
-    <td>\
-      {{md-title}}\
-      {{#if md-title-remainder}}\
-        ({{md-title-remainder}})\
-      {{/if}}\
-      {{#if md-title-responsibility}}\
-        <i>{{md-title-responsibility}}</i>\
-      {{/if}}\
-    </td>\
-  </tr>\
-  {{#if md-date}}\
-  <tr>\
-    <th>{{mkws-translate "Date"}}</th>\
-    <td>{{md-date}}</td>\
-  </tr>\
-  {{/if}}\
-  {{#if md-author}}\
-  <tr>\
-    <th>{{mkws-translate "Author"}}</th>\
-    <td>{{md-author}}</td>\
-  </tr>\
-  {{/if}}\
-  {{#if md-electronic-url}}\
-  <tr>\
-    <th>{{mkws-translate "Links"}}</th>\
-    <td>\
-      {{#each md-electronic-url}}\
-        <a href="{{this}}">Link{{mkws-index1}}</a>\
-      {{/each}}\
-    </td>\
-  </tr>\
-  {{/if}}\
-  {{#mkws-if-any location having="md-subject"}}\
-  <tr>\
-    <th>{{mkws-translate "Subject"}}</th>\
-    <td>\
-      {{#mkws-first location having="md-subject"}}\
-        {{#if md-subject}}\
-          {{#mkws-commaList md-subject}}\
-            {{this}}{{/mkws-commaList}}\
-        {{/if}}\
-      {{/mkws-first}}\
-    </td>\
-  </tr>\
-  {{/mkws-if-any}}\
-  <tr>\
-    <th>{{mkws-translate "Locations"}}</th>\
-    <td>\
-      {{#mkws-commaList location}}\
-        {{mkws-attr "@name"}}{{/mkws-commaList}}\
-    </td>\
-  </tr>\
-</table>\
-';
-  } else if (name === "Summary") {
-    return '\
-<a href="#" id="{{_id}}" onclick="{{_onclick}}">\
-  <b>{{md-title}}</b>\
-</a>\
-{{#if md-title-remainder}}\
-  <span>{{md-title-remainder}}</span>\
-{{/if}}\
-{{#if md-title-responsibility}}\
-  <span><i>{{md-title-responsibility}}</i></span>\
-{{/if}}\
-';
-  } else if (name === "Image") {
-    return '\
-      <a href="#" id="{{_id}}" onclick="{{_onclick}}">\
-        {{#mkws-first md-thumburl}}\
-          <img src="{{this}}" alt="{{../md-title}}"/>\
-        {{/mkws-first}}\
-        <br/>\
-      </a>\
-';
-  }
-
-  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.
index 25a24d6..d209e43 100644 (file)
@@ -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;
 
 
index 3761752..03eba2e 100644 (file)
@@ -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('<div class="mkwsSummary mkwsTeam_' + team.name() + ' ' + divId + '">', renderSummary(hit), '</div>');
+      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 (file)
index 0000000..02bf01d
--- /dev/null
@@ -0,0 +1,6 @@
+      <a href="#" id="{{_id}}" onclick="{{_onclick}}">
+        {{#mkws-first md-thumburl}}
+          <img src="{{this}}" alt="{{../md-title}}"/>
+        {{/mkws-first}}
+        <br/>
+      </a>
diff --git a/src/mkws-widget-main.templates/Record.handlebars b/src/mkws-widget-main.templates/Record.handlebars
new file mode 100644 (file)
index 0000000..f8bf951
--- /dev/null
@@ -0,0 +1,59 @@
+{{!
+Full record display.
+}}
+<table>
+  <tr>
+    <th>{{mkws-translate "Title"}}</th>
+    <td>
+      {{md-title}}
+      {{#if md-title-remainder}}
+        ({{md-title-remainder}})
+      {{/if}}
+      {{#if md-title-responsibility}}
+        <i>{{md-title-responsibility}}</i>
+      {{/if}}
+    </td>
+  </tr>
+  {{#if md-date}}
+  <tr>
+    <th>{{mkws-translate "Date"}}</th>
+    <td>{{md-date}}</td>
+  </tr>
+  {{/if}}
+  {{#if md-author}}
+  <tr>
+    <th>{{mkws-translate "Author"}}</th>
+    <td>{{md-author}}</td>
+  </tr>
+  {{/if}}
+  {{#if md-electronic-url}}
+  <tr>
+    <th>{{mkws-translate "Links"}}</th>
+    <td>
+      {{#each md-electronic-url}}
+        <a href="{{this}}">Link{{mkws-index1}}</a>
+      {{/each}}
+    </td>
+  </tr>
+  {{/if}}
+  {{#mkws-if-any location having="md-subject"}}
+  <tr>
+    <th>{{mkws-translate "Subject"}}</th>
+    <td>
+      {{#mkws-first location having="md-subject"}}
+        {{#if md-subject}}
+          {{#mkws-commaList md-subject}}
+            {{this}}{{/mkws-commaList}}
+        {{/if}}
+      {{/mkws-first}}
+    </td>
+  </tr>
+  {{/mkws-if-any}}
+  <tr>
+    <th>{{mkws-translate "Locations"}}</th>
+    <td>
+      {{#mkws-commaList location}}
+        {{mkws-attr "@name"}}{{/mkws-commaList}}
+    </td>
+  </tr>
+</table>
diff --git a/src/mkws-widget-main.templates/Records.handlebars b/src/mkws-widget-main.templates/Records.handlebars
new file mode 100644 (file)
index 0000000..03ba9a2
--- /dev/null
@@ -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}}
+  <div class="{{containerClass}}">
+    <a href="#" id="{{detailLinkId}}" onclick="{{detailClick}}">
+      <b>{{md-title}}</b>
+    </a>
+    {{#if md-title-remainder}}
+      <span>{{md-title-remainder}}</span>
+    {{/if}}
+    {{#if md-title-responsibility}}
+      <span><i>{{md-title-responsibility}}</i></span>
+    {{/if}}
+    {{#if renderedDetails}}
+      {{{renderedDetails}}}
+    {{/if}}
+  </div>
+{{/each}}