]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/web/js/ui/default/conify/global/config/circ_matrix_matchpoint.js
30044065195f50537d80127aa45402a3ede9cec4
[working/Evergreen.git] / Open-ILS / web / js / ui / default / conify / global / config / circ_matrix_matchpoint.js
1 dojo.require('dijit.layout.ContentPane');
2 dojo.require('dijit.form.Button');
3 dojo.require('openils.widget.AutoGrid');
4 dojo.require('openils.widget.AutoFieldWidget');
5 dojo.require('openils.PermaCrud');
6 dojo.require('openils.widget.ProgressDialog');
7
8 var circModEditor = null;
9 var circModGroupTables = [];
10 var circModGroupCache = {};
11 var circModEntryCache = {};
12 var matchPoint;
13
14 function load(){
15     cmGrid.loadAll({order_by:{ccmm:'circ_modifier'}});
16     cmGrid.onEditPane = buildEditPaneAdditions;
17     circModEditor = dojo.byId('circ-mod-editor').parentNode.removeChild(dojo.byId('circ-mod-editor'));
18 }
19
20 function byName(name, ctxt) {
21     return dojo.query('[name=' + name + ']', ctxt)[0];
22 }
23
24 function buildEditPaneAdditions(editPane) {
25     var node = circModEditor.cloneNode(true);
26     var tableTmpl = node.removeChild(byName('circ-mod-group-table', node));
27     circModGroupTables = [];
28     matchPoint = editPane.fmObject.id();
29
30     byName('add-circ-mod-group', node).onclick = function() {
31         addCircModGroup(node, tableTmpl)
32     }
33
34     if(editPane.mode == 'update') {
35         var groups = new openils.PermaCrud().search('ccmcmt', {matchpoint: editPane.fmObject.id()});
36         dojo.forEach(groups, function(g) { addCircModGroup(node, tableTmpl, g); } );
37     } 
38
39     editPane.domNode.appendChild(node);
40 }
41
42
43 function addCircModGroup(node, tableTmpl, group) {
44
45     var table = tableTmpl.cloneNode(true);
46     var circModRowTmpl = byName('circ-mod-entry-tbody', table).removeChild(byName('circ-mod-entry-row', table));
47     circModGroupTables.push(table);
48
49     var entries = [];
50     if(group) {
51         entries = new openils.PermaCrud().search('ccmcmtm', {circ_mod_test : group.id()});
52         table.setAttribute('group', group.id());
53         circModGroupCache[group.id()] = group;
54         circModEntryCache[group.id()] = entries;
55     }
56
57     function addMod(code, name) {
58         name = name || code; // XXX
59         var row = circModRowTmpl.cloneNode(true);
60         byName('circ-mod', row).innerHTML = name;
61         byName('circ-mod', row).setAttribute('code', code);
62         byName('circ-mod-entry-tbody', table).appendChild(row);
63         byName('remove-circ-mod', row).onclick = function() {
64             byName('circ-mod-entry-tbody', table).removeChild(row);
65         }
66     }
67
68     dojo.forEach(entries, function(e) { addMod(e.circ_mod()); });
69
70     byName('circ-mod-count', table).value = (group) ? group.items_out() : 0;
71
72     var selector = new openils.widget.AutoFieldWidget({
73         fmClass : 'ccmcmtm',
74         fmField : 'circ_mod',
75         parentNode : byName('circ-mod-selector', table)
76     });
77     selector.build();
78
79     byName('add-circ-mod', table).onclick = function() {
80         addMod(selector.widget.attr('value'), selector.widget.attr('displayedValue'));
81     }
82
83     node.insertBefore(table, byName('add-circ-mod-group-span', node));
84     node.insertBefore(dojo.create('hr'), byName('add-circ-mod-group-span', node));
85 }
86
87 function applyCircModChanges() {
88     var pcrud = new openils.PermaCrud();
89     progressDialog.show(true);
90
91     for(var idx in circModGroupTables) {
92         var table = circModGroupTables[idx];
93         var gp = table.getAttribute('group');
94
95         var count = byName('circ-mod-count', table).value;
96         var mods = [];
97         var entries = [];
98
99         dojo.forEach(dojo.query('[name=circ-mod]', table), function(td) { 
100             mods.push(td.getAttribute('code'));
101         });
102
103         var group = circModGroupCache[gp];
104
105         if(!group) {
106
107             group = new fieldmapper.ccmcmt();
108             group.isnew(true);
109             dojo.forEach(mods, function(mod) {
110                 var entry = new fieldmapper.ccmcmtm();
111                 entry.isnew(true);
112                 entry.circ_mod(mod);
113                 entries.push(entry);
114             });
115
116
117         } else {
118
119             var existing = circModEntryCache[group.id()];
120             dojo.forEach(mods, function(mod) {
121                 
122                 // new circ mod for this group
123                 if(!existing.filter(function(i){ return (i.circ_mod() == mod)})[0]) {
124                     var entry = new fieldmapper.ccmcmtm();
125                     entry.isnew(true);
126                     entry.circ_mod(mod);
127                     entries.push(entry);
128                     entry.circ_mod_test(group.id());
129                 }
130             });
131
132             dojo.forEach(existing, function(eMod) {
133                 if(!mods.filter(function(i){ return (i == eMod.circ_mod()) })[0]) {
134                     eMod.isdeleted(true);
135                     entries.push(eMod);
136                 }
137             });
138         }
139
140         group.items_out(count);
141         group.matchpoint(matchPoint);
142
143         if(group.isnew()) {
144
145             pcrud.create(group, {
146                 oncomplete : function(r) {
147                     var group = openils.Util.readResponse(r);
148                     dojo.forEach(entries, function(e) { e.circ_mod_test(group.id()) } );
149                     pcrud.create(entries, {
150                         oncomplete : function() {
151                             progressDialog.hide();
152                         }
153                     });
154                 }
155             });
156
157         } else {
158
159             pcrud.update(group, {
160                 oncomplete : function(r) {
161                     openils.Util.readResponse(r);
162                     var newOnes = entries.filter(function(e) { return e.isnew() });
163                     var delOnes = entries.filter(function(e) { return e.isdeleted() });
164                     if(!delOnes.length && !newOnes.length) {
165                         progressDialog.hide();
166                         return;
167                     }
168                     if(newOnes.length) {
169                         pcrud.create(newOnes, {
170                             oncomplete : function() {
171                                 if(delOnes.length) {
172                                     pcrud.delete(delOnes, {
173                                         oncomplete : function() {
174                                             progressDialog.hide();
175                                         }
176                                     });
177                                 } else {
178                                     progressDialog.hide();
179                                 }
180                             }
181                         });
182                     } else {
183                         pcrud.delete(delOnes, {
184                             oncomplete : function() {
185                                 progressDialog.hide();
186                             }
187                         });
188                     }
189                 }
190             });
191         }
192     }
193 }
194
195 openils.Util.addOnLoad(load);
196