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