/
tag.js
61 lines (45 loc) · 1.58 KB
/
tag.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
import riot from 'riot';
import route from 'riot-route';
riot.tag2('router', '<yield></yield>', '', '', function(opts) {
var this$1 = this;
this.route = route.create();
this.select = function (target) {
[].concat(this$1.tags.route)
.forEach(function (r) { return r.show = (r === target); });
};
this.on('mount', function () {
window.setTimeout(function () { return route.start(true); }, 0);
});
this.on('unmount', function () {
this$1.route.stop();
});
});
riot.tag2('route', '<virtual if="{show}"><yield></yield></virtual>', '', '', function(opts) {
var this$1 = this;
this.show = false;
var showRoute = function () {
var args = [], len = arguments.length;
while ( len-- ) args[ len ] = arguments[ len ];
this$1.one('updated', function () {
flatten(this$1.tags).forEach(function (tag) {
tag.trigger.apply(tag, [ 'route' ].concat( args ));
tag.update();
});
});
this$1.parent.select(this$1);
this$1.parent.update();
};
var getPathFromBase = !!window && !!window.route && !!window.route._
? window.route._.getPathFromBase
: function () { return ''; };
if(opts.path === getPathFromBase()){
setTimeout(showRoute, 0);
}
this.parent.route(opts.path, showRoute);
function flatten(tags) {
return Object.keys(tags)
.map(function (key) { return tags[key]; })
.reduce(function (acc, tag) { return acc.concat(tag); }, [])
}
});
export default route;