sdk: flatten out some nested callbacks

This commit is contained in:
mappu 2023-05-17 17:34:22 +12:00
parent cb29cf83ac
commit bf3339fec7

View File

@ -1,10 +1,8 @@
; ;(function() {
//
var contented = (function() {
"use strict"; "use strict";
var installPolyfills = function() {
// @ref https://developer.mozilla.org/en-US/docs/Web/API/HTMLCanvasElement/toBlob // @ref https://developer.mozilla.org/en-US/docs/Web/API/HTMLCanvasElement/toBlob
if (!HTMLCanvasElement.prototype.toBlob) { if (!HTMLCanvasElement.prototype.toBlob) {
Object.defineProperty(HTMLCanvasElement.prototype, 'toBlob', { Object.defineProperty(HTMLCanvasElement.prototype, 'toBlob', {
@ -22,6 +20,7 @@ var contented = (function() {
} }
}); });
} }
}
var getCurrentScriptPath = function () { var getCurrentScriptPath = function () {
// Determine current script path // Determine current script path
@ -34,66 +33,6 @@ var contented = (function() {
return currentScript.replace(currentScriptFile, ''); return currentScript.replace(currentScriptFile, '');
}; };
var currentScriptPath = getCurrentScriptPath();
var baseURL = currentScriptPath.replace('sdk.js', '');
return {
"loaded": false,
"baseURL": baseURL,
"__preInit": [],
/**
* initArea shows the contented upload widget over the top of a target DOM element.
*
* @param any element Drop target (string selector / DOMElement / jQuery)
* @param Function onUploaded Called with an array of upload IDs
* @param Function onClose Called when the widget is being destroyed
*/
"init": function(elementSelector, onUploaded, onClose) {
contented.__preInit.push([elementSelector, onUploaded, onClose]);
},
/**
* supportsDrop returns whether drag-and-drop is supported by this browser.
*
* @return bool
*/
"supportsDrop": function() {
return ('ondrop' in window && 'FormData' in window && 'FileReader' in window);
},
"getPreviewURL": function(id) {
return baseURL + "p/" + encodeURIComponent(id);
},
"getMultiPreviewURL": function(items) {
return baseURL + "p/" + encodeURIComponent(items.join("-"));
},
"getDownloadURL": function(id) {
return baseURL + "get/" + encodeURIComponent(id);
},
"getInfoJSONURL": function(id) {
return baseURL + "info/" + encodeURIComponent(id);
},
"getThumbnailURL": function(thumbnailType, id) {
return baseURL + "thumb/" + encodeURIComponent(thumbnailType) + "/" + encodeURIComponent(id);
},
"thumbnail": {
"small_square": "s",
"medium_square": "b",
"medium": "t",
"large": "m",
"xlarge": "l",
"xxlarge": "h"
}
};
})();
;(function() {
"use strict";
var loadScript = function(url, onLoad) { var loadScript = function(url, onLoad) {
var script = document.createElement('script'); var script = document.createElement('script');
script.onload = onLoad; script.onload = onLoad;
@ -139,8 +78,6 @@ var contented = (function() {
}); });
}; };
var afterScriptsLoaded = function() {
var initArea = function (elementSelector, onUploaded, onClose) { var initArea = function (elementSelector, onUploaded, onClose) {
onUploaded = onUploaded || function () { }; onUploaded = onUploaded || function () { };
onClose = onClose || function () { }; onClose = onClose || function () { };
@ -438,7 +375,9 @@ var contented = (function() {
}); });
} };
var afterScriptsLoaded = function() {
// Update fields in global variable // Update fields in global variable
contented.init = initArea; contented.init = initArea;
@ -450,6 +389,65 @@ var contented = (function() {
} }
}; };
var init = function() {
installPolyfills();
var currentScriptPath = getCurrentScriptPath();
var baseURL = currentScriptPath.replace('sdk.js', '');
window.contented = {
"loaded": false,
"baseURL": baseURL,
"__preInit": [],
/**
* initArea shows the contented upload widget over the top of a target DOM element.
*
* @param any element Drop target (string selector / DOMElement / jQuery)
* @param Function onUploaded Called with an array of upload IDs
* @param Function onClose Called when the widget is being destroyed
*/
"init": function(elementSelector, onUploaded, onClose) {
contented.__preInit.push([elementSelector, onUploaded, onClose]);
},
/**
* supportsDrop returns whether drag-and-drop is supported by this browser.
*
* @return bool
*/
"supportsDrop": function() {
return ('ondrop' in window && 'FormData' in window && 'FileReader' in window);
},
"getPreviewURL": function(id) {
return baseURL + "p/" + encodeURIComponent(id);
},
"getMultiPreviewURL": function(items) {
return baseURL + "p/" + encodeURIComponent(items.join("-"));
},
"getDownloadURL": function(id) {
return baseURL + "get/" + encodeURIComponent(id);
},
"getInfoJSONURL": function(id) {
return baseURL + "info/" + encodeURIComponent(id);
},
"getThumbnailURL": function(thumbnailType, id) {
return baseURL + "thumb/" + encodeURIComponent(thumbnailType) + "/" + encodeURIComponent(id);
},
"thumbnail": {
"small_square": "s",
"medium_square": "b",
"medium": "t",
"large": "m",
"xlarge": "l",
"xxlarge": "h"
}
};
// Load scripts // Load scripts
var needScripts = []; var needScripts = [];
if (typeof jQuery === "undefined") { if (typeof jQuery === "undefined") {
@ -460,5 +458,8 @@ var contented = (function() {
} }
loadScripts(needScripts, afterScriptsLoaded); loadScripts(needScripts, afterScriptsLoaded);
};
init();
})() })()