From 2cfcae6db2ae306a14a23317b40ad04924ff7ec6 Mon Sep 17 00:00:00 2001 From: Wu Cheng-Han Date: Thu, 22 Oct 2015 17:09:55 +0800 Subject: [PATCH] Support pinning note in history --- public/css/cover.css | 16 ++++++++++++ public/js/cover.js | 61 +++++++++++++++++++++++++++++++++++++++++--- public/js/history.js | 15 ++++++++--- 3 files changed, 85 insertions(+), 7 deletions(-) diff --git a/public/css/cover.css b/public/css/cover.css index 26cc8d5fa..0a731cd26 100644 --- a/public/css/cover.css +++ b/public/css/cover.css @@ -257,6 +257,22 @@ input { .ui-history-close:hover { opacity: 1; } +.ui-history-pin { + position: absolute; + left: 14px; + top: 15px; + font-size: 16px; + opacity: 0.2; + transition: opacity 0.2s ease-in-out; + -webkit-transition: opacity 0.2s ease-in-out; +} +.item:hover .ui-history-pin:hover { + opacity: 1; +} +.item .ui-history-pin.active { + opacity: 1; + color: #d43f3a; +} .ui-or { margin-top: 5px; margin-bottom: 5px; diff --git a/public/js/cover.js b/public/js/cover.js index 567d64229..f2f3ae911 100644 --- a/public/js/cover.js +++ b/public/js/cover.js @@ -1,9 +1,10 @@ var options = { - valueNames: ['id', 'text', 'timestamp', 'fromNow', 'time', 'tags'], + valueNames: ['id', 'text', 'timestamp', 'fromNow', 'time', 'tags', 'pinned'], item: '
  • \ \ \
    \ +
    \
    \
    \

    \ @@ -83,17 +84,41 @@ function checkHistoryList() { function parseHistoryCallback(list, notehistory) { checkHistoryList(); - list.sort('timestamp', { - order: "desc" - }); + //sort by pinned then timestamp + list.sort('', { + sortFunction: function (a, b) { + var notea = a.values(); + var noteb = b.values(); + if (notea.pinned && !noteb.pinned) { + return -1; + } else if (!notea.pinned && noteb.pinned) { + return 1; + } else { + if (notea.timestamp > noteb.timestamp) { + return -1; + } else if (notea.timestamp < noteb.timestamp) { + return 1; + } else { + return 0; + } + } + } + }); var filtertags = []; $(".item").each(function (key, value) { var a = $(this).closest("a"); + var pin = $(this).find(".ui-history-pin"); var id = a.siblings("span").html(); var tagsEl = $(this).find(".tags"); var item = historyList.get('id', id); if (item.length > 0 && item[0]) { var values = item[0].values(); + //parse pinned + if (values.pinned) { + pin.addClass('active'); + } else { + pin.removeClass('active'); + } //parse link to element a a.attr('href', '/' + values.id); //parse tags @@ -124,6 +149,34 @@ function parseHistoryCallback(list, notehistory) { $('.ui-delete-modal-item').html(' ' + value.text + '
    ' + value.time); clearHistory = false; deleteId = id; + }); + $(".ui-history-pin").click(function (e) { + e.preventDefault(); + var $this = $(this); + var id = $this.closest("a").siblings("span").html(); + var item = list.get('id', id)[0]; + var values = item.values(); + var pinned = values.pinned; + if (!values.pinned) { + pinned = true; + item._values.pinned = true; + } else { + pinned = false; + item._values.pinned = false; + } + getHistory(function (notehistory) { + for(var i = 0; i < notehistory.length; i++) { + if (notehistory[i].id == id) { + notehistory[i].pinned = pinned; + break; + } + } + saveHistory(notehistory); + if (pinned) + $this.addClass('active'); + else + $this.removeClass('active'); + }); }); buildTagsFilter(filtertags); } diff --git a/public/js/history.js b/public/js/history.js index 82c145dab..b3656d896 100644 --- a/public/js/history.js +++ b/public/js/history.js @@ -111,12 +111,13 @@ function clearDuplicatedHistory(notehistory) { return newnotehistory; } -function addHistory(id, text, time, tags, notehistory) { +function addHistory(id, text, time, tags, pinned, notehistory) { notehistory.push({ id: id, text: text, time: time, - tags: tags + tags: tags, + pinned: pinned }); return notehistory; } @@ -232,8 +233,16 @@ function renderHistory(view) { function generateHistory(view, notehistory) { var info = renderHistory(view); + //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; + } + } notehistory = removeHistory(info.id, notehistory); - notehistory = addHistory(info.id, info.text, info.time, info.tags, notehistory); + notehistory = addHistory(info.id, info.text, info.time, info.tags, pinned, notehistory); notehistory = clearDuplicatedHistory(notehistory); return notehistory; }