diff options
author | Yukai Huang | 2017-01-05 16:48:23 +0800 |
---|---|---|
committer | Yukai Huang | 2017-01-05 16:48:23 +0800 |
commit | 6a06c0bb9fea6d499d7fd2f7e6a65e16099cfb05 (patch) | |
tree | ba2d9fa143d517a1bc1d33ce1b197d859eabab71 | |
parent | 45c202172ed293564925d10782ea65d415cb513c (diff) |
Convert common.js to es6
-rw-r--r-- | public/js/common.js | 107 | ||||
-rw-r--r-- | public/js/index.js | 28 |
2 files changed, 65 insertions, 70 deletions
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/<urlpath> -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/<urlpath> + 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 }; diff --git a/public/js/index.js b/public/js/index.js index 46dfffd9..381f051e 100644 --- a/public/js/index.js +++ b/public/js/index.js @@ -17,18 +17,18 @@ var _ = require("lodash"); var List = require('list.js'); -var common = require('./common.js'); -var urlpath = common.urlpath; -var noteid = common.noteid; -var debug = common.debug; -var version = common.version; -var GOOGLE_API_KEY = common.GOOGLE_API_KEY; -var GOOGLE_CLIENT_ID = common.GOOGLE_CLIENT_ID; -var DROPBOX_APP_KEY = common.DROPBOX_APP_KEY; -var noteurl = common.noteurl; - -var checkLoginStateChanged = common.checkLoginStateChanged; -var loginStateChangeEvent = common.loginStateChangeEvent; +import { + checkLoginStateChanged, + setloginStateChangeEvent, + debug, + DROPBOX_APP_KEY, + GOOGLE_API_KEY, + GOOGLE_CLIENT_ID, + noteid, + noteurl, + urlpath, + version +} from './common'; var extra = require('./extra'); var md = extra.md; @@ -963,10 +963,10 @@ function setNeedRefresh() { showStatus(statusType.offline); } -loginStateChangeEvent = function () { +setloginStateChangeEvent(function () { setRefreshModal('user-state-changed'); setNeedRefresh(); -}; +}); //visibility var wasFocus = false; |