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