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