2016-10-08 12:02:30 +00:00
|
|
|
var store = require('store');
|
|
|
|
|
|
|
|
var common = require('./common');
|
|
|
|
var checkIfAuth = common.checkIfAuth;
|
|
|
|
var urlpath = common.urlpath;
|
2016-10-10 02:14:17 +00:00
|
|
|
var serverurl = common.serverurl;
|
2016-10-11 12:17:44 +00:00
|
|
|
var getLoginState = common.getLoginState;
|
2016-10-08 12:02:30 +00:00
|
|
|
|
|
|
|
var extra = require('./extra');
|
|
|
|
var renderFilename = extra.renderFilename;
|
|
|
|
var md = extra.md;
|
|
|
|
|
2015-05-15 04:58:13 +00:00
|
|
|
var migrateHistoryFromTempCallback = null;
|
|
|
|
|
|
|
|
migrateHistoryFromTemp();
|
|
|
|
|
|
|
|
function migrateHistoryFromTemp() {
|
|
|
|
if (url('#tempid')) {
|
2016-02-17 04:08:44 +00:00
|
|
|
$.get(serverurl + '/temp', {
|
2015-05-15 04:58:13 +00:00
|
|
|
tempid: url('#tempid')
|
|
|
|
})
|
|
|
|
.done(function (data) {
|
|
|
|
if (data && data.temp) {
|
|
|
|
getStorageHistory(function (olddata) {
|
|
|
|
if (!olddata || olddata.length == 0) {
|
|
|
|
saveHistoryToStorage(JSON.parse(data.temp));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.always(function () {
|
|
|
|
var hash = location.hash.split('#')[1];
|
|
|
|
hash = hash.split('&');
|
|
|
|
for (var i = 0; i < hash.length; i++)
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
function saveHistory(notehistory) {
|
|
|
|
checkIfAuth(
|
|
|
|
function () {
|
|
|
|
saveHistoryToServer(notehistory);
|
|
|
|
},
|
|
|
|
function () {
|
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
|
2016-10-10 12:49:42 +00:00
|
|
|
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) {
|
2016-02-17 04:08:44 +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();
|
|
|
|
}
|
|
|
|
|
|
|
|
function saveStorageHistoryToServer(callback) {
|
|
|
|
var data = store.get('notehistory');
|
|
|
|
if (data) {
|
2016-02-17 04:08:44 +00:00
|
|
|
$.post(serverurl + '/history', {
|
2015-05-15 04:58:13 +00:00
|
|
|
history: data
|
|
|
|
})
|
|
|
|
.done(function (data) {
|
|
|
|
callback(data);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-04 07:53:29 +00:00
|
|
|
function saveCookieHistoryToServer(callback) {
|
2016-02-17 04:08:44 +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
|
|
|
})
|
|
|
|
.done(function (data) {
|
2015-05-15 04:58:13 +00:00
|
|
|
callback(data);
|
2015-05-04 07:53:29 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function clearDuplicatedHistory(notehistory) {
|
|
|
|
var newnotehistory = [];
|
|
|
|
for (var i = 0; i < notehistory.length; i++) {
|
|
|
|
var found = false;
|
|
|
|
for (var j = 0; j < newnotehistory.length; j++) {
|
2016-06-01 17:06:35 +00:00
|
|
|
var id = notehistory[i].id.replace(/\=+$/, '');
|
|
|
|
var newId = newnotehistory[j].id.replace(/\=+$/, '');
|
2015-09-24 03:55:56 +00:00
|
|
|
if (id == newId || notehistory[i].id == newnotehistory[j].id || !notehistory[i].id || !newnotehistory[j].id) {
|
2016-10-11 08:46:50 +00:00
|
|
|
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'));
|
2015-09-24 03:55:56 +00:00
|
|
|
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) {
|
2016-02-17 04:08:44 +00:00
|
|
|
// only add when note id exists
|
|
|
|
if (id) {
|
|
|
|
notehistory.push({
|
|
|
|
id: id,
|
|
|
|
text: text,
|
|
|
|
time: time,
|
|
|
|
tags: tags,
|
|
|
|
pinned: pinned
|
|
|
|
});
|
|
|
|
}
|
2015-05-04 07:53:29 +00:00
|
|
|
return notehistory;
|
|
|
|
}
|
|
|
|
|
|
|
|
function removeHistory(id, notehistory) {
|
|
|
|
for (var i = 0; i < notehistory.length; i++) {
|
2015-09-26 02:25:00 +00:00
|
|
|
if (notehistory[i].id == id) {
|
2015-05-04 07:53:29 +00:00
|
|
|
notehistory.splice(i, 1);
|
2015-09-26 02:25:00 +00:00
|
|
|
i--;
|
|
|
|
}
|
2015-05-04 07:53:29 +00:00
|
|
|
}
|
|
|
|
return notehistory;
|
|
|
|
}
|
|
|
|
|
|
|
|
//used for inner
|
|
|
|
function writeHistory(view) {
|
|
|
|
checkIfAuth(
|
|
|
|
function () {
|
2016-10-10 12:55:33 +00:00
|
|
|
// no need to do this anymore, this will count from server-side
|
|
|
|
// writeHistoryToServer(view);
|
2015-05-04 07:53:29 +00:00
|
|
|
},
|
|
|
|
function () {
|
2015-05-15 04:58:13 +00:00
|
|
|
writeHistoryToStorage(view);
|
2015-05-04 07:53:29 +00:00
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
function writeHistoryToServer(view) {
|
2016-02-17 04:08:44 +00:00
|
|
|
$.get(serverurl + '/history')
|
2015-05-04 07:53:29 +00:00
|
|
|
.done(function (data) {
|
|
|
|
try {
|
|
|
|
if (data.history) {
|
|
|
|
var notehistory = data.history;
|
|
|
|
} else {
|
|
|
|
var notehistory = [];
|
|
|
|
}
|
|
|
|
} catch (err) {
|
|
|
|
var notehistory = [];
|
|
|
|
}
|
2015-09-24 03:55:56 +00:00
|
|
|
if (!notehistory)
|
2015-06-01 10:04:25 +00:00
|
|
|
notehistory = [];
|
2015-09-24 03:55:56 +00:00
|
|
|
|
2015-05-04 07:53:29 +00:00
|
|
|
var newnotehistory = generateHistory(view, notehistory);
|
|
|
|
saveHistoryToServer(newnotehistory);
|
|
|
|
})
|
2016-10-10 12:50:01 +00:00
|
|
|
.fail(function (xhr, status, error) {
|
|
|
|
console.error(xhr.responseText);
|
2015-05-04 07:53:29 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function writeHistoryToCookie(view) {
|
|
|
|
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 = [];
|
|
|
|
}
|
2015-09-24 03:55:56 +00:00
|
|
|
if (!notehistory)
|
2015-06-01 10:04:25 +00:00
|
|
|
notehistory = [];
|
2015-09-24 03:55:56 +00:00
|
|
|
|
2015-05-04 07:53:29 +00:00
|
|
|
var newnotehistory = generateHistory(view, notehistory);
|
|
|
|
saveHistoryToCookie(newnotehistory);
|
|
|
|
}
|
|
|
|
|
2015-05-15 04:58:13 +00:00
|
|
|
function writeHistoryToStorage(view) {
|
|
|
|
if (store.enabled) {
|
|
|
|
var data = store.get('notehistory');
|
|
|
|
if (data) {
|
|
|
|
if (typeof data == "string")
|
|
|
|
data = JSON.parse(data);
|
|
|
|
var notehistory = data;
|
|
|
|
} else
|
|
|
|
var notehistory = [];
|
2015-09-24 03:55:56 +00:00
|
|
|
if (!notehistory)
|
2015-06-01 10:04:25 +00:00
|
|
|
notehistory = [];
|
2015-09-24 03:55:56 +00:00
|
|
|
|
2015-05-15 04:58:13 +00:00
|
|
|
var newnotehistory = generateHistory(view, notehistory);
|
|
|
|
saveHistoryToStorage(newnotehistory);
|
|
|
|
} else {
|
|
|
|
writeHistoryToCookie(view);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-21 13:42:03 +00:00
|
|
|
if (!Array.isArray) {
|
|
|
|
Array.isArray = function(arg) {
|
|
|
|
return Object.prototype.toString.call(arg) === '[object Array]';
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2015-05-04 07:53:29 +00:00
|
|
|
function renderHistory(view) {
|
|
|
|
var title = renderFilename(view);
|
|
|
|
|
|
|
|
var tags = [];
|
|
|
|
var rawtags = [];
|
2016-06-21 13:42:03 +00:00
|
|
|
if (md && md.meta && md.meta.tags && (typeof md.meta.tags == "string" || typeof md.meta.tags == "number")) {
|
|
|
|
var metaTags = ('' + md.meta.tags).split(',');
|
|
|
|
for (var i = 0; i < metaTags.length; i++) {
|
|
|
|
var text = metaTags[i].trim();
|
|
|
|
if (text) rawtags.push(text);
|
2015-05-04 07:53:29 +00:00
|
|
|
}
|
2016-06-21 13:42:03 +00:00
|
|
|
} else {
|
|
|
|
view.find('h6').each(function (key, value) {
|
|
|
|
if (/^tags/gmi.test($(value).text())) {
|
|
|
|
var codes = $(value).find("code");
|
|
|
|
for (var i = 0; i < codes.length; i++) {
|
|
|
|
var text = codes[i].innerHTML.trim();
|
|
|
|
if (text) rawtags.push(text);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2015-05-04 07:53:29 +00:00
|
|
|
for (var i = 0; i < rawtags.length; i++) {
|
|
|
|
var found = false;
|
|
|
|
for (var j = 0; j < tags.length; j++) {
|
2016-06-21 13:42:03 +00:00
|
|
|
if (tags[j] == rawtags[i]) {
|
2015-05-04 07:53:29 +00:00
|
|
|
found = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!found)
|
2016-06-21 13:42:03 +00:00
|
|
|
tags.push(rawtags[i]);
|
2015-05-04 07:53:29 +00:00
|
|
|
}
|
|
|
|
//console.debug(tags);
|
2016-02-17 04:08:44 +00:00
|
|
|
var 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 {
|
2016-02-17 04:08:44 +00:00
|
|
|
id: id,
|
2015-05-04 07:53:29 +00:00
|
|
|
text: title,
|
2016-10-11 08:46:50 +00:00
|
|
|
time: moment().valueOf(),
|
2015-05-04 07:53:29 +00:00
|
|
|
tags: tags
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
function generateHistory(view, notehistory) {
|
|
|
|
var info = renderHistory(view);
|
2015-10-22 09:09:55 +00:00
|
|
|
//keep any pinned data
|
|
|
|
var pinned = false;
|
|
|
|
for (var 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);
|
2015-09-24 03:55:56 +00:00
|
|
|
notehistory = clearDuplicatedHistory(notehistory);
|
2015-05-04 07:53:29 +00:00
|
|
|
return notehistory;
|
|
|
|
}
|
|
|
|
|
|
|
|
//used for outer
|
|
|
|
function getHistory(callback) {
|
|
|
|
checkIfAuth(
|
|
|
|
function () {
|
|
|
|
getServerHistory(callback);
|
|
|
|
},
|
|
|
|
function () {
|
2015-05-15 04:58:13 +00:00
|
|
|
getStorageHistory(callback);
|
2015-05-04 07:53:29 +00:00
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
function getServerHistory(callback) {
|
2016-02-17 04:08:44 +00:00
|
|
|
$.get(serverurl + '/history')
|
2015-05-04 07:53:29 +00:00
|
|
|
.done(function (data) {
|
|
|
|
if (data.history) {
|
|
|
|
callback(data.history);
|
|
|
|
}
|
|
|
|
})
|
2016-10-10 12:50:01 +00:00
|
|
|
.fail(function (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'));
|
|
|
|
}
|
|
|
|
|
|
|
|
function getStorageHistory(callback) {
|
|
|
|
if (store.enabled) {
|
|
|
|
var data = store.get('notehistory');
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2015-05-15 04:58:13 +00:00
|
|
|
function parseHistory(list, callback) {
|
2015-05-04 07:53:29 +00:00
|
|
|
checkIfAuth(
|
|
|
|
function () {
|
2015-05-15 04:58:13 +00:00
|
|
|
parseServerToHistory(list, callback);
|
2015-05-04 07:53:29 +00:00
|
|
|
},
|
|
|
|
function () {
|
2015-05-15 04:58:13 +00:00
|
|
|
parseStorageToHistory(list, callback);
|
2015-05-04 07:53:29 +00:00
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2015-05-15 04:58:13 +00:00
|
|
|
function parseServerToHistory(list, callback) {
|
2016-02-17 04:08:44 +00:00
|
|
|
$.get(serverurl + '/history')
|
2015-05-04 07:53:29 +00:00
|
|
|
.done(function (data) {
|
|
|
|
if (data.history) {
|
2015-05-15 04:58:13 +00:00
|
|
|
parseToHistory(list, data.history, callback);
|
2015-05-04 07:53:29 +00:00
|
|
|
}
|
|
|
|
})
|
2016-10-10 12:50:01 +00:00
|
|
|
.fail(function (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) {
|
|
|
|
var notehistory = Cookies.getJSON('notehistory');
|
|
|
|
parseToHistory(list, notehistory, callback);
|
2015-05-04 07:53:29 +00:00
|
|
|
}
|
|
|
|
|
2015-05-15 04:58:13 +00:00
|
|
|
function parseStorageToHistory(list, callback) {
|
|
|
|
if (store.enabled) {
|
|
|
|
var data = store.get('notehistory');
|
|
|
|
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) {
|
2015-05-04 07:53:29 +00:00
|
|
|
for (var i = 0; i < notehistory.length; i++) {
|
2015-05-15 04:58:13 +00:00
|
|
|
//parse time to timestamp and fromNow
|
2016-10-11 08:46:50 +00:00
|
|
|
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');
|
2016-02-17 04:08:44 +00:00
|
|
|
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);
|
2016-10-08 12:02:30 +00:00
|
|
|
}
|
|
|
|
|
2016-10-10 12:55:33 +00:00
|
|
|
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);
|
|
|
|
});
|
2016-10-11 10:39:15 +00:00
|
|
|
}
|
|
|
|
|
2016-10-08 12:02:30 +00:00
|
|
|
module.exports = {
|
2016-10-10 02:14:17 +00:00
|
|
|
writeHistory: writeHistory,
|
|
|
|
parseHistory: parseHistory,
|
|
|
|
getStorageHistory: getStorageHistory,
|
|
|
|
getHistory: getHistory,
|
|
|
|
saveHistory: saveHistory,
|
|
|
|
removeHistory: removeHistory,
|
2016-10-11 10:39:15 +00:00
|
|
|
parseStorageToHistory: parseStorageToHistory,
|
|
|
|
postHistoryToServer: postHistoryToServer,
|
2016-10-11 12:17:44 +00:00
|
|
|
deleteServerHistory: deleteServerHistory,
|
|
|
|
parseServerToHistory: parseServerToHistory,
|
|
|
|
saveStorageHistoryToServer: saveStorageHistoryToServer
|
2016-10-08 12:02:30 +00:00
|
|
|
}
|