From 53f75c7fe26b67e5b35ca3fe40e5e4ba11924aae Mon Sep 17 00:00:00 2001 From: Mo Bitar Date: Wed, 16 May 2018 11:39:29 -0500 Subject: [PATCH] Handle case where currently selected note is deleted by a remote sync --- .../javascripts/app/controllers/editor.js | 20 +++++++++++++++++++ .../javascripts/app/controllers/home.js | 8 ++++---- .../javascripts/app/controllers/notes.js | 9 ++++++--- .../javascripts/app/services/modelManager.js | 8 ++++++++ 4 files changed, 38 insertions(+), 7 deletions(-) diff --git a/app/assets/javascripts/app/controllers/editor.js b/app/assets/javascripts/app/controllers/editor.js index 6f4e309af..d2f8766b8 100644 --- a/app/assets/javascripts/app/controllers/editor.js +++ b/app/assets/javascripts/app/controllers/editor.js @@ -40,6 +40,26 @@ angular.module('app') this.loadTagsString(); }.bind(this)); + modelManager.addItemSyncObserver("component-manager", "Note", (allItems, validItems, deletedItems, source) => { + if(!this.note) { return; } + if(!ModelManager.isMappingSourceRetrieved(source)) {return;} + + var matchingNote = allItems.find((item) => { + return item.uuid == this.note.uuid; + }); + + if(!matchingNote) { + return; + } + + if(matchingNote.deleted) { + $rootScope.notifyDelete(); + } else { + // Update tags + this.loadTagsString(); + } + }); + this.noteDidChange = function(note, oldNote) { this.setNote(note, oldNote); this.reloadComponentContext(); diff --git a/app/assets/javascripts/app/controllers/home.js b/app/assets/javascripts/app/controllers/home.js index fef917306..f3a1e8253 100644 --- a/app/assets/javascripts/app/controllers/home.js +++ b/app/assets/javascripts/app/controllers/home.js @@ -226,7 +226,7 @@ angular.module('app') this.$apply(fn); }; - $scope.notifyDelete = function() { + $rootScope.notifyDelete = function() { $timeout(function() { $rootScope.$broadcast("noteDeleted"); }.bind(this), 0); @@ -242,7 +242,7 @@ angular.module('app') if(note.dummy) { modelManager.removeItemLocally(note); - $scope.notifyDelete(); + $rootScope.notifyDelete(); return; } @@ -250,11 +250,11 @@ angular.module('app') if(authManager.offline()) { // when deleting items while ofline, we need to explictly tell angular to refresh UI setTimeout(function () { - $scope.notifyDelete(); + $rootScope.notifyDelete(); $scope.safeApply(); }, 50); } else { - $scope.notifyDelete(); + $rootScope.notifyDelete(); } }, null, "deleteNote"); } diff --git a/app/assets/javascripts/app/controllers/notes.js b/app/assets/javascripts/app/controllers/notes.js index 138efb42a..c9e5478c2 100644 --- a/app/assets/javascripts/app/controllers/notes.js +++ b/app/assets/javascripts/app/controllers/notes.js @@ -89,7 +89,7 @@ angular.module('app') this.onNoteRemoval = function() { let visibleNotes = this.visibleNotes(); if(this.selectedIndex < visibleNotes.length) { - this.selectNote(visibleNotes[this.selectedIndex]); + this.selectNote(visibleNotes[Math.max(this.selectedIndex, 0)]); } else { this.selectNote(visibleNotes[visibleNotes.length - 1]); } @@ -190,11 +190,14 @@ angular.module('app') } this.selectNote = function(note, viaClick = false) { - if(!note) { return; } + if(!note) { + this.createNewNote(); + return; + } this.selectedNote = note; note.conflict_of = null; // clear conflict this.selectionMade()(note); - this.selectedIndex = this.visibleNotes().indexOf(note); + this.selectedIndex = Math.max(this.visibleNotes().indexOf(note), 0); if(viaClick && this.isFiltering()) { desktopManager.searchText(this.noteFilter.text); diff --git a/app/assets/javascripts/app/services/modelManager.js b/app/assets/javascripts/app/services/modelManager.js index 3fc57cca4..3ba84cf01 100644 --- a/app/assets/javascripts/app/services/modelManager.js +++ b/app/assets/javascripts/app/services/modelManager.js @@ -10,6 +10,14 @@ class ModelManager { ModelManager.MappingSourceRemoteActionRetrieved = "MappingSourceRemoteActionRetrieved"; /* aciton-based Extensions like note history */ ModelManager.MappingSourceFileImport = "MappingSourceFileImport"; + ModelManager.isMappingSourceRetrieved = (source) => { + return [ + ModelManager.MappingSourceRemoteRetrieved, + ModelManager.MappingSourceComponentRetrieved, + ModelManager.MappingSourceRemoteActionRetrieved + ].includes(source); + } + this.storageManager = storageManager; this.notes = []; this.tags = [];