X-Git-Url: http://git.indexdata.com/?a=blobdiff_plain;f=tools%2Fhtdocs%2Fmkws.js;h=22230d024284950b73c58558f94360f174bcb9f5;hb=a01f87567b8ad321d474caf8a03d5602a067b6fd;hp=ee13dba15fa719cd7a6e2ceab197954a466b89f5;hpb=c3390fc0d2188fda0e382a3ee8eca39de2be0a2c;p=mkws-moved-to-github.git diff --git a/tools/htdocs/mkws.js b/tools/htdocs/mkws.js index ee13dba..22230d0 100644 --- a/tools/htdocs/mkws.js +++ b/tools/htdocs/mkws.js @@ -1,22 +1,24 @@ -/* A very simple client that shows a basic usage of the pz2.js -*/ +/*! MKWS, the MasterKey Widget Set. Copyright (C) 2013, Index Data */ "use strict"; // HTML5: disable for debug_level >= 2 // Set up namespace and some state. -var mkws = {}; +var mkws = { + sort: 'relevance', + authenticated: false, + filters: [] +}; -/* - * global config object: mkws_config - * - * Needs to be defined in the HTML header before including this JS file. - * Define empty mkws_config for simple applications that don't define it. - */ +// Define empty mkws_config for simple applications that don't define it. if (!mkws_config) var mkws_config = {}; -// Wrapper for jQuery -(function ($) { +// wrapper for jQuery lib +function _mkws($) { + // if (console && console.log) console.log("run _mkws()"); + + // call this function only once + if (mkws.init) return; mkws.locale_lang = { "de": { @@ -85,7 +87,7 @@ mkws.debug_function = function (string) { } var now = $.now(); - var timestamp = (now - mkws.debug_time.start)/1000 + " (+" + (now - mkws.debug_time.last)/1000 + ") " + var timestamp = ((now - mkws.debug_time.start)/1000).toFixed(3) + " (+" + ((now - mkws.debug_time.last)/1000).toFixed(3) + ") " mkws.debug_time.last = now; // you need to disable use strict at the top of the file!!! @@ -97,11 +99,67 @@ mkws.debug_function = function (string) { console.log(timestamp + string); } var debug = mkws.debug_function; // local alias +debug("start running MKWS"); + + +Handlebars.registerHelper('json', function(obj) { + return $.toJSON(obj); +}); + + +// We need {{attr '@name'}} because Handlebars can't parse {{@name}} +Handlebars.registerHelper('attr', function(attrName) { + return this[attrName]; +}); + + +/* + * Use as follows: {{#if-any NAME1 having="NAME2"}} + * Applicable when NAME1 is the name of an array + * The guarded code runs only if at least one element of the NAME1 + * array has a subelement called NAME2. + */ +Handlebars.registerHelper('if-any', function(items, options) { + var having = options.hash.having; + for (var i in items) { + var item = items[i] + if (!having || item[having]) { + return options.fn(this); + } + } + return ""; +}); + + +Handlebars.registerHelper('first', function(items, options) { + var having = options.hash.having; + for (var i in items) { + var item = items[i] + if (!having || item[having]) { + return options.fn(item); + } + } + return ""; +}); + + +Handlebars.registerHelper('commaList', function(items, options) { + var out = ""; + + for (var i in items) { + if (i > 0) out += ", "; + out += options.fn(items[i]) + } + + return out; +}); + { /* default mkws config */ var config_default = { use_service_proxy: true, + pazpar2_url: "http://mkws.indexdata.com/service-proxy/", service_proxy_auth: "http://mkws.indexdata.com/service-proxy-auth", lang: "", sort_options: [["relevance"], ["title:1", "title"], ["date:0", "newest"], ["date:1", "oldest"]], @@ -135,6 +193,11 @@ var debug = mkws.debug_function; // local alias } } +mkws.sort = mkws_config.sort_default; +debug("copied mkws_config.sort_default '" + mkws_config.sort_default + "' to mkws.sort"); + +mkws.usesessions = mkws_config.use_service_proxy ? false : true; + if (mkws_config.query_width < 5 || mkws_config.query_width > 150) { debug("Reset query width: " + mkws_config.query_width); mkws_config.query_width = 50; @@ -151,19 +214,19 @@ for (var key in mkws_config) { } } - +debug("Create main pz2 object"); // create a parameters array and pass it to the pz2's constructor // then register the form submit event with the pz2.search function // autoInit is set to true on default var my_paz = new pz2( { "onshow": my_onshow, "showtime": 500, //each timer (show, stat, term, bytarget) can be specified this way - "pazpar2path": mkws_config.pazpar2_url || "http://mkws.indexdata.com/service-proxy/", + "pazpar2path": mkws_config.pazpar2_url, "oninit": my_oninit, "onstat": my_onstat, "onterm": my_onterm, "termlist": "xtargets,subject,author", "onbytarget": my_onbytarget, - "usesessions" : mkws_config.use_service_proxy ? false : true, + "usesessions" : mkws.usesessions, "showResponseType": '', // or "json" (for debugging?) "onrecord": my_onrecord } ); @@ -175,8 +238,6 @@ var recPerPage = 20; var totalRec = 0; var curDetRecId = ''; var curDetRecData = null; -var curSort = 'relevance'; -var curFilter = null; var submitted = false; var SourceMax = 16; var SubjectMax = 10; @@ -209,24 +270,29 @@ function my_onshow(data) { var html = []; for (var i = 0; i < data.hits.length; i++) { var hit = data.hits[i]; - html.push('
'+ marker + ' | |
No locations for record! |
Title | \ +\ + {{md-title}}\ + {{#if md-title-remainder}}\ + ({{md-title-remainder}})\ + {{/if}}\ + {{#if md-title-responsibility}}\ + {{md-title-responsibility}}\ + {{/if}}\ + | \ +
---|---|
Date | \ +{{md-date}} | \ +
Author | \ +{{md-author}} | \ +
URL | \ +\
+ {{#each md-electronic-url}}\
+ {{this}} \ + {{/each}}\ + | \
+
Subject | \ +\ + {{#first location having="md-subject"}}\ + {{#if md-subject}}\ + {{md-subject}}\ + {{/if}}\ + {{/first}}\ + | \ +
Locations | \ +\ + {{#commaList location}}\ + {{attr "@name"}}{{/commaList}}\ + | \ +