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