sdk: simplify interface

This commit is contained in:
mappu 2017-10-08 14:10:47 +13:00
parent f6b9fd2536
commit a2a3beb714
2 changed files with 28 additions and 9 deletions

View File

@ -22,7 +22,7 @@ html, body {
<script type="text/javascript" src="/sdk.js"></script>
<script type="text/javascript">
"use strict";
contented.enableDrop( $("#surrogate-area")[0], "", function(itm) {
contented.init("#surrogate-area", function(itm) {
console.log(itm);
});
</script>

View File

@ -1,8 +1,10 @@
;"use strict";
var contented = (function($) {
var contented = (function($, currentScriptPath) {
"use strict";
var baseURL = currentScriptPath.replace('sdk.js', '');
/**
* contented_SupportsDrop returns whether drag-and-drop is supported by this
* browser.
@ -17,19 +19,23 @@ var contented = (function($) {
* contented_EnableDrop enables drag-and-drop upload on a DOM element.
* The class "is-dragover" will be toggled on the target element.
*
* @param DOMElement element Drop target
* @param string baseURL Base URL of the contented server
* @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(element, baseURL, onUploaded, onClose) {
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) {
$.get(baseURL + "about", function(ret) {
$("title").text( ret.AppTitle );
var extraText = "";
@ -37,7 +43,7 @@ var contented = (function($) {
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);
$f.find(".contented-extratext").text(extraText);
@ -147,7 +153,20 @@ var contented = (function($) {
return {
'supportsDrop': contented_SupportsDrop,
'enableDrop': contented_EnableDrop
'init': contented_EnableDrop
};
})(jQuery);
})(
jQuery,
(function() {
// 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, '' );
})()
);