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 // determine the visible sorting from the
62 // view and update our list of cells to match
63 refreshCells : function() {
64 var cells = this.cells;
69 _this.grid.views.views[0].structure.cells[0],
71 for (var i = 0; i < cells.length; i++) {
72 if (cells[i].field == vCell.field) {
73 cells[i]._visible = true;
74 _this.cells.push(cells[i]);
81 // Depending on how the grid structure is built, there may be
82 // cells in the structure that are not yet in the view. Push
83 // any remaining cells onto the end.
87 existing = _this.cells.filter(function(s){return s.field == cell.field})[0]
89 cell._visible = false;
90 _this.cells.push(cell);
96 buildDialog : function() {
101 var dialog = new dijit.Dialog({title : 'Column Picker'});
102 var table = dojo.create('table', {'class':'oils-generic-table', innerHTML :
103 "<table><thead><tr><th width='30%'>Column</th><th width='23%'>Display</th>" +
104 "<th width='23%'>Auto Width</th><th width='23%'>Sort Priority</th></tr></thead>" +
107 var tDiv = dojo.create('div', {style : 'height:400px; overflow-y:auto;'});
108 tDiv.appendChild(table);
110 var bDiv = dojo.create('div', {style : 'text-align:right; width:100%;',
111 innerHTML : "<span name='cancel_button'></span><span name='save_button'></span>"});
113 var textDiv = dojo.create('div', {style : 'padding:5px; margin-top:5px; border-top:1px solid #333',
115 "<i>A Sort Priority of '0' means no sorting is applied.<br/>" +
116 "<i>Apply a negative Sort Priority for descending sort."});
118 var wrapper = dojo.create('div');
119 wrapper.appendChild(tDiv);
120 wrapper.appendChild(textDiv);
121 wrapper.appendChild(bDiv);
122 dialog.containerNode.appendChild(wrapper);
124 var button = new dijit.form.Button({label:'Save'},
125 dojo.query('[name=save_button]', bDiv)[0]);
126 button.onClick = function() { dialog.hide(); self.update(true); };
128 button = new dijit.form.Button({label:'Cancel'},
129 dojo.query('[name=cancel_button]', bDiv)[0]);
130 button.onClick = function() { dialog.hide(); };
135 // builds the column-picker dialog table
138 var rows = dojo.query('tr', this.dialogTable);
140 for(var i = 0; i < rows.length; i++) {
141 if(rows[i].getAttribute('picker'))
142 this.dialogTable.removeChild(rows[i]);
145 rows = dojo.query('tr', this.dialogTable);
146 var lastChild = null;
148 lastChild = rows[rows.length-1];
150 for(var i = 0; i < this.cells.length; i++) {
151 // setting table.innerHTML breaks stuff, so do it the hard way
152 var cell = this.cells[i];
153 tr = document.createElement('tr');
154 tr.setAttribute('picker', 'picker');
155 td0 = document.createElement('td');
156 td1 = document.createElement('td');
157 td2 = document.createElement('td');
158 td3 = document.createElement('td');
160 ipt = document.createElement('input');
161 ipt.setAttribute('type', 'checkbox');
162 ipt.setAttribute('name', 'selector');
164 ipt2 = document.createElement('input');
165 ipt2.setAttribute('type', 'checkbox');
166 ipt2.setAttribute('name', 'width');
168 ipt3 = document.createElement('div');
170 if (cell.nonSelectable) {
171 ipt.setAttribute('checked', 'checked');
172 ipt.setAttribute('disabled', true);
173 ipt2.setAttribute('disabled', true);
177 ipt.setAttribute('checked', 'checked');
178 if (cell.width == 'auto')
179 ipt2.setAttribute('checked', 'checked');
181 ipt.removeAttribute('checked');
185 if (cell.field == '+selector') {
186 // pick up the unescaped unicode checkmark char
187 td0.innerHTML = cell.name;
189 td0.appendChild(document.createTextNode(cell.name));
191 td1.appendChild(ipt);
192 td2.appendChild(ipt2);
193 td3.appendChild(ipt3);
200 this.dialogTable.insertBefore(tr, lastChild);
202 this.dialogTable.appendChild(tr);
204 if ( this.grid.canSort(i+1) ) { // column index is 1-based
206 // must be added after its parent node is inserted into the DOM.
207 var ns = new dijit.form.NumberSpinner(
208 { constraints : {places : 0},
209 value : cell._sort || 0,
218 // update the grid based on the items selected in the picker dialog
219 update : function(persist) {
220 var rows = dojo.query('[picker=picker]', this.dialogTable);
222 var displayCells = [];
223 var sortUpdated = false;
225 for (var i = 0; i < rows.length; i++) {
227 var selector = dojo.query('[name=selector]', row)[0];
228 var width = dojo.query('[name=width]', row)[0];
229 var sort = dojo.query('[name=sort]', row)[0];
230 var cell = this.cells[i]; // index should match dialog
232 if (sort && cell._sort != sort.value) {
234 cell._sort = sort.value;
237 if (selector.checked) {
238 cell._visible = true;
241 } else if(cell.width == 'auto') {
244 displayCells.push(cell);
247 cell._visible = false;
252 if (sortUpdated && this.onSortChange)
253 this.onSortChange(this.buildSortList());
255 this.structure[0].cells[0] = displayCells;
256 this.grid.setStructure(this.structure);
259 if (persist) this.persist(true);
262 // extract cells that have sorting applied, order lowest to highest
263 buildSortList : function() {
264 var sortList = this.cells.filter(
265 function(cella) { return Number(cella._sort) }
268 if (Math.abs(a._sort) < Math.abs(b._sort)) return -1;
273 return sortList.map(function(f){
274 var dir = f._sort < 0 ? 'desc' : 'asc';
275 return {field : f.field, direction : dir};
279 // save me as a user setting
280 persist : function(noRefresh) {
283 if (!noRefresh) this.refreshCells();
285 for(var i = 0; i < this.cells.length; i++) {
286 var cell = this.cells[i];
288 list.push(cell.field);
289 if(cell.width == 'auto')
290 autos.push(cell.field);
295 setting[this.persistKey] = {
298 'sort' : this.buildSortList().map(function(f){return f.field})
302 fieldmapper.standardRequest(
303 ['open-ils.actor', 'open-ils.actor.patron.settings.update'],
305 params: [this.authtoken, null, setting],
306 oncomplete: function(r) {
307 var stat = openils.Util.readResponse(r);
309 onmethoderror : function() {},
310 onerror : function() {
311 console.log("No user setting '" + _this.persistKey + "' configured. Cannot persist")
317 loadColsFromSetting : function(setting) {
319 this.setting = setting;
320 var displayCells = [];
322 // new component, existing settings may not have this
323 if (!setting.sort) setting.sort = [];
325 dojo.forEach(setting.columns,
327 var cell = _this.cells.filter(function(c){return c.field == col})[0];
329 cell._visible = true;
330 displayCells.push(cell);
332 if(setting.auto.indexOf(cell.field) > -1) {
335 if(cell.width == 'auto')
338 cell._sort = setting.sort.indexOf(cell.field) + 1;
341 console.log('Unknown setting column '+col+'. Ignoring...');
346 // any cells not in the setting must be marked as non-visible
347 dojo.forEach(this.cells, function(cell) {
348 if (setting.columns.indexOf(cell.field) == -1) {
349 cell._visible = false;
354 this.structure[0].cells[0] = displayCells;
355 this.grid.setStructure(this.structure);
362 // if load is called before the user has logged in,
363 // queue the loading up for after authentication.
364 if (!this.authtoken) {
366 openils.Util.addOnLoad(function() {
367 _this.authtoken = openils.User.authtoken;
374 this.loadColsFromSetting(this.setting);
375 this._onLoad({sortFields : this.buildSortList()});
379 fieldmapper.standardRequest(
380 ['open-ils.actor', 'open-ils.actor.patron.settings.retrieve'],
382 params: [this.authtoken, null, this.persistKey],
383 oncomplete: function(r) {
384 var set = openils.Util.readResponse(r);
386 _this.loadColsFromSetting(set);
388 _this.grid.setStructure(_this.structure);
391 _this._onLoad({sortFields : _this.buildSortList()});