]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/web/conify/global/config/marc_code_maps.js
Improve Firefox/XULRunner Support
[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.AutoIDL');
19 dojo.require('fieldmapper.dojoData');
20 dojo.require('openils.widget.TranslatorPopup');
21 dojo.require('openils.PermaCrud');
22 dojo.require('dojo.cookie');
23 dojo.require('dojo.parser');
24 dojo.require('dojo.string');
25 dojo.require('dojo.data.ItemFileWriteStore');
26 dojo.require('dijit.form.Form');
27 dojo.require('dijit.form.TextBox');
28 dojo.require('dijit.form.ValidationTextBox');
29 dojo.require('dijit.form.Textarea');
30 dojo.require('dijit.layout.TabContainer');
31 dojo.require('dijit.layout.ContentPane');
32 dojo.require('dijit.layout.LayoutContainer');
33 dojo.require('dijit.layout.BorderContainer');
34 dojo.require('dojox.widget.Toaster');
35 dojo.require('dojox.fx');
36 dojo.require('dojox.grid.Grid');
37 dojo.require('openils.XUL');
38 dojo.requireLocalization("openils.conify", "conify");
39
40 console.log('loading marc_code_maps.js');
41
42 // some handy globals
43 var cgi = new CGI();
44 var ses = dojo.cookie('ses') || cgi.param('ses');
45 if(!ses && openils.XUL.isXUL()) {
46     var stash = openils.XUL.getStash();
47     ses = stash.session.key;
48 }
49 var pCRUD = new openils.PermaCrud({authtoken:ses});
50
51 console.log('initialized pcrud session');
52
53 var stores = {};
54 var current_item = {};
55
56 var cam_strings = dojo.i18n.getLocalization('openils.conify', 'conify');
57
58 /*
59 var highlighter = {
60         green : dojox.fx.highlight( { color : '#B4FFB4', node : 'grid_container', duration : 500 } ),
61         red : dojox.fx.highlight( { color : '#FF2018', node : 'grid_container', duration : 500 } )
62 };
63
64 console.log('highlighters set up');
65 */
66
67 var dirtyStore = [];
68
69 function status_update (markup) {
70         if (parent !== window && parent.status_update) parent.status_update( markup );
71 }
72
73 console.log('local status function built');
74
75 function save_code (classname) {
76
77         var item = current_item[classname];
78         var obj = new fieldmapper[classname]().fromStoreItem( item );
79
80         obj.ischanged( 1 );
81         obj.code( dojo.string.trim( obj.code() ) );
82         obj.value( dojo.string.trim( obj.value() ) );
83         if(classname == 'cam' || classname == 'clfm')
84                 obj.description( dojo.string.trim( obj.description() ) );
85
86         pCRUD.update(obj, {
87                 onerror : function (r) {
88                         //highlighter.red.play();
89                         status_update( dojo.string.substitute(cam_strings.ERROR_SAVING_DATA_CAM, [classname, obj.code()]) );
90                 },
91                 oncomplete : function (r) {
92                         stores[classname].setValue( current_item, 'ischanged', 0 );
93                         status_update( dojo.string.substitute(cam_strings.SUCCESS_SAVE, stores[classname].getValue( item, 'code' )) );
94                 }
95         });
96 }
97
98 function save_them_all (event) {
99
100         for (var classname in stores) {
101
102                 var store = stores[classname];
103                 store.fetch({
104                         query : { ischanged : 1 },
105                         onItem : function (item, req) { try { if (this.isItem( item )) window.dirtyStore.push( item ); } catch (e) { /* meh */ } },
106                         scope : store
107                 });
108
109                 var confirmation = true;
110
111                 if (event && dirtyStore.length > 0) {
112                         confirmation = confirm( cam_strings.CONFIRM_EXIT_CAM );
113                         event = null;
114                 }
115
116                 if (confirmation) {
117                         for (var i in dirtyStore) {
118                                 current_item[classname] = dirtyStore[i];
119                                 save_object(classname);
120                         }
121
122                         dirtyStore = [];
123                 }
124         }
125 }
126
127 dojo.addOnUnload( save_them_all );
128
129 function delete_grid_selection(classname, grid ) {
130
131     var selected_rows = grid.selection.getSelected();
132         
133     var selected_items = [];
134     for (var i in selected_rows) {
135         selected_items.push(
136             grid.model.getRow( selected_rows[i] ).__dojo_data_item
137         );
138     }
139
140     grid.selection.clear();
141
142     for (var i in selected_items) {
143         var item = selected_items[i];
144
145         if ( confirm( dojo.string.substitute( cam_strings.CONFIRM_DELETE, [grid.model.store.getValue( item, 'value' )] ) ) ) {
146
147             grid.model.store.setValue( item, 'isdeleted', 1 );
148             
149             var obj = new fieldmapper[classname]().fromStoreItem( item );
150             obj.isdeleted( 1 );
151             
152             pCRUD.eliminate(obj, {
153                 onerror : function (r) {
154                     //highlighter.red.play();
155                     status_update( dojo.string.substitute( cam_strings.ERROR_DELETING, [grid.model.store.getValue( item, 'value' )] ) );
156                 },
157                 oncomplete : function (r) {
158                     grid.model.store.fetch({
159                         query : { code : grid.model.store.getValue( item, 'code' ) },
160                         onItem : function (item, req) { try { if (this.isItem( item )) this.deleteItem( item ); } catch (e) { /* meh */ } },
161                         scope : grid.model.store
162                     });
163             
164                     status_update( dojo.string.substitute( cam_strings.STATUS_DELETED, [old_name] ) );
165                 }
166             });
167         
168         }
169     }
170 }
171
172 function create_marc_code (data) {
173
174         var cl = data.classname;
175         if (!cl) return false;
176
177         data.code = dojo.string.trim( data.code );
178         data.value = dojo.string.trim( data.value );
179
180         if(!data.code || !data.value) return false;
181
182         if(cl == 'cam' || cl == 'clfm')
183                 data.description = dojo.string.trim( data.description );
184
185     var new_fm_obj = new fieldmapper[cl]().fromHash( data )
186     new_fm_obj.isnew(1);
187
188     var err = false;
189     pCRUD.create(new_fm_obj, {
190         onerror : function (r) {
191             //highlighter.red.play();
192             status_update( dojo.string.substitute( cam_strings.ERROR_CALLING_METHOD_CAM, [cl] ) );
193             err = true;
194         },
195         oncomplete : function (r, list) {
196             var new_item_hash = list[0].toHash();
197             stores[cl].newItem( new_item_hash );
198             status_update( dojo.string.substitute( cam_strings.SUCCESS_CREATING_CODE, [new_item_hash.code, cl] ) );
199             //highlighter.green.play();
200         }
201     });
202
203         return false;
204 }
205