205 lines
7.4 KiB
JavaScript
205 lines
7.4 KiB
JavaScript
;
|
|
var contented = (function ($, currentScriptPath) {
|
|
"use strict";
|
|
|
|
var baseURL = currentScriptPath.replace('sdk.js', '');
|
|
|
|
var formatBytes = function(bytes) {
|
|
if (bytes < 1024) {
|
|
return bytes + " B";
|
|
} else if (bytes < (1024*1024)) {
|
|
return (bytes / 1024).toFixed(1) + " KiB";
|
|
} else if (bytes < (1024*1024*1024)) {
|
|
return (bytes / (1024*1024)).toFixed(1) + " MiB";
|
|
} else {
|
|
return (bytes / (1024*1024*1024)).toFixed(1) + " GiB";
|
|
}
|
|
};
|
|
|
|
/**
|
|
* contented_SupportsDrop returns whether drag-and-drop is supported by this
|
|
* browser.
|
|
*
|
|
* @return bool
|
|
*/
|
|
var contented_SupportsDrop = function () {
|
|
return ('ondrop' in window && 'FormData' in window && 'FileReader' in window);
|
|
}
|
|
|
|
/**
|
|
* contented_EnableDrop enables drag-and-drop upload on a DOM element.
|
|
* The class "is-dragover" will be toggled on the target element.
|
|
*
|
|
* @param any element Drop target (string selector / DOMElement / jQuery)
|
|
* @param Function onUploaded Called with a property object for every uploaded file
|
|
* @param Function onClose Called when the widget should be destroyed
|
|
*/
|
|
var contented_EnableDrop = function (elementSelector, onUploaded, onClose) {
|
|
onUploaded = onUploaded || function () { };
|
|
onClose = onClose || function () { };
|
|
|
|
if ($(elementSelector).length != 1) {
|
|
return; // should only find one element
|
|
}
|
|
var element = $(elementSelector)[0];
|
|
|
|
// <input type="hidden" name="MAX_FILE_SIZE" value="` + ret.MaxUploadBytes + `" />
|
|
|
|
// Create a new div for ourselves on top of the existing area
|
|
$.get(baseURL + "about", function (ret) {
|
|
$("title").text(ret.AppTitle);
|
|
|
|
var extraText = "";
|
|
if (ret.MaxUploadBytes > 0) {
|
|
extraText = " (max " + formatBytes(ret.MaxUploadBytes) + ")";
|
|
}
|
|
|
|
$.get(baseURL + "widget.html", function (widgetHtml) {
|
|
|
|
var $f = $("<div>").html(widgetHtml);
|
|
$f.find(".contented-extratext").text(extraText);
|
|
|
|
var ourClose = function () {
|
|
$f.remove(); // remove from dom
|
|
onClose(); // upstream close
|
|
};
|
|
$f.find(".contented-close").click(function () {
|
|
ourClose();
|
|
})
|
|
|
|
var setType = function (type) {
|
|
$f.find(".contented-upload-type").removeClass("contented-upload-type-active");
|
|
$f.find(".contented-upload-type[data-upload-type=" + type + "]").addClass("contented-upload-type-active");
|
|
|
|
$f.find(".contented-upload-if").removeClass("contented-active");
|
|
$f.find(".contented-if-" + type).addClass("contented-active");
|
|
};
|
|
|
|
$f.find(".contented-upload-type").click(function () {
|
|
setType($(this).attr('data-upload-type'));
|
|
});
|
|
|
|
if (!contented_SupportsDrop()) {
|
|
// switch default
|
|
setType('file');
|
|
}
|
|
|
|
//
|
|
|
|
var $element = $(element);
|
|
var offset = $element.offset();
|
|
|
|
$f.css({
|
|
'position': 'absolute',
|
|
'left': offset.left + "px",
|
|
'top': offset.top + "px",
|
|
'width': $element.width() + "px",
|
|
'min-width': $element.width() + "px",
|
|
'max-width': $element.width() + "px",
|
|
'height': $element.height() + "px",
|
|
'min-height': $element.height() + "px",
|
|
'max-height': $element.height() + "px"
|
|
});
|
|
|
|
$f.find('.contented').on('dragover dragenter', function (e) {
|
|
e.preventDefault();
|
|
e.stopPropagation();
|
|
$(this).addClass('is-dragging');
|
|
});
|
|
|
|
$f.find('.contented').on('dragleave dragend', function (e) {
|
|
e.preventDefault();
|
|
e.stopPropagation();
|
|
$(this).removeClass('is-dragging');
|
|
});
|
|
|
|
$f.find('.contented').on('drop', function (e) {
|
|
e.preventDefault();
|
|
e.stopPropagation();
|
|
handleUploadFrom(e.originalEvent.dataTransfer.files);
|
|
});
|
|
|
|
$("body").append($f);
|
|
|
|
var setProgressCaption = function(message) {
|
|
$f.find(".contented-if-progress label").text(message);
|
|
};
|
|
var setProgressPercentage = function(frc) {
|
|
$f.find(".contented-progress-element").css('width', (frc * 100) + "%");
|
|
};
|
|
|
|
var handleUploadFrom = function (files) {
|
|
|
|
setProgressCaption("Uploading, please wait...");
|
|
setProgressPercentage(0);
|
|
setType("progress");
|
|
|
|
$f.find(".contented-upload-type-selector").hide();
|
|
$f.find(".contented").removeClass('is-dragging');
|
|
|
|
// Ajax uploader
|
|
var ajaxData = new FormData();
|
|
for (var i = 0; i < files.length; ++i) {
|
|
ajaxData.append("f", files[i]);
|
|
}
|
|
|
|
// ajax request
|
|
$.ajax({
|
|
url: baseURL + "upload",
|
|
type: "POST",
|
|
data: ajaxData,
|
|
dataType: 'json', // response type
|
|
cache: false,
|
|
contentType: false,
|
|
processData: false,
|
|
beforeSend: function (xhr) {
|
|
xhr.on('progress', function (ev) {
|
|
if (ev.lengthComputable) {
|
|
setProgressCaption("Uploaded " + formatBytes(evt.loaded) + " of " + formatBytes(evt.total) + "...");
|
|
setProgressPercentage(evt.loaded / evt.total);
|
|
}
|
|
});
|
|
},
|
|
complete: function () {
|
|
setProgressCaption("Upload complete.");
|
|
setProgressPercentage(1);
|
|
},
|
|
success: function (data) {
|
|
onUploaded(data);
|
|
ourClose();
|
|
},
|
|
error: function () {
|
|
setProgressCaption("Upload failed.");
|
|
}
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
//
|
|
return {
|
|
'supportsDrop': contented_SupportsDrop,
|
|
'init': contented_EnableDrop
|
|
};
|
|
|
|
})(
|
|
jQuery,
|
|
(function () {
|
|
"use strict";
|
|
|
|
// Determine current script path
|
|
// @ref https://stackoverflow.com/a/26023176
|
|
var scripts = document.querySelectorAll('script[src]');
|
|
var currentScript = scripts[scripts.length - 1].src;
|
|
var currentScriptChunks = currentScript.split('/');
|
|
var currentScriptFile = currentScriptChunks[currentScriptChunks.length - 1];
|
|
|
|
return currentScript.replace(currentScriptFile, '');
|
|
})()
|
|
);
|