Preserve serial interface context, if present
[working/Evergreen.git] / Open-ILS / web / js / ui / default / serial / list_stream.js
1 dojo.require("dijit.form.Button");
2 dojo.require("dijit.form.RadioButton");
3 dojo.require("dijit.form.NumberSpinner");
4 dojo.require("dijit.form.TextBox");
5 dojo.require("dojo.dnd.Source");
6 dojo.require("openils.widget.AutoGrid");
7 dojo.require("openils.widget.ProgressDialog");
8 dojo.require("openils.PermaCrud");
9 dojo.require("openils.CGI");
10
11 var pcrud;
12 var dist_id;
13 var rlu_editor;
14 var cgi;
15 var context_url_param;
16
17 function format_routing_label(routing_label) {
18     return routing_label ? routing_label : "[None]";
19 }
20
21 function load_sstr_grid() {
22     sstr_grid.overrideEditWidgets.distribution =
23         new dijit.form.TextBox({"disabled": true, "value": dist_id});
24
25     sstr_grid.resetStore();
26     sstr_grid.loadAll(
27         {"order_by": {"ssub": "start_date DESC"}},
28         {"distribution": dist_id}
29     );
30 }
31
32 function load_sdist_display() {
33     pcrud.retrieve(
34         "sdist", dist_id, {
35             "onresponse": function(r) {
36                 if (r = openils.Util.readResponse(r)) {
37                     var link = dojo.byId("sdist_label_here");
38                     link.onclick = function() {
39                         location.href = oilsBasePath +
40                             "/serial/subscription?id=" +
41                             r.subscription() + "&tab=distributions" +
42                             context_url_param;
43                     }
44                     link.innerHTML = r.label();
45                     load_sdist_org_unit_display(r);
46                 }
47             }
48         }
49     );
50 }
51
52 function load_sdist_org_unit_display(dist) {
53     dojo.byId("sdist_org_unit_name_here").innerHTML =
54         aou.findOrgUnit(dist.holding_lib()).name();
55 }
56
57 function create_many_streams(fields) {
58     var streams = [];
59     for (var i = 0; i < fields.quantity; i++) {
60         var stream = new sstr();
61         stream.distribution(dist_id);
62         streams.push(stream);
63     }
64
65     progress_dialog.show(true);
66     this.pcrud.create(
67         streams, {
68             "oncomplete": function(r, list) {
69                 progress_dialog.hide();
70                 sstr_grid.refresh();
71             },
72             "onerror": function(r) {
73                 progress_dialog.hide();
74                 alert("Error creating streams!"); /* XXX i18n */
75             }
76         }
77     );
78 }
79
80 function RLUEditor() {
81     var self = this;
82
83     function _reader_xor_dept_toggle(value) {
84         var reader = dijit.byId("reader");
85         var department = dijit.byId("department");
86
87         if (this.id.match(/\w+$/).pop() == "reader")
88             _reader_toggle(value, reader, department);
89         else
90             _department_toggle(value, reader, department);
91     }
92
93     function _reader_toggle(value, reader, department) {
94         if (value) {
95             reader.attr("disabled", false);
96             department.attr("disabled", true);
97             setTimeout(function() { reader.focus(); }, 125);
98         }
99     }
100
101     function _department_toggle(value, reader, department) {
102         if (value) {
103             reader.attr("disabled", true);
104             department.attr("disabled", false);
105             setTimeout(function() { department.focus(); }, 125);
106         }
107     }
108
109     this.user_to_source_entry = function(user) {
110         var node = dojo.create("li");
111         var s;
112         if (user.reader()) {
113             s = dojo.string.substitute(
114                 this.template.reader, [
115                     user.reader().card().barcode(),
116                     user.reader().family_name(),
117                     user.reader().first_given_name(),
118                     user.reader().second_given_name(),
119                     user.reader().home_ou().shortname()
120                 ].map(function(o) { return o == null ? "" : o; })
121             );
122         } else {
123             s = dojo.string.substitute(
124                 this.template.department, [user.department()]
125             );
126         }
127
128         if (user.note()) {
129             s += dojo.string.substitute(this.template.note, [user.note()]);
130         }
131
132         node.innerHTML = "&nbsp;" + s;
133
134         dojo.create(
135             "a", {
136                 "href": "javascript:void(0);",
137                 "onclick": function() { self.toggle_deleted(node); },
138                 "innerHTML": this.template.remove
139             }, node, "first"
140         );
141
142         node._user = user;
143         return node;
144     };
145
146     this.toggle_deleted = function(node) {
147         if (node._user.isdeleted()) {
148             dojo.style(node, "textDecoration", "none");
149             node._user.isdeleted(false);
150         } else {
151             dojo.style(node, "textDecoration", "line-through");
152             node._user.isdeleted(true);
153         }
154     };
155
156     this.new_user = function() {
157         var form = this.dialog.attr("value");
158         var user = new fieldmapper.srlu();
159         user.isnew(true);
160         user.stream(this.stream);
161
162         if (form.note)
163             user.note(form.note);
164
165         if (form.department) {
166             user.department(form.department);
167         } else if (form.reader) {
168             this.add_button.attr("disabled", true);
169             fieldmapper.standardRequest(
170                 ["open-ils.actor",
171                     "open-ils.actor.user.fleshed.retrieve_by_barcode"], {
172                     "params": [openils.User.authtoken, form.reader, true],
173                     "timeout": 10, /* sync */
174                     "onresponse": function(r) {
175                         if (r = openils.Util.readResponse(r)) {
176                             user.reader(r);
177                         }
178                     }
179                 }
180             );
181             this.add_button.attr("disabled", false);
182         } else {
183             alert("Provide either a reader or a department."); /* XXX i18n */
184             return;
185         }
186
187         ["reader", "department", "note"].forEach(
188             function(s) { dijit.byId(s).attr("value", ""); }
189         );
190
191         this.source.insertNodes(false, [self.user_to_source_entry(user)]);
192     }
193
194     this.show = function() {
195         if (sstr_grid.getSelectedRows().length != 1) {
196             alert(
197                 "Use the checkboxes to select exactly one stream " +
198                 "for this operation."   /* XXX i18n */
199             );
200         } else {
201             /* AutoGrid.getSelectedItems() yields a weird, non-FM object */
202             this.stream = sstr_grid.getSelectedItems()[0].id[0];
203
204             this.source.selectAll();
205             this.source.deleteSelectedNodes();
206             this.source.clearItems();
207
208             this.dialog.show();
209
210             fieldmapper.standardRequest(
211                 ["open-ils.serial",
212                     "open-ils.serial.routing_list_users.fleshed_and_ordered"], {
213                     "params": [openils.User.authtoken, this.stream],
214                     "async": true,
215                     "onresponse": function(r) {
216                         if (r = openils.Util.readResponse(r)) {
217                             self.source.insertNodes(
218                                 false, [self.user_to_source_entry(r)]
219                             );
220                         }
221                     },
222                     "oncomplete": function() {
223                         setTimeout(
224                             function() { self.save_button.focus(); }, 125
225                         );
226                     }
227                 }
228             );
229         }
230     };
231
232     this.save = function() {
233         var obj_list = this.source.getAllNodes().map(
234             function(node) {
235                 var obj = node._user;
236                 if (obj.reader())
237                     obj.reader(obj.reader().id());
238
239                 return obj;
240             }
241         );
242
243         this.save_button.attr("disabled", true);
244
245         /* pcrud.apply *almost* could have handled this, but there's a reason
246          * it doesn't, and it has to do with the unique key constraint on the
247          * pos field in srlu objects.
248          */
249         try {
250             fieldmapper.standardRequest(
251                 /* This method will set pos in ascending order. */
252                 ["open-ils.serial",
253                     "open-ils.serial.routing_list_users.replace"], {
254                     "params": [openils.User.authtoken, obj_list],
255                     "timeout": 10, /* sync */
256                     "oncomplete": function(r) {
257                         openils.Util.readResponse(r);   /* display exceptions */
258                     }
259                 }
260             );
261         } catch (E) {
262             alert(E); /* XXX i18n */
263         }
264
265         this.save_button.attr("disabled", false);
266     };
267
268     this._init = function(dialog) {
269         this.dialog = dijit.byId("routing_list_dialog");
270         this.source = routing_list_source;
271
272         this.template = {};
273         ["reader", "department", "note", "remove"].forEach(
274             function(n) {
275                 self.template[n] =
276                     dojo.byId("routing_list_user_template_" + n).innerHTML;
277             }
278         );
279
280         this.add_button = dijit.byId("routing_list_add_button");
281         this.save_button = dijit.byId("routing_list_save_button");
282
283         dijit.byId("reader_xor_dept-reader").onChange =
284             _reader_xor_dept_toggle;
285         dijit.byId("reader_xor_dept-department").onChange =
286             _reader_xor_dept_toggle;
287     };
288
289     this._init.apply(this, arguments);
290 }
291
292 openils.Util.addOnLoad(
293     function() {
294         cgi = new openils.CGI();
295         pcrud = new openils.PermaCrud();
296         rlu_editor = new RLUEditor();
297
298         dist_id = cgi.param("distribution");
299         load_sdist_display();
300         load_sstr_grid();
301
302         var context = cgi.param('context');
303         if (context) {
304             context_url_param = '&context=' + context;
305         } else {
306             context_url_param = '';
307         }
308     }
309 );