2017-03-08 18:41:05 +00:00
|
|
|
/* eslint-env browser, jquery */
|
2018-06-30 14:47:37 +00:00
|
|
|
/* global serverurl, moment */
|
2017-03-08 18:41:05 +00:00
|
|
|
|
|
|
|
import store from 'store'
|
|
|
|
import S from 'string'
|
2018-03-03 08:25:30 +00:00
|
|
|
import LZString from 'lz-string'
|
|
|
|
|
|
|
|
import {
|
|
|
|
checkNoteIdValid,
|
|
|
|
encodeNoteId
|
|
|
|
} from './utils'
|
2017-01-13 14:51:44 +00:00
|
|
|
|
|
|
|
import {
|
|
|
|
checkIfAuth
|
2017-03-08 18:41:05 +00:00
|
|
|
} from './lib/common/login'
|
2017-01-13 14:51:44 +00:00
|
|
|
|
2017-01-05 12:56:16 +00:00
|
|
|
import {
|
|
|
|
urlpath
|
2017-03-08 18:41:05 +00:00
|
|
|
} from './lib/config'
|
2016-10-08 12:02:30 +00:00
|
|
|
|
2017-03-08 18:41:05 +00:00
|
|
|
window.migrateHistoryFromTempCallback = null
|
2015-05-15 04:58:13 +00:00
|
|
|
|
2017-03-08 18:41:05 +00:00
|
|
|
migrateHistoryFromTemp()
|
2015-05-15 04:58:13 +00:00
|
|
|
|
2017-03-08 18:41:05 +00:00
|
|
|
function migrateHistoryFromTemp () {
|
|
|
|
if (window.url('#tempid')) {
|
|
|
|
$.get(`${serverurl}/temp`, {
|
|
|
|
tempid: window.url('#tempid')
|
|
|
|
})
|
|
|
|
.done(data => {
|
|
|
|
if (data && data.temp) {
|
|
|
|
getStorageHistory(olddata => {
|
|
|
|
if (!olddata || olddata.length === 0) {
|
|
|
|
saveHistoryToStorage(JSON.parse(data.temp))
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.always(() => {
|
|
|
|
let hash = location.hash.split('#')[1]
|
|
|
|
hash = hash.split('&')
|
|
|
|
for (let i = 0; i < hash.length; i++) {
|
|
|
|
if (hash[i].indexOf('tempid') === 0) {
|
|
|
|
hash.splice(i, 1)
|
|
|
|
i--
|
|
|
|
}
|
|
|
|
}
|
|
|
|
hash = hash.join('&')
|
|
|
|
location.hash = hash
|
|
|
|
if (window.migrateHistoryFromTempCallback) { window.migrateHistoryFromTempCallback() }
|
|
|
|
})
|
|
|
|
}
|
2015-05-04 07:53:29 +00:00
|
|
|
}
|
|
|
|
|
2017-03-08 18:41:05 +00:00
|
|
|
export function saveHistory (notehistory) {
|
|
|
|
checkIfAuth(
|
2017-01-05 12:56:16 +00:00
|
|
|
() => {
|
2017-03-08 18:41:05 +00:00
|
|
|
saveHistoryToServer(notehistory)
|
2015-05-04 07:53:29 +00:00
|
|
|
},
|
2017-01-05 12:56:16 +00:00
|
|
|
() => {
|
2017-03-08 18:41:05 +00:00
|
|
|
saveHistoryToStorage(notehistory)
|
2015-05-04 07:53:29 +00:00
|
|
|
}
|
2017-03-08 18:41:05 +00:00
|
|
|
)
|
2015-05-04 07:53:29 +00:00
|
|
|
}
|
|
|
|
|
2017-03-08 18:41:05 +00:00
|
|
|
function saveHistoryToStorage (notehistory) {
|
2018-06-30 14:47:37 +00:00
|
|
|
store.set('notehistory', JSON.stringify(notehistory))
|
2015-05-04 07:53:29 +00:00
|
|
|
}
|
|
|
|
|
2017-03-08 18:41:05 +00:00
|
|
|
function saveHistoryToServer (notehistory) {
|
|
|
|
$.post(`${serverurl}/history`, {
|
|
|
|
history: JSON.stringify(notehistory)
|
|
|
|
})
|
2015-05-04 07:53:29 +00:00
|
|
|
}
|
|
|
|
|
2017-03-08 18:41:05 +00:00
|
|
|
export function saveStorageHistoryToServer (callback) {
|
|
|
|
const data = store.get('notehistory')
|
|
|
|
if (data) {
|
2017-01-05 12:56:16 +00:00
|
|
|
$.post(`${serverurl}/history`, {
|
2017-03-08 18:41:05 +00:00
|
|
|
history: data
|
|
|
|
})
|
|
|
|
.done(data => {
|
|
|
|
callback(data)
|
|
|
|
})
|
|
|
|
}
|
2015-05-04 07:53:29 +00:00
|
|
|
}
|
|
|
|
|
2017-03-08 18:41:05 +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) {
|
|
|
|
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
|
|
|
}
|
2017-03-08 18:41:05 +00:00
|
|
|
found = true
|
|
|
|
break
|
|
|
|
}
|
2015-05-04 07:53:29 +00:00
|
|
|
}
|
2017-03-08 18:41:05 +00:00
|
|
|
if (!found) { newnotehistory.push(notehistory[i]) }
|
|
|
|
}
|
|
|
|
return newnotehistory
|
2015-05-04 07:53:29 +00:00
|
|
|
}
|
|
|
|
|
2017-03-08 18:41:05 +00:00
|
|
|
function addHistory (id, text, time, tags, pinned, notehistory) {
|
2016-02-17 04:08:44 +00:00
|
|
|
// only add when note id exists
|
2017-03-08 18:41:05 +00:00
|
|
|
if (id) {
|
|
|
|
notehistory.push({
|
|
|
|
id,
|
|
|
|
text,
|
|
|
|
time,
|
|
|
|
tags,
|
|
|
|
pinned
|
|
|
|
})
|
|
|
|
}
|
|
|
|
return notehistory
|
2015-05-04 07:53:29 +00:00
|
|
|
}
|
|
|
|
|
2017-03-08 18:41:05 +00:00
|
|
|
export function removeHistory (id, notehistory) {
|
|
|
|
for (let i = 0; i < notehistory.length; i++) {
|
|
|
|
if (notehistory[i].id === id) {
|
|
|
|
notehistory.splice(i, 1)
|
|
|
|
i -= 1
|
2015-05-04 07:53:29 +00:00
|
|
|
}
|
2017-03-08 18:41:05 +00:00
|
|
|
}
|
|
|
|
return notehistory
|
2015-05-04 07:53:29 +00:00
|
|
|
}
|
|
|
|
|
2017-03-08 18:41:05 +00:00
|
|
|
// used for inner
|
|
|
|
export function writeHistory (title, tags) {
|
|
|
|
checkIfAuth(
|
2017-01-05 12:56:16 +00:00
|
|
|
() => {
|
2016-10-10 12:55:33 +00:00
|
|
|
// no need to do this anymore, this will count from server-side
|
2016-10-19 14:10:33 +00:00
|
|
|
// writeHistoryToServer(title, tags);
|
2015-05-04 07:53:29 +00:00
|
|
|
},
|
2017-01-05 12:56:16 +00:00
|
|
|
() => {
|
2017-03-08 18:41:05 +00:00
|
|
|
writeHistoryToStorage(title, tags)
|
2015-05-04 07:53:29 +00:00
|
|
|
}
|
2017-03-08 18:41:05 +00:00
|
|
|
)
|
2015-05-04 07:53:29 +00:00
|
|
|
}
|
|
|
|
|
2017-03-08 18:41:05 +00:00
|
|
|
function writeHistoryToStorage (title, tags) {
|
2018-06-30 14:47:37 +00:00
|
|
|
let data = store.get('notehistory')
|
|
|
|
let notehistory
|
|
|
|
if (data && typeof data === 'string') {
|
|
|
|
notehistory = JSON.parse(data)
|
2017-03-08 18:41:05 +00:00
|
|
|
} else {
|
2018-06-30 14:47:37 +00:00
|
|
|
notehistory = []
|
2017-03-08 18:41:05 +00:00
|
|
|
}
|
2018-06-30 14:47:37 +00:00
|
|
|
|
|
|
|
const newnotehistory = generateHistory(title, tags, notehistory)
|
|
|
|
saveHistoryToStorage(newnotehistory)
|
2015-05-15 04:58:13 +00:00
|
|
|
}
|
|
|
|
|
2016-06-21 13:42:03 +00:00
|
|
|
if (!Array.isArray) {
|
2017-03-08 18:41:05 +00:00
|
|
|
Array.isArray = arg => Object.prototype.toString.call(arg) === '[object Array]'
|
2016-06-21 13:42:03 +00:00
|
|
|
}
|
|
|
|
|
2017-03-08 18:41:05 +00:00
|
|
|
function renderHistory (title, tags) {
|
|
|
|
// console.debug(tags);
|
|
|
|
const id = urlpath ? location.pathname.slice(urlpath.length + 1, location.pathname.length).split('/')[1] : location.pathname.split('/')[1]
|
|
|
|
return {
|
|
|
|
id,
|
|
|
|
text: title,
|
|
|
|
time: moment().valueOf(),
|
|
|
|
tags
|
|
|
|
}
|
2015-05-04 07:53:29 +00:00
|
|
|
}
|
|
|
|
|
2017-03-08 18:41:05 +00:00
|
|
|
function generateHistory (title, tags, notehistory) {
|
|
|
|
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
|
2017-01-05 12:56:16 +00:00
|
|
|
}
|
2017-03-08 18:41:05 +00:00
|
|
|
}
|
|
|
|
notehistory = removeHistory(info.id, notehistory)
|
|
|
|
notehistory = addHistory(info.id, info.text, info.time, info.tags, pinned, notehistory)
|
|
|
|
notehistory = clearDuplicatedHistory(notehistory)
|
|
|
|
return notehistory
|
2015-05-04 07:53:29 +00:00
|
|
|
}
|
|
|
|
|
2017-03-08 18:41:05 +00:00
|
|
|
// used for outer
|
|
|
|
export function getHistory (callback) {
|
|
|
|
checkIfAuth(
|
2017-01-05 12:56:16 +00:00
|
|
|
() => {
|
2017-03-08 18:41:05 +00:00
|
|
|
getServerHistory(callback)
|
2015-05-04 07:53:29 +00:00
|
|
|
},
|
2017-01-05 12:56:16 +00:00
|
|
|
() => {
|
2017-03-08 18:41:05 +00:00
|
|
|
getStorageHistory(callback)
|
2015-05-04 07:53:29 +00:00
|
|
|
}
|
2017-03-08 18:41:05 +00:00
|
|
|
)
|
2015-05-04 07:53:29 +00:00
|
|
|
}
|
|
|
|
|
2017-03-08 18:41:05 +00:00
|
|
|
function getServerHistory (callback) {
|
|
|
|
$.get(`${serverurl}/history`)
|
2017-01-05 12:56:16 +00:00
|
|
|
.done(data => {
|
2017-03-08 18:41:05 +00:00
|
|
|
if (data.history) {
|
|
|
|
callback(data.history)
|
|
|
|
}
|
2015-05-04 07:53:29 +00:00
|
|
|
})
|
2017-01-05 12:56:16 +00:00
|
|
|
.fail((xhr, status, error) => {
|
2017-03-08 18:41:05 +00:00
|
|
|
console.error(xhr.responseText)
|
|
|
|
})
|
2015-05-04 07:53:29 +00:00
|
|
|
}
|
|
|
|
|
2017-03-08 18:41:05 +00:00
|
|
|
export function getStorageHistory (callback) {
|
2018-06-30 14:47:37 +00:00
|
|
|
let data = store.get('notehistory')
|
|
|
|
if (data) {
|
|
|
|
if (typeof data === 'string') { data = JSON.parse(data) }
|
|
|
|
callback(data)
|
2017-03-08 18:41:05 +00:00
|
|
|
}
|
2018-06-30 14:47:37 +00:00
|
|
|
callback([])
|
2015-05-04 07:53:29 +00:00
|
|
|
}
|
|
|
|
|
2017-03-08 18:41:05 +00:00
|
|
|
export function parseHistory (list, callback) {
|
|
|
|
checkIfAuth(
|
2017-01-05 12:56:16 +00:00
|
|
|
() => {
|
2017-03-08 18:41:05 +00:00
|
|
|
parseServerToHistory(list, callback)
|
2015-05-04 07:53:29 +00:00
|
|
|
},
|
2017-01-05 12:56:16 +00:00
|
|
|
() => {
|
2017-03-08 18:41:05 +00:00
|
|
|
parseStorageToHistory(list, callback)
|
2015-05-04 07:53:29 +00:00
|
|
|
}
|
2017-03-08 18:41:05 +00:00
|
|
|
)
|
2015-05-04 07:53:29 +00:00
|
|
|
}
|
|
|
|
|
2017-03-08 18:41:05 +00:00
|
|
|
export function parseServerToHistory (list, callback) {
|
|
|
|
$.get(`${serverurl}/history`)
|
2017-01-05 12:56:16 +00:00
|
|
|
.done(data => {
|
2017-03-08 18:41:05 +00:00
|
|
|
if (data.history) {
|
|
|
|
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) => {
|
2017-03-08 18:41:05 +00:00
|
|
|
console.error(xhr.responseText)
|
|
|
|
})
|
2015-05-04 07:53:29 +00:00
|
|
|
}
|
|
|
|
|
2017-03-08 18:41:05 +00:00
|
|
|
export function parseStorageToHistory (list, callback) {
|
2018-06-30 14:47:37 +00:00
|
|
|
let data = store.get('notehistory')
|
|
|
|
if (data) {
|
|
|
|
if (typeof data === 'string') { data = JSON.parse(data) }
|
|
|
|
parseToHistory(list, data, callback)
|
2017-03-08 18:41:05 +00:00
|
|
|
}
|
2018-06-30 14:47:37 +00:00
|
|
|
parseToHistory(list, [], callback)
|
2015-05-15 04:58:13 +00:00
|
|
|
}
|
|
|
|
|
2017-03-08 18:41:05 +00:00
|
|
|
function parseToHistory (list, notehistory, callback) {
|
|
|
|
if (!callback) return
|
|
|
|
else if (!list || !notehistory) callback(list, notehistory)
|
|
|
|
else if (notehistory && notehistory.length > 0) {
|
|
|
|
for (let i = 0; i < notehistory.length; i++) {
|
2018-03-03 08:25:30 +00:00
|
|
|
// migrate LZString encoded id to base64url encoded id
|
|
|
|
try {
|
|
|
|
let id = LZString.decompressFromBase64(notehistory[i].id)
|
|
|
|
if (id && checkNoteIdValid(id)) {
|
|
|
|
notehistory[i].id = encodeNoteId(id)
|
|
|
|
}
|
|
|
|
} catch (err) {
|
2018-03-10 19:00:36 +00:00
|
|
|
console.error(err)
|
2018-03-03 08:25:30 +00:00
|
|
|
}
|
2017-03-08 18:41:05 +00:00
|
|
|
// parse time to timestamp and fromNow
|
|
|
|
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')
|
2016-11-26 14:56:03 +00:00
|
|
|
// prevent XSS
|
2017-03-08 18:41:05 +00:00
|
|
|
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(',') : []
|
2016-11-26 14:56:03 +00:00
|
|
|
// add to list
|
2017-03-08 18:41:05 +00:00
|
|
|
if (notehistory[i].id && list.get('id', notehistory[i].id).length === 0) { list.add(notehistory[i]) }
|
2015-05-04 07:53:29 +00:00
|
|
|
}
|
2017-03-08 18:41:05 +00:00
|
|
|
}
|
|
|
|
callback(list, notehistory)
|
2016-10-08 12:02:30 +00:00
|
|
|
}
|
|
|
|
|
2017-03-08 18:41:05 +00:00
|
|
|
export function postHistoryToServer (noteId, data, callback) {
|
|
|
|
$.post(`${serverurl}/history/${noteId}`, data)
|
2017-01-05 12:56:16 +00:00
|
|
|
.done(result => callback(null, result))
|
|
|
|
.fail((xhr, status, error) => {
|
2017-03-08 18:41:05 +00:00
|
|
|
console.error(xhr.responseText)
|
|
|
|
return callback(error, null)
|
|
|
|
})
|
2016-10-10 12:55:33 +00:00
|
|
|
}
|
|
|
|
|
2017-03-08 18:41:05 +00:00
|
|
|
export function deleteServerHistory (noteId, callback) {
|
|
|
|
$.ajax({
|
|
|
|
url: `${serverurl}/history${noteId ? '/' + noteId : ''}`,
|
|
|
|
type: 'DELETE'
|
|
|
|
})
|
2017-01-05 12:56:16 +00:00
|
|
|
.done(result => callback(null, result))
|
|
|
|
.fail((xhr, status, error) => {
|
2017-03-08 18:41:05 +00:00
|
|
|
console.error(xhr.responseText)
|
|
|
|
return callback(error, null)
|
|
|
|
})
|
2016-10-11 10:39:15 +00:00
|
|
|
}
|