From 53300ac1b6d89638803e6baa2650bdeeffcd888f Mon Sep 17 00:00:00 2001 From: Mo Bitar Date: Fri, 1 Jun 2018 11:03:57 -0500 Subject: [PATCH] Client updated at --- .../javascripts/app/controllers/editor.js | 3 -- .../javascripts/app/controllers/notes.js | 11 +++++-- app/assets/javascripts/app/models/api/item.js | 30 +++++++++++++++++-- .../javascripts/app/services/modelManager.js | 4 +-- app/assets/templates/notes.html.haml | 6 ++-- 5 files changed, 42 insertions(+), 12 deletions(-) diff --git a/app/assets/javascripts/app/controllers/editor.js b/app/assets/javascripts/app/controllers/editor.js index 3b8d2cd63..ca6bcfa30 100644 --- a/app/assets/javascripts/app/controllers/editor.js +++ b/app/assets/javascripts/app/controllers/editor.js @@ -326,19 +326,16 @@ angular.module('app') this.togglePin = function() { this.note.setAppDataItem("pinned", !this.note.pinned); - this.note.setDirty(true); this.changesMade(); } this.toggleLockNote = function() { this.note.setAppDataItem("locked", !this.note.locked); - this.note.setDirty(true); this.changesMade(); } this.toggleArchiveNote = function() { this.note.setAppDataItem("archived", !this.note.archived); - this.note.setDirty(true); this.changesMade(true); $rootScope.$broadcast("noteArchived"); } diff --git a/app/assets/javascripts/app/controllers/notes.js b/app/assets/javascripts/app/controllers/notes.js index c9e5478c2..6f7362e3d 100644 --- a/app/assets/javascripts/app/controllers/notes.js +++ b/app/assets/javascripts/app/controllers/notes.js @@ -41,7 +41,14 @@ angular.module('app') this.loadPreferences = function() { let prevSortValue = this.sortBy; + this.sortBy = authManager.getUserPrefValue("sortBy", "created_at"); + + if(this.sortBy == "updated_at") { + // use client_updated_at instead + this.sortBy = "client_updated_at"; + } + if(prevSortValue && prevSortValue != this.sortBy) { $timeout(() => { this.selectFirstNote(); @@ -120,7 +127,7 @@ angular.module('app') var base = ""; if(this.sortBy == "created_at") { base += " Date Added"; - } else if(this.sortBy == "updated_at") { + } else if(this.sortBy == "client_updated_at") { base += " Date Modifed"; } else if(this.sortBy == "title") { base += " Title"; @@ -274,7 +281,7 @@ angular.module('app') } this.selectedSortByUpdated = function() { - this.setSortBy("updated_at"); + this.setSortBy("client_updated_at"); this.sortDescending = true; } diff --git a/app/assets/javascripts/app/models/api/item.js b/app/assets/javascripts/app/models/api/item.js index b3ebec65d..5fa9e81ba 100644 --- a/app/assets/javascripts/app/models/api/item.js +++ b/app/assets/javascripts/app/models/api/item.js @@ -48,6 +48,9 @@ class Item { this.updated_at = new Date(); } + // Allows the getter to be re-invoked + this._client_updated_at = null; + if(json.content) { this.mapContentToLocalProperties(this.contentObject); } else if(json.deleted == true) { @@ -68,7 +71,7 @@ class Item { // Subclasses can override } - setDirty(dirty) { + setDirty(dirty, dontUpdateClientDate) { this.dirty = dirty; // Allows the syncManager to check if an item has been marked dirty after a sync has been started @@ -81,6 +84,11 @@ class Item { this.dirtyCount = 0; } + if(dirty && !dontUpdateClientDate) { + // Set the client modified date to now if marking the item as dirty + this.client_updated_at = new Date(); + } + if(dirty) { this.notifyObserversOfChange(); } @@ -214,6 +222,24 @@ class Item { return this.getAppDataItem("locked"); } + get client_updated_at() { + if(!this._client_updated_at) { + var saved = this.getAppDataItem("client_updated_at"); + if(saved) { + this._client_updated_at = new Date(saved); + } else { + this._client_updated_at = new Date(this.updated_at); + } + } + return this._client_updated_at; + } + + set client_updated_at(date) { + this._client_updated_at = date; + + this.setAppDataItem("client_updated_at", date); + } + /* During sync conflicts, when determing whether to create a duplicate for an item, we can omit keys that have no meaningful weight and can be ignored. For example, if one component has active = true and another component has active = false, @@ -245,7 +271,7 @@ class Item { } updatedAtString() { - return this.dateToLocalizedString(this.updated_at); + return this.dateToLocalizedString(this.client_updated_at); } dateToLocalizedString(date) { diff --git a/app/assets/javascripts/app/services/modelManager.js b/app/assets/javascripts/app/services/modelManager.js index f41626325..aaf577d37 100644 --- a/app/assets/javascripts/app/services/modelManager.js +++ b/app/assets/javascripts/app/services/modelManager.js @@ -410,13 +410,13 @@ class ModelManager { } /* Used when changing encryption key */ - setAllItemsDirty() { + setAllItemsDirty(dontUpdateClientDates = true) { var relevantItems = this.allItems.filter(function(item){ return _.includes(this.acceptableContentTypes, item.content_type); }.bind(this)); for(var item of relevantItems) { - item.setDirty(true); + item.setDirty(true, dontUpdateClientDates); } } diff --git a/app/assets/templates/notes.html.haml b/app/assets/templates/notes.html.haml index ac2c81dcb..7e516461b 100644 --- a/app/assets/templates/notes.html.haml +++ b/app/assets/templates/notes.html.haml @@ -27,7 +27,7 @@ %h4.title Sort By %menu-row{"label" => "'Date Added'", "circle" => "ctrl.sortBy == 'created_at' && 'success'", "ng-click" => "ctrl.selectedMenuItem($event); ctrl.selectedSortByCreated()", "desc" => "'Sort notes by newest first'"} - %menu-row{"label" => "'Date Modified'", "circle" => "ctrl.sortBy == 'updated_at' && 'success'", "ng-click" => "ctrl.selectedMenuItem($event); ctrl.selectedSortByUpdated()", "desc" => "'Sort notes with the most recently updated first'"} + %menu-row{"label" => "'Date Modified'", "circle" => "ctrl.sortBy == 'client_updated_at' && 'success'", "ng-click" => "ctrl.selectedMenuItem($event); ctrl.selectedSortByUpdated()", "desc" => "'Sort notes with the most recently updated first'"} %menu-row{"label" => "'Title'", "circle" => "ctrl.sortBy == 'title' && 'success'", "ng-click" => "ctrl.selectedMenuItem($event); ctrl.selectedSortByTitle()", "desc" => "'Sort notes alphabetically by their title'"} .section{"ng-if" => "!ctrl.tag.archiveTag"} @@ -64,7 +64,7 @@ .note-preview{"ng-if" => "!ctrl.hideNotePreview"} {{note.text}} .date.faded{"ng-if" => "!ctrl.hideDate"} - %span{"ng-if" => "ctrl.sortBy == 'updated_at'"} Modified {{note.updatedAtString() || 'Now'}} - %span{"ng-if" => "ctrl.sortBy != 'updated_at'"} {{note.createdAtString() || 'Now'}} + %span{"ng-if" => "ctrl.sortBy == 'client_updated_at'"} Modified {{note.updatedAtString() || 'Now'}} + %span{"ng-if" => "ctrl.sortBy != 'client_updated_at'"} {{note.createdAtString() || 'Now'}} %panel-resizer{"panel-id" => "'notes-column'", "on-resize-finish" => "ctrl.onPanelResize", "control" => "ctrl.panelController", "hoverable" => "true", "collapsable" => "true"}