/
oskari.app.js
executable file
·255 lines (241 loc) · 8.46 KB
/
oskari.app.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
// So IE won't use a cached xhr result -> adds a _=timestamp param for each request...
jQuery.ajaxSetup({ cache: false });
(function (o) {
if (!o) {
// can't add loader if no Oskari ref
return;
}
// Cross-site request forgery protection with cookie based token
function getCookie (cname) {
var name = cname + '=';
var decodedCookie = decodeURIComponent(document.cookie);
var ca = decodedCookie.split(';');
var result = ca.map(function (cook) {
return cook.trim();
}).filter(function (cook) {
return cook.indexOf(name) === 0;
}).map(function (cook) {
return cook.substring(name.length, cook.length);
});
if (result.length > 0) {
return result[0];
}
}
var csrfToken = getCookie('XSRF-TOKEN');
if (csrfToken) {
jQuery(document).ajaxSend(function (e, xhr, options) {
xhr.setRequestHeader('X-XSRF-TOKEN', csrfToken);
});
}
// /CSRF
var App = function () {
/**
* @property appSetup
* application startup sequence
*/
this.appSetup = null;
/**
* @property appConfig
* application configuration (state) for instances
* this is injected to instances before 'start' is called
*/
this.appConfig = {};
};
/**
* FACADE will have only a couple of methods which trigger alotta operations
*/
App.prototype = {
/**
* @public @method getBundleInstanceConfigurationByName
* Returns configuration for instance by bundleinstancename
*
* @param {string} biid Bundle instance ID
*
* @return {Object} Bundle instance configuration
*/
getBundleInstanceConfigurationByName: function (biid) {
return this.appConfig[biid];
},
/**
* @public @method playBundle
* Plays a bundle player JSON object by instantiating any required
* bundle instances.
*
* @param {Object} recData Bundle player JSON
* @param {function(Object)} callback Callback function
*
*/
playBundle: function (recData, config, callback) {
if (typeof recData !== 'object') {
throw new Error('Bundle def is not an object');
}
if (typeof config === 'function') {
callback = config;
config = undefined;
}
if (config) {
// wrap to acceptable format
var configName = recData.bundleinstancename || recData.bundlename;
var tmp = {};
tmp[configName] = config;
config = tmp;
} else {
config = this.appConfig;
}
var loader = Oskari.loader([recData], config);
loader.processSequence(callback);
},
/**
* Convenience function to load appsetup from url with given params and startup the Oskari app.
* @param {String} url Url to load the appsetup json from
* @param {Object} params Optional parameters to pass for the request
* @param {Function} errorCB Optional callback for handling error
* @param {Function} successCB Optional callback that is called when the application has started
* @param {Function} modifyCB Optional callback that is called appsetup is loaded, but before it's used by Oskari
*/
loadAppSetup: function (url, params, errorCB, successCB, modifyCB) {
var me = this;
jQuery.ajax({
type: 'GET',
dataType: 'json',
data: params || {},
url: url,
success: function (setup) {
if (typeof modifyCB === 'function') {
modifyCB(setup);
}
me.init(setup);
me.startApplication(successCB);
},
error: function (jqXHR) {
if (typeof errorCB === 'function') {
errorCB(jqXHR);
}
}
});
},
/**
* @public @method init
* Initializes the internal state so startApplication() can be called to startup the initialized app.
*
* @param {Object} setup JSON application setup {
* startupSequence: [ <bundledef1>, <bundledef2>, ...],
* env: { ... },
* configuration: { ... }
* }
*/
init: function (setup) {
this.appSetup = setup;
if (setup.configuration) {
this.setConfiguration(setup.configuration);
}
setup.env = setup.env || {};
if (typeof Oskari.setLang === 'function') {
Oskari.setLang(setup.env.lang || window.language);
}
if (typeof Oskari.setSupportedLocales === 'function') {
Oskari.setSupportedLocales(setup.env.locales);
}
if (typeof Oskari.setDecimalSeparator === 'function') {
Oskari.setDecimalSeparator(setup.env.decimalSeparator);
}
if (typeof Oskari.setMarkers === 'function') {
Oskari.setMarkers(setup.env.svgMarkers || []);
}
if (typeof Oskari.user === 'function') {
Oskari.user(setup.env.user);
}
Oskari.urls.set(setup.env.urls);
},
/**
* @public @method setApplicationSetup
* @deprecated Use init() instead.
*
* @param {Object} setup JSON application setup {
* startupSequence: [ <bundledef1>, <bundledef2>, ...],
* env: { ... },
* configuration: { ... }
* }
*/
setApplicationSetup: function (setup) {
if (window.console && window.console.warn) {
console.warn('Oskari.app.setApplicationSetup() is deprecated. Use Oskari.app.init() instead.');
}
this.init(setup);
},
/**
* Returns the identifier for this appsetup (if loaded from oskari-server/db)
* @return {String}
*/
getUuid: function () {
var env = this.getApplicationSetup().env || {};
var app = env.app || {};
return app.uuid;
},
/**
* Returns appsetup type like "user", "published" etc
* @return {String}
*/
getType: function () {
var env = this.getApplicationSetup().env || {};
var app = env.app || {};
return app.type;
},
/**
* Returns true if this appsetup is publicly available.
* Returns false if it's a non-public personal view of a user.
* @return {Boolean}
*/
isPublic: function () {
var env = this.getApplicationSetup().env || {};
var app = env.app || {};
return !!app.public;
},
/**
* Returns the token for xsrf
*/
getXSRFToken: function () {
return csrfToken;
},
/**
* @public @method getApplicationSetup
* @return {Object} Application setup
*/
getApplicationSetup: function () {
return this.appSetup || {};
},
/**
* @public @method setConfiguration
* @param {Object} config Config
*/
setConfiguration: function (config) {
this.appConfig = config;
},
/**
* @public @method getConfiguration
* @return {Object}
*/
getConfiguration: function () {
return this.appConfig || {};
},
startApplication: function (callback) {
var loader = Oskari.loader(this.appSetup.startupSequence, this.appConfig);
loader.processSequence(callback);
},
/**
* @method stopApplication
* Might stop app if/when all stops implemented
*/
stopApplication: function () {
throw new Error('Not supported');
},
/**
* @method getSystemDefaultViews
* @return {Object[]} default view objects Array
*/
getSystemDefaultViews: function () {
return this.appSetup.env.defaultApps || [];
}
};
o.app = new App();
}(Oskari));