-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
160 lines (140 loc) · 3.48 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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/**********************
* Module Dependencies.
*********************/
var debug = require('debug')('glint-block');
var isBrowser = require('is-browser');
var slice = require('sliced');
var inherits = require('inherits');
var EventEmitter = require('events').EventEmitter;
/**
* Expose Block element.
*/
exports = module.exports = Block;
inherits(Block, EventEmitter);
/**
* Initialize a new `Block` element.
* @param {[type]} options [description]
*/
function Block(block) {
if (!(this instanceof Block)) return new Block(block);
this.buffer = [];
this.delegate(block);
this.init();
}
/****************
* API Functions.
***************/
Block.prototype.api = Block.api = 'block';
['id', 'selector', 'el', 'place'].forEach(function(attribute) {
Block.prototype[attribute] = function(value) {
this.emit(attribute, value);
if (typeof value !== 'undefined') {
this.send(attribute, value);
return this;
}
return this.send(attribute);
};
});
// blocks must implement:
// - load, edit, save
// the other methods are optional:
// - cancel, hasChanged, isValid
['load', 'edit', 'save', 'cancel', 'hasChanged', 'isValid',].forEach(function(name) {
Block.prototype[name] = function(arg) {
var errorReturn = '';
var args = slice(arguments);
this.emit.apply(this, ['pre-' + name].concat(args));
var result = errorReturn;
try {
result = this.send.apply(this, [name].concat(args));
} catch (e) {
var message = 'block command "' + name + '" was not successful';
debug(message, e.message, e.stack);
console.log(message, ',arguments', args, ',selector', this.selector(), ',el', this.el());
result = arg || errorReturn;
}
this.emit.apply(this, ['post-' + name].concat(args));
return result;
};
});
/**
* Forward function calls to this Implementation
*/
Block.prototype.delegate = function(block) {
if (!block) return this.block;
this.block = block;
// execute buffered commands
while (this.buffer.length > 0) {
var cmd = this.buffer.shift();
this.exec(cmd);
}
return this;
};
Block.prototype.undelegate = function(block) {
this.block = undefined;
};
/**
* Use the given `plugin`.
*
* @param {Function} plugin
* @api private
*/
Block.prototype.use = function(plugin) {
plugin(this);
return this;
};
/**
* Mixin the given `mixins` functions.
* @param {Object} mixins e.g.
*
* { key : function fn () {} }
*
* @returns {Block}
*/
Block.prototype.mixin = function(mixins) {
var self = this;
Object.keys(mixins).forEach(function(key) {
self[key] = mixins[key];
});
return this;
};
/****************
* Base functions.
***************/
Block.prototype.init = function() {
this.on('selector', function(selector) {
if (isBrowser && selector) this.send('el', document.querySelector(selector));
});
};
Block.prototype.send = function(command, rest) {
var args = slice(arguments);
if (!this.block) {
// buffer
this.buffer.push(args);
return this;
} else {
// send
return this.exec(args);
}
};
/**
* Don't call this method directly.
*
* @param args
* @api private
*/
Block.prototype.exec = function(args) {
var command = args.shift();
var impl = this.block[command];
if (typeof impl == 'function') {
// call function
return this.block[command].apply(this.block, args);
} else if (args.length > 0) {
// set attribute
this.block[command] = args.shift();
return this;
} else {
// get attribute
return this.block[command];
}
};