|
198 | 198 | this.languages = opts.languages || [];
|
199 | 199 | this.collection = opts.collection;
|
200 | 200 | this.entry = opts.entry;
|
201 |
| - this.ws = {send:function(){}, close:function(){}}; |
202 |
| - |
| 201 | + this.ws = { |
| 202 | + send: function(){}, |
| 203 | + close: function(){} |
| 204 | + }; |
203 | 205 | this.mode = 'desktop';
|
204 | 206 | this.group = '';
|
205 | 207 | this.lang = opts.lang || '';
|
206 | 208 | this.$idle = false;
|
| 209 | + this.populate = opts.settings.populate; |
| 210 | + this.linked_entries = {}; |
207 | 211 |
|
208 | 212 | this.settings = App.$.extend({
|
209 | 213 | url: '',
|
|
230 | 234 | $this.$cache = JSON.stringify(this.entry);
|
231 | 235 |
|
232 | 236 | if (this.settings.wsurl) {
|
233 |
| - |
234 | 237 | if (this.settings.wsurl && !window.WebSocket) {
|
235 | 238 | console.log('Missing support for Websockets');
|
236 | 239 | } else {
|
237 | 240 | this.initWebsocket();
|
238 | 241 | }
|
239 |
| - }; |
| 242 | + } |
240 | 243 |
|
241 | 244 | this.refs.iframe.addEventListener('load', function() {
|
242 |
| - |
243 | 245 | $this.$iframe = $this.refs.iframe.contentWindow;
|
244 | 246 | $this.$idle = setInterval(_.throttle(function() {
|
245 | 247 |
|
246 | 248 | var hash = JSON.stringify({entry:$this.entry, lang: $this.lang});
|
247 | 249 |
|
248 |
| - if ($this.$cache != hash) { |
| 250 | + if ($this.$cache !== hash) { |
249 | 251 | $this.$cache = hash;
|
250 | 252 | $this.updateIframe();
|
251 | 253 | }
|
|
270 | 272 | }
|
271 | 273 |
|
272 | 274 | updateIframe() {
|
273 |
| - |
274 | 275 | if (!this.$iframe) return;
|
275 | 276 |
|
276 | 277 | var data = {
|
277 | 278 | 'event': 'cockpit:collections.preview',
|
278 | 279 | 'collection': this.collection.name,
|
279 |
| - 'entry': this.entry, |
| 280 | + 'entry': this.enhanceEntry(this.entry), |
280 | 281 | 'lang': this.lang || 'default'
|
281 | 282 | };
|
282 | 283 |
|
|
304 | 305 | return false;
|
305 | 306 | }
|
306 | 307 |
|
307 |
| - if (field == '_modified' || |
308 |
| - App.$data.user.group == 'admin' || |
| 308 | + if (field === '_modified' || |
| 309 | + App.$data.user.group === 'admin' || |
309 | 310 | !acl ||
|
310 | 311 | (Array.isArray(acl) && !acl.length) ||
|
311 | 312 | acl.indexOf(App.$data.user.group) > -1 ||
|
|
351 | 352 | };
|
352 | 353 |
|
353 | 354 | ws.reconnect = function(e){
|
354 |
| - console.log(1) |
| 355 | + console.log(1); |
355 | 356 | ws.removeAllListeners();
|
356 | 357 | setTimeout(function(){ $this.initWebsocket(); }, 5000);
|
357 | 358 | };
|
|
361 | 362 | };
|
362 | 363 |
|
363 | 364 | ws.onerror = function(e) {
|
364 |
| - if (e.code == 'ECONNREFUSED') ws.reconnect(e); |
| 365 | + if (e.code === "ECONNREFUSED") ws.reconnect(e); |
365 | 366 | };
|
366 | 367 |
|
367 | 368 | return ws;
|
368 | 369 | }
|
369 | 370 |
|
| 371 | + enhanceEntry(entry) { |
| 372 | + if (!this.populate) return entry; |
| 373 | + var previewEntry = {}; |
| 374 | + for (var property of Object.keys(entry)) { |
| 375 | + previewEntry[property] = this._enhanceData(entry[property]); |
| 376 | + } |
| 377 | + return previewEntry; |
| 378 | + } |
| 379 | + |
| 380 | + _enhanceData(data) { |
| 381 | + if (Array.isArray(data)) { |
| 382 | + return data.map(this._enhanceData); |
| 383 | + } else if (data != null && typeof data === "object") { |
| 384 | + if(data.link && data._id) { |
| 385 | + var collection = data.link; |
| 386 | + var _id = data._id; |
| 387 | + var hash = collection + _id; |
| 388 | + if ($this.linked_entries[hash] !== undefined) { |
| 389 | + // console.log("found " + hash); |
| 390 | + return $this.linked_entries[hash]; |
| 391 | + } else { |
| 392 | + // console.log("loading " + hash); |
| 393 | + var options = { |
| 394 | + filter: { _id: _id }, |
| 395 | + limit: 1, |
| 396 | + lang: this.lang |
| 397 | + }; |
| 398 | + App.request('/collections/find', { |
| 399 | + collection: collection, |
| 400 | + options: options |
| 401 | + }).then(function (result) { |
| 402 | + // console.log("loaded " + hash); |
| 403 | + $this.linked_entries[hash] = result.entries[0]; |
| 404 | + $this.updateIframe(); |
| 405 | + }); |
| 406 | + return data; |
| 407 | + } |
| 408 | + } else { |
| 409 | + var entry = {}; |
| 410 | + for (var property of Object.keys(data)) { |
| 411 | + entry[property] = this._enhanceData(data[property]); |
| 412 | + } |
| 413 | + return entry; |
| 414 | + } |
| 415 | + } |
| 416 | + return data; |
| 417 | + } |
370 | 418 | </script>
|
371 | 419 |
|
372 | 420 |
|
|
0 commit comments