Commit 5930c4f2 by Thimo Kraemer

Added the optional usage of a MutationObserver

parent 6ce7fda0
/*
* KSS-RPC v0.16-beta
* KSS-RPC v0.17-beta
*
* Copyright (c) 2017, joonis new media
* Author: Thimo Kraemer <thimo.kraemer@joonis.de>
......@@ -61,7 +61,7 @@
******************************************************************************************/
var kss = {
version: '0.16-beta',
version: '0.17-beta',
_initialized: false,
_ruleSheets: {},
_eventBinders: {},
......@@ -70,7 +70,9 @@ var kss = {
_actionProviders: {},
_paramProviders: {},
_stateVars: {},
_observer: null,
// Default configuration
useObserver: false,
logging: 'warn',
config: {
protocol: 'json-rpc', /* json-rpc | xml-rpc | url-encoded | json */
......@@ -153,16 +155,89 @@ kss._init = function() {
}
});
kss.info('Found ' + count + ' rulesheet link(s) in document');
setInterval(kss._cleanUp, 60000);
window.addEventListener('unload', kss._cleanUp, false);
// MutationObserver
if (kss.useObserver && window.MutationObserver) {
kss.info('Using MutationObserver: yes');
kss._observer = new MutationObserver(kss._observe);
kss._observer.observe(
document.documentElement,
{childList: true, subtree: true}
);
}
else {
kss.info('Using MutationObserver: no');
setInterval(kss._cleanUp, 60000);
}
window.addEventListener('unload', function() {
kss._cleanUp(true);
}, false);
};
if (typeof window != 'undefined') {
document.addEventListener('DOMContentLoaded', kss._init, false);
}
kss._cleanUp = function(purgeAll) {
kss._observe = function(records, observer) {
var applyRulesTo = [];
var cleanUp = false;
function isRelevantNode(node) {
if (node.nodeType == 1) {
var tag = node.tagName.toLowerCase();
if(tag != 'script' && tag != 'style')
return true;
}
return false;
}
kss.each(records, function(record) {
if (record.type != 'childList') {
return;
}
var target = record.target;
var tag = target.tagName.toLowerCase();
if (tag == 'html' || tag == 'head') {
return;
}
// Check if we need a clean up
if (!cleanUp) {
cleanUp = kss.each(record.removedNodes, function(node) {
if (isRelevantNode(node)) return true;
}) || false;
}
// Check if we need to setup rules
var setupRules = kss.each(record.addedNodes, function(node) {
if (isRelevantNode(node) && node.parentNode) return true;
}) || false;
if (setupRules) {
// Skip nodes that are already part of the other
for (var i=applyRulesTo.length-1; i >= 0; i--) {
var node = applyRulesTo[i];
if (target.contains(node)) {
applyRulesTo.splice(i, 1);
}
else if (node.contains(target)) {
setupRules = false;
break;
}
}
if (setupRules) {
applyRulesTo.push(target);
}
}
});
if (cleanUp) {
kss._cleanUp(false, true);
}
kss.each(applyRulesTo, function(context) {
kss.applyRules(context, true);
});
};
kss._cleanUp = function(purgeAll, observed) {
if (kss._observer && !observed && !purgeAll) {
return;
}
var counter = 0;
var html = document.documentElement;
var handlers = kss._activeHandlers;
......@@ -567,8 +642,10 @@ kss.addRuleSheet = function(url, config) {
});
};
kss.applyRules = function(context) {
kss._cleanUp();
kss.applyRules = function(context, observed) {
if (kss._observer && !observed) {
return;
}
kss.each(kss._ruleSheets, function (sheet) {
sheet.apply(context);
});
......@@ -1797,6 +1874,9 @@ kss.insertNodes = function(nodes, toNode, position, copy) {
// Appends to parentNode if beforeNode is null
parentNode.insertBefore(node, beforeNode);
});
if (position == 'replace' || position == 'replaceinner' || !copy) {
kss._cleanUp();
}
kss.applyRules(parentNode);
};
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment