diff options
| author | syuilo <syuilotan@yahoo.co.jp> | 2017-03-18 20:05:11 +0900 |
|---|---|---|
| committer | syuilo <syuilotan@yahoo.co.jp> | 2017-03-18 20:05:11 +0900 |
| commit | 45e8331e261244628b134a18e3d0fbe0ebb3a7dc (patch) | |
| tree | 44ac1719fcea0a61c33698b23fb89400141e00d9 /src/web/app/common/scripts | |
| parent | Better notification (diff) | |
| download | sharkey-45e8331e261244628b134a18e3d0fbe0ebb3a7dc.tar.gz sharkey-45e8331e261244628b134a18e3d0fbe0ebb3a7dc.tar.bz2 sharkey-45e8331e261244628b134a18e3d0fbe0ebb3a7dc.zip | |
:sushi:
Closes #12, #227 and #58
Diffstat (limited to 'src/web/app/common/scripts')
| -rw-r--r-- | src/web/app/common/scripts/api.js | 4 | ||||
| -rw-r--r-- | src/web/app/common/scripts/bytes-to-size.js | 4 | ||||
| -rw-r--r-- | src/web/app/common/scripts/check-for-update.js | 14 | ||||
| -rw-r--r-- | src/web/app/common/scripts/config.js | 2 | ||||
| -rw-r--r-- | src/web/app/common/scripts/contains.js | 4 | ||||
| -rw-r--r-- | src/web/app/common/scripts/copy-to-clipboard.js | 2 | ||||
| -rw-r--r-- | src/web/app/common/scripts/date-stringify.js | 10 | ||||
| -rw-r--r-- | src/web/app/common/scripts/gcd.js | 2 | ||||
| -rw-r--r-- | src/web/app/common/scripts/generate-default-userdata.js | 4 | ||||
| -rw-r--r-- | src/web/app/common/scripts/get-cat.js | 4 | ||||
| -rw-r--r-- | src/web/app/common/scripts/get-post-summary.js | 2 | ||||
| -rw-r--r-- | src/web/app/common/scripts/i.js | 20 | ||||
| -rw-r--r-- | src/web/app/common/scripts/is-promise.js | 2 | ||||
| -rw-r--r-- | src/web/app/common/scripts/loading.js | 2 | ||||
| -rw-r--r-- | src/web/app/common/scripts/messaging-stream.js | 6 | ||||
| -rw-r--r-- | src/web/app/common/scripts/signout.js | 4 | ||||
| -rw-r--r-- | src/web/app/common/scripts/stream.js | 71 | ||||
| -rw-r--r-- | src/web/app/common/scripts/text-compiler.js | 22 | ||||
| -rw-r--r-- | src/web/app/common/scripts/uuid.js | 6 |
19 files changed, 95 insertions, 90 deletions
diff --git a/src/web/app/common/scripts/api.js b/src/web/app/common/scripts/api.js index 3df54b645a..4855f736c7 100644 --- a/src/web/app/common/scripts/api.js +++ b/src/web/app/common/scripts/api.js @@ -2,7 +2,7 @@ * API Request */ -const CONFIG = require('./config'); +import CONFIG from './config'; let spinner = null; let pending = 0; @@ -14,7 +14,7 @@ let pending = 0; * @param {any} [data={}] Data * @return {Promise<any>} Response */ -module.exports = (i, endpoint, data = {}) => { +export default (i, endpoint, data = {}) => { if (++pending === 1) { spinner = document.createElement('div'); spinner.setAttribute('id', 'wait'); diff --git a/src/web/app/common/scripts/bytes-to-size.js b/src/web/app/common/scripts/bytes-to-size.js index 717f9ad507..e143387141 100644 --- a/src/web/app/common/scripts/bytes-to-size.js +++ b/src/web/app/common/scripts/bytes-to-size.js @@ -1,6 +1,6 @@ -module.exports = function(bytes) { +export default bytes => { var sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB']; if (bytes == 0) return '0Byte'; var i = parseInt(Math.floor(Math.log(bytes) / Math.log(1024))); return Math.round(bytes / Math.pow(1024, i), 2) + sizes[i]; -} +}; diff --git a/src/web/app/common/scripts/check-for-update.js b/src/web/app/common/scripts/check-for-update.js new file mode 100644 index 0000000000..637e6a9fb8 --- /dev/null +++ b/src/web/app/common/scripts/check-for-update.js @@ -0,0 +1,14 @@ +import CONFIG from './config'; + +export default function() { + fetch(CONFIG.apiUrl + '/meta', { + method: 'POST' + }).then(res => { + res.json().then(meta => { + if (meta.version != VERSION) { + localStorage.setItem('should-refresh', 'true'); + alert('Misskeyの新しいバージョンがあります。ページを再度読み込みすると更新が適用されます。'); + } + }); + }); +}; diff --git a/src/web/app/common/scripts/config.js b/src/web/app/common/scripts/config.js index 4203431bd0..16f75d6e16 100644 --- a/src/web/app/common/scripts/config.js +++ b/src/web/app/common/scripts/config.js @@ -9,7 +9,7 @@ const apiUrl = `${scheme}//api.${host}`; const devUrl = `${scheme}//dev.${host}`; const aboutUrl = `${scheme}//about.${host}`; -module.exports = { +export default { host, scheme, url, diff --git a/src/web/app/common/scripts/contains.js b/src/web/app/common/scripts/contains.js index fe73666193..a5071b3f25 100644 --- a/src/web/app/common/scripts/contains.js +++ b/src/web/app/common/scripts/contains.js @@ -1,8 +1,8 @@ -module.exports = function(parent, child) { +export default (parent, child) => { let node = child.parentNode; while (node) { if (node == parent) return true; node = node.parentNode; } return false; -} +}; diff --git a/src/web/app/common/scripts/copy-to-clipboard.js b/src/web/app/common/scripts/copy-to-clipboard.js index 2e67024c85..3d2741f8d7 100644 --- a/src/web/app/common/scripts/copy-to-clipboard.js +++ b/src/web/app/common/scripts/copy-to-clipboard.js @@ -1,7 +1,7 @@ /** * Clipboardに値をコピー(TODO: 文字列以外も対応) */ -module.exports = val => { +export default val => { const form = document.createElement('textarea'); form.textContent = val; document.body.appendChild(form); diff --git a/src/web/app/common/scripts/date-stringify.js b/src/web/app/common/scripts/date-stringify.js index d803587f2c..e51de8833d 100644 --- a/src/web/app/common/scripts/date-stringify.js +++ b/src/web/app/common/scripts/date-stringify.js @@ -1,12 +1,12 @@ -module.exports = date => { +export default date => { if (typeof date == 'string') date = new Date(date); return ( - date.getFullYear() + '年' + - (date.getMonth() + 1) + '月' + + date.getFullYear() + '年' + + (date.getMonth() + 1) + '月' + date.getDate() + '日' + ' ' + - date.getHours() + '時' + - date.getMinutes() + '分' + + date.getHours() + '時' + + date.getMinutes() + '分' + ' ' + `(${['日', '月', '火', '水', '木', '金', '土'][date.getDay()]})` ); diff --git a/src/web/app/common/scripts/gcd.js b/src/web/app/common/scripts/gcd.js index 43bfbc57ae..9a19f9da66 100644 --- a/src/web/app/common/scripts/gcd.js +++ b/src/web/app/common/scripts/gcd.js @@ -1,2 +1,2 @@ const gcd = (a, b) => !b ? a : gcd(b, a % b); -module.exports = gcd; +export default gcd; diff --git a/src/web/app/common/scripts/generate-default-userdata.js b/src/web/app/common/scripts/generate-default-userdata.js index fbe2e99075..c19228dd49 100644 --- a/src/web/app/common/scripts/generate-default-userdata.js +++ b/src/web/app/common/scripts/generate-default-userdata.js @@ -1,4 +1,4 @@ -const uuid = require('./uuid.js'); +import uuid from './uuid'; const home = { left: [ @@ -17,7 +17,7 @@ const home = { ] }; -module.exports = () => { +export default () => { const homeData = []; home.left.forEach(widget => { diff --git a/src/web/app/common/scripts/get-cat.js b/src/web/app/common/scripts/get-cat.js index cd28c7bdaa..cad42c88c8 100644 --- a/src/web/app/common/scripts/get-cat.js +++ b/src/web/app/common/scripts/get-cat.js @@ -1,3 +1 @@ -module.exports = () => { - return '(=^・・^=)'; -}; +export default () => '(=^・・^=)'; diff --git a/src/web/app/common/scripts/get-post-summary.js b/src/web/app/common/scripts/get-post-summary.js index 5e8319b61c..83eda8f6b4 100644 --- a/src/web/app/common/scripts/get-post-summary.js +++ b/src/web/app/common/scripts/get-post-summary.js @@ -32,4 +32,4 @@ const summarize = post => { return summary.trim(); }; -module.exports = summarize; +export default summarize; diff --git a/src/web/app/common/scripts/i.js b/src/web/app/common/scripts/i.js deleted file mode 100644 index 20c33c6402..0000000000 --- a/src/web/app/common/scripts/i.js +++ /dev/null @@ -1,20 +0,0 @@ -const riot = require('riot'); - -module.exports = me => { - riot.mixin('i', { - init: function() { - this.I = me; - this.SIGNIN = me != null; - - if (this.SIGNIN) { - this.on('mount', () => { - me.on('updated', this.update); - }); - this.on('unmount', () => { - me.off('updated', this.update); - }); - } - }, - me: me - }); -}; diff --git a/src/web/app/common/scripts/is-promise.js b/src/web/app/common/scripts/is-promise.js index fd3dc42da3..3b4cd70b49 100644 --- a/src/web/app/common/scripts/is-promise.js +++ b/src/web/app/common/scripts/is-promise.js @@ -1 +1 @@ -module.exports = x => typeof x.then == 'function'; +export default x => typeof x.then == 'function'; diff --git a/src/web/app/common/scripts/loading.js b/src/web/app/common/scripts/loading.js index fa7eafaf96..c48e626648 100644 --- a/src/web/app/common/scripts/loading.js +++ b/src/web/app/common/scripts/loading.js @@ -6,7 +6,7 @@ NProgress.configure({ const root = document.getElementsByTagName('html')[0]; -module.exports = { +export default { start: () => { root.classList.add('progress'); NProgress.start(); diff --git a/src/web/app/common/scripts/messaging-stream.js b/src/web/app/common/scripts/messaging-stream.js index 0c8ce3c9d2..50d41c2be9 100644 --- a/src/web/app/common/scripts/messaging-stream.js +++ b/src/web/app/common/scripts/messaging-stream.js @@ -1,6 +1,6 @@ const ReconnectingWebSocket = require('reconnecting-websocket'); -const riot = require('riot'); -const CONFIG = require('./config'); +import * as riot from 'riot'; +import CONFIG from './config'; class Connection { constructor(me, otherparty) { @@ -40,4 +40,4 @@ class Connection { } } -module.exports = Connection; +export default Connection; diff --git a/src/web/app/common/scripts/signout.js b/src/web/app/common/scripts/signout.js index 7242ebc5b0..6c95cfbc9c 100644 --- a/src/web/app/common/scripts/signout.js +++ b/src/web/app/common/scripts/signout.js @@ -1,6 +1,6 @@ -const CONFIG = require('./config'); +import CONFIG from './config'; -module.exports = () => { +export default () => { localStorage.removeItem('me'); document.cookie = `i=; domain=.${CONFIG.host}; expires=Thu, 01 Jan 1970 00:00:01 GMT;`; location.href = '/'; diff --git a/src/web/app/common/scripts/stream.js b/src/web/app/common/scripts/stream.js index fd7bac7faa..d6e6bf8aa5 100644 --- a/src/web/app/common/scripts/stream.js +++ b/src/web/app/common/scripts/stream.js @@ -1,40 +1,53 @@ const ReconnectingWebSocket = require('reconnecting-websocket'); -const riot = require('riot'); -const CONFIG = require('./config'); +import * as riot from 'riot'; +import CONFIG from './config'; -module.exports = me => { - let state = 'initializing'; - const stateEv = riot.observable(); - const event = riot.observable(); - const host = CONFIG.apiUrl.replace('http', 'ws'); - const socket = new ReconnectingWebSocket(`${host}?i=${me.token}`); +class Connection { + constructor(me) { + // BIND ----------------------------------- + this.onOpen = this.onOpen.bind(this); + this.onClose = this.onClose.bind(this); + this.onMessage = this.onMessage.bind(this); + this.close = this.close.bind(this); + // ---------------------------------------- - socket.onopen = () => { - state = 'connected'; - stateEv.trigger('connected'); - }; + this.state = 'initializing'; + this.stateEv = riot.observable(); + this.event = riot.observable(); + this.me = me; - socket.onclose = () => { - state = 'reconnecting'; - stateEv.trigger('closed'); - }; + const host = CONFIG.apiUrl.replace('http', 'ws'); + this.socket = new ReconnectingWebSocket(`${host}?i=${me.token}`); + this.socket.addEventListener('open', this.onOpen); + this.socket.addEventListener('close', this.onClose); + this.socket.addEventListener('message', this.onMessage); - socket.onmessage = message => { + this.event.on('i_updated', me.update); + } + + onOpen() { + this.state = 'connected'; + this.stateEv.trigger('connected'); + } + + onClose() { + this.state = 'reconnecting'; + this.stateEv.trigger('closed'); + } + + onMessage(message) { try { const msg = JSON.parse(message.data); - if (msg.type) { - event.trigger(msg.type, msg.body); - } - } catch (e) { + if (msg.type) this.event.trigger(msg.type, msg.body); + } catch(e) { // noop } - }; + } - event.on('i_updated', me.update); + close() { + this.socket.removeEventListener('open', this.onOpen); + this.socket.removeEventListener('message', this.onMessage); + } +} - return { - stateEv: stateEv, - getState: () => state, - event: event - }; -}; +export default Connection; diff --git a/src/web/app/common/scripts/text-compiler.js b/src/web/app/common/scripts/text-compiler.js index d4570ca923..1743a549aa 100644 --- a/src/web/app/common/scripts/text-compiler.js +++ b/src/web/app/common/scripts/text-compiler.js @@ -1,13 +1,13 @@ -const riot = require('riot'); +import * as riot from 'riot'; //const emojinize = require('emojinize'); -const CONFIG = require('./config'); +import CONFIG from './config'; const escape = text => text .replace(/>/g, '>') .replace(/</g, '<'); -module.exports = (tokens, shouldBreak) => { +export default (tokens, shouldBreak) => { if (shouldBreak == null) { shouldBreak = true; } @@ -20,21 +20,21 @@ module.exports = (tokens, shouldBreak) => { return escape(token.content) .replace(/(\r\n|\n|\r)/g, shouldBreak ? '<br>' : ' '); case 'bold': - return '<strong>' + escape(token.bold) + '</strong>'; + return `<strong>${escape(token.bold)}</strong>`; case 'url': - return '<mk-url href="' + escape(token.content) + '" target="_blank"></mk-url>'; + return `<mk-url href="${escape(token.content)}" target="_blank"></mk-url>`; case 'link': - return '<a class="link" href="' + escape(token.url) + '" target="_blank">' + escape(token.title) + '</a>'; + return `<a class="link" href="${escape(token.url)}" target="_blank" title="${escape(token.url)}">${escape(token.title)}</a>`; case 'mention': - return '<a href="' + CONFIG.url + '/' + escape(token.username) + '" target="_blank" data-user-preview="' + token.content + '" ' + (me && me.username == token.username ? 'data-is-me' : '') + '>' + token.content + '</a>'; + return `<a href="${CONFIG.url + '/' + escape(token.username)}" target="_blank" data-user-preview="${token.content}" ${me && me.username == token.username ? 'data-is-me' : ''}>${token.content}</a>`; case 'hashtag': // TODO - return '<a>' + escape(token.content) + '</a>'; + return `<a>${escape(token.content)}</a>`; case 'code': - return '<pre><code>' + token.html + '</code></pre>'; + return `<pre><code>${token.html}</code></pre>`; case 'inline-code': - return '<code>' + token.html + '</code>'; + return `<code>${token.html}</code>`; case 'emoji': - return '<i>' + token.content + '</i>'; + return `<i>${token.content}</i>`; //return emojinize.encode(token.content) } }).join(''); diff --git a/src/web/app/common/scripts/uuid.js b/src/web/app/common/scripts/uuid.js index 6161190d63..6a103d6e04 100644 --- a/src/web/app/common/scripts/uuid.js +++ b/src/web/app/common/scripts/uuid.js @@ -1,12 +1,12 @@ -module.exports = function () { +export default () => { var uuid = '', i, random; for (i = 0; i < 32; i++) { random = Math.random() * 16 | 0; if (i == 8 || i == 12 || i == 16 || i == 20) { - uuid += '-' + uuid += '-'; } uuid += (i == 12 ? 4 : (i == 16 ? (random & 3 | 8) : random)).toString(16); } return uuid; -} +}; |