2021-03-10 11:57:03 +00:00
|
|
|
/* global Plugins, __, require, PluginHost, App, dojo */
|
2013-07-31 14:58:56 +00:00
|
|
|
|
2018-12-07 19:56:34 +00:00
|
|
|
Plugins.Shorten_Expanded = {
|
2021-03-10 11:57:03 +00:00
|
|
|
threshold: 1.5, // of window height
|
|
|
|
shorten_if_needed: function(row) {
|
|
|
|
|
|
|
|
const content = row.querySelector(".content");
|
|
|
|
const content_inner = row.querySelector(".content-inner");
|
|
|
|
|
|
|
|
console.log('shorten_expanded', row.id, content.offsetHeight, 'vs', this.threshold * window.innerHeight);
|
|
|
|
|
|
|
|
if (content && content_inner && content.offsetHeight >= this.threshold * window.innerHeight) {
|
|
|
|
|
|
|
|
const attachments = row.querySelector(".attachments-inline"); // optional
|
|
|
|
|
|
|
|
content_inner.innerHTML = `
|
|
|
|
<div class="content-shrink-wrap">
|
|
|
|
${content_inner.innerHTML}
|
|
|
|
${attachments ? attachments.innerHTML : ''}
|
|
|
|
</div>
|
|
|
|
<button dojoType="dijit.form.Button" class="alt-info expand-prompt" onclick="return Plugins.Shorten_Expanded.expand('${row.id}')" href="#">
|
|
|
|
${App.FormFields.icon('add')}
|
|
|
|
${__("Expand article")}
|
|
|
|
</button>`;
|
|
|
|
|
|
|
|
if (attachments)
|
|
|
|
attachments.innerHTML = "";
|
|
|
|
|
|
|
|
dojo.parser.parse(content_inner);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
},
|
|
|
|
process_row: function(row) {
|
|
|
|
|
|
|
|
if (this.shorten_if_needed(row))
|
|
|
|
return;
|
|
|
|
|
|
|
|
const promises = [];
|
|
|
|
|
|
|
|
[...row.querySelectorAll("img, video")].forEach((img) => {
|
|
|
|
const promise = new Promise((resolve, reject) => {
|
2021-03-10 14:51:06 +00:00
|
|
|
|
|
|
|
// lazy load breaks our calculations
|
|
|
|
img.removeAttribute('loading');
|
|
|
|
|
2021-03-10 11:57:03 +00:00
|
|
|
img.onload = () => resolve(img);
|
|
|
|
img.onloadeddata = () => resolve(img);
|
|
|
|
img.error = () => reject(new Error("unable to load video"));
|
|
|
|
img.onerror = () => reject(new Error("unable to load image"));
|
|
|
|
});
|
|
|
|
|
|
|
|
const timeout = new Promise((resolve, reject) => {
|
|
|
|
const id = setTimeout(() => {
|
|
|
|
clearTimeout(id);
|
|
|
|
reject(new Error("timed out"));
|
2021-03-10 12:08:36 +00:00
|
|
|
}, 2000)
|
2021-03-10 11:57:03 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
promises.push(Promise.race([promise, timeout]));
|
|
|
|
});
|
|
|
|
|
|
|
|
Promise.allSettled(promises).then(() => {
|
|
|
|
this.shorten_if_needed(row);
|
|
|
|
});
|
|
|
|
},
|
2018-12-07 19:56:34 +00:00
|
|
|
expand: function(id) {
|
2021-03-10 11:57:03 +00:00
|
|
|
const row = App.byId(id);
|
2013-07-31 14:58:56 +00:00
|
|
|
|
2018-12-07 19:56:34 +00:00
|
|
|
if (row) {
|
2021-02-18 18:51:18 +00:00
|
|
|
const content = row.querySelector(".content-shrink-wrap");
|
|
|
|
const link = row.querySelector(".expand-prompt");
|
2013-07-31 14:58:56 +00:00
|
|
|
|
2018-12-07 19:56:34 +00:00
|
|
|
if (content) content.removeClassName("content-shrink-wrap");
|
|
|
|
if (link) Element.hide(link);
|
|
|
|
}
|
2013-07-31 14:58:56 +00:00
|
|
|
|
2018-12-07 19:56:34 +00:00
|
|
|
return false;
|
|
|
|
}
|
2013-07-31 14:58:56 +00:00
|
|
|
}
|
|
|
|
|
2016-08-10 07:01:05 +00:00
|
|
|
require(['dojo/_base/kernel', 'dojo/ready'], function (dojo, ready) {
|
2016-08-10 09:22:55 +00:00
|
|
|
ready(function() {
|
2016-08-10 07:01:05 +00:00
|
|
|
PluginHost.register(PluginHost.HOOK_ARTICLE_RENDERED_CDM, function(row) {
|
2021-03-10 11:57:03 +00:00
|
|
|
Plugins.Shorten_Expanded.process_row(row);
|
2016-08-10 07:01:05 +00:00
|
|
|
return true;
|
|
|
|
});
|
2013-07-31 14:58:56 +00:00
|
|
|
});
|
|
|
|
});
|