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('openils.User');
24 dojo.require('openils.Event');
25 dojo.require('openils.Util');
26 dojo.require('fieldmapper.Fieldmapper');
29 dojo.declare('openils.widget.GridColumnPicker', null, {
31 USER_PERSIST_SETTING : 'ui.grid_columns',
33 constructor : function (authtoken, persistPrefix, grid, structure) {
34 this.dialog = this.buildDialog();
36 this.structure = structure;
38 this.structure = this.grid.attr('structure');
39 this.dialogTable = this.dialog.containerNode.getElementsByTagName('tbody')[0];
40 this.baseCellList = this.structure[0].cells[0].slice();
42 this.authtoken = authtoken;
43 this.savedColums = null;
44 this.persistPrefix = persistPrefix;
48 this.grid.onHeaderContextMenu = function(e) {
54 buildDialog : function() {
59 var dialog = new dijit.Dialog({title : 'Column Picker'});
60 var table = dojo.create('table', {'class':'oils-generic-table', innerHTML :
61 "<thead><tr><th width='33%'>Column</th><th width='33%'>Display</th><th width='33%'>Auto Width</th></tr></thead>" +
62 "<tbody><tr><td><div name='cancel_button'></div></td><td><div name='save_button'></div></td></tr></tbody></table>" });
64 dialog.containerNode.appendChild(table);
66 var button = new dijit.form.Button({label:'Save'}, dojo.query('[name=save_button]', table)[0]);
67 button.onClick = function() { dialog.hide(); self.update(true); };
69 button = new dijit.form.Button({label:'Cancel'}, dojo.query('[name=cancel_button]', table)[0]);
70 button.onClick = function() { dialog.hide(); };
75 // builds the column-picker dialog table
77 var cells = this._selectableCellList();
79 var rows = dojo.query('tr', this.dialogTable);
81 for(var i = 0; i < rows.length; i++) {
82 if(rows[i].getAttribute('picker'))
83 this.dialogTable.removeChild(rows[i]);
86 rows = dojo.query('tr', this.dialogTable);
89 lastChild = rows[rows.length-1];
91 for(var i = 0; i < cells.length; i++) {
92 // setting table.innerHTML breaks stuff, so do it the hard way
94 tr = document.createElement('tr');
95 tr.setAttribute('picker', 'picker');
96 td1 = document.createElement('td');
97 td2 = document.createElement('td');
98 td3 = document.createElement('td');
100 ipt = document.createElement('input');
101 ipt.setAttribute('type', 'checkbox');
102 ipt.setAttribute('checked', 'checked');
103 ipt.setAttribute('ident', cell.field+''+cell.name);
104 ipt.setAttribute('name', 'selector');
106 ipt2 = document.createElement('input');
107 ipt2.setAttribute('type', 'checkbox');
108 ipt2.setAttribute('ident', cell.field+''+cell.name);
109 ipt2.setAttribute('name', 'width');
112 // set the UI based on the loaded settings
113 if(this._arrayHas(this.setting.columns, cell.field)) {
114 if(this._arrayHas(this.setting.auto, cell.field))
115 ipt2.setAttribute('checked', 'checked');
117 ipt.removeAttribute('checked');
121 td1.appendChild(document.createTextNode(cell.name));
122 td2.appendChild(ipt);
123 td3.appendChild(ipt2);
128 this.dialogTable.insertBefore(tr, lastChild);
130 this.dialogTable.appendChild(tr);
134 // update the grid based on the items selected in the picker dialog
135 update : function(persist) {
136 var newCellList = [];
137 var rows = dojo.query('[picker=picker]', this.dialogTable);
139 for(var j = 0; j < this.baseCellList.length; j++) {
140 var cell = this.baseCellList[j];
141 if(cell.selectableColumn) {
142 for(var i = 0; i < rows.length; i++) {
144 var selector = dojo.query('[name=selector]', row)[0];
145 var width = dojo.query('[name=width]', row)[0];
146 if(selector.checked && selector.getAttribute('ident') == cell.field+''+cell.name) {
150 if(cell.width == 'auto')
153 newCellList.push(cell);
156 } else { // if it's not selectable, always show it
157 newCellList.push(cell);
161 this.structure[0].cells[0] = newCellList;
162 this.grid.setStructure(this.structure);
165 if(persist) this.persist();
168 _selectableCellList : function() {
169 var cellList = this.structure[0].cells[0];
171 for(var i = 0; i < cellList.length; i++) {
172 var cell = cellList[i];
173 if(!cell.nonSelectable) cell.selectableColumn = true;
174 if(cell.selectableColumn)
175 cells.push({name:cell.name, field:cell.field});
180 // save me as a user setting
181 persist : function() {
182 var cells = this.structure[0].cells[0];
185 for(var i = 0; i < cells.length; i++) {
187 if(cell.selectableColumn) {
188 list.push(cell.field);
189 if(cell.width == 'auto')
190 autos.push(cell.field);
194 setting[this.USER_PERSIST_SETTING+'.'+this.persistPrefix] = {'columns':list, 'auto':autos};
195 fieldmapper.standardRequest(
196 ['open-ils.actor', 'open-ils.actor.patron.settings.update'],
198 params: [this.authtoken, null, setting],
199 oncomplete: function(r) {
200 var stat = openils.Util.readResponse(r);
206 _arrayHas : function(arr, val) {
207 for(var i = 0; arr && i < arr.length; i++) {
214 _loadColsFromSetting : function(setting) {
215 this.setting = setting;
216 var newCellList = [];
217 for(var j = 0; j < this.baseCellList.length; j++) {
218 var cell = this.baseCellList[j];
219 if(cell.selectableColumn) {
220 if(this._arrayHas(setting.columns, cell.field)) {
221 newCellList.push(cell);
222 if(this._arrayHas(setting.auto, cell.field)) {
225 if(cell.width == 'auto')
229 } else { // if it's not selectable, always show it
230 newCellList.push(cell);
235 this.structure[0].cells[0] = newCellList;
236 this.grid.setStructure(this.structure);
242 return this._loadColsFromSetting(this.setting);
244 fieldmapper.standardRequest(
245 ['open-ils.actor', 'open-ils.actor.patron.settings.retrieve'],
247 params: [this.authtoken, null, this.USER_PERSIST_SETTING+'.'+this.persistPrefix],
248 oncomplete: function(r) {
249 var set = openils.Util.readResponse(r);
251 picker._loadColsFromSetting(set);
254 picker.grid.setStructure(picker.structure);
255 picker.grid.update();