-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
127 lines (119 loc) · 3.11 KB
/
main.js
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/**
* dom
*
* This module provides DOM utility functions for simplifying common DOM
* operations, including traversal, manipulation, etc.
*/
define(['./lib/collection',
'./lib/traversal',
'./lib/manipulation',
'./lib/metrics',
'./lib/style',
'./lib/events',
'selector',
'class'],
function(Collection, Traversal, Manipulation, Metrics, Style, Events, select, clazz) {
/**
* Augment `Collection` with DOM utility functions.
*/
clazz.augment(Collection, Traversal);
clazz.augment(Collection, Manipulation);
clazz.augment(Collection, Metrics);
clazz.augment(Collection, Style);
clazz.augment(Collection, Events);
/**
* Returns a collection of DOM elements with usable utility functions.
*
* @param {Mixed} nodes
* @return {Collection}
* @api public
*/
function dom(nodes) {
// TODO: Document the various ways of using this function.
if (nodes instanceof Collection) return nodes;
if (typeof nodes == 'string') {
if (/^\s*<([^\s>]+)/.test(nodes)) {
// HTML fragment
nodes = dom.fragment(nodes);
} else {
// CSS selector
nodes = select(nodes)
}
}
return new Collection(nodes);
}
/**
* Creates a DOM element with tag and optional attributes and text.
*
* Examples:
*
* $.create('div');
*
* $.create('p', 'Hello');
*
* $.create('p', { 'id': 'welcome' }, 'Hello Jared');
*
* @param {String} tag
* @param {Object} attrs
* @param {String} text
* @return {Element} DOM element
* @api public
*/
dom.create = function(tag, attrs, text) {
if (typeof attrs == 'string') {
text = attrs;
attrs = {};
}
var el = document.createElement(tag);
for (var name in attrs) {
el.setAttribute(name, attrs[name]);
}
if (text) el.textContent = text;
return el;
}
/**
* Creates a DOM element from an HTML fragment.
*
* @param {String} html HTML fragment.
* @return {Array} DOM elements
* @api public
*/
dom.fragment = function(html) {
var div = document.createElement('div');
div.innerHTML = html;
return div.childNodes;
}
/**
* Augments DOM utilities with functions from `mixin`.
*
*
* Transition from jQuery or Zepto:
*
* `augment()` provides functionality similiar to that used to author jQuery
* plugins. jQuery is extended by adding functions to `jQuery.fn`:
*
* jQuery.fn.myUtility = function() { ... };
*
* In Anchor, this translates to:
*
* $.augment({
* myUtility: function() { ... }
* });
*
* Transition from Ender/Bonzo:
*
* This function is equivalent to `bonzo.aug()`. `bonzo.aug()` has an
* optional `target` argument that indicates the object to augment, which
* defaults to `Bonzo.prototype`. In Anchor, this argument is not available;
* the `Collection` DOM wrapper is always augmented.
*
*
* @param {Object} mixin.
* @api public
*/
dom.augment = function(mixin) {
clazz.augment(Collection, mixin);
return this;
}
return dom;
});