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