forked from npmcomponent/tower-directive
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
136 lines (111 loc) · 2.91 KB
/
index.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
128
129
130
131
132
133
134
135
136
/**
* Module dependencies.
*/
// commented out by npm-component: var Emitter = require('tower-emitter');
// commented out by npm-component: var compile = require('tower-directive-expression');
// commented out by npm-component: var content = require('tower-content');
var statics = require('./lib/statics');
var proto = require('./lib/proto');
/**
* Global document (for client and server).
*/
var document = 'undefined' === typeof document
? undefined // tower/server-dom
: window.document;
/**
* Expose `directive`.
*/
exports = module.exports = directive;
/**
* Expose `collection`.
*/
exports.collection = [];
/**
* Get/set directive function.
*
* @param {String} name The directive's name.
* @param {Function} fn Function called on directive definition.
* @return {Directive} A `Directive` object.
* @api public
*/
function directive(name, fn, manualCompile) {
if (undefined === fn && exports.collection[name])
return exports.collection[name];
/**
* Class representing the extensions to HTML.
*
* @class
*
* @param {String} name The directive's name.
* @param {Function} The directive function to be executed.
* @api private
*/
function Directive(el, attrs) {
this.name = name;
this.attrs = attrs;
this.document = document;
// attribute, text, element, comment
if (1 === el.nodeType) {
if (this.element) {
// XXX: compile attributes for element?
} else if (this.attribute) {
var val = el.getAttribute(this.name);
if (val) {
attrs[this.name] = exports.expression(Directive._expression, val);
}
}
}
}
Directive.id = name;
Directive.expressions = {};
Directive.prototype.attribute = true;
Directive.prototype.processChildren = true;
if (fn) {
if (manualCompile || 1 === fn.length) {
Directive._compile = fn;
} else {
Directive._exec = fn;
}
}
for (var key in statics) Directive[key] = statics[key];
for (var key in proto) Directive.prototype[key] = proto[key];
exports.collection[name] = Directive;
exports.collection.push(Directive);
exports.emit('define', Directive);
return Directive;
}
/**
* Mixin `Emitter`.
*/
Emitter(exports);
/**
* Get/compile directive expression.
*/
exports.expression = function(name, val, opts, fn){
var Directive = exports(name);
return Directive.expressions[val]
= Directive.expressions[val]
|| compile.apply(null, arguments);
};
/**
* Check if a directive is defined.
*
* @param {String} name A directive name.
* @return {Boolean} true if the `Directive` has been defined, but false otherwise
* @api public
*/
exports.defined = function(name){
return exports.collection.hasOwnProperty(name);
};
/**
* Clear all directives.
*
* @chainable
* @return {Function} exports The main `directive` function.
* @api public
*/
exports.clear = function(){
exports.off();
exports.collection = [];
return exports;
};