aboutsummaryrefslogtreecommitdiffstats
path: root/src/tools.js
blob: 14e19a302c53a9c42c2da42b187f148a69eab1bf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import { ns } from './namespaces.js'
import { getClass } from './adopter.js'

export function nodeOrNew (name, node) {
  return node || makeNode(name)
}

// Method for element creation
export function makeNode (name) {
  // create element
  return document.createElementNS(ns, name)
}

// 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]
    }
  }
}

// FIXME: enhanced constructors here
export function addFactory (modules, methods) {
  extend(modules, methods)
}

// Invent new element
export function invent (config) {
  // Create element initializer
  var initializer = typeof config.create === 'function' ? config.create
    : function (node) {
      config.inherit.call(this, node || makeNode(config.create))
    }

  // Inherit prototype
  if (config.inherit) {
    /* eslint new-cap: "off" */
    initializer.prototype = new config.inherit()
    initializer.prototype.constructor = initializer
  }

  // Extend with methods
  if (config.extend) {
    extend(initializer, config.extend)
  }

  // Attach construct method to parent
  if (config.construct) { extend(config.parent || getClass('Container'), config.construct) }

  return initializer
}