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 From c2a8911b9c7872ea0d085020aa17e82162130f3d Mon Sep 17 00:00:00 2001 From: Yukai Huang Date: Fri, 13 Jan 2017 22:46:38 +0800 Subject: Move config variable to lib/config --- public/js/common.js | 25 +------------------------ 1 file changed, 1 insertion(+), 24 deletions(-) (limited to 'public/js/common.js') diff --git a/public/js/common.js b/public/js/common.js index 6d54b450..9a60122b 100644 --- a/public/js/common.js +++ b/public/js/common.js @@ -1,21 +1,4 @@ -// 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 -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'; +import { serverurl } from './lib/config'; let checkAuth = false; let profile = null; @@ -101,12 +84,6 @@ export function checkIfAuth(yesCallback, noCallback) { } export default { - domain, - urlpath, - debug, - GOOGLE_API_KEY, - GOOGLE_CLIENT_ID, - DROPBOX_APP_KEY, checkAuth, profile, lastLoginState, -- cgit v1.2.3 From 0fca629c34b83617b2d72e42aa0edb66fd2e71f6 Mon Sep 17 00:00:00 2001 From: Yukai Huang Date: Fri, 13 Jan 2017 22:51:44 +0800 Subject: Rename common.js to login.js --- public/js/common.js | 92 ----------------------------------------------------- 1 file changed, 92 deletions(-) delete mode 100644 public/js/common.js (limited to 'public/js/common.js') diff --git a/public/js/common.js b/public/js/common.js deleted file mode 100644 index 9a60122b..00000000 --- a/public/js/common.js +++ /dev/null @@ -1,92 +0,0 @@ -import { serverurl } from './lib/config'; - -let checkAuth = false; -let profile = null; -let lastLoginState = getLoginState(); -let lastUserId = getUserId(); -let loginStateChangeEvent = null; - -export function setloginStateChangeEvent(func) { - loginStateChangeEvent = func; -} - -export function resetCheckAuth() { - checkAuth = false; -} - -export function setLoginState(bool, id) { - Cookies.set('loginstate', bool, { - expires: 365 - }); - if (id) { - Cookies.set('userid', id, { - expires: 365 - }); - } else { - Cookies.remove('userid'); - } - lastLoginState = bool; - lastUserId = id; - checkLoginStateChanged(); -} - -export function checkLoginStateChanged() { - if (getLoginState() != lastLoginState || getUserId() != lastUserId) { - if(loginStateChangeEvent) { - loginStateChangeEvent(); - } - return true; - } else { - return false; - } -} - -export function getLoginState() { - const state = Cookies.get('loginstate'); - return state === "true" || state === true; -} - -export function getUserId() { - return Cookies.get('userid'); -} - -export function clearLoginState() { - Cookies.remove('loginstate'); -} - -export function checkIfAuth(yesCallback, noCallback) { - const cookieLoginState = getLoginState(); - if (checkLoginStateChanged()) - checkAuth = false; - if (!checkAuth || typeof cookieLoginState == 'undefined') { - $.get(`${serverurl}/me`) - .done(data => { - if (data && data.status == 'ok') { - profile = data; - yesCallback(profile); - setLoginState(true, data.id); - } else { - noCallback(); - setLoginState(false); - } - }) - .fail(() => { - noCallback(); - }) - .always(() => { - checkAuth = true; - }); - } else if (cookieLoginState) { - yesCallback(profile); - } else { - noCallback(); - } -} - -export default { - checkAuth, - profile, - lastLoginState, - lastUserId, - loginStateChangeEvent -}; -- cgit v1.2.3