From b2b1be3dda406312add39997ad8bd9494f20b7f8 Mon Sep 17 00:00:00 2001 From: Cheng-Han, Wu Date: Tue, 16 Feb 2016 20:08:44 -0800 Subject: Support set url path and use relative url, move raphael to bower and fixed minor issue in history --- public/js/common.js | 11 +++++++++-- public/js/cover.js | 2 +- public/js/extra.js | 7 ++++--- public/js/history.js | 36 ++++++++++++++++++++---------------- public/js/index.js | 16 ++++++++-------- 5 files changed, 42 insertions(+), 30 deletions(-) (limited to 'public/js') diff --git a/public/js/common.js b/public/js/common.js index e6928e98..e84bc169 100644 --- a/public/js/common.js +++ b/public/js/common.js @@ -1,5 +1,12 @@ //common -var domain = 'change this'; +var domain = 'change this'; // domain name +var urlpath = ''; // sub url path, like: www.example.com/ + +var port = window.location.port; +var serverurl = window.location.protocol + '//' + domain + (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 checkAuth = false; var profile = null; var lastLoginState = getLoginState(); @@ -53,7 +60,7 @@ function checkIfAuth(yesCallback, noCallback) { if (checkLoginStateChanged()) checkAuth = false; if (!checkAuth || typeof cookieLoginState == 'undefined') { - $.get('/me') + $.get(serverurl + '/me') .done(function (data) { if (data && data.status == 'ok') { profile = data; diff --git a/public/js/cover.js b/public/js/cover.js index 04d825cf..9bca3a1e 100644 --- a/public/js/cover.js +++ b/public/js/cover.js @@ -120,7 +120,7 @@ function parseHistoryCallback(list, notehistory) { pin.removeClass('active'); } //parse link to element a - a.attr('href', '/' + values.id); + a.attr('href', serverurl + '/' + values.id); //parse tags if (values.tags) { var tags = values.tags; diff --git a/public/js/extra.js b/public/js/extra.js index cbb30697..676c41cc 100644 --- a/public/js/extra.js +++ b/public/js/extra.js @@ -402,10 +402,11 @@ function exportToHTML(view) { var tocAffix = $('#toc-affix').clone(); tocAffix.find('*').removeClass('active'); //generate html via template - $.get('/css/html.min.css', function (css) { - $.get('/views/html.hbs', function (data) { + $.get(serverurl + '/css/html.min.css', function (css) { + $.get(serverurl + '/views/html.hbs', function (data) { var template = Handlebars.compile(data); var context = { + url: serverurl, title: title, css: css, html: src[0].outerHTML, @@ -657,7 +658,7 @@ emojify.setConfig({ elements: ['script', 'textarea', 'a', 'pre', 'code', 'svg'], classes: ['no-emojify'] }, - img_dir: '/vendor/emojify/images', + img_dir: serverurl + '/vendor/emojify/images', ignore_emoticons: true }); diff --git a/public/js/history.js b/public/js/history.js index b3656d89..edecde1d 100644 --- a/public/js/history.js +++ b/public/js/history.js @@ -4,7 +4,7 @@ migrateHistoryFromTemp(); function migrateHistoryFromTemp() { if (url('#tempid')) { - $.get('/temp', { + $.get(serverurl + '/temp', { tempid: url('#tempid') }) .done(function (data) { @@ -57,7 +57,7 @@ function saveHistoryToCookie(notehistory) { } function saveHistoryToServer(notehistory) { - $.post('/history', { + $.post(serverurl + '/history', { history: JSON.stringify(notehistory) }); } @@ -70,7 +70,7 @@ function saveCookieHistoryToStorage(callback) { function saveStorageHistoryToServer(callback) { var data = store.get('notehistory'); if (data) { - $.post('/history', { + $.post(serverurl + '/history', { history: data }) .done(function (data) { @@ -80,7 +80,7 @@ function saveStorageHistoryToServer(callback) { } function saveCookieHistoryToServer(callback) { - $.post('/history', { + $.post(serverurl + '/history', { history: Cookies.get('notehistory') }) .done(function (data) { @@ -112,13 +112,16 @@ function clearDuplicatedHistory(notehistory) { } function addHistory(id, text, time, tags, pinned, notehistory) { - notehistory.push({ - id: id, - text: text, - time: time, - tags: tags, - pinned: pinned - }); + // only add when note id exists + if (id) { + notehistory.push({ + id: id, + text: text, + time: time, + tags: tags, + pinned: pinned + }); + } return notehistory; } @@ -145,7 +148,7 @@ function writeHistory(view) { } function writeHistoryToServer(view) { - $.get('/history') + $.get(serverurl + '/history') .done(function (data) { try { if (data.history) { @@ -223,8 +226,9 @@ function renderHistory(view) { tags.push(rawtags[i].innerHTML); } //console.debug(tags); + var id = urlpath ? location.pathname.slice(urlpath.length + 1, location.pathname.length).split('/')[1] : location.pathname.split('/')[1]; return { - id: location.pathname.split('/')[1], + id: id, text: title, time: moment().format('MMMM Do YYYY, h:mm:ss a'), tags: tags @@ -260,7 +264,7 @@ function getHistory(callback) { } function getServerHistory(callback) { - $.get('/history') + $.get(serverurl + '/history') .done(function (data) { if (data.history) { callback(data.history); @@ -301,7 +305,7 @@ function parseHistory(list, callback) { } function parseServerToHistory(list, callback) { - $.get('/history') + $.get(serverurl + '/history') .done(function (data) { if (data.history) { parseToHistory(list, data.history, callback); @@ -340,7 +344,7 @@ function parseToHistory(list, notehistory, callback) { notehistory[i].timestamp = moment(notehistory[i].time, 'MMMM Do YYYY, h:mm:ss a').valueOf(); notehistory[i].fromNow = moment(notehistory[i].time, 'MMMM Do YYYY, h:mm:ss a').fromNow(); notehistory[i].time = moment(notehistory[i].time, 'MMMM Do YYYY, h:mm:ss a').format('llll'); - if (list.get('id', notehistory[i].id).length == 0) + if (notehistory[i].id && list.get('id', notehistory[i].id).length == 0) list.add(notehistory[i]); } } diff --git a/public/js/index.js b/public/js/index.js index 6fbdd517..be22e8dc 100644 --- a/public/js/index.js +++ b/public/js/index.js @@ -288,7 +288,7 @@ var statusLength = null; var statusKeymap = null; var statusIndent = null; -$.get('/views/statusbar.html', function (template) { +$.get(serverurl + '/views/statusbar.html', function (template) { statusBarTemplate = template; }); @@ -994,9 +994,8 @@ function closestIndex(arr, closestTo) { } //button actions -var url = window.location.protocol + '//' + window.location.host + window.location.pathname; //share -ui.toolbar.publish.attr("href", url + "/publish"); +ui.toolbar.publish.attr("href", noteurl + "/publish"); //download //markdown ui.toolbar.download.markdown.click(function (e) { @@ -1021,7 +1020,7 @@ ui.toolbar.export.dropbox.click(function () { var options = { files: [ { - 'url': url + "/download", + 'url': noteurl + "/download", 'filename': filename } ], @@ -1032,7 +1031,7 @@ ui.toolbar.export.dropbox.click(function () { Dropbox.save(options); }); //export to gist -ui.toolbar.export.gist.attr("href", url + "/gist"); +ui.toolbar.export.gist.attr("href", noteurl + "/gist"); //import from dropbox ui.toolbar.import.dropbox.click(function () { var options = { @@ -1064,9 +1063,9 @@ ui.toc.dropdown.click(function (e) { }); //beta //pdf -ui.toolbar.beta.pdf.attr("download", "").attr("href", url + "/pdf"); +ui.toolbar.beta.pdf.attr("download", "").attr("href", noteurl + "/pdf"); //slide -ui.toolbar.beta.slide.attr("href", url + "/slide"); +ui.toolbar.beta.slide.attr("href", noteurl + "/slide"); function scrollToTop() { if (currentMode == modeType.both) { @@ -1325,6 +1324,7 @@ function havePermission() { //socket.io actions var socket = io.connect({ + path: urlpath ? '/' + urlpath + '/socket.io/' : '', timeout: 10000 //10 secs to timeout }); //overwrite original event for checking login state @@ -2450,7 +2450,7 @@ $(editor.getInputField()) checkCursorMenu(); }, template: function (value) { - return ' ' + value; + return ' ' + value; }, replace: function (value) { return ':' + value + ':'; -- cgit v1.2.3