summaryrefslogtreecommitdiffstats
path: root/src/elements/Svg.js
diff options
context:
space:
mode:
authorUlrich-Matthias Schäfer <ulima.ums@googlemail.com>2018-11-19 20:45:07 +0100
committerUlrich-Matthias Schäfer <ulima.ums@googlemail.com>2018-11-19 20:45:07 +0100
commit9943813f3779d2ede508a90dadd087fc0ad12f1f (patch)
tree9e0d2c1008ed540936ca9675152f6ba79ff4bd64 /src/elements/Svg.js
parentd5a8faa4d06d2664a7748ea570937751ef5271d5 (diff)
downloadsvg.js-9943813f3779d2ede508a90dadd087fc0ad12f1f.tar.gz
svg.js-9943813f3779d2ede508a90dadd087fc0ad12f1f.zip
renamed `Doc` to `Svg` according to (#932)
Diffstat (limited to 'src/elements/Svg.js')
-rw-r--r--src/elements/Svg.js78
1 files changed, 78 insertions, 0 deletions
diff --git a/src/elements/Svg.js b/src/elements/Svg.js
new file mode 100644
index 0000000..e634c6a
--- /dev/null
+++ b/src/elements/Svg.js
@@ -0,0 +1,78 @@
+import {
+ adopt,
+ nodeOrNew,
+ register,
+ wrapWithAttrCheck
+} from '../utils/adopter.js'
+import { ns, svgjs, xlink, xmlns } from '../modules/core/namespaces.js'
+import { registerMethods } from '../utils/methods.js'
+import Container from './Container.js'
+import Defs from './Defs.js'
+import { globals } from '../utils/window.js'
+
+export default class Svg extends Container {
+ constructor (node) {
+ super(nodeOrNew('svg', node), node)
+ this.namespace()
+ }
+
+ isRoot () {
+ return !this.node.parentNode ||
+ !(this.node.parentNode instanceof globals.window.SVGElement) ||
+ this.node.parentNode.nodeName === '#document'
+ }
+
+ // Check if this is a root svg
+ // If not, call docs from this element
+ doc () {
+ if (this.isRoot()) return this
+ return super.doc()
+ }
+
+ // Add namespaces
+ namespace () {
+ if (!this.isRoot()) return this.doc().namespace()
+ return this
+ .attr({ xmlns: ns, version: '1.1' })
+ .attr('xmlns:xlink', xlink, xmlns)
+ .attr('xmlns:svgjs', svgjs, xmlns)
+ }
+
+ // Creates and returns defs element
+ defs () {
+ if (!this.isRoot()) return this.doc().defs()
+
+ return adopt(this.node.getElementsByTagName('defs')[0]) ||
+ this.put(new Defs())
+ }
+
+ // custom parent method
+ parent (type) {
+ if (this.isRoot()) {
+ return this.node.parentNode.nodeName === '#document'
+ ? null
+ : adopt(this.node.parentNode)
+ }
+
+ return super.parent(type)
+ }
+
+ clear () {
+ // remove children
+ while (this.node.hasChildNodes()) {
+ this.node.removeChild(this.node.lastChild)
+ }
+ return this
+ }
+}
+
+registerMethods({
+ Container: {
+ // Create nested svg document
+ nested: wrapWithAttrCheck(function () {
+ return this.put(new Svg())
+ })
+ }
+})
+
+register(Svg, 'Svg', true)