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