Dotclear

source: admin/js/meta-editor.js @ 1920:0bddb3c72fe9

Revision 1920:0bddb3c72fe9, 6.9 KB checked in by franck <carnet.franck.paul@…>, 12 years ago (diff)

Cope with more than one client ! Thank's lipki for patch

Line 
1//~ metaEditor & metaEditor.prototype should go to the core.
2function metaEditor(target,meta_field,meta_type) {
3     this.target = target;
4     this.meta_field = meta_field;
5     this.meta_type = meta_type;
6};
7
8metaEditor.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         
30          this.meta_dialog = $('<input type="text" class="ib" />');
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         
44          this.submit_button = $('<input type="button" value="ok" class="ib" />');
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('&nbsp;').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('&nbsp;').append(a_remove);
108                         This.meta_list.append(li);
109                    });
110               });
111          }
112     },
113     
114     addMetaDialog: function() {
115         
116          if (this.submit_button == null) {
117               this.target.append($('<p></p>').append(this.meta_dialog));
118          } else {
119               this.target.append($('<p></p>').append(this.meta_dialog).append(' ').append(this.submit_button));
120          }
121         
122          if (this.text_separation != '') {
123               this.target.append($('<p></p>').addClass('form-note').append(this.text_separation.replace(/%s/,this.meta_type)));
124          }
125         
126          this.showMetaList(metaEditor.prototype.meta_type,this.target);
127         
128     },
129     
130     showMetaList: function(type,target) {
131         
132          var params = {
133               f: 'getMeta',
134               metaType: this.meta_type,
135               sortby: 'metaId,asc'
136          };
137         
138          if (type == 'more') {
139               params.limit = '30';
140          }
141         
142          var This = this;
143         
144          $.get(this.service_uri,params,function(data) {
145               
146               var pl = $('<p class="addMeta"></p>');
147               
148               $(target).find('.addMeta').remove();
149               
150               if ($(data).find('meta').length > 0) {
151                    pl.empty();
152                    var meta_link;
153                   
154                    $(data).find('meta').each(function(i) {
155                         meta_link = $('<a href="#">' + $(this).text() + '</a>');
156                         meta_link.get(0).meta_id = $(this).text();
157                         meta_link.click(function() {
158                              var v = This.splitMetaValues(This.meta_dialog.val() + ',' + this.meta_id);
159                              This.meta_dialog.val(v.join(','));
160                              return false;
161                         });
162                         
163                         if (i>0) {
164                              pl.append(', ');
165                         }
166                         pl.append(meta_link);
167                    });
168                   
169                    if (type == 'more') {
170                         var a_more = $('<a href="#" class="metaGetMore"></a>');
171                         a_more.append(This.text_all + String.fromCharCode(160)+String.fromCharCode(187));
172                         a_more.click(function() {
173                              This.showMetaList('all',target);
174                              return false;
175                         });
176                         pl.append(', ').append(a_more);
177                         
178                         pl.addClass('hide');
179                         
180                         var pa = $('<p></p>');
181                         target.append(pa);
182                         
183                         var a = $('<a href="#" class="metaGetList">' + This.text_choose + '</a>');
184                         a.click(function() {
185                              $(this).parent().next().removeClass('hide');
186                              $(this).remove();
187                              return false;
188                         });
189                         
190                         pa.append(a);
191                    }
192                   
193                    target.append(pl);
194                   
195               } else {
196                    pl.empty();
197               }
198          });
199     },
200     
201     addMeta: function(str) {
202          str = this.splitMetaValues(str).join(',');
203          if (this.post_id == false) {
204               str = this.splitMetaValues(this.meta_field.val() + ',' + str);
205               this.meta_field.val(str);
206               
207               this.meta_dialog.val('');
208               this.displayMetaList();
209          } else {
210               var params = {
211                    xd_check: dotclear.nonce,
212                    f: 'setPostMeta',
213                    postId: this.post_id,
214                    metaType: this.meta_type,
215                    meta: str
216               };
217               
218               var This = this;
219               $.post(this.service_uri,params,function(data) {
220                    if ($(data).find('rsp').attr('status') == 'ok') {
221                         This.meta_dialog.val('');
222                         This.displayMetaList();
223                    } else {
224                         alert($(data).find('message').text());
225                    }
226               });
227          }
228     },
229     
230     removeMeta: function(meta_id) {
231          if (this.post_id == false) {
232               var meta = this.splitMetaValues(this.meta_field.val());
233               for (var i=0; i<meta.length; i++) {
234                    if (meta[i] == meta_id) {
235                         meta.splice(i,1);
236                         break;
237                    }
238               }
239               this.meta_field.val(meta.join(','));
240               this.displayMetaList();
241          } else {
242               var text_confirm_msg = this.text_confirm_remove.replace(/%s/,this.meta_type);
243               
244               if (window.confirm(text_confirm_msg)) {
245                    var This = this;
246                    var params = {
247                         xd_check: dotclear.nonce,
248                         f: 'delMeta',
249                         postId: this.post_id,
250                         metaId: meta_id,
251                         metaType: this.meta_type
252                    };
253                   
254                    $.post(this.service_uri,params,function(data) {
255                         if ($(data).find('rsp').attr('status') == 'ok') {
256                              This.displayMetaList();
257                         } else {
258                              alert($(data).find('message').text());
259                         }
260                    });
261               }
262          }
263     },
264     
265     splitMetaValues: function(str) {
266          function inArray(needle,stack) {
267               for (var i=0; i<stack.length; i++) {
268                    if (stack[i] == needle) {
269                         return true;
270                    }
271               }
272               return false;
273          }
274         
275          var res = new Array();
276          var v = str.split(',');
277          v.sort();
278          for (var i=0; i<v.length; i++) {
279               v[i] = v[i].replace(/^\s*/,'').replace(/\s*$/,'');
280               if (v[i] != '' && !inArray(v[i],res)) {
281                    res.push(v[i]);
282               }
283          }
284          res.sort();
285          return res;
286     }
287};
Note: See TracBrowser for help on using the repository browser.

Sites map