| 1 | //~ metaEditor & metaEditor.prototype should go to the core. |
|---|
| 2 | function metaEditor(target,meta_field,meta_type,meta_options) { |
|---|
| 3 | this.target = target; |
|---|
| 4 | this.meta_field = meta_field; |
|---|
| 5 | this.meta_type = meta_type; |
|---|
| 6 | |
|---|
| 7 | // options |
|---|
| 8 | meta_options = meta_options || {}; |
|---|
| 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; |
|---|
| 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', |
|---|
| 25 | list_type: 'more', |
|---|
| 26 | |
|---|
| 27 | target: null, |
|---|
| 28 | meta_type: null, |
|---|
| 29 | meta_dialog: null, |
|---|
| 30 | meta_field: null, |
|---|
| 31 | submit_button: null, |
|---|
| 32 | post_id: false, |
|---|
| 33 | |
|---|
| 34 | service_uri: 'services.php', |
|---|
| 35 | |
|---|
| 36 | displayMeta: function(type,post_id) { |
|---|
| 37 | this.meta_type = type; |
|---|
| 38 | this.post_id = post_id; |
|---|
| 39 | this.target.empty(); |
|---|
| 40 | |
|---|
| 41 | this.meta_dialog = $('<input type="text" class="ib" />'); |
|---|
| 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 | }); |
|---|
| 52 | |
|---|
| 53 | var This = this; |
|---|
| 54 | |
|---|
| 55 | this.submit_button = $('<input type="button" value="ok" class="ib meta-helper" />'); |
|---|
| 56 | this.submit_button.click(function() { |
|---|
| 57 | var v = This.meta_dialog.val(); |
|---|
| 58 | This.addMeta(v); |
|---|
| 59 | return false; |
|---|
| 60 | }); |
|---|
| 61 | |
|---|
| 62 | this.addMetaDialog(); |
|---|
| 63 | |
|---|
| 64 | if (this.post_id == false) { |
|---|
| 65 | this.target.append(this.meta_field); |
|---|
| 66 | } |
|---|
| 67 | this.displayMetaList(); |
|---|
| 68 | }, |
|---|
| 69 | |
|---|
| 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 | } |
|---|
| 76 | |
|---|
| 77 | if (this.post_id == false) { |
|---|
| 78 | var meta = this.splitMetaValues(this.meta_field.val()); |
|---|
| 79 | |
|---|
| 80 | this.meta_list.empty(); |
|---|
| 81 | for (var i=0; i < meta.length; i++) { |
|---|
| 82 | li = $('<li>'+meta[i]+'</li>'); |
|---|
| 83 | a_remove = $('<button type="button" class="metaRemove meta-helper"><img src="images/trash.png" alt="remove" /></button>'); |
|---|
| 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 | }); |
|---|
| 90 | li.prepend(' ').prepend(a_remove); |
|---|
| 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 | }; |
|---|
| 101 | |
|---|
| 102 | $.get(this.service_uri,params,function(data) { |
|---|
| 103 | data = $(data); |
|---|
| 104 | |
|---|
| 105 | if (data.find('rsp').attr('status') != 'ok') { return; } |
|---|
| 106 | |
|---|
| 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>'); |
|---|
| 111 | a_remove = $('<button type="button" class="metaRemove meta-helper"><img src="images/trash.png" alt="remove" /></button>'); |
|---|
| 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 | }); |
|---|
| 118 | li.prepend(' ').prepend(a_remove); |
|---|
| 119 | This.meta_list.append(li); |
|---|
| 120 | }); |
|---|
| 121 | }); |
|---|
| 122 | } |
|---|
| 123 | }, |
|---|
| 124 | |
|---|
| 125 | addMetaDialog: function() { |
|---|
| 126 | |
|---|
| 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 | } |
|---|
| 132 | |
|---|
| 133 | if (this.text_separation != '') { |
|---|
| 134 | this.target.append($('<p></p>').addClass('form-note').append(this.text_separation.replace(/%s/,this.meta_type))); |
|---|
| 135 | } |
|---|
| 136 | |
|---|
| 137 | this.showMetaList(this.list_type,this.target); |
|---|
| 138 | |
|---|
| 139 | }, |
|---|
| 140 | |
|---|
| 141 | showMetaList: function(list_type,target) { |
|---|
| 142 | var params = { |
|---|
| 143 | f: 'getMeta', |
|---|
| 144 | metaType: this.meta_type, |
|---|
| 145 | sortby: 'metaId,asc' |
|---|
| 146 | }; |
|---|
| 147 | |
|---|
| 148 | if (list_type == 'more') { |
|---|
| 149 | params.limit = '30'; |
|---|
| 150 | } |
|---|
| 151 | |
|---|
| 152 | var This = this; |
|---|
| 153 | |
|---|
| 154 | $.get(this.service_uri,params,function(data) { |
|---|
| 155 | |
|---|
| 156 | var pl = $('<p class="addMeta"></p>'); |
|---|
| 157 | |
|---|
| 158 | $(target).find('.addMeta').remove(); |
|---|
| 159 | |
|---|
| 160 | if ($(data).find('meta').length > 0) { |
|---|
| 161 | pl.empty(); |
|---|
| 162 | var meta_link; |
|---|
| 163 | |
|---|
| 164 | $(data).find('meta').each(function(i) { |
|---|
| 165 | meta_link = $('<button type="button" class="metaItem meta-helper">' + $(this).text() + '</button>'); |
|---|
| 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 | }); |
|---|
| 172 | |
|---|
| 173 | if (i>0) { |
|---|
| 174 | pl.append(', '); |
|---|
| 175 | } |
|---|
| 176 | pl.append(meta_link); |
|---|
| 177 | }); |
|---|
| 178 | |
|---|
| 179 | if (list_type == 'more') { |
|---|
| 180 | var a_more = $('<button type="button" class="button metaGetMore meta-helper"></button>'); |
|---|
| 181 | a_more.append(This.text_all + String.fromCharCode(160)+String.fromCharCode(187)); |
|---|
| 182 | a_more.click(function() { |
|---|
| 183 | This.showMetaList('more-all',target); |
|---|
| 184 | return false; |
|---|
| 185 | }); |
|---|
| 186 | pl.append(', ').append(a_more); |
|---|
| 187 | } |
|---|
| 188 | |
|---|
| 189 | if (list_type != 'more-all') { |
|---|
| 190 | pl.addClass('hide'); |
|---|
| 191 | |
|---|
| 192 | var pa = $('<p></p>'); |
|---|
| 193 | target.append(pa); |
|---|
| 194 | |
|---|
| 195 | var a = $('<button type="button" class="button metaGetList meta-helper">' + This.text_choose + '</button>'); |
|---|
| 196 | a.click(function() { |
|---|
| 197 | $(this).parent().next().removeClass('hide'); |
|---|
| 198 | $(this).remove(); |
|---|
| 199 | return false; |
|---|
| 200 | }); |
|---|
| 201 | |
|---|
| 202 | pa.append(a); |
|---|
| 203 | } |
|---|
| 204 | |
|---|
| 205 | target.append(pl); |
|---|
| 206 | |
|---|
| 207 | } else { |
|---|
| 208 | pl.empty(); |
|---|
| 209 | } |
|---|
| 210 | }); |
|---|
| 211 | }, |
|---|
| 212 | |
|---|
| 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); |
|---|
| 218 | |
|---|
| 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 | }; |
|---|
| 229 | |
|---|
| 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 | }, |
|---|
| 241 | |
|---|
| 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); |
|---|
| 255 | |
|---|
| 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 | }; |
|---|
| 265 | |
|---|
| 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 | }, |
|---|
| 276 | |
|---|
| 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 | } |
|---|
| 286 | |
|---|
| 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 | } |
|---|
| 299 | }; |
|---|