-
Notifications
You must be signed in to change notification settings - Fork 77
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Roy van Kaathoven
committed
Dec 30, 2012
1 parent
7b76194
commit 4f87f42
Showing
6 changed files
with
98 additions
and
150 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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); | ||
|
@@ -336,7 +219,6 @@ | |
inlineattach = new inlineAttach(options), | ||
last_upload; | ||
|
||
// onPaste | ||
el.addEventListener('paste', function(e) { | ||
inlineattach.onPaste(e); | ||
}, false); | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.