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