From 4f87f42518d76a08a4eeda115b83718efb74680b Mon Sep 17 00:00:00 2001 From: Roy van Kaathoven Date: Sun, 30 Dec 2012 14:46:53 +0100 Subject: [PATCH] Updated dist version --- dist/codemirror.inline-attach.js | 172 +++++---------------------- dist/codemirror.inline-attach.min.js | 2 +- dist/inline-attach.js | 35 +++++- dist/inline-attach.min.js | 2 +- dist/jquery.inline-attach.js | 35 +++++- dist/jquery.inline-attach.min.js | 2 +- 6 files changed, 98 insertions(+), 150 deletions(-) diff --git a/dist/codemirror.inline-attach.js b/dist/codemirror.inline-attach.js index 8021489..a122f50 100644 --- a/dist/codemirror.inline-attach.js +++ b/dist/codemirror.inline-attach.js @@ -144,157 +144,40 @@ * @param {Object} options */ window.inlineAttach.attachToInput = function(input, options) { - // TODO - }; - -})(document, window); -/*! inline-attach - v0.1.0 - 2012-12-22 */ - -/* - * Inline Text Attachment - * - * Copyright 2012 Roy van Kaathoven. - * Contact: royvankaathoven@hotmail.com - * - * Licensed under the MIT License. - * - * Version 0.1 - */ -(function(document, window) { - - /** - * Simple function to merge the given objects - * - * @param {Object[]} object Multiple object parameters - * @returns {Object} - */ - function merge() { - var result = {}; - for (var i = arguments.length - 1; i >= 0; i--) { - var obj = arguments[i]; - for (var k in obj) { - result[k] = obj[k]; - } - } - return result; - } - - /** - * @param {Object} options - */ - window.inlineAttach = function(options) { - - var settings = merge(options, inlineAttach.defaults), - me = this; - - /** - * Upload a given file blob - * - * @param {Blob} file - */ - this.uploadFile = function(file) { - var formData = new FormData(), - xhr = new XMLHttpRequest(); - formData.append('file', file); - - xhr.open('POST', settings.upload_url); - xhr.upload.onprogress = function(event) { - // TODO show progress in text - }; - xhr.onload = function(e) { - if (xhr.status === 200) { - var data = JSON.parse(xhr.responseText); - settings.onUploadedFile(data); - } - }; - xhr.send(formData); - }; - - /** - * Check if the given file is allowed - * - * @param {File} file - */ - this.isAllowedFile = function(file) { - return settings.allowed_types.indexOf(file.type) >= 0; - }; + options = options || {}; - /** - * Catches the paste event - * - * @param {Event} e - * @returns {Boolean} If a file is handled - */ - this.onPaste = function(e) { - var result = false, - clipboardData = e.clipboardData; - for (var i = 0; i < clipboardData.items.length; i++) { - var item = clipboardData.items[i]; - if (me.isAllowedFile(item)) { - result = true; - settings.onRecievedFile(item.getAsFile()); - this.uploadFile(item.getAsFile()); - } - } - - return result; + options.onRecievedFile = function(file) { + last_upload = '![Uploadf file...]()'; + input.value = (input.value + "\n\n" + last_upload); }; - /** - * Catches onDrop event - * - * @param {Event} e - * @returns {Boolean} If a file is handled - */ - this.onDrop = function(e) { - var result = false; - for (var i = 0; i < e.dataTransfer.files.length; i++) { - var file = e.dataTransfer.files[i]; - if (me.isAllowedFile(file)) { - result = true; - me.uploadFile(file); - settings.onRecievedFile(file); - } + options.onUploadedFile = function(data) { + if (data.filename) { + var val = input.value.replace(last_upload, "![file](" + data.filename + ")") + input.value = val; } - - return result; }; - }; - - /** - * Default configuration - */ - window.inlineAttach.defaults = { - upload_url: 'upload_attachment.php', - allowed_types: [ - 'image/jpeg', - 'image/png', - 'image/jpg', - 'image/gif' - ], - /** - * When a file is recieved by drag-drop or paste - * - * @param {Blob} file - */ - onRecievedFile: function(file) {}, - /** - * When a file has succesfully been uploaded - * - * @param {Object} json Recieved json data - */ - onUploadedFile: function(json) {} - }; - /** - * Attach to a standard input field - * - * @param {Input} input - * @param {Object} options - */ - window.inlineAttach.attachToInput = function(input, options) { - // TODO + var inlineattach = new inlineAttach(options), + last_upload; + + input.addEventListener('paste', function(e) { + inlineattach.onPaste(e); + }, false); + input.addEventListener('drop', function(e) { + e.stopPropagation(); + e.preventDefault(); + inlineattach.onDrop(e); + }, false); + input.addEventListener('dragenter', function(e) { + e.stopPropagation(); + e.preventDefault(); + }, false); + input.addEventListener('dragover', function(e) { + e.stopPropagation(); + e.preventDefault(); + }, false); }; })(document, window); @@ -336,7 +219,6 @@ inlineattach = new inlineAttach(options), last_upload; - // onPaste el.addEventListener('paste', function(e) { inlineattach.onPaste(e); }, false); diff --git a/dist/codemirror.inline-attach.min.js b/dist/codemirror.inline-attach.min.js index 35e6ffa..5314502 100644 --- a/dist/codemirror.inline-attach.min.js +++ b/dist/codemirror.inline-attach.min.js @@ -1,2 +1,2 @@ /*! inline-attach - v0.1.0 - 2012-12-30 */ -(function(e,t){function n(){var e={};for(var t=arguments.length-1;t>=0;t--){var n=arguments[t];for(var r in n)e[r]=n[r]}return e}t.inlineAttach=function(e){var t=n(e,inlineAttach.defaults),r=this;this.uploadFile=function(e){var n=new FormData,r=new XMLHttpRequest;n.append("file",e),r.open("POST",t.upload_url),r.upload.onprogress=function(e){},r.onload=function(e){if(r.status===200){var n=JSON.parse(r.responseText);t.onUploadedFile(n)}},r.send(n)},this.isAllowedFile=function(e){return t.allowed_types.indexOf(e.type)>=0},this.onPaste=function(e){var n=!1,i=e.clipboardData;for(var s=0;s=0;t--){var n=arguments[t];for(var r in n)e[r]=n[r]}return e}t.inlineAttach=function(e){var t=n(e,inlineAttach.defaults),r=this;this.uploadFile=function(e){var n=new FormData,r=new XMLHttpRequest;n.append("file",e),r.open("POST",t.upload_url),r.upload.onprogress=function(e){},r.onload=function(e){if(r.status===200){var n=JSON.parse(r.responseText);t.onUploadedFile(n)}},r.send(n)},this.isAllowedFile=function(e){return t.allowed_types.indexOf(e.type)>=0},this.onPaste=function(e){var n=!1,i=e.clipboardData;for(var s=0;s=0;t--){var n=arguments[t];for(var r in n)e[r]=n[r]}return e}t.inlineAttach=function(e){var t=n(e,inlineAttach.defaults),r=this;this.uploadFile=function(e){var n=new FormData,r=new XMLHttpRequest;n.append("file",e),r.open("POST",t.upload_url),r.upload.onprogress=function(e){},r.onload=function(e){if(r.status===200){var n=JSON.parse(r.responseText);t.onUploadedFile(n)}},r.send(n)},this.isAllowedFile=function(e){return t.allowed_types.indexOf(e.type)>=0},this.onPaste=function(e){var n=!1,i=e.clipboardData;for(var s=0;s=0;t--){var n=arguments[t];for(var r in n)e[r]=n[r]}return e}t.inlineAttach=function(e){var t=n(e,inlineAttach.defaults),r=this;this.uploadFile=function(e){var n=new FormData,r=new XMLHttpRequest;n.append("file",e),r.open("POST",t.upload_url),r.upload.onprogress=function(e){},r.onload=function(e){if(r.status===200){var n=JSON.parse(r.responseText);t.onUploadedFile(n)}},r.send(n)},this.isAllowedFile=function(e){return t.allowed_types.indexOf(e.type)>=0},this.onPaste=function(e){var n=!1,i=e.clipboardData;for(var s=0;s=0;t--){var n=arguments[t];for(var r in n)e[r]=n[r]}return e}t.inlineAttach=function(e){var t=n(e,inlineAttach.defaults),r=this;this.uploadFile=function(e){var n=new FormData,r=new XMLHttpRequest;n.append("file",e),r.open("POST",t.upload_url),r.upload.onprogress=function(e){},r.onload=function(e){if(r.status===200){var n=JSON.parse(r.responseText);t.onUploadedFile(n)}},r.send(n)},this.isAllowedFile=function(e){return t.allowed_types.indexOf(e.type)>=0},this.onPaste=function(e){var n=!1,i=e.clipboardData;for(var s=0;s=0;t--){var n=arguments[t];for(var r in n)e[r]=n[r]}return e}t.inlineAttach=function(e){var t=n(e,inlineAttach.defaults),r=this;this.uploadFile=function(e){var n=new FormData,r=new XMLHttpRequest;n.append("file",e),r.open("POST",t.upload_url),r.upload.onprogress=function(e){},r.onload=function(e){if(r.status===200){var n=JSON.parse(r.responseText);t.onUploadedFile(n)}},r.send(n)},this.isAllowedFile=function(e){return t.allowed_types.indexOf(e.type)>=0},this.onPaste=function(e){var n=!1,i=e.clipboardData;for(var s=0;s",{src:t.target.result,width:100}))},n.readAsDataURL(t)}e.fn.inlineattach=function(t){var n=e(this);return n.each(function(){var n=e(this);t.onRecievedFile=function(e){i="![Uploadf file...]()",n.val(n.val()+"\n\n"+i)},t.onUploadedFile=function(e){if(e.filename){var t=n.val().replace(i,"![file]("+e.filename+")");n.val(t)}};var r=new inlineAttach(t),i;n.bind({paste:function(e){r.onPaste(e.originalEvent)},drop:function(e){e.stopPropagation(),e.preventDefault(),r.onDrop(e.originalEvent)},"dragenter dragover":function(e){e.stopPropagation(),e.preventDefault()}})}),this}}(jQuery); \ No newline at end of file +(function(e,t){function n(){var e={};for(var t=arguments.length-1;t>=0;t--){var n=arguments[t];for(var r in n)e[r]=n[r]}return e}t.inlineAttach=function(e){var t=n(e,inlineAttach.defaults),r=this;this.uploadFile=function(e){var n=new FormData,r=new XMLHttpRequest;n.append("file",e),r.open("POST",t.upload_url),r.upload.onprogress=function(e){},r.onload=function(e){if(r.status===200){var n=JSON.parse(r.responseText);t.onUploadedFile(n)}},r.send(n)},this.isAllowedFile=function(e){return t.allowed_types.indexOf(e.type)>=0},this.onPaste=function(e){var n=!1,i=e.clipboardData;for(var s=0;s",{src:t.target.result,width:100}))},n.readAsDataURL(t)}e.fn.inlineattach=function(t){var n=e(this);return n.each(function(){var n=e(this);t.onRecievedFile=function(e){i="![Uploadf file...]()",n.val(n.val()+"\n\n"+i)},t.onUploadedFile=function(e){if(e.filename){var t=n.val().replace(i,"![file]("+e.filename+")");n.val(t)}};var r=new inlineAttach(t),i;n.bind({paste:function(e){r.onPaste(e.originalEvent)},drop:function(e){e.stopPropagation(),e.preventDefault(),r.onDrop(e.originalEvent)},"dragenter dragover":function(e){e.stopPropagation(),e.preventDefault()}})}),this}}(jQuery); \ No newline at end of file