Repository navigation
Expand file tree
/
Copy pathrouter.js
More file actions
162 lines (133 loc) · 3.73 KB
/
Copy pathrouter.js
File metadata and controls
162 lines (133 loc) · 3.73 KB
Copy raw file
<
8000
button data-component="IconButton" type="button" data-testid="download-raw-button" class="prc-Button-ButtonBase-9n-Xk downloadButton__gSQP8w prc-Button-IconButton-fyge7" data-loading="false" data-no-visuals="true" data-size="small" data-variant="default" aria-labelledby="_R_eaucpala9lik5_">Download raw file
Edit and raw actions
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
(function(window) {
var COPY_METHODS = ['start', 'stop', 'show'];
function Router(page) {
var i = 0;
var len = COPY_METHODS.length;
this.page = page;
this._activeObjects = [];
for (; i < len; i++) {
this[COPY_METHODS[i]] = this.page[COPY_METHODS[i]].bind(this.page);
}
this._lastState = this._lastState.bind(this);
}
Router.prototype = {
/**
* Tells router to manage the object.
* This will call the 'onactive'
* method if present on the object.
*
* When the route is changed all 'manged'
* object will be cleared and 'oninactive'
* will be fired.
*/
mangeObject: function() {
var args = Array.prototype.slice.call(arguments);
var object = args.shift();
this._activeObjects.push(object);
// intentionally using 'in'
if ('onactive' in object) {
object.onactive.apply(object, args);
}
},
/**
* Clears active objects, calls oninactive
* on object if available.
*/
clearObjects: function(ctx) {
var item;
while ((item = this._activeObjects.pop())) {
// intentionally using 'in'
if ('oninactive' in item) {
item.oninactive();
}
}
},
/**
* This method serves two purposes.
*
* 1. to safely end the loop by _not_ calling next.
* 2. to store the last location.
*
* This function is added to the end of every rule.
*/
_lastState: function(ctx) {
this.last = ctx;
},
resetState: function() {
if (!this.currentPath) {
this.currentPath = '/month/';
}
this.show(this.currentPath);
},
/**
* Adds a route that represents a state of the page.
* The theory is that a page can only enter
* one state at a time (basically yield control to some
* view or other object).
*
* Modifiers can be used to alter the behaviour
* of a given state (without exiting it)
*
* @param {String} path path as defined by page.js.
* @param {String|Array} one or multiple view identifiers.
* @param {Object} options (clear, path).
*/
state: function(path, views, options) {
options = options || {};
if (!Array.isArray(views)) {
views = [views];
}
var self = this;
var viewObjs = [];
function setPath(ctx, next) {
// Reset our views
viewObjs = [];
if (options.path !== false) {
document.body.dataset.path = ctx.canonicalPath;
}
next();
}
function loadAllViews(ctx, next) {
var len = views.length;
var numViews = len;
var i;
for (i = 0; i < numViews; i++) {
Calendar.App.view(views[i], function(view) {
viewObjs.push(view);
len--;
if (!len) {
next();
}
});
}
}
function handleViews(ctx, next) {
// Clear views if needed
if (options.clear !== false) {
self.clearObjects();
}
// Activate objects
for (var i = 0, view; view = viewObjs[i]; i++) {
self.mangeObject(view, ctx);
}
// Set the current path
if (options.appPath !== false) {
self.currentPath = ctx.canonicalPath;
}
next();
}
this.page(path, setPath, loadAllViews, handleViews, this._lastState);
},
/**
* Adds a modifier route
* Modifiers are essentially views, without the currentPath updating
*/
modifier: function(path, view, options) {
options = options || {};
options.appPath = false;
options.clear = false;
this.state(path, view, options);
}
};
Calendar.Router = Router;
}(this));