]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/web/js/ui/default/serial/subscription/caption_and_pattern.js
Serials: dojo/autogrid-based scaffolding for building serials objects
[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 }