3 lines
7.3 KiB
JavaScript
3 lines
7.3 KiB
JavaScript
/*! jquery-jeditable https://github.com/NicolasCARPi/jquery_jeditable#readme */
|
|
|
|
!function($){$.fn.editable=function(target,options){if("disable"!=target)if("enable"!=target){if("destroy"!=target){var settings=$.extend({},$.fn.editable.defaults,{target:target},options),plugin=$.editable.types[settings.type].plugin||function(){},submit=$.editable.types[settings.type].submit||function(){},buttons=$.editable.types[settings.type].buttons||$.editable.types.defaults.buttons,content=$.editable.types[settings.type].content||$.editable.types.defaults.content,element=$.editable.types[settings.type].element||$.editable.types.defaults.element,reset=$.editable.types[settings.type].reset||$.editable.types.defaults.reset,callback=settings.callback||function(){},onedit=settings.onedit||function(){},onsubmit=settings.onsubmit||function(){},onreset=settings.onreset||function(){},onerror=settings.onerror||reset;return settings.tooltip&&$(this).attr("title",settings.tooltip),this.each(function(){var self=this;$(this).data("event.editable",settings.event),$.trim($(this).html())||$(this).html(settings.placeholder),$(this).bind(settings.event,function(e){if(!0!==$(this).data("disabled.editable")&&!self.editing&&!1!==onedit.apply(this,[settings,self])){e.preventDefault(),e.stopPropagation(),settings.tooltip&&$(self).removeAttr("title"),$(this).html().toLowerCase().replace(/(;|"|\/)/g,"")==settings.placeholder.toLowerCase().replace(/(;|"|\/)/g,"")&&$(this).html(""),self.editing=!0,self.revert=$(self).html(),$(self).html("");var form=$("<form />");settings.cssclass&&("inherit"==settings.cssclass?form.attr("class",$(self).attr("class")):form.attr("class",settings.cssclass)),settings.style&&("inherit"==settings.style?(form.attr("style",$(self).attr("style")),form.css("display",$(self).css("display"))):form.attr("style",settings.style));var input_content,input=element.apply(form,[settings,self]);if(settings.loadurl){var t=setTimeout(function(){input.disabled=!0,content.apply(form,[settings.loadtext,settings,self])},100),loaddata={};loaddata[settings.id]=self.id,$.isFunction(settings.loaddata)?$.extend(loaddata,settings.loaddata.apply(self,[self.revert,settings])):$.extend(loaddata,settings.loaddata),$.ajax({type:settings.loadtype,url:settings.loadurl,data:loaddata,async:!1,cache:!1,success:function(result){window.clearTimeout(t),input_content=result,input.disabled=!1}})}else settings.data?(input_content=settings.data,$.isFunction(settings.data)&&(input_content=settings.data.apply(self,[self.revert,settings]))):input_content=self.revert;content.apply(form,[input_content,settings,self]),input.attr("name",settings.name),buttons.apply(form,[settings,self]),$(self).append(form),plugin.apply(form,[settings,self]),$(":input:visible:enabled:first",form).focus(),settings.select&&input.select(),input.keydown(function(e){27==e.keyCode&&(e.preventDefault(),reset.apply(form,[settings,self]))});"cancel"==settings.onblur?input.blur(function(e){t=setTimeout(function(){reset.apply(form,[settings,self])},500)}):"submit"==settings.onblur?input.blur(function(e){t=setTimeout(function(){form.submit()},200)}):$.isFunction(settings.onblur)?input.blur(function(e){settings.onblur.apply(self,[input.val(),settings])}):input.blur(function(e){}),form.submit(function(e){if(t&&clearTimeout(t),e.preventDefault(),!1!==onsubmit.apply(form,[settings,self])&&!1!==submit.apply(form,[settings,self]))if($.isFunction(settings.target)){var str=settings.target.apply(self,[input.val(),settings]);$(self).html(str),self.editing=!1,callback.apply(self,[self.innerHTML,settings]),$.trim($(self).html())||$(self).html(settings.placeholder)}else{var submitdata={};submitdata[settings.name]=input.val(),submitdata[settings.id]=self.id,$.isFunction(settings.submitdata)?$.extend(submitdata,settings.submitdata.apply(self,[self.revert,settings])):$.extend(submitdata,settings.submitdata),"PUT"==settings.method&&(submitdata._method="put"),$(self).html(settings.indicator);var ajaxoptions={type:"POST",data:submitdata,dataType:"html",url:settings.target,success:function(result,status){"html"==ajaxoptions.dataType&&$(self).html(result),self.editing=!1,callback.apply(self,[result,settings]),$.trim($(self).html())||$(self).html(settings.placeholder)},error:function(xhr,status,error){onerror.apply(form,[settings,self,xhr])}};$.extend(ajaxoptions,settings.ajaxoptions),$.ajax(ajaxoptions)}return $(self).attr("title",settings.tooltip),!1})}}),this.reset=function(form){this.editing&&!1!==onreset.apply(form,[settings,self])&&($(self).html(self.revert),self.editing=!1,$.trim($(self).html())||$(self).html(settings.placeholder),settings.tooltip&&$(self).attr("title",settings.tooltip))}})}$(this).unbind($(this).data("event.editable")).removeData("disabled.editable").removeData("event.editable")}else $(this).data("disabled.editable",!1);else $(this).data("disabled.editable",!0)},$.editable={types:{defaults:{element:function(settings,original){var input=$('<input type="hidden"></input>');return $(this).append(input),input},content:function(string,settings,original){$(":input:first",this).val(string)},reset:function(settings,original){original.reset(this)},buttons:function(settings,original){var form=this;if(settings.submit){if(settings.submit.match(/>$/))var submit=$(settings.submit).click(function(){"submit"!=submit.attr("type")&&form.submit()});else{(submit=$('<button type="submit" />')).html(settings.submit)}$(this).append(submit)}if(settings.cancel){if(settings.cancel.match(/>$/))var cancel=$(settings.cancel);else{(cancel=$('<button type="cancel" />')).html(settings.cancel)}$(this).append(cancel),$(cancel).click(function(event){if($.isFunction($.editable.types[settings.type].reset))var reset=$.editable.types[settings.type].reset;else reset=$.editable.types.defaults.reset;return reset.apply(form,[settings,original]),!1})}}},text:{element:function(settings,original){var input=$("<input type='text' />");return"none"!=settings.width&&input.css("width",settings.width),"none"!=settings.height&&input.css("height",settings.height),input.attr("autocomplete","off"),settings.size&&input.attr("size",settings.size),settings.maxlength&&input.attr("maxlength",settings.maxlength),$(this).append(input),input}},textarea:{element:function(settings,original){var textarea=$("<textarea></textarea>");return settings.rows?textarea.attr("rows",settings.rows):"none"!=settings.height&&textarea.height(settings.height),settings.cols?textarea.attr("cols",settings.cols):"none"!=settings.width&&textarea.width(settings.width),$(this).append(textarea),textarea}},select:{element:function(settings,original){var select=$("<select />");return $(this).append(select),select},content:function(data,settings,original){if(String==data.constructor)var json=JSON.parse(string);else json=data;for(var key in json)if(json.hasOwnProperty(key)&&"selected"!=key){var option=$("<option />").val(key).append(json[key]);$("select",this).append(option)}if($("select",this).children().each(function(){$(this).val()!=json.selected&&$(this).text()!=$.trim(original.revert)||$(this).prop("selected",!0)}),!settings.submit){var form=this;$("select",this).change(function(){form.submit()})}}}},addInputType:function(name,input){$.editable.types[name]=input}},$.fn.editable.defaults={name:"value",id:"id",type:"text",width:"auto",height:"auto",event:"click.editable",onblur:"cancel",loadtype:"GET",loadtext:"Loading...",placeholder:"Click to edit",loaddata:{},submitdata:{},ajaxoptions:{}}}(jQuery); |