forked from foam-framework/foam
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
125 lines (114 loc) · 3.73 KB
/
index.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
/**
* @license
* Copyright 2015 Google Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
// Read the query parameters and instantiate the model.
(function() {
var search = /([^&=]+)=?([^&]*)/g;
var query = window.location.search.substring(1);
var decode = function(s) {
return decodeURIComponent(s.replace(/\+/g, ' '));
};
var params = {};
var match;
while (match = search.exec(query)) {
params[decode(match[1])] = decode(match[2]);
}
var classpath = params.classpath;
if ( classpath ) {
classpath = classpath.split(',');
for ( var i = 0 ; i < classpath.length ; i++ ) {
X.ModelDAO = X.foam.core.bootstrap.OrDAO.create({
delegate: X.foam.core.bootstrap.BrowserFileDAO.create({
rootPath: classpath[i]
}),
primary: X.ModelDAO
});
}
}
var models = [];
var model = params.model_ || params.model || 'foam.navigator.Controller';
models.push(X.arequire(model));
if (params.model_) delete params.model_;
else delete params.model;
var viewName = params.view;
if ( viewName ) {
models.push(X.arequire(viewName));
delete params.view;
}
var viewParams = {};
Object_forEach(params, function(value, key) {
var match = /^view([A-Z].*)$/.exec(key);
var viewPropName;
if ( match && (viewPropName = match[1]) ) {
viewParams[viewPropName.charAt(0).toLowerCase() +
viewPropName.slice(1)] = value;
}
});
models.push(X.arequire('foam.ui.View'));
Object_forEach(params, function(value, key) {
var match = /^[a-z]+[.]([a-z]+[.])*[A-Z][a-zA-Z]*$/.exec(value);
if ( match ) models.push(X.arequire(value));
});
var showActions = params.showActions;
if ( showActions ) {
showActions = showActions.equalsIC('y') ||
showActions.equalsIC('yes') ||
showActions.equalsIC('true') ||
showActions.equalsIC('t');
} else {
showActions = true;
}
delete params.showActions;
for ( var key in params ) {
if ( key.startsWith('p:') ) {
params[key.substring(2)] = params[key];
delete params[key];
}
}
apar.apply(null, models).aseq(
function() {
var m = X.lookup(model);
if ( ! m ) {
document.body.innerHTML = 'Unable to load model: ' + model;
return;
}
var obj = m.create(params);
var view;
if ( viewName ) {
viewParams.data = obj;
view = X.lookup(viewName).create(viewParams);
// 'CView' refers to old CView
// TODO(kgr): remove this check when CView's converted to foam.graphics.CView
} else if ( ( X.lookup('foam.ui.View').isInstance(obj) )
|| ( 'CView' in GLOBAL && CView.isInstance(obj) ) ) {
view = obj;
} else if ( obj.toView_ ) {
view = obj.toView_();
} else {
X.arequire('foam.ui.DetailView')(function(m) {
viewParams.data = obj;
viewParams.model = obj.model_;
viewParams.showActions = showActions;
view = m.create(viewParams, obj.Y);
document.body.insertAdjacentHTML('beforeend', view.toHTML());
view.initHTML();
});
return;
}
document.body.insertAdjacentHTML('beforeend', view.toHTML());
view.initHTML();
})();
})();