//~ metaEditor & metaEditor.prototype should go to the core.
function metaEditor(target,meta_field,meta_type) {
this.target = target;
this.meta_field = meta_field;
this.meta_type = meta_type;
};
metaEditor.prototype = {
meta_url: '',
text_confirm_remove: 'Are you sure you want to remove this %s?',
text_add_meta: 'Add a %s to this entry',
text_choose: 'Choose from list',
text_all: 'all',
text_separation: 'Separate each %s by comas',
target: null,
meta_type: null,
meta_dialog: null,
meta_field: null,
submit_button: null,
post_id: false,
service_uri: 'services.php',
displayMeta: function(type,post_id) {
this.meta_type = type;
this.post_id = post_id;
this.target.empty();
this.meta_dialog = $('');
this.meta_dialog.attr('title',this.text_add_meta.replace(/%s/,this.meta_type));
this.meta_dialog.attr('id','post_meta_input');
// Meta dialog input
this.meta_dialog.keypress(function(evt) { // We don't want to submit form!
if (evt.keyCode == 13) {
This.addMeta(this.value);
return false;
}
return true;
});
var This = this;
this.submit_button = $('');
this.submit_button.click(function() {
var v = This.meta_dialog.val();
This.addMeta(v);
return false;
});
this.addMetaDialog();
if (this.post_id == false) {
this.target.append(this.meta_field);
}
this.displayMetaList();
},
displayMetaList: function() {
var li;
if (this.meta_list == undefined) {
this.meta_list = $('
');
this.target.prepend(this.meta_list);
}
if (this.post_id == false) {
var meta = this.splitMetaValues(this.meta_field.val());
this.meta_list.empty();
for (var i=0; i'+meta[i]+'');
a_remove = $('[x]');
a_remove.get(0).caller = this;
a_remove.get(0).meta_id = meta[i];
a_remove.click(function() {
this.caller.removeMeta(this.meta_id);
return false;
});
li.append(' ').append(a_remove);
this.meta_list.append(li);
}
} else {
var This = this;
var params = {
f: 'getMeta',
metaType: this.meta_type,
sortby: 'metaId,asc',
postId: this.post_id
};
$.get(this.service_uri,params,function(data) {
data = $(data);
if (data.find('rsp').attr('status') != 'ok') { return; }
This.meta_list.empty();
data.find('meta').each(function() {
var meta_id = $(this).text();
li = $(''+meta_id+'');
a_remove = $('[x]');
a_remove.get(0).caller = This;
a_remove.get(0).meta_id = meta_id;
a_remove.click(function() {
this.caller.removeMeta(this.meta_id);
return false;
});
li.append(' ').append(a_remove);
This.meta_list.append(li);
});
});
}
},
addMetaDialog: function() {
var This = this;
if (this.submit_button == null) {
this.target.append($('').append(this.meta_dialog));
} else {
this.target.append($('').append(this.meta_dialog).append(' ').append(this.submit_button));
}
// View meta list
var a = $('' + this.text_choose + '');
a.click(function() {
This.showMetaList(metaEditor.prototype.meta_type,$(this).parent());
return false;
});
if (this.text_separation != '') {
this.target.append($('').addClass('form-note').append(this.text_separation.replace(/%s/,this.meta_type)));
}
this.target.append($('').append(a));
},
showMetaList: function(type,target) {
target.empty();
target.append('...');
target.addClass('addMeta');
var params = {
f: 'getMeta',
metaType: this.meta_type,
sortby: 'metaId,asc'
};
if (type == 'more') {
params.limit = '30';
}
var This = this;
$.get(this.service_uri,params,function(data) {
if ($(data).find('meta').length > 0) {
target.empty();
var meta_link;
$(data).find('meta').each(function(i) {
meta_link = $('' + $(this).text() + '');
meta_link.get(0).meta_id = $(this).text();
meta_link.click(function() {
var v = This.splitMetaValues(This.meta_dialog.val() + ',' + this.meta_id);
This.meta_dialog.val(v.join(','));
return false;
});
if (i>0) {
target.append(', ');
}
target.append(meta_link);
});
if (type == 'more') {
var a_more = $('');
a_more.append(This.text_all + String.fromCharCode(160)+String.fromCharCode(187));
a_more.click(function() {
This.showMetaList('all',target);
return false;
});
target.append(', ').append(a_more);
}
} else {
target.empty();
}
});
},
addMeta: function(str) {
str = this.splitMetaValues(str).join(',');
if (this.post_id == false) {
str = this.splitMetaValues(this.meta_field.val() + ',' + str);
this.meta_field.val(str);
this.meta_dialog.val('');
this.displayMetaList();
} else {
var params = {
xd_check: dotclear.nonce,
f: 'setPostMeta',
postId: this.post_id,
metaType: this.meta_type,
meta: str
};
var This = this;
$.post(this.service_uri,params,function(data) {
if ($(data).find('rsp').attr('status') == 'ok') {
This.meta_dialog.val('');
This.displayMetaList();
} else {
alert($(data).find('message').text());
}
});
}
},
removeMeta: function(meta_id) {
if (this.post_id == false) {
var meta = this.splitMetaValues(this.meta_field.val());
for (var i=0; i