7e56758f883a768c8fe88b6ada4c42934dae3b89.svn-base 4.3 KB

12
  1. /*! inline-attachment - v2.0.2 - 2015-11-03 */
  2. !function(a,b){"use strict";var c=function(a,b){this.settings=c.util.merge(a,c.defaults),this.editor=b,this.filenameTag="{filename}",this.lastValue=null};c.editors={},c.util={merge:function(){for(var a={},b=arguments.length-1;b>=0;b--){var c=arguments[b];for(var d in c)c.hasOwnProperty(d)&&(a[d]=c[d])}return a},appendInItsOwnLine:function(a,b){return(a+"\n\n[[D]]"+b).replace(/(\n{2,})\[\[D\]\]/,"\n\n").replace(/^(\n*)/,"")},insertTextAtCursor:function(b,c){var d,e=b.scrollTop,f=0,g=!1;b.selectionStart||"0"===b.selectionStart?g="ff":a.selection&&(g="ie"),"ie"===g?(b.focus(),d=a.selection.createRange(),d.moveStart("character",-b.value.length),f=d.text.length):"ff"===g&&(f=b.selectionStart);var h=b.value.substring(0,f),i=b.value.substring(f,b.value.length);b.value=h+c+i,f+=c.length,"ie"===g?(b.focus(),d=a.selection.createRange(),d.moveStart("character",-b.value.length),d.moveStart("character",f),d.moveEnd("character",0),d.select()):"ff"===g&&(b.selectionStart=f,b.selectionEnd=f,b.focus()),b.scrollTop=e}},c.defaults={uploadUrl:"upload_attachment.php",uploadMethod:"POST",uploadFieldName:"file",defaultExtension:"png",jsonFieldName:"filename",allowedTypes:["image/jpeg","image/png","image/jpg","image/gif"],progressText:"![Uploading file...]()",urlText:"![file]({filename})",errorText:"Error uploading file",extraParams:{},extraHeaders:{},beforeFileUpload:function(){return!0},onFileReceived:function(){},onFileUploadResponse:function(){return!0},onFileUploadError:function(){return!0},onFileUploaded:function(){}},c.prototype.uploadFile=function(a){var b=this,c=new FormData,d=new XMLHttpRequest,e=this.settings,f=e.defaultExtension||e.defualtExtension;if("function"==typeof e.setupFormData&&e.setupFormData(c,a),a.name){var g=a.name.match(/\.(.+)$/);g&&(f=g[1])}var h="image-"+Date.now()+"."+f;if("function"==typeof e.remoteFilename&&(h=e.remoteFilename(a)),c.append(e.uploadFieldName,a,h),"object"==typeof e.extraParams)for(var i in e.extraParams)e.extraParams.hasOwnProperty(i)&&c.append(i,e.extraParams[i]);if(d.open("POST",e.uploadUrl),"object"==typeof e.extraHeaders)for(var j in e.extraHeaders)e.extraHeaders.hasOwnProperty(j)&&d.setRequestHeader(j,e.extraHeaders[j]);return d.onload=function(){200===d.status||201===d.status?b.onFileUploadResponse(d):b.onFileUploadError(d)},e.beforeFileUpload(d)!==!1&&d.send(c),d},c.prototype.isFileAllowed=function(a){return 0===this.settings.allowedTypes.indexOf("*")?!0:this.settings.allowedTypes.indexOf(a.type)>=0},c.prototype.onFileUploadResponse=function(a){if(this.settings.onFileUploadResponse.call(this,a)!==!1){var b=JSON.parse(a.responseText),c=b[this.settings.jsonFieldName];if(b&&c){var d=this.settings.urlText.replace(this.filenameTag,c),e=this.editor.getValue().replace(this.lastValue,d);this.editor.setValue(e),this.settings.onFileUploaded.call(this,c)}}},c.prototype.onFileUploadError=function(a){if(this.settings.onFileUploadError.call(this,a)!==!1){var b=this.editor.getValue().replace(this.lastValue,"");this.editor.setValue(b)}},c.prototype.onFileInserted=function(a){this.settings.onFileReceived.call(this,a)!==!1&&(this.lastValue=this.settings.progressText,this.editor.insertValue(this.lastValue))},c.prototype.onPaste=function(a){var b,c=!1,d=a.clipboardData;if("object"==typeof d){b=d.items||d.files||[];for(var e=0;e<b.length;e++){var f=b[e];this.isFileAllowed(f)&&(c=!0,this.onFileInserted(f.getAsFile()),this.uploadFile(f.getAsFile()))}}return c&&a.preventDefault(),c},c.prototype.onDrop=function(a){for(var b=!1,c=0;c<a.dataTransfer.files.length;c++){var d=a.dataTransfer.files[c];this.isFileAllowed(d)&&(b=!0,this.onFileInserted(d),this.uploadFile(d))}return b},b.inlineAttachment=c}(document,window),function(){"use strict";inlineAttachment.editors.input={Editor:function(a){var b=a;return{getValue:function(){return b.value},insertValue:function(a){inlineAttachment.util.insertTextAtCursor(b,a)},setValue:function(a){b.value=a}}},attachToInput:function(a,b){b=b||{};var c=new inlineAttachment.editors.input.Editor(a),d=new inlineAttachment(b,c);a.addEventListener("paste",function(a){d.onPaste(a)},!1),a.addEventListener("drop",function(a){a.stopPropagation(),a.preventDefault(),d.onDrop(a)},!1),a.addEventListener("dragenter",function(a){a.stopPropagation(),a.preventDefault()},!1),a.addEventListener("dragover",function(a){a.stopPropagation(),a.preventDefault()},!1)}}}();