From 9943813f3779d2ede508a90dadd087fc0ad12f1f Mon Sep 17 00:00:00 2001 From: Ulrich-Matthias Schäfer Date: Mon, 19 Nov 2018 20:45:07 +0100 Subject: renamed `Doc` to `Svg` according to (#932) --- src/animation/Timeline.js | 19 +++++------- src/elements/Container.js | 2 +- src/elements/Doc.js | 78 ----------------------------------------------- src/elements/Element.js | 4 +-- src/elements/Svg.js | 78 +++++++++++++++++++++++++++++++++++++++++++++++ src/main.js | 6 ++-- 6 files changed, 92 insertions(+), 95 deletions(-) delete mode 100644 src/elements/Doc.js create mode 100644 src/elements/Svg.js (limited to 'src') diff --git a/src/animation/Timeline.js b/src/animation/Timeline.js index f1d540a..6abcb80 100644 --- a/src/animation/Timeline.js +++ b/src/animation/Timeline.js @@ -1,8 +1,7 @@ +import { globals } from '../utils/window.js' import { registerMethods } from '../utils/methods.js' import Animator from './Animator.js' -import { globals } from '../utils/window.js' - -var time = globals.window.performance || Date +import EventTarget from '../types/EventTarget.js' var makeSchedule = function (runnerInfo) { var start = runnerInfo.start @@ -11,15 +10,16 @@ var makeSchedule = function (runnerInfo) { return { start: start, duration: duration, end: end, runner: runnerInfo.runner } } -export default class Timeline { +export default class Timeline extends EventTarget { // Construct a new timeline on the given element constructor () { + super() + this._timeSource = function () { - return time.now() + let w = globals.window + return (w.performance || w.Date).now() } - this._dispatcher = globals.document.createElement('div') - // Store the timing variables this._startTime = 0 this._speed = 1.0 @@ -38,16 +38,13 @@ export default class Timeline { this._lastStepTime = 0 } - getEventTarget () { - return this._dispatcher - } - /** * */ // schedules a runner on the timeline schedule (runner, delay, when) { + // FIXME: how to sort? maybe by runner id? if (runner == null) { return this._runners.map(makeSchedule).sort(function (a, b) { return (a.start - b.start) || (a.duration - b.duration) diff --git a/src/elements/Container.js b/src/elements/Container.js index 9415341..b47972e 100644 --- a/src/elements/Container.js +++ b/src/elements/Container.js @@ -8,7 +8,7 @@ export default class Container extends Element { return this.toParent(parent) }) - // we need this so that Doc does not get removed + // we need this so that the root does not get removed this.node.firstElementChild || this.remove() return this diff --git a/src/elements/Doc.js b/src/elements/Doc.js deleted file mode 100644 index d56fae3..0000000 --- a/src/elements/Doc.js +++ /dev/null @@ -1,78 +0,0 @@ -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 Doc 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 Doc()) - }) - } -}) - -register(Doc, 'Doc', true) diff --git a/src/elements/Element.js b/src/elements/Element.js index 456ddad..1f208c5 100644 --- a/src/elements/Element.js +++ b/src/elements/Element.js @@ -15,7 +15,7 @@ import Dom from './Dom.js' import List from '../types/List.js' import SVGNumber from '../types/SVGNumber.js' -const Doc = getClass(root) +const Svg = getClass(root) export default class Element extends Dom { constructor (node, attrs) { @@ -57,7 +57,7 @@ export default class Element extends Dom { // Get parent document doc () { - let p = this.parent(Doc) + let p = this.parent(Svg) return p && p.doc() } 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) diff --git a/src/main.js b/src/main.js index 701b23b..919fb25 100644 --- a/src/main.js +++ b/src/main.js @@ -14,7 +14,6 @@ import Circle from './elements/Circle.js' import Color from './types/Color.js' import Container from './elements/Container.js' import Defs from './elements/Defs.js' -import Doc from './elements/Doc.js' import Dom from './elements/Dom.js' import Element from './elements/Element.js' import Ellipse from './elements/Ellipse.js' @@ -43,6 +42,7 @@ import Runner from './animation/Runner.js' import SVGArray from './types/SVGArray.js' import SVGNumber from './types/SVGNumber.js' import Shape from './elements/Shape.js' +import Svg from './elements/Svg.js' import Text from './elements/Text.js' import Tspan from './elements/Tspan.js' import * as defaults from './modules/core/defaults.js' @@ -90,7 +90,6 @@ export { default as Circle } from './elements/Circle.js' export { default as ClipPath } from './elements/ClipPath.js' export { default as Container } from './elements/Container.js' export { default as Defs } from './elements/Defs.js' -export { default as Doc } from './elements/Doc.js' export { default as Dom } from './elements/Dom.js' export { default as Element } from './elements/Element.js' export { default as Ellipse } from './elements/Ellipse.js' @@ -110,6 +109,7 @@ export { default as Rect } from './elements/Rect.js' export { default as Shape } from './elements/Shape.js' export { default as Stop } from './elements/Stop.js' export { default as Style } from './elements/Style.js' +export { default as Svg } from './elements/Svg.js' export { default as Symbol } from './elements/Symbol.js' export { default as Text } from './elements/Text.js' export { default as TextPath } from './elements/TextPath.js' @@ -117,7 +117,7 @@ export { default as Tspan } from './elements/Tspan.js' export { default as Use } from './elements/Use.js' extend([ - Doc, + Svg, Symbol, Image, Pattern, -- cgit v1.2.3