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