]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/web/js/dojo/openils/GridColumnPicker.js
7ba129dd4031478ea71618b2eb01f846ccccd36c
[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 if(!dojo._hasResource["openils.GridColumnPicker"]) {
18     dojo._hasResource["openils.GridColumnPicker"] = true;
19     dojo.provide('openils.GridColumnPicker');
20     dojo.declare('openils.GridColumnPicker', null, {
21
22         constructor : function (dialog, grid) {
23             this.dialog = dialog;
24             this.grid = grid;
25             this.structure = grid.structure;
26             this.dialogTable = dialog.domNode.getElementsByTagName('tbody')[0];
27             this.baseCellList = this.structure[0].cells[0].slice();
28             this.build();
29             this.grid.model.fields.get(0).sort = false;
30         },
31
32         // builds the column-picker dialog table
33         build : function() {
34             var  cells = this._selectableCellList();
35             var str = '';
36             var rows = dojo.query('tr', this.dialogTable);
37
38             for(var i = 0; i < rows.length; i++) {
39                 if(rows[i].getAttribute('picker'))
40                     this.dialogTable.removeChild(rows[i]);
41             }
42
43             rows = dojo.query('tr', this.dialogTable);
44             var lastChild = null;
45             if(rows.length > 0)
46                 lastChild = rows[rows.length-1];
47
48             for(var i = 0; i < cells.length; i++) {
49                 // setting table.innerHTML breaks stuff, so do it the hard way
50                 tr = document.createElement('tr');
51                 tr.setAttribute('picker', 'picker');
52                 td1 = document.createElement('td');
53                 td2 = document.createElement('td');
54                 td3 = document.createElement('td');
55
56                 ipt = document.createElement('input');
57                 ipt.setAttribute('type', 'checkbox');
58                 ipt.setAttribute('checked', 'checked');
59                 ipt.setAttribute('ident', cells[i].field+''+cells[i].name);
60                 ipt.setAttribute('name', 'selector');
61
62                 ipt2 = document.createElement('input');
63                 ipt2.setAttribute('type', 'checkbox');
64                 ipt2.setAttribute('ident', cells[i].field+''+cells[i].name);
65                 ipt2.setAttribute('name', 'width');
66
67                 td1.appendChild(document.createTextNode(cells[i].name));
68                 td2.appendChild(ipt);
69                 td3.appendChild(ipt2);
70                 tr.appendChild(td1);
71                 tr.appendChild(td2);
72                 tr.appendChild(td3);
73                 if(lastChild)
74                     this.dialogTable.insertBefore(tr, lastChild);
75                 else
76                     this.dialogTable.appendChild(tr);
77             }
78         },
79
80         // update the grid based on the items selected in the picker dialog
81         update : function() {
82             var newCellList = [];
83             var rows = dojo.query('[picker=picker]', this.dialogTable);
84
85             for(var j = 0; j < this.baseCellList.length; j++) {
86                 var cell = this.baseCellList[j];
87                 if(cell.selectableColumn) {
88                     for(var i = 0; i < rows.length; i++) {
89                         var row = rows[i];
90                         var selector = dojo.query('[name=selector]', row)[0];
91                         var width = dojo.query('[name=width]', row)[0];
92                         if(selector.checked && selector.getAttribute('ident') == cell.field+''+cell.name) {
93                             if(width.checked)
94                                 cell.width = 'auto';
95                             else delete cell.width;
96                             newCellList.push(cell);
97                         }
98                     }
99                 } else { // if it's not selectable, always show it
100                     newCellList.push(cell); 
101                 }
102             }
103
104             this.structure[0].cells[0] = newCellList;
105             this.grid.setStructure(this.structure);
106             this.grid.update();
107         },
108
109         _selectableCellList : function() {
110             var cellList = this.structure[0].cells[0];
111             var cells = [];
112             for(var i = 0; i < cellList.length; i++) {
113                 var cell = cellList[i];
114                 if(cell.selectableColumn) 
115                     cells.push({name:cell.name, field:cell.field}); 
116             }
117             return cells;
118         },
119
120         // save me as a user setting
121         persist : function() {
122         }
123     });
124 }
125