1 /* ---------------------------------------------------------------------------
2 * Copyright (C) 2008 Georgia Public Library Service
3 * Bill Erickson <erickson@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 * ---------------------------------------------------------------------------
18 if(!dojo._hasResource["openils.widget.GridColumnPicker"]) {
19 dojo.provide('openils.widget.GridColumnPicker');
21 dojo.require('dijit.Dialog');
22 dojo.require('dijit.form.Button');
23 dojo.require('dijit.form.NumberSpinner');
24 dojo.require('openils.User');
25 dojo.require('openils.Event');
26 dojo.require('openils.Util');
27 dojo.require('fieldmapper.Fieldmapper');
30 dojo.declare('openils.widget.GridColumnPicker', null, {
32 USER_PERSIST_SETTING : 'ui.grid_columns',
34 constructor : function (authtoken, persistKey, grid, structure) {
37 this.persistKey = this.USER_PERSIST_SETTING+'.'+persistKey;
38 this.authtoken = authtoken || openils.User.authtoken;
39 this.structure = structure || this.grid.structure;
40 this.cells = this.structure[0].cells[0].slice();
42 this.dialog = this.buildDialog();
43 this.dialogTable = this.dialog.containerNode.getElementsByTagName('tbody')[0];
45 // replace: called after any sort changes
46 this.onSortChange = function(list) {console.log('onSortChange()')}
47 // replace: called after user settings are first retrieved
48 this.onLoad = function(opts) {console.log('onLoad()')};
50 // internal onload handler
52 this._onLoad = function(opts) {_this.loaded = true; _this.onLoad(opts)};
54 this.grid.onHeaderContextMenu = function(e) {
61 /** Loads any grid column label changes, clears any
62 * non-visible fields from the structure, and passes
63 * the structure back to the grid to force a UI refresh.
65 reloadStructure : function() {
67 // update our copy of the column labels
70 this.grid.structure[0].cells[0],
72 var cell = _this.cells.filter(
73 function(c) { return c.field == gcell.field }
75 cell.name = gcell.name;
79 this.pruneInvisibleFields();
80 this.grid.setStructure(this.structure);
84 // determine the visible sorting from the
85 // view and update our list of cells to match
86 refreshCells : function() {
87 var cells = this.cells;
92 _this.grid.views.views[0].structure.cells[0],
94 for (var i = 0; i < cells.length; i++) {
95 if (cells[i].field == vCell.field) {
96 cells[i]._visible = true;
97 _this.cells.push(cells[i]);
104 // Depending on how the grid structure is built, there may be
105 // cells in the structure that are not yet in the view. Push
106 // any remaining cells onto the end.
110 existing = _this.cells.filter(function(s){return s.field == cell.field})[0]
112 cell._visible = false;
113 _this.cells.push(cell);
119 buildDialog : function() {
124 var dialog = new dijit.Dialog({title : 'Column Picker'});
125 var table = dojo.create('table', {'class':'oils-generic-table', innerHTML :
126 "<table><thead><tr><th width='30%'>Column</th><th width='23%'>Display</th>" +
127 "<th width='23%'>Auto Width</th><th width='23%'>Sort Priority</th></tr></thead>" +
130 var tDiv = dojo.create('div');
131 tDiv.appendChild(table);
133 var bDiv = dojo.create('div', {style : 'text-align:right; width:100%;',
134 innerHTML : "<span name='cancel_button'></span><span name='save_button'></span>"});
136 var textDiv = dojo.create('div', {style : 'padding:5px; margin-top:5px; border-top:1px solid #333',
138 "<i>A Sort Priority of '0' means no sorting is applied.<br/>" +
139 "<i>Apply a negative Sort Priority for descending sort."});
141 var wrapper = dojo.create('div');
142 wrapper.appendChild(tDiv);
143 wrapper.appendChild(textDiv);
144 wrapper.appendChild(bDiv);
145 dialog.containerNode.appendChild(wrapper);
147 var button = new dijit.form.Button({label:'Save'},
148 dojo.query('[name=save_button]', bDiv)[0]);
149 button.onClick = function() { dialog.hide(); self.update(true); };
151 button = new dijit.form.Button({label:'Cancel'},
152 dojo.query('[name=cancel_button]', bDiv)[0]);
153 button.onClick = function() { dialog.hide(); };
158 // builds the column-picker dialog table
161 var rows = dojo.query('tr', this.dialogTable);
163 for(var i = 0; i < rows.length; i++) {
164 if(rows[i].getAttribute('picker'))
165 this.dialogTable.removeChild(rows[i]);
168 rows = dojo.query('tr', this.dialogTable);
169 var lastChild = null;
171 lastChild = rows[rows.length-1];
173 for(var i = 0; i < this.cells.length; i++) {
174 // setting table.innerHTML breaks stuff, so do it the hard way
175 var cell = this.cells[i];
176 tr = document.createElement('tr');
177 tr.setAttribute('picker', 'picker');
178 td0 = document.createElement('td');
179 td1 = document.createElement('td');
180 td2 = document.createElement('td');
181 td3 = document.createElement('td');
183 ipt = document.createElement('input');
184 ipt.setAttribute('type', 'checkbox');
185 ipt.setAttribute('name', 'selector');
187 ipt2 = document.createElement('input');
188 ipt2.setAttribute('type', 'checkbox');
189 ipt2.setAttribute('name', 'width');
191 ipt3 = document.createElement('div');
193 if (cell.nonSelectable) {
194 ipt.setAttribute('checked', 'checked');
195 ipt.setAttribute('disabled', true);
196 ipt2.setAttribute('disabled', true);
200 ipt.setAttribute('checked', 'checked');
201 if (cell.width == 'auto')
202 ipt2.setAttribute('checked', 'checked');
204 ipt.removeAttribute('checked');
208 if (cell.field == '+selector') {
209 // pick up the unescaped unicode checkmark char
210 td0.innerHTML = cell.name;
212 td0.appendChild(document.createTextNode(cell.name));
214 td1.appendChild(ipt);
215 td2.appendChild(ipt2);
216 td3.appendChild(ipt3);
223 this.dialogTable.insertBefore(tr, lastChild);
225 this.dialogTable.appendChild(tr);
227 if (this.grid.canSort(
228 i + 1, /* column index is 1-based */
229 true /* skip structure test (API abuse) */
232 /* Ugly kludge. When using with FlattenerGrid the
233 * conditional is needed. Shouldn't hurt usage with
235 if (typeof cell.fsort == "undefined" || cell.fsort) {
236 // must be added after its parent node is inserted into the DOM.
237 var ns = new dijit.form.NumberSpinner(
238 { constraints : {places : 0},
239 value : cell._sort || 0,
249 // update the grid based on the items selected in the picker dialog
250 update : function(persist) {
251 var rows = dojo.query('[picker=picker]', this.dialogTable);
253 var displayCells = [];
254 var sortUpdated = false;
256 for (var i = 0; i < rows.length; i++) {
258 var selector = dojo.query('[name=selector]', row)[0];
259 var width = dojo.query('[name=width]', row)[0];
260 var sort = dojo.query('[name=sort]', row)[0];
261 var cell = this.cells[i]; // index should match dialog
263 if (sort && cell._sort != sort.value) {
265 cell._sort = sort.value;
268 if (selector.checked) {
269 cell._visible = true;
272 } else if(cell.width == 'auto') {
275 displayCells.push(cell);
278 cell._visible = false;
283 if (sortUpdated && this.onSortChange)
284 this.onSortChange(this.buildSortList());
286 this.structure[0].cells[0] = displayCells;
287 this.grid.setStructure(this.structure);
290 if (persist) this.persist(true);
293 // extract cells that have sorting applied, order lowest to highest
294 buildSortList : function() {
295 var sortList = this.cells.filter(
296 function(cella) { return Number(cella._sort) }
299 if (Math.abs(a._sort) < Math.abs(b._sort)) return -1;
304 return sortList.map(function(f){
305 var dir = f._sort < 0 ? 'desc' : 'asc';
306 return {field : f.field, direction : dir};
310 // save me as a user setting
311 persist : function(noRefresh) {
314 if (!noRefresh) this.refreshCells();
316 for(var i = 0; i < this.cells.length; i++) {
317 var cell = this.cells[i];
319 list.push(cell.field);
320 if(cell.width == 'auto')
321 autos.push(cell.field);
326 setting[this.persistKey] = {
329 'sort' : this.buildSortList().map(function(f){return f.field})
333 fieldmapper.standardRequest(
334 ['open-ils.actor', 'open-ils.actor.patron.settings.update'],
336 params: [this.authtoken, null, setting],
337 oncomplete: function(r) {
338 var stat = openils.Util.readResponse(r);
340 onmethoderror : function() {},
341 onerror : function() {
342 console.log("No user setting '" + _this.persistKey + "' configured. Cannot persist")
348 loadColsFromSetting : function(setting) {
350 this.setting = setting;
351 var displayCells = [];
353 // new component, existing settings may not have this
354 if (!setting.sort) setting.sort = [];
356 dojo.forEach(setting.columns,
358 var cell = _this.cells.filter(function(c){return c.field == col})[0];
360 cell._visible = true;
361 displayCells.push(cell);
363 if(setting.auto.indexOf(cell.field) > -1) {
366 if(cell.width == 'auto')
369 cell._sort = setting.sort.indexOf(cell.field) + 1;
372 console.log('Unknown setting column '+col+'. Ignoring...');
377 // any cells not in the setting must be marked as non-visible
378 dojo.forEach(this.cells, function(cell) {
379 if (setting.columns.indexOf(cell.field) == -1) {
380 cell._visible = false;
385 this.structure[0].cells[0] = displayCells;
386 this.grid.setStructure(this.structure);
390 // *only* call this when no usr setting tells us what columns
391 // are visible or not.
392 pruneInvisibleFields : function() {
393 this.structure[0].cells[0] = dojo.filter(
394 this.structure[0].cells[0],
395 dojo.hitch(this, function(c) {
396 // keep true or undef, lose false
397 return typeof c._visible == "undefined" || c._visible;
405 // if load is called before the user has logged in,
406 // queue the loading up for after authentication.
407 if (!this.authtoken) {
409 openils.Util.addOnLoad(function() {
410 _this.authtoken = openils.User.authtoken;
417 this.loadColsFromSetting(this.setting);
418 this._onLoad({sortFields : this.buildSortList()});
422 fieldmapper.standardRequest(
423 ['open-ils.actor', 'open-ils.actor.patron.settings.retrieve'],
425 params: [this.authtoken, null, this.persistKey],
426 oncomplete: function(r) {
427 var set = openils.Util.readResponse(r);
429 _this.loadColsFromSetting(set);
431 _this.grid.setStructure(_this.structure);
434 _this._onLoad({sortFields : _this.buildSortList()});