]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/web/js/dojo/openils/widget/GridColumnPicker.js
1993e27a5f9f40baa9f8787d3e76a112b320f0b8
[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('fieldmapper.Fieldmapper');
29     dojo.require('openils.Util');
30
31     dojo.declare(
32         'openils.widget.GridColumnPicker',
33         [dijit.Menu],
34         {
35
36             USER_PERSIST_SETTING : 'ui.grid_columns',
37
38             /**
39              * Load the fields from the grid and map them to the MenuItem's.  
40              * Load settings from server
41              */
42             init : function(args) {
43
44                 this.grid = args.grid;
45                 this.persistPrefix = args.prefix;
46                 this.authtoken = args.authtoken;
47                 this.cells = this.grid.structure[0].cells[0];
48                 var self = this;
49
50                 dojo.style(this.grid.domNode.id, 'visibility', 'hidden');
51
52                 dojo.forEach(this.getChildren(),
53                     function(child) {
54                         for(var i in self.cells) {
55                             var name = self.cells[i].name;
56                             if(name == child.attr('label')) {
57                                 child.field = {label:name, ident:self.cells[i].field};
58                                 break;
59                             }   
60                         }
61                     }
62                 );
63                 this.load();
64             },
65
66             onClose : function() {
67                 this.inherited('onClose',arguments);
68                 this.persist();
69             },
70
71             /**
72              * Save new settings on the server
73              */
74             persist : function() {
75                 var selected = [];
76                 var autoFields = [];
77                 dojo.forEach(this.getChildren(),
78                     function(child) {
79                         if(child.checked) {
80                             selected.push(child.field.ident)
81                         }
82                     }
83                 );
84                 var setting = {};
85                 setting[this.USER_PERSIST_SETTING+'.'+this.persistPrefix] = {'columns':selected, 'auto':autoFields};
86                 fieldmapper.standardRequest(
87                     ['open-ils.actor', 'open-ils.actor.patron.settings.update'],
88                     {   async: true,
89                         params: [this.authtoken, null, setting],
90                         oncomplete: function(r) {
91                             openils.Util.readResponse(r);
92                         }
93                     }
94                 );
95             },
96
97             /**
98              * Load existing settings from the server
99              */
100             load : function() {
101                 var self = this;
102                 fieldmapper.standardRequest(
103                     ['open-ils.actor', 'open-ils.actor.patron.settings.retrieve'],
104                     {   async: true,
105                         params: [this.authtoken, null, this.USER_PERSIST_SETTING+'.'+this.persistPrefix],
106                         oncomplete: function(r) { self._loadCallback(r); }
107                     }
108                 );
109             },
110
111             _loadCallback : function(r) {
112                 dojo.style(this.grid.domNode.id, 'visibility', 'visible');
113                 if(settings = openils.Util.readResponse(r)) {
114                     dojo.forEach(this.getChildren(),
115                         function(child) {
116                             if(child.field) {
117                                 if(!openils.Util.arrayContains(settings.columns, child.field.ident)) {
118                                     child.attr("checked", false);
119                                     child.onChange(child.checked);
120                                 }
121                             }
122                         }
123                     );
124                 }
125             },
126
127         } // class def
128     );
129 }