thedesk/app/js/platform/nano.js

256 lines
6.3 KiB
JavaScript
Raw Normal View History

2018-03-11 01:22:59 +11:00
//TL取得
2019-10-14 02:28:10 +11:00
var websocket;
2018-03-11 01:22:59 +11:00
function tl(data) {
2019-05-19 17:39:30 +10:00
var tlid = 0;
2019-10-14 02:28:10 +11:00
if(websocket){
websocket.close()
}
2019-05-19 17:39:30 +10:00
var acct_id = $("#post-acct-sel").val();
var type = $("#type-sel").val();
2018-03-11 01:22:59 +11:00
var domain = localStorage.getItem("domain_" + acct_id);
//タグの場合はカラム追加して描画
2019-05-19 17:39:30 +10:00
if (!type) {
//デフォルト
var type = "local";
}
var at = localStorage.getItem("acct_" + acct_id + "_at");
2018-03-11 01:22:59 +11:00
$("#notice_nano").text(cap(type, data) + " TL(" + localStorage.getItem(
"user_" + acct_id) + "@" + domain + ")");
var start = "https://" + domain + "/api/v1/timelines/" + com(type, data);
fetch(start, {
method: 'GET',
headers: {
'content-type': 'application/json',
'Authorization': 'Bearer ' + at
},
2019-05-19 17:39:30 +10:00
}).then(function (response) {
2018-03-11 01:22:59 +11:00
return response.json();
2019-05-19 17:39:30 +10:00
}).catch(function (error) {
2018-03-11 01:22:59 +11:00
console.error(error);
2019-05-19 17:39:30 +10:00
}).then(function (json) {
2018-03-11 01:22:59 +11:00
var templete = parse([json[0]], '', acct_id, tlid);
$("#timeline_nano").html(templete);
jQuery("time.timeago").timeago();
2019-10-14 02:28:10 +11:00
$("#menu").addClass("hide");
2018-03-11 01:22:59 +11:00
});
2019-05-19 17:39:30 +10:00
//Streaming接続
var tlid = 0;
2018-03-11 01:22:59 +11:00
if (type == "home") {
var start = "wss://" + domain +
"/api/v1/streaming/?stream=user&access_token=" + at;
} else if (type == "pub") {
var start = "wss://" + domain +
"/api/v1/streaming/?stream=public&access_token=" + at;
} else if (type == "local") {
var start = "wss://" + domain +
"/api/v1/streaming/?stream=public:local&access_token=" + at;
} else if (type == "tag") {
var start = "wss://" + domain +
2019-05-19 17:39:30 +10:00
"/api/v1/streaming/?stream=hashtag&tag=" + data + "&access_token=" + at;
}
2019-10-14 02:28:10 +11:00
websocket = new WebSocket(start);
websocket.onopen = function (mess) {
2018-03-11 01:22:59 +11:00
$("#notice_icon_" + tlid).removeClass("red-text");
}
2019-10-14 02:28:10 +11:00
websocket.onmessage = function (mess) {
2018-03-11 01:22:59 +11:00
var typeA = JSON.parse(mess.data).event;
2019-10-14 02:28:10 +11:00
if (typeA == "update") {
2018-03-11 01:22:59 +11:00
var obj = JSON.parse(JSON.parse(mess.data).payload);
2019-05-19 17:39:30 +10:00
var templete = parse([obj], '', acct_id, tlid);
2019-10-14 02:28:10 +11:00
jQuery("time.timeago").timeago();
2019-05-19 17:39:30 +10:00
$("#timeline_nano").html(templete);
2018-03-11 01:22:59 +11:00
}
2019-10-14 02:28:10 +11:00
2018-03-11 01:22:59 +11:00
}
2019-10-14 02:28:10 +11:00
websocket.onerror = function (error) {
2018-03-11 01:22:59 +11:00
console.error('WebSocket Error ' + error);
};
2019-10-14 02:28:10 +11:00
websocket.onclose = function (mess) {
console.error("Close Streaming API:" + type);
}
2018-03-11 01:22:59 +11:00
}
//TLのタイトル
function cap(type, data) {
if (type == "home") {
return "Home"
} else if (type == "local") {
return "Local"
} else if (type == "pub") {
return "Public"
} else if (type == "tag") {
return "#" + data
} else if (type == "list") {
return "List(id:" + data + ")"
} else if (type == "notf") {
return "Notification"
}
}
//TLのURL
function com(type, data) {
if (type == "home") {
return "home?"
} else if (type == "local") {
return "public?local=true&"
} else if (type == "pub") {
return "public?"
} else if (type == "tag") {
return "tag/" + data + "?"
}
if (type == "list") {
return "list/" + data + "?"
}
}
//TLのアイコン
function icon(type) {
if (type == "home") {
return "home"
} else if (type == "local") {
return "people_outline"
} else if (type == "pub") {
return "language"
} else if (type == "tag") {
return "search"
}
if (type == "list") {
return "subject"
}
}
2019-05-19 17:39:30 +10:00
function todo() { }
function todc() { }
function hide() { }
$(function ($) {
2018-03-13 04:41:38 +11:00
//キーボードショートカット
2019-05-19 17:39:30 +10:00
$(window).keydown(function (e) {
2018-03-13 04:41:38 +11:00
var hasFocus = $('input').is(':focus');
var hasFocus2 = $('textarea').is(':focus');
//Ctrl+Enter:投稿
if (event.ctrlKey) {
if (e.keyCode === 13) {
post();
return false;
}
}
});
});
2019-10-14 02:28:10 +11:00
function set() {
$("#menu").toggleClass("hide");
if($("#menu").hasClass("hide")){
$("#setting").text("Setting")
}else{
$("#setting").text("Close")
}
}
var multi = localStorage.getItem("multi");
if (!multi) {
var obj = [
{
at: localStorage.getItem(localStorage.getItem("domain_" + acct_id) + "_at"),
name: localStorage.getItem("name_" + acct_id),
domain: localStorage.getItem("domain_" + acct_id),
user: localStorage.getItem("user_" + acct_id),
prof: localStorage.getItem("prof_" + acct_id)
}
];
var json = JSON.stringify(obj);
localStorage.setItem("multi", json);
} else {
var obj = JSON.parse(multi);
}
var templete;
var last = localStorage.getItem("last-use");
var sel;
Object.keys(obj).forEach(function(key) {
var acct = obj[key];
var list = key * 1 + 1;
if (key == last) {
sel = "selected";
} else {
sel = "";
}
templete = '<option value="' + key + '" ' + sel + ">" + acct.user + "@" + acct.domain + "</option>";
$("#post-acct-sel").append(templete);
});
function mov() {
return false;
}
function resetmv() {
return false;
}
function post() {
var acct_id = $("#post-acct-sel").val();
var domain = localStorage.getItem("domain_" + acct_id);
var at = localStorage.getItem("acct_" + acct_id + "_at");
var start = "https://" + domain + "/api/v1/statuses";
var str = $("#textarea").val();
var toot = {
status: str
};
var vis = loadVis(acct_id);
toot.visibility = vis;
var httpreq = new XMLHttpRequest();
httpreq.open("POST", start, true);
httpreq.setRequestHeader("Content-Type", "application/json");
httpreq.setRequestHeader("Authorization", "Bearer " + at);
httpreq.responseType = "json";
httpreq.send(JSON.stringify(toot));
httpreq.onreadystatechange = function() {
if (httpreq.readyState === 4) {
$("#textarea").val("");
}
};
}
function loadVis(acct_id) {
var vist = localStorage.getItem("vis");
if (!vist) {
return "public";
} else {
if (vist == "memory") {
var memory = localStorage.getItem("vis-memory-" + acct_id);
if (!memory) {
memory = "public";
}
return memory;
} else if (vist == "server" || vist == "useapi") {
var multi = localStorage.getItem("multi");
var obj = JSON.parse(multi);
var memory = obj[acct_id]["vis"];
if (!memory) {
memory = "public";
}
return memory;
} else {
return vist;
}
}
}
function loader(){
var acct_id = $("#post-acct-sel").val()
console.log(loadVis(acct_id))
$("#vis-sel").val(loadVis(acct_id));
}
loader()
$("textarea").height(15); //init
$("textarea").css("lineHeight", "1rem"); //init
$("textarea").on("input", function(evt) {
if (evt.target.scrollHeight > evt.target.offsetHeight) {
$(evt.target).height(evt.target.scrollHeight);
} else {
var lineHeight = Number(
$(evt.target)
.css("lineHeight")
.split("px")[0]
);
while (true) {
$(evt.target).height($(evt.target).height() - lineHeight);
if (evt.target.scrollHeight > evt.target.offsetHeight) {
$(evt.target).height(evt.target.scrollHeight);
break;
}
}
}
});