Commit 6e2bb1b7 authored by Thimo Kraemer's avatar Thimo Kraemer

Added functionality to move/copy dom content

parent 5998748c
/*
* KSS-RPC v0.14-beta
* KSS-RPC v0.15-beta
*
* Copyright (c) 2017, joonis new media
* Author: Thimo Kraemer <thimo.kraemer@joonis.de>
......@@ -61,7 +61,7 @@
******************************************************************************************/
var kss = {
version: '0.14-beta',
version: '0.15-beta',
_initialized: false,
_ruleSheets: {},
_eventBinders: {},
......@@ -1752,6 +1752,53 @@ kss.cssQuery = function(selector, context, dropContext) {
catch (err) { return result; } // IE8
};
kss.insertNodes = function(nodes, toNode, position, copy) {
if (typeof toNode == 'string') {
toNode = document.getElementById(toNode);
if (!toNode) {
throw new Error('Target element not found to insert nodes');
}
}
if (!kss.isArrayLike(nodes)) {
nodes = [nodes];
}
var parentNode, beforeNode;
switch (position) {
case 'beforebegin':
parentNode = toNode.parentNode;
beforeNode = toNode;
break;
case 'afterbegin':
parentNode = toNode;
beforeNode = toNode.firstChild;
break;
case 'replaceinner':
toNode.innerHTML = '';
case 'beforeend':
parentNode = toNode;
beforeNode = null;
break;
case 'replace':
case 'afterend':
parentNode = toNode.parentNode;
beforeNode = toNode.nextSibling;
break;
default:
throw new Error('Invalid position to insert: ' + position);
}
if (position == 'replace') {
parentNode.removeChild(toNode);
}
kss.each(nodes, function(node) {
if (copy) {
node = node.cloneNode(true);
}
// Appends to parentNode if beforeNode is null
parentNode.insertBefore(node, beforeNode);
});
kss.applyRules(parentNode);
};
kss.parseHTML = function(html, runScripts) {
var div = document.createElement('div');
div.innerHTML = html;
......@@ -1773,44 +1820,33 @@ kss.parseHTML = function(html, runScripts) {
return nodes;
};
kss._insertHTMLBefore = function(parent, node, html, runScripts) {
node = node || null;
kss.insertHTML = function(node, html, position, runScripts) {
var nodes = kss.parseHTML(html, runScripts);
kss.each(nodes, function(child) {
// Appends to parent if node is null
parent.insertBefore(child, node);
});
kss.applyRules(parent);
kss.insertNodes(nodes, node, position);
};
kss.replaceInnerHTML = function(node, html, runScripts) {
node.innerHTML = '';
kss._insertHTMLBefore(node, null, html, runScripts);
kss.insertHTML(node, html, 'replaceinner', runScripts);
};
kss.replaceHTML = function(node, html, runScripts) {
var parent = node.parentNode;
var nextSibling = node.nextElementSibling;
parent.removeChild(node);
kss._insertHTMLBefore(parent, nextSibling, html, runScripts);
kss.insertHTML(node, html, 'replace', runScripts);
};
kss.appendHTML = function(node, html, runScripts) {
kss._insertHTMLBefore(node, null, html, runScripts);
kss.insertHTML(node, html, 'beforeend', runScripts);
};
kss.prependHTML = function(node, html, runScripts) {
kss._insertHTMLBefore(
node, node.firstElementChild, html, runScripts);
kss.insertHTML(node, html, 'afterbegin', runScripts);
};
kss.insertHTMLAfter = function(node, html, runScripts) {
kss._insertHTMLBefore(
node.parentNode, node.nextElementSibling, html, runScripts);
kss.insertHTML(node, html, 'afterend', runScripts);
};
kss.insertHTMLBefore = function(node, html, runScripts) {
kss._insertHTMLBefore(node.parentNode, node, html, runScripts);
kss.insertHTML(node, html, 'beforebegin', runScripts);
};
kss.deleteNode = function(node) {
......@@ -1818,6 +1854,22 @@ kss.deleteNode = function(node) {
kss._cleanUp();
};
kss.appendNodes = function(nodes, toNode, copy) {
kss.insertNodes(nodes, toNode, 'beforeend', copy);
};
kss.prependNodes = function(nodes, toNode, copy) {
kss.insertNodes(nodes, toNode, 'afterbegin', copy);
};
kss.insertNodesAfter = function(nodes, toNode, copy) {
kss.insertNodes(nodes, toNode, 'afterend', copy);
};
kss.insertNodesBefore = function(nodes, toNode, copy) {
kss.insertNodes(nodes, toNode, 'beforebegin', copy);
};
kss.getNodeAttr = function(node, name, recursive) {
do {
var value = node.getAttribute(name);
......@@ -2268,6 +2320,32 @@ kss.registerActionProvider({
deleteNode: function(node, params) {
kss.deleteNode(node);
},
// Actions to move content
moveNodeBefore: function(node, params) {
kss.insertNodesBefore(node, params.htmlId);
},
moveNodeAfter: function(node, params) {
kss.insertNodesAfter(node, params.htmlId);
},
moveNodeAsFirstChild: function(node, params) {
kss.prependNodes(node, params.htmlId);
},
moveNodeAsLastChild: function(node, params) {
kss.appendNodes(node, params.htmlId);
},
// Actions to copy content
copyNodeBefore: function(node, params) {
kss.insertNodesBefore(node, params.htmlId, true);
},
copyNodeAfter: function(node, params) {
kss.insertNodesAfter(node, params.htmlId, true);
},
copyNodeAsFirstChild: function(node, params) {
kss.prependNodes(node, params.htmlId, true);
},
copyNodeAsLastChild: function(node, params) {
kss.appendNodes(node, params.htmlId, true);
},
// Actions to set attributes
setNodeAttr: function(node, params) {
kss.setNodeAttr(node, params.name, params.value);
......
This diff is collapsed.
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