]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/web/conify/global/config/marc_code_maps.js
applying dan's dojo 1.2 compat patch
[Evergreen.git] / Open-ILS / web / conify / global / config / marc_code_maps.js
1 /*
2 # ---------------------------------------------------------------------------
3 # Copyright (C) 2008  Georgia Public Library Service / Equinox Software, Inc
4 # Mike Rylander <miker@esilibrary.com>
5
6 # This program is free software; you can redistribute it and/or
7 # modify it under the terms of the GNU General Public License
8 # as published by the Free Software Foundation; either version 2
9 # of the License, or (at your option) any later version.
10
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 # GNU General Public License for more details.
15 # ---------------------------------------------------------------------------
16 */
17
18 dojo.require('fieldmapper.dojoData');
19 dojo.require('openils.widget.TranslatorPopup');
20 dojo.require('dojo.cookie');
21 dojo.require('dojo.parser');
22 dojo.require('dojo.string');
23 dojo.require('dojo.data.ItemFileWriteStore');
24 dojo.require('dijit.form.TextBox');
25 dojo.require('dijit.form.ValidationTextBox');
26 dojo.require('dijit.form.Textarea');
27 dojo.require('dijit.layout.TabContainer');
28 dojo.require('dijit.layout.ContentPane');
29 dojo.require('dijit.layout.LayoutContainer');
30 dojo.require('dijit.layout.BorderContainer');
31 dojo.require('dojox.widget.Toaster');
32 dojo.require('dojox.fx');
33 dojo.require('dojox.grid.Grid');
34 dojo.require('dojox.grid.compat._data.model');
35 dojo.require("dojox.grid.compat._data.editors");
36
37
38
39 console.log('loading marc_code_maps.js');
40
41 // some handy globals
42 var cgi = new CGI();
43 var ses = dojo.cookie('ses') || cgi.param('ses');
44 var pCRUD = new OpenSRF.ClientSession('open-ils.permacrud');
45
46 console.log('initialized pcrud session');
47
48 var stores = {};
49 var current_item = {};
50
51 /*
52 var highlighter = {
53         green : dojox.fx.highlight( { color : '#B4FFB4', node : 'grid_container', duration : 500 } ),
54         red : dojox.fx.highlight( { color : '#FF2018', node : 'grid_container', duration : 500 } )
55 };
56
57 console.log('highlighters set up');
58 */
59
60 var dirtyStore = [];
61
62 function status_update (markup) {
63         if (parent !== window && parent.status_update) parent.status_update( markup );
64 }
65
66 console.log('local status function built');
67
68 function save_code (classname) {
69
70         var item = current_item[classname];
71         var obj = new fieldmapper[classname]().fromStoreItem( item );
72
73         obj.ischanged( 1 );
74         obj.code( dojo.string.trim( obj.code() ) );
75         obj.value( dojo.string.trim( obj.value() ) );
76         if(classname == 'cam' || classname == 'clfm')
77                 obj.description( dojo.string.trim( obj.description() ) );
78
79         pCRUD.request({
80                 method : 'open-ils.permacrud.update.' + classname,
81                 timeout : 10,
82                 params : [ ses, modified_ppl ],
83                 onerror : function (r) {
84                         //highlighter.red.play();
85                         status_update( 'Problem saving data for ' + classname + ' ' + obj.code() );
86                 },
87                 oncomplete : function (r) {
88                         var res = r.recv();
89                         if ( res && res.content() ) {
90                                 stores[classname].setValue( current_item, 'ischanged', 0 );
91                                 //highlighter.green.play();
92                                 status_update( 'Saved changes to ' + stores[classname].getValue( item, 'code' ) );
93                         } else {
94                                 //highlighter.red.play();
95                                 status_update( 'Problem saving data for ' + classname + ' ' + stores[classname].getValue( item, 'code' ) );
96                         }
97                 },
98         }).send();
99 }
100
101 function save_them_all (event) {
102
103         for (var classname in stores) {
104
105                 var store = stores[classname];
106                 store.fetch({
107                         query : { ischanged : 1 },
108                         onItem : function (item, req) { try { if (this.isItem( item )) window.dirtyStore.push( item ); } catch (e) { /* meh */ } },
109                         scope : store
110                 });
111
112                 var confirmation = true;
113
114                 if (event && dirtyStore.length > 0) {
115                         confirmation = confirm(
116                                 'There are unsaved modified ' + classname + ' codes!  '+
117                                 'OK to save these changes, Cancel to abandon them.'
118                         );
119                         event = null;
120                 }
121
122                 if (confirmation) {
123                         for (var i in dirtyStore) {
124                                 current_item[classname] = dirtyStore[i];
125                                 save_object(classname);
126                         }
127
128                         dirtyStore = [];
129                 }
130         }
131 }
132
133 dojo.addOnUnload( save_them_all );
134
135 function delete_grid_selection(classname, grid ) {
136
137     var selected_rows = grid.selection.getSelected();
138         
139     var selected_items = [];
140     for (var i in selected_rows) {
141         selected_items.push(
142             grid.model.getRow( selected_rows[i] ).__dojo_data_item
143         );
144     }
145
146     grid.selection.clear();
147
148     for (var i in selected_items) {
149         var item = selected_items[i];
150
151         if ( confirm('Are you sure you want to delete ' + grid.model.store.getValue( item, 'value' ) + '?')) {
152
153             grid.model.store.setValue( item, 'isdeleted', 1 );
154             
155             var obj = new fieldmapper[classname]().fromStoreItem( item );
156             obj.isdeleted( 1 );
157             
158             pCRUD.request({
159                 method : 'open-ils.permacrud.delete.' + classname,
160                 timeout : 10,
161                 params : [ ses, obj ],
162                 onerror : function (r) {
163                     //highlighter.red.play();
164                     status_update( 'Problem deleting ' + grid.model.store.getValue( item, 'value' ) );
165                 },
166                 oncomplete : function (r) {
167                     var res = r.recv();
168                     var old_name = grid.model.store.getValue( item, 'value' );
169                     if ( res && res.content() ) {
170
171                         grid.model.store.fetch({
172                             query : { code : grid.model.store.getValue( item, 'code' ) },
173                             onItem : function (item, req) { try { if (this.isItem( item )) this.deleteItem( item ); } catch (e) { /* meh */ } },
174                             scope : grid.model.store
175                         });
176             
177                         //highlighter.green.play();
178                         status_update( old_name + ' deleted' );
179                     } else {
180                         //highlighter.red.play();
181                         status_update( 'Problem deleting ' + old_name );
182                     }
183                 }
184             }).send();
185         
186         }
187     }
188 }
189
190 function create_marc_code (data) {
191
192         var cl = data.classname;
193         if (!cl) return false;
194
195         data.code = dojo.string.trim( data.code );
196         data.value = dojo.string.trim( data.value );
197
198         if(!data.code || !data.value) return false;
199
200         if(cl == 'cam' || cl == 'clfm')
201                 data.description = dojo.string.trim( data.description );
202
203     var new_fm_obj = new fieldmapper[cl]().fromHash( data )
204     new_fm_obj.isnew(1);
205
206     var err = false;
207     pCRUD.request({
208         method : 'open-ils.permacrud.create.' + cl,
209         timeout : 10,
210         params : [ ses, new_fm_obj ],
211         onerror : function (r) {
212             //highlighter.red.play();
213             status_update( 'Problem calling method to create new ' + cl );
214             err = true;
215         },
216         oncomplete : function (r) {
217             var res = r.recv();
218             if ( res && res.content() ) {
219                 var new_item_hash = res.content().toHash();
220                 stores[cl].newItem( new_item_hash );
221                 status_update( 'New ' + new_item_hash.code + ' ' + cl + ' created' );
222                 //highlighter.green.play();
223             } else {
224                 //highlighter.red.play();
225                 status_update( 'Problem creating new Permission' );
226                 err = true;
227             }
228         }
229     }).send();
230
231         return false;
232 }
233