-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathriot.js
116 lines (90 loc) · 2.83 KB
/
riot.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
/* Riot 1.0.0, @license MIT, (c) 2014 Muut Inc + contributors */
(function(riot) { "use strict";
riot.observable = function(el) {
var callbacks = {}, slice = [].slice;
el.on = function(events, fn) {
if (typeof fn === "function") {
events.replace(/[^\s]+/g, function(name, pos) {
(callbacks[name] = callbacks[name] || []).push(fn);
fn.typed = pos > 0;
});
}
return el;
};
el.off = function(events) {
events.replace(/[^\s]+/g, function(name) {
callbacks[name] = [];
});
if (events == "*") callbacks = {};
return el;
};
// only single event supported
el.one = function(name, fn) {
if (fn) fn.one = true;
return el.on(name, fn);
};
el.trigger = function(name) {
var args = slice.call(arguments, 1),
fns = callbacks[name] || [];
for (var i = 0, fn; (fn = fns[i]); ++i) {
if (!fn.busy) {
fn.busy = true;
fn.apply(el, fn.typed ? [name].concat(args) : args);
if (fn.one) { fns.splice(i, 1); i--; }
fn.busy = false;
}
}
return el;
};
return el;
};
var FN = {}, // Precompiled templates (JavaScript functions)
template_escape = {"\\": "\\\\", "\n": "\\n", "\r": "\\r", "'": "\\'"},
render_escape = {'&': '&', '"': '"', '<': '<', '>': '>'};
function default_escape_fn(str, key) {
return str == undefined ? '' : (str+'').replace(/[&\"<>]/g, function(char) {
return render_escape[char];
});
}
riot.render = function(tmpl, data, escape_fn) {
if (escape_fn === true) escape_fn = default_escape_fn;
tmpl = tmpl || '';
return (FN[tmpl] = FN[tmpl] || new Function("_", "e", "try { return '" +
tmpl.replace(/[\\\n\r']/g, function(char) {
return template_escape[char];
}).replace(/{\s*([\w\.]+)\s*}/g, "' + (e?e(_.$1,'$1'):_.$1||(_.$1==undefined?'':_.$1)) + '")
+ "' } catch(e) { return '' }"
)
)(data, escape_fn);
};
/* Cross browser popstate */
// for browsers only
if (typeof top != "object") return;
var currentHash,
pops = riot.observable({}),
listen = window.addEventListener,
doc = document;
function pop(hash) {
hash = hash.type ? location.hash : hash;
if (hash != currentHash) pops.trigger("pop", hash);
currentHash = hash;
}
/* Always fire pop event upon page load (normalize behaviour across browsers) */
// standard browsers
if (listen) {
listen("popstate", pop, false);
doc.addEventListener("DOMContentLoaded", pop, false);
// IE
} else {
doc.attachEvent("onreadystatechange", function() {
if (doc.readyState === "complete") pop("");
});
}
/* Change the browser URL or listen to changes on the URL */
riot.route = function(to) {
// listen
if (typeof to === "function") return pops.on("pop", to);
// fire
if (history.pushState) history.pushState(0, 0, to);
pop(to);
};})(typeof top == "object" ? window.riot = {} : exports);