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