thedesk/app/js/post/suggest.js

204 lines
5.8 KiB
JavaScript
Raw Normal View History

2018-01-28 23:22:43 +11:00
//入力時にハッシュタグと@をサジェスト
var timer = null;
var input = document.getElementById("textarea");
var prev_val = input.value;
var oldSuggest;
var suggest;
2018-04-09 00:17:33 +10:00
2019-05-19 17:39:30 +10:00
input.addEventListener("focus", function () {
2018-04-09 00:17:33 +10:00
var acct_id = $("#post-acct-sel").val();
2018-01-28 23:22:43 +11:00
$("#suggest").html("");
window.clearInterval(timer);
2019-05-19 17:39:30 +10:00
timer = window.setInterval(function () {
2018-01-28 23:22:43 +11:00
var new_val = input.value;
2019-05-19 17:39:30 +10:00
if (new_val == "") {
2019-03-13 02:51:07 +11:00
$("#suggest").html("");
2019-05-19 17:39:30 +10:00
if ($("#poll").hasClass("hide") && $("#emoji").hasClass("hide")) {
2019-03-13 02:51:07 +11:00
$("#right-side").hide()
}
return;
}
2018-01-28 23:22:43 +11:00
if (prev_val != new_val) {
2018-04-09 00:17:33 +10:00
var semoji = new_val.match(/:(\S{3,})/);
2019-05-19 17:39:30 +10:00
if (semoji) {
2018-04-09 00:17:33 +10:00
var obj = JSON.parse(localStorage.getItem("emoji_" + acct_id));
2019-05-19 17:39:30 +10:00
if (!obj) {
var ehtml = lang.lang_suggest_nodata;
} else {
2018-08-11 01:58:00 +10:00
var num = obj.length;
2019-05-19 17:39:30 +10:00
var ehtml = "";
2018-08-11 01:58:00 +10:00
for (i = 0; i < num; i++) {
var emoji = obj[i];
2019-05-19 17:39:30 +10:00
if (~emoji.shortcode.indexOf(semoji[1])) {
2018-08-11 01:58:00 +10:00
if (emoji) {
2019-05-19 17:39:30 +10:00
ehtml = ehtml + '<a onclick="emojiInsert(\':' + emoji.shortcode +
': \',\':' + semoji[1] + '\')" class="pointer"><img src="' + emoji.url + '" width="20"></a>';
2018-08-11 01:58:00 +10:00
}
2018-04-09 00:17:33 +10:00
}
2019-05-19 17:39:30 +10:00
}
2018-08-11 01:58:00 +10:00
}
2019-05-19 17:39:30 +10:00
if (ehtml != "") {
2019-03-13 02:51:07 +11:00
$("#right-side").show()
$("#poll").addClass("hide")
$("#emoji").addClass("hide")
2019-05-19 17:39:30 +10:00
} else {
if ($("#poll").hasClass("hide") && $("#emoji").hasClass("hide")) {
2019-03-13 02:51:07 +11:00
$("#right-side").hide()
}
}
2018-04-09 00:17:33 +10:00
$("#suggest").html(ehtml);
}
2019-05-19 17:39:30 +10:00
2018-01-28 23:22:43 +11:00
var tag = new_val.match(/#(\S{3,})/);
var acct = new_val.match(/@(\S{3,})/);
2019-04-11 02:52:01 +10:00
if (tag && tag[1]) {
2018-01-28 23:22:43 +11:00
var q = tag[1];
2018-03-14 05:31:31 +11:00
} else if (acct && acct[1]) {
2018-01-28 23:22:43 +11:00
var q = acct[1];
2019-05-19 17:39:30 +10:00
} else {
2019-03-13 02:51:07 +11:00
$("#suggest").html("");
2019-05-19 17:39:30 +10:00
if ($("#poll").hasClass("hide") && $("#emoji").hasClass("hide")) {
2019-03-13 02:51:07 +11:00
$("#right-side").hide()
}
2018-01-28 23:22:43 +11:00
return;
}
var domain = localStorage.getItem("domain_" + acct_id);
2019-05-19 17:39:30 +10:00
var at = localStorage.getItem("acct_" + acct_id + "_at");
2019-07-29 01:16:49 +10:00
suggest = "https://" + domain + "/api/v2/search?q=" + q
2018-01-28 23:22:43 +11:00
if (suggest != oldSuggest) {
2019-05-19 17:39:30 +10:00
console.log("Try to get suggest at " + suggest)
2018-01-28 23:22:43 +11:00
fetch(suggest, {
method: 'GET',
headers: {
'content-type': 'application/json',
'Authorization': 'Bearer ' + at
},
2019-05-19 17:39:30 +10:00
}).then(function (response) {
2018-01-28 23:22:43 +11:00
return response.json();
2019-05-19 17:39:30 +10:00
}).catch(function (error) {
2018-01-28 23:22:43 +11:00
todo(error);
console.error(error);
2019-05-19 17:39:30 +10:00
}).then(function (json) {
2019-07-29 01:16:49 +10:00
console.log(["Search", json]);
//ハッシュタグ
2019-03-13 02:51:07 +11:00
if (json.hashtags[0] && tag) {
2019-05-19 17:39:30 +10:00
if (tag[1]) {
2019-07-29 01:42:17 +10:00
var tags = [];
2019-05-19 17:39:30 +10:00
Object.keys(json.hashtags).forEach(function (key4) {
var tag = json.hashtags[key4];
2019-07-29 01:16:49 +10:00
var his = tag.history;
2019-07-29 01:42:17 +10:00
var uses = his[0].uses * 1 + his[1].uses * 1 + his[2].uses * 1 + his[3].uses * 1 + his[4].uses * 1 + his[5].uses * 1 + his[6].uses * 1;
tagHTML = '<br><a onclick="tagInsert(\'#' + escapeHTML(tag.name) + '\',\'#' + q + '\')" class="pointer">#' +
2019-07-29 01:52:12 +10:00
escapeHTML(tag.name) + '</a>&nbsp;' + uses + 'toot(s)'
2019-07-29 01:42:17 +10:00
var item = {
"uses": uses,
"html": tagHTML
}
tags.push(item)
2019-05-19 17:39:30 +10:00
});
2019-07-29 01:42:17 +10:00
var num_a = -1;
var num_b = 1;
tags = tags.sort(function (a, b) {
var x = a["uses"];
var y = b["uses"];
if (x > y) return num_a;
if (x < y) return num_b;
return 0;
});
var ins = ""
2019-07-29 01:52:12 +10:00
var nev = false
2019-07-29 01:42:17 +10:00
Object.keys(tags).forEach(function (key7) {
ins = ins + tags[key7].html
2019-07-29 01:52:12 +10:00
if (key7 <= 0 && !nev) {
ins = ins + '<br>'
nev = true
}
2019-07-29 01:42:17 +10:00
});
$("#suggest").html(ins);
2019-05-19 17:39:30 +10:00
$("#right-side").show()
$("#poll").addClass("hide")
$("#emoji").addClass("hide")
2019-03-13 02:51:07 +11:00
}
2018-01-28 23:22:43 +11:00
} else if (json.accounts[0] && acct[1]) {
var accts = "";
2019-05-19 17:39:30 +10:00
Object.keys(json.accounts).forEach(function (key3) {
2018-01-28 23:22:43 +11:00
var acct = json.accounts[key3];
2019-05-19 17:39:30 +10:00
if (acct.acct != q) {
2019-03-13 02:51:07 +11:00
accts = accts + '<a onclick="tagInsert(\'@' + acct.acct +
2019-05-19 17:39:30 +10:00
'\',\'@' + q + '\')" class="pointer">@' + acct.acct + '</a><br>';
2019-03-13 02:51:07 +11:00
}
2018-01-28 23:22:43 +11:00
});
2019-03-13 02:51:07 +11:00
$("#right-side").show()
2019-03-21 15:39:30 +11:00
$("#suggest").html(accts);
2019-03-13 02:51:07 +11:00
$("#poll").addClass("hide")
$("#emoji").addClass("hide")
2018-01-28 23:22:43 +11:00
} else {
2019-05-19 17:39:30 +10:00
if ($("#poll").hasClass("hide") && $("#emoji").hasClass("hide")) {
2019-03-13 02:51:07 +11:00
$("#right-side").hide()
}
2018-01-28 23:22:43 +11:00
}
});
}
};
oldSuggest = suggest;
prev_value = new_val;
}, 1000);
}, false);
2019-05-19 17:39:30 +10:00
input.addEventListener("blur", function () {
2018-01-28 23:22:43 +11:00
window.clearInterval(timer);
2018-03-11 01:22:59 +11:00
favTag();
2018-01-28 23:22:43 +11:00
}, false);
2018-03-14 05:31:31 +11:00
function tagInsert(code, del) {
var now = $("#textarea").val();
var selin = $("#textarea").prop('selectionStart');
if (!del) {
} else {
2019-06-16 22:52:41 +10:00
var regExp = new RegExp(del.replace(/[.*+?^=!:${}()|[\]\/\\]/g, "\\$&"), "g");
2018-03-14 05:31:31 +11:00
var now = now.replace(regExp, "");
2019-05-19 17:39:30 +10:00
selin = selin - del.length;
2018-03-14 05:31:31 +11:00
}
2019-05-19 17:39:30 +10:00
if (selin > 0) {
var before = now.substr(0, selin);
var after = now.substr(selin, now.length);
newt = before + " " + code + " " + after;
} else {
newt = code + " " + now;
2018-03-14 05:31:31 +11:00
}
$("#textarea").val(newt);
$("#textarea").focus();
2019-05-19 17:39:30 +10:00
if ($("#poll").hasClass("hide") && $("#emoji").hasClass("hide")) {
2019-03-13 02:51:07 +11:00
$("#right-side").hide()
}
2018-03-14 05:31:31 +11:00
$("#suggest").html("");
2018-07-22 23:03:46 +10:00
}
2019-05-19 17:39:30 +10:00
function cgNPs(q) {
2018-07-22 23:03:46 +10:00
suggest = "https://cg.toot.app/api/v1/search/light?q=" + q
2019-05-19 17:39:30 +10:00
if (suggest != oldSuggest) {
console.log("Try to get suggest at " + suggest)
fetch(suggest, {
method: 'GET',
headers: {
'content-type': 'application/json'
},
}).then(function (response) {
return response.json();
}).catch(function (error) {
todo(error);
console.error(error);
}).then(function (json) {
if (json[0]) {
var tags = "";
Object.keys(json).forEach(function (key4) {
var tag = json[key4];
tags = tags + '<a onclick="cgNP(\'' + json[key4] + '\')" class="pointer">' + escapeHTML(json[key4]) + '</a> ';
2018-07-22 23:03:46 +10:00
});
2019-05-19 17:39:30 +10:00
$("#suggest").html("Cinderella NowPlaying:" + tags);
} else {
$("#suggest").html("Cinderella NowPlaying:Not Found");
2018-07-22 23:03:46 +10:00
}
2019-05-19 17:39:30 +10:00
});
}
2018-03-14 05:31:31 +11:00
}