thedesk/app/js/platform/nano.js

265 lines
6.5 KiB
JavaScript
Raw Permalink Normal View History

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