diff options
Diffstat (limited to 'src/Parent.js')
-rw-r--r-- | src/Parent.js | 194 |
1 files changed, 129 insertions, 65 deletions
diff --git a/src/Parent.js b/src/Parent.js index ce22f80..e2c725c 100644 --- a/src/Parent.js +++ b/src/Parent.js @@ -1,93 +1,157 @@ -import {makeInstance} from './helpers.js' -import Element from './Element.js' -import {adopt} from './tools.js' +import {makeInstance, adopt} from './adopter.js' import {map} from './utils.js' -export default class Parent extends Element { - // Returns all child elements - children () { - return map(this.node.children, function (node) { - return adopt(node) - }) + +// Returns all child elements +export function children () { + return map(this.node.children, function (node) { + return adopt(node) + }) +} + +// Add given element at a position +export function add (element, i) { + element = makeInstance(element) + + if (element.node !== this.node.children[i]) { + this.node.insertBefore(element.node, this.node.children[i] || null) } - // Add given element at a position - add (element, i) { - element = makeInstance(element) + return this +} + +// Basically does the same as `add()` but returns the added element instead +export function put (element, i) { + this.add(element, i) + return element.instance || element +} + +// Checks if the given element is a child +export function has (element) { + return this.index(element) >= 0 +} + +// Gets index of given element +export function index (element) { + return [].slice.call(this.node.children).indexOf(element.node) +} + +// Get a element at the given index +export function get (i) { + return adopt(this.node.children[i]) +} + +// Get first child +export function first () { + return this.get(0) +} - if (element.node !== this.node.children[i]) { - this.node.insertBefore(element.node, this.node.children[i] || null) +// Get the last child +export function last () { + return this.get(this.node.children.length - 1) +} + +// Iterates over all children and invokes a given block +export function each (block, deep) { + var children = this.children() + var i, il + + for (i = 0, il = children.length; i < il; i++) { + if (children[i] instanceof Base) { + block.apply(children[i], [i, children]) } - return this + if (deep && (children[i] instanceof Base && children[i].is('Parent'))) { + children[i].each(block, deep) + } } - // Basically does the same as `add()` but returns the added element instead - put (element, i) { - this.add(element, i) - return element.instance || element - } + return this +} - // Checks if the given element is a child - has (element) { - return this.index(element) >= 0 - } +// Remove a given child +export function removeElement (element) { + this.node.removeChild(element.node) - // Gets index of given element - index (element) { - return [].slice.call(this.node.children).indexOf(element.node) - } + return this +} - // Get a element at the given index - get (i) { - return adopt(this.node.children[i]) +// Remove all elements in this container +export function clear () { + // remove children + while (this.node.hasChildNodes()) { + this.node.removeChild(this.node.lastChild) } - // Get first child - first () { - return this.get(0) - } + // remove defs reference + delete this._defs - // Get the last child - last () { - return this.get(this.node.children.length - 1) - } + return this +} - // Iterates over all children and invokes a given block - each (block, deep) { - var children = this.children() - var i, il +// Import raw svg +export function svg (svg) { + var well, len - for (i = 0, il = children.length; i < il; i++) { - if (children[i] instanceof Element) { - block.apply(children[i], [i, children]) - } + // act as a setter if svg is given + if (svg) { + // create temporary holder + well = document.createElementNS(ns, 'svg') + // dump raw svg + well.innerHTML = svg - if (deep && (children[i] instanceof Parent)) { - children[i].each(block, deep) - } + // transplant nodes + for (len = well.children.length; len--;) { + this.node.appendChild(well.firstElementChild) } - return this + // otherwise act as a getter + } else { + // write svgjs data to the dom + this.writeDataToDom() + + return this.node.outerHTML } - // Remove a given child - removeElement (element) { - this.node.removeChild(element.node) + return this +} + +// write svgjs data to the dom +export function writeDataToDom () { + // dump variables recursively + this.each(function () { + this.writeDataToDom() + }) - return this + // remove previously set data + this.node.removeAttribute('svgjs:data') + + if (Object.keys(this.dom).length) { + this.node.setAttribute('svgjs:data', JSON.stringify(this.dom)) // see #428 } + return this +} - // Remove all elements in this container - clear () { - // remove children - while (this.node.hasChildNodes()) { - this.node.removeChild(this.node.lastChild) - } +export function flatten (parent) { + this.each(function () { + if (this.is('Parent')) return this.flatten(parent).ungroup(parent) + return this.toParent(parent) + }) - // remove defs reference - delete this._defs + // we need this so that Doc does not get removed + this.node.firstElementChild || this.remove() - return this - } + return this +} + +export function ungroup (parent) { + parent = parent || this.parent() + + this.each(function () { + return this.toParent(parent) + }) + + this.remove() + + return this } |