From bf4c6d021c353f9f6779ba813dab8d44b17addbf Mon Sep 17 00:00:00 2001 From: Wu Cheng-Han Date: Mon, 10 Oct 2016 16:25:51 +0800 Subject: Extract config.js from common.js to make client setting file clean and also make upgrade easier --- .gitignore | 2 +- README.md | 6 +-- bin/setup | 6 +-- public/js/common.js | 85 ++++++++++++++++++++++++++++++++++++++++ public/js/common.js.example | 95 --------------------------------------------- public/js/config.js.example | 10 +++++ public/views/foot.ejs | 1 + public/views/index.ejs | 1 + public/views/pretty.ejs | 1 + public/views/slide.ejs | 1 + 10 files changed, 106 insertions(+), 102 deletions(-) create mode 100644 public/js/common.js delete mode 100644 public/js/common.js.example create mode 100644 public/js/config.js.example diff --git a/.gitignore b/.gitignore index f6c8d5da..28204428 100644 --- a/.gitignore +++ b/.gitignore @@ -19,5 +19,5 @@ backups/ # ignore config files config.json -public/js/common.js +public/js/config.js .sequelizerc diff --git a/README.md b/README.md index 3443e1d5..ea3ab5ad 100644 --- a/README.md +++ b/README.md @@ -83,10 +83,10 @@ There are some configs you need to change in the files below ``` ./config.json --- for server settings -./public/js/common.js --- for client settings +./public/js/config.js --- for client settings ``` -Client settings `common.js` +Client settings `config.js` --- | variables | example values | description | @@ -148,7 +148,7 @@ Third-party integration api key settings | ------- | --------- | ----------- | | facebook, twitter, github, gitlab, dropbox, google | `config.json` | for signin | | imgur | `config.json` | for image upload | -| google drive, dropbox | `public/js/common.js` | for export and import | +| google drive, dropbox | `public/js/config.js` | for export and import | Third-party integration oauth callback urls --- diff --git a/bin/setup b/bin/setup index 66279e81..8cd20dc7 100755 --- a/bin/setup +++ b/bin/setup @@ -25,8 +25,8 @@ if [ ! -f config.json ]; then cp config.json.example config.json fi -if [ ! -f publis/js/common.js ]; then - cp public/js/common.js.example public/js/common.js +if [ ! -f publis/js/config.js ]; then + cp public/js/config.js.example public/js/config.js fi if [ ! -f .sequelizerc ]; then @@ -43,7 +43,7 @@ Edit the following config file to setup hackmd server and client. Read more info at https://github.com/hackmdio/hackmd#configuration-files * config.json -- server config -* public/js/common.js -- client config +* public/js/config.js -- client config * .sequelizerc -- db config EOF diff --git a/public/js/common.js b/public/js/common.js new file mode 100644 index 00000000..a14e6fc9 --- /dev/null +++ b/public/js/common.js @@ -0,0 +1,85 @@ +//common +var port = window.location.port; +var 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; + +var version = '0.4.4'; + +var checkAuth = false; +var profile = null; +var lastLoginState = getLoginState(); +var lastUserId = getUserId(); +var loginStateChangeEvent = null; + +function resetCheckAuth() { + checkAuth = false; +} + +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(); +} + +function checkLoginStateChanged() { + if (getLoginState() != lastLoginState || getUserId() != lastUserId) { + if(loginStateChangeEvent) + loginStateChangeEvent(); + return true; + } else { + return false; + } +} + +function getLoginState() { + var state = Cookies.get('loginstate'); + return state === "true" || state === true; +} + +function getUserId() { + return Cookies.get('userid'); +} + +function clearLoginState() { + Cookies.remove('loginstate'); +} + +function checkIfAuth(yesCallback, noCallback) { + var cookieLoginState = getLoginState(); + if (checkLoginStateChanged()) + checkAuth = false; + if (!checkAuth || typeof cookieLoginState == 'undefined') { + $.get(serverurl + '/me') + .done(function (data) { + if (data && data.status == 'ok') { + profile = data; + yesCallback(profile); + setLoginState(true, data.id); + } else { + noCallback(); + setLoginState(false); + } + }) + .fail(function () { + noCallback(); + }) + .always(function () { + checkAuth = true; + }); + } else if (cookieLoginState) { + yesCallback(profile); + } else { + noCallback(); + } +} \ No newline at end of file diff --git a/public/js/common.js.example b/public/js/common.js.example deleted file mode 100644 index 11c32acc..00000000 --- a/public/js/common.js.example +++ /dev/null @@ -1,95 +0,0 @@ -//common -var domain = ''; // domain name -var urlpath = ''; // sub url path, like: www.example.com/ -//settings -var debug = false; - -var GOOGLE_API_KEY = ''; -var GOOGLE_CLIENT_ID = ''; - -var DROPBOX_APP_KEY = ''; - -var port = window.location.port; -var 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; - -var version = '0.4.4'; - -var checkAuth = false; -var profile = null; -var lastLoginState = getLoginState(); -var lastUserId = getUserId(); -var loginStateChangeEvent = null; - -function resetCheckAuth() { - checkAuth = false; -} - -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(); -} - -function checkLoginStateChanged() { - if (getLoginState() != lastLoginState || getUserId() != lastUserId) { - if(loginStateChangeEvent) - loginStateChangeEvent(); - return true; - } else { - return false; - } -} - -function getLoginState() { - var state = Cookies.get('loginstate'); - return state === "true" || state === true; -} - -function getUserId() { - return Cookies.get('userid'); -} - -function clearLoginState() { - Cookies.remove('loginstate'); -} - -function checkIfAuth(yesCallback, noCallback) { - var cookieLoginState = getLoginState(); - if (checkLoginStateChanged()) - checkAuth = false; - if (!checkAuth || typeof cookieLoginState == 'undefined') { - $.get(serverurl + '/me') - .done(function (data) { - if (data && data.status == 'ok') { - profile = data; - yesCallback(profile); - setLoginState(true, data.id); - } else { - noCallback(); - setLoginState(false); - } - }) - .fail(function () { - noCallback(); - }) - .always(function () { - checkAuth = true; - }); - } else if (cookieLoginState) { - yesCallback(profile); - } else { - noCallback(); - } -} \ No newline at end of file diff --git a/public/js/config.js.example b/public/js/config.js.example new file mode 100644 index 00000000..197d18f2 --- /dev/null +++ b/public/js/config.js.example @@ -0,0 +1,10 @@ +//config +var domain = ''; // domain name +var urlpath = ''; // sub url path, like: www.example.com/ +//settings +var debug = false; + +var GOOGLE_API_KEY = ''; +var GOOGLE_CLIENT_ID = ''; + +var DROPBOX_APP_KEY = ''; \ No newline at end of file diff --git a/public/views/foot.ejs b/public/views/foot.ejs index 9328ac9c..0397317d 100644 --- a/public/views/foot.ejs +++ b/public/views/foot.ejs @@ -75,6 +75,7 @@ + diff --git a/public/views/index.ejs b/public/views/index.ejs index f5f848e6..61544c86 100644 --- a/public/views/index.ejs +++ b/public/views/index.ejs @@ -205,6 +205,7 @@ + diff --git a/public/views/pretty.ejs b/public/views/pretty.ejs index 2546b1ac..7af474a6 100644 --- a/public/views/pretty.ejs +++ b/public/views/pretty.ejs @@ -127,6 +127,7 @@ + diff --git a/public/views/slide.ejs b/public/views/slide.ejs index 3c5498c6..a0016375 100644 --- a/public/views/slide.ejs +++ b/public/views/slide.ejs @@ -122,6 +122,7 @@ + -- cgit v1.2.3