src/ztfy/myams/resources/js/myams.js
changeset 192 d58af89193e3
parent 189 688803362e9d
child 193 68dd5a05c059
--- a/src/ztfy/myams/resources/js/myams.js	Wed Nov 30 16:01:07 2016 +0100
+++ b/src/ztfy/myams/resources/js/myams.js	Fri Dec 09 12:55:38 2016 +0100
@@ -540,6 +540,16 @@
 		}
 	};
 
+	/**
+	 * Check to know if given element is still present in DOM
+	 */
+	MyAMS.isInDOM = function(element) {
+		element = $(element);
+		if (!element.exists()) {
+			return false;
+		}
+		return globals.document.contains(element[0]);
+	};
 
 	/**
 	 * Get script or CSS file using browser cache
@@ -2500,7 +2510,7 @@
 			// Inner plug-in loader function
 			var plugin;
 
-			function _loadPlugin() {
+			function _loadPlugin(reload) {
 				var index;
 				var callbacks = plugin.callbacks,
 					callback;
@@ -2522,9 +2532,9 @@
 						ams.plugins.enabled[name] = null;
 					}
 				}
-				// If running in async mode, registered plug-ins are run
+				// If running in async mode, newly registered plug-ins are run
 				// before callback is called so we call plug-in manually
-				if (callbacks && callbacks.length && (plugin.async !== false)) {
+				if ((reload !== true) && callbacks && callbacks.length && (plugin.async !== false)) {
 					for (index=0; index < callbacks.length; index++) {
 						callback = callbacks[index];
 						ams.executeFunctionByName(callback.callback, element, callback.context);
@@ -2532,12 +2542,27 @@
 				}
 			}
 
+			function _checkPluginContext() {
+				// Update context for an already loaded plug-in
+				var enabled = ams.plugins.enabled[name];
+				// Clean all plug-in contexts
+				for (index=0; index < enabled.length; index++) {
+					var callback = enabled[index];
+					if (callback && callback.context && !ams.isInDOM(callback.context)) {
+						enabled[index] = null;
+					}
+				}
+			}
+
 			for (name in plugins) {
+				plugin = plugins[name];
 				if (ams.plugins.enabled[name] === undefined) {
-					plugin = plugins[name];
 					ams.getScript(plugin.src, _loadPlugin, {
 						async: plugin.async === undefined ? true : plugin.async
 					});
+				} else {
+					_checkPluginContext();
+					_loadPlugin(true);
 				}
 			}
 
@@ -2563,7 +2588,9 @@
 										callback(element);
 										break;
 									default:
-										callback.callback(callback.context);
+										if (callback && callback.callback) {
+											callback.callback(callback.context);
+										}
 								}
 							}
 					}
@@ -4924,6 +4951,11 @@
 		// Initialize custom change handlers
 		$(document).on('change', '[data-ams-change-handler]', function(e) {
 			var source = $(this);
+			// Disable change handlers for readonly inputs
+			// These change handlers are activated by IE!!!
+			if (source.prop('readonly')) {
+				return;
+			}
 			var handlers = source.data('ams-disabled-handlers');
 			if ((handlers === true) || (handlers === 'change') || (handlers === 'all')) {
 				return;