'use strict'
/* eslint-disable no-new */
/* global __, ngettext, App, Headlines, xhr, dojo, dijit, PluginHost, Notify, fox */
const Article = {
_scroll_reset_timeout: false,
getScoreClass: function (score) {
if (score > 500) {
return "score-high";
} else if (score > 0) {
return "score-half-high";
} else if (score < -100) {
return "score-low";
} else if (score < 0) {
return "score-half-low";
} else {
return "score-neutral";
}
},
getScorePic: function (score) {
if (score > 500) {
return "trending_up";
} else if (score > 0) {
return "trending_up";
} else if (score < 0) {
return "trending_down";
} else {
return "trending_neutral";
}
},
selectionSetScore: function () {
const ids = Headlines.getSelected();
if (ids.length > 0) {
const score = prompt(__("Please enter new score for selected articles:"));
if (!isNaN(parseInt(score))) {
ids.forEach((id) => {
const row = App.byId(`RROW-${id}`);
if (row) {
row.setAttribute("data-score", score);
const pic = row.querySelector(".icon-score");
pic.innerHTML = Article.getScorePic(score);
pic.setAttribute("title", score);
["score-low", "score-high", "score-half-low", "score-half-high", "score-neutral"]
.forEach(function(scl) {
if (row.hasClassName(scl))
row.removeClassName(scl);
});
row.addClassName(Article.getScoreClass(score));
}
});
}
} else {
alert(__("No articles selected."));
}
},
setScore: function (id, pic) {
const row = pic.closest("div[id*=RROW]");
if (row) {
const score_old = row.getAttribute("data-score");
const score = prompt(__("Please enter new score for this article:"), score_old);
if (!isNaN(parseInt(score))) {
row.setAttribute("data-score", score);
const pic = row.querySelector(".icon-score");
pic.innerHTML = Article.getScorePic(score);
pic.setAttribute("title", score);
["score-low", "score-high", "score-half-low", "score-half-high", "score-neutral"]
.forEach(function(scl) {
if (row.hasClassName(scl))
row.removeClassName(scl);
});
row.addClassName(Article.getScoreClass(score));
}
}
},
popupOpenUrl: function(url) {
const w = window.open("");
w.opener = null;
w.location = url;
},
cdmToggleGridSpan: function(id) {
const row = App.byId(`RROW-${id}`);
if (row) {
row.toggleClassName('grid-span-row');
this.setActive(id);
this.cdmMoveToId(id);
}
},
cdmUnsetActive: function (event) {
const row = App.byId(`RROW-${Article.getActive()}`);
if (row) {
row.removeClassName("active");
if (event)
event.stopPropagation();
return false;
}
},
close: function () {
if (dijit.byId("content-insert"))
dijit.byId("headlines-wrap-inner").removeChild(
dijit.byId("content-insert"));
Article.setActive(0);
},
displayUrl: function (id) {
const query = {op: "Article", method: "getmetadatabyid", id: id};
xhr.json("backend.php", query, (reply) => {
if (reply && reply.link) {
prompt(__("Article URL:"), reply.link);
} else {
alert(__("No URL could be displayed for this article."));
}
});
},
openInNewWindow: function (id) {
/* global __csrf_token */
App.postOpenWindow("backend.php",
{ "op": "Article", "method": "redirect", "id": id, "csrf_token": __csrf_token });
Headlines.toggleUnread(id, 0);
},
renderNote: function (id, note) {
return `
${App.FormFields.icon('note')}
${note ? App.escapeHtml(note) : ""}
`;
},
renderTags: function (id, tags) {
return `
${tags.length > 0 ? tags.map((tag) => `
${tag}`
).join(", ") : `${__("no tags")}`}`;
},
renderLabels: function(id, labels) {
return `
${labels.map((label) => `
${App.escapeHtml(label[1])}
`
).join("")}
`;
},
renderEnclosures: function (enclosures) {
return `
${enclosures.formatted}
${enclosures.can_inline ?
`
${enclosures.entries.map((enc) => {
if (!enclosures.inline_text_only) {
if (enc.content_type && enc.content_type.indexOf("image/") != -1) {
return `
`
} else if (enc.content_type && enc.content_type.indexOf("audio/") != -1 && App.audioCanPlay(enc.content_type)) {
return `
`;
} else {
return `
${App.escapeHtml(enc.content_url)}
`
}
} else {
return `
${App.escapeHtml(enc.content_url)}
`
}
}).join("")}
` : ''}
${enclosures.entries.length > 0 ?
`
${__('Attachments')}
${enclosures.entries.map((enc) => `
${enc.title ? enc.title : enc.filename}
`).join("")}
` : ''}
`
},
render: function (article) {
App.cleanupMemory("content-insert");
dijit.byId("headlines-wrap-inner").addChild(
dijit.byId("content-insert"));
const c = dijit.byId("content-insert");
try {
c.domNode.scrollTop = 0;
} catch (e) {
}
c.attr('content', article);
PluginHost.run(PluginHost.HOOK_ARTICLE_RENDERED, c.domNode);
//Headlines.correctHeadlinesOffset(Article.getActive());
try {
c.focus();
} catch (e) {
}
},
formatComments: function(hl) {
let comments = "";
if (hl.comments || hl.num_comments > 0) {
let comments_msg = __("comments");
if (hl.num_comments > 0) {
comments_msg = hl.num_comments + " " + ngettext("comment", "comments", hl.num_comments)
}
comments = `(${comments_msg})`;
}
return comments;
},
unpack: function(row) {
if (row.getAttribute("data-is-packed") == "1") {
console.log("unpacking: " + row.id);
const container = row.querySelector(".content-inner");
container.innerHTML = row.getAttribute("data-content").trim() + row.getAttribute("data-rendered-enclosures").trim();
dojo.parser.parse(container);
// blank content element might screw up onclick selection and keyboard moving
if (container.textContent.length == 0)
container.innerHTML += " ";
// in expandable mode, save content for later, so that we can pack unfocused rows back
if (App.isCombinedMode() && App.byId("main").hasClassName("expandable"))
row.setAttribute("data-content-original", row.getAttribute("data-content"));
row.setAttribute("data-is-packed", "0");
PluginHost.run(PluginHost.HOOK_ARTICLE_RENDERED_CDM, row);
}
},
pack: function(row) {
if (row.getAttribute("data-is-packed") != "1") {
console.log("packing", row.id);
row.setAttribute("data-is-packed", "1");
const content_inner = row.querySelector(".content-inner");
// missing in unexpanded mode
if (content_inner)
content_inner.innerHTML = `
${__("Loading, please wait...")}
`
}
},
view: function (id, no_expand) {
this.setActive(id);
Headlines.scrollToArticleId(id);
if (!no_expand) {
const hl = Headlines.objectById(id);
if (hl) {
const comments = this.formatComments(hl);
const article = `
${Article.renderNote(hl.id, hl.note)}
${hl.content}
${Article.renderEnclosures(hl.enclosures)}
`;
Headlines.toggleUnread(id, 0);
this.render(article);
}
}
return false;
},
autocompleteInject: function(elem, targetId) {
const target = App.byId(targetId);
if (!target)
return;
target.value = target.value.split(',')
.slice(0, -1)
.map((w) => w.trim())
.concat([elem.innerText])
.join(', ') + ', ';
target.focus();
},
editTags: function (id) {
const dialog = new fox.SingleUseDialog({
title: __("Article tags"),
content: `
${App.FormFields.hidden_tag("id", id.toString())}
${App.FormFields.hidden_tag("op", "article")}
${App.FormFields.hidden_tag("method", "setArticleTags")}
${__("Tags for this article (separated by commas):")}
`,
execute: function () {
if (this.validate()) {
Notify.progress("Saving article tags...", true);
xhr.json("backend.php", this.attr('value'), (data) => {
try {
Notify.close();
dialog.hide();
Headlines.onTagsUpdated(data);
} catch (e) {
App.Error.report(e);
}
});
}
},
});
const tmph = dojo.connect(dialog, 'onShow', function () {
dojo.disconnect(tmph);
xhr.json("backend.php", {op: "Article", method: "printArticleTags", id: id}, (reply) => {
dijit.getEnclosingWidget(App.byId("tags_str"))
.attr('value', reply.tags.join(", "))
.attr('disabled', false);
App.byId('tags_str').onkeyup = (e) => {
const last_tag = e.target.value.split(',').pop().trim();
xhr.json("backend.php", {op: 'Article', method: 'completeTags', search: last_tag}, (data) => {
App.byId("tags_choices").innerHTML = `${data.map((tag) =>
`${tag}` )
.join(', ')}`
});
};
});
});
dialog.show();
},
cdmMoveToId: function (id, params = {}) {
const force_to_top = params.force_to_top || false;
const ctr = App.byId("headlines-frame");
const row = App.byId(`RROW-${id}`);
if (ctr && row) {
const grid_gap = parseInt(window.getComputedStyle(ctr).gridGap) || 0;
if (force_to_top || !App.Scrollable.fitsInContainer(row, ctr)) {
ctr.scrollTop = row.offsetTop - grid_gap;
}
}
},
setActive: function (id) {
if (id != Article.getActive()) {
console.log("setActive", id, "was", Article.getActive());
App.findAll("div[id*=RROW][class*=active]").forEach((row) => {
row.removeClassName("active");
if (App.isCombinedMode() && !App.getInitParam("cdm_expanded"))
Article.pack(row);
});
const row = App.byId(`RROW-${id}`);
if (row) {
Article.unpack(row);
row.removeClassName("Unread");
row.addClassName("active");
PluginHost.run(PluginHost.HOOK_ARTICLE_SET_ACTIVE, row.getAttribute("data-article-id"));
}
}
},
getActive: function () {
const row = document.querySelector("#headlines-frame > div[id*=RROW][class*=active]");
if (row)
return row.getAttribute("data-article-id");
else
return 0;
},
scrollByPages: function (page_offset) {
App.Scrollable.scrollByPages(App.byId("content-insert"), page_offset);
},
scroll: function (offset) {
App.Scrollable.scroll(App.byId("content-insert"), offset);
},
mouseIn: function (id) {
this.post_under_pointer = id;
},
mouseOut: function (/* id */) {
this.post_under_pointer = false;
},
getUnderPointer: function () {
return this.post_under_pointer;
}
}