product options
[bse.git] / site / templates / admin / edit_prodopts.tmpl
CommitLineData
58baa27b
TC
1<:wrap admin/xbase.tmpl title=>"[articleType] Options", showtitle=>1, menuitem=>prodopts:>
2<:ifMessage:><div id="message"><:message:></div><:or:><:eif:>
3<:include admin/product_menu.tmpl:>
4
5<:if Dboptions:>
6<h2>Product options</h2>
7<div id="prodoptmenu"></div>
8<div id="productoptions">
9<:iterator begin dboptions:>
10<div id="prodopt<:dboption id:>" class="prodopt">
11<div id="prodoptmenu<:dboption id:>" class="prodoptmenu">Option: <span id="prodoptname<:dboption id:>"><:dboption name:></span> <a href="<:script:>?id=<:article id:>&amp;a_edit_option=1&amp;option_id=<:dboption id:>">Edit</a> <a href="<:script:>?id=<:article id:>&amp;a_delconf_option=1&amp;option_id=<:dboption id:>">Delete</a> <:dboption_move:></div>
12<:if Dboptionvalues:>
13<ul id="vallist<:dboption id:>" class="prodoptvalues">
14<:iterator begin dboptionvalues:>
15<li id="valentry<:dboptionvalue id:>"><:dboptionvalue value:> <:ifEq [dboptionvalue id] [dboption default_value]:>(default)<:or:><:eif:> <a href="<:script:>?id=<:product id:>&amp;value_id=<:dboptionvalue id:>&amp;a_edit_option_value=1">Edit</a> <a href="<:script:>?id=<:product id:>&amp;value_id=<:dboptionvalue id:>&amp;a_confdel_option_value=1">Delete</a> <:dboptionvalue_move:></li>
16<:iterator end dboptionvalues:>
17</ul>
18<form action="<:script:>" method="post" id="valform<:dboption id:>" /><input type="hidden" name="id" value="<:article id:>" /><input type="hidden" name="option_id" value="<:dboption id:>" /><input type="hidden" name="_t" value="prodopts" /><:csrfp admin_add_option_value hidden:><input type="text" name="value" /><input type="submit" name="a_add_option_value" value="Add Value" /></form>
19<:or Dboptionvalues:><:eif Dboptionvalues:>
20</div>
21<:iterator end dboptions:>
22</div>
23<:or Dboptions:><:eif Dboptions:>
24<div id="addoptionform">
25<form action="<:script:>" method="post">
26<:csrfp admin_add_option hidden:>
27<input type="hidden" name="_t" value="prodopts" />
28<input type="hidden" name="id" value="<:article id:>" />
29<table>
30 <tr>
31 <th>Name</th>
32 <td><input type="text" name="name" value="<:old name:>" size="40" maxlength="40" /><:error_img name:></td>
33 </tr>
34 <tr>
35 <th>Values</th>
36 <td><input type="text" name="value1" value="<:old value1:>" size="40" maxlength="40" /><:error_img value1:><br />
37<input type="text" name="value2" value="<:old value2:>" size="40" maxlength="40" /><:error_img value2:><br />
38<input type="text" name="value3" value="<:old value3:>" size="40" maxlength="40" /><:error_img value3:><br />
39<input type="text" name="value4" value="<:old value4:>" size="40" maxlength="40" /><:error_img value4:><br />
40<input type="text" name="value5" value="<:old value5:>" size="40" maxlength="40" /><:error_img value5:><br /></td>
41 </tr>
42 <tr>
43 <td colspan="2"><input type="submit" name="a_add_option" value="Add New Option" /></td>
44 </tr>
45</table>
46</form>
47</div>
48<div id="addoptionbutton" style="display: none">
49<a href="#" onclick="javascript: document.getElementById('addoptionform').style.display='block'; return false;">Add an option</a>
50</div>
51<:ifDboptions:>
52<script>
53
54// this really all belongs in a separate file
55
56$('addoptionform').style.display='none';
57$('addoptionbutton').style.display='block';
58var prodopts = <:dboptionsjson:>;
59var prodopts_by_id = new Object;
60var reorder_values_csrf = '<:csrfp admin_move_option_value:>';
61var reorder_options_csrf = '<:csrfp admin_move_option:>';
62var menu = $('prodoptmenu');
63if (menu) {
64 menu.appendChild(document.createTextNode("All Options: | "));
65 var sort_a = document.createElement("a");
66 sort_a.href = "javascript:sort_prodopts()";
67 sort_a.title = "Sort product options alphabetically";
68 sort_a.appendChild(document.createTextNode("Sort"));
69 menu.appendChild(sort_a);
70 menu.appendChild(document.createTextNode(" |"));
71}
72for (var i = 0; i < prodopts.length; ++i) {
73 var opt = prodopts[i];
74 prodopts_by_id[opt.id] = opt;
75 var opt_ele_id = 'prodoptmenu' + opt.id;
76 var opt_ele = $(opt_ele_id);
77 opt_ele.appendChild(document.createTextNode(" "));
78 var sort_a = document.createElement("a");
79 sort_a.href = "javascript:sort_prodopt_values('" + opt.id + "')";
80 sort_a.title = "Sort values alphabetically";
81 sort_a.appendChild(document.createTextNode("Sort"));
82 opt_ele.appendChild(sort_a);
83 for (var j = 0; j < opt.values.length; ++j) {
84 var val = opt.values[j];
85 var s = $('optval' + val.id);
86 var p = $('valentry' + val.id);
87 var f = $('editval' + val.id);
88
89 if (s && f && p) {
90 s.style.display = 'inline';
91 f.style.display = 'none';
92 var a = document.createElement("a");
93 a.onclick = make_edit_handler(f, a, s);
94 a.href="#";
95 var atext = document.createTextNode("Edit");
96 a.appendChild(atext);
97 p.appendChild(document.createTextNode(" "));
98 p.appendChild(a);
99 }
100 }
101}
102
103function reorder_option_values(id, order) {
104 var parent = $("vallist"+id);
105 var nodes = new Array;
106 var nodes_by_id = new Object;
107 for (var i = 0; i < parent.childNodes.length; ++i) {
108 var n = parent.childNodes[i];
109 if (n.id) {
110 var m = n.id.match(/^valentry(\d+)$/);
111 if (m) {
112 nodes_by_id[m[1]] = n;
113 nodes.push(n);
114 }
115 }
116 }
117 // remove our value nodes
118 for (var i = 0; i < nodes; ++i) {
119 parent.removeChild(nodes[i]);
120 }
121 // put them back in, in the new order
122 for (var i = 0; i < order.length; ++i) {
123 var n = nodes_by_id[order[i]];
124 if (n)
125 parent.appendChild(n);
126 }
127
128 // TODO: reorder the values in prodopts
129}
130
131function reorder_options(order) {
132 var parent = $("productoptions");
133 var nodes = new Array;
134 var nodes_by_id = new Object;
135 for (var i = 0; i < parent.childNodes.length; ++i) {
136 var n = parent.childNodes[i];
137 if (n.id) {
138 var m = n.id.match(/^prodopt(\d+)$/);
139 if (m) {
140 nodes_by_id[m[1]] = n;
141 nodes.push(n);
142 }
143 }
144 }
145 // remove our value nodes
146 for (var i = 0; i < nodes; ++i) {
147 parent.removeChild(nodes[i]);
148 }
149 // put them back in, in the new order
150 for (var i = 0; i < order.length; ++i) {
151 var n = nodes_by_id[order[i]];
152 if (n)
153 parent.appendChild(n);
154 }
155
156 // TODO: reorder the values in prodopts
157}
158
159var busy_img = $('busy_img');
160
161function set_busy(id) {
162 if (busy_img)
163 busy_img.style.display = 'inline';
164}
165function set_not_busy(id) {
166 if (busy_img)
167 busy_img.style.display = 'none';
168}
169
170function sort_prodopt_values(id) {
171 var opt = prodopts_by_id[id];
172
173 var ord = new Array;
174 // duplicate to sort it
175 for (var i = 0; i < opt.values.length; ++i) {
176 ord.push(opt.values[i]);
177 }
178 ord.sort(function(a, b) {
179 var va = a.value.toLowerCase();
180 var vb = b.value.toLowerCase();
181 if (va < vb) return -1;
182 if (va > vb) return 1;
183 return 0;
184 });
185 var ids = new Array;
186 for (var i = 0; i < ord.length; ++i) {
187 ids.push(ord[i].id);
188 }
189 set_busy();
190 new Ajax.Request("<:script:>", {
191 method: "post",
192 parameters: {
193 a_option_value_reorder: 1,
194 id: "<:article id:>",
195 option_id: id,
196 value_ids: ids.join(","),
197 _csrfp: reorder_values_csrf,
198 _: 1
199 },
200 onSuccess: function(xport) {
201 var json = xport.responseJSON;
202 set_not_busy();
203 if (json.success) {
204 reorder_option_values(json.option.id, json.order);
205 }
206 else {
207 alert("Error sorting: " + json.error);
208 }
209 },
210 onFailure: function() {
211 alert("Error contacting server");
212 set_not_busy();
213 }
214 });
215}
216
217function sort_prodopts() {
218 var ord = new Array;
219 for (var i = 0; i < prodopts.length; ++i) {
220 ord.push(prodopts[i]);
221 }
222 ord.sort(function(a, b) {
223 var va = a.name.toLowerCase();
224 var vb = b.name.toLowerCase();
225 if (va < vb) return -1;
226 if (va > vb) return 1;
227 return 0;
228 });
229 var ids = new Array;
230 for (var i = 0; i < ord.length; ++i) {
231 ids.push(ord[i].id);
232 }
233 set_busy();
234 new Ajax.Request("<:script:>", {
235 method: "post",
236 parameters: {
237 a_option_reorder: 1,
238 id: "<:article id:>",
239 option_ids: ids.join(","),
240 _csrfp: reorder_options_csrf,
241 _: 1
242 },
243 onSuccess: function(xport) {
244 var json = xport.responseJSON;
245 set_not_busy();
246 if (json.success) {
247 reorder_options(json.order);
248 }
249 else {
250 alert("Error sorting: " + json.error);
251 }
252 },
253 onFailure: function() {
254 alert("Error contacting server");
255 set_not_busy();
256 }
257 });
258}
259</script>
260<:or:><:eif:>