Convert history.js to es6

This commit is contained in:
Yukai Huang 2017-01-05 20:56:16 +08:00
parent 71aece7429
commit fce08cc164
3 changed files with 112 additions and 132 deletions

View file

@ -11,20 +11,21 @@ import {
setloginStateChangeEvent setloginStateChangeEvent
} from './common'; } from './common';
import historyModule from './history'; import {
const parseStorageToHistory = historyModule.parseStorageToHistory; clearDuplicatedHistory,
const parseHistory = historyModule.parseHistory; deleteServerHistory,
const getStorageHistory = historyModule.getStorageHistory; getHistory,
const getHistory = historyModule.getHistory; getStorageHistory,
const saveHistory = historyModule.saveHistory; parseHistory,
const removeHistory = historyModule.removeHistory; parseServerToHistory,
const postHistoryToServer = historyModule.postHistoryToServer; parseStorageToHistory,
const deleteServerHistory = historyModule.deleteServerHistory; postHistoryToServer,
const parseServerToHistory = historyModule.parseServerToHistory; removeHistory,
const saveStorageHistoryToServer = historyModule.saveStorageHistoryToServer; saveHistory,
const clearDuplicatedHistory = historyModule.clearDuplicatedHistory; saveStorageHistoryToServer
} from './history';
import {saveAs} from 'file-saver'; import { saveAs } from 'file-saver';
import List from 'list.js'; import List from 'list.js';
import S from 'string'; import S from 'string';

View file

@ -1,10 +1,9 @@
var store = require('store'); import store from 'store';
var S = require('string'); import S from 'string';
import {
var common = require('./common'); checkIfAuth,
var checkIfAuth = common.checkIfAuth; urlpath
var urlpath = common.urlpath; } from './common';
var getLoginState = common.getLoginState;
window.migrateHistoryFromTempCallback = null; window.migrateHistoryFromTempCallback = null;
@ -12,22 +11,22 @@ migrateHistoryFromTemp();
function migrateHistoryFromTemp() { function migrateHistoryFromTemp() {
if (url('#tempid')) { if (url('#tempid')) {
$.get(serverurl + '/temp', { $.get(`${serverurl}/temp`, {
tempid: url('#tempid') tempid: url('#tempid')
}) })
.done(function (data) { .done(data => {
if (data && data.temp) { if (data && data.temp) {
getStorageHistory(function (olddata) { getStorageHistory(olddata => {
if (!olddata || olddata.length == 0) { if (!olddata || olddata.length == 0) {
saveHistoryToStorage(JSON.parse(data.temp)); saveHistoryToStorage(JSON.parse(data.temp));
} }
}); });
} }
}) })
.always(function () { .always(() => {
var hash = location.hash.split('#')[1]; let hash = location.hash.split('#')[1];
hash = hash.split('&'); hash = hash.split('&');
for (var i = 0; i < hash.length; i++) for (let i = 0; i < hash.length; i++)
if (hash[i].indexOf('tempid') == 0) { if (hash[i].indexOf('tempid') == 0) {
hash.splice(i, 1); hash.splice(i, 1);
i--; i--;
@ -40,12 +39,12 @@ function migrateHistoryFromTemp() {
} }
} }
function saveHistory(notehistory) { export function saveHistory(notehistory) {
checkIfAuth( checkIfAuth(
function () { () => {
saveHistoryToServer(notehistory); saveHistoryToServer(notehistory);
}, },
function () { () => {
saveHistoryToStorage(notehistory); saveHistoryToStorage(notehistory);
} }
); );
@ -65,7 +64,7 @@ function saveHistoryToCookie(notehistory) {
} }
function saveHistoryToServer(notehistory) { function saveHistoryToServer(notehistory) {
$.post(serverurl + '/history', { $.post(`${serverurl}/history`, {
history: JSON.stringify(notehistory) history: JSON.stringify(notehistory)
}); });
} }
@ -75,37 +74,37 @@ function saveCookieHistoryToStorage(callback) {
callback(); callback();
} }
function saveStorageHistoryToServer(callback) { export function saveStorageHistoryToServer(callback) {
var data = store.get('notehistory'); const data = store.get('notehistory');
if (data) { if (data) {
$.post(serverurl + '/history', { $.post(`${serverurl}/history`, {
history: data history: data
}) })
.done(function (data) { .done(data => {
callback(data); callback(data);
}); });
} }
} }
function saveCookieHistoryToServer(callback) { function saveCookieHistoryToServer(callback) {
$.post(serverurl + '/history', { $.post(`${serverurl}/history`, {
history: Cookies.get('notehistory') history: Cookies.get('notehistory')
}) })
.done(function (data) { .done(data => {
callback(data); callback(data);
}); });
} }
function clearDuplicatedHistory(notehistory) { export function clearDuplicatedHistory(notehistory) {
var newnotehistory = []; const newnotehistory = [];
for (var i = 0; i < notehistory.length; i++) { for (let i = 0; i < notehistory.length; i++) {
var found = false; let found = false;
for (var j = 0; j < newnotehistory.length; j++) { for (let j = 0; j < newnotehistory.length; j++) {
var id = notehistory[i].id.replace(/\=+$/, ''); const id = notehistory[i].id.replace(/\=+$/, '');
var newId = newnotehistory[j].id.replace(/\=+$/, ''); const newId = newnotehistory[j].id.replace(/\=+$/, '');
if (id == newId || notehistory[i].id == newnotehistory[j].id || !notehistory[i].id || !newnotehistory[j].id) { if (id == newId || notehistory[i].id == newnotehistory[j].id || !notehistory[i].id || !newnotehistory[j].id) {
var time = (typeof notehistory[i].time === 'number' ? moment(notehistory[i].time) : moment(notehistory[i].time, 'MMMM Do YYYY, h:mm:ss a')); const time = (typeof notehistory[i].time === 'number' ? moment(notehistory[i].time) : moment(notehistory[i].time, 'MMMM Do YYYY, h:mm:ss a'));
var newTime = (typeof newnotehistory[i].time === 'number' ? moment(newnotehistory[i].time) : moment(newnotehistory[i].time, 'MMMM Do YYYY, h:mm:ss a')); const newTime = (typeof newnotehistory[i].time === 'number' ? moment(newnotehistory[i].time) : moment(newnotehistory[i].time, 'MMMM Do YYYY, h:mm:ss a'));
if(time >= newTime) { if(time >= newTime) {
newnotehistory[j] = notehistory[i]; newnotehistory[j] = notehistory[i];
} }
@ -123,42 +122,42 @@ function addHistory(id, text, time, tags, pinned, notehistory) {
// only add when note id exists // only add when note id exists
if (id) { if (id) {
notehistory.push({ notehistory.push({
id: id, id,
text: text, text,
time: time, time,
tags: tags, tags,
pinned: pinned pinned
}); });
} }
return notehistory; return notehistory;
} }
function removeHistory(id, notehistory) { export function removeHistory(id, notehistory) {
for (var i = 0; i < notehistory.length; i++) { for (let i = 0; i < notehistory.length; i++) {
if (notehistory[i].id == id) { if (notehistory[i].id == id) {
notehistory.splice(i, 1); notehistory.splice(i, 1);
i--; i -= 1;
} }
} }
return notehistory; return notehistory;
} }
//used for inner //used for inner
function writeHistory(title, tags) { export function writeHistory(title, tags) {
checkIfAuth( checkIfAuth(
function () { () => {
// no need to do this anymore, this will count from server-side // no need to do this anymore, this will count from server-side
// writeHistoryToServer(title, tags); // writeHistoryToServer(title, tags);
}, },
function () { () => {
writeHistoryToStorage(title, tags); writeHistoryToStorage(title, tags);
} }
); );
} }
function writeHistoryToServer(title, tags) { function writeHistoryToServer(title, tags) {
$.get(serverurl + '/history') $.get(`${serverurl}/history`)
.done(function (data) { .done(data => {
try { try {
if (data.history) { if (data.history) {
var notehistory = data.history; var notehistory = data.history;
@ -171,10 +170,10 @@ function writeHistoryToServer(title, tags) {
if (!notehistory) if (!notehistory)
notehistory = []; notehistory = [];
var newnotehistory = generateHistory(title, tags, notehistory); const newnotehistory = generateHistory(title, tags, notehistory);
saveHistoryToServer(newnotehistory); saveHistoryToServer(newnotehistory);
}) })
.fail(function (xhr, status, error) { .fail((xhr, status, error) => {
console.error(xhr.responseText); console.error(xhr.responseText);
}); });
} }
@ -188,13 +187,13 @@ function writeHistoryToCookie(title, tags) {
if (!notehistory) if (!notehistory)
notehistory = []; notehistory = [];
var newnotehistory = generateHistory(title, tags, notehistory); const newnotehistory = generateHistory(title, tags, notehistory);
saveHistoryToCookie(newnotehistory); saveHistoryToCookie(newnotehistory);
} }
function writeHistoryToStorage(title, tags) { function writeHistoryToStorage(title, tags) {
if (store.enabled) { if (store.enabled) {
var data = store.get('notehistory'); let data = store.get('notehistory');
if (data) { if (data) {
if (typeof data == "string") if (typeof data == "string")
data = JSON.parse(data); data = JSON.parse(data);
@ -204,7 +203,7 @@ function writeHistoryToStorage(title, tags) {
if (!notehistory) if (!notehistory)
notehistory = []; notehistory = [];
var newnotehistory = generateHistory(title, tags, notehistory); const newnotehistory = generateHistory(title, tags, notehistory);
saveHistoryToStorage(newnotehistory); saveHistoryToStorage(newnotehistory);
} else { } else {
writeHistoryToCookie(title, tags); writeHistoryToCookie(title, tags);
@ -212,27 +211,25 @@ function writeHistoryToStorage(title, tags) {
} }
if (!Array.isArray) { if (!Array.isArray) {
Array.isArray = function(arg) { Array.isArray = arg => Object.prototype.toString.call(arg) === '[object Array]';
return Object.prototype.toString.call(arg) === '[object Array]';
};
} }
function renderHistory(title, tags) { function renderHistory(title, tags) {
//console.debug(tags); //console.debug(tags);
var id = urlpath ? location.pathname.slice(urlpath.length + 1, location.pathname.length).split('/')[1] : location.pathname.split('/')[1]; const id = urlpath ? location.pathname.slice(urlpath.length + 1, location.pathname.length).split('/')[1] : location.pathname.split('/')[1];
return { return {
id: id, id,
text: title, text: title,
time: moment().valueOf(), time: moment().valueOf(),
tags: tags tags
}; };
} }
function generateHistory(title, tags, notehistory) { function generateHistory(title, tags, notehistory) {
var info = renderHistory(title, tags); const info = renderHistory(title, tags);
//keep any pinned data //keep any pinned data
var pinned = false; let pinned = false;
for (var i = 0; i < notehistory.length; i++) { for (let i = 0; i < notehistory.length; i++) {
if (notehistory[i].id == info.id && notehistory[i].pinned) { if (notehistory[i].id == info.id && notehistory[i].pinned) {
pinned = true; pinned = true;
break; break;
@ -245,25 +242,25 @@ function generateHistory(title, tags, notehistory) {
} }
//used for outer //used for outer
function getHistory(callback) { export function getHistory(callback) {
checkIfAuth( checkIfAuth(
function () { () => {
getServerHistory(callback); getServerHistory(callback);
}, },
function () { () => {
getStorageHistory(callback); getStorageHistory(callback);
} }
); );
} }
function getServerHistory(callback) { function getServerHistory(callback) {
$.get(serverurl + '/history') $.get(`${serverurl}/history`)
.done(function (data) { .done(data => {
if (data.history) { if (data.history) {
callback(data.history); callback(data.history);
} }
}) })
.fail(function (xhr, status, error) { .fail((xhr, status, error) => {
console.error(xhr.responseText); console.error(xhr.responseText);
}); });
} }
@ -272,9 +269,9 @@ function getCookieHistory(callback) {
callback(Cookies.getJSON('notehistory')); callback(Cookies.getJSON('notehistory'));
} }
function getStorageHistory(callback) { export function getStorageHistory(callback) {
if (store.enabled) { if (store.enabled) {
var data = store.get('notehistory'); let data = store.get('notehistory');
if (data) { if (data) {
if (typeof data == "string") if (typeof data == "string")
data = JSON.parse(data); data = JSON.parse(data);
@ -286,37 +283,37 @@ function getStorageHistory(callback) {
} }
} }
function parseHistory(list, callback) { export function parseHistory(list, callback) {
checkIfAuth( checkIfAuth(
function () { () => {
parseServerToHistory(list, callback); parseServerToHistory(list, callback);
}, },
function () { () => {
parseStorageToHistory(list, callback); parseStorageToHistory(list, callback);
} }
); );
} }
function parseServerToHistory(list, callback) { export function parseServerToHistory(list, callback) {
$.get(serverurl + '/history') $.get(`${serverurl}/history`)
.done(function (data) { .done(data => {
if (data.history) { if (data.history) {
parseToHistory(list, data.history, callback); parseToHistory(list, data.history, callback);
} }
}) })
.fail(function (xhr, status, error) { .fail((xhr, status, error) => {
console.error(xhr.responseText); console.error(xhr.responseText);
}); });
} }
function parseCookieToHistory(list, callback) { function parseCookieToHistory(list, callback) {
var notehistory = Cookies.getJSON('notehistory'); const notehistory = Cookies.getJSON('notehistory');
parseToHistory(list, notehistory, callback); parseToHistory(list, notehistory, callback);
} }
function parseStorageToHistory(list, callback) { export function parseStorageToHistory(list, callback) {
if (store.enabled) { if (store.enabled) {
var data = store.get('notehistory'); let data = store.get('notehistory');
if (data) { if (data) {
if (typeof data == "string") if (typeof data == "string")
data = JSON.parse(data); data = JSON.parse(data);
@ -332,9 +329,9 @@ function parseToHistory(list, notehistory, callback) {
if (!callback) return; if (!callback) return;
else if (!list || !notehistory) callback(list, notehistory); else if (!list || !notehistory) callback(list, notehistory);
else if (notehistory && notehistory.length > 0) { else if (notehistory && notehistory.length > 0) {
for (var i = 0; i < notehistory.length; i++) { for (let i = 0; i < notehistory.length; i++) {
//parse time to timestamp and fromNow //parse time to timestamp and fromNow
var timestamp = (typeof notehistory[i].time === 'number' ? moment(notehistory[i].time) : moment(notehistory[i].time, 'MMMM Do YYYY, h:mm:ss a')); const timestamp = (typeof notehistory[i].time === 'number' ? moment(notehistory[i].time) : moment(notehistory[i].time, 'MMMM Do YYYY, h:mm:ss a'));
notehistory[i].timestamp = timestamp.valueOf(); notehistory[i].timestamp = timestamp.valueOf();
notehistory[i].fromNow = timestamp.fromNow(); notehistory[i].fromNow = timestamp.fromNow();
notehistory[i].time = timestamp.format('llll'); notehistory[i].time = timestamp.format('llll');
@ -349,42 +346,23 @@ function parseToHistory(list, notehistory, callback) {
callback(list, notehistory); callback(list, notehistory);
} }
function postHistoryToServer(noteId, data, callback) { export function postHistoryToServer(noteId, data, callback) {
$.post(serverurl + '/history/' + noteId, data) $.post(`${serverurl}/history/${noteId}`, data)
.done(function (result) { .done(result => callback(null, result))
return callback(null, result); .fail((xhr, status, error) => {
})
.fail(function (xhr, status, error) {
console.error(xhr.responseText); console.error(xhr.responseText);
return callback(error, null); return callback(error, null);
}); });
} }
function deleteServerHistory(noteId, callback) { export function deleteServerHistory(noteId, callback) {
$.ajax({ $.ajax({
url: serverurl + '/history' + (noteId ? '/' + noteId : ""), url: `${serverurl}/history${noteId ? '/' + noteId : ""}`,
type: 'DELETE' type: 'DELETE'
}) })
.done(function (result) { .done(result => callback(null, result))
return callback(null, result); .fail((xhr, status, error) => {
})
.fail(function (xhr, status, error) {
console.error(xhr.responseText); console.error(xhr.responseText);
return callback(error, null); return callback(error, null);
}); });
} }
module.exports = {
writeHistory: writeHistory,
parseHistory: parseHistory,
getStorageHistory: getStorageHistory,
getHistory: getHistory,
saveHistory: saveHistory,
removeHistory: removeHistory,
parseStorageToHistory: parseStorageToHistory,
postHistoryToServer: postHistoryToServer,
deleteServerHistory: deleteServerHistory,
parseServerToHistory: parseServerToHistory,
saveStorageHistoryToServer: saveStorageHistoryToServer,
clearDuplicatedHistory: clearDuplicatedHistory
}

View file

@ -59,12 +59,13 @@ import {
syncScrollToView syncScrollToView
} from './syncscroll'; } from './syncscroll';
var historyModule = require('./history'); import {
var writeHistory = historyModule.writeHistory; writeHistory,
var deleteServerHistory = historyModule.deleteServerHistory; deleteServerHistory,
var getHistory = historyModule.getHistory; getHistory,
var saveHistory = historyModule.saveHistory; saveHistory,
var removeHistory = historyModule.removeHistory; removeHistory
} from './history';
var renderer = require('./render'); var renderer = require('./render');
var preventXSS = renderer.preventXSS; var preventXSS = renderer.preventXSS;