]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/web/js/dojo/openils/conify/BestHoldOrder.js
52d2862e20c37fcbd73e78eb36be6aac988dfd41
[working/Evergreen.git] / Open-ILS / web / js / dojo / openils / conify / BestHoldOrder.js
1 if (!dojo._hasResource["openils.conify.BestHoldOrder"]) {
2     dojo.requireLocalization("openils.conify", "conify");
3
4     dojo._hasResource["openils.conify.BestHoldOrder"] = true;
5     dojo.provide("openils.conify.BestHoldOrder");
6     dojo.provide("openils.conify.SetOrderer");
7
8     dojo.require("dojo.string");
9     dojo.require("openils.Util");
10     dojo.require("openils.User");
11     dojo.require("openils.PermaCrud");
12     dojo.require("openils.widget.AutoFieldWidget");
13
14 (function() {
15     var localeStrings =
16         dojo.i18n.getLocalization("openils.conify", "conify");
17
18     /* This helper module is OO. */
19     dojo.declare(
20         "openils.conify.SetOrderer", null, {
21             "constructor": function(select, field_map, format_string) {
22                 this.select = select;   /* HTML <select> node */
23                 this.field_map = field_map; /* object of id:label pairs */
24                 this.format_string = format_string || "[${0}] ${1}";
25             },
26
27             "clear": function() {
28                 dojo.forEach(
29                     this.select.options,
30                     dojo.hitch(
31                         this, function(o) { this.select.options.remove(o); }
32                     )
33                 );
34             },
35
36             /* This trusts that what you are passing is actually a set (no
37              * repeats). */
38             "set": function(
39                 set, pos_callback /* called for each set member's <option>
40                                      node now and at any position change */
41             ) {
42                 this.clear();
43                 this.pos_callback = pos_callback;
44                 dojo.forEach(
45                     set, dojo.hitch(this, function(o, p) { this.add(o, p); })
46                 );
47             },
48
49             "focus": function() {
50                 this.select.focus();
51             },
52
53             /* For now this trusts that your item is in the field_map */
54             "add": function(item, position) {
55                 var option = dojo.create(
56                     "option", {
57                         "value": item,
58                         "innerHTML": dojo.string.substitute(
59                             this.format_string, [item, this.field_map[item]]
60                         )
61                     }
62                 );
63
64                 this.select.options.add(option, null);
65                 if (this.pos_callback)
66                     this.pos_callback(option, position);
67             },
68
69             /* Returns option values in order, as a set, assuming you didn't
70              * add dupes. */
71             "get": function() {
72                 /* XXX Could probably use dojo.forEach() here, but don't have
73                  * time to check whether it's sure to preserve order
74                  * with pseudo-arrays or NodeLists or whatever this is. */
75                 var list = [];
76                 for (var i = 0; i < this.select.options.length; i++)
77                     list.push(this.select.options[i].value);
78
79                 return list;
80             },
81
82             "move_selected": function(offset) {
83                 var si = this.select.selectedIndex;
84                 if (si < 0)
85                     return false;
86
87                 var opt = this.select.options[si];
88                 var len = this.select.options.length;
89                 var newpos = si + offset;
90
91                 if (newpos >= 0 && newpos < len) {
92                     var newopt = dojo.clone(opt);
93                     this.select.remove(si);
94                     this.select.add(newopt, newpos);
95
96                     if (this.pos_callback)
97                         for (var i = 0; i < len; i++)
98                             this.pos_callback(this.select.options[i], i);
99
100                     this.select.selectedIndex = newpos;
101                     return true;
102                 } else {
103                     return false;
104                 }
105             },
106         }
107     );
108
109     /* This module is *not* OO. */
110     dojo.declare("openils.conify.BestHoldOrder", null, {});
111
112     var module = openils.conify.BestHoldOrder;
113
114     /* We could get these from the IDL, but if we add more fields to that
115      * later, we have no particular mechanism for determining what is or
116      * isn't metadata. */
117     module.fields = ["pprox", "hprox", "aprox", "priority", "cut", "depth",
118         "htime", "rtime", "approx", "shtime"];
119
120     module.init = function() {
121         module.progress_dialog = dijit.byId("progress-dialog");
122         module.existing_dialog = dijit.byId("cbho-existing");
123
124         dojo.connect(
125             dijit.byId("cbho-existing-edit-go"),
126             "onClick",
127             null,
128             module.editor_load_selected_cbho
129         );
130
131         module.field_labels = {};
132         dojo.forEach(
133             module.fields, function(f) {
134                 module.field_labels[f] = fieldmapper.IDL.fmclasses.cbho.
135                     field_map[f].label
136             }
137         );
138
139         module.set_orderer = new openils.conify.SetOrderer(
140             dojo.byId("cbho-field-order"),
141             module.field_labels,
142             localeStrings.CBHO_FIELD_DISPLAY
143         );
144
145         openils.Util.hide("cbho-loading");
146         openils.Util.show("cbho-main-body");
147     };
148
149     module.new_cbho = function() {
150         module.cbho = new fieldmapper.cbho();
151
152         module.editor_start();
153     };
154
155     module.edit_cbho = function() {
156         module.progress_dialog.show(true);
157
158         function proceed(w) {
159             module.edit_cbho_selector = w;
160             module.progress_dialog.hide();
161             module.existing_dialog.show();
162         };
163
164         if (module.edit_cbho_selector) {
165             proceed(module.edit_cbho_selector);
166         } else {
167             new openils.widget.AutoFieldWidget({
168                 "fmClass": "cbho",
169                 "selfReference": true,
170                 "dijitArgs": {"required": true},
171                 "parentNode": dojo.create(
172                     "span", null, dojo.byId("cbho-existing-selector")
173                 )
174             }).build(proceed);
175         }
176     };
177
178     /* Causes next use of Edit Existing button to recreate, thereby picking
179      * up any new objects */
180     module.clear_cbho_selector = function() {
181         if (module.edit_cbho_selector) {
182             module.edit_cbho_selector.destroy();
183             module.edit_cbho_selector = null;
184         }
185     };
186
187     module.editor_load_selected_cbho = function() {
188         var id = module.edit_cbho_selector.attr("value");
189
190         if (id) {
191             module.cbho = (new openils.PermaCrud()).retrieve("cbho", id);
192             module.editor_start();
193         } else {
194             alert(localeStrings.CBHO_NO_LOAD);
195         }
196     };
197
198     module.editor_start = function() {
199         dojo.byId("cbho-editing").innerHTML = module.cbho.id() ?
200             dojo.string.substitute(
201                 localeStrings.CBHO_EDITING_EXISTING,
202                 [module.cbho.id(), module.cbho.name()]
203             ) :
204             localeStrings.CBHO_EDITING_NEW;
205
206         dojo.byId("cbho-name").value = module.cbho.name() || "";
207         module.editor_reset_order();
208
209         openils.Util.show("cbho-edit-space");
210         module.editor_changed(false);
211     };
212
213     /* Used to set all <option> nodes in the set_orderer to appear disabled if
214      * they now come after rtime. */
215     module.set_pos_callback = function(opt_node, pos) {
216         var method = module.rtime_reached ? "addClass" : "removeClass";
217         dojo[method](opt_node, "post-rtime");
218
219         if (opt_node.value == "rtime")
220             module.rtime_reached = true;
221     };
222
223     module.stored_cbho_field_order = function() {
224         var obj = module.cbho;
225
226         return module.fields.sort(
227             function(a, b) {
228                 a = obj[a]();
229                 var left = (a === null || typeof a == "undefined") ?
230                     999 : Number(a);
231
232                 b = obj[b]();
233                 var right = (b === null || typeof b == "undefined") ?
234                     999 : Number(b);
235
236                 return left - right;
237             }
238         );
239     };
240
241     module.editor_reset_order = function() {
242         module.rtime_reached = false;
243         module.set_orderer.set(
244             module.stored_cbho_field_order(), module.set_pos_callback
245         );
246     };
247
248     module.editor_move = function(offset) {
249         module.rtime_reached = false;
250         if (module.set_orderer.move_selected(offset))
251             module.editor_changed(true);
252
253         /* Without this, focus is now on the up or down button, breaking
254          * the user's ability to select other rows with the arrow keys. */
255         module.set_orderer.focus();
256     };
257
258     module.editor_changed = function(changed) {
259         dojo.attr("cbho-save-changes", "disabled", !changed);
260         if (changed)
261             openils.Util.show("cbho-needs-saved", "inline");
262         else
263             openils.Util.hide("cbho-needs-saved");
264     };
265
266     module.editor_save = function() {
267         var name = dojo.byId("cbho-name").value;
268         if (!name || !name.length) {
269             alert(localeStrings.CBHO_NEEDS_NAME);
270             return false;
271         } else {
272             module.cbho.name(name);
273         }
274
275         module.progress_dialog.show(true);
276         var fields = module.set_orderer.get();
277         for (var i = 0; i < fields.length; i++)
278             module.cbho[fields[i]](i);
279
280         try {
281             var pcrud = new openils.PermaCrud();
282             pcrud[module.cbho.id() ? "update" : "create"](
283                 module.cbho, {
284                     "oncomplete": function(r, list) {
285                         module.progress_dialog.hide();
286                         openils.Util.readResponse(r); /* alert on exceptions? */
287
288                         if (dojo.isArray(list) && list.length) {
289                             if (typeof list[0] == "object")
290                                 module.cbho = list[0];
291
292                             module.clear_cbho_selector();
293                             module.editor_start();
294                         }
295
296                         pcrud.session.disconnect(); /* good hygiene? */
297                     }
298                 }
299             );
300         } catch (E) {
301             alert(E);   /* better than doing nothing? */
302         }
303     };
304
305 })();
306
307 }