move some more shared stuff to CommonDialogs, Filters, and Utils

This commit is contained in:
Andrew Dolgov 2018-12-01 22:39:29 +03:00
parent 4b492cc93e
commit 1e2d4410d3
12 changed files with 502 additions and 495 deletions

View File

@ -174,7 +174,7 @@ class Dlg extends Handler_Protected {
print "<div align='center'>";
print "<button dojoType=\"dijit.form.Button\" onclick=\"return genUrlChangeKey('$feed_id', '$is_cat')\">".
print "<button dojoType=\"dijit.form.Button\" onclick=\"return CommonDialogs.genUrlChangeKey('$feed_id', '$is_cat')\">".
__('Generate new URL')."</button> ";
print "<button dojoType=\"dijit.form.Button\" onclick=\"return closeInfoBox()\">".

View File

@ -765,7 +765,7 @@ class Pref_Feeds extends Handler_Protected {
print "<div class='dlgButtons'>
<div style=\"float : left\">
<button class=\"btn-danger\" dojoType=\"dijit.form.Button\" onclick='return unsubscribeFeed($feed_id, \"$title\")'>".
<button class=\"btn-danger\" dojoType=\"dijit.form.Button\" onclick='return CommonDialogs.unsubscribeFeed($feed_id, \"$title\")'>".
__('Unsubscribe')."</button>";
print "</div>";
@ -1247,7 +1247,7 @@ class Pref_Feeds extends Handler_Protected {
var bare_id = id.substr(id.indexOf(':')+1);
if (id.match('FEED:')) {
editFeed(bare_id);
CommonDialogs.editFeed(bare_id);
} else if (id.match('CAT:')) {
editCat(bare_id, item);
}
@ -1441,7 +1441,7 @@ class Pref_Feeds extends Handler_Protected {
print "<a class=\"visibleLink\" href=\"#\" ".
"title=\"".__("Click to edit feed")."\" ".
"onclick=\"editFeed(".$line["id"].")\">".
"onclick=\"CommonDialogs.editFeed(".$line["id"].")\">".
htmlspecialchars($line["title"])."</a>";
print "</td><td class=\"insensitive\" align='right'>";
@ -1506,7 +1506,7 @@ class Pref_Feeds extends Handler_Protected {
print "<a class=\"visibleLink\" href=\"#\" ".
"title=\"".__("Click to edit feed")."\" ".
"onclick=\"editFeed(".$line["id"].")\">".
"onclick=\"CommonDialogs.editFeed(".$line["id"].")\">".
htmlspecialchars($line["title"])."</a>: ";
print "<span class=\"insensitive\">";

View File

@ -797,7 +797,7 @@ class Pref_Filters extends Handler_Protected {
dojoType=\"dijit.MenuItem\">".__('None')."</div>";
print "</div></div>";
print "<button dojoType=\"dijit.form.Button\" onclick=\"return quickAddFilter()\">".
print "<button dojoType=\"dijit.form.Button\" onclick=\"return Filters.quickAddFilter()\">".
__('Create filter')."</button> ";
print "<button dojoType=\"dijit.form.Button\" onclick=\"return joinSelectedFilters()\">".
@ -1044,7 +1044,7 @@ class Pref_Filters extends Handler_Protected {
print "<div class=\"dlgSecCont\">";
print "<select name=\"action_id\" dojoType=\"dijit.form.Select\"
onchange=\"filterDlgCheckAction(this)\">";
onchange=\"Filters.filterDlgCheckAction(this)\">";
$res = $this->pdo->query("SELECT id,description FROM ttrss_filter_actions
ORDER BY name");

View File

@ -48,7 +48,7 @@ define(["dojo/_base/declare", "dojo/dom-construct", "dijit/Tree", "dijit/Menu"],
menu.addChild(new dijit.MenuItem({
label: __("Edit feed"),
onClick: function() {
editFeed(this.getParent().row_id, false);
CommonDialogs.editFeed(this.getParent().row_id, false);
}}));
/* menu.addChild(new dijit.MenuItem({

View File

@ -55,13 +55,13 @@ define(["dojo/_base/declare", "dojo/dom-construct", "lib/CheckBoxTree"], functio
menu.addChild(new dijit.MenuItem({
label: __("Edit feed"),
onClick: function() {
editFeed(this.getParent().row_id);
CommonDialogs.editFeed(this.getParent().row_id);
}}));
menu.addChild(new dijit.MenuItem({
label: __("Unsubscribe"),
onClick: function() {
unsubscribeFeed(this.getParent().row_id, this.getParent().item.name);
CommonDialogs.unsubscribeFeed(this.getParent().row_id, this.getParent().item.name);
}}));
menu.bindDomNode(tnode.domNode);

View File

@ -291,7 +291,7 @@ const Utils = {
}
if (k == "daemon_is_running" && v != 1) {
notify_error("<span onclick=\"explainError(1)\">Update daemon is not running.</span>", true);
notify_error("<span onclick=\"Utils.explainError(1)\">Update daemon is not running.</span>", true);
return;
}
@ -306,7 +306,7 @@ const Utils = {
}
if (k == "daemon_stamp_ok" && v != 1) {
notify_error("<span onclick=\"explainError(3)\">Update daemon is not updating feeds.</span>", true);
notify_error("<span onclick=\"Utils.explainError(3)\">Update daemon is not updating feeds.</span>", true);
return;
}
@ -376,7 +376,10 @@ const Utils = {
}
App.initSecondStage();
}
},
explainError: function(code) {
return this.displayDlg(__("Error explained"), "explainError", code);
},
};
const CommonDialogs = {
@ -674,6 +677,97 @@ const CommonDialogs = {
}
});
}
},
unsubscribeFeed: function(feed_id, title) {
const msg = __("Unsubscribe from %s?").replace("%s", title);
if (title == undefined || confirm(msg)) {
notify_progress("Removing feed...");
const query = {op: "pref-feeds", quiet: 1, method: "remove", ids: feed_id};
xhrPost("backend.php", query, (transport) => {
if (dijit.byId("feedEditDlg")) dijit.byId("feedEditDlg").hide();
if (App.isPrefs()) {
Feeds.reload();
} else {
if (feed_id == Feeds.getActiveFeedId())
setTimeout(() => {
Feeds.viewfeed({feed: -5})
},
100);
if (feed_id < 0) Feeds.reload();
}
});
}
return false;
},
editFeed: function (feed) {
if (feed <= 0)
return alert(__("You can't edit this kind of feed."));
const query = {op: "pref-feeds", method: "editfeed", id: feed};
console.log("editFeed", query);
if (dijit.byId("filterEditDlg"))
dijit.byId("filterEditDlg").destroyRecursive();
if (dijit.byId("feedEditDlg"))
dijit.byId("feedEditDlg").destroyRecursive();
const dialog = new dijit.Dialog({
id: "feedEditDlg",
title: __("Edit Feed"),
style: "width: 600px",
execute: function () {
if (this.validate()) {
notify_progress("Saving data...", true);
xhrPost("backend.php", dialog.attr('value'), () => {
dialog.hide();
notify('');
Feeds.reload();
});
}
},
href: "backend.php?" + dojo.objectToQuery(query)
});
dialog.show();
},
genUrlChangeKey: function(feed, is_cat) {
if (confirm(__("Generate new syndication address for this feed?"))) {
notify_progress("Trying to change address...", true);
const query = {op: "pref-feeds", method: "regenFeedKey", id: feed, is_cat: is_cat};
xhrJson("backend.php", query, (reply) => {
const new_link = reply.link;
const e = $('gen_feed_url');
if (new_link) {
e.innerHTML = e.innerHTML.replace(/\&amp;key=.*$/,
"&amp;key=" + new_link);
e.href = e.href.replace(/\&key=.*$/,
"&key=" + new_link);
new Effect.Highlight(e);
notify('');
} else {
notify_error("Could not change feed URL.");
}
});
}
return false;
}
};
@ -994,8 +1088,8 @@ function fatalError(code, msg, ext_info) {
}
// noinspection JSUnusedGlobalSymbols
function filterDlgCheckAction(sender) {
const Filters = {
filterDlgCheckAction: function(sender) {
const action = sender.value;
const action_param = $("filterDlg_paramBox");
@ -1024,73 +1118,8 @@ function filterDlgCheckAction(sender) {
} else {
Element.hide(action_param);
}
}
function explainError(code) {
return Utils.displayDlg(__("Error explained"), "explainError", code);
}
function strip_tags(s) {
return s.replace(/<\/?[^>]+(>|$)/g, "");
}
// noinspection JSUnusedGlobalSymbols
function uploadIconHandler(rc) {
switch (rc) {
case 0:
notify_info("Upload complete.");
if (App.isPrefs()) {
Feeds.reload();
} else {
setTimeout('Feeds.reload(false, false)', 50);
}
break;
case 1:
notify_error("Upload failed: icon is too big.");
break;
case 2:
notify_error("Upload failed.");
break;
}
}
// noinspection JSUnusedGlobalSymbols
function removeFeedIcon(id) {
if (confirm(__("Remove stored feed icon?"))) {
notify_progress("Removing feed icon...", true);
const query = { op: "pref-feeds", method: "removeicon", feed_id: id };
xhrPost("backend.php", query, (transport) => {
notify_info("Feed icon removed.");
if (App.isPrefs()) {
Feeds.reload();
} else {
setTimeout('Feeds.reload(false, false)', 50);
}
});
}
return false;
}
// noinspection JSUnusedGlobalSymbols
function uploadFeedIcon() {
const file = $("icon_file");
if (file.value.length == 0) {
alert(__("Please select an image file to upload."));
} else if (confirm(__("Upload new icon for this feed?"))) {
notify_progress("Uploading, please wait...", true);
return true;
}
return false;
}
function createNewRuleElement(parentNode, replaceNode) {
},
createNewRuleElement: function(parentNode, replaceNode) {
const form = document.forms["filter_new_rule_form"];
const query = {op: "pref-filters", method: "printrulename", rule: dojo.formToJson(form)};
@ -1102,18 +1131,22 @@ function createNewRuleElement(parentNode, replaceNode) {
new dijit.form.CheckBox({
onChange: function () {
toggleSelectListRow2(this) },
toggleSelectListRow2(this)
},
}, cb);
dojo.create("input", { type: "hidden",
dojo.create("input", {
type: "hidden",
name: "rule[]",
value: dojo.formToJson(form) }, li);
value: dojo.formToJson(form)
}, li);
dojo.create("span", {
onclick: function () {
dijit.byId('filterEditDlg').editRule(this);
},
innerHTML: transport.responseText }, li);
innerHTML: transport.responseText
}, li);
if (replaceNode) {
parentNode.replaceChild(li, replaceNode);
@ -1124,9 +1157,8 @@ function createNewRuleElement(parentNode, replaceNode) {
exception_error(e);
}
});
}
function createNewActionElement(parentNode, replaceNode) {
},
createNewActionElement: function(parentNode, replaceNode) {
const form = document.forms["filter_new_action_form"];
if (form.action_id.value == 7) {
@ -1135,8 +1167,10 @@ function createNewActionElement(parentNode, replaceNode) {
form.action_param.value = form.action_param_plugin.value;
}
const query = { op: "pref-filters", method: "printactionname",
action: dojo.formToJson(form) };
const query = {
op: "pref-filters", method: "printactionname",
action: dojo.formToJson(form)
};
xhrPost("backend.php", query, (transport) => {
try {
@ -1146,18 +1180,22 @@ function createNewActionElement(parentNode, replaceNode) {
new dijit.form.CheckBox({
onChange: function () {
toggleSelectListRow2(this) },
toggleSelectListRow2(this)
},
}, cb);
dojo.create("input", { type: "hidden",
dojo.create("input", {
type: "hidden",
name: "action[]",
value: dojo.formToJson(form) }, li);
value: dojo.formToJson(form)
}, li);
dojo.create("span", {
onclick: function () {
dijit.byId('filterEditDlg').editAction(this);
},
innerHTML: transport.responseText }, li);
innerHTML: transport.responseText
}, li);
if (replaceNode) {
parentNode.replaceChild(li, replaceNode);
@ -1169,10 +1207,8 @@ function createNewActionElement(parentNode, replaceNode) {
exception_error(e);
}
});
}
function addFilterRule(replaceNode, ruleStr) {
},
addFilterRule: function(replaceNode, ruleStr) {
if (dijit.byId("filterNewRuleDlg"))
dijit.byId("filterNewRuleDlg").destroyRecursive();
@ -1185,16 +1221,16 @@ function addFilterRule(replaceNode, ruleStr) {
style: "width: 600px",
execute: function () {
if (this.validate()) {
createNewRuleElement($("filterDlg_Matches"), replaceNode);
Filters.createNewRuleElement($("filterDlg_Matches"), replaceNode);
this.hide();
}
},
href: query});
href: query
});
rule_dlg.show();
}
function addFilterAction(replaceNode, actionStr) {
},
addFilterAction: function(replaceNode, actionStr) {
if (dijit.byId("filterNewActionDlg"))
dijit.byId("filterNewActionDlg").destroyRecursive();
@ -1207,16 +1243,16 @@ function addFilterAction(replaceNode, actionStr) {
style: "width: 600px",
execute: function () {
if (this.validate()) {
createNewActionElement($("filterDlg_Actions"), replaceNode);
Filters.createNewActionElement($("filterDlg_Actions"), replaceNode);
this.hide();
}
},
href: query});
href: query
});
rule_dlg.show();
}
function editFilterTest(query) {
},
editFilterTest: function(query) {
if (dijit.byId("filterTestDlg"))
dijit.byId("filterTestDlg").destroyRecursive();
@ -1294,22 +1330,23 @@ function editFilterTest(query) {
});
},
href: query});
href: query
});
dojo.connect(test_dlg, "onLoad", null, function (e) {
test_dlg.getTestResults(query, 0);
});
test_dlg.show();
}
function quickAddFilter() {
},
quickAddFilter: function() {
let query;
if (!App.isPrefs()) {
query = { op: "pref-filters", method: "newfilter",
feed: Feeds.getActiveFeedId(), is_cat: Feeds.activeFeedIsCat() };
query = {
op: "pref-filters", method: "newfilter",
feed: Feeds.getActiveFeedId(), is_cat: Feeds.activeFeedIsCat()
};
} else {
query = {op: "pref-filters", method: "newfilter"};
}
@ -1329,7 +1366,7 @@ function quickAddFilter() {
test: function () {
const query = "backend.php?" + dojo.formToQuery("filter_new_form") + "&savemode=test";
editFilterTest(query);
Filters.editFilterTest(query);
},
selectRules: function (select) {
$$("#filterDlg_Matches input[type=checkbox]").each(function (e) {
@ -1354,27 +1391,35 @@ function quickAddFilter() {
editRule: function (e) {
const li = e.parentNode;
const rule = li.getElementsByTagName("INPUT")[1].value;
addFilterRule(li, rule);
Filters.addFilterRule(li, rule);
},
editAction: function (e) {
const li = e.parentNode;
const action = li.getElementsByTagName("INPUT")[1].value;
addFilterAction(li, action);
Filters.addFilterAction(li, action);
},
addAction: function () {
Filters.addFilterAction();
},
addRule: function () {
Filters.addFilterRule();
},
addAction: function() { addFilterAction(); },
addRule: function() { addFilterRule(); },
deleteAction: function () {
$$("#filterDlg_Actions li[class*=Selected]").each(function(e) { e.parentNode.removeChild(e) });
$$("#filterDlg_Actions li[class*=Selected]").each(function (e) {
e.parentNode.removeChild(e)
});
},
deleteRule: function () {
$$("#filterDlg_Matches li[class*=Selected]").each(function(e) { e.parentNode.removeChild(e) });
$$("#filterDlg_Matches li[class*=Selected]").each(function (e) {
e.parentNode.removeChild(e)
});
},
execute: function () {
if (this.validate()) {
const query = dojo.formToQuery("filter_new_form");
xhrPost("backend.php", query, (transport) => {
xhrPost("backend.php", query, () => {
if (App.isPrefs()) {
updateFilterList();
}
@ -1383,7 +1428,8 @@ function quickAddFilter() {
});
}
},
href: "backend.php?" + dojo.objectToQuery(query)});
href: "backend.php?" + dojo.objectToQuery(query)
});
if (!App.isPrefs()) {
const selectedText = getSelectionText();
@ -1398,7 +1444,7 @@ function quickAddFilter() {
const rule = {reg_exp: selectedText, feed_id: [feed_id], filter_type: 1};
addFilterRule(null, dojo.toJson(rule));
Filters.addFilterRule(null, dojo.toJson(rule));
} else {
@ -1420,7 +1466,7 @@ function quickAddFilter() {
const rule = {reg_exp: title, feed_id: [feed_id], filter_type: 1};
addFilterRule(null, dojo.toJson(rule));
Filters.addFilterRule(null, dojo.toJson(rule));
}
});
}
@ -1428,29 +1474,47 @@ function quickAddFilter() {
}
dialog.show();
},
};
}
function unsubscribeFeed(feed_id, title) {
const msg = __("Unsubscribe from %s?").replace("%s", title);
if (title == undefined || confirm(msg)) {
notify_progress("Removing feed...");
const query = { op: "pref-feeds", quiet: 1, method: "remove", ids: feed_id };
xhrPost("backend.php", query, (transport) => {
if (dijit.byId("feedEditDlg")) dijit.byId("feedEditDlg").hide();
/* function strip_tags(s) {
return s.replace(/<\/?[^>]+(>|$)/g, "");
} */
// noinspection JSUnusedGlobalSymbols
function uploadIconHandler(rc) {
switch (rc) {
case 0:
notify_info("Upload complete.");
if (App.isPrefs()) {
Feeds.reload();
} else {
if (feed_id == Feeds.getActiveFeedId())
setTimeout(() => { Feeds.viewfeed({feed:-5}) },
100);
setTimeout('Feeds.reload(false, false)', 50);
}
break;
case 1:
notify_error("Upload failed: icon is too big.");
break;
case 2:
notify_error("Upload failed.");
break;
}
}
if (feed_id < 0) Feeds.reload();
// noinspection JSUnusedGlobalSymbols
function removeFeedIcon(id) {
if (confirm(__("Remove stored feed icon?"))) {
notify_progress("Removing feed icon...", true);
const query = { op: "pref-feeds", method: "removeicon", feed_id: id };
xhrPost("backend.php", query, (transport) => {
notify_info("Feed icon removed.");
if (App.isPrefs()) {
Feeds.reload();
} else {
setTimeout('Feeds.reload(false, false)', 50);
}
});
}
@ -1459,33 +1523,16 @@ function unsubscribeFeed(feed_id, title) {
}
// noinspection JSUnusedGlobalSymbols
function genUrlChangeKey(feed, is_cat) {
if (confirm(__("Generate new syndication address for this feed?"))) {
function uploadFeedIcon() {
const file = $("icon_file");
notify_progress("Trying to change address...", true);
const query = { op: "pref-feeds", method: "regenFeedKey", id: feed, is_cat: is_cat };
xhrJson("backend.php", query, (reply) => {
const new_link = reply.link;
const e = $('gen_feed_url');
if (new_link) {
e.innerHTML = e.innerHTML.replace(/\&amp;key=.*$/,
"&amp;key=" + new_link);
e.href = e.href.replace(/\&key=.*$/,
"&key=" + new_link);
new Effect.Highlight(e);
notify('');
} else {
notify_error("Could not change feed URL.");
}
});
if (file.value.length == 0) {
alert(__("Please select an image file to upload."));
} else if (confirm(__("Upload new icon for this feed?"))) {
notify_progress("Uploading, please wait...", true);
return true;
}
return false;
}
@ -1541,7 +1588,6 @@ function selectTableRows(id, mode) {
}
}
}
}
function getSelectedTableRowIds(id) {
@ -1559,45 +1605,6 @@ function getSelectedTableRowIds(id) {
return rows;
}
function editFeed(feed) {
if (feed <= 0)
return alert(__("You can't edit this kind of feed."));
const query = { op: "pref-feeds", method: "editfeed", id: feed };
console.log("editFeed", query);
if (dijit.byId("filterEditDlg"))
dijit.byId("filterEditDlg").destroyRecursive();
if (dijit.byId("feedEditDlg"))
dijit.byId("feedEditDlg").destroyRecursive();
const dialog = new dijit.Dialog({
id: "feedEditDlg",
title: __("Edit Feed"),
style: "width: 600px",
execute: function() {
if (this.validate()) {
notify_progress("Saving data...", true);
xhrPost("backend.php", dialog.attr('value'), () => {
dialog.hide();
notify('');
Feeds.reload();
});
}
},
href: "backend.php?" + dojo.objectToQuery(query)});
dialog.show();
}
function get_timestamp() {
const date = new Date();
return Math.round(date.getTime() / 1000);
}
// noinspection JSUnusedGlobalSymbols
function label_to_feed_id(label) {
return _label_base_index - 1 - Math.abs(label);

View File

@ -82,7 +82,7 @@ const App = {
const param = getURLParam('methodparam');
window.setTimeout(function () {
editFeed(param)
CommonDialogs.editFeed(param)
}, 100);
}
},
@ -100,7 +100,7 @@ const App = {
CommonDialogs.addLabel();
return false;
case "create_filter":
quickAddFilter();
Filters.quickAddFilter();
return false;
case "help_dialog":
Utils.helpDialog("main");
@ -575,7 +575,7 @@ function editSelectedFeed() {
notify("");
editFeed(rows[0], {});
CommonDialogs.editFeed(rows[0], {});
}

View File

@ -360,7 +360,7 @@ const App = {
if (Feeds.activeFeedIsCat())
alert(__("You can't edit this kind of feed."));
else
editFeed(Feeds.getActiveFeedId());
CommonDialogs.editFeed(Feeds.getActiveFeedId());
};
this.hotkey_actions["feed_catchup"] = function () {
if (Feeds.getActiveFeedId() != undefined) {
@ -424,7 +424,7 @@ const App = {
CommonDialogs.addLabel();
};
this.hotkey_actions["create_filter"] = function () {
quickAddFilter();
Filters.quickAddFilter();
};
this.hotkey_actions["collapse_sidebar"] = function () {
Feeds.viewCurrentFeed();
@ -501,7 +501,7 @@ const App = {
if (Feeds.activeFeedIsCat())
alert(__("You can't edit this kind of feed."));
else
editFeed(Feeds.getActiveFeedId());
CommonDialogs.editFeed(Feeds.getActiveFeedId());
break;
case "qmcRemoveFeed":
var actid = Feeds.getActiveFeedId();
@ -521,7 +521,7 @@ const App = {
var pr = __("Unsubscribe from %s?").replace("%s", fn);
if (confirm(pr)) {
unsubscribeFeed(actid);
CommonDialogs.unsubscribeFeed(actid);
}
break;
case "qmcCatchupAll":

View File

@ -1560,7 +1560,7 @@ const Headlines = {
menu.addChild(new dijit.MenuItem({
label: __("Edit feed"),
onClick: function () {
editFeed(this.getParent().currentTarget.getAttribute("data-feed-id"));
CommonDialogs.editFeed(this.getParent().currentTarget.getAttribute("data-feed-id"));
}
}));

View File

@ -202,7 +202,7 @@ class Af_Psql_Trgm extends Plugin {
print "<li>" .
"<img src='images/pub_set.png'
style='vertical-align : middle'> <a href='#'
onclick='editFeed($f)'>" .
onclick='CommonDialogs.editFeed($f)'>" .
Feeds::getFeedTitle($f) . "</a></li>";
}
print "</ul>";

View File

@ -94,7 +94,7 @@ class Af_Readability extends Plugin {
print "<li>" .
"<img src='images/pub_set.png'
style='vertical-align : middle'> <a href='#'
onclick='editFeed($f)'>".
onclick='CommonDialogs.editFeed($f)'>".
Feeds::getFeedTitle($f) . "</a></li>";
}
print "</ul>";

View File

@ -25,7 +25,7 @@ class BasicTest extends PHPUnit_Extensions_Selenium2TestCase {
public function testBasicDialogs() {
$this->testLogin();
$this->execute(["script" => "quickAddFilter()", "args" => []]);
$this->execute(["script" => "Filters.quickAddFilter()", "args" => []]);
$this->byCssSelector("#filterEditDlg")->displayed();
$this->execute(["script" => "dijit.byId('filterEditDlg').hide();", "args" => []]);