1 | /*global $, dotclear */ |
---|
2 | 'use strict'; |
---|
3 | |
---|
4 | //~ metaEditor & metaEditor.prototype should go to the core. |
---|
5 | function metaEditor(target, meta_field, meta_type, meta_options) { |
---|
6 | this.target = target; |
---|
7 | this.meta_field = meta_field; |
---|
8 | this.meta_type = meta_type; |
---|
9 | |
---|
10 | // options |
---|
11 | meta_options = meta_options || {}; |
---|
12 | this.meta_url = meta_options.meta_url || this.meta_url; |
---|
13 | this.list_type = meta_options.list_type || this.list_type; |
---|
14 | this.text_confirm_remove = meta_options.text_confirm_remove || this.text_confirm_remove; |
---|
15 | this.text_add_meta = meta_options.text_add_meta || this.text_add_meta; |
---|
16 | this.text_choose = meta_options.text_choose || this.text_choose; |
---|
17 | this.text_all = meta_options.text_all || this.text_all; |
---|
18 | this.text_separation = meta_options.text_separation || this.text_separation; |
---|
19 | } |
---|
20 | |
---|
21 | metaEditor.prototype = { |
---|
22 | meta_url: '', |
---|
23 | text_confirm_remove: 'Are you sure you want to remove this %s?', |
---|
24 | text_add_meta: 'Add a %s to this entry', |
---|
25 | text_choose: 'Choose from list', |
---|
26 | text_all: 'all', |
---|
27 | text_separation: 'Separate each %s by comas', |
---|
28 | list_type: 'more', |
---|
29 | |
---|
30 | target: null, |
---|
31 | meta_type: null, |
---|
32 | meta_dialog: null, |
---|
33 | meta_field: null, |
---|
34 | submit_button: null, |
---|
35 | post_id: false, |
---|
36 | |
---|
37 | service_uri: 'services.php', |
---|
38 | |
---|
39 | displayMeta: function(type, post_id) { |
---|
40 | this.meta_type = type; |
---|
41 | this.post_id = post_id; |
---|
42 | this.target.empty(); |
---|
43 | |
---|
44 | this.meta_dialog = $('<input type="text" class="ib" />'); |
---|
45 | this.meta_dialog.attr('title', this.text_add_meta.replace(/%s/, this.meta_type)); |
---|
46 | this.meta_dialog.attr('id', 'post_meta_input'); |
---|
47 | // Meta dialog input |
---|
48 | this.meta_dialog.keypress(function(evt) { // We don't want to submit form! |
---|
49 | if (evt.keyCode == 13) { |
---|
50 | This.addMeta(this.value); |
---|
51 | return false; |
---|
52 | } |
---|
53 | return true; |
---|
54 | }); |
---|
55 | |
---|
56 | var This = this; |
---|
57 | |
---|
58 | this.submit_button = $('<input type="button" value="ok" class="ib meta-helper" />'); |
---|
59 | this.submit_button.click(function() { |
---|
60 | var v = This.meta_dialog.val(); |
---|
61 | This.addMeta(v); |
---|
62 | return false; |
---|
63 | }); |
---|
64 | |
---|
65 | this.addMetaDialog(); |
---|
66 | |
---|
67 | if (this.post_id == false) { |
---|
68 | this.target.append(this.meta_field); |
---|
69 | } |
---|
70 | this.displayMetaList(); |
---|
71 | }, |
---|
72 | |
---|
73 | displayMetaList: function() { |
---|
74 | var li; |
---|
75 | if (this.meta_list == undefined) { |
---|
76 | this.meta_list = $('<ul class="metaList"></ul>'); |
---|
77 | this.target.prepend(this.meta_list); |
---|
78 | } |
---|
79 | |
---|
80 | if (this.post_id == false) { |
---|
81 | var meta = this.splitMetaValues(this.meta_field.val()); |
---|
82 | |
---|
83 | this.meta_list.empty(); |
---|
84 | for (var i = 0; i < meta.length; i++) { |
---|
85 | li = $('<li>' + meta[i] + '</li>'); |
---|
86 | var a_remove = $('<button type="button" class="metaRemove meta-helper"><img src="images/trash.png" alt="remove" /></button>'); |
---|
87 | a_remove.get(0).caller = this; |
---|
88 | a_remove.get(0).meta_id = meta[i]; |
---|
89 | a_remove.click(function() { |
---|
90 | this.caller.removeMeta(this.meta_id); |
---|
91 | return false; |
---|
92 | }); |
---|
93 | li.prepend(' ').prepend(a_remove); |
---|
94 | this.meta_list.append(li); |
---|
95 | } |
---|
96 | } else { |
---|
97 | var This = this; |
---|
98 | var params = { |
---|
99 | f: 'getMeta', |
---|
100 | metaType: this.meta_type, |
---|
101 | sortby: 'metaId,asc', |
---|
102 | postId: this.post_id |
---|
103 | }; |
---|
104 | |
---|
105 | $.get(this.service_uri, params, function(data) { |
---|
106 | data = $(data); |
---|
107 | |
---|
108 | if (data.find('rsp').attr('status') != 'ok') { |
---|
109 | return; |
---|
110 | } |
---|
111 | |
---|
112 | This.meta_list.empty(); |
---|
113 | data.find('meta').each(function() { |
---|
114 | var meta_id = $(this).text(); |
---|
115 | li = $('<li><a href="' + This.meta_url + $(this).attr('uri') + '">' + meta_id + '</a></li>'); |
---|
116 | a_remove = $('<button type="button" class="metaRemove meta-helper"><img src="images/trash.png" alt="remove" /></button>'); |
---|
117 | a_remove.get(0).caller = This; |
---|
118 | a_remove.get(0).meta_id = meta_id; |
---|
119 | a_remove.click(function() { |
---|
120 | this.caller.removeMeta(this.meta_id); |
---|
121 | return false; |
---|
122 | }); |
---|
123 | li.prepend(' ').prepend(a_remove); |
---|
124 | This.meta_list.append(li); |
---|
125 | }); |
---|
126 | }); |
---|
127 | } |
---|
128 | }, |
---|
129 | |
---|
130 | addMetaDialog: function() { |
---|
131 | |
---|
132 | if (this.submit_button == null) { |
---|
133 | this.target.append($('<p></p>').append(this.meta_dialog)); |
---|
134 | } else { |
---|
135 | this.target.append($('<p></p>').append(this.meta_dialog).append(' ').append(this.submit_button)); |
---|
136 | } |
---|
137 | |
---|
138 | if (this.text_separation != '') { |
---|
139 | this.target.append($('<p></p>').addClass('form-note').append(this.text_separation.replace(/%s/, this.meta_type))); |
---|
140 | } |
---|
141 | |
---|
142 | this.showMetaList(this.list_type, this.target); |
---|
143 | |
---|
144 | }, |
---|
145 | |
---|
146 | showMetaList: function(list_type, target) { |
---|
147 | var params = { |
---|
148 | f: 'getMeta', |
---|
149 | metaType: this.meta_type, |
---|
150 | sortby: 'metaId,asc' |
---|
151 | }; |
---|
152 | |
---|
153 | if (list_type == 'more') { |
---|
154 | params.limit = '30'; |
---|
155 | } |
---|
156 | |
---|
157 | var This = this; |
---|
158 | |
---|
159 | $.get(this.service_uri, params, function(data) { |
---|
160 | |
---|
161 | var pl = $('<p class="addMeta"></p>'); |
---|
162 | |
---|
163 | $(target).find('.addMeta').remove(); |
---|
164 | |
---|
165 | if ($(data).find('meta').length > 0) { |
---|
166 | pl.empty(); |
---|
167 | var meta_link; |
---|
168 | |
---|
169 | $(data).find('meta').each(function(i) { |
---|
170 | meta_link = $('<button type="button" class="metaItem meta-helper">' + $(this).text() + '</button>'); |
---|
171 | meta_link.get(0).meta_id = $(this).text(); |
---|
172 | meta_link.click(function() { |
---|
173 | var v = This.splitMetaValues(This.meta_dialog.val() + ',' + this.meta_id); |
---|
174 | This.meta_dialog.val(v.join(',')); |
---|
175 | return false; |
---|
176 | }); |
---|
177 | |
---|
178 | if (i > 0) { |
---|
179 | pl.append(', '); |
---|
180 | } |
---|
181 | pl.append(meta_link); |
---|
182 | }); |
---|
183 | |
---|
184 | if (list_type == 'more') { |
---|
185 | var a_more = $('<button type="button" class="button metaGetMore meta-helper"></button>'); |
---|
186 | a_more.append(This.text_all + String.fromCharCode(160) + String.fromCharCode(187)); |
---|
187 | a_more.click(function() { |
---|
188 | This.showMetaList('more-all', target); |
---|
189 | return false; |
---|
190 | }); |
---|
191 | pl.append(', ').append(a_more); |
---|
192 | } |
---|
193 | |
---|
194 | if (list_type != 'more-all') { |
---|
195 | pl.addClass('hide'); |
---|
196 | |
---|
197 | var pa = $('<p></p>'); |
---|
198 | target.append(pa); |
---|
199 | |
---|
200 | var a = $('<button type="button" class="button metaGetList meta-helper">' + This.text_choose + '</button>'); |
---|
201 | a.click(function() { |
---|
202 | $(this).parent().next().removeClass('hide'); |
---|
203 | $(this).remove(); |
---|
204 | return false; |
---|
205 | }); |
---|
206 | |
---|
207 | pa.append(a); |
---|
208 | } |
---|
209 | |
---|
210 | target.append(pl); |
---|
211 | |
---|
212 | } else { |
---|
213 | pl.empty(); |
---|
214 | } |
---|
215 | }); |
---|
216 | }, |
---|
217 | |
---|
218 | addMeta: function(str) { |
---|
219 | str = this.splitMetaValues(str).join(','); |
---|
220 | if (this.post_id == false) { |
---|
221 | str = this.splitMetaValues(this.meta_field.val() + ',' + str); |
---|
222 | this.meta_field.val(str); |
---|
223 | |
---|
224 | this.meta_dialog.val(''); |
---|
225 | this.displayMetaList(); |
---|
226 | } else { |
---|
227 | var params = { |
---|
228 | xd_check: dotclear.nonce, |
---|
229 | f: 'setPostMeta', |
---|
230 | postId: this.post_id, |
---|
231 | metaType: this.meta_type, |
---|
232 | meta: str |
---|
233 | }; |
---|
234 | |
---|
235 | var This = this; |
---|
236 | $.post(this.service_uri, params, function(data) { |
---|
237 | if ($(data).find('rsp').attr('status') == 'ok') { |
---|
238 | This.meta_dialog.val(''); |
---|
239 | This.displayMetaList(); |
---|
240 | } else { |
---|
241 | window.alert($(data).find('message').text()); |
---|
242 | } |
---|
243 | }); |
---|
244 | } |
---|
245 | }, |
---|
246 | |
---|
247 | removeMeta: function(meta_id) { |
---|
248 | if (this.post_id == false) { |
---|
249 | var meta = this.splitMetaValues(this.meta_field.val()); |
---|
250 | for (var i = 0; i < meta.length; i++) { |
---|
251 | if (meta[i] == meta_id) { |
---|
252 | meta.splice(i, 1); |
---|
253 | break; |
---|
254 | } |
---|
255 | } |
---|
256 | this.meta_field.val(meta.join(',')); |
---|
257 | this.displayMetaList(); |
---|
258 | } else { |
---|
259 | var text_confirm_msg = this.text_confirm_remove.replace(/%s/, this.meta_type); |
---|
260 | |
---|
261 | if (window.confirm(text_confirm_msg)) { |
---|
262 | var This = this; |
---|
263 | var params = { |
---|
264 | xd_check: dotclear.nonce, |
---|
265 | f: 'delMeta', |
---|
266 | postId: this.post_id, |
---|
267 | metaId: meta_id, |
---|
268 | metaType: this.meta_type |
---|
269 | }; |
---|
270 | |
---|
271 | $.post(this.service_uri, params, function(data) { |
---|
272 | if ($(data).find('rsp').attr('status') == 'ok') { |
---|
273 | This.displayMetaList(); |
---|
274 | } else { |
---|
275 | window.alert($(data).find('message').text()); |
---|
276 | } |
---|
277 | }); |
---|
278 | } |
---|
279 | } |
---|
280 | }, |
---|
281 | |
---|
282 | splitMetaValues: function(str) { |
---|
283 | function inArray(needle, stack) { |
---|
284 | for (var i = 0; i < stack.length; i++) { |
---|
285 | if (stack[i] == needle) { |
---|
286 | return true; |
---|
287 | } |
---|
288 | } |
---|
289 | return false; |
---|
290 | } |
---|
291 | |
---|
292 | var res = []; |
---|
293 | var v = str.split(','); |
---|
294 | v.sort(); |
---|
295 | for (var i = 0; i < v.length; i++) { |
---|
296 | v[i] = v[i].replace(/^\s*/, '').replace(/\s*$/, ''); |
---|
297 | if (v[i] != '' && !inArray(v[i], res)) { |
---|
298 | res.push(v[i]); |
---|
299 | } |
---|
300 | } |
---|
301 | res.sort(); |
---|
302 | return res; |
---|
303 | } |
---|
304 | }; |
---|