summaryrefslogtreecommitdiff
path: root/public/js/history.js
diff options
context:
space:
mode:
authorYukai Huang2016-10-11 18:39:15 +0800
committerYukai Huang2016-10-11 18:40:23 +0800
commit6e651c8108783d224c5f40d1bb8047a9ebbeff00 (patch)
treec7d501cc6ebb46399c397768b3f58ef05034c522 /public/js/history.js
parent21028c57735028574c769fb6650322eb3f0cb924 (diff)
parentcd9f8fe36b707ff5a9f8f7be4d55145ddee97f3a (diff)
Merge branch 'master' into webpack-frontend
Diffstat (limited to 'public/js/history.js')
-rw-r--r--public/js/history.js59
1 files changed, 44 insertions, 15 deletions
diff --git a/public/js/history.js b/public/js/history.js
index 192a40b3..91f3cccf 100644
--- a/public/js/history.js
+++ b/public/js/history.js
@@ -58,7 +58,7 @@ function saveHistoryToStorage(notehistory) {
if (store.enabled)
store.set('notehistory', JSON.stringify(notehistory));
else
- saveHistoryToStorage(notehistory);
+ saveHistoryToCookie(notehistory);
}
function saveHistoryToCookie(notehistory) {
@@ -107,8 +107,8 @@ function clearDuplicatedHistory(notehistory) {
var id = notehistory[i].id.replace(/\=+$/, '');
var newId = newnotehistory[j].id.replace(/\=+$/, '');
if (id == newId || notehistory[i].id == newnotehistory[j].id || !notehistory[i].id || !newnotehistory[j].id) {
- var time = moment(notehistory[i].time, 'MMMM Do YYYY, h:mm:ss a');
- var newTime = moment(newnotehistory[j].time, 'MMMM Do YYYY, h:mm:ss a');
+ var 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'));
if(time >= newTime) {
newnotehistory[j] = notehistory[i];
}
@@ -150,7 +150,8 @@ function removeHistory(id, notehistory) {
function writeHistory(view) {
checkIfAuth(
function () {
- writeHistoryToServer(view);
+ // no need to do this anymore, this will count from server-side
+ // writeHistoryToServer(view);
},
function () {
writeHistoryToStorage(view);
@@ -176,8 +177,8 @@ function writeHistoryToServer(view) {
var newnotehistory = generateHistory(view, notehistory);
saveHistoryToServer(newnotehistory);
})
- .fail(function () {
- writeHistoryToStorage(view);
+ .fail(function (xhr, status, error) {
+ console.error(xhr.responseText);
});
}
@@ -257,7 +258,7 @@ function renderHistory(view) {
return {
id: id,
text: title,
- time: moment().format('MMMM Do YYYY, h:mm:ss a'),
+ time: moment().valueOf(),
tags: tags
};
}
@@ -297,8 +298,8 @@ function getServerHistory(callback) {
callback(data.history);
}
})
- .fail(function () {
- getStorageHistory(callback);
+ .fail(function (xhr, status, error) {
+ console.error(xhr.responseText);
});
}
@@ -338,8 +339,8 @@ function parseServerToHistory(list, callback) {
parseToHistory(list, data.history, callback);
}
})
- .fail(function () {
- parseStorageToHistory(list, callback);
+ .fail(function (xhr, status, error) {
+ console.error(xhr.responseText);
});
}
@@ -368,9 +369,10 @@ function parseToHistory(list, notehistory, callback) {
else if (notehistory && notehistory.length > 0) {
for (var i = 0; i < notehistory.length; i++) {
//parse time to timestamp and fromNow
- 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');
+ var 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');
if (notehistory[i].id && list.get('id', notehistory[i].id).length == 0)
list.add(notehistory[i]);
}
@@ -378,6 +380,31 @@ function parseToHistory(list, notehistory, callback) {
callback(list, notehistory);
}
+function postHistoryToServer(noteId, data, callback) {
+ $.post(serverurl + '/history/' + noteId, data)
+ .done(function (result) {
+ return callback(null, result);
+ })
+ .fail(function (xhr, status, error) {
+ console.error(xhr.responseText);
+ return callback(error, null);
+ });
+}
+
+function deleteServerHistory(noteId, callback) {
+ $.ajax({
+ url: serverurl + '/history' + (noteId ? '/' + noteId : ""),
+ type: 'DELETE'
+ })
+ .done(function (result) {
+ return callback(null, result);
+ })
+ .fail(function (xhr, status, error) {
+ console.error(xhr.responseText);
+ return callback(error, null);
+ });
+}
+
module.exports = {
writeHistory: writeHistory,
parseHistory: parseHistory,
@@ -385,5 +412,7 @@ module.exports = {
getHistory: getHistory,
saveHistory: saveHistory,
removeHistory: removeHistory,
- parseStorageToHistory: parseStorageToHistory
+ parseStorageToHistory: parseStorageToHistory,
+ postHistoryToServer: postHistoryToServer,
+ deleteServerHistory: deleteServerHistory
}