codimd/public/js/history.js

373 lines
10 KiB
JavaScript
Raw Normal View History

2017-01-05 12:56:16 +00:00
import store from 'store';
import S from 'string';
2017-01-13 14:51:44 +00:00
import {
checkIfAuth
} from './lib/common/login';
2017-01-05 12:56:16 +00:00
import {
urlpath
2017-01-13 14:51:44 +00:00
} from './lib/config';
2016-10-13 08:25:38 +00:00
window.migrateHistoryFromTempCallback = null;
2015-05-15 04:58:13 +00:00
migrateHistoryFromTemp();
function migrateHistoryFromTemp() {
if (url('#tempid')) {
2017-01-05 12:56:16 +00:00
$.get(`${serverurl}/temp`, {
2015-05-15 04:58:13 +00:00
tempid: url('#tempid')
})
2017-01-05 12:56:16 +00:00
.done(data => {
2015-05-15 04:58:13 +00:00
if (data && data.temp) {
2017-01-05 12:56:16 +00:00
getStorageHistory(olddata => {
2015-05-15 04:58:13 +00:00
if (!olddata || olddata.length == 0) {
saveHistoryToStorage(JSON.parse(data.temp));
}
});
}
})
2017-01-05 12:56:16 +00:00
.always(() => {
let hash = location.hash.split('#')[1];
2015-05-15 04:58:13 +00:00
hash = hash.split('&');
2017-01-05 12:56:16 +00:00
for (let i = 0; i < hash.length; i++)
2015-05-15 04:58:13 +00:00
if (hash[i].indexOf('tempid') == 0) {
hash.splice(i, 1);
i--;
}
hash = hash.join('&');
location.hash = hash;
if (migrateHistoryFromTempCallback)
migrateHistoryFromTempCallback();
});
}
2015-05-04 07:53:29 +00:00
}
2017-01-05 12:56:16 +00:00
export function saveHistory(notehistory) {
2015-05-04 07:53:29 +00:00
checkIfAuth(
2017-01-05 12:56:16 +00:00
() => {
2015-05-04 07:53:29 +00:00
saveHistoryToServer(notehistory);
},
2017-01-05 12:56:16 +00:00
() => {
2015-05-15 04:58:13 +00:00
saveHistoryToStorage(notehistory);
2015-05-04 07:53:29 +00:00
}
);
}
2015-05-15 04:58:13 +00:00
function saveHistoryToStorage(notehistory) {
if (store.enabled)
store.set('notehistory', JSON.stringify(notehistory));
else
saveHistoryToCookie(notehistory);
2015-05-15 04:58:13 +00:00
}
2015-05-04 07:53:29 +00:00
function saveHistoryToCookie(notehistory) {
2015-05-15 04:58:13 +00:00
Cookies.set('notehistory', notehistory, {
2015-05-04 07:53:29 +00:00
expires: 365
});
}
function saveHistoryToServer(notehistory) {
2017-01-05 12:56:16 +00:00
$.post(`${serverurl}/history`, {
2015-05-04 07:53:29 +00:00
history: JSON.stringify(notehistory)
});
}
2015-05-15 04:58:13 +00:00
function saveCookieHistoryToStorage(callback) {
store.set('notehistory', Cookies.get('notehistory'));
callback();
}
2017-01-05 12:56:16 +00:00
export function saveStorageHistoryToServer(callback) {
const data = store.get('notehistory');
2015-05-15 04:58:13 +00:00
if (data) {
2017-01-05 12:56:16 +00:00
$.post(`${serverurl}/history`, {
2015-05-15 04:58:13 +00:00
history: data
})
2017-01-05 12:56:16 +00:00
.done(data => {
2015-05-15 04:58:13 +00:00
callback(data);
});
}
}
2015-05-04 07:53:29 +00:00
function saveCookieHistoryToServer(callback) {
2017-01-05 12:56:16 +00:00
$.post(`${serverurl}/history`, {
2015-05-15 04:58:13 +00:00
history: Cookies.get('notehistory')
2015-05-04 07:53:29 +00:00
})
2017-01-05 12:56:16 +00:00
.done(data => {
2015-05-15 04:58:13 +00:00
callback(data);
2015-05-04 07:53:29 +00:00
});
}
2017-01-05 12:56:16 +00:00
export function clearDuplicatedHistory(notehistory) {
const newnotehistory = [];
for (let i = 0; i < notehistory.length; i++) {
let found = false;
for (let j = 0; j < newnotehistory.length; j++) {
const id = notehistory[i].id.replace(/\=+$/, '');
const newId = newnotehistory[j].id.replace(/\=+$/, '');
if (id == newId || notehistory[i].id == newnotehistory[j].id || !notehistory[i].id || !newnotehistory[j].id) {
2017-01-05 12:56:16 +00:00
const time = (typeof notehistory[i].time === 'number' ? moment(notehistory[i].time) : moment(notehistory[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) {
newnotehistory[j] = notehistory[i];
}
2015-05-04 07:53:29 +00:00
found = true;
break;
}
}
if (!found)
newnotehistory.push(notehistory[i]);
}
2015-05-15 04:58:13 +00:00
return newnotehistory;
2015-05-04 07:53:29 +00:00
}
2015-10-22 09:09:55 +00:00
function addHistory(id, text, time, tags, pinned, notehistory) {
// only add when note id exists
if (id) {
notehistory.push({
2017-01-05 12:56:16 +00:00
id,
text,
time,
tags,
pinned
});
}
2015-05-04 07:53:29 +00:00
return notehistory;
}
2017-01-05 12:56:16 +00:00
export function removeHistory(id, notehistory) {
for (let i = 0; i < notehistory.length; i++) {
if (notehistory[i].id == id) {
2015-05-04 07:53:29 +00:00
notehistory.splice(i, 1);
2017-01-05 12:56:16 +00:00
i -= 1;
}
2015-05-04 07:53:29 +00:00
}
return notehistory;
}
//used for inner
2017-01-05 12:56:16 +00:00
export function writeHistory(title, tags) {
2015-05-04 07:53:29 +00:00
checkIfAuth(
2017-01-05 12:56:16 +00:00
() => {
// no need to do this anymore, this will count from server-side
// writeHistoryToServer(title, tags);
2015-05-04 07:53:29 +00:00
},
2017-01-05 12:56:16 +00:00
() => {
writeHistoryToStorage(title, tags);
2015-05-04 07:53:29 +00:00
}
);
}
function writeHistoryToServer(title, tags) {
2017-01-05 12:56:16 +00:00
$.get(`${serverurl}/history`)
.done(data => {
2015-05-04 07:53:29 +00:00
try {
if (data.history) {
var notehistory = data.history;
} else {
var notehistory = [];
}
} catch (err) {
var notehistory = [];
}
if (!notehistory)
2015-06-01 10:04:25 +00:00
notehistory = [];
2017-01-05 12:56:16 +00:00
const newnotehistory = generateHistory(title, tags, notehistory);
2015-05-04 07:53:29 +00:00
saveHistoryToServer(newnotehistory);
})
2017-01-05 12:56:16 +00:00
.fail((xhr, status, error) => {
console.error(xhr.responseText);
2015-05-04 07:53:29 +00:00
});
}
function writeHistoryToCookie(title, tags) {
2015-05-04 07:53:29 +00:00
try {
2015-05-15 04:58:13 +00:00
var notehistory = Cookies.getJSON('notehistory');
2015-05-04 07:53:29 +00:00
} catch (err) {
var notehistory = [];
}
if (!notehistory)
2015-06-01 10:04:25 +00:00
notehistory = [];
2017-01-05 12:56:16 +00:00
const newnotehistory = generateHistory(title, tags, notehistory);
2015-05-04 07:53:29 +00:00
saveHistoryToCookie(newnotehistory);
}
function writeHistoryToStorage(title, tags) {
2015-05-15 04:58:13 +00:00
if (store.enabled) {
2017-01-05 12:56:16 +00:00
let data = store.get('notehistory');
2015-05-15 04:58:13 +00:00
if (data) {
if (typeof data == "string")
data = JSON.parse(data);
var notehistory = data;
} else
var notehistory = [];
if (!notehistory)
2015-06-01 10:04:25 +00:00
notehistory = [];
2017-01-05 12:56:16 +00:00
const newnotehistory = generateHistory(title, tags, notehistory);
2015-05-15 04:58:13 +00:00
saveHistoryToStorage(newnotehistory);
} else {
writeHistoryToCookie(title, tags);
2015-05-15 04:58:13 +00:00
}
}
if (!Array.isArray) {
2017-01-05 12:56:16 +00:00
Array.isArray = arg => Object.prototype.toString.call(arg) === '[object Array]';
}
function renderHistory(title, tags) {
2015-05-04 07:53:29 +00:00
//console.debug(tags);
2017-01-05 12:56:16 +00:00
const id = urlpath ? location.pathname.slice(urlpath.length + 1, location.pathname.length).split('/')[1] : location.pathname.split('/')[1];
2015-05-04 07:53:29 +00:00
return {
2017-01-05 12:56:16 +00:00
id,
2015-05-04 07:53:29 +00:00
text: title,
time: moment().valueOf(),
2017-01-05 12:56:16 +00:00
tags
2015-05-04 07:53:29 +00:00
};
}
function generateHistory(title, tags, notehistory) {
2017-01-05 12:56:16 +00:00
const info = renderHistory(title, tags);
//keep any pinned data
let pinned = false;
for (let i = 0; i < notehistory.length; i++) {
if (notehistory[i].id == info.id && notehistory[i].pinned) {
pinned = true;
break;
}
}
2015-05-04 07:53:29 +00:00
notehistory = removeHistory(info.id, notehistory);
2015-10-22 09:09:55 +00:00
notehistory = addHistory(info.id, info.text, info.time, info.tags, pinned, notehistory);
notehistory = clearDuplicatedHistory(notehistory);
2015-05-04 07:53:29 +00:00
return notehistory;
}
//used for outer
2017-01-05 12:56:16 +00:00
export function getHistory(callback) {
2015-05-04 07:53:29 +00:00
checkIfAuth(
2017-01-05 12:56:16 +00:00
() => {
2015-05-04 07:53:29 +00:00
getServerHistory(callback);
},
2017-01-05 12:56:16 +00:00
() => {
2015-05-15 04:58:13 +00:00
getStorageHistory(callback);
2015-05-04 07:53:29 +00:00
}
);
}
function getServerHistory(callback) {
2017-01-05 12:56:16 +00:00
$.get(`${serverurl}/history`)
.done(data => {
2015-05-04 07:53:29 +00:00
if (data.history) {
callback(data.history);
}
})
2017-01-05 12:56:16 +00:00
.fail((xhr, status, error) => {
console.error(xhr.responseText);
2015-05-04 07:53:29 +00:00
});
}
function getCookieHistory(callback) {
2015-05-15 04:58:13 +00:00
callback(Cookies.getJSON('notehistory'));
}
2017-01-05 12:56:16 +00:00
export function getStorageHistory(callback) {
2015-05-15 04:58:13 +00:00
if (store.enabled) {
2017-01-05 12:56:16 +00:00
let data = store.get('notehistory');
2015-05-15 04:58:13 +00:00
if (data) {
if (typeof data == "string")
data = JSON.parse(data);
callback(data);
} else
getCookieHistory(callback);
} else {
getCookieHistory(callback);
}
2015-05-04 07:53:29 +00:00
}
2017-01-05 12:56:16 +00:00
export function parseHistory(list, callback) {
2015-05-04 07:53:29 +00:00
checkIfAuth(
2017-01-05 12:56:16 +00:00
() => {
2015-05-15 04:58:13 +00:00
parseServerToHistory(list, callback);
2015-05-04 07:53:29 +00:00
},
2017-01-05 12:56:16 +00:00
() => {
2015-05-15 04:58:13 +00:00
parseStorageToHistory(list, callback);
2015-05-04 07:53:29 +00:00
}
);
}
2017-01-05 12:56:16 +00:00
export function parseServerToHistory(list, callback) {
$.get(`${serverurl}/history`)
.done(data => {
2015-05-04 07:53:29 +00:00
if (data.history) {
2015-05-15 04:58:13 +00:00
parseToHistory(list, data.history, callback);
2015-05-04 07:53:29 +00:00
}
})
2017-01-05 12:56:16 +00:00
.fail((xhr, status, error) => {
console.error(xhr.responseText);
2015-05-04 07:53:29 +00:00
});
}
2015-05-15 04:58:13 +00:00
function parseCookieToHistory(list, callback) {
2017-01-05 12:56:16 +00:00
const notehistory = Cookies.getJSON('notehistory');
2015-05-15 04:58:13 +00:00
parseToHistory(list, notehistory, callback);
2015-05-04 07:53:29 +00:00
}
2017-01-05 12:56:16 +00:00
export function parseStorageToHistory(list, callback) {
2015-05-15 04:58:13 +00:00
if (store.enabled) {
2017-01-05 12:56:16 +00:00
let data = store.get('notehistory');
2015-05-15 04:58:13 +00:00
if (data) {
if (typeof data == "string")
data = JSON.parse(data);
parseToHistory(list, data, callback);
} else
parseCookieToHistory(list, callback);
} else {
parseCookieToHistory(list, callback);
}
}
function parseToHistory(list, notehistory, callback) {
if (!callback) return;
else if (!list || !notehistory) callback(list, notehistory);
else if (notehistory && notehistory.length > 0) {
2017-01-05 12:56:16 +00:00
for (let i = 0; i < notehistory.length; i++) {
2015-05-15 04:58:13 +00:00
//parse time to timestamp and fromNow
2017-01-05 12:56:16 +00:00
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].fromNow = timestamp.fromNow();
notehistory[i].time = timestamp.format('llll');
// prevent XSS
notehistory[i].text = S(notehistory[i].text).escapeHTML().s;
notehistory[i].tags = (notehistory[i].tags && notehistory[i].tags.length > 0) ? S(notehistory[i].tags).escapeHTML().s.split(',') : [];
// add to list
if (notehistory[i].id && list.get('id', notehistory[i].id).length == 0)
2015-05-15 04:58:13 +00:00
list.add(notehistory[i]);
2015-05-04 07:53:29 +00:00
}
}
2015-05-15 04:58:13 +00:00
callback(list, notehistory);
}
2017-01-05 12:56:16 +00:00
export function postHistoryToServer(noteId, data, callback) {
$.post(`${serverurl}/history/${noteId}`, data)
.done(result => callback(null, result))
.fail((xhr, status, error) => {
console.error(xhr.responseText);
return callback(error, null);
});
}
2017-01-05 12:56:16 +00:00
export function deleteServerHistory(noteId, callback) {
$.ajax({
2017-01-05 12:56:16 +00:00
url: `${serverurl}/history${noteId ? '/' + noteId : ""}`,
type: 'DELETE'
})
2017-01-05 12:56:16 +00:00
.done(result => callback(null, result))
.fail((xhr, status, error) => {
console.error(xhr.responseText);
return callback(error, null);
});
}