]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/web/js/dojo/openils/GridColumnPicker.js
implemented column persistence via user setting. only update grid layout when necess...
[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     dojo._hasResource["openils.GridColumnPicker"] = true;
23     dojo.provide('openils.GridColumnPicker');
24     dojo.declare('openils.GridColumnPicker', null, {
25
26         USER_PERSIST_SETTING : 'ui.grid_columns',
27
28         constructor : function (dialog, grid, structure, authtoken, persistId) {
29             this.dialog = dialog;
30             this.grid = grid;
31             this.structure = structure;
32             this.dialogTable = dialog.domNode.getElementsByTagName('tbody')[0];
33             this.baseCellList = this.structure[0].cells[0].slice();
34             this.build();
35             this.grid.model.fields.get(0).sort = false;
36             this.authtoken = authtoken;
37             this.savedColums = null;
38             this.persistId = persistId;
39             this.setting = null;
40         },
41
42         // builds the column-picker dialog table
43         build : function() {
44             var  cells = this._selectableCellList();
45             var str = '';
46             var rows = dojo.query('tr', this.dialogTable);
47
48             for(var i = 0; i < rows.length; i++) {
49                 if(rows[i].getAttribute('picker'))
50                     this.dialogTable.removeChild(rows[i]);
51             }
52
53             rows = dojo.query('tr', this.dialogTable);
54             var lastChild = null;
55             if(rows.length > 0)
56                 lastChild = rows[rows.length-1];
57
58             for(var i = 0; i < cells.length; i++) {
59                 // setting table.innerHTML breaks stuff, so do it the hard way
60                 var cell = cells[i];
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');
66
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');
72
73                 ipt2 = document.createElement('input');
74                 ipt2.setAttribute('type', 'checkbox');
75                 ipt2.setAttribute('ident', cell.field+''+cell.name);
76                 ipt2.setAttribute('name', 'width');
77
78                 if(this.setting) {
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');
83                     } else {
84                         ipt.removeAttribute('checked');
85                     }
86                 }
87
88                 td1.appendChild(document.createTextNode(cell.name));
89                 td2.appendChild(ipt);
90                 td3.appendChild(ipt2);
91                 tr.appendChild(td1);
92                 tr.appendChild(td2);
93                 tr.appendChild(td3);
94                 if(lastChild)
95                     this.dialogTable.insertBefore(tr, lastChild);
96                 else
97                     this.dialogTable.appendChild(tr);
98             }
99         },
100
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);
105
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++) {
110                         var row = rows[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) {
114                             if(width.checked)
115                                 cell.width = 'auto';
116                             else delete cell.width;
117                             newCellList.push(cell);
118                         }
119                     }
120                 } else { // if it's not selectable, always show it
121                     newCellList.push(cell); 
122                 }
123             }
124
125             this.structure[0].cells[0] = newCellList;
126             this.grid.setStructure(this.structure);
127             this.grid.update();
128
129             if(persist) this.persist();
130         },
131
132         _selectableCellList : function() {
133             var cellList = this.structure[0].cells[0];
134             var cells = [];
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}); 
139             }
140             return cells;
141         },
142
143         // save me as a user setting
144         persist : function() {
145             var cells = this.structure[0].cells[0];
146             var list = [];
147             var autos = [];
148             for(var i = 0; i < cells.length; i++) {
149                 var cell = cells[i];
150                 if(cell.selectableColumn) {
151                     list.push(cell.field);
152                     if(cell.width == 'auto')
153                         autos.push(cell.field);
154                 }
155             }
156             var setting = {};
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'],
160                 {   async: true,
161                     params: [this.authtoken, null, setting],
162                     oncomplete: function(r) {
163                         var stat = r.recv().content();
164                         if(e = openils.Event.parse(stat))
165                             return alert(e);
166                     }
167                 }
168             );
169         }, 
170
171         _arrayHas : function(arr, val) {
172             for(var i = 0; arr && i < arr.length; i++) {
173                 if(arr[i] == val)
174                     return true;
175             }
176             return false;
177         },
178
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))
188                             cell.width = 'auto';
189                         else delete cell.width;
190                     }
191                 }  else { // if it's not selectable, always show it
192                     newCellList.push(cell); 
193                 }
194             }
195
196             this.build();
197             this.structure[0].cells[0] = newCellList;
198             this.grid.setStructure(this.structure);
199             this.grid.update();
200         },
201
202         load : function() {
203             var picker = this;
204             fieldmapper.standardRequest(
205                 ['open-ils.actor', 'open-ils.actor.patron.settings.retrieve'],
206                 {   async: true,
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))
211                             return alert(e)
212                         if(set) 
213                             picker._loadColsFromSetting(set);
214                     }
215                 }
216             );
217         },
218     });
219 }
220