prevent filter selected text dialog from opening in wrong order

master
Andrew Dolgov 4 years ago
parent 590b1fc39e
commit da97b29dbe

@ -358,6 +358,27 @@ const App = {
return p;
}
},
// http://stackoverflow.com/questions/6251937/how-to-get-selecteduser-highlighted-text-in-contenteditable-element-and-replac
getSelectedText: function() {
let text = "";
if (typeof window.getSelection != "undefined") {
const sel = window.getSelection();
if (sel.rangeCount) {
const container = document.createElement("div");
for (let i = 0, len = sel.rangeCount; i < len; ++i) {
container.appendChild(sel.getRangeAt(i).cloneContents());
}
text = container.innerHTML;
}
} else if (typeof document.selection != "undefined") {
if (document.selection.type == "Text") {
text = document.selection.createRange().textText;
}
}
return text.stripTags();
},
displayIfChecked: function(checkbox, elemId) {
if (checkbox.checked) {
Element.show(elemId);

@ -19,342 +19,332 @@ const Filters = {
query = {op: "pref-filters", method: "edit", id: id};
}
xhr.post("backend.php", query, function (reply) {
try {
const dialog = new fox.SingleUseDialog({
id: "filterEditDlg",
title: id ? __("Edit Filter") : __("Create Filter"),
test: function() {
const test_dialog = new fox.SingleUseDialog({
title: "Test Filter",
results: 0,
limit: 100,
max_offset: 10000,
getTestResults: function (params, offset) {
params.method = 'testFilterDo';
params.offset = offset;
params.limit = test_dialog.limit;
console.log("getTestResults:" + offset);
xhr.json("backend.php", params, (result) => {
try {
if (result && test_dialog && test_dialog.open) {
test_dialog.results += result.length;
console.log("got results:" + result.length);
App.byId("prefFilterProgressMsg").innerHTML = __("Looking for articles (%d processed, %f found)...")
.replace("%f", test_dialog.results)
.replace("%d", offset);
console.log(offset + " " + test_dialog.max_offset);
for (let i = 0; i < result.length; i++) {
const tmp = dojo.create("table", { innerHTML: result[i]});
App.byId("prefFilterTestResultList").innerHTML += tmp.innerHTML;
}
if (test_dialog.results < 30 && offset < test_dialog.max_offset) {
// get the next batch
window.setTimeout(function () {
test_dialog.getTestResults(params, offset + test_dialog.limit);
}, 0);
} else {
// all done
Element.hide("prefFilterLoadingIndicator");
if (test_dialog.results == 0) {
App.byId("prefFilterTestResultList").innerHTML = `<tr><td align='center'>
${__('No recent articles matching this filter have been found.')}</td></tr>`;
App.byId("prefFilterProgressMsg").innerHTML = "Articles matching this filter:";
} else {
App.byId("prefFilterProgressMsg").innerHTML = __("Found %d articles matching this filter:")
.replace("%d", test_dialog.results);
}
}
} else if (!result) {
console.log("getTestResults: can't parse results object");
Element.hide("prefFilterLoadingIndicator");
Notify.error("Error while trying to get filter test results.");
} else {
console.log("getTestResults: dialog closed, bailing out.");
}
} catch (e) {
App.Error.report(e);
}
});
},
content: `
<div>
<img id='prefFilterLoadingIndicator' src='images/indicator_tiny.gif'>&nbsp;
<span id='prefFilterProgressMsg'>Looking for articles...</span>
</div>
<ul class='panel panel-scrollable list list-unstyled' id='prefFilterTestResultList'></ul>
<footer class='text-center'>
<button dojoType='dijit.form.Button' type='submit' class='alt-primary'>${__('Close this window')}</button>
</footer>
`
});
const dialog = new fox.SingleUseDialog({
id: "filterEditDlg",
title: id ? __("Edit Filter") : __("Create Filter"),
test: function() {
const test_dialog = new fox.SingleUseDialog({
title: "Test Filter",
results: 0,
limit: 100,
max_offset: 10000,
getTestResults: function (params, offset) {
params.method = 'testFilterDo';
params.offset = offset;
params.limit = test_dialog.limit;
console.log("getTestResults:" + offset);
xhr.json("backend.php", params, (result) => {
try {
if (result && test_dialog && test_dialog.open) {
test_dialog.results += result.length;
const tmph = dojo.connect(test_dialog, "onShow", null, function (/* e */) {
dojo.disconnect(tmph);
console.log("got results:" + result.length);
test_dialog.getTestResults(dialog.attr('value'), 0);
});
App.byId("prefFilterProgressMsg").innerHTML = __("Looking for articles (%d processed, %f found)...")
.replace("%f", test_dialog.results)
.replace("%d", offset);
test_dialog.show();
},
createNewRuleElement: function(parentNode, replaceNode) {
const rule = dojo.formToJson("filter_new_rule_form");
console.log(offset + " " + test_dialog.max_offset);
xhr.post("backend.php", {op: "pref-filters", method: "printrulename", rule: rule}, (reply) => {
try {
const li = document.createElement('li');
for (let i = 0; i < result.length; i++) {
const tmp = dojo.create("table", { innerHTML: result[i]});
li.innerHTML = `<input dojoType='dijit.form.CheckBox' type='checkbox' onclick='Lists.onRowChecked(this)'>
<span onclick='App.dialogOf(this).onRuleClicked(this)'>${reply}</span>
${App.FormFields.hidden_tag("rule[]", rule)}`;
App.byId("prefFilterTestResultList").innerHTML += tmp.innerHTML;
}
dojo.parser.parse(li);
if (test_dialog.results < 30 && offset < test_dialog.max_offset) {
if (replaceNode) {
parentNode.replaceChild(li, replaceNode);
} else {
parentNode.appendChild(li);
}
} catch (e) {
App.Error.report(e);
}
});
},
createNewActionElement: function(parentNode, replaceNode) {
const form = document.forms["filter_new_action_form"];
// get the next batch
window.setTimeout(function () {
test_dialog.getTestResults(params, offset + test_dialog.limit);
}, 0);
if (form.action_id.value == 7) {
form.action_param.value = form.action_param_label.value;
} else if (form.action_id.value == 9) {
form.action_param.value = form.action_param_plugin.value;
}
const action = dojo.formToJson(form);
} else {
// all done
xhr.post("backend.php", { op: "pref-filters", method: "printactionname", action: action }, (reply) => {
try {
const li = document.createElement('li');
Element.hide("prefFilterLoadingIndicator");
li.innerHTML = `<input dojoType='dijit.form.CheckBox' type='checkbox' onclick='Lists.onRowChecked(this)'>
<span onclick='App.dialogOf(this).onActionClicked(this)'>${reply}</span>
${App.FormFields.hidden_tag("action[]", action)}`;
if (test_dialog.results == 0) {
App.byId("prefFilterTestResultList").innerHTML = `<tr><td align='center'>
${__('No recent articles matching this filter have been found.')}</td></tr>`;
App.byId("prefFilterProgressMsg").innerHTML = "Articles matching this filter:";
} else {
App.byId("prefFilterProgressMsg").innerHTML = __("Found %d articles matching this filter:")
.replace("%d", test_dialog.results);
}
dojo.parser.parse(li);
}
if (replaceNode) {
parentNode.replaceChild(li, replaceNode);
} else if (!result) {
console.log("getTestResults: can't parse results object");
Element.hide("prefFilterLoadingIndicator");
Notify.error("Error while trying to get filter test results.");
} else {
parentNode.appendChild(li);
console.log("getTestResults: dialog closed, bailing out.");
}
} catch (e) {
App.Error.report(e);
}
});
},
editRule: function(replaceNode, ruleStr) {
const edit_rule_dialog = new fox.SingleUseDialog({
id: "filterNewRuleDlg",
title: ruleStr ? __("Edit rule") : __("Add rule"),
execute: function () {
if (this.validate()) {
dialog.createNewRuleElement(App.byId("filterDlg_Matches"), replaceNode);
this.hide();
}
},
content: __('Loading, please wait...'),
});
content: `
<div>
<img id='prefFilterLoadingIndicator' src='images/indicator_tiny.gif'>&nbsp;
<span id='prefFilterProgressMsg'>Looking for articles...</span>
</div>
<ul class='panel panel-scrollable list list-unstyled' id='prefFilterTestResultList'></ul>
<footer class='text-center'>
<button dojoType='dijit.form.Button' type='submit' class='alt-primary'>${__('Close this window')}</button>
</footer>
`
});
const tmph = dojo.connect(edit_rule_dialog, "onShow", null, function (/* e */) {
dojo.disconnect(tmph);
const tmph = dojo.connect(test_dialog, "onShow", null, function (/* e */) {
dojo.disconnect(tmph);
xhr.post("backend.php", {op: 'pref-filters', method: 'newrule', rule: ruleStr}, (reply) => {
edit_rule_dialog.attr('content', reply);
});
});
test_dialog.getTestResults(dialog.attr('value'), 0);
});
edit_rule_dialog.show();
},
editAction: function(replaceNode, actionStr) {
const edit_action_dialog = new fox.SingleUseDialog({
title: actionStr ? __("Edit action") : __("Add action"),
hideOrShowActionParam: function(sender) {
const action = sender.value;
test_dialog.show();
},
createNewRuleElement: function(parentNode, replaceNode) {
const rule = dojo.formToJson("filter_new_rule_form");
const action_param = App.byId("filterDlg_paramBox");
xhr.post("backend.php", {op: "pref-filters", method: "printrulename", rule: rule}, (reply) => {
try {
const li = document.createElement('li');
if (!action_param) {
console.log("hideOrShowActionParam: can't find action param box!");
return;
}
li.innerHTML = `<input dojoType='dijit.form.CheckBox' type='checkbox' onclick='Lists.onRowChecked(this)'>
<span onclick='App.dialogOf(this).onRuleClicked(this)'>${reply}</span>
${App.FormFields.hidden_tag("rule[]", rule)}`;
// if selected action supports parameters, enable params field
if (action == 4 || action == 6 || action == 7 || action == 9) {
Element.show(action_param);
dojo.parser.parse(li);
Element.hide(dijit.byId("filterDlg_actionParam").domNode);
Element.hide(dijit.byId("filterDlg_actionParamLabel").domNode);
Element.hide(dijit.byId("filterDlg_actionParamPlugin").domNode);
if (replaceNode) {
parentNode.replaceChild(li, replaceNode);
} else {
parentNode.appendChild(li);
}
} catch (e) {
App.Error.report(e);
}
});
},
createNewActionElement: function(parentNode, replaceNode) {
const form = document.forms["filter_new_action_form"];
if (form.action_id.value == 7) {
form.action_param.value = form.action_param_label.value;
} else if (form.action_id.value == 9) {
form.action_param.value = form.action_param_plugin.value;
}
if (action == 7) {
Element.show(dijit.byId("filterDlg_actionParamLabel").domNode);
} else if (action == 9) {
Element.show(dijit.byId("filterDlg_actionParamPlugin").domNode);
} else {
Element.show(dijit.byId("filterDlg_actionParam").domNode);
}
const action = dojo.formToJson(form);
} else {
Element.hide(action_param);
}
},
xhr.post("backend.php", { op: "pref-filters", method: "printactionname", action: action }, (reply) => {
try {
const li = document.createElement('li');
execute: function () {
if (this.validate()) {
dialog.createNewActionElement(App.byId("filterDlg_Actions"), replaceNode);
this.hide();
}
}
});
li.innerHTML = `<input dojoType='dijit.form.CheckBox' type='checkbox' onclick='Lists.onRowChecked(this)'>
<span onclick='App.dialogOf(this).onActionClicked(this)'>${reply}</span>
${App.FormFields.hidden_tag("action[]", action)}`;
const tmph = dojo.connect(edit_action_dialog, "onShow", null, function (/* e */) {
dojo.disconnect(tmph);
dojo.parser.parse(li);
xhr.post("backend.php", {op: 'pref-filters', method: 'newaction', action: actionStr}, (reply) => {
edit_action_dialog.attr('content', reply);
});
});
if (replaceNode) {
parentNode.replaceChild(li, replaceNode);
} else {
parentNode.appendChild(li);
}
edit_action_dialog.show();
},
selectRules: function (select) {
Lists.select("filterDlg_Matches", select);
},
selectActions: function (select) {
Lists.select("filterDlg_Actions", select);
} catch (e) {
App.Error.report(e);
}
});
},
editRule: function(replaceNode, ruleStr) {
const edit_rule_dialog = new fox.SingleUseDialog({
id: "filterNewRuleDlg",
title: ruleStr ? __("Edit rule") : __("Add rule"),
execute: function () {
if (this.validate()) {
dialog.createNewRuleElement(App.byId("filterDlg_Matches"), replaceNode);
this.hide();
}
},
onRuleClicked: function (e) {
const li = e.closest('li');
const rule = li.querySelector('input[name="rule[]"]').value
content: __('Loading, please wait...'),
});
this.editRule(li, rule);
},
onActionClicked: function (e) {
const li = e.closest('li');
const action = li.querySelector('input[name="action[]"]').value
const tmph = dojo.connect(edit_rule_dialog, "onShow", null, function (/* e */) {
dojo.disconnect(tmph);
this.editAction(li, action);
},
removeFilter: function () {
const msg = __("Remove filter?");
xhr.post("backend.php", {op: 'pref-filters', method: 'newrule', rule: ruleStr}, (reply) => {
edit_rule_dialog.attr('content', reply);
});
});
if (confirm(msg)) {
this.hide();
edit_rule_dialog.show();
},
editAction: function(replaceNode, actionStr) {
const edit_action_dialog = new fox.SingleUseDialog({
title: actionStr ? __("Edit action") : __("Add action"),
hideOrShowActionParam: function(sender) {
const action = sender.value;
const action_param = App.byId("filterDlg_paramBox");
if (!action_param) {
console.log("hideOrShowActionParam: can't find action param box!");
return;
}
Notify.progress("Removing filter...");
// if selected action supports parameters, enable params field
if (action == 4 || action == 6 || action == 7 || action == 9) {
Element.show(action_param);
const query = {op: "pref-filters", method: "remove", ids: this.attr('value').id};
Element.hide(dijit.byId("filterDlg_actionParam").domNode);
Element.hide(dijit.byId("filterDlg_actionParamLabel").domNode);
Element.hide(dijit.byId("filterDlg_actionParamPlugin").domNode);
xhr.post("backend.php", query, () => {
const tree = dijit.byId("filterTree");
if (action == 7) {
Element.show(dijit.byId("filterDlg_actionParamLabel").domNode);
} else if (action == 9) {
Element.show(dijit.byId("filterDlg_actionParamPlugin").domNode);
} else {
Element.show(dijit.byId("filterDlg_actionParam").domNode);
}
if (tree) tree.reload();
});
} else {
Element.hide(action_param);
}
},
addAction: function () {
this.editAction();
},
addRule: function () {
this.editRule();
},
deleteAction: function () {
App.findAll("#filterDlg_Actions li[class*=Selected]").forEach(function (e) {
e.parentNode.removeChild(e)
});
},
deleteRule: function () {
App.findAll("#filterDlg_Matches li[class*=Selected]").forEach(function (e) {
e.parentNode.removeChild(e)
});
},
execute: function () {
if (this.validate()) {
dialog.createNewActionElement(App.byId("filterDlg_Actions"), replaceNode);
this.hide();
}
}
});
Notify.progress("Saving data...", true);
xhr.post("backend.php", this.attr('value'), () => {
dialog.hide();
const tmph = dojo.connect(edit_action_dialog, "onShow", null, function (/* e */) {
dojo.disconnect(tmph);
const tree = dijit.byId("filterTree");
if (tree) tree.reload();
});
}
},
content: reply
xhr.post("backend.php", {op: 'pref-filters', method: 'newaction', action: actionStr}, (reply) => {
edit_action_dialog.attr('content', reply);
});
});
if (!App.isPrefs()) {
/* global getSelectionText */
const selectedText = getSelectionText();
edit_action_dialog.show();
},
selectRules: function (select) {
Lists.select("filterDlg_Matches", select);
},
selectActions: function (select) {
Lists.select("filterDlg_Actions", select);
},
onRuleClicked: function (e) {
const li = e.closest('li');
const rule = li.querySelector('input[name="rule[]"]').value
const lh = dojo.connect(dialog, "onShow", function () {
dojo.disconnect(lh);
this.editRule(li, rule);
},
onActionClicked: function (e) {
const li = e.closest('li');
const action = li.querySelector('input[name="action[]"]').value
if (selectedText != "") {
this.editAction(li, action);
},
removeFilter: function () {
const msg = __("Remove filter?");
const feed_id = Feeds.activeIsCat() ? 'CAT:' + parseInt(Feeds.getActive()) :
Feeds.getActive();
if (confirm(msg)) {
this.hide();
const rule = {reg_exp: selectedText, feed_id: [feed_id], filter_type: 1};
Notify.progress("Removing filter...");
dialog.editRule(null, dojo.toJson(rule));
const query = {op: "pref-filters", method: "remove", ids: this.attr('value').id};
} else {
xhr.post("backend.php", query, () => {
const tree = dijit.byId("filterTree");
if (tree) tree.reload();
});
}
},
addAction: function () {
this.editAction();
},
addRule: function () {
this.editRule();
},
deleteAction: function () {
App.findAll("#filterDlg_Actions li[class*=Selected]").forEach(function (e) {
e.parentNode.removeChild(e)
});
},
deleteRule: function () {
App.findAll("#filterDlg_Matches li[class*=Selected]").forEach(function (e) {
e.parentNode.removeChild(e)
});
},
execute: function () {
if (this.validate()) {
Notify.progress("Saving data...", true);
xhr.post("backend.php", this.attr('value'), () => {
dialog.hide();
const tree = dijit.byId("filterTree");
if (tree) tree.reload();
});
}
},
content: __("Loading, please wait...")
});
const query = {op: "article", method: "getmetadatabyid", id: Article.getActive()};
const tmph = dojo.connect(dialog, 'onShow', function () {
dojo.disconnect(tmph);
xhr.json("backend.php", query, (reply) => {
let title = false;
xhr.post("backend.php", query, function (reply) {
dialog.attr('content', reply);
if (reply && reply.title) title = reply.title;
if (!App.isPrefs()) {
const selectedText = App.getSelectedText();
if (title || Feeds.getActive() || Feeds.activeIsCat()) {
if (selectedText != "") {
const feed_id = Feeds.activeIsCat() ? 'CAT:' + parseInt(Feeds.getActive()) :
Feeds.getActive();
const rule = {reg_exp: selectedText, feed_id: [feed_id], filter_type: 1};
console.log(title + " " + Feeds.getActive());
dialog.editRule(null, dojo.toJson(rule));
} else {
const query = {op: "article", method: "getmetadatabyid", id: Article.getActive()};
const feed_id = Feeds.activeIsCat() ? 'CAT:' + parseInt(Feeds.getActive()) :
Feeds.getActive();
xhr.json("backend.php", query, (reply) => {
let title;
const rule = {reg_exp: title, feed_id: [feed_id], filter_type: 1};
if (reply && reply.title) title = reply.title;
dialog.editRule(null, dojo.toJson(rule));
}
});
}
});
}
dialog.show();
if (title || Feeds.getActive() || Feeds.activeIsCat()) {
console.log(title + " " + Feeds.getActive());
const feed_id = Feeds.activeIsCat() ? 'CAT:' + parseInt(Feeds.getActive()) :
Feeds.getActive();
const rule = {reg_exp: title, feed_id: [feed_id], filter_type: 1};
} catch (e) {
App.Error.report(e);
}
dialog.editRule(null, dojo.toJson(rule));
}
});
}
}
});
});
dialog.show();
},
};

@ -401,25 +401,3 @@ const Notify = {
}
};
// http://stackoverflow.com/questions/6251937/how-to-get-selecteduser-highlighted-text-in-contenteditable-element-and-replac
/* exported getSelectionText */
function getSelectionText() {
let text = "";
if (typeof window.getSelection != "undefined") {
const sel = window.getSelection();
if (sel.rangeCount) {
const container = document.createElement("div");
for (let i = 0, len = sel.rangeCount; i < len; ++i) {
container.appendChild(sel.getRangeAt(i).cloneContents());
}
text = container.innerHTML;
}
} else if (typeof document.selection != "undefined") {
if (document.selection.type == "Text") {
text = document.selection.createRange().textText;
}
}
return text.stripTags();
}

Loading…
Cancel
Save