1 if (!dojo._hasResource["openils.conify.BestHoldOrder"]) {
2 dojo.requireLocalization("openils.conify", "conify");
4 dojo._hasResource["openils.conify.BestHoldOrder"] = true;
5 dojo.provide("openils.conify.BestHoldOrder");
6 dojo.provide("openils.conify.SetOrderer");
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");
16 dojo.i18n.getLocalization("openils.conify", "conify");
18 /* This helper module is OO. */
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}";
31 this, function(o) { this.select.options.remove(o); }
36 /* This trusts that what you are passing is actually a set (no
39 set, pos_callback /* called for each set member's <option>
40 node now and at any position change */
43 this.pos_callback = pos_callback;
45 set, dojo.hitch(this, function(o, p) { this.add(o, p); })
53 /* For now this trusts that your item is in the field_map */
54 "add": function(item, position) {
55 var option = dojo.create(
58 "innerHTML": dojo.string.substitute(
59 this.format_string, [item, this.field_map[item]]
64 this.select.options.add(option, null);
65 if (this.pos_callback)
66 this.pos_callback(option, position);
69 /* Returns option values in order, as a set, assuming you didn't
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. */
76 for (var i = 0; i < this.select.options.length; i++)
77 list.push(this.select.options[i].value);
82 "move_selected": function(offset) {
83 var si = this.select.selectedIndex;
87 var opt = this.select.options[si];
88 var len = this.select.options.length;
89 var newpos = si + offset;
91 if (newpos >= 0 && newpos < len) {
92 var newopt = dojo.clone(opt);
93 this.select.remove(si);
94 this.select.add(newopt, newpos);
96 if (this.pos_callback)
97 for (var i = 0; i < len; i++)
98 this.pos_callback(this.select.options[i], i);
100 this.select.selectedIndex = newpos;
109 /* This module is *not* OO. */
110 dojo.declare("openils.conify.BestHoldOrder", null, {});
112 var module = openils.conify.BestHoldOrder;
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
117 module.fields = ["pprox", "hprox", "aprox", "priority", "cut", "depth",
118 "htime", "rtime", "approx", "shtime"];
120 module.init = function() {
121 module.progress_dialog = dijit.byId("progress-dialog");
122 module.existing_dialog = dijit.byId("cbho-existing");
125 dijit.byId("cbho-existing-edit-go"),
128 module.editor_load_selected_cbho
131 module.field_labels = {};
133 module.fields, function(f) {
134 module.field_labels[f] = fieldmapper.IDL.fmclasses.cbho.
139 module.set_orderer = new openils.conify.SetOrderer(
140 dojo.byId("cbho-field-order"),
142 localeStrings.CBHO_FIELD_DISPLAY
145 openils.Util.hide("cbho-loading");
146 openils.Util.show("cbho-main-body");
149 module.new_cbho = function() {
150 module.cbho = new fieldmapper.cbho();
152 module.editor_start();
155 module.edit_cbho = function() {
156 module.progress_dialog.show(true);
158 function proceed(w) {
159 module.edit_cbho_selector = w;
160 module.progress_dialog.hide();
161 module.existing_dialog.show();
164 if (module.edit_cbho_selector) {
165 proceed(module.edit_cbho_selector);
167 new openils.widget.AutoFieldWidget({
169 "selfReference": true,
170 "dijitArgs": {"required": true},
171 "parentNode": dojo.create(
172 "span", null, dojo.byId("cbho-existing-selector")
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;
187 module.editor_load_selected_cbho = function() {
188 var id = module.edit_cbho_selector.attr("value");
191 module.cbho = (new openils.PermaCrud()).retrieve("cbho", id);
192 module.editor_start();
194 alert(localeStrings.CBHO_NO_LOAD);
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()]
204 localeStrings.CBHO_EDITING_NEW;
206 dojo.byId("cbho-name").value = module.cbho.name() || "";
207 module.editor_reset_order();
209 openils.Util.show("cbho-edit-space");
210 module.editor_changed(false);
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");
219 if (opt_node.value == "rtime")
220 module.rtime_reached = true;
223 module.stored_cbho_field_order = function() {
224 var obj = module.cbho;
226 return module.fields.sort(
229 var left = (a === null || typeof a == "undefined") ?
233 var right = (b === null || typeof b == "undefined") ?
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
248 module.editor_move = function(offset) {
249 module.rtime_reached = false;
250 if (module.set_orderer.move_selected(offset))
251 module.editor_changed(true);
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();
258 module.editor_changed = function(changed) {
259 dojo.attr("cbho-save-changes", "disabled", !changed);
261 openils.Util.show("cbho-needs-saved", "inline");
263 openils.Util.hide("cbho-needs-saved");
266 module.editor_save = function() {
267 var name = dojo.byId("cbho-name").value;
268 if (!name || !name.length) {
269 alert(localeStrings.CBHO_NEEDS_NAME);
272 module.cbho.name(name);
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);
281 var pcrud = new openils.PermaCrud();
282 pcrud[module.cbho.id() ? "update" : "create"](
284 "oncomplete": function(r, list) {
285 module.progress_dialog.hide();
286 openils.Util.readResponse(r); /* alert on exceptions? */
288 if (dojo.isArray(list) && list.length) {
289 if (typeof list[0] == "object")
290 module.cbho = list[0];
292 module.clear_cbho_selector();
293 module.editor_start();
296 pcrud.session.disconnect(); /* good hygiene? */
301 alert(E); /* better than doing nothing? */