quickAddFilter: if text is currently highlighted, use that instead of active article title for placeholder text

This commit is contained in:
Andrew Dolgov 2013-11-12 14:21:28 +04:00
parent 43d790f054
commit 0c7e49936a
1 changed files with 52 additions and 15 deletions

View File

@ -1150,33 +1150,48 @@ function quickAddFilter() {
href: query});
if (!inPreferences()) {
var selectedText = getSelectionText();
var lh = dojo.connect(dialog, "onLoad", function(){
dojo.disconnect(lh);
var query = "op=rpc&method=getlinktitlebyid&id=" + getActiveArticleId();
if (selectedText != "") {
new Ajax.Request("backend.php", {
parameters: query,
onComplete: function(transport) {
var reply = JSON.parse(transport.responseText);
var feed_id = activeFeedIsCat() ? 'CAT:' + parseInt(getActiveFeedId()) :
getActiveFeedId();
var title = false;
var rule = { reg_exp: selectedText, feed_id: feed_id, filter_type: 1 };
if (reply && reply) title = reply.title;
addFilterRule(null, dojo.toJson(rule));
if (title || getActiveFeedId() || activeFeedIsCat()) {
} else {
console.log(title + " " + getActiveFeedId());
var query = "op=rpc&method=getlinktitlebyid&id=" + getActiveArticleId();
var feed_id = activeFeedIsCat() ? 'CAT:' + parseInt(getActiveFeedId()) :
getActiveFeedId();
new Ajax.Request("backend.php", {
parameters: query,
onComplete: function(transport) {
var reply = JSON.parse(transport.responseText);
var rule = { reg_exp: title, feed_id: feed_id, filter_type: 1 };
var title = false;
addFilterRule(null, dojo.toJson(rule));
}
if (reply && reply) title = reply.title;
} });
if (title || getActiveFeedId() || activeFeedIsCat()) {
console.log(title + " " + getActiveFeedId());
var feed_id = activeFeedIsCat() ? 'CAT:' + parseInt(getActiveFeedId()) :
getActiveFeedId();
var rule = { reg_exp: title, feed_id: feed_id, filter_type: 1 };
addFilterRule(null, dojo.toJson(rule));
}
} });
}
});
}
@ -1936,3 +1951,25 @@ function feed_to_label_id(feed) {
return _label_base_index - 1 + Math.abs(feed);
}
// http://stackoverflow.com/questions/6251937/how-to-get-selecteduser-highlighted-text-in-contenteditable-element-and-replac
function getSelectionText() {
var text = "";
if (typeof window.getSelection != "undefined") {
var sel = window.getSelection();
if (sel.rangeCount) {
var container = document.createElement("div");
for (var 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();
}