Skip to content

Commit

Permalink
Updated dist version
Browse files Browse the repository at this point in the history
  • Loading branch information
Roy van Kaathoven committed Dec 30, 2012
1 parent 7b76194 commit 4f87f42
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 150 deletions.
172 changes: 27 additions & 145 deletions dist/codemirror.inline-attach.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: [email protected]
*
* 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);
Expand Down Expand Up @@ -336,7 +219,6 @@
inlineattach = new inlineAttach(options),
last_upload;

// onPaste
el.addEventListener('paste', function(e) {
inlineattach.onPaste(e);
}, false);
Expand Down
2 changes: 1 addition & 1 deletion dist/codemirror.inline-attach.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

35 changes: 34 additions & 1 deletion dist/inline-attach.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,40 @@
* @param {Object} options
*/
window.inlineAttach.attachToInput = function(input, options) {
// TODO

options = options || {};

options.onRecievedFile = function(file) {
last_upload = '![Uploadf file...]()';
input.value = (input.value + "\n\n" + last_upload);
};

options.onUploadedFile = function(data) {
if (data.filename) {
var val = input.value.replace(last_upload, "![file](" + data.filename + ")")
input.value = val;
}
};

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);
2 changes: 1 addition & 1 deletion dist/inline-attach.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

35 changes: 34 additions & 1 deletion dist/jquery.inline-attach.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,40 @@
* @param {Object} options
*/
window.inlineAttach.attachToInput = function(input, options) {
// TODO

options = options || {};

options.onRecievedFile = function(file) {
last_upload = '![Uploadf file...]()';
input.value = (input.value + "\n\n" + last_upload);
};

options.onUploadedFile = function(data) {
if (data.filename) {
var val = input.value.replace(last_upload, "![file](" + data.filename + ")")
input.value = val;
}
};

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);
Expand Down
Loading

0 comments on commit 4f87f42

Please sign in to comment.