forked from ryanflorence/limescripts
/
limescripts.js
216 lines (187 loc) · 5.95 KB
/
limescripts.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
/*!
* limescripts.js (c) Ryan Florence
* https://github.com/rpflorence/limescripts
* MIT License <http://www.opensource.org/licenses/mit-license.php>
*/
(function () {
/*
* Main API into limescript, pretty much all you need. The whole system is
* built around binding to global events that trigger when a message is
* added. There are static events, and dynamic events (like fired for
* elements with certain classNames, etc). To get a feel for the types of
* events available, uncomment the first line of `function trigger` later
* in this file while you use lime chat.
*
* @param {String} event - the name of the event you want to bind to
* @param {Function} handler - the function to call
*
* Static events
* -------------
*
* @event 'line'
* @signature - function(line)
* @context - line
*
* @event 'link' - when a message contains a link
* @signature - function(href, line)
* @context - the link anchor element
*
* @event 'line:highlight' - when a message is highlighted
* @signature - function(line)
* @context - line
*
* Dynamic Events
* --------------
*
* @event "#{type}" - when a line of `type` is added
* @signature - function(line)
* @context - line
*
* @event "message:#{type}" - when a message of `type` is added
* @signature - function(line)
* @context - message element
*
* @event "#{className}" - when an element with `className` is added
* @signature - function(line)
* @context - the element matching `className`
*
*/
this.bind = function (event, handler) {
if (events[event] == null) events[event] = [];
events[event].push(handler);
};
/*
* Load a JavaScript or CoffeeScript file. If you specify `load.language`
* to either `js` or `coffee` then you can omit the extension.
*
* @param {String} src - file name relative to `lib` directory
* @param {Function} callback - callback on load of script
*/
this.load = function (src, callback) {
if (load.language) src += '.' + load.language;
var loader = src.match(/\.js$/) ? loadJS : loadCS;
loader('limescripts/lib/' + src, callback);
};
/*
* Load a JavaScript file
*
* @param {String} src
* @param {Function} callback
*/
this.loadJS = function (src, callback) {
var script = document.createElement('script');
if (src.indexOf('?') === -1) src += '?';
// web view caches like crazy
script.src = src + '&bust=' + Date.now();
script.type = 'text/javascript';
if (callback) script.onload = callback;
document.body.appendChild(script);
};
/*
* Load a CoffeeScript file
*
* @param {String} src
* @param {Function} calback
*/
this.loadCS = function (src, callback) {
CoffeeScript.load(src, callback);
};
/*
* Really terrible debugging, thinking about getting firebug lite
* but haven't looked at it yet
*/
this.console = {};
/*
* Logs arguments as strings to the screen
*/
this.console.log = function () {
var args = [].slice.call(arguments, 0);
args.unshift('js-console-log');
log.apply(null, args);
};
/*
* same as console.log but adds `js-console-error` class for styling
*/
this.console.error = function () {
var args = [].slice.call(arguments, 0);
args.unshift('js-console-error');
log.apply(null, args);
};
// Override window.onerror so we actually get some error reporting
this.onerror = function (error) {
console.error('ERROR:', error);
};
// its all pretty much non-api private stuff from here
function log (className) {
var msgs = [].slice.call(arguments, 1);
var node = document.createElement('div');
node.className = className;
node.innerHTML = '<span class="message">' + msgs.join(' ') + '</span>';
document.body.appendChild(node);
}
// triggers all the events
function triggerEvents (line) {
var type = line.getAttribute('type');
// line event
trigger('line', line, [line]);
// line type event
trigger(type, line, [line]);
// highlight event
if (line.getAttribute('highlight') === 'true') {
trigger('line:highlight', line, [line]);
}
// fire events for all elements in the line
var els = line.querySelectorAll('*');
for (var i = 0, l = els.length; i < l; i++) {
triggerElementEvents(els[i], line);
}
}
function triggerElementEvents (el, line) {
// link event
if (el.tagName === 'A') {
trigger('link', el, [el.getAttribute('href'), line]);
}
// message:#{type} event
if (el.className === 'message') {
trigger('message:' + el.className, el, [line]);
}
// className event
trigger(el.className, el, [line]);
}
// event storage object used in `this.bind`
var events = {};
// triggers an event
function trigger (name, context, args) {
// console.log(name) // <-- do this to discover events
var handlers = events[name];
if (!handlers) return;
for (var i = 0, l = handlers.length; i < l; i++) {
handlers[i].apply(context, args);
}
}
// the handler called whenever a dom node is inserted, ensures that we don't
// trigger events unless it's the top level element of a new message
function processMessage (event) {
var node = event.target;
var parent = node.parentNode;
var isLine = node.className.match(/line/);
if (parent !== document.body || !isLine) return;
triggerEvents(node);
}
// attach the main handler
document.addEventListener('DOMNodeInserted', processMessage, false);
// load in coffeesscript (for the hipsters) and jQuery (for the wussies)
// and the helpers file
(function() {
var scriptsLoaded = 0;
loadJS('limescripts/vendor/coffee-script.js', finish);
loadJS('limescripts/vendor/jquery-1.7.1.min.js', finish);
function finish() {
scriptsLoaded++;
if (scriptsLoaded !== 2) return;
load('helpers.coffee', function() {
load('main.coffee');
});
}
})();
}).call(this);