]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/web/js/dojo/openils/GridColumnPicker.js
.gitignore additions for (new) generated files
[working/Evergreen.git] / Open-ILS / web / js / dojo / openils / GridColumnPicker.js
1 /* ---------------------------------------------------------------------------
2  * Copyright (C) 2008  Georgia Public Library Service
3  * Bill Erickson <erickson@esilibrary.com>
4  *
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.
9  *
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  * ---------------------------------------------------------------------------
15  */
16
17 dojo.require('openils.User');
18 dojo.require('openils.Event');
19 dojo.require('fieldmapper.Fieldmapper');
20
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, {
26
27         USER_PERSIST_SETTING : 'ui.grid_columns',
28
29         constructor : function (dialog, grid, structure, authtoken, persistId) {
30             this.dialog = dialog;
31             this.grid = grid;
32             this.structure = structure;
33             this.dialogTable = dialog.containerNode.getElementsByTagName('tbody')[0];
34             this.baseCellList = this.structure[0].cells[0].slice();
35             this.build();
36             this.grid.model.fields.get(0).sort = false;
37             this.authtoken = authtoken;
38             this.savedColums = null;
39             this.persistId = persistId;
40             this.setting = null;
41         },
42
43         // builds the column-picker dialog table
44         build : function() {
45             var  cells = this._selectableCellList();
46             var str = '';
47             var rows = dojo.query('tr', this.dialogTable);
48
49             for(var i = 0; i < rows.length; i++) {
50                 if(rows[i].getAttribute('picker'))
51                     this.dialogTable.removeChild(rows[i]);
52             }
53
54             rows = dojo.query('tr', this.dialogTable);
55             var lastChild = null;
56             if(rows.length > 0)
57                 lastChild = rows[rows.length-1];
58
59             for(var i = 0; i < cells.length; i++) {
60                 // setting table.innerHTML breaks stuff, so do it the hard way
61                 var cell = cells[i];
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');
67
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');
73
74                 ipt2 = document.createElement('input');
75                 ipt2.setAttribute('type', 'checkbox');
76                 ipt2.setAttribute('ident', cell.field+''+cell.name);
77                 ipt2.setAttribute('name', 'width');
78
79                 if(this.setting) {
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');
84                     } else {
85                         ipt.removeAttribute('checked');
86                     }
87                 }
88
89                 td1.appendChild(document.createTextNode(cell.name));
90                 td2.appendChild(ipt);
91                 td3.appendChild(ipt2);
92                 tr.appendChild(td1);
93                 tr.appendChild(td2);
94                 tr.appendChild(td3);
95                 if(lastChild)
96                     this.dialogTable.insertBefore(tr, lastChild);
97                 else
98                     this.dialogTable.appendChild(tr);
99             }
100         },
101
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);
106
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++) {
111                         var row = rows[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) {
115                             if(width.checked)
116                                 cell.width = 'auto';
117                             else delete cell.width;
118                             newCellList.push(cell);
119                         }
120                     }
121                 } else { // if it's not selectable, always show it
122                     newCellList.push(cell); 
123                 }
124             }
125
126             this.structure[0].cells[0] = newCellList;
127             this.grid.setStructure(this.structure);
128             this.grid.update();
129
130             if(persist) this.persist();
131         },
132
133         _selectableCellList : function() {
134             var cellList = this.structure[0].cells[0];
135             var cells = [];
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}); 
140             }
141             return cells;
142         },
143
144         // save me as a user setting
145         persist : function() {
146             var cells = this.structure[0].cells[0];
147             var list = [];
148             var autos = [];
149             for(var i = 0; i < cells.length; i++) {
150                 var cell = cells[i];
151                 if(cell.selectableColumn) {
152                     list.push(cell.field);
153                     if(cell.width == 'auto')
154                         autos.push(cell.field);
155                 }
156             }
157             var setting = {};
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'],
161                 {   async: true,
162                     params: [this.authtoken, null, setting],
163                     oncomplete: function(r) {
164                         var stat = r.recv().content();
165                         if(e = openils.Event.parse(stat))
166                             return alert(e);
167                     }
168                 }
169             );
170         }, 
171
172         _arrayHas : function(arr, val) {
173             for(var i = 0; arr && i < arr.length; i++) {
174                 if(arr[i] == val)
175                     return true;
176             }
177             return false;
178         },
179
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))
189                             cell.width = 'auto';
190                         else delete cell.width;
191                     }
192                 }  else { // if it's not selectable, always show it
193                     newCellList.push(cell); 
194                 }
195             }
196
197             this.build();
198             this.structure[0].cells[0] = newCellList;
199             this.grid.setStructure(this.structure);
200             this.grid.update();
201         },
202
203         load : function() {
204             if(this.setting)
205                 return this._loadColsFromSetting(this.setting);
206             var picker = this;
207             fieldmapper.standardRequest(
208                 ['open-ils.actor', 'open-ils.actor.patron.settings.retrieve'],
209                 {   async: true,
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))
214                             return alert(e)
215                         if(set) {
216                             picker._loadColsFromSetting(set);
217                         } else {
218                             picker.build();
219                             picker.grid.setStructure(picker.structure);
220                             picker.grid.update();
221                         }
222                     }
223                 }
224             );
225         },
226     });
227 }
228