From: Jakub Skoczen Date: Mon, 2 Apr 2007 15:50:27 +0000 (+0000) Subject: Internal query handling completely rewritten. X-Git-Tag: PAZPAR2.1.0.0~371 X-Git-Url: http://jsfdemo.indexdata.com/cgi-bin?a=commitdiff_plain;h=ee6170fc31c80c61c67cdf7a6fbc42b85593acfd;p=pazpar2-moved-to-github.git Internal query handling completely rewritten. --- diff --git a/www/masterkey/index.html b/www/masterkey/index.html index 9258862..92f9b06 100755 --- a/www/masterkey/index.html +++ b/www/masterkey/index.html @@ -6,6 +6,7 @@ + diff --git a/www/masterkey/js/client.js b/www/masterkey/js/client.js index f70d98c..6a8683d 100644 --- a/www/masterkey/js/client.js +++ b/www/masterkey/js/client.js @@ -1,5 +1,5 @@ /* -** $Id: client.js,v 1.9 2007-04-02 09:44:34 jakub Exp $ +** $Id: client.js,v 1.10 2007-04-02 15:50:27 jakub Exp $ ** MasterKey - pazpar2's javascript client . */ @@ -15,11 +15,12 @@ var my_paz = new pz2( { "onshow": my_onshow, /* some state variable */ var currentSort = 'relevance'; var currentResultsPerPage = 20; -var currentQuery = null; -var currentQueryArr = new Array(); +/*var currentQuery = null; +var currentQueryArr = new Array();*/ var currentPage = 0; -var currentFilter = undefined; -var currentFilterName = null; +var curQuery = new pzQuery(); +/*var currentFilter = undefined;*/ +/*var currentFilterName = null;*/ var currentDetailedId = null; var currentDetailedData = null; @@ -54,12 +55,10 @@ $(document).ready( function() { /* search button event handler */ function onFormSubmitEventHandler() { - if(!loadQueryFromForm()) - return false; + loadQueryFromForm(); + curQuery.clearFilter(); fireSearch(); drawBreadcrumb(); - //hack for now - currentFilter = undefined; $('div.content').show(); $("div.leftbar").show(); return false; @@ -103,7 +102,8 @@ function my_onshow(data) if( author ) { recBody.append(' by '); - $(''+author+'\n').click(function(){ refine(this.name, this.firstChild.nodeValue) }).appendTo(recBody); + $(''+author+'\n').click(function(){ + refine(this.name, this.firstChild.nodeValue) }).appendTo(recBody); } if( currentDetailedId == id ) { @@ -227,8 +227,12 @@ function my_onbytarget(data){} */ function fireSearch() { - my_paz.search(currentQuery, currentResultsPerPage, currentSort, currentFilter); + $('div.showing').empty().text('No records to show.'); + $('div.pages').empty().html(' '); $('div.records').empty(); + if( !curQuery.totalLength() ) + return false; + my_paz.search(curQuery.toCCL(), currentResultsPerPage, currentSort, curQuery.getFilterString() ); } function toggleAdvanced() @@ -243,6 +247,7 @@ function toggleAdvanced() $("div.advanced").show(); advancedOn = true; $("#advanced").text("Simple search"); + loadFormFieldsFromQuery(); } } @@ -274,75 +279,53 @@ function drawDetailedRec(detailBox) function refine(field, value, opt) { - // for the time being - //if(!advancedOn) - // toggleAdvanced(); - switch(field) { - case "author": currentQueryArr.push('au="'+value+'"'); - if(document.search.author.value != '') document.search.author.value+='; '; - document.search.author.value += value; break; - - case "title": currentQueryArr.push('ti="'+value+'"'); - //if(document.search.tile.value != '') document.search.title.value+='; '; - //document.search.title.value += value; break; - - case "date": currentQueryArr.push('date="'+value+'"'); - if(document.search.date.value != '') document.search.date.value+='; '; - document.search.date.value += value; break; - - case "subject": currentQueryArr.push('su="'+value+'"'); - if(document.search.subject.value != '') document.search.subject.value+='; '; - document.search.subject.value += value; break; - - case "xtarget": currentFilter = 'id='+value; - currentFilterName = opt; break; + case "author": curQuery.addTerm('au', value); break; + case "title": curQuery.addTerm('ti', value); break; + case "date": curQuery.addTerm('date', value); break; + case "subject": curQuery.addTerm('su', value); break; + case "xtarget": curQuery.setFilter(opt, value); break; } + if(advancedOn) + loadFormFieldsFromQuery(); + currentPage = 0; - currentQuery = currentQueryArr.join(' and '); drawBreadcrumb(); fireSearch(); } function loadQueryFromForm() { - query = new Array(); - if( document.search.query.value !== '' ) query.push(document.search.query.value); + curQuery.reset(); + curQuery.simpleQuery = document.search.query.value; if( advancedOn ) { - var input; - if( (input = parseField(document.search.author.value, 'au')).length ) query = query.concat(input); - if( (input = parseField(document.search.title.value, 'ti')).length ) query = query.concat(input); - if( (input = parseField(document.search.date.value, 'date')).length ) query = query.concat(input); - if( (input = parseField(document.search.subject.value, 'su')).length ) query = query.concat(input); - } - - if( query.length ) { - currentQueryArr = query; - currentQuery = query.join(" and "); - return true; - } else { - return false; + curQuery.addTermsFromList(document.search.author.value, 'au'); + curQuery.addTermsFromList(document.search.title.value, 'ti'); + curQuery.addTermsFromList(document.search.date.value, 'date'); + curQuery.addTermsFromList(document.search.subject.value, 'su'); } } -function parseField(inputString, field) +function loadFormFieldsFromQuery() { - var inputArr = inputString.split(';'); - var outputArr = new Array(); - for(var i=0; i < inputArr.length; i++){ - if(inputArr[i].length < 3){ - continue; + document.search.author.value = ''; + document.search.title.value = ''; + document.search.date.value = ''; + document.search.subject.value = ''; + + for(var i = 0; i < curQuery.numTerms; i++) + { + switch( curQuery.getTermFieldByIdx(i) ) + { + case "au": document.search.author.value += curQuery.getTermValueByIdx(i) + ';'; break; + case "ti": document.search.title.value += curQuery.getTermValueByIdx(i) + ';'; break; + case "date": document.search.date.value += curQuery.getTermValueByIdx(i) + ';'; break; + case "su": document.search.subject.value += curQuery.getTermValueByIdx(i) + ';'; break; } - outputArr.push(field+'="'+inputArr[i]+'"'); } - //if( outputArr.length ){ - return outputArr;//.join(" and "); - //}else { - // return false; - //} } function drawPager(max, hits) @@ -410,16 +393,19 @@ function drawBreadcrumb() var bc = $("#breadcrumb"); bc.empty(); - if(currentFilter) $(''+currentFilterName+': ').click(function(){ - currentFilter = undefined; currentFilterName = null; refine();}).appendTo(bc); + if(curQuery.filterNums) $(''+curQuery.getFilterName(0)+': ').click(function() { + curQuery.removeFilter(0); + refine(); + }).appendTo(bc); - bc.append(''+currentQueryArr[0]+''); + bc.append(''+curQuery.simpleQuery+''); - for(var i = 1; i < currentQueryArr.length; i++){ + for(var i = 0; i < curQuery.numTerms; i++){ bc.append(' + '); - var bcLink = $(''+ - currentQueryArr[i].substring(currentQueryArr[i].indexOf('"') + 1, currentQueryArr[i].lastIndexOf('"')) - +'').click(function() { currentQueryArr.splice(this.id.split('_')[1], 1);refine(); }); + var bcLink = $(''+curQuery.getTermValueByIdx(i)+'').click(function() { + curQuery.removeTermByIdx(this.id.split('_')[1]); + refine(); + }); bc.append(bcLink); } } diff --git a/www/masterkey/js/pzQuery.js b/www/masterkey/js/pzQuery.js new file mode 100644 index 0000000..fa77eda --- /dev/null +++ b/www/masterkey/js/pzQuery.js @@ -0,0 +1,123 @@ +/* +********************************************************************************* +** QUERY CLASS ****************************************************************** +********************************************************************************* +*/ +var pzQuery = function() +{ + this.simpleQuery = ''; + this.advTerms = new Array(); + this.filterHash = new Array(); + this.numTerms = 0; + this.filterNums = 0; +}; +pzQuery.prototype = { + reset: function() + { + this.simpleQuery = ''; + this.advTerms = new Array(); + this.filterHash = new Array(); + this.numTerms = 0; + }, + addTerm: function(field, value) + { + var term = {"field": field, "value": value}; + this.advTerms[this.numTerms] = term; + this.numTerms++; + }, + getTermValueByIdx: function(index) + { + return this.advTerms[index].value; + }, + getTermFieldByIdx: function(index) + { + return this.advTerms[index].field; + }, + /* semicolon separated list of terms for given field*/ + getTermsByField: function(field) + { + var terms = ''; + for(var i = 0; i < this.advTerms.length; i++) + { + if( this.advTerms[i].field == field ) + terms = terms + this.queryHas[i].value + ';'; + } + return terms; + }, + addTermsFromList: function(inputString, field) + { + var inputArr = inputString.split(';'); + for(var i=0; i < inputArr.length; i++) + { + if(inputArr[i].length < 3) continue; + this.advTerms[this.numTerms] = {"field": field, "value": inputArr[i] }; + this.numTerms++; + } + }, + removeTermByIdx: function(index) + { + this.advTerms.splice(index, 1); + this.numTerms--; + }, + toCCL: function() + { + // TODO escape the characters + var ccl = ''; + if( this.simpleQuery != '') + ccl = '"'+this.simpleQuery+'"'; + for(var i = 0; i < this.advTerms.length; i++) + { + if (ccl != '') ccl = ccl + ' and '; + ccl = ccl + this.advTerms[i].field+'="'+this.advTerms[i].value+'"'; + } + return ccl; + }, + addFilter: function(name, value) + { + var filter = {"name": name, "id": value }; + this.filterHash[this.filterNums] = filter; + this.filterNums++; + }, + setFilter: function(name, value) + { + this.filterHash = new Array(); + this.filterNums = 0; + this.addFilter(name, value); + }, + getFilter: function(index) + { + return this.filterHash[index].id; + }, + getFilterName: function(index) + { + return this.filterHash[index].name; + }, + removeFilter: function(index) + { + this.filterHash.splice(index, 1); + this.filterNums--; + }, + clearFilter: function() + { + this.filterHash = new Array(); + this.filterNums = 0; + }, + getFilterString: function() + { + //temporary + if(!this.filterNums) + return undefined; + var filter = ''; + for(var i = 0; i < this.filterHash.length; i++) + { + if (filter != '') filter = filter + '|'; + filter += 'id='+this.filterHash[i].id; + } + return filter; + }, + totalLength: function() + { + var simpleLength = this.simpleQuery != '' ? 1 : 0; + return this.advTerms.length + simpleLength; + } +}