Skip to content
Snippets Groups Projects
Commit 76e83a9c authored by syuilo's avatar syuilo
Browse files

Use tab for indantaion

parent 26d0d131
No related branches found
No related tags found
No related merge requests found
...@@ -28,17 +28,17 @@ require('fetch'); ...@@ -28,17 +28,17 @@ require('fetch');
// ↓ NodeList、HTMLCollectionで forEach を使えるようにする // ↓ NodeList、HTMLCollectionで forEach を使えるようにする
if (NodeList.prototype.forEach === undefined) { if (NodeList.prototype.forEach === undefined) {
NodeList.prototype.forEach = Array.prototype.forEach; NodeList.prototype.forEach = Array.prototype.forEach;
} }
if (HTMLCollection.prototype.forEach === undefined) { if (HTMLCollection.prototype.forEach === undefined) {
HTMLCollection.prototype.forEach = Array.prototype.forEach; HTMLCollection.prototype.forEach = Array.prototype.forEach;
} }
// ↓ iOSでプライベートモードだとlocalStorageが使えないので既存のメソッドを上書きする // ↓ iOSでプライベートモードだとlocalStorageが使えないので既存のメソッドを上書きする
try { try {
localStorage.setItem('kyoppie', 'yuppie'); localStorage.setItem('kyoppie', 'yuppie');
} catch (e) { } catch (e) {
Storage.prototype.setItem = () => { }; // noop Storage.prototype.setItem = () => { }; // noop
} }
// MAIN PROCESS // MAIN PROCESS
...@@ -52,118 +52,118 @@ checkForUpdate(); ...@@ -52,118 +52,118 @@ checkForUpdate();
const i = (document.cookie.match(/i=(\w+)/) || [null, null])[1]; const i = (document.cookie.match(/i=(\w+)/) || [null, null])[1];
if (i != null) { if (i != null) {
log("ME: " + i); log("ME: " + i);
} }
// ユーザーをフェッチしてコールバックする // ユーザーをフェッチしてコールバックする
module.exports = callback => { module.exports = callback => {
// Get cached account data // Get cached account data
let cachedMe = JSON.parse(localStorage.getItem('me')); let cachedMe = JSON.parse(localStorage.getItem('me'));
if (cachedMe != null && cachedMe.data != null && cachedMe.data.cache) { if (cachedMe != null && cachedMe.data != null && cachedMe.data.cache) {
fetched(cachedMe); fetched(cachedMe);
// 後から新鮮なデータをフェッチ // 後から新鮮なデータをフェッチ
fetchme(i, true, freshData => { fetchme(i, true, freshData => {
Object.assign(cachedMe, freshData); Object.assign(cachedMe, freshData);
cachedMe.trigger('updated'); cachedMe.trigger('updated');
}); });
} else { } else {
// キャッシュ無効なのにキャッシュが残ってたら掃除 // キャッシュ無効なのにキャッシュが残ってたら掃除
if (cachedMe != null) { if (cachedMe != null) {
localStorage.removeItem('me'); localStorage.removeItem('me');
} }
fetchme(i, false, fetched); fetchme(i, false, fetched);
} }
function fetched(me) { function fetched(me) {
if (me != null) { if (me != null) {
riot.observable(me); riot.observable(me);
if (me.data.cache) { if (me.data.cache) {
localStorage.setItem('me', JSON.stringify(me)); localStorage.setItem('me', JSON.stringify(me));
me.on('updated', () => { me.on('updated', () => {
// キャッシュ更新 // キャッシュ更新
localStorage.setItem('me', JSON.stringify(me)); localStorage.setItem('me', JSON.stringify(me));
}); });
} }
log("Fetched! Hello " + me.username + "."); log("Fetched! Hello " + me.username + ".");
} }
mixins(me); mixins(me);
const init = document.getElementById('init'); const init = document.getElementById('init');
init.parentNode.removeChild(init); init.parentNode.removeChild(init);
const app = document.createElement('div'); const app = document.createElement('div');
app.setAttribute('id', 'app'); app.setAttribute('id', 'app');
document.body.appendChild(app); document.body.appendChild(app);
try { try {
callback(me); callback(me);
} catch (e) { } catch (e) {
panic(e); panic(e);
} }
} }
}; };
// ユーザーをフェッチしてコールバックする // ユーザーをフェッチしてコールバックする
function fetchme(token, silent, cb) { function fetchme(token, silent, cb) {
let me = null; let me = null;
// Return when not signed in // Return when not signed in
if (token == null) { if (token == null) {
return done(); return done();
} }
// Fetch user // Fetch user
fetch(CONFIG.api.url + "/i", { fetch(CONFIG.api.url + "/i", {
method: 'POST', method: 'POST',
headers: { headers: {
'Content-Type': 'application/x-www-form-urlencoded; charset=utf-8' 'Content-Type': 'application/x-www-form-urlencoded; charset=utf-8'
}, },
body: "i=" + token body: "i=" + token
}).then(res => { }).then(res => {
// When failed to authenticate user // When failed to authenticate user
if (res.status !== 200) { if (res.status !== 200) {
signout(); signout();
} }
res.json().then(i => { res.json().then(i => {
me = i; me = i;
me.token = token; me.token = token;
// initialize it if user data is empty // initialize it if user data is empty
if (me.data != null) { if (me.data != null) {
done(); done();
} else { } else {
init(); init();
} }
}); });
}).catch(() => { }).catch(() => {
if (!silent) { if (!silent) {
const info = document.body.appendChild(document.createElement('mk-core-error')); const info = document.body.appendChild(document.createElement('mk-core-error'));
riot.mount(info, { riot.mount(info, {
retry: () => { retry: () => {
fetchme(token, false, cb); fetchme(token, false, cb);
} }
}); });
}
});
function done() {
if (cb != null) {
cb(me);
} }
}); }
function done() { function init() {
if (cb != null) { var data, this$ = this;
cb(me); data = generateDefaultUserdata();
} api(token, 'i/appdata/set', {
} data: JSON.stringify(data)
}).then(() => {
function init() { me.data = data;
var data, this$ = this; done();
data = generateDefaultUserdata(); });
api(token, 'i/appdata/set', { }
data: JSON.stringify(data)
}).then(() => {
me.data = data;
done();
});
}
} }
function panic(e) { function panic(e) {
console.error(e); console.error(e);
document.body.innerHTML = '<div id="error"><p>致命的な問題が発生しました。</p></div>'; document.body.innerHTML = '<div id="error"><p>致命的な問題が発生しました。</p></div>';
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment