Skip to content

Commit fa3ab92

Browse files
committed
New feature: Populate entry preview
1 parent 568b012 commit fa3ab92

File tree

2 files changed

+61
-12
lines changed

2 files changed

+61
-12
lines changed

Diff for: modules/Collections/assets/collection-entrypreview.tag

+60-12
Original file line numberDiff line numberDiff line change
@@ -198,12 +198,16 @@
198198
this.languages = opts.languages || [];
199199
this.collection = opts.collection;
200200
this.entry = opts.entry;
201-
this.ws = {send:function(){}, close:function(){}};
202-
201+
this.ws = {
202+
send: function(){},
203+
close: function(){}
204+
};
203205
this.mode = 'desktop';
204206
this.group = '';
205207
this.lang = opts.lang || '';
206208
this.$idle = false;
209+
this.populate = opts.settings.populate;
210+
this.linked_entries = {};
207211

208212
this.settings = App.$.extend({
209213
url: '',
@@ -230,22 +234,20 @@
230234
$this.$cache = JSON.stringify(this.entry);
231235

232236
if (this.settings.wsurl) {
233-
234237
if (this.settings.wsurl && !window.WebSocket) {
235238
console.log('Missing support for Websockets');
236239
} else {
237240
this.initWebsocket();
238241
}
239-
};
242+
}
240243

241244
this.refs.iframe.addEventListener('load', function() {
242-
243245
$this.$iframe = $this.refs.iframe.contentWindow;
244246
$this.$idle = setInterval(_.throttle(function() {
245247

246248
var hash = JSON.stringify({entry:$this.entry, lang: $this.lang});
247249

248-
if ($this.$cache != hash) {
250+
if ($this.$cache !== hash) {
249251
$this.$cache = hash;
250252
$this.updateIframe();
251253
}
@@ -270,13 +272,12 @@
270272
}
271273

272274
updateIframe() {
273-
274275
if (!this.$iframe) return;
275276

276277
var data = {
277278
'event': 'cockpit:collections.preview',
278279
'collection': this.collection.name,
279-
'entry': this.entry,
280+
'entry': this.enhanceEntry(this.entry),
280281
'lang': this.lang || 'default'
281282
};
282283

@@ -304,8 +305,8 @@
304305
return false;
305306
}
306307

307-
if (field == '_modified' ||
308-
App.$data.user.group == 'admin' ||
308+
if (field === '_modified' ||
309+
App.$data.user.group === 'admin' ||
309310
!acl ||
310311
(Array.isArray(acl) && !acl.length) ||
311312
acl.indexOf(App.$data.user.group) > -1 ||
@@ -351,7 +352,7 @@
351352
};
352353

353354
ws.reconnect = function(e){
354-
console.log(1)
355+
console.log(1);
355356
ws.removeAllListeners();
356357
setTimeout(function(){ $this.initWebsocket(); }, 5000);
357358
};
@@ -361,12 +362,59 @@
361362
};
362363

363364
ws.onerror = function(e) {
364-
if (e.code == 'ECONNREFUSED') ws.reconnect(e);
365+
if (e.code === "ECONNREFUSED") ws.reconnect(e);
365366
};
366367

367368
return ws;
368369
}
369370

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+
}
370418
</script>
371419

372420

Diff for: modules/Collections/views/collection.php

+1
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,7 @@
209209
<input class="uk-width-1-1 uk-form-large uk-text-primary" type="text" placeholder="protocol-1, protocol-2" bind="collection.contentpreview.wsprotocols" title="Websocket Protocol">
210210
</div>
211211
</div>
212+
<div class="uk-margin-top"><field-boolean bind="collection.contentpreview.populate" label="@lang('Populate')"></field-boolean></div>
212213
</div>
213214
214215
</div>

0 commit comments

Comments
 (0)