From 653ac0d15395968cc3bbe791dde29025f9bac6f4 Mon Sep 17 00:00:00 2001 From: miker <miker@dcc99617-32d9-48b4-a31d-7c20da2025e4> Date: Mon, 3 May 2010 01:01:29 +0000 Subject: [PATCH] sub-object limiting (think: holdings); query limit/offset; new "templated" mode using opac/template-value elements and dojo.string.substitute; org depth support in constructed tag URIs git-svn-id: svn://svn.open-ils.org/ILS/trunk@16371 dcc99617-32d9-48b4-a31d-7c20da2025e4 --- Open-ILS/web/js/dojo/openils/BibTemplate.js | 80 +++++++++++++++++++++++------ 1 file changed, 65 insertions(+), 15 deletions(-) diff --git a/Open-ILS/web/js/dojo/openils/BibTemplate.js b/Open-ILS/web/js/dojo/openils/BibTemplate.js index f8be3653d4..e5958485d6 100644 --- a/Open-ILS/web/js/dojo/openils/BibTemplate.js +++ b/Open-ILS/web/js/dojo/openils/BibTemplate.js @@ -16,16 +16,22 @@ if(!dojo._hasResource["openils.BibTemplate"]) { + dojo.require('DojoSRF'); dojo.require('dojox.data.dom'); + dojo.require('dojo.string'); dojo._hasResource["openils.BibTemplate"] = true; dojo.provide("openils.BibTemplate"); dojo.declare('openils.BibTemplate', null, { constructor : function(kwargs) { this.root = kwargs.root; + this.subObjectLimit = kwargs.subObjectLimit; + this.subObjectOffset = kwargs.subObjectOffset; + this.tagURI = kwargs.tagURI; this.record = kwargs.record; this.org_unit = kwargs.org_unit || '-'; - this.locale = kwargs.locale || 'en-US'; + this.depth = kwargs.depth; + this.locale = kwargs.locale || OpenSRF.locale || 'en-US'; this.mode = 'biblio-record_entry'; this.default_datatype = 'marcxml-uris'; @@ -46,39 +52,83 @@ if(!dojo._hasResource["openils.BibTemplate"]) { // with bib records that have hundreds or thousands of copies var current_datatype = default_datatype; - if (s.getAttribute('type').indexOf('+') > -1) + if (s.getAttribute('datatype')) { + current_datatype = s.getAttribute('datatype'); + } else if (s.getAttribute('type').indexOf('+') > -1) { current_datatype = s.getAttribute('type').split('+').reverse()[0]; + } if (!slots[current_datatype]) slots[current_datatype] = []; slots[current_datatype].push(s); + }); for (var datatype in slots) { - (function (slot_list,dtype,mode,rec,org) { + //(function (slot_list,dtype,mode,rec,org) { + (function (args) { + var BT = args.renderer; + + var uri = ''; + if (BT.tagURI) { + uri = BT.tagURI; + } else { + uri = 'tag:evergreen-opac:' + BT.mode + '/' + BT.record; + if (BT.subObjectLimit) { + uri += '[' + BT.subObjectLimit; + if (BT.subObjectOffset) + uri += ',' + BT.subObjectOffset; + uri += ']'; + } + uri += '/' + BT.org_unit; + if (BT.depth || BT.depth == '0') uri += '/' + BT.depth; + } dojo.xhrGet({ - url: '/opac/extras/unapi?id=tag:opac:' + mode + '/' + rec + '/' + org + '&format=' + dtype + '&locale=' + this.locale, + url: '/opac/extras/unapi?id=' + uri + '&format=' + args.dtype + '&locale=' + BT.locale, handleAs: 'xml', load: function (bib) { - dojo.forEach(slot_list, function (slot) { + dojo.forEach(args.slot_list, function (slot) { var joiner = slot.getAttribute('join') || ' '; + var item_limit = slot.getAttribute('limit'); + var item_offset = slot.getAttribute('offset') || 0; - var slot_handler = dojo.map( - dojo.query( '*[type=opac/slot-format]', slot ).orphan(), // IE, I really REALLY hate you - function(x){ return dojox.data.dom.textContent(x) || x.innerHTML } - ).join(''); - - if (slot_handler) slot_handler = new Function('item', slot_handler); - else slot_handler = new Function('item','return dojox.data.dom.textContent(item);'); - var item_list = dojo.query( slot.getAttribute('query'), bib ); - if (item_list.length) slot.innerHTML = dojo.map(item_list, slot_handler).join(joiner); + if (item_limit) item_list = item_list.splice(parseInt(item_offset),parseInt(item_limit)); + if (!item_list.length) return; + + var templated = slot.getAttribute('templated') == 'true'; + if (templated) { + var template_values = {}; + + dojo.query( + '*[type=opac/template-value]', + slot + ).orphan().forEach(function(x) { + dojo.setObject( + x.getAttribute('name'), + (new Function( 'item_list', 'BT', 'slotXML', unescape(x.innerHTML) ))(item_list,BT,bib), + template_values + ); + }); + + slot.innerHTML = dojo.string.substitute( unescape(slot.innerHTML), template_values ); + } + + var handler_node = dojo.query( '*[type=opac/slot-format]', slot )[0]; + if (handler_node) slot_handler = new Function('item_list', 'BT', 'slotXML', 'item', dojox.data.dom.textContent(handler_node) || handler_node.innerHTML); + else slot_handler = new Function('item_list', 'BT', 'slotXML', 'item','return dojox.data.dom.textContent(item) || item.innerHTML;'); + + if (item_list.length) { + var content = dojo.map(item_list, dojo.partial(slot_handler,item_list,BT,bib)).join(joiner); + if (templated) handler_node.parentNode.replaceChild( dojo.doc.createTextNode( content ), handler_node ); + else slot.innerHTML = content; + } delete(slot_handler); @@ -86,7 +136,7 @@ if(!dojo._hasResource["openils.BibTemplate"]) { } }); - })(slots[datatype],datatype,this.mode,this.record,this.org_unit); + })({ slot_list : slots[datatype].reverse(), dtype : datatype, renderer : this }); } -- 2.11.0