63 $.fn.editable =
function(target, options) {
65 if (
'disable' == target) {
66 $(
this).data(
'disabled.editable',
true);
69 if (
'enable' == target) {
70 $(
this).data(
'disabled.editable',
false);
73 if (
'destroy' == target) {
75 .unbind($(
this).data(
'event.editable'))
76 .removeData(
'disabled.editable')
77 .removeData(
'event.editable');
81 var settings = $.extend({}, $.fn.editable.defaults, {target:target}, options);
84 var plugin = $.editable.types[settings.type].plugin ||
function() { };
85 var submit = $.editable.types[settings.type].submit ||
function() { };
86 var buttons = $.editable.types[settings.type].buttons
87 || $.editable.types[
'defaults'].buttons;
88 var content = $.editable.types[settings.type].content
89 || $.editable.types[
'defaults'].content;
90 var element = $.editable.types[settings.type].element
91 || $.editable.types[
'defaults'].element;
92 var reset = $.editable.types[settings.type].reset
93 || $.editable.types[
'defaults'].reset;
94 var callback = settings.callback ||
function() { };
95 var onedit = settings.onedit ||
function() { };
96 var onsubmit = settings.onsubmit ||
function() { };
97 var onreset = settings.onreset ||
function() { };
98 var onerror = settings.onerror || reset;
101 if (settings.tooltip) {
102 $(
this).attr(
'title', settings.tooltip);
105 settings.autowidth =
'auto' == settings.width;
106 settings.autoheight =
'auto' == settings.height;
108 return this.each(
function() {
115 var savedwidth = $(
self).width();
116 var savedheight = $(
self).height();
119 $(
this).data(
'event.editable', settings.event);
122 if (!$.
trim($(
this).html())) {
123 $(
this).html(settings.placeholder);
126 $(
this).bind(settings.event,
function(e) {
129 if (
true === $(
this).data(
'disabled.editable')) {
139 if (
false === onedit.apply(
this, [settings,
self])) {
148 if (settings.tooltip) {
149 $(
self).removeAttr(
'title');
154 if (0 == $(
self).width()) {
156 settings.width = savedwidth;
157 settings.height = savedheight;
159 if (settings.width !=
'none') {
161 settings.autowidth ? $(
self).width() : settings.width;
163 if (settings.height !=
'none') {
165 settings.autoheight ? $(
self).height() : settings.height;
171 if ($(
this).html().toLowerCase().replace(/(;|
")/g, '') == 172 settings.placeholder.toLowerCase().replace(/(;|")/g,
'')) {
177 self.revert = $(
self).html();
178 self.origin = $(
self).text();
182 var form = $(
'<form />');
185 if (settings.cssclass) {
186 if (
'inherit' == settings.cssclass) {
187 form.attr(
'class', $(
self).attr(
'class'));
189 form.attr(
'class', settings.cssclass);
193 if (settings.style) {
194 if (
'inherit' == settings.style) {
195 form.attr(
'style', $(
self).attr(
'style'));
197 form.css(
'display', $(
self).css(
'display'));
199 form.attr(
'style', settings.style);
204 var input = element.apply(form, [settings,
self]);
209 if (settings.loadurl) {
210 var t = setTimeout(
function() {
211 input.disabled =
true;
212 content.apply(form, [settings.loadtext, settings,
self]);
216 loaddata[settings.id] =
self.id;
217 if ($.isFunction(settings.loaddata)) {
218 $.extend(loaddata, settings.loaddata.apply(
self, [
self.revert, settings]));
220 $.extend(loaddata, settings.loaddata);
223 type : settings.loadtype,
224 url : settings.loadurl,
227 success:
function(result) {
228 window.clearTimeout(t);
229 input_content = result;
230 input.disabled =
false;
233 }
else if (settings.data) {
234 input_content = settings.data;
235 if ($.isFunction(settings.data)) {
236 input_content = settings.data.apply(
self, [
self.revert, settings]);
239 input_content =
self.origin;
241 content.apply(form, [input_content, settings,
self]);
243 input.attr(
'name', settings.name);
246 buttons.apply(form, [settings,
self]);
249 $(
self).append(form);
252 plugin.apply(form, [settings,
self]);
255 $(
':input:visible:enabled:first', form).focus();
258 if (settings.select) {
263 input.keydown(
function(e) {
264 if (e.keyCode == 27) {
267 reset.apply(form, [settings, self]);
274 if (
'cancel' == settings.onblur) {
275 input.blur(
function(e) {
277 t = setTimeout(
function() {
278 reset.apply(form, [settings,
self]);
281 }
else if (
'submit' == settings.onblur) {
282 input.blur(
function(e) {
284 t = setTimeout(
function() {
288 }
else if ($.isFunction(settings.onblur)) {
289 input.blur(
function(e) {
290 settings.onblur.apply(
self, [input.val(), settings]);
293 input.blur(
function(e) {
298 form.submit(
function(e) {
309 if (
false !== onsubmit.apply(form, [settings,
self])) {
312 if (
false !== submit.apply(form, [settings,
self])) {
315 if ($.isFunction(settings.target)) {
316 var str = settings.target.apply(
self, [input.val(), settings]);
318 self.editing =
false;
319 callback.apply(
self, [
self.innerHTML, settings]);
321 if (!$.
trim($(
self).html())) {
322 $(
self).html(settings.placeholder);
327 submitdata[settings.name] = input.val();
328 submitdata[settings.id] =
self.id;
330 if ($.isFunction(settings.submitdata)) {
331 $.extend(submitdata, settings.submitdata.apply(
self, [
self.revert, settings]));
333 $.extend(submitdata, settings.submitdata);
337 if (
'PUT' == settings.method) {
338 submitdata[
'_method'] =
'put';
342 $(
self).html(settings.indicator);
349 url : settings.target,
350 success :
function(result, status) {
351 if (ajaxoptions.dataType ==
'html') {
352 $(
self).html(result);
354 self.editing =
false;
355 callback.apply(
self, [result, settings]);
356 if (!$.
trim($(
self).html())) {
357 $(
self).html(settings.placeholder);
360 error :
function(xhr, status, error) {
361 onerror.apply(form, [settings,
self, xhr]);
366 $.extend(ajaxoptions, settings.ajaxoptions);
374 $(
self).attr(
'title', settings.tooltip);
381 this.reset =
function(form) {
385 if (
false !== onreset.apply(form, [settings,
self])) {
386 $(
self).html(
self.revert);
387 self.editing =
false;
388 if (!$.
trim($(
self).html())) {
389 $(
self).html(settings.placeholder);
392 if (settings.tooltip) {
393 $(
self).attr(
'title', settings.tooltip);
406 element :
function(settings, original) {
407 var input = $(
'<input type="hidden"></input>');
408 $(
this).append(input);
411 content :
function(string, settings, original) {
412 $(
':input:first',
this).val(
string);
414 reset :
function(settings, original) {
415 original.reset(
this);
417 buttons :
function(settings, original) {
419 if (settings.submit) {
421 if (settings.submit.match(/>$/)) {
422 var submit = $(settings.submit).click(
function() {
423 if (submit.attr(
"type") !=
"submit") {
429 var submit = $(
'<button type="submit" />');
430 submit.html(settings.submit);
432 $(
this).append(submit);
434 if (settings.cancel) {
436 if (settings.cancel.match(/>$/)) {
437 var cancel = $(settings.cancel);
440 var cancel = $(
'<button type="cancel" />');
441 cancel.html(settings.cancel);
443 $(
this).append(cancel);
445 $(cancel).click(
function(event) {
447 if ($.isFunction($.editable.types[settings.type].reset)) {
448 var reset = $.editable.types[settings.type].reset;
450 var reset = $.editable.types[
'defaults'].reset;
452 reset.apply(form, [settings, original]);
459 element :
function(settings, original) {
460 var input = $(
'<input />');
461 if (settings.width !=
'none') { input.width(settings.width); }
462 if (settings.height !=
'none') { input.height(settings.height); }
465 input.attr(
'autocomplete',
'off');
466 $(
this).append(input);
471 element :
function(settings, original) {
472 var textarea = $(
'<textarea />');
474 textarea.attr(
'rows', settings.rows);
475 }
else if (settings.height !=
"none") {
476 textarea.height(settings.height);
479 textarea.attr(
'cols', settings.cols);
480 }
else if (settings.width !=
"none") {
481 textarea.width(settings.width);
483 $(
this).append(textarea);
488 element :
function(settings, original) {
489 var select = $(
'<select />');
490 $(
this).append(select);
493 content :
function(data, settings, original) {
495 if (String == data.constructor) {
496 eval (
'var json = ' + data);
501 for (var key in json) {
502 if (!json.hasOwnProperty(key)) {
505 if (
'selected' == key) {
508 var option = $(
'<option />').val(key).append(json[key]);
509 $(
'select',
this).append(option);
512 $(
'select',
this).children().each(
function() {
513 if ($(
this).val() == json[
'selected'] ||
514 $(
this).text() == $.
trim(original.revert)) {
515 $(this).attr(
'selected',
'selected');
523 addInputType:
function(name, input) {
524 $.editable.types[name] = input;
529 $.fn.editable.defaults = {
535 event :
'click.editable',
538 loadtext :
'Loading...',
539 placeholder:
'Click to edit',
char * trim(char *ptext)
Trimming whitespace.