-
Notifications
You must be signed in to change notification settings - Fork 5
/
routePathByModels.js
284 lines (241 loc) · 7.17 KB
/
routePathByModels.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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
define(function (require) {
'use strict';
var spv = require('spv');
var cloneObj = spv.cloneObj;
var getSPI = getterSPI();
var getSPIConstr = getterSPIConstr();
var routePathByModels = function routePathByModels(start_md, pth_string, need_constr, strict, options) {
if (!pth_string) {
throw new Error('Empty path can\'t be used. Use / to get start page')
}
/*
catalog
users
tags
*/
/*
#/catalog/The+Killers/_/Try me
#?q=be/tags/beautiful
#/catalog/Varios+Artist/Eternal+Sunshine+of+the+spotless+mind/Phone+Call
#/catalog/Varios+Artist/Eternal+Sunshine+of+the+spotless+mind/Beastie+boys/Phone+Call
#/catalog/The+Killers/+similar/Beastie+boys/Phone+Call
#/recommendations/Beastie+boys/Phone+Call
#/loved/Beastie+boys/Phone+Call
#/radio/artist/The+Killers/similarartist/Bestie+Boys/Intergalactic
#?q=be/directsearch/vk/345345
#/ds/vk/25325_2344446
http://www.lastfm.ru/music/65daysofstatic/+similar
*/
var cleanPath = pth_string.replace(/^\//, '').replace(/([^\/])\+/g, '$1 ');/*.replace(/^\//,'')*/
if (!cleanPath) {
return start_md;
}
var pth = cleanPath.split('/');
var cur_md = start_md;
var result = null;
var tree_parts_group = null;
for (var i = 0; i < pth.length; i++) {
var types = spv.getTargetField(cur_md, '_sub_pager.type');
if (types && types[pth[i]]){
if (!tree_parts_group){
tree_parts_group = [];
}
tree_parts_group.push(pth[i]);
continue;
}
var path_full_string;
if (tree_parts_group){
path_full_string = [].concat(tree_parts_group, [pth[i]]).join('/');
} else {
path_full_string = pth[i];
}
tree_parts_group = null;
if (need_constr) {
var Constr = getSPIConstr(cur_md, path_full_string);
if (!Constr) {
throw new Error('you must use supported path');
} else {
cur_md = Constr.prototype;
result = Constr;
}
continue;
}
var md = getSPI(cur_md, path_full_string, options);
if (md){
cur_md = md;
result = md;
} else if (strict) {
return null;
} else {
break;
}
}
return result;
}
function slash(str) {
return str.split('/');
}
function subPageType(type_obj, parts) {
var target = type_obj[decodeURIComponent(parts[0])];
if (typeof target !== 'function') {
return target || null;
}
return target(parts[1]);
}
function selectRouteItem(self, sp_name) {
if (self._sub_pages && self._sub_pages[sp_name]) {
return self._sub_pages[sp_name];
}
var sub_pager = self._sub_pager;
if (sub_pager) {
if (sub_pager.item) {
return sub_pager.item;
} else {
var types = sub_pager.by_type;
var type = subPageType(sub_pager.type, slash(sp_name));
if (type && !types[type]) {
throw new Error('unexpected type: ' + type + ', expecting: ' + Object.keys(type));
}
if (type) {
return sub_pager.by_type[type];
}
}
}
if (self.subPager){
console.warn('`subPager` is legacy. (there is no proper way to get `constr`, only `instance`). so get rid of `subPager`');
}
}
function getSPOpts(md, sp_name, slashed, byType) {
var normal_part = byType ? slashed.slice(1) : slashed;
var by_colon = normal_part[0].split(':').map(decodeURIComponent);
var by_comma = normal_part[0].split(',').map(decodeURIComponent);
var by_slash = normal_part.map(decodeURIComponent);
return [
{
url_part: '/' + sp_name
},
{
simple_name: sp_name,
decoded_name: decodeURIComponent(sp_name),
name_spaced: by_colon[1],
by_comma: by_comma,
by_colon: by_colon,
by_slash: by_slash,
}];
};
function getterSPI(){
var init = function(parent, target, data) {
if (target.hasOwnProperty('_provoda_id')) {
return target;
}
parent.useMotivator(target, function(instance) {
instance.init(parent.getSiOpts(), data);
});
return target;
};
var prepare = function(self, item, sp_name, slashed) {
var Constr = self._all_chi[item.key];
/*
берем данные из родителя
накладываем стандартные данные
накладываем данные из урла
*/
var common_opts = getSPOpts(self, sp_name, slashed, item.byType);
var states = common_opts[0] || {};
var hbu_declr = item.getHead;
var morph_helpers = self.app.morph_helpers
var head_by_urlname = hbu_declr && hbu_declr(common_opts[1], null, morph_helpers);
if (Constr.prototype.handling_v2_init) {
return self.initSi(Constr, {
by: 'routePathByModels',
init_version: 2,
states: states,
head: head_by_urlname,
url_params: common_opts[1],
});
}
var instance_data = {}
cloneObj(instance_data, states)
instance_data.head = head_by_urlname
return self.initSi(Constr, instance_data, null, null, common_opts[0]);
};
return function getSPI(self, sp_name, options) {
var reuse = options && options.reuse;
var autocreate = !options || options.autocreate !== false
var item = selectRouteItem(self, sp_name);
if (item) {
var can_be_reusable = item.can_be_reusable;
if (reuse && can_be_reusable && self._last_subpages[item.key]) {
var instance = self._last_subpages[item.key];
if (instance.state('$$reusable_url')) {
return instance;
}
}
var getKey = item.getKey;
var key = getKey ? getKey(decodeURIComponent(sp_name), sp_name) : sp_name;
if (self.sub_pages && self.sub_pages[key]) {
return self.sub_pages[key];
}
if (!autocreate) {
return null
}
var instance = item && prepare(self, item, sp_name, slash(sp_name));
if (instance) {
watchSubPageKey(self, instance, key);
self.sub_pages[key] = instance;
if (can_be_reusable) {
self._last_subpages[item.key] = instance;
}
return instance;
}
}
if (self.subPager){
if (self.sub_pages[sp_name]) {
return self.sub_pages[sp_name];
}
if (!autocreate) {
return null
}
var sub_page = self.subPager(decodeURIComponent(sp_name), sp_name);
var instance = Array.isArray(sub_page)
? init(self, sub_page[0], sub_page[1])
: sub_page;
self.sub_pages[sp_name] = instance;
watchSubPageKey(self, instance, sp_name);
return instance;
}
};
}
function getterSPIConstr(){
return function(self, sp_name) {
var item = selectRouteItem(self, sp_name);
if (item) {
return self._all_chi[item.key];
}
if (self.subPager){
var result = self.getSPC(decodeURIComponent(sp_name), sp_name);
if (Array.isArray(result)) {
return result[0];
} else {
return result;
}
}
};
}
function watchSubPageKey(self, instance, key) {
if (!instance.hasComplexStateFn('url_part')) {
return;
}
var cur_key = key;
self.lwch(instance, 'url_part', function(value) {
if (self.sub_pages[cur_key] === instance) {
self.sub_pages[cur_key] = null;
}
self.sub_pages[value] = instance;
cur_key = value;
});
}
routePathByModels.getSPI = getSPI;
routePathByModels.getSPIConstr = getSPIConstr;
return routePathByModels;
});