sdk: reformat

This commit is contained in:
mappu 2017-10-08 14:15:16 +13:00
parent 656c69ab5d
commit ba3f85919f
1 changed files with 48 additions and 40 deletions

View File

@ -1,6 +1,5 @@
;"use strict"; ;
var contented = (function ($, currentScriptPath) {
var contented = (function($, currentScriptPath) {
"use strict"; "use strict";
var baseURL = currentScriptPath.replace('sdk.js', ''); var baseURL = currentScriptPath.replace('sdk.js', '');
@ -11,7 +10,7 @@ var contented = (function($, currentScriptPath) {
* *
* @return bool * @return bool
*/ */
var contented_SupportsDrop = function() { var contented_SupportsDrop = function () {
return ('ondrop' in window && 'FormData' in window && 'FileReader' in window); return ('ondrop' in window && 'FormData' in window && 'FileReader' in window);
} }
@ -23,9 +22,9 @@ var contented = (function($, currentScriptPath) {
* @param Function onUploaded Called with a property object for every uploaded file * @param Function onUploaded Called with a property object for every uploaded file
* @param Function onClose Called when the widget should be destroyed * @param Function onClose Called when the widget should be destroyed
*/ */
var contented_EnableDrop = function(elementSelector, onUploaded, onClose) { var contented_EnableDrop = function (elementSelector, onUploaded, onClose) {
onUploaded = onUploaded || function(){}; onUploaded = onUploaded || function () { };
onClose = onClose || function(){}; onClose = onClose || function () { };
if ($(elementSelector).length != 1) { if ($(elementSelector).length != 1) {
return; // should only find one element return; // should only find one element
@ -35,28 +34,28 @@ var contented = (function($, currentScriptPath) {
// <input type="hidden" name="MAX_FILE_SIZE" value="` + ret.MaxUploadBytes + `" /> // <input type="hidden" name="MAX_FILE_SIZE" value="` + ret.MaxUploadBytes + `" />
// Create a new div for ourselves on top of the existing area // Create a new div for ourselves on top of the existing area
$.get(baseURL + "about", function(ret) { $.get(baseURL + "about", function (ret) {
$("title").text( ret.AppTitle ); $("title").text(ret.AppTitle);
var extraText = ""; var extraText = "";
if (ret.MaxUploadBytes > 0) { if (ret.MaxUploadBytes > 0) {
extraText = " (max " + Math.floor(ret.MaxUploadBytes / (1024*1024)) + " MiB)"; extraText = " (max " + Math.floor(ret.MaxUploadBytes / (1024 * 1024)) + " MiB)";
} }
$.get(baseURL + "widget.html", function(widgetHtml) { $.get(baseURL + "widget.html", function (widgetHtml) {
var $f = $("<div>").html(widgetHtml); var $f = $("<div>").html(widgetHtml);
$f.find(".contented-extratext").text(extraText); $f.find(".contented-extratext").text(extraText);
var ourClose = function() { var ourClose = function () {
$f.remove(); // remove from dom $f.remove(); // remove from dom
onClose(); // upstream close onClose(); // upstream close
}; };
$f.find(".contented-close").click(function() { $f.find(".contented-close").click(function () {
ourClose(); ourClose();
}) })
var setType = function(type) { var setType = function (type) {
$f.find(".contented-upload-type").removeClass("contented-upload-type-active"); $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-type[data-upload-type=" + type + "]").addClass("contented-upload-type-active");
@ -64,8 +63,8 @@ var contented = (function($, currentScriptPath) {
$f.find(".contented-if-" + type).addClass("contented-active"); $f.find(".contented-if-" + type).addClass("contented-active");
}; };
$f.find(".contented-upload-type").click(function() { $f.find(".contented-upload-type").click(function () {
setType( $(this).attr('data-upload-type') ); setType($(this).attr('data-upload-type'));
}); });
if (!contented_SupportsDrop()) { if (!contented_SupportsDrop()) {
@ -80,29 +79,38 @@ var contented = (function($, currentScriptPath) {
$f.css({ $f.css({
'position': 'absolute', 'position': 'absolute',
'left': offset.left + "px", 'left': offset.left + "px",
'top': offset.top + "px", 'top': offset.top + "px",
'width': $element.width() + "px", 'width': $element.width() + "px",
'min-width': $element.width() + "px", 'min-width': $element.width() + "px",
'max-width': $element.width() + "px", 'max-width': $element.width() + "px",
'height': $element.height() + "px", 'height': $element.height() + "px",
'min-height': $element.height() + "px", 'min-height': $element.height() + "px",
'max-height': $element.height() + "px" 'max-height': $element.height() + "px"
}); });
$f.find('.contented').on('dragover dragenter', function(e) { $f.find('.contented').on('dragover dragenter', function (e) {
e.preventDefault(); e.preventDefault();
e.stopPropagation(); e.stopPropagation();
$(this).addClass('is-dragging'); $(this).addClass('is-dragging');
}); });
$f.find('.contented').on('dragleave dragend', function(e) { $f.find('.contented').on('dragleave dragend', function (e) {
e.preventDefault(); e.preventDefault();
e.stopPropagation(); e.stopPropagation();
$(this).removeClass('is-dragging'); $(this).removeClass('is-dragging');
}); });
$f.find('.contented').on('drag', function(e) { $f.find('.contented').on('drag', function (e) {
e.preventDefault();
e.stopPropagation();
handleUploadFrom(event.originalEvent.dataTransfer.files);
});
$("body").append($f);
var handleUploadFrom = function (files) {
input.files = event.originalEvent.dataTransfer.files; input.files = event.originalEvent.dataTransfer.files;
refreshCaption(); refreshCaption();
setType("progress"); setType("progress");
@ -110,9 +118,8 @@ var contented = (function($, currentScriptPath) {
// Ajax uploader // Ajax uploader
// TODO // TODO
form.submit(); form.submit();
});
$("body").append($f); }
}); });
@ -130,15 +137,16 @@ var contented = (function($, currentScriptPath) {
})( })(
jQuery, jQuery,
(function() { (function () {
"use strict";
// Determine current script path // Determine current script path
// @ref https://stackoverflow.com/a/26023176 // @ref https://stackoverflow.com/a/26023176
var scripts = document.querySelectorAll( 'script[src]' ); var scripts = document.querySelectorAll('script[src]');
var currentScript = scripts[ scripts.length - 1 ].src; var currentScript = scripts[scripts.length - 1].src;
var currentScriptChunks = currentScript.split( '/' ); var currentScriptChunks = currentScript.split('/');
var currentScriptFile = currentScriptChunks[ currentScriptChunks.length - 1 ]; var currentScriptFile = currentScriptChunks[currentScriptChunks.length - 1];
return currentScript.replace( currentScriptFile, '' ); return currentScript.replace(currentScriptFile, '');
})() })()
); );