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 the current grid structure and passes the
62 * structure back to the grid to force a UI refresh.
63 * This is necessary if external forces alter the structure.
65 reloadStructure : function() {
66 this.structure = this.grid.structure;
67 this.cells = this.structure[0].cells[0].slice();
68 this.grid.setStructure(this.structure);
71 // determine the visible sorting from the
72 // view and update our list of cells to match
73 refreshCells : function() {
74 var cells = this.cells;
79 _this.grid.views.views[0].structure.cells[0],
81 for (var i = 0; i < cells.length; i++) {
82 if (cells[i].field == vCell.field) {
83 cells[i]._visible = true;
84 _this.cells.push(cells[i]);
91 // Depending on how the grid structure is built, there may be
92 // cells in the structure that are not yet in the view. Push
93 // any remaining cells onto the end.
97 existing = _this.cells.filter(function(s){return s.field == cell.field})[0]
99 cell._visible = false;
100 _this.cells.push(cell);
106 buildDialog : function() {
111 var dialog = new dijit.Dialog({title : 'Column Picker'});
112 var table = dojo.create('table', {'class':'oils-generic-table', innerHTML :
113 "<table><thead><tr><th width='30%'>Column</th><th width='23%'>Display</th>" +
114 "<th width='23%'>Auto Width</th><th width='23%'>Sort Priority</th></tr></thead>" +
117 var tDiv = dojo.create('div', {style : 'height:400px; overflow-y:auto;'});
118 tDiv.appendChild(table);
120 var bDiv = dojo.create('div', {style : 'text-align:right; width:100%;',
121 innerHTML : "<span name='cancel_button'></span><span name='save_button'></span>"});
123 var textDiv = dojo.create('div', {style : 'padding:5px; margin-top:5px; border-top:1px solid #333',
125 "<i>A Sort Priority of '0' means no sorting is applied.<br/>" +
126 "<i>Apply a negative Sort Priority for descending sort."});
128 var wrapper = dojo.create('div');
129 wrapper.appendChild(tDiv);
130 wrapper.appendChild(textDiv);
131 wrapper.appendChild(bDiv);
132 dialog.containerNode.appendChild(wrapper);
134 var button = new dijit.form.Button({label:'Save'},
135 dojo.query('[name=save_button]', bDiv)[0]);
136 button.onClick = function() { dialog.hide(); self.update(true); };
138 button = new dijit.form.Button({label:'Cancel'},
139 dojo.query('[name=cancel_button]', bDiv)[0]);
140 button.onClick = function() { dialog.hide(); };
145 // builds the column-picker dialog table
148 var rows = dojo.query('tr', this.dialogTable);
150 for(var i = 0; i < rows.length; i++) {
151 if(rows[i].getAttribute('picker'))
152 this.dialogTable.removeChild(rows[i]);
155 rows = dojo.query('tr', this.dialogTable);
156 var lastChild = null;
158 lastChild = rows[rows.length-1];
160 for(var i = 0; i < this.cells.length; i++) {
161 // setting table.innerHTML breaks stuff, so do it the hard way
162 var cell = this.cells[i];
163 tr = document.createElement('tr');
164 tr.setAttribute('picker', 'picker');
165 td0 = document.createElement('td');
166 td1 = document.createElement('td');
167 td2 = document.createElement('td');
168 td3 = document.createElement('td');
170 ipt = document.createElement('input');
171 ipt.setAttribute('type', 'checkbox');
172 ipt.setAttribute('name', 'selector');
174 ipt2 = document.createElement('input');
175 ipt2.setAttribute('type', 'checkbox');
176 ipt2.setAttribute('name', 'width');
178 ipt3 = document.createElement('div');
180 if (cell.nonSelectable) {
181 ipt.setAttribute('checked', 'checked');
182 ipt.setAttribute('disabled', true);
183 ipt2.setAttribute('disabled', true);
187 ipt.setAttribute('checked', 'checked');
188 if (cell.width == 'auto')
189 ipt2.setAttribute('checked', 'checked');
191 ipt.removeAttribute('checked');
195 if (cell.field == '+selector') {
196 // pick up the unescaped unicode checkmark char
197 td0.innerHTML = cell.name;
199 td0.appendChild(document.createTextNode(cell.name));
201 td1.appendChild(ipt);
202 td2.appendChild(ipt2);
203 td3.appendChild(ipt3);
210 this.dialogTable.insertBefore(tr, lastChild);
212 this.dialogTable.appendChild(tr);
214 if ( this.grid.canSort(i+1) ) { // column index is 1-based
216 // must be added after its parent node is inserted into the DOM.
217 var ns = new dijit.form.NumberSpinner(
218 { constraints : {places : 0},
219 value : cell._sort || 0,
228 // update the grid based on the items selected in the picker dialog
229 update : function(persist) {
230 var rows = dojo.query('[picker=picker]', this.dialogTable);
232 var displayCells = [];
233 var sortUpdated = false;
235 for (var i = 0; i < rows.length; i++) {
237 var selector = dojo.query('[name=selector]', row)[0];
238 var width = dojo.query('[name=width]', row)[0];
239 var sort = dojo.query('[name=sort]', row)[0];
240 var cell = this.cells[i]; // index should match dialog
242 if (sort && cell._sort != sort.value) {
244 cell._sort = sort.value;
247 if (selector.checked) {
248 cell._visible = true;
251 } else if(cell.width == 'auto') {
254 displayCells.push(cell);
257 cell._visible = false;
262 if (sortUpdated && this.onSortChange)
263 this.onSortChange(this.buildSortList());
265 this.structure[0].cells[0] = displayCells;
266 this.grid.setStructure(this.structure);
269 if (persist) this.persist(true);
272 // extract cells that have sorting applied, order lowest to highest
273 buildSortList : function() {
274 var sortList = this.cells.filter(
275 function(cella) { return Number(cella._sort) }
278 if (Math.abs(a._sort) < Math.abs(b._sort)) return -1;
283 return sortList.map(function(f){
284 var dir = f._sort < 0 ? 'desc' : 'asc';
285 return {field : f.field, direction : dir};
289 // save me as a user setting
290 persist : function(noRefresh) {
293 if (!noRefresh) this.refreshCells();
295 for(var i = 0; i < this.cells.length; i++) {
296 var cell = this.cells[i];
298 list.push(cell.field);
299 if(cell.width == 'auto')
300 autos.push(cell.field);
305 setting[this.persistKey] = {
308 'sort' : this.buildSortList().map(function(f){return f.field})
312 fieldmapper.standardRequest(
313 ['open-ils.actor', 'open-ils.actor.patron.settings.update'],
315 params: [this.authtoken, null, setting],
316 oncomplete: function(r) {
317 var stat = openils.Util.readResponse(r);
319 onmethoderror : function() {},
320 onerror : function() {
321 console.log("No user setting '" + _this.persistKey + "' configured. Cannot persist")
327 loadColsFromSetting : function(setting) {
329 this.setting = setting;
330 var displayCells = [];
332 // new component, existing settings may not have this
333 if (!setting.sort) setting.sort = [];
335 dojo.forEach(setting.columns,
337 var cell = _this.cells.filter(function(c){return c.field == col})[0];
339 cell._visible = true;
340 displayCells.push(cell);
342 if(setting.auto.indexOf(cell.field) > -1) {
345 if(cell.width == 'auto')
348 cell._sort = setting.sort.indexOf(cell.field) + 1;
351 console.log('Unknown setting column '+col+'. Ignoring...');
356 // any cells not in the setting must be marked as non-visible
357 dojo.forEach(this.cells, function(cell) {
358 if (setting.columns.indexOf(cell.field) == -1) {
359 cell._visible = false;
364 this.structure[0].cells[0] = displayCells;
365 this.grid.setStructure(this.structure);
372 // if load is called before the user has logged in,
373 // queue the loading up for after authentication.
374 if (!this.authtoken) {
376 openils.Util.addOnLoad(function() {
377 _this.authtoken = openils.User.authtoken;
384 this.loadColsFromSetting(this.setting);
385 this._onLoad({sortFields : this.buildSortList()});
389 fieldmapper.standardRequest(
390 ['open-ils.actor', 'open-ils.actor.patron.settings.retrieve'],
392 params: [this.authtoken, null, this.persistKey],
393 oncomplete: function(r) {
394 var set = openils.Util.readResponse(r);
396 _this.loadColsFromSetting(set);
398 _this.grid.setStructure(_this.structure);
401 _this._onLoad({sortFields : _this.buildSortList()});