Convert common.js to es6

This commit is contained in:
Yukai Huang 2017-01-05 16:48:23 +08:00
parent 45c202172e
commit 6a06c0bb9f
2 changed files with 65 additions and 70 deletions

View file

@ -1,30 +1,37 @@
var config = require('./config'); // import config from './config';
var domain = config.domain; // domain name
var urlpath = config.urlpath; // sub url path, like: www.example.com/<urlpath> import {
var debug = config.debug; domain, // domain name
var GOOGLE_API_KEY = config.GOOGLE_API_KEY; urlpath, // sub url path, like: www.example.com/<urlpath>
var GOOGLE_CLIENT_ID = config.GOOGLE_CLIENT_ID; debug,
var DROPBOX_APP_KEY = config.DROPBOX_APP_KEY; GOOGLE_API_KEY,
GOOGLE_CLIENT_ID,
DROPBOX_APP_KEY
} from './config';
//common //common
var port = window.location.port; export const port = window.location.port;
window.serverurl = window.location.protocol + '//' + (domain ? domain : window.location.hostname) + (port ? ':' + port : '') + (urlpath ? '/' + urlpath : ''); window.serverurl = `${window.location.protocol}//${domain ? domain : window.location.hostname}${port ? ':' + port : ''}${urlpath ? '/' + urlpath : ''}`;
var noteid = urlpath ? window.location.pathname.slice(urlpath.length + 1, window.location.pathname.length).split('/')[1] : window.location.pathname.split('/')[1]; export const noteid = urlpath ? window.location.pathname.slice(urlpath.length + 1, window.location.pathname.length).split('/')[1] : window.location.pathname.split('/')[1];
var noteurl = serverurl + '/' + noteid; export const noteurl = `${serverurl}/${noteid}`;
var version = '0.5.0'; export const version = '0.5.0';
var checkAuth = false; let checkAuth = false;
var profile = null; let profile = null;
var lastLoginState = getLoginState(); let lastLoginState = getLoginState();
var lastUserId = getUserId(); let lastUserId = getUserId();
var loginStateChangeEvent = null; let loginStateChangeEvent = null;
function resetCheckAuth() { export function setloginStateChangeEvent(func) {
loginStateChangeEvent = func;
}
export function resetCheckAuth() {
checkAuth = false; checkAuth = false;
} }
function setLoginState(bool, id) { export function setLoginState(bool, id) {
Cookies.set('loginstate', bool, { Cookies.set('loginstate', bool, {
expires: 365 expires: 365
}); });
@ -40,36 +47,37 @@ function setLoginState(bool, id) {
checkLoginStateChanged(); checkLoginStateChanged();
} }
function checkLoginStateChanged() { export function checkLoginStateChanged() {
if (getLoginState() != lastLoginState || getUserId() != lastUserId) { if (getLoginState() != lastLoginState || getUserId() != lastUserId) {
if(loginStateChangeEvent) if(loginStateChangeEvent) {
loginStateChangeEvent(); loginStateChangeEvent();
}
return true; return true;
} else { } else {
return false; return false;
} }
} }
function getLoginState() { export function getLoginState() {
var state = Cookies.get('loginstate'); const state = Cookies.get('loginstate');
return state === "true" || state === true; return state === "true" || state === true;
} }
function getUserId() { export function getUserId() {
return Cookies.get('userid'); return Cookies.get('userid');
} }
function clearLoginState() { export function clearLoginState() {
Cookies.remove('loginstate'); Cookies.remove('loginstate');
} }
function checkIfAuth(yesCallback, noCallback) { export function checkIfAuth(yesCallback, noCallback) {
var cookieLoginState = getLoginState(); const cookieLoginState = getLoginState();
if (checkLoginStateChanged()) if (checkLoginStateChanged())
checkAuth = false; checkAuth = false;
if (!checkAuth || typeof cookieLoginState == 'undefined') { if (!checkAuth || typeof cookieLoginState == 'undefined') {
$.get(serverurl + '/me') $.get(`${serverurl}/me`)
.done(function (data) { .done(data => {
if (data && data.status == 'ok') { if (data && data.status == 'ok') {
profile = data; profile = data;
yesCallback(profile); yesCallback(profile);
@ -79,10 +87,10 @@ function checkIfAuth(yesCallback, noCallback) {
setLoginState(false); setLoginState(false);
} }
}) })
.fail(function () { .fail(() => {
noCallback(); noCallback();
}) })
.always(function () { .always(() => {
checkAuth = true; checkAuth = true;
}); });
} else if (cookieLoginState) { } else if (cookieLoginState) {
@ -92,29 +100,16 @@ function checkIfAuth(yesCallback, noCallback) {
} }
} }
module.exports = { export default {
domain: domain, domain,
urlpath: urlpath, urlpath,
debug: debug, debug,
GOOGLE_API_KEY: GOOGLE_API_KEY, GOOGLE_API_KEY,
GOOGLE_CLIENT_ID: GOOGLE_CLIENT_ID, GOOGLE_CLIENT_ID,
DROPBOX_APP_KEY: DROPBOX_APP_KEY, DROPBOX_APP_KEY,
port: port, checkAuth,
noteid: noteid, profile,
noteurl: noteurl, lastLoginState,
version: version, lastUserId,
checkAuth: checkAuth, loginStateChangeEvent
profile: profile,
lastLoginState: lastLoginState,
lastUserId: lastUserId,
loginStateChangeEvent: loginStateChangeEvent,
/* export functions */
resetCheckAuth: resetCheckAuth,
setLoginState: setLoginState,
checkLoginStateChanged: checkLoginStateChanged,
getLoginState: getLoginState,
getUserId: getUserId,
clearLoginState: clearLoginState,
checkIfAuth: checkIfAuth
}; };

View file

@ -17,18 +17,18 @@ var _ = require("lodash");
var List = require('list.js'); var List = require('list.js');
var common = require('./common.js'); import {
var urlpath = common.urlpath; checkLoginStateChanged,
var noteid = common.noteid; setloginStateChangeEvent,
var debug = common.debug; debug,
var version = common.version; DROPBOX_APP_KEY,
var GOOGLE_API_KEY = common.GOOGLE_API_KEY; GOOGLE_API_KEY,
var GOOGLE_CLIENT_ID = common.GOOGLE_CLIENT_ID; GOOGLE_CLIENT_ID,
var DROPBOX_APP_KEY = common.DROPBOX_APP_KEY; noteid,
var noteurl = common.noteurl; noteurl,
urlpath,
var checkLoginStateChanged = common.checkLoginStateChanged; version
var loginStateChangeEvent = common.loginStateChangeEvent; } from './common';
var extra = require('./extra'); var extra = require('./extra');
var md = extra.md; var md = extra.md;
@ -963,10 +963,10 @@ function setNeedRefresh() {
showStatus(statusType.offline); showStatus(statusType.offline);
} }
loginStateChangeEvent = function () { setloginStateChangeEvent(function () {
setRefreshModal('user-state-changed'); setRefreshModal('user-state-changed');
setNeedRefresh(); setNeedRefresh();
}; });
//visibility //visibility
var wasFocus = false; var wasFocus = false;