]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/web/js/dojo/openils/widget/GridColumnPicker.js
added column picker support for suppressing picker actions on configured columns
[working/Evergreen.git] / Open-ILS / web / js / dojo / openils / widget / 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
18 /**
19  * Create a new menu that can be used as a grid column picker.  This version
20  * takes advantage of Dojo's 1.2 headerMenu attribute for selecting which
21  * columns to display.  As columns are chosen, they are updated on the server
22  * with user settings.
23  */
24
25 if(!dojo._hasResource['openils.widget.GridColumnPicker']) {
26     dojo.provide('openils.widget.GridColumnPicker');
27     dojo.require('dijit.Menu');
28     dojo.require('dojox.widget.PlaceholderMenuItem');
29     dojo.require('fieldmapper.Fieldmapper');
30     dojo.require('openils.Util');
31
32     dojo.declare(
33         'openils.widget.GridColumnPicker',
34         [dijit.Menu],
35         {
36
37             USER_PERSIST_SETTING : 'ui.grid_columns',
38
39             /**
40              * Load the fields from the grid and map them to the MenuItem's.  
41              * Load settings from server
42              */
43             init : function(args) {
44
45                 this.grid = args.grid;
46                 this.persistPrefix = args.prefix;
47                 this.authtoken = args.authtoken;
48                 this.cells = this.grid.structure[0].cells[0];
49                 var self = this;
50
51                 dojo.style(this.grid.domNode.id, 'visibility', 'hidden');
52
53                 dojo.forEach(this.getChildren(),
54                     function(child) {
55                         dojo.forEach(self.cells,
56                             function(cell) {
57                                 if(cell.name == child.attr('label')) {
58                                     if(cell.nonSelectable) {
59                                         console.log("removing child " + child.attr('label'));
60                                         self.removeChild(child);
61                                     } else {
62                                         child.field = {label:name, ident:cell.field};
63                                     }
64                                     return;
65                                 }   
66                             }
67                         )
68                     }
69                 );
70                 this.load();
71             },
72
73             onClose : function() {
74                 this.inherited('onClose',arguments);
75                 this.persist();
76             },
77
78             /**
79              * Save new settings on the server
80              */
81             persist : function() {
82                 var selected = [];
83                 var autoFields = [];
84                 dojo.forEach(this.getChildren(),
85                     function(child) {
86                         if(child.checked) {
87                             selected.push(child.field.ident)
88                         }
89                     }
90                 );
91                 var setting = {};
92                 setting[this.USER_PERSIST_SETTING+'.'+this.persistPrefix] = {'columns':selected, 'auto':autoFields};
93                 fieldmapper.standardRequest(
94                     ['open-ils.actor', 'open-ils.actor.patron.settings.update'],
95                     {   async: true,
96                         params: [this.authtoken, null, setting],
97                         oncomplete: function(r) {
98                             openils.Util.readResponse(r);
99                         }
100                     }
101                 );
102             },
103
104             /**
105              * Load existing settings from the server
106              */
107             load : function() {
108                 var self = this;
109                 fieldmapper.standardRequest(
110                     ['open-ils.actor', 'open-ils.actor.patron.settings.retrieve'],
111                     {   async: true,
112                         params: [this.authtoken, null, this.USER_PERSIST_SETTING+'.'+this.persistPrefix],
113                         oncomplete: function(r) { self._loadCallback(r); }
114                     }
115                 );
116             },
117
118             _loadCallback : function(r) {
119                 dojo.style(this.grid.domNode.id, 'visibility', 'visible');
120                 if(settings = openils.Util.readResponse(r)) {
121                     dojo.forEach(this.getChildren(),
122                         function(child) {
123                             if(child.field) {
124                                 if(!openils.Util.arrayContains(settings.columns, child.field.ident)) {
125                                     child.attr("checked", false);
126                                     child.onChange(child.checked);
127                                 }
128                             }
129                         }
130                     );
131                 }
132             },
133
134         } // class def
135     );
136 }