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 console.log("DEPRECATED! Use openils.widget.GridColumnPicker instead.");
23 dojo._hasResource["openils.GridColumnPicker"] = true;
24 dojo.provide('openils.GridColumnPicker');
25 dojo.declare('openils.GridColumnPicker', null, {
27 USER_PERSIST_SETTING : 'ui.grid_columns',
29 constructor : function (dialog, grid, structure, authtoken, persistId) {
32 this.structure = structure;
33 this.dialogTable = dialog.containerNode.getElementsByTagName('tbody')[0];
34 this.baseCellList = this.structure[0].cells[0].slice();
36 this.grid.model.fields.get(0).sort = false;
37 this.authtoken = authtoken;
38 this.savedColums = null;
39 this.persistId = persistId;
43 // builds the column-picker dialog table
45 var cells = this._selectableCellList();
47 var rows = dojo.query('tr', this.dialogTable);
49 for(var i = 0; i < rows.length; i++) {
50 if(rows[i].getAttribute('picker'))
51 this.dialogTable.removeChild(rows[i]);
54 rows = dojo.query('tr', this.dialogTable);
57 lastChild = rows[rows.length-1];
59 for(var i = 0; i < cells.length; i++) {
60 // setting table.innerHTML breaks stuff, so do it the hard way
62 tr = document.createElement('tr');
63 tr.setAttribute('picker', 'picker');
64 td1 = document.createElement('td');
65 td2 = document.createElement('td');
66 td3 = document.createElement('td');
68 ipt = document.createElement('input');
69 ipt.setAttribute('type', 'checkbox');
70 ipt.setAttribute('checked', 'checked');
71 ipt.setAttribute('ident', cell.field+''+cell.name);
72 ipt.setAttribute('name', 'selector');
74 ipt2 = document.createElement('input');
75 ipt2.setAttribute('type', 'checkbox');
76 ipt2.setAttribute('ident', cell.field+''+cell.name);
77 ipt2.setAttribute('name', 'width');
80 // set the UI based on the loaded settings
81 if(this._arrayHas(this.setting.columns, cell.field)) {
82 if(this._arrayHas(this.setting.auto, cell.field))
83 ipt2.setAttribute('checked', 'checked');
85 ipt.removeAttribute('checked');
89 td1.appendChild(document.createTextNode(cell.name));
91 td3.appendChild(ipt2);
96 this.dialogTable.insertBefore(tr, lastChild);
98 this.dialogTable.appendChild(tr);
102 // update the grid based on the items selected in the picker dialog
103 update : function(persist) {
104 var newCellList = [];
105 var rows = dojo.query('[picker=picker]', this.dialogTable);
107 for(var j = 0; j < this.baseCellList.length; j++) {
108 var cell = this.baseCellList[j];
109 if(cell.selectableColumn) {
110 for(var i = 0; i < rows.length; i++) {
112 var selector = dojo.query('[name=selector]', row)[0];
113 var width = dojo.query('[name=width]', row)[0];
114 if(selector.checked && selector.getAttribute('ident') == cell.field+''+cell.name) {
117 else delete cell.width;
118 newCellList.push(cell);
121 } else { // if it's not selectable, always show it
122 newCellList.push(cell);
126 this.structure[0].cells[0] = newCellList;
127 this.grid.setStructure(this.structure);
130 if(persist) this.persist();
133 _selectableCellList : function() {
134 var cellList = this.structure[0].cells[0];
136 for(var i = 0; i < cellList.length; i++) {
137 var cell = cellList[i];
138 if(cell.selectableColumn)
139 cells.push({name:cell.name, field:cell.field});
144 // save me as a user setting
145 persist : function() {
146 var cells = this.structure[0].cells[0];
149 for(var i = 0; i < cells.length; i++) {
151 if(cell.selectableColumn) {
152 list.push(cell.field);
153 if(cell.width == 'auto')
154 autos.push(cell.field);
158 setting[this.USER_PERSIST_SETTING+'.'+this.persistId] = {'columns':list, 'auto':autos};
159 fieldmapper.standardRequest(
160 ['open-ils.actor', 'open-ils.actor.patron.settings.update'],
162 params: [this.authtoken, null, setting],
163 oncomplete: function(r) {
164 var stat = r.recv().content();
165 if(e = openils.Event.parse(stat))
172 _arrayHas : function(arr, val) {
173 for(var i = 0; arr && i < arr.length; i++) {
180 _loadColsFromSetting : function(setting) {
181 this.setting = setting;
182 var newCellList = [];
183 for(var j = 0; j < this.baseCellList.length; j++) {
184 var cell = this.baseCellList[j];
185 if(cell.selectableColumn) {
186 if(this._arrayHas(setting.columns, cell.field)) {
187 newCellList.push(cell);
188 if(this._arrayHas(setting.auto, cell.field))
190 else delete cell.width;
192 } else { // if it's not selectable, always show it
193 newCellList.push(cell);
198 this.structure[0].cells[0] = newCellList;
199 this.grid.setStructure(this.structure);
205 return this._loadColsFromSetting(this.setting);
207 fieldmapper.standardRequest(
208 ['open-ils.actor', 'open-ils.actor.patron.settings.retrieve'],
210 params: [this.authtoken, null, this.USER_PERSIST_SETTING+'.'+this.persistId],
211 oncomplete: function(r) {
212 var set = r.recv().content();
213 if(e = openils.Event.parse(set))
216 picker._loadColsFromSetting(set);
219 picker.grid.setStructure(picker.structure);
220 picker.grid.update();