summaryrefslogtreecommitdiffstats
path: root/src/utils
diff options
context:
space:
mode:
authorUlrich-Matthias Schäfer <ulima.ums@googlemail.com>2018-11-06 13:48:05 +0100
committerUlrich-Matthias Schäfer <ulima.ums@googlemail.com>2018-11-06 13:48:05 +0100
commita0b13ebcacfd74b9f521110c7225bb404325bcd3 (patch)
treea07c5cc422645e31d7dfef81ce4e54f03f0945f6 /src/utils
parent9f2696e8a2cf7e4eebc1cc7e31027fe2070094fa (diff)
downloadsvg.js-a0b13ebcacfd74b9f521110c7225bb404325bcd3.tar.gz
svg.js-a0b13ebcacfd74b9f521110c7225bb404325bcd3.zip
reordered modules, add es6 build
Diffstat (limited to 'src/utils')
-rw-r--r--src/utils/adopter.js115
-rw-r--r--src/utils/methods.js32
-rw-r--r--src/utils/utils.js96
3 files changed, 243 insertions, 0 deletions
diff --git a/src/utils/adopter.js b/src/utils/adopter.js
new file mode 100644
index 0000000..8017359
--- /dev/null
+++ b/src/utils/adopter.js
@@ -0,0 +1,115 @@
+import { capitalize } from './utils.js'
+import { ns } from '../modules/core/namespaces.js'
+import Base from '../types/Base.js'
+
+const elements = {}
+export const root = Symbol('root')
+
+// Method for element creation
+export function makeNode (name) {
+ // create element
+ return document.createElementNS(ns, name)
+}
+
+export function makeInstance (element) {
+ if (element instanceof Base) return element
+
+ if (typeof element === 'object') {
+ return adopt(element)
+ }
+
+ if (element == null) {
+ return new elements[root]()
+ }
+
+ if (typeof element === 'string' && element.charAt(0) !== '<') {
+ return adopt(document.querySelector(element))
+ }
+
+ var node = makeNode('svg')
+ node.innerHTML = element
+
+ // We can use firstChild here because we know,
+ // that the first char is < and thus an element
+ element = adopt(node.firstChild)
+
+ return element
+}
+
+export function nodeOrNew (name, node) {
+ return node || makeNode(name)
+}
+
+// Adopt existing svg elements
+export function adopt (node) {
+ // check for presence of node
+ if (!node) return null
+
+ // make sure a node isn't already adopted
+ if (node.instance instanceof Base) return node.instance
+
+ if (!(node instanceof window.SVGElement)) {
+ return new elements.HtmlNode(node)
+ }
+
+ // initialize variables
+ var element
+
+ // adopt with element-specific settings
+ if (node.nodeName === 'svg') {
+ element = new elements[root](node)
+ } else if (node.nodeName === 'linearGradient' || node.nodeName === 'radialGradient') {
+ element = new elements.Gradient(node)
+ } else if (elements[capitalize(node.nodeName)]) {
+ element = new elements[capitalize(node.nodeName)](node)
+ } else {
+ element = new elements.Bare(node)
+ }
+
+ return element
+}
+
+export function register (element, name = element.name, asRoot = false) {
+ elements[name] = element
+ if (asRoot) elements[root] = element
+ return element
+}
+
+export function getClass (name) {
+ return elements[name]
+}
+
+// Element id sequence
+let did = 1000
+
+// Get next named element id
+export function eid (name) {
+ return 'Svgjs' + capitalize(name) + (did++)
+}
+
+// Deep new id assignment
+export function assignNewId (node) {
+ // do the same for SVG child nodes as well
+ for (var i = node.children.length - 1; i >= 0; i--) {
+ assignNewId(node.children[i])
+ }
+
+ if (node.id) {
+ return adopt(node).id(eid(node.nodeName))
+ }
+
+ return adopt(node)
+}
+
+// Method for extending objects
+export function extend (modules, methods) {
+ var key, i
+
+ modules = Array.isArray(modules) ? modules : [modules]
+
+ for (i = modules.length - 1; i >= 0; i--) {
+ for (key in methods) {
+ modules[i].prototype[key] = methods[key]
+ }
+ }
+}
diff --git a/src/utils/methods.js b/src/utils/methods.js
new file mode 100644
index 0000000..2373445
--- /dev/null
+++ b/src/utils/methods.js
@@ -0,0 +1,32 @@
+const methods = {}
+const constructors = {}
+
+export function registerMethods (name, m) {
+ if (Array.isArray(name)) {
+ for (let _name of name) {
+ registerMethods(_name, m)
+ }
+ return
+ }
+
+ if (typeof name === 'object') {
+ for (let [_name, _m] of Object.entries(name)) {
+ registerMethods(_name, _m)
+ }
+ return
+ }
+
+ methods[name] = Object.assign(methods[name] || {}, m)
+}
+
+export function getMethodsFor (name) {
+ return methods[name] || {}
+}
+
+export function registerConstructor (name, setup) {
+ constructors[name] = setup
+}
+
+export function getConstructor (name) {
+ return constructors[name] ? { setup: constructors[name], name } : {}
+}
diff --git a/src/utils/utils.js b/src/utils/utils.js
new file mode 100644
index 0000000..e3c9111
--- /dev/null
+++ b/src/utils/utils.js
@@ -0,0 +1,96 @@
+// Map function
+export function map (array, block) {
+ var i
+ var il = array.length
+ var result = []
+
+ for (i = 0; i < il; i++) {
+ result.push(block(array[i]))
+ }
+
+ return result
+}
+
+// Filter function
+export function filter (array, block) {
+ var i
+ var il = array.length
+ var result = []
+
+ for (i = 0; i < il; i++) {
+ if (block(array[i])) { result.push(array[i]) }
+ }
+
+ return result
+}
+
+// Degrees to radians
+export function radians (d) {
+ return d % 360 * Math.PI / 180
+}
+
+// Radians to degrees
+export function degrees (r) {
+ return r * 180 / Math.PI % 360
+}
+
+// Convert dash-separated-string to camelCase
+export function camelCase (s) {
+ return s.toLowerCase().replace(/-(.)/g, function (m, g) {
+ return g.toUpperCase()
+ })
+}
+
+// Capitalize first letter of a string
+export function capitalize (s) {
+ return s.charAt(0).toUpperCase() + s.slice(1)
+}
+
+// Calculate proportional width and height values when necessary
+export function proportionalSize (element, width, height) {
+ if (width == null || height == null) {
+ var box = element.bbox()
+
+ if (width == null) {
+ width = box.width / box.height * height
+ } else if (height == null) {
+ height = box.height / box.width * width
+ }
+ }
+
+ return {
+ width: width,
+ height: height
+ }
+}
+
+export function getOrigin (o, element) {
+ // Allow origin or around as the names
+ let origin = o.origin // o.around == null ? o.origin : o.around
+ let ox, oy
+
+ // Allow the user to pass a string to rotate around a given point
+ if (typeof origin === 'string' || origin == null) {
+ // Get the bounding box of the element with no transformations applied
+ const string = (origin || 'center').toLowerCase().trim()
+ const { height, width, x, y } = element.bbox()
+
+ // Calculate the transformed x and y coordinates
+ let bx = string.includes('left') ? x
+ : string.includes('right') ? x + width
+ : x + width / 2
+ let by = string.includes('top') ? y
+ : string.includes('bottom') ? y + height
+ : y + height / 2
+
+ // Set the bounds eg : "bottom-left", "Top right", "middle" etc...
+ ox = o.ox != null ? o.ox : bx
+ oy = o.oy != null ? o.oy : by
+ } else {
+ ox = origin[0]
+ oy = origin[1]
+ }
+
+ // Return the origin as it is if it wasn't a string
+ return [ ox, oy ]
+}