This repository was archived by the owner on Oct 22, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathindex.js
More file actions
174 lines (160 loc) · 4.17 KB
/
Copy pathindex.js
File metadata and controls
174 lines (160 loc) · 4.17 KB
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
var hyperHTMLApp = (function () {'use strict';
var onpushstate = require('onpushstate');
var path2regexp = require('path-to-regexp');
var app = HyperHTMLApplication.prototype;
app.get = function get(path) {
for (var
keys = [],
re = asPath2RegExp(path, keys),
info = this._paths[re] || (this._paths[re] = {
keys: keys,
cb: [],
re: re
}),
i = 1, length = arguments.length;
i < length; i++
) {
info.cb.push(arguments[i]);
}
return this;
};
app.delete = function del(path) {
for (var
re = asPath2RegExp(path, []),
info = this._paths[re],
i = 1, length = arguments.length;
i < length; i++
) {
var cb = arguments[i];
var index = info ? info.cb.lastIndexOf(cb) : -1;
if (-1 < index) info.cb.splice(index, 1);
}
return this;
};
app.use = function use(mount, cb) {
if (typeof mount === 'function') {
cb = mount;
mount = '(.*)';
}
for (var
paths = [].concat(mount),
i = 0, length = paths.length;
i < length; i++
) {
this.get(paths[i], cb);
}
return this;
};
app.param = function param(name, cb) {
for (var
names = [].concat(name),
i = 0, length = names.length;
i < length; i++
) {
this._params[names[i]] = cb;
}
return this;
};
app.navigate = function navigate(pathname, options) {
switch (true) {
case !!options:
switch (true) {
case !!options.replace:
case !!options.replaceState:
history.replaceState(history.state, document.title, pathname);
break;
}
break;
case pathname === (location.pathname + location.search):
this.handleEvent({type: 'samestate'});
break;
default:
var doc = document;
var html = doc.documentElement;
var navigator = doc.createElement('a');
navigator.href = pathname;
navigator.onclick = remove;
html.insertBefore(navigator, html.firstChild);
navigator.click();
break;
}
return this;
};
app.handleEvent = function handleEvent(e) {
var paths = this._paths;
for (var key in paths) {
if (paths.hasOwnProperty(key)) {
var info = paths[key];
var match = info.re.exec(location.pathname);
if (match) {
var invoked = [];
var keys = [];
var params = this._params;
var ctx = {
params: createParams(match, info.keys),
type: e.type
};
var i = 0;
var length = info.cb.length;
for (key in ctx.params) {
if (params.hasOwnProperty(key)) {
keys.push(key);
}
}
(function param() {
if (keys.length) {
key = keys.shift();
params[key](ctx, param, ctx.params[key]);
} else {
(function next() {
if (i < length) {
var cb = info.cb[i++];
if (invoked.lastIndexOf(cb) < 0) {
invoked.push(cb);
cb(ctx, next);
} else {
next();
}
}
}());
}
}());
return;
}
}
}
};
function asPath2RegExp(path, keys) {
if (typeof path !== 'string') {
path = path.toString();
path = path.slice(1, path.lastIndexOf('/'));
}
return path2regexp(path, keys);
}
function createParams(match, keys) {
for (var
value,
params = {},
i = 1,
length = match.length;
i < length; i++
) {
value = match[i];
if (value) params[keys[i - 1].name] = value;
}
return params;
}
function remove() {
this.parentNode.removeChild(this);
}
function HyperHTMLApplication() {
this._params = {};
this._paths = {};
global.addEventListener('popstate', this, false);
global.addEventListener('pushstate', this, false);
}
return function hyperHTMLApp() {
return new HyperHTMLApplication();
};
}());
module.exports = (global.hyperHTML || {}).app = hyperHTMLApp;