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 * ---------------------------------------------------------------------------
17 dojo.require('openils.User');
18 dojo.require('openils.Event');
19 dojo.require('fieldmapper.Fieldmapper');
21 if(!dojo._hasResource["openils.GridColumnPicker"]) {
22 dojo._hasResource["openils.GridColumnPicker"] = true;
23 dojo.provide('openils.GridColumnPicker');
24 dojo.declare('openils.GridColumnPicker', null, {
26 USER_PERSIST_SETTING : 'ui.grid_columns',
28 constructor : function (dialog, grid, structure, authtoken, persistId) {
31 this.structure = structure;
32 this.dialogTable = dialog.domNode.getElementsByTagName('tbody')[0];
33 this.baseCellList = this.structure[0].cells[0].slice();
35 this.grid.model.fields.get(0).sort = false;
36 this.authtoken = authtoken;
37 this.savedColums = null;
38 this.persistId = persistId;
42 // builds the column-picker dialog table
44 var cells = this._selectableCellList();
46 var rows = dojo.query('tr', this.dialogTable);
48 for(var i = 0; i < rows.length; i++) {
49 if(rows[i].getAttribute('picker'))
50 this.dialogTable.removeChild(rows[i]);
53 rows = dojo.query('tr', this.dialogTable);
56 lastChild = rows[rows.length-1];
58 for(var i = 0; i < cells.length; i++) {
59 // setting table.innerHTML breaks stuff, so do it the hard way
61 tr = document.createElement('tr');
62 tr.setAttribute('picker', 'picker');
63 td1 = document.createElement('td');
64 td2 = document.createElement('td');
65 td3 = document.createElement('td');
67 ipt = document.createElement('input');
68 ipt.setAttribute('type', 'checkbox');
69 ipt.setAttribute('checked', 'checked');
70 ipt.setAttribute('ident', cell.field+''+cell.name);
71 ipt.setAttribute('name', 'selector');
73 ipt2 = document.createElement('input');
74 ipt2.setAttribute('type', 'checkbox');
75 ipt2.setAttribute('ident', cell.field+''+cell.name);
76 ipt2.setAttribute('name', 'width');
79 // set the UI based on the loaded settings
80 if(this._arrayHas(this.setting.columns, cell.field)) {
81 if(this._arrayHas(this.setting.auto, cell.field))
82 ipt2.setAttribute('checked', 'checked');
84 ipt.removeAttribute('checked');
88 td1.appendChild(document.createTextNode(cell.name));
90 td3.appendChild(ipt2);
95 this.dialogTable.insertBefore(tr, lastChild);
97 this.dialogTable.appendChild(tr);
101 // update the grid based on the items selected in the picker dialog
102 update : function(persist) {
103 var newCellList = [];
104 var rows = dojo.query('[picker=picker]', this.dialogTable);
106 for(var j = 0; j < this.baseCellList.length; j++) {
107 var cell = this.baseCellList[j];
108 if(cell.selectableColumn) {
109 for(var i = 0; i < rows.length; i++) {
111 var selector = dojo.query('[name=selector]', row)[0];
112 var width = dojo.query('[name=width]', row)[0];
113 if(selector.checked && selector.getAttribute('ident') == cell.field+''+cell.name) {
116 else delete cell.width;
117 newCellList.push(cell);
120 } else { // if it's not selectable, always show it
121 newCellList.push(cell);
125 this.structure[0].cells[0] = newCellList;
126 this.grid.setStructure(this.structure);
129 if(persist) this.persist();
132 _selectableCellList : function() {
133 var cellList = this.structure[0].cells[0];
135 for(var i = 0; i < cellList.length; i++) {
136 var cell = cellList[i];
137 if(cell.selectableColumn)
138 cells.push({name:cell.name, field:cell.field});
143 // save me as a user setting
144 persist : function() {
145 var cells = this.structure[0].cells[0];
148 for(var i = 0; i < cells.length; i++) {
150 if(cell.selectableColumn) {
151 list.push(cell.field);
152 if(cell.width == 'auto')
153 autos.push(cell.field);
157 setting[this.USER_PERSIST_SETTING+'.'+this.persistId] = {'columns':list, 'auto':autos};
158 fieldmapper.standardRequest(
159 ['open-ils.actor', 'open-ils.actor.patron.settings.update'],
161 params: [this.authtoken, null, setting],
162 oncomplete: function(r) {
163 var stat = r.recv().content();
164 if(e = openils.Event.parse(stat))
171 _arrayHas : function(arr, val) {
172 for(var i = 0; arr && i < arr.length; i++) {
179 _loadColsFromSetting : function(setting) {
180 this.setting = setting;
181 var newCellList = [];
182 for(var j = 0; j < this.baseCellList.length; j++) {
183 var cell = this.baseCellList[j];
184 if(cell.selectableColumn) {
185 if(this._arrayHas(setting.columns, cell.field)) {
186 newCellList.push(cell);
187 if(this._arrayHas(setting.auto, cell.field))
189 else delete cell.width;
191 } else { // if it's not selectable, always show it
192 newCellList.push(cell);
197 this.structure[0].cells[0] = newCellList;
198 this.grid.setStructure(this.structure);
204 fieldmapper.standardRequest(
205 ['open-ils.actor', 'open-ils.actor.patron.settings.retrieve'],
207 params: [this.authtoken, null, this.USER_PERSIST_SETTING+'.'+this.persistId],
208 oncomplete: function(r) {
209 var set = r.recv().content();
210 if(e = openils.Event.parse(set))
213 picker._loadColsFromSetting(set);