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 | const This = this; |
---|
57 | |
---|
58 | this.submit_button = $('<input type="button" value="ok" class="ib meta-helper" />'); |
---|
59 | this.submit_button.click(function() { |
---|
60 | const 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 | let 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 | const meta = this.splitMetaValues(this.meta_field.val()); |
---|
82 | |
---|
83 | this.meta_list.empty(); |
---|
84 | for (let i = 0; i < meta.length; i++) { |
---|
85 | li = $('<li>' + meta[i] + '</li>'); |
---|
86 | const 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 | const This = this; |
---|
98 | const 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 | const meta_id = $(this).text(); |
---|
115 | li = $('<li><a href="' + This.meta_url + $(this).attr('uri') + '">' + meta_id + '</a></li>'); |
---|
116 | const 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 | const 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 | const This = this; |
---|
158 | |
---|
159 | $.get(this.service_uri, params, function(data) { |
---|
160 | |
---|
161 | const pl = $('<p class="addMeta"></p>'); |
---|
162 | |
---|
163 | $(target).find('.addMeta').remove(); |
---|
164 | |
---|
165 | if ($(data).find('meta').length > 0) { |
---|
166 | pl.empty(); |
---|
167 | |
---|
168 | $(data).find('meta').each(function(i) { |
---|
169 | const meta_link = $('<button type="button" class="metaItem meta-helper">' + $(this).text() + '</button>'); |
---|
170 | meta_link.get(0).meta_id = $(this).text(); |
---|
171 | meta_link.click(function() { |
---|
172 | const v = This.splitMetaValues(This.meta_dialog.val() + ',' + this.meta_id); |
---|
173 | This.meta_dialog.val(v.join(',')); |
---|
174 | return false; |
---|
175 | }); |
---|
176 | |
---|
177 | if (i > 0) { |
---|
178 | pl.append(', '); |
---|
179 | } |
---|
180 | pl.append(meta_link); |
---|
181 | }); |
---|
182 | |
---|
183 | if (list_type == 'more') { |
---|
184 | const a_more = $('<button type="button" class="button metaGetMore meta-helper"></button>'); |
---|
185 | a_more.append(This.text_all + String.fromCharCode(160) + String.fromCharCode(187)); |
---|
186 | a_more.click(function() { |
---|
187 | This.showMetaList('more-all', target); |
---|
188 | return false; |
---|
189 | }); |
---|
190 | pl.append(', ').append(a_more); |
---|
191 | } |
---|
192 | |
---|
193 | if (list_type != 'more-all') { |
---|
194 | pl.addClass('hide'); |
---|
195 | |
---|
196 | const pa = $('<p></p>'); |
---|
197 | target.append(pa); |
---|
198 | |
---|
199 | const a = $(`<button type="button" class="button metaGetList meta-helper">${This.text_choose}</button>`); |
---|
200 | a.click(function() { |
---|
201 | $(this).parent().next().removeClass('hide'); |
---|
202 | $(this).remove(); |
---|
203 | return false; |
---|
204 | }); |
---|
205 | |
---|
206 | pa.append(a); |
---|
207 | } |
---|
208 | |
---|
209 | target.append(pl); |
---|
210 | |
---|
211 | } else { |
---|
212 | pl.empty(); |
---|
213 | } |
---|
214 | }); |
---|
215 | }, |
---|
216 | |
---|
217 | addMeta: function(str) { |
---|
218 | str = this.splitMetaValues(str).join(','); |
---|
219 | if (this.post_id == false) { |
---|
220 | str = this.splitMetaValues(this.meta_field.val() + ',' + str); |
---|
221 | this.meta_field.val(str); |
---|
222 | |
---|
223 | this.meta_dialog.val(''); |
---|
224 | this.displayMetaList(); |
---|
225 | } else { |
---|
226 | const params = { |
---|
227 | xd_check: dotclear.nonce, |
---|
228 | f: 'setPostMeta', |
---|
229 | postId: this.post_id, |
---|
230 | metaType: this.meta_type, |
---|
231 | meta: str |
---|
232 | }; |
---|
233 | |
---|
234 | const This = this; |
---|
235 | $.post(this.service_uri, params, function(data) { |
---|
236 | if ($(data).find('rsp').attr('status') == 'ok') { |
---|
237 | This.meta_dialog.val(''); |
---|
238 | This.displayMetaList(); |
---|
239 | } else { |
---|
240 | window.alert($(data).find('message').text()); |
---|
241 | } |
---|
242 | }); |
---|
243 | } |
---|
244 | }, |
---|
245 | |
---|
246 | removeMeta: function(meta_id) { |
---|
247 | if (this.post_id == false) { |
---|
248 | let meta = this.splitMetaValues(this.meta_field.val()); |
---|
249 | for (let i = 0; i < meta.length; i++) { |
---|
250 | if (meta[i] == meta_id) { |
---|
251 | meta.splice(i, 1); |
---|
252 | break; |
---|
253 | } |
---|
254 | } |
---|
255 | this.meta_field.val(meta.join(',')); |
---|
256 | this.displayMetaList(); |
---|
257 | } else { |
---|
258 | const text_confirm_msg = this.text_confirm_remove.replace(/%s/, this.meta_type); |
---|
259 | |
---|
260 | if (window.confirm(text_confirm_msg)) { |
---|
261 | const This = this; |
---|
262 | const params = { |
---|
263 | xd_check: dotclear.nonce, |
---|
264 | f: 'delMeta', |
---|
265 | postId: this.post_id, |
---|
266 | metaId: meta_id, |
---|
267 | metaType: this.meta_type |
---|
268 | }; |
---|
269 | |
---|
270 | $.post(this.service_uri, params, function(data) { |
---|
271 | if ($(data).find('rsp').attr('status') == 'ok') { |
---|
272 | This.displayMetaList(); |
---|
273 | } else { |
---|
274 | window.alert($(data).find('message').text()); |
---|
275 | } |
---|
276 | }); |
---|
277 | } |
---|
278 | } |
---|
279 | }, |
---|
280 | |
---|
281 | splitMetaValues: function(str) { |
---|
282 | function inArray(needle, stack) { |
---|
283 | for (var i = 0; i < stack.length; i++) { |
---|
284 | if (stack[i] == needle) { |
---|
285 | return true; |
---|
286 | } |
---|
287 | } |
---|
288 | return false; |
---|
289 | } |
---|
290 | |
---|
291 | let res = []; |
---|
292 | let v = str.split(','); |
---|
293 | v.sort(); |
---|
294 | for (let i = 0; i < v.length; i++) { |
---|
295 | v[i] = v[i].replace(/^\s*/, '').replace(/\s*$/, ''); |
---|
296 | if (v[i] != '' && !inArray(v[i], res)) { |
---|
297 | res.push(v[i]); |
---|
298 | } |
---|
299 | } |
---|
300 | res.sort(); |
---|
301 | return res; |
---|
302 | } |
---|
303 | }; |
---|