[3706] | 1 | /*global $, dotclear */ |
---|
| 2 | 'use strict'; |
---|
| 3 | |
---|
[175] | 4 | //~ metaEditor & metaEditor.prototype should go to the core. |
---|
[3706] | 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; |
---|
[2559] | 9 | |
---|
[3706] | 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 | } |
---|
[175] | 20 | |
---|
| 21 | metaEditor.prototype = { |
---|
[3706] | 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', |
---|
[2545] | 29 | |
---|
[3706] | 30 | target: null, |
---|
| 31 | meta_type: null, |
---|
| 32 | meta_dialog: null, |
---|
| 33 | meta_field: null, |
---|
| 34 | submit_button: null, |
---|
| 35 | post_id: false, |
---|
[2545] | 36 | |
---|
[3706] | 37 | service_uri: 'services.php', |
---|
[2545] | 38 | |
---|
[3706] | 39 | displayMeta: function(type, post_id) { |
---|
| 40 | this.meta_type = type; |
---|
| 41 | this.post_id = post_id; |
---|
| 42 | this.target.empty(); |
---|
[2545] | 43 | |
---|
[3706] | 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 | }); |
---|
[2545] | 55 | |
---|
[3880] | 56 | const This = this; |
---|
[2545] | 57 | |
---|
[3706] | 58 | this.submit_button = $('<input type="button" value="ok" class="ib meta-helper" />'); |
---|
| 59 | this.submit_button.click(function() { |
---|
[3880] | 60 | const v = This.meta_dialog.val(); |
---|
[3706] | 61 | This.addMeta(v); |
---|
| 62 | return false; |
---|
| 63 | }); |
---|
[2545] | 64 | |
---|
[3706] | 65 | this.addMetaDialog(); |
---|
[2545] | 66 | |
---|
[3706] | 67 | if (this.post_id == false) { |
---|
| 68 | this.target.append(this.meta_field); |
---|
| 69 | } |
---|
| 70 | this.displayMetaList(); |
---|
| 71 | }, |
---|
[2545] | 72 | |
---|
[3706] | 73 | displayMetaList: function() { |
---|
[3880] | 74 | let li; |
---|
[3706] | 75 | if (this.meta_list == undefined) { |
---|
| 76 | this.meta_list = $('<ul class="metaList"></ul>'); |
---|
| 77 | this.target.prepend(this.meta_list); |
---|
| 78 | } |
---|
[2545] | 79 | |
---|
[3706] | 80 | if (this.post_id == false) { |
---|
[3880] | 81 | const meta = this.splitMetaValues(this.meta_field.val()); |
---|
[2545] | 82 | |
---|
[3706] | 83 | this.meta_list.empty(); |
---|
[3880] | 84 | for (let i = 0; i < meta.length; i++) { |
---|
[3706] | 85 | li = $('<li>' + meta[i] + '</li>'); |
---|
[3880] | 86 | const a_remove = $('<button type="button" class="metaRemove meta-helper"><img src="images/trash.png" alt="remove" /></button>'); |
---|
[3706] | 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 { |
---|
[3880] | 97 | const This = this; |
---|
| 98 | const params = { |
---|
[3706] | 99 | f: 'getMeta', |
---|
| 100 | metaType: this.meta_type, |
---|
| 101 | sortby: 'metaId,asc', |
---|
| 102 | postId: this.post_id |
---|
| 103 | }; |
---|
[2545] | 104 | |
---|
[3706] | 105 | $.get(this.service_uri, params, function(data) { |
---|
| 106 | data = $(data); |
---|
[2545] | 107 | |
---|
[3706] | 108 | if (data.find('rsp').attr('status') != 'ok') { |
---|
| 109 | return; |
---|
| 110 | } |
---|
[2545] | 111 | |
---|
[3706] | 112 | This.meta_list.empty(); |
---|
| 113 | data.find('meta').each(function() { |
---|
[3880] | 114 | const meta_id = $(this).text(); |
---|
[3706] | 115 | li = $('<li><a href="' + This.meta_url + $(this).attr('uri') + '">' + meta_id + '</a></li>'); |
---|
[3880] | 116 | const a_remove = $('<button type="button" class="metaRemove meta-helper"><img src="images/trash.png" alt="remove" /></button>'); |
---|
[3706] | 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 | }, |
---|
[2545] | 129 | |
---|
[3706] | 130 | addMetaDialog: function() { |
---|
[2545] | 131 | |
---|
[3706] | 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 | } |
---|
[2545] | 137 | |
---|
[3706] | 138 | if (this.text_separation != '') { |
---|
| 139 | this.target.append($('<p></p>').addClass('form-note').append(this.text_separation.replace(/%s/, this.meta_type))); |
---|
| 140 | } |
---|
[2545] | 141 | |
---|
[3706] | 142 | this.showMetaList(this.list_type, this.target); |
---|
[2545] | 143 | |
---|
[3706] | 144 | }, |
---|
[2545] | 145 | |
---|
[3706] | 146 | showMetaList: function(list_type, target) { |
---|
[3880] | 147 | const params = { |
---|
[3706] | 148 | f: 'getMeta', |
---|
| 149 | metaType: this.meta_type, |
---|
| 150 | sortby: 'metaId,asc' |
---|
| 151 | }; |
---|
[2545] | 152 | |
---|
[3706] | 153 | if (list_type == 'more') { |
---|
| 154 | params.limit = '30'; |
---|
| 155 | } |
---|
[2545] | 156 | |
---|
[3880] | 157 | const This = this; |
---|
[2545] | 158 | |
---|
[3706] | 159 | $.get(this.service_uri, params, function(data) { |
---|
[2545] | 160 | |
---|
[3880] | 161 | const pl = $('<p class="addMeta"></p>'); |
---|
[2545] | 162 | |
---|
[3706] | 163 | $(target).find('.addMeta').remove(); |
---|
[2545] | 164 | |
---|
[3706] | 165 | if ($(data).find('meta').length > 0) { |
---|
| 166 | pl.empty(); |
---|
[2545] | 167 | |
---|
[3706] | 168 | $(data).find('meta').each(function(i) { |
---|
[3880] | 169 | const meta_link = $('<button type="button" class="metaItem meta-helper">' + $(this).text() + '</button>'); |
---|
[3706] | 170 | meta_link.get(0).meta_id = $(this).text(); |
---|
| 171 | meta_link.click(function() { |
---|
[3880] | 172 | const v = This.splitMetaValues(This.meta_dialog.val() + ',' + this.meta_id); |
---|
[3706] | 173 | This.meta_dialog.val(v.join(',')); |
---|
| 174 | return false; |
---|
| 175 | }); |
---|
[2545] | 176 | |
---|
[3706] | 177 | if (i > 0) { |
---|
| 178 | pl.append(', '); |
---|
| 179 | } |
---|
| 180 | pl.append(meta_link); |
---|
| 181 | }); |
---|
[2545] | 182 | |
---|
[3706] | 183 | if (list_type == 'more') { |
---|
[3880] | 184 | const a_more = $('<button type="button" class="button metaGetMore meta-helper"></button>'); |
---|
[3706] | 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 | } |
---|
[2545] | 192 | |
---|
[3706] | 193 | if (list_type != 'more-all') { |
---|
| 194 | pl.addClass('hide'); |
---|
[2545] | 195 | |
---|
[3880] | 196 | const pa = $('<p></p>'); |
---|
[3706] | 197 | target.append(pa); |
---|
[2545] | 198 | |
---|
[3880] | 199 | const a = $(`<button type="button" class="button metaGetList meta-helper">${This.text_choose}</button>`); |
---|
[3706] | 200 | a.click(function() { |
---|
| 201 | $(this).parent().next().removeClass('hide'); |
---|
| 202 | $(this).remove(); |
---|
| 203 | return false; |
---|
| 204 | }); |
---|
[2545] | 205 | |
---|
[3706] | 206 | pa.append(a); |
---|
| 207 | } |
---|
[2545] | 208 | |
---|
[3706] | 209 | target.append(pl); |
---|
[2545] | 210 | |
---|
[3706] | 211 | } else { |
---|
| 212 | pl.empty(); |
---|
| 213 | } |
---|
| 214 | }); |
---|
| 215 | }, |
---|
[2545] | 216 | |
---|
[3706] | 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); |
---|
[2545] | 222 | |
---|
[3706] | 223 | this.meta_dialog.val(''); |
---|
| 224 | this.displayMetaList(); |
---|
| 225 | } else { |
---|
[3880] | 226 | const params = { |
---|
[3706] | 227 | xd_check: dotclear.nonce, |
---|
| 228 | f: 'setPostMeta', |
---|
| 229 | postId: this.post_id, |
---|
| 230 | metaType: this.meta_type, |
---|
| 231 | meta: str |
---|
| 232 | }; |
---|
[2545] | 233 | |
---|
[3880] | 234 | const This = this; |
---|
[3706] | 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 | }, |
---|
[2545] | 245 | |
---|
[3706] | 246 | removeMeta: function(meta_id) { |
---|
| 247 | if (this.post_id == false) { |
---|
[3880] | 248 | let meta = this.splitMetaValues(this.meta_field.val()); |
---|
| 249 | for (let i = 0; i < meta.length; i++) { |
---|
[3706] | 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 { |
---|
[3880] | 258 | const text_confirm_msg = this.text_confirm_remove.replace(/%s/, this.meta_type); |
---|
[2545] | 259 | |
---|
[3706] | 260 | if (window.confirm(text_confirm_msg)) { |
---|
[3880] | 261 | const This = this; |
---|
| 262 | const params = { |
---|
[3706] | 263 | xd_check: dotclear.nonce, |
---|
| 264 | f: 'delMeta', |
---|
| 265 | postId: this.post_id, |
---|
| 266 | metaId: meta_id, |
---|
| 267 | metaType: this.meta_type |
---|
| 268 | }; |
---|
[2545] | 269 | |
---|
[3706] | 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 | }, |
---|
[2545] | 280 | |
---|
[3706] | 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 | } |
---|
[2545] | 290 | |
---|
[3880] | 291 | let res = []; |
---|
| 292 | let v = str.split(','); |
---|
[3706] | 293 | v.sort(); |
---|
[3880] | 294 | for (let i = 0; i < v.length; i++) { |
---|
[3706] | 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 | } |
---|
[2545] | 303 | }; |
---|