From 6a06c0bb9fea6d499d7fd2f7e6a65e16099cfb05 Mon Sep 17 00:00:00 2001 From: Yukai Huang Date: Thu, 5 Jan 2017 16:48:23 +0800 Subject: Convert common.js to es6 --- public/js/common.js | 107 +++++++++++++++++++++++++--------------------------- 1 file changed, 51 insertions(+), 56 deletions(-) (limited to 'public/js/common.js') diff --git a/public/js/common.js b/public/js/common.js index f5bfc8ec..6d54b450 100644 --- a/public/js/common.js +++ b/public/js/common.js @@ -1,30 +1,37 @@ -var config = require('./config'); -var domain = config.domain; // domain name -var urlpath = config.urlpath; // sub url path, like: www.example.com/ -var debug = config.debug; -var GOOGLE_API_KEY = config.GOOGLE_API_KEY; -var GOOGLE_CLIENT_ID = config.GOOGLE_CLIENT_ID; -var DROPBOX_APP_KEY = config.DROPBOX_APP_KEY; +// import config from './config'; + +import { + domain, // domain name + urlpath, // sub url path, like: www.example.com/ + debug, + GOOGLE_API_KEY, + GOOGLE_CLIENT_ID, + DROPBOX_APP_KEY +} from './config'; //common -var port = window.location.port; -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]; -var noteurl = serverurl + '/' + noteid; +export const port = window.location.port; +window.serverurl = `${window.location.protocol}//${domain ? domain : window.location.hostname}${port ? ':' + port : ''}${urlpath ? '/' + urlpath : ''}`; +export const noteid = urlpath ? window.location.pathname.slice(urlpath.length + 1, window.location.pathname.length).split('/')[1] : window.location.pathname.split('/')[1]; +export const noteurl = `${serverurl}/${noteid}`; + +export const version = '0.5.0'; -var version = '0.5.0'; +let checkAuth = false; +let profile = null; +let lastLoginState = getLoginState(); +let lastUserId = getUserId(); +let loginStateChangeEvent = null; -var checkAuth = false; -var profile = null; -var lastLoginState = getLoginState(); -var lastUserId = getUserId(); -var loginStateChangeEvent = null; +export function setloginStateChangeEvent(func) { + loginStateChangeEvent = func; +} -function resetCheckAuth() { +export function resetCheckAuth() { checkAuth = false; } -function setLoginState(bool, id) { +export function setLoginState(bool, id) { Cookies.set('loginstate', bool, { expires: 365 }); @@ -40,36 +47,37 @@ function setLoginState(bool, id) { checkLoginStateChanged(); } -function checkLoginStateChanged() { +export function checkLoginStateChanged() { if (getLoginState() != lastLoginState || getUserId() != lastUserId) { - if(loginStateChangeEvent) + if(loginStateChangeEvent) { loginStateChangeEvent(); + } return true; } else { return false; } } -function getLoginState() { - var state = Cookies.get('loginstate'); +export function getLoginState() { + const state = Cookies.get('loginstate'); return state === "true" || state === true; } -function getUserId() { +export function getUserId() { return Cookies.get('userid'); } -function clearLoginState() { +export function clearLoginState() { Cookies.remove('loginstate'); } -function checkIfAuth(yesCallback, noCallback) { - var cookieLoginState = getLoginState(); +export function checkIfAuth(yesCallback, noCallback) { + const cookieLoginState = getLoginState(); if (checkLoginStateChanged()) checkAuth = false; if (!checkAuth || typeof cookieLoginState == 'undefined') { - $.get(serverurl + '/me') - .done(function (data) { + $.get(`${serverurl}/me`) + .done(data => { if (data && data.status == 'ok') { profile = data; yesCallback(profile); @@ -79,10 +87,10 @@ function checkIfAuth(yesCallback, noCallback) { setLoginState(false); } }) - .fail(function () { + .fail(() => { noCallback(); }) - .always(function () { + .always(() => { checkAuth = true; }); } else if (cookieLoginState) { @@ -92,29 +100,16 @@ function checkIfAuth(yesCallback, noCallback) { } } -module.exports = { - domain: domain, - urlpath: urlpath, - debug: debug, - GOOGLE_API_KEY: GOOGLE_API_KEY, - GOOGLE_CLIENT_ID: GOOGLE_CLIENT_ID, - DROPBOX_APP_KEY: DROPBOX_APP_KEY, - port: port, - noteid: noteid, - noteurl: noteurl, - version: version, - checkAuth: checkAuth, - profile: profile, - lastLoginState: lastLoginState, - lastUserId: lastUserId, - loginStateChangeEvent: loginStateChangeEvent, - - /* export functions */ - resetCheckAuth: resetCheckAuth, - setLoginState: setLoginState, - checkLoginStateChanged: checkLoginStateChanged, - getLoginState: getLoginState, - getUserId: getUserId, - clearLoginState: clearLoginState, - checkIfAuth: checkIfAuth +export default { + domain, + urlpath, + debug, + GOOGLE_API_KEY, + GOOGLE_CLIENT_ID, + DROPBOX_APP_KEY, + checkAuth, + profile, + lastLoginState, + lastUserId, + loginStateChangeEvent }; -- cgit v1.2.3