src/pyams_skin/resources/js/myams-loader.js
changeset 557 bca7a7e058a3
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/pyams_skin/resources/js/myams-loader.js	Thu Feb 13 11:43:31 2020 +0100
@@ -0,0 +1,44 @@
+/**
+ * MyAMS extensions loader
+ * This extension can be used when application or site is only using the core MyAMS package, to
+ * download a set of required extensions
+ */
+(function($, globals) {
+
+	var ams = globals.MyAMS;
+
+	// Array of already loaded extensions
+	var loaded_extensions = [];
+
+	/**
+	 * MyAMS require function
+	 * This function can be used to download a set of MyAMS packages
+	 *
+	 * @param packages: array of packages names to download
+	 * @param callback: a function callback which will be executed when all required packages
+	 *   have been downloaded
+	 */
+	ams.require = function(packages, callback) {
+		var deferred = [];
+		for (var index=0; index < packages.length; index++) {
+			var package_name = packages[index];
+			if (loaded_extensions.indexOf(package_name) < 0) {
+				if (package_name.startsWith('http://') || package_name.startsWith('https://')) {
+					var source = package_name;
+				} else {
+					source = ams.baseURL + 'myams-' + package_name + ams.devext + '.js';
+				}
+				deferred.push(ams.getScript(source, {async: true}, console.error));
+				loaded_extensions.push(package_name);
+			}
+		}
+		if (deferred.length > 0) {
+			$.when.apply($, deferred).then(function () {
+				callback();
+			});
+		} else {
+			callback();
+		}
+	};
+
+})(jQuery, this);