refactor: fix lint warning on public/js/history.js

Signed-off-by: BoHong Li <a60814billy@gmail.com>
This commit is contained in:
BoHong Li 2019-04-12 17:55:10 +08:00
parent 573501389a
commit 0498dc70e8
No known key found for this signature in database
GPG Key ID: 9696D5590D58290F
1 changed files with 77 additions and 81 deletions

View File

@ -13,13 +13,9 @@ import {
encodeNoteId
} from './utils'
import {
checkIfAuth
} from './lib/common/login'
import { checkIfAuth } from './lib/common/login'
import {
urlpath
} from './lib/config'
import { urlpath } from './lib/config'
window.migrateHistoryFromTempCallback = null
@ -30,40 +26,40 @@ function migrateHistoryFromTemp () {
$.get(`${serverurl}/temp`, {
tempid: wurl('#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--
.done(data => {
if (data && data.temp) {
getStorageHistory(olddata => {
if (!olddata || olddata.length === 0) {
saveHistoryToStorage(JSON.parse(data.temp))
}
})
}
}
hash = hash.join('&')
location.hash = hash
if (window.migrateHistoryFromTempCallback) { window.migrateHistoryFromTempCallback() }
})
})
.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() }
})
}
}
export function saveHistory (notehistory) {
checkIfAuth(
() => {
saveHistoryToServer(notehistory)
},
() => {
saveHistoryToStorage(notehistory)
}
)
() => {
saveHistoryToServer(notehistory)
},
() => {
saveHistoryToStorage(notehistory)
}
)
}
function saveHistoryToStorage (notehistory) {
@ -82,9 +78,9 @@ export function saveStorageHistoryToServer (callback) {
$.post(`${serverurl}/history`, {
history: data
})
.done(data => {
callback(data)
})
.done(data => {
callback(data)
})
}
}
@ -111,7 +107,7 @@ export function clearDuplicatedHistory (notehistory) {
}
function addHistory (id, text, time, tags, pinned, notehistory) {
// only add when note id exists
// only add when note id exists
if (id) {
notehistory.push({
id,
@ -137,14 +133,14 @@ export function removeHistory (id, notehistory) {
// used for inner
export function writeHistory (title, tags) {
checkIfAuth(
() => {
// no need to do this anymore, this will count from server-side
// writeHistoryToServer(title, tags);
},
() => {
writeHistoryToStorage(title, tags)
}
)
() => {
// no need to do this anymore, this will count from server-side
// writeHistoryToServer(title, tags);
},
() => {
writeHistoryToStorage(title, tags)
}
)
}
function writeHistoryToStorage (title, tags) {
@ -165,7 +161,7 @@ if (!Array.isArray) {
}
function renderHistory (title, tags) {
// console.debug(tags);
// console.debug(tags);
const id = urlpath ? location.pathname.slice(urlpath.length + 1, location.pathname.length).split('/')[1] : location.pathname.split('/')[1]
return {
id,
@ -177,7 +173,7 @@ function renderHistory (title, tags) {
function generateHistory (title, tags, notehistory) {
const info = renderHistory(title, tags)
// keep any pinned data
// keep any pinned data
let pinned = false
for (let i = 0; i < notehistory.length; i++) {
if (notehistory[i].id === info.id && notehistory[i].pinned) {
@ -194,25 +190,25 @@ function generateHistory (title, tags, notehistory) {
// used for outer
export function getHistory (callback) {
checkIfAuth(
() => {
getServerHistory(callback)
},
() => {
getStorageHistory(callback)
}
)
() => {
getServerHistory(callback)
},
() => {
getStorageHistory(callback)
}
)
}
function getServerHistory (callback) {
$.get(`${serverurl}/history`)
.done(data => {
if (data.history) {
callback(data.history)
}
})
.fail((xhr, status, error) => {
console.error(xhr.responseText)
})
.done(data => {
if (data.history) {
callback(data.history)
}
})
.fail((xhr, status, error) => {
console.error(xhr.responseText)
})
}
export function getStorageHistory (callback) {
@ -227,25 +223,25 @@ export function getStorageHistory (callback) {
export function parseHistory (list, callback) {
checkIfAuth(
() => {
parseServerToHistory(list, callback)
},
() => {
parseStorageToHistory(list, callback)
}
)
() => {
parseServerToHistory(list, callback)
},
() => {
parseStorageToHistory(list, callback)
}
)
}
export function parseServerToHistory (list, callback) {
$.get(`${serverurl}/history`)
.done(data => {
if (data.history) {
parseToHistory(list, data.history, callback)
}
})
.fail((xhr, status, error) => {
console.error(xhr.responseText)
})
.done(data => {
if (data.history) {
parseToHistory(list, data.history, callback)
}
})
.fail((xhr, status, error) => {
console.error(xhr.responseText)
})
}
export function parseStorageToHistory (list, callback) {
@ -271,15 +267,15 @@ function parseToHistory (list, notehistory, callback) {
} catch (err) {
console.error(err)
}
// parse time to timestamp and fromNow
// 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')
// prevent XSS
// prevent XSS
notehistory[i].text = escapeHTML(notehistory[i].text)
notehistory[i].tags = (notehistory[i].tags && notehistory[i].tags.length > 0) ? escapeHTML(notehistory[i].tags).split(',') : []
// add to list
// add to list
if (notehistory[i].id && list.get('id', notehistory[i].id).length === 0) { list.add(notehistory[i]) }
}
}