]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/web/conify/global/config/marc_code_maps.js
marc code editor works (sans vr format map)
[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._data.model');
35 dojo.require("dojox.grid.editors");
36
37
38 console.log('loading marc_code_maps.js');
39
40 // some handy globals
41 var cgi = new CGI();
42 var ses = dojo.cookie('ses') || cgi.param('ses');
43 var pCRUD = new OpenSRF.ClientSession('open-ils.permacrud');
44
45 console.log('initialized pcrud session');
46
47 var stores = {};
48 var current_item = {};
49
50 /*
51 var highlighter = {
52         green : dojox.fx.highlight( { color : '#B4FFB4', node : 'grid_container', duration : 500 } ),
53         red : dojox.fx.highlight( { color : '#FF2018', node : 'grid_container', duration : 500 } )
54 };
55
56 console.log('highlighters set up');
57 */
58
59 var dirtyStore = [];
60
61 function status_update (markup) {
62         if (parent !== window && parent.status_update) parent.status_update( markup );
63 }
64
65 console.log('local status function built');
66
67 function save_code (classname) {
68
69         var item = current_item[classname];
70         var obj = new fieldmapper[classname]().fromStoreItem( item );
71
72         obj.ischanged( 1 );
73         obj.code( dojo.string.trim( obj.code() ) );
74         obj.value( dojo.string.trim( obj.value() ) );
75         if(classname == 'cam' || classname == 'clfm')
76                 obj.description( dojo.string.trim( obj.description() ) );
77
78         pCRUD.request({
79                 method : 'open-ils.permacrud.update.' + classname,
80                 timeout : 10,
81                 params : [ ses, modified_ppl ],
82                 onerror : function (r) {
83                         //highlighter.red.play();
84                         status_update( 'Problem saving data for ' + classname + ' ' + obj.code() );
85                 },
86                 oncomplete : function (r) {
87                         var res = r.recv();
88                         if ( res && res.content() ) {
89                                 stores[classname].setValue( current_item, 'ischanged', 0 );
90                                 //highlighter.green.play();
91                                 status_update( 'Saved changes to ' + stores[classname].getValue( item, 'code' ) );
92                         } else {
93                                 //highlighter.red.play();
94                                 status_update( 'Problem saving data for ' + classname + ' ' + stores[classname].getValue( item, 'code' ) );
95                         }
96                 },
97         }).send();
98 }
99
100 function save_them_all (event) {
101
102         for (var classname in stores) {
103
104                 var store = stores[classname];
105                 store.fetch({
106                         query : { ischanged : 1 },
107                         onItem : function (item, req) { try { if (this.isItem( item )) window.dirtyStore.push( item ); } catch (e) { /* meh */ } },
108                         scope : store
109                 });
110
111                 var confirmation = true;
112
113                 if (event && dirtyStore.length > 0) {
114                         confirmation = confirm(
115                                 'There are unsaved modified ' + classname + ' codes!  '+
116                                 'OK to save these changes, Cancel to abandon them.'
117                         );
118                         event = null;
119                 }
120
121                 if (confirmation) {
122                         for (var i in dirtyStore) {
123                                 current_item[classname] = dirtyStore[i];
124                                 save_object(classname);
125                         }
126
127                         dirtyStore = [];
128                 }
129         }
130 }
131
132 dojo.addOnUnload( save_them_all );
133
134 function delete_grid_selection(classname, grid ) {
135
136     var selected_rows = grid.selection.getSelected();
137         
138     var selected_items = [];
139     for (var i in selected_rows) {
140         selected_items.push(
141             grid.model.getRow( selected_rows[i] ).__dojo_data_item
142         );
143     }
144
145     grid.selection.clear();
146
147     for (var i in selected_items) {
148         var item = selected_items[i];
149
150         if ( confirm('Are you sure you want to delete ' + grid.model.store.getValue( item, 'value' ) + '?')) {
151
152             grid.model.store.setValue( item, 'isdeleted', 1 );
153             
154             var obj = new fieldmapper[classname]().fromStoreItem( item );
155             obj.isdeleted( 1 );
156             
157             pCRUD.request({
158                 method : 'open-ils.permacrud.delete.' + classname,
159                 timeout : 10,
160                 params : [ ses, obj ],
161                 onerror : function (r) {
162                     //highlighter.red.play();
163                     status_update( 'Problem deleting ' + grid.model.store.getValue( item, 'value' ) );
164                 },
165                 oncomplete : function (r) {
166                     var res = r.recv();
167                     var old_name = grid.model.store.getValue( item, 'value' );
168                     if ( res && res.content() ) {
169
170                         grid.model.store.fetch({
171                             query : { code : grid.model.store.getValue( item, 'code' ) },
172                             onItem : function (item, req) { try { if (this.isItem( item )) this.deleteItem( item ); } catch (e) { /* meh */ } },
173                             scope : grid.model.store
174                         });
175             
176                         //highlighter.green.play();
177                         status_update( old_name + ' deleted' );
178                     } else {
179                         //highlighter.red.play();
180                         status_update( 'Problem deleting ' + old_name );
181                     }
182                 }
183             }).send();
184         
185         }
186     }
187 }
188
189 function create_marc_code (data) {
190
191         var cl = data.classname;
192         if (!cl) return false;
193
194         data.code = dojo.string.trim( data.code );
195         data.value = dojo.string.trim( data.value );
196
197         if(!data.code || !data.value) return false;
198
199         if(cl == 'cam' || cl == 'clfm')
200                 data.description = dojo.string.trim( data.description );
201
202     var new_fm_obj = new fieldmapper[cl]().fromHash( data )
203     new_fm_obj.isnew(1);
204
205     var err = false;
206     pCRUD.request({
207         method : 'open-ils.permacrud.create.' + cl,
208         timeout : 10,
209         params : [ ses, new_fm_obj ],
210         onerror : function (r) {
211             //highlighter.red.play();
212             status_update( 'Problem calling method to create new ' + cl );
213             err = true;
214         },
215         oncomplete : function (r) {
216             var res = r.recv();
217             if ( res && res.content() ) {
218                 var new_item_hash = res.content().toHash();
219                 stores[cl].newItem( new_item_hash );
220                 status_update( 'New ' + new_item_hash.code + ' ' + cl + ' created' );
221                 //highlighter.green.play();
222             } else {
223                 //highlighter.red.play();
224                 status_update( 'Problem creating new Permission' );
225                 err = true;
226             }
227         }
228     }).send();
229
230         return false;
231 }
232