1 /* ---------------------------------------------------------------------------
2 * Copyright (C) 2010 Equinox Software, Inc
3 * Mike Rylander <miker@esilibrary.com>
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 * ---------------------------------------------------------------------------
19 <div id='facetSidebarContainer' class='hide_me'>
21 <div class="side_bar_item" style="margin-top: 10px; font-weight: bold;">
22 <span>&navigate.facetRefine;</span>
26 dojoType='openils.widget.FacetSidebar'
28 searchSubmit='search_submit'
30 maxValuesPerFacet='10'
31 classOrder='[{"name":"author","facetOrder":["personal","corporate"]},{"name":"subject","facetOrder":["topic"]},"series",{"name":"subject","facetOrder":["name","geographic"]}]'
33 <script type='dojo/method' event='populate'><![CDATA[
35 attachEvt("result", "allRecordsReceived", function () {
36 if(!resultFacetKey) return;
37 if (f_sidebar.facetCacheKey) return; // already rendered it
39 dojo.removeClass('facetSidebarContainer','hide_me');
41 f_sidebar.facetCacheKey = resultFacetKey;
51 if(!dojo._hasResource["openils.widget.FacetSidebar"]) {
53 dojo._hasResource["openils.widget.FacetSidebar"] = true;
54 dojo.provide("openils.widget.FacetSidebar");
55 dojo.require("openils.widget.Searcher");
58 'openils.widget.FacetSidebar',
59 [dijit._Widget, dijit._Templated],
62 templateString : '<div dojoAttachPoint="myTop"><div dojoAttachPoint="containerNode"></div><div class="facetClassContainer" dojoAttachPoint="facetClasses"></div></div>',
63 widgetsInTemplate: false,
68 classOrder : null, // Array of cmc.name values, OR array of objects with name and facetOrder properties
69 displayItemLimit : 999, // Number of distinctly described entries (classes or facets), that have values, to display from classOrder
72 maxValuesPerFacet : 100,
74 startup : function () {
76 this.inherited(arguments)
79 populate : function () {},
81 render : function () {
82 if (!this.facetCacheKey) return;
84 if (openils.widget.Searcher._cache.facetData) {
85 this.facetData = openils.widget.Searcher._cache.facetData;
86 this._render_callback();
88 var limit = dojo.isIE ? this.facetLimit : this.maxValuesPerFacet;
90 fieldmapper.standardRequest(
91 [ 'open-ils.search', 'open-ils.search.facet_cache.retrieve'],
93 params : [this.facetCacheKey, limit],
94 oncomplete : function(r) {
95 var facetData = r.recv().content();
96 if (!facetData) return;
97 self.facetData = openils.widget.Searcher._cache.facetData = facetData;
98 self._render_callback();
106 _render_callback : function(facetData) {
107 var facetData = this.facetData;
108 var classes = openils.widget.Searcher._cache.arr.cmc;
109 if (this.classOrder && this.classOrder.length > 0) {
114 if (dojo.isObject(x)) classes.push(x);
115 else classes.push({name:x});
120 var displayedItems = 0;
125 var possible_facets = [];
127 dojo.forEach(x.facetOrder, function(fname) {
128 var maybe_facet = dojo.filter(
129 openils.widget.Searcher._cache.arr.cmf,
131 if (y.field_class == x.name && y.name == fname && facetData[y.id]) {
132 if (displayedItems < me.displayItemLimit) {
140 if (maybe_facet) possible_facets.push(maybe_facet);
143 possible_facets = dojo.filter(
144 openils.widget.Searcher._cache.arr.cmf,
146 if (y.field_class == x.name && facetData[y.id]) {
147 if (displayedItems < me.displayItemLimit) {
156 if (possible_facets.length > 0) me.addClass( x.name, possible_facets );
161 addClass : function (thisclass, facets) {
162 return new openils.widget.FacetSidebar.facetClass(
163 { facetLimit: this.facetLimit, searchBox : this.searchBox, searchSubmit : this.searchSubmit, facetClass : thisclass, facetData : this.facetData, facetList : facets }
164 ).placeAt(this.facetClasses, 'last');
169 dojo._hasResource["openils.widget.FacetSidebar.facetClass"] = true;
170 dojo.provide("openils.widget.FacetSidebar.facetClass");
173 'openils.widget.FacetSidebar.facetClass',
174 [dijit._Widget, dijit._Templated],
178 '<div class="facetClassLabelContainer">' +
179 ' <div class="facetClassLabel" dojoAttachPoint="facetClassLabel"></div>' +
180 ' <div class="facetFieldContainer" dojoAttachPoint="facetFields"></div>' +
182 widgetsInTemplate: false,
191 postCreate : function () {
192 if (!this.facetClass) return;
193 if (!this.facetData) return;
195 var myclass = this.facetClass;
197 var fclass = dojo.filter(openils.widget.Searcher._cache.arr.cmc, function (x) { if (x.name == myclass) return 1; return 0; })[0];
198 this.facetClassLabel.innerHTML = fclass.label;
203 function (f) { me.addFacets(f); }
207 addFacets : function (f) {
208 return new openils.widget.FacetSidebar.facetField(
209 { facetLimit: this.facetLimit, searchBox : this.searchBox, searchSubmit : this.searchSubmit, facet : f, facetData : this.facetData[f.id] }
210 ).placeAt( this.facetFields, 'last' );
215 dojo._hasResource["openils.widget.FacetSidebar.facetField"] = true;
216 dojo.provide("openils.widget.FacetSidebar.facetField");
219 'openils.widget.FacetSidebar.facetField',
220 [dijit._Widget, dijit._Templated],
224 '<div class="facetField" dojoAttachPoint="myTop">' +
225 ' <div class="extraFacetFieldsWrapper" dojoAttachPoint="toggleExtraFacetFieldsWrapper"><button class="toggleExtraFacetFieldsButton" dojoType="dijit.form.Button" dojoAttachPoint="toggleExtraFacetFields" dojoAttachEvent="onClick:toggleExtraFacets"></button></div>' +
226 ' <div class="facetFieldLabel" dojoAttachPoint="facetLabel"></div>' +
227 ' <div class="facetFields" dojoAttachPoint="facetFields"></div>' +
228 ' <div class="facetFields hide_me" dojoAttachPoint="extraFacetFields"></div>' +
231 widgetsInTemplate: true,
239 postCreate : function () {
240 this.nls = dojo.i18n.getLocalization("openils.widget", "Searcher");
242 var keylist = []; for (var i in this.facetData) { keylist.push(i); }
244 keylist = keylist.sort(function(a,b){
245 if (me.facetData[a] < me.facetData[b]) return 1;
246 if (me.facetData[a] > me.facetData[b]) return -1;
247 if (a < b) return -1;
252 this.facetLabel.innerHTML = this.facet.label;
253 this.toggleExtraFacetFields.setLabel(this.nls.more);
260 var have_it = dojo.byId(me.searchBox).value.indexOf(me.facet.field_class + '|' + me.facet.name + '[' + value + ']') > -1;
262 var container = dojo.create('div',{'class':'facetFieldLine'});
263 dojo.create('span',{'class':'facetFieldLineCount', innerHTML: me.facetData[value]},container);
266 dojo.create('a',{href : '#', 'class':'facetFieldLineValue', onclick : function(){ me.undoSearch(value); return false;}, innerHTML: '<b>(' + value + ')</b>'},container);
268 dojo.create('a',{href : '#', 'class':'facetFieldLineValue', onclick : function(){ me.doSearch(value); return false;}, innerHTML: value},container);
271 if (pos >= me.facetLimit) dojo.place(container,me.extraFacetFields,'last');
272 else dojo.place(container,me.facetFields,'last');
278 if (pos < me.facetLimit + 1) dojo.query(this.toggleExtraFacetFieldsWrapper).toggleClass('hide_me');
282 toggleExtraFacets : function () {
283 dojo.query(this.extraFacetFields).toggleClass('hide_me');
284 this.extraHidden = !this.extraHidden;
285 this.extraHidden ? this.toggleExtraFacetFields.setLabel(this.nls.more) : this.toggleExtraFacetFields.setLabel(this.nls.less);
288 undoSearch : function (value) {
289 var sb = dojo.byId(this.searchBox);
290 sb.value = sb.value.replace(this.facet.field_class + '|' + this.facet.name + '[' + value + ']','');
291 dojo.byId(this.searchSubmit).click();
294 doSearch : function (value) {
295 dojo.byId(this.searchBox).value += ' ' + this.facet.field_class + '|' + this.facet.name + '[' + value + ']';
296 dojo.byId(this.searchSubmit).click();