]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/web/js/ui/default/serial/subscription/caption_and_pattern.js
5382de90025a65237b991a45fdc7ab962b845157
[working/Evergreen.git] / Open-ILS / web / js / ui / default / serial / subscription / caption_and_pattern.js
1 function SCAPRow() {
2     var self = this;
3     var _fields = ["id", "type", "pattern_code", "active", "create_date"];
4
5     this.init = function(id, manager, datum) {
6         this.id = id;
7         this.manager = manager;
8         this.element = dojo.clone(manager.template);
9
10         /* find the controls for each field */
11         this.controls = {};
12         _fields.forEach(
13             function(k) {
14                 self.controls[k] = dojo.query(
15                     "[name='" + k + "'] [control]", self.element
16                 )[0];
17             }
18         );
19
20         /* set up the remover button */
21         this.remover = dojo.query("[name='remover'] button", this.element)[0];
22         this.remover.onclick = function() { manager.remove_row(self); };
23
24         this.save_button = dojo.query("[name='save'] button", this.element)[0];
25         this.save_button.onclick = function() { manager.save_row(self); };
26
27         this.wizard_button = dojo.query(
28             "[name='pattern_code'] button", this.element
29         )[0];
30         this.wizard_button.onclick = function() {
31             if (
32                 openils.Util.trimString(
33                     self.controls.pattern_code.value
34                 ).length &&
35                 !confirm(
36                     "Are you sure you want to erase this pattern code\n" +
37                     "and create a new one via the Wizard?"  /* XXX i18n */
38                 )
39             ) {
40                 return;
41             }
42             try {
43                 netscape.security.PrivilegeManager.enablePrivilege(
44                     "UniversalXPConnect"
45                 );
46                 window.openDialog(
47                     xulG.url_prefix("/xul/server/serial/pattern_wizard.xul"),
48                     "pattern_wizard",
49                     "width=800",
50                     function(value) {
51                         self.controls.pattern_code.value = value;
52                         self.controls.pattern_code.onchange();
53                     }
54                 );
55             } catch (E) {
56                 alert(E); /* XXX */
57             }
58         };
59
60         /* set up onchange handlers for control fields */
61         this.controls.type.onchange = function() {
62             self.has_changed(true);
63             self.datum.type(this.getValue());
64         };
65         this.controls.pattern_code.onchange = function() {
66             self.has_changed(true);
67             self.datum.pattern_code(this.value);
68         };
69         this.controls.active.onchange = function() {
70             self.has_changed(true);
71             self.datum.active(this.checked ? "t" : "f");
72         };
73
74         this.load_fm_object(datum);
75     };
76
77     this.load_fm_object = function(datum) {
78         if (typeof datum != "undefined") {
79             this.datum = datum;
80
81             this.controls.type.setValue(datum.type());
82             this.controls.pattern_code.value = datum.pattern_code();
83             this.controls.active.checked = openils.Util.isTrue(datum.active());
84             this.controls.id.innerHTML = datum.id() || "";
85             this.controls.create_date.innerHTML =
86                 openils.Util.timeStamp(datum.create_date());
87
88             /* Once created, scap objects' pattern_code field is meant to
89              * be immutable.
90              *
91              * See http://list.georgialibraries.org/pipermail/open-ils-dev/2010-May/006079.html
92              *
93              * The DB trigger mentioned was never created to enforce this
94              * immutability at that level, but this should keep users from
95              * doing the wrong thing by mistake.
96              */
97             this.controls.pattern_code.readOnly = true;
98             this.wizard_button.disabled = true;
99
100             this.has_changed(false);
101         } else {
102             this.datum = new scap();
103             this.datum.subscription(this.manager.sub_id);
104
105             _fields.forEach(
106                 function(k) {
107                     try { self.controls[k].onchange(); } catch (E) { ; }
108                 }
109             );
110         }
111     };
112
113     this.has_changed = function(has) {
114         if (typeof has != "undefined") {
115             this._has_changed = has;
116             this.save_button.disabled = !has;
117             dojo.attr(this.element, "changed", String(has));
118         }
119
120         return this._has_changed;
121     };
122
123     this.init.apply(this, arguments);
124 }
125
126 function SCAPEditor() {
127     var self = this;
128
129     this.init = function(sub_id, pcrud) {
130         this.sub_id = sub_id;
131         this.pcrud = pcrud || new openils.PermaCrud();
132
133         this.setup();
134         this.reset();
135         this.load_existing();
136     };
137
138     this.reset = function() {
139         this.virtRowCount = 0;
140         this.rows = {};
141
142         dojo.empty(this.body);
143     };
144
145     this.setup = function() {
146         var template = dojo.query("#scap_editor tbody tr")[0];
147         this.body = template.parentNode;
148         this.template = this.body.removeChild(template);
149
150         dojo.query("#scap_editor button[name='add']")[0].onclick =
151             function() { self.add_row(); };
152
153         openils.Util.show("scap_editor");
154     };
155
156     this.load_existing = function() {
157         this.pcrud.search("scap", {
158                 "subscription": this.sub_id
159             }, {
160                 "order_by": {"scap": "create_date"},
161                 "onresponse": function(r) {
162                     if (r = openils.Util.readResponse(r)) {
163                         r.forEach(function(datum) { self.add_row(datum); });
164                     }
165                 }
166             }
167         );
168     };
169
170     this.add_row = function(datum) {
171         var id;
172         if (typeof datum == "undefined") {
173             id = --(this.virtRowCount);
174             this.rows[id] = new SCAPRow(id, this);
175         } else {
176             id = datum.id();
177             this.rows[id] = new SCAPRow(id, this, datum);
178         }
179
180         dojo.place(this.rows[id].element, this.body, "last");
181     };
182
183     this.save_row = function(row) {
184         var old_id = row.id;
185         if (old_id < 0) {
186             this.pcrud.create(
187                 row.datum, {
188                     "oncomplete": function(r, list) {
189                         openils.Util.readResponse(r);
190                         var new_id = list[0].id();
191                         row.id = new_id;
192                         delete self.rows[old_id];
193                         self.rows[new_id] = row;
194                         row.load_fm_object(list[0]);
195                         row.has_changed(false);
196                     }
197                 }
198             );
199         } else {
200             this.pcrud.update(
201                 row.datum, {
202                     "oncomplete": function(r, list) {
203                         openils.Util.readResponse(r);
204                         row.has_changed(false);
205                     }
206                 }
207             );
208         }
209     };
210
211     this.remove_row = function(row) {
212         function _remove(row) {
213             dojo.destroy(self.rows[row.id].element);
214             delete self.rows[row.id];
215         }
216
217         if (row.id < 0) { /* virtual row */
218             _remove(row);
219         } else { /* real row */
220             this.pcrud.eliminate(
221                 row.datum, {
222                     "oncomplete": function(r, list) {
223                         openils.Util.readResponse(r);
224                         _remove(row);
225                     }
226                 }
227             );
228         }
229     };
230
231     this.init.apply(this, arguments);
232 }
233
234 function SCAPImporter() {
235     var self = this;
236
237     this.init = function(sub) {
238         this.sub = sub;
239
240         this.template = dojo.byId("record_template");
241         this.template = this.template.parentNode.removeChild(this.template);
242         this.template.removeAttribute("id");
243
244         dojo.byId("scaps_from_bib").onclick = function() { self.launch(); };
245     };
246
247     this.launch = function() {
248         this.reset();
249         progress_dialog.show(true);
250
251         fieldmapper.standardRequest(
252             ["open-ils.serial",
253                 "open-ils.serial.caption_and_pattern.find_legacy_by_bib_record"], {
254                 "params": [openils.User.authtoken, this.sub.record_entry()],
255                 "timeout": 10, /* sync */
256                 "onresponse": function(r) {
257                     if (r = openils.Util.readResponse(r)) {
258                         self.add_record(r);
259                     }
260                 }
261             }
262         );
263
264         progress_dialog.hide();
265         if (this.any_records())
266             scaps_from_bib_dialog.show();
267         else /* XXX i18n */
268             alert("No related records with any caption and pattern fields.");
269     };
270
271     this.reset = function() {
272         dojo.empty("record_holder");
273         this._records = [];
274     };
275
276     this.any_records = function() {
277         return Boolean(this._records.length);
278     }
279
280     this.add_record = function(obj) {
281         var row = dojo.clone(this.template);
282
283         var checkbox = dojo.query("input[type='checkbox']", row)[0];
284         obj._checkbox = checkbox;
285
286         this._records.push(obj);
287
288         if (obj.classname == "bre") {
289             /* XXX i18n */
290             node_by_name("obj_class", row).innerHTML = "Bibliographic";
291             node_by_name("obj_id", row).innerHTML = obj.tcn_value();
292             if (obj.owner()) {
293                 openils.Util.show(
294                     node_by_name("obj_owner_container", row), "inline"
295                 );
296                 node_by_name("obj_owner", row).innerHTML = obj.owner();
297             }
298         } else {
299             /* XXX i18n */
300             node_by_name("obj_class", row).innerHTML = "Legacy serial";
301             node_by_name("obj_id", row).innerHTML = obj.id();
302             node_by_name("obj_owner", row).innerHTML = obj.owning_lib();
303             openils.Util.show(
304                 node_by_name("obj_owner_container", row), "inline"
305             );
306         }
307
308         if (!openils.Util.isTrue(obj.active()))
309             openils.Util.show(node_by_name("obj_inactive", row), "inline");
310
311         node_by_name("obj_create", row).innerHTML =
312             /* XXX i18n */
313             dojo.string.substitute(
314                 "${0}, ${1} ${2}", [
315                     obj.creator().family_name(),
316                     obj.creator().first_given_name(),
317                     obj.creator().second_given_name(),
318                 ].map(function(o) { return o || ""; })
319             ) + " on " + openils.Util.timeStamp(obj.create_date());
320
321         node_by_name("obj_edit", row).innerHTML =
322             /* XXX i18n */
323             dojo.string.substitute(
324                 "${0}, ${1} ${2}", [
325                     obj.editor().family_name(),
326                     obj.editor().first_given_name(),
327                     obj.editor().second_given_name(),
328                 ].map(function(o) { return o || ""; })
329             ) + " on " + openils.Util.timeStamp(obj.edit_date());
330
331         dojo.place(row, "record_holder", "last");
332     };
333
334     this.import = function() {
335         var documents = this._records.filter(
336             function(o) { return o._checkbox.checked; }
337         ).map(
338             function(o) { return o.marc(); }
339         );
340
341         if (!documents.length) {
342             /* XXX i18n */
343             alert("You have selected no records from which to import.");
344         } else {
345             progress_dialog.show(true);
346             fieldmapper.standardRequest(
347                 ["open-ils.serial",
348                     "open-ils.serial.caption_and_pattern.create_from_records"],{
349                     "params": [openils.User.authtoken,this.sub.id(),documents],
350                     "async": false,
351                     "onresponse": function(r) {
352                         if (r = openils.Util.readResponse(r)) {
353                             cap_editor.add_row(r);
354                         }
355                     }
356                 }
357             );
358             progress_dialog.hide();
359         }
360     };
361
362     this.init.apply(this, arguments);
363 }