1 | var inherits = require('inherits')
|
2 | var domify = require('domify')
|
3 | var insert = require('insert')
|
4 | var css = require('dom-css')
|
5 | var attr = require('./lib/attr')
|
6 | var html = require('./lib/html')
|
7 | var classes = require('dom-classes')
|
8 |
|
9 | module.exports = Elle
|
10 | function Elle(element) {
|
11 | if (!(this instanceof Elle))
|
12 | return new Elle(element)
|
13 | this[0] = toElement(element) || document.createElement('div')
|
14 | }
|
15 |
|
16 |
|
17 | inherits(Elle, Array)
|
18 |
|
19 | var Proto = Elle.prototype
|
20 |
|
21 |
|
22 | Object.defineProperty(Proto, 'view', {
|
23 | get: function() {
|
24 | return this[0]
|
25 | },
|
26 | enumerable: true,
|
27 | configurable: true
|
28 | })
|
29 |
|
30 |
|
31 |
|
32 | Proto.css = wrap(css)
|
33 | Proto.html = property(html)
|
34 | Proto.attr = property(attr, 1)
|
35 |
|
36 | Proto.parent = function() {
|
37 | return Elle(this[0].parentNode)
|
38 | }
|
39 |
|
40 |
|
41 | Proto.hasClass = function(name) {
|
42 | return classes.has(this[0], name)
|
43 | }
|
44 |
|
45 | ;['remove', 'add', 'toggle']
|
46 | .forEach(function(name) {
|
47 | Proto[name+'Class'] = wrap(classes[name])
|
48 | })
|
49 |
|
50 |
|
51 | ;['remove', 'prepend', 'append',
|
52 | 'before', 'after', 'replace']
|
53 | .forEach(function(name) {
|
54 | Proto[name] = wrap(insert[name], toElement)
|
55 | })
|
56 |
|
57 |
|
58 | Proto.appendTo = wrapTarget(insert.append)
|
59 | Proto.prependTo = wrapTarget(insert.prepend)
|
60 | Proto.insertBefore = wrapTarget(insert.before)
|
61 | Proto.insertAfter = wrapTarget(insert.after)
|
62 |
|
63 |
|
64 |
|
65 | function wrap(fn, map) {
|
66 | return function(...args) {
|
67 | if (map)
|
68 | args = args.map(map)
|
69 | var a = [this[0], ...args]
|
70 | fn(...a)
|
71 | return this
|
72 | }
|
73 | }
|
74 |
|
75 |
|
76 | function property(fn, expected) {
|
77 | return function(...args) {
|
78 | var a = [this[0], ...args]
|
79 | var ret = fn(...a)
|
80 | return args.length===(expected|0) ? ret : this
|
81 | }
|
82 | }
|
83 |
|
84 |
|
85 | function wrapTarget(fn) {
|
86 | return function(target) {
|
87 | fn(toElement(target), this[0])
|
88 | return this
|
89 | }
|
90 | }
|
91 |
|
92 | function toElement(e) {
|
93 | if (typeof e === 'string')
|
94 | return domify(e)
|
95 | else if (e && e.view && e.view.nodeType)
|
96 | return e.view
|
97 | return e
|
98 | } |
\ | No newline at end of file |