Notebook Snapshot menu is only updating section and page names for the editor's view #3982 (#4002)

* if default section/page is missing then clear default notebook and hide option from dropdown.

* handle edge case when section is null/undefined.

* refactored notebook localstorage data + fixed some edge cases when default section/page gets deleted.

Co-authored-by: Shefali Joshi <simplyrender@gmail.com>
This commit is contained in:
Nikhil
2021-08-12 13:29:01 -07:00
committed by GitHub
parent 9f4190f781
commit 359e7377ac
12 changed files with 291 additions and 211 deletions

View File

@ -72,7 +72,7 @@
<script> <script>
import LayoutFrame from './LayoutFrame.vue'; import LayoutFrame from './LayoutFrame.vue';
import conditionalStylesMixin from "../mixins/objectStyles-mixin"; import conditionalStylesMixin from "../mixins/objectStyles-mixin";
import { getDefaultNotebook } from '@/plugins/notebook/utils/notebook-storage.js'; import { getDefaultNotebook, getNotebookSectionAndPage } from '@/plugins/notebook/utils/notebook-storage.js';
const DEFAULT_TELEMETRY_DIMENSIONS = [10, 5]; const DEFAULT_TELEMETRY_DIMENSIONS = [10, 5];
const DEFAULT_POSITION = [1, 1]; const DEFAULT_POSITION = [1, 1];
@ -336,12 +336,15 @@ export default {
}, },
async getContextMenuActions() { async getContextMenuActions() {
const defaultNotebook = getDefaultNotebook(); const defaultNotebook = getDefaultNotebook();
const domainObject = defaultNotebook && await this.openmct.objects.get(defaultNotebook.notebookMeta.identifier);
let defaultNotebookName; let defaultNotebookName;
if (defaultNotebook) { if (defaultNotebook) {
const defaultPath = domainObject && `${domainObject.name} - ${defaultNotebook.section.name} - ${defaultNotebook.page.name}`; const domainObject = await this.openmct.objects.get(defaultNotebook.identifier);
defaultNotebookName = `Copy to Notebook ${defaultPath}`; const { section, page } = getNotebookSectionAndPage(domainObject, defaultNotebook.defaultSectionId, defaultNotebook.defaultPageId);
if (section && page) {
const defaultPath = domainObject && `${domainObject.name} - ${section.name} - ${page.name}`;
defaultNotebookName = `Copy to Notebook ${defaultPath}`;
}
} }
return CONTEXT_MENU_ACTIONS return CONTEXT_MENU_ACTIONS

View File

@ -1,4 +1,4 @@
import { getDefaultNotebook } from '../utils/notebook-storage'; import { getDefaultNotebook, getNotebookSectionAndPage } from '../utils/notebook-storage';
import { addNotebookEntry } from '../utils/notebook-entries'; import { addNotebookEntry } from '../utils/notebook-entries';
export default class CopyToNotebookAction { export default class CopyToNotebookAction {
@ -15,11 +15,16 @@ export default class CopyToNotebookAction {
copyToNotebook(entryText) { copyToNotebook(entryText) {
const notebookStorage = getDefaultNotebook(); const notebookStorage = getDefaultNotebook();
this.openmct.objects.get(notebookStorage.notebookMeta.identifier) this.openmct.objects.get(notebookStorage.identifier)
.then(domainObject => { .then(domainObject => {
addNotebookEntry(this.openmct, domainObject, notebookStorage, null, entryText); addNotebookEntry(this.openmct, domainObject, notebookStorage, null, entryText);
const defaultPath = `${domainObject.name} - ${notebookStorage.section.name} - ${notebookStorage.page.name}`; const { section, page } = getNotebookSectionAndPage(domainObject, notebookStorage.defaultSectionId, notebookStorage.defaultPageId);
if (!section || !page) {
return;
}
const defaultPath = `${domainObject.name} - ${section.name} - ${page.name}`;
const msg = `Saved to Notebook ${defaultPath}`; const msg = `Saved to Notebook ${defaultPath}`;
this.openmct.notifications.info(msg); this.openmct.notifications.info(msg);
}); });

View File

@ -43,14 +43,16 @@
class="c-notebook__nav c-sidebar c-drawer c-drawer--align-left" class="c-notebook__nav c-sidebar c-drawer c-drawer--align-left"
:class="[{'is-expanded': showNav}, {'c-drawer--push': !sidebarCoversEntries}, {'c-drawer--overlays': sidebarCoversEntries}]" :class="[{'is-expanded': showNav}, {'c-drawer--push': !sidebarCoversEntries}, {'c-drawer--overlays': sidebarCoversEntries}]"
:default-page-id="defaultPageId" :default-page-id="defaultPageId"
:selected-page-id="selectedPageId" :selected-page-id="getSelectedPageId()"
:default-section-id="defaultSectionId" :default-section-id="defaultSectionId"
:selected-section-id="selectedSectionId" :selected-section-id="getSelectedSectionId()"
:domain-object="domainObject" :domain-object="domainObject"
:page-title="domainObject.configuration.pageTitle" :page-title="domainObject.configuration.pageTitle"
:section-title="domainObject.configuration.sectionTitle" :section-title="domainObject.configuration.sectionTitle"
:sections="sections" :sections="sections"
:sidebar-covers-entries="sidebarCoversEntries" :sidebar-covers-entries="sidebarCoversEntries"
@defaultPageDeleted="cleanupDefaultNotebook"
@defaultSectionDeleted="cleanupDefaultNotebook"
@pagesChanged="pagesChanged" @pagesChanged="pagesChanged"
@selectPage="selectPage" @selectPage="selectPage"
@sectionsChanged="sectionsChanged" @sectionsChanged="sectionsChanged"
@ -136,7 +138,7 @@ import NotebookEntry from './NotebookEntry.vue';
import Search from '@/ui/components/search.vue'; import Search from '@/ui/components/search.vue';
import SearchResults from './SearchResults.vue'; import SearchResults from './SearchResults.vue';
import Sidebar from './Sidebar.vue'; import Sidebar from './Sidebar.vue';
import { clearDefaultNotebook, getDefaultNotebook, setDefaultNotebook, setDefaultNotebookSection, setDefaultNotebookPage } from '../utils/notebook-storage'; import { clearDefaultNotebook, getDefaultNotebook, setDefaultNotebook, setDefaultNotebookSectionId, setDefaultNotebookPageId } from '../utils/notebook-storage';
import { addNotebookEntry, createNewEmbed, getEntryPosById, getNotebookEntries, mutateObject } from '../utils/notebook-entries'; import { addNotebookEntry, createNewEmbed, getEntryPosById, getNotebookEntries, mutateObject } from '../utils/notebook-entries';
import { NOTEBOOK_VIEW_TYPE } from '../notebook-constants'; import { NOTEBOOK_VIEW_TYPE } from '../notebook-constants';
import objectUtils from 'objectUtils'; import objectUtils from 'objectUtils';
@ -164,8 +166,10 @@ export default {
}, },
data() { data() {
return { return {
selectedSectionId: this.getDefaultSectionId(), defaultPageId: this.getDefaultPageId(),
selectedPageId: this.getDefaultPageId(), defaultSectionId: this.getDefaultSectionId(),
selectedSectionId: this.getSelectedSectionId(),
selectedPageId: this.getSelectedPageId(),
defaultSort: this.domainObject.configuration.defaultSort, defaultSort: this.domainObject.configuration.defaultSort,
focusEntryId: null, focusEntryId: null,
search: '', search: '',
@ -176,12 +180,6 @@ export default {
}; };
}, },
computed: { computed: {
defaultPageId() {
return this.getDefaultPageId();
},
defaultSectionId() {
return this.getDefaultSectionId();
},
filteredAndSortedEntries() { filteredAndSortedEntries() {
const filterTime = Date.now(); const filterTime = Date.now();
const pageEntries = getNotebookEntries(this.domainObject, this.selectedSection, this.selectedPage) || []; const pageEntries = getNotebookEntries(this.domainObject, this.selectedSection, this.selectedPage) || [];
@ -203,24 +201,38 @@ export default {
}, },
selectedPage() { selectedPage() {
const pages = this.getPages(); const pages = this.getPages();
const selectedPage = pages.find(page => page.id === this.selectedPageId); if (!pages.length) {
return undefined;
}
const selectedPage = pages.find(page => page.id === this.selectedPageId);
if (selectedPage) { if (selectedPage) {
return selectedPage; return selectedPage;
} }
if (!selectedPage && !pages.length) { const defaultPage = pages.find(page => page.id === this.defaultPageId);
return undefined; if (defaultPage) {
return defaultPage;
} }
return pages[0]; return this.pages[0];
}, },
selectedSection() { selectedSection() {
if (!this.sections.length) { if (!this.sections.length) {
return null; return undefined;
} }
return this.sections.find(section => section.id === this.selectedSectionId); const selectedSection = this.sections.find(section => section.id === this.selectedSectionId);
if (selectedSection) {
return selectedSection;
}
const defaultSection = this.sections.find(section => section.id === this.defaultSectionId);
if (defaultSection) {
return defaultSection;
}
return this.sections[0];
} }
}, },
watch: { watch: {
@ -301,26 +313,29 @@ export default {
this.sectionsChanged({ sections }); this.sectionsChanged({ sections });
this.resetSearch(); this.resetSearch();
}, },
cleanupDefaultNotebook() {
this.defaultPageId = undefined;
this.defaultSectionId = undefined;
this.removeDefaultClass(this.domainObject);
clearDefaultNotebook();
},
setSectionAndPageFromUrl() { setSectionAndPageFromUrl() {
let sectionId = this.getSectionIdFromUrl() || this.selectedSectionId; let sectionId = this.getSectionIdFromUrl() || this.getDefaultSectionId() || this.getSelectedSectionId();
let pageId = this.getPageIdFromUrl() || this.selectedPageId; let pageId = this.getPageIdFromUrl() || this.getDefaultPageId() || this.getSelectedPageId();
this.selectSection(sectionId); this.selectSection(sectionId);
this.selectPage(pageId); this.selectPage(pageId);
}, },
createNotebookStorageObject() { createNotebookStorageObject() {
const notebookMeta = {
name: this.domainObject.name,
identifier: this.domainObject.identifier,
link: this.getLinktoNotebook()
};
const page = this.selectedPage; const page = this.selectedPage;
const section = this.selectedSection; const section = this.selectedSection;
return { return {
notebookMeta, name: this.domainObject.name,
page, identifier: this.domainObject.identifier,
section link: this.getLinktoNotebook(),
defaultSectionId: section.id,
defaultPageId: page.id
}; };
}, },
deleteEntry(entryId) { deleteEntry(entryId) {
@ -419,35 +434,21 @@ export default {
this.sidebarCoversEntries = sidebarCoversEntries; this.sidebarCoversEntries = sidebarCoversEntries;
}, },
getDefaultPageId() { getDefaultPageId() {
let defaultPageId; return this.isDefaultNotebook()
? getDefaultNotebook().defaultPageId
if (this.isDefaultNotebook()) { : undefined;
defaultPageId = getDefaultNotebook().page.id;
} else {
const firstSection = this.getSections()[0];
defaultPageId = firstSection && firstSection.pages[0].id;
}
return defaultPageId;
}, },
isDefaultNotebook() { isDefaultNotebook() {
const defaultNotebook = getDefaultNotebook(); const defaultNotebook = getDefaultNotebook();
const defaultNotebookIdentifier = defaultNotebook && defaultNotebook.notebookMeta.identifier; const defaultNotebookIdentifier = defaultNotebook && defaultNotebook.identifier;
return defaultNotebookIdentifier !== null return defaultNotebookIdentifier !== null
&& this.openmct.objects.areIdsEqual(defaultNotebookIdentifier, this.domainObject.identifier); && this.openmct.objects.areIdsEqual(defaultNotebookIdentifier, this.domainObject.identifier);
}, },
getDefaultSectionId() { getDefaultSectionId() {
let defaultSectionId; return this.isDefaultNotebook()
? getDefaultNotebook().defaultSectionId
if (this.isDefaultNotebook()) { : undefined;
defaultSectionId = getDefaultNotebook().section.id;
} else {
const firstSection = this.getSections()[0];
defaultSectionId = firstSection && firstSection.id;
}
return defaultSectionId;
}, },
getDefaultNotebookObject() { getDefaultNotebookObject() {
const oldNotebookStorage = getDefaultNotebook(); const oldNotebookStorage = getDefaultNotebook();
@ -455,7 +456,7 @@ export default {
return null; return null;
} }
return this.openmct.objects.get(oldNotebookStorage.notebookMeta.identifier); return this.openmct.objects.get(oldNotebookStorage.identifier);
}, },
getLinktoNotebook() { getLinktoNotebook() {
const objectPath = this.openmct.router.path; const objectPath = this.openmct.router.path;
@ -573,6 +574,22 @@ export default {
return selectedSection.pages; return selectedSection.pages;
}, },
getSelectedPageId() {
const page = this.selectedPage;
if (!page) {
return undefined;
}
return page.id;
},
getSelectedSectionId() {
const section = this.selectedSection;
if (!section) {
return undefined;
}
return section.id;
},
newEntry(embed = null) { newEntry(embed = null) {
this.resetSearch(); this.resetSearch();
const notebookStorage = this.createNotebookStorageObject(); const notebookStorage = this.createNotebookStorageObject();
@ -616,51 +633,26 @@ export default {
}, },
async updateDefaultNotebook(notebookStorage) { async updateDefaultNotebook(notebookStorage) {
const defaultNotebookObject = await this.getDefaultNotebookObject(); const defaultNotebookObject = await this.getDefaultNotebookObject();
if (!defaultNotebookObject) { const isSameNotebook = defaultNotebookObject
setDefaultNotebook(this.openmct, notebookStorage, this.domainObject); && objectUtils.makeKeyString(defaultNotebookObject.identifier) === objectUtils.makeKeyString(notebookStorage.identifier);
} else if (objectUtils.makeKeyString(defaultNotebookObject.identifier) !== objectUtils.makeKeyString(notebookStorage.notebookMeta.identifier)) { if (!isSameNotebook) {
this.removeDefaultClass(defaultNotebookObject); this.removeDefaultClass(defaultNotebookObject);
}
if (!defaultNotebookObject || !isSameNotebook) {
setDefaultNotebook(this.openmct, notebookStorage, this.domainObject); setDefaultNotebook(this.openmct, notebookStorage, this.domainObject);
} }
if (this.defaultSectionId && this.defaultSectionId.length === 0 || this.defaultSectionId !== notebookStorage.section.id) { if (this.defaultSectionId !== notebookStorage.defaultSectionId) {
this.defaultSectionId = notebookStorage.section.id; setDefaultNotebookSectionId(notebookStorage.defaultSectionId);
setDefaultNotebookSection(notebookStorage.section); this.defaultSectionId = notebookStorage.defaultSectionId;
} }
if (this.defaultPageId && this.defaultPageId.length === 0 || this.defaultPageId !== notebookStorage.page.id) { if (this.defaultPageId !== notebookStorage.defaultPageId) {
this.defaultPageId = notebookStorage.page.id; setDefaultNotebookPageId(notebookStorage.defaultPageId);
setDefaultNotebookPage(notebookStorage.page); this.defaultPageId = notebookStorage.defaultPageId;
} }
}, },
updateDefaultNotebookPage(pages, id) {
if (!id) {
return;
}
const notebookStorage = getDefaultNotebook();
if (!notebookStorage
|| notebookStorage.notebookMeta.identifier.key !== this.domainObject.identifier.key) {
return;
}
const defaultNotebookPage = notebookStorage.page;
const page = pages.find(p => p.id === id);
if (!page && defaultNotebookPage.id === id) {
this.defaultSectionId = null;
this.defaultPageId = null;
this.removeDefaultClass(this.domainObject);
clearDefaultNotebook();
return;
}
if (id !== defaultNotebookPage.id) {
return;
}
setDefaultNotebookPage(page);
},
updateDefaultNotebookSection(sections, id) { updateDefaultNotebookSection(sections, id) {
if (!id) { if (!id) {
return; return;
@ -668,26 +660,26 @@ export default {
const notebookStorage = getDefaultNotebook(); const notebookStorage = getDefaultNotebook();
if (!notebookStorage if (!notebookStorage
|| notebookStorage.notebookMeta.identifier.key !== this.domainObject.identifier.key) { || notebookStorage.identifier.key !== this.domainObject.identifier.key) {
return; return;
} }
const defaultNotebookSection = notebookStorage.section; const defaultNotebookSectionId = notebookStorage.defaultSectionId;
const section = sections.find(s => s.id === id); if (defaultNotebookSectionId === id) {
if (!section && defaultNotebookSection.id === id) { const section = sections.find(s => s.id === id);
this.defaultSectionId = null; if (!section) {
this.defaultPageId = null; this.removeDefaultClass(this.domainObject);
this.removeDefaultClass(this.domainObject); clearDefaultNotebook();
clearDefaultNotebook();
return;
}
}
if (id !== defaultNotebookSectionId) {
return; return;
} }
if (id !== defaultNotebookSection.id) { setDefaultNotebookSectionId(defaultNotebookSectionId);
return;
}
setDefaultNotebookSection(section);
}, },
updateEntry(entry) { updateEntry(entry) {
const entries = getNotebookEntries(this.domainObject, this.selectedSection, this.selectedPage); const entries = getNotebookEntries(this.domainObject, this.selectedSection, this.selectedPage);
@ -715,19 +707,27 @@ export default {
sectionId: this.selectedSectionId sectionId: this.selectedSectionId
}); });
}, },
sectionsChanged({ sections, id = null }) { sectionsChanged({ sections, id = undefined }) {
mutateObject(this.openmct, this.domainObject, 'configuration.sections', sections); mutateObject(this.openmct, this.domainObject, 'configuration.sections', sections);
this.updateDefaultNotebookSection(sections, id); this.updateDefaultNotebookSection(sections, id);
}, },
selectPage(pageId) { selectPage(pageId) {
if (!pageId) {
return;
}
this.selectedPageId = pageId; this.selectedPageId = pageId;
this.syncUrlWithPageAndSection(); this.syncUrlWithPageAndSection();
}, },
selectSection(sectionId) { selectSection(sectionId) {
if (!sectionId) {
return;
}
this.selectedSectionId = sectionId; this.selectedSectionId = sectionId;
const defaultPageId = this.selectedSection.pages[0].id; const pageId = this.selectedSection.pages[0].id;
this.selectPage(defaultPageId); this.selectPage(pageId);
this.syncUrlWithPageAndSection(); this.syncUrlWithPageAndSection();
} }

View File

@ -17,7 +17,7 @@
<script> <script>
import Snapshot from '../snapshot'; import Snapshot from '../snapshot';
import { getDefaultNotebook, validateNotebookStorageObject } from '../utils/notebook-storage'; import { getDefaultNotebook, getNotebookSectionAndPage, validateNotebookStorageObject } from '../utils/notebook-storage';
import { NOTEBOOK_DEFAULT, NOTEBOOK_SNAPSHOT } from '../notebook-constants'; import { NOTEBOOK_DEFAULT, NOTEBOOK_SNAPSHOT } from '../notebook-constants';
export default { export default {
@ -56,11 +56,10 @@ export default {
this.setDefaultNotebookStatus(); this.setDefaultNotebookStatus();
}, },
methods: { methods: {
async getDefaultNotebookObject() { getDefaultNotebookObject() {
const defaultNotebook = getDefaultNotebook(); const defaultNotebook = getDefaultNotebook();
const defaultNotebookObject = defaultNotebook && await this.openmct.objects.get(defaultNotebook.notebookMeta.identifier);
return defaultNotebookObject; return defaultNotebook && this.openmct.objects.get(defaultNotebook.identifier);
}, },
async showMenu(event) { async showMenu(event) {
const notebookTypes = []; const notebookTypes = [];
@ -70,20 +69,22 @@ export default {
const defaultNotebookObject = await this.getDefaultNotebookObject(); const defaultNotebookObject = await this.getDefaultNotebookObject();
if (defaultNotebookObject) { if (defaultNotebookObject) {
const name = defaultNotebookObject.name;
const defaultNotebook = getDefaultNotebook(); const defaultNotebook = getDefaultNotebook();
const sectionName = defaultNotebook.section.name; const { section, page } = getNotebookSectionAndPage(defaultNotebookObject, defaultNotebook.defaultSectionId, defaultNotebook.defaultPageId);
const pageName = defaultNotebook.page.name; if (section && page) {
const defaultPath = `${name} - ${sectionName} - ${pageName}`; const name = defaultNotebookObject.name;
const sectionName = section.name;
const pageName = page.name;
const defaultPath = `${name} - ${sectionName} - ${pageName}`;
notebookTypes.push({ notebookTypes.push({
cssClass: 'icon-notebook', cssClass: 'icon-notebook',
name: `Save to Notebook ${defaultPath}`, name: `Save to Notebook ${defaultPath}`,
onItemClicked: () => { onItemClicked: () => {
return this.snapshot(NOTEBOOK_DEFAULT); return this.snapshot(NOTEBOOK_DEFAULT);
} }
}); });
}
} }
notebookTypes.push({ notebookTypes.push({
@ -119,9 +120,8 @@ export default {
}, },
setDefaultNotebookStatus() { setDefaultNotebookStatus() {
let defaultNotebookObject = getDefaultNotebook(); let defaultNotebookObject = getDefaultNotebook();
if (defaultNotebookObject) {
if (defaultNotebookObject && defaultNotebookObject.notebookMeta) { let notebookIdentifier = defaultNotebookObject.identifier;
let notebookIdentifier = defaultNotebookObject.notebookMeta.identifier;
this.openmct.status.set(notebookIdentifier, 'notebook-default'); this.openmct.status.set(notebookIdentifier, 'notebook-default');
} }

View File

@ -87,22 +87,26 @@ export default {
const selectedPage = this.pages.find(p => p.isSelected); const selectedPage = this.pages.find(p => p.isSelected);
const defaultNotebook = getDefaultNotebook(); const defaultNotebook = getDefaultNotebook();
const defaultpage = defaultNotebook && defaultNotebook.page; const defaultPageId = defaultNotebook && defaultNotebook.defaultPageId;
const isPageSelected = selectedPage && selectedPage.id === id; const isPageSelected = selectedPage && selectedPage.id === id;
const isPageDefault = defaultpage && defaultpage.id === id; const isPageDefault = defaultPageId === id;
const pages = this.pages.filter(s => s.id !== id); const pages = this.pages.filter(s => s.id !== id);
let selectedPageId; let selectedPageId;
if (isPageSelected && defaultpage) { if (isPageSelected && defaultPageId) {
pages.forEach(s => { pages.forEach(s => {
s.isSelected = false; s.isSelected = false;
if (defaultpage && defaultpage.id === s.id) { if (defaultPageId === s.id) {
selectedPageId = s.id; selectedPageId = s.id;
} }
}); });
} }
if (pages.length && isPageSelected && (!defaultpage || isPageDefault)) { if (isPageDefault) {
this.$emit('defaultPageDeleted');
}
if (pages.length && isPageSelected && (!defaultPageId || isPageDefault)) {
selectedPageId = pages[0].id; selectedPageId = pages[0].id;
} }

View File

@ -75,21 +75,25 @@ export default {
const selectedSection = this.sections.find(s => s.id === this.selectedSectionId); const selectedSection = this.sections.find(s => s.id === this.selectedSectionId);
const defaultNotebook = getDefaultNotebook(); const defaultNotebook = getDefaultNotebook();
const defaultSection = defaultNotebook && defaultNotebook.section; const defaultSectionId = defaultNotebook && defaultNotebook.defaultSectionId;
const isSectionSelected = selectedSection && selectedSection.id === id; const isSectionSelected = selectedSection && selectedSection.id === id;
const isSectionDefault = defaultSection && defaultSection.id === id; const isSectionDefault = defaultSectionId === id;
const sections = this.sections.filter(s => s.id !== id); const sections = this.sections.filter(s => s.id !== id);
if (isSectionSelected && defaultSection) { if (isSectionSelected && defaultSectionId) {
sections.forEach(s => { sections.forEach(s => {
s.isSelected = false; s.isSelected = false;
if (defaultSection && defaultSection.id === s.id) { if (defaultSectionId === s.id) {
s.isSelected = true; s.isSelected = true;
} }
}); });
} }
if (sections.length && isSectionSelected && (!defaultSection || isSectionDefault)) { if (isSectionDefault) {
this.$emit('defaultSectionDeleted');
}
if (sections.length && isSectionSelected && (!defaultSectionId || isSectionDefault)) {
sections[0].isSelected = true; sections[0].isSelected = true;
} }

View File

@ -19,6 +19,7 @@
:domain-object="domainObject" :domain-object="domainObject"
:sections="sections" :sections="sections"
:section-title="sectionTitle" :section-title="sectionTitle"
@defaultSectionDeleted="defaultSectionDeleted"
@updateSection="sectionsChanged" @updateSection="sectionsChanged"
@selectSection="selectSection" @selectSection="selectSection"
/> />
@ -50,6 +51,7 @@
:sections="sections" :sections="sections"
:sidebar-covers-entries="sidebarCoversEntries" :sidebar-covers-entries="sidebarCoversEntries"
:page-title="pageTitle" :page-title="pageTitle"
@defaultPageDeleted="defaultPageDeleted"
@toggleNav="toggleNav" @toggleNav="toggleNav"
@updatePage="pagesChanged" @updatePage="pagesChanged"
@selectPage="selectPage" @selectPage="selectPage"
@ -218,6 +220,12 @@ export default {
sectionTitle sectionTitle
}; };
}, },
defaultPageDeleted() {
this.$emit('defaultPageDeleted');
},
defaultSectionDeleted() {
this.$emit('defaultSectionDeleted');
},
toggleNav() { toggleNav() {
this.$emit('toggleNav'); this.$emit('toggleNav');
}, },

View File

@ -1,5 +1,5 @@
import { addNotebookEntry, createNewEmbed } from './utils/notebook-entries'; import { addNotebookEntry, createNewEmbed } from './utils/notebook-entries';
import { getDefaultNotebook, getDefaultNotebookLink, setDefaultNotebook } from './utils/notebook-storage'; import { getDefaultNotebook, getNotebookSectionAndPage, getDefaultNotebookLink, setDefaultNotebook } from './utils/notebook-storage';
import { NOTEBOOK_DEFAULT } from '@/plugins/notebook/notebook-constants'; import { NOTEBOOK_DEFAULT } from '@/plugins/notebook/notebook-constants';
import { createNotebookImageDomainObject, DEFAULT_SIZE } from './utils/notebook-image'; import { createNotebookImageDomainObject, DEFAULT_SIZE } from './utils/notebook-image';
@ -58,20 +58,25 @@ export default class Snapshot {
*/ */
_saveToDefaultNoteBook(embed) { _saveToDefaultNoteBook(embed) {
const notebookStorage = getDefaultNotebook(); const notebookStorage = getDefaultNotebook();
this.openmct.objects.get(notebookStorage.notebookMeta.identifier) this.openmct.objects.get(notebookStorage.identifier)
.then(async (domainObject) => { .then(async (domainObject) => {
addNotebookEntry(this.openmct, domainObject, notebookStorage, embed); addNotebookEntry(this.openmct, domainObject, notebookStorage, embed);
let link = notebookStorage.notebookMeta.link; let link = notebookStorage.link;
// Backwards compatibility fix (old notebook model without link) // Backwards compatibility fix (old notebook model without link)
if (!link) { if (!link) {
link = await getDefaultNotebookLink(this.openmct, domainObject); link = await getDefaultNotebookLink(this.openmct, domainObject);
notebookStorage.notebookMeta.link = link; notebookStorage.link = link;
setDefaultNotebook(this.openmct, notebookStorage); setDefaultNotebook(this.openmct, notebookStorage);
} }
const defaultPath = `${domainObject.name} - ${notebookStorage.section.name} - ${notebookStorage.page.name}`; const { section, page } = getNotebookSectionAndPage(domainObject, notebookStorage.defaultSectionId, notebookStorage.defaultPageId);
if (!section || !page) {
return;
}
const defaultPath = `${domainObject.name} - ${section.name} - ${page.name}`;
const msg = `Saved to Notebook ${defaultPath}`; const msg = `Saved to Notebook ${defaultPath}`;
this._showNotification(msg, link); this._showNotification(msg, link);
}); });

View File

@ -9,24 +9,24 @@ const TIME_BOUNDS = {
}; };
export function addEntryIntoPage(notebookStorage, entries, entry) { export function addEntryIntoPage(notebookStorage, entries, entry) {
const defaultSection = notebookStorage.section; const defaultSectionId = notebookStorage.defaultSectionId;
const defaultPage = notebookStorage.page; const defaultPageId = notebookStorage.defaultPageId;
if (!defaultSection || !defaultPage) { if (!defaultSectionId || !defaultPageId) {
return; return;
} }
const newEntries = JSON.parse(JSON.stringify(entries)); const newEntries = JSON.parse(JSON.stringify(entries));
let section = newEntries[defaultSection.id]; let section = newEntries[defaultSectionId];
if (!section) { if (!section) {
newEntries[defaultSection.id] = {}; newEntries[defaultSectionId] = {};
} }
let page = newEntries[defaultSection.id][defaultPage.id]; let page = newEntries[defaultSectionId][defaultPageId];
if (!page) { if (!page) {
newEntries[defaultSection.id][defaultPage.id] = []; newEntries[defaultSectionId][defaultPageId] = [];
} }
newEntries[defaultSection.id][defaultPage.id].push(entry); newEntries[defaultSectionId][defaultPageId].push(entry);
return newEntries; return newEntries;
} }

View File

@ -23,28 +23,13 @@ import * as NotebookEntries from './notebook-entries';
import { createOpenMct, resetApplicationState } from 'utils/testing'; import { createOpenMct, resetApplicationState } from 'utils/testing';
const notebookStorage = { const notebookStorage = {
notebookMeta: { name: 'notebook',
name: 'notebook', identifier: {
identifier: { namespace: '',
namespace: '', key: 'test-notebook'
key: 'test-notebook'
}
}, },
section: { defaultSectionId: '03a79b6a-971c-4e56-9892-ec536332c3f0',
id: '03a79b6a-971c-4e56-9892-ec536332c3f0', defaultPageId: '8b548fd9-2b8a-4b02-93a9-4138e22eba00'
isDefault: true,
isSelected: true,
name: 'section',
pages: [],
sectionTitle: 'Section'
},
page: {
id: '8b548fd9-2b8a-4b02-93a9-4138e22eba00',
isDefault: true,
isSelected: true,
name: 'page',
pageTitle: 'Page'
}
}; };
const notebookEntries = { const notebookEntries = {

View File

@ -19,18 +19,22 @@ function defaultNotebookObjectChanged(newDomainObject) {
clearDefaultNotebook(); clearDefaultNotebook();
} }
function observeDefaultNotebookObject(openmct, notebookMeta, domainObject) { function observeDefaultNotebookObject(openmct, notebookStorage, domainObject) {
if (currentNotebookObjectIdentifier if (currentNotebookObjectIdentifier
&& objectUtils.makeKeyString(currentNotebookObjectIdentifier) === objectUtils.makeKeyString(notebookMeta.identifier)) { && objectUtils.makeKeyString(currentNotebookObjectIdentifier) === objectUtils.makeKeyString(notebookStorage.identifier)) {
return; return;
} }
removeListener();
unlisten = openmct.objects.observe(domainObject, '*', defaultNotebookObjectChanged);
}
function removeListener() {
if (unlisten) { if (unlisten) {
unlisten(); unlisten();
unlisten = null; unlisten = null;
} }
unlisten = openmct.objects.observe(domainObject, '*', defaultNotebookObjectChanged);
} }
function saveDefaultNotebook(notebookStorage) { function saveDefaultNotebook(notebookStorage) {
@ -39,6 +43,8 @@ function saveDefaultNotebook(notebookStorage) {
export function clearDefaultNotebook() { export function clearDefaultNotebook() {
currentNotebookObjectIdentifier = null; currentNotebookObjectIdentifier = null;
removeListener();
window.localStorage.setItem(NOTEBOOK_LOCAL_STORAGE, null); window.localStorage.setItem(NOTEBOOK_LOCAL_STORAGE, null);
} }
@ -48,6 +54,17 @@ export function getDefaultNotebook() {
return JSON.parse(notebookStorage); return JSON.parse(notebookStorage);
} }
export function getNotebookSectionAndPage(domainObject, sectionId, pageId) {
const configuration = domainObject.configuration;
const section = configuration && configuration.sections.find(s => s.id === sectionId);
const page = section && section.pages.find(p => p.id === pageId);
return {
section,
page
};
}
export async function getDefaultNotebookLink(openmct, domainObject = null) { export async function getDefaultNotebookLink(openmct, domainObject = null) {
if (!domainObject) { if (!domainObject) {
return null; return null;
@ -59,9 +76,9 @@ export async function getDefaultNotebookLink(openmct, domainObject = null) {
.reverse() .reverse()
.join('/') .join('/')
); );
const { page, section } = getDefaultNotebook(); const { defaultPageId, defaultSectionId } = getDefaultNotebook();
return `#/browse/${path}?sectionId=${section.id}&pageId=${page.id}`; return `#/browse/${path}?sectionId=${defaultSectionId}&pageId=${defaultPageId}`;
} }
export function setDefaultNotebook(openmct, notebookStorage, domainObject) { export function setDefaultNotebook(openmct, notebookStorage, domainObject) {
@ -69,15 +86,15 @@ export function setDefaultNotebook(openmct, notebookStorage, domainObject) {
saveDefaultNotebook(notebookStorage); saveDefaultNotebook(notebookStorage);
} }
export function setDefaultNotebookSection(section) { export function setDefaultNotebookSectionId(sectionId) {
const notebookStorage = getDefaultNotebook(); const notebookStorage = getDefaultNotebook();
notebookStorage.section = section; notebookStorage.defaultSectionId = sectionId;
saveDefaultNotebook(notebookStorage); saveDefaultNotebook(notebookStorage);
} }
export function setDefaultNotebookPage(page) { export function setDefaultNotebookPageId(pageId) {
const notebookStorage = getDefaultNotebook(); const notebookStorage = getDefaultNotebook();
notebookStorage.page = page; notebookStorage.defaultPageId = pageId;
saveDefaultNotebook(notebookStorage); saveDefaultNotebook(notebookStorage);
} }

View File

@ -23,37 +23,44 @@
import * as NotebookStorage from './notebook-storage'; import * as NotebookStorage from './notebook-storage';
import { createOpenMct, resetApplicationState } from 'utils/testing'; import { createOpenMct, resetApplicationState } from 'utils/testing';
const notebookSection = {
id: 'temp-section',
isDefault: false,
isSelected: true,
name: 'section',
pages: [
{
id: 'temp-page',
isDefault: false,
isSelected: true,
name: 'page',
pageTitle: 'Page'
}
],
sectionTitle: 'Section'
};
const domainObject = { const domainObject = {
name: 'notebook', name: 'notebook',
identifier: { identifier: {
namespace: '', namespace: '',
key: 'test-notebook' key: 'test-notebook'
},
configuration: {
sections: [
notebookSection
]
} }
}; };
const notebookStorage = { const notebookStorage = {
notebookMeta: { name: 'notebook',
name: 'notebook', identifier: {
identifier: { namespace: '',
namespace: '', key: 'test-notebook'
key: 'test-notebook'
}
}, },
section: { defaultSectionId: 'temp-section',
id: 'temp-section', defaultPageId: 'temp-page'
isDefault: false,
isSelected: true,
name: 'section',
pages: [],
sectionTitle: 'Section'
},
page: {
id: 'temp-page',
isDefault: false,
isSelected: true,
name: 'page',
pageTitle: 'Page'
}
}; };
let openmct; let openmct;
@ -104,7 +111,7 @@ describe('Notebook Storage:', () => {
expect(JSON.stringify(defaultNotebook)).toBe(JSON.stringify(notebookStorage)); expect(JSON.stringify(defaultNotebook)).toBe(JSON.stringify(notebookStorage));
}); });
it('has correct section on setDefaultNotebookSection', () => { it('has correct section on setDefaultNotebookSectionId', () => {
const section = { const section = {
id: 'new-temp-section', id: 'new-temp-section',
isDefault: true, isDefault: true,
@ -115,14 +122,14 @@ describe('Notebook Storage:', () => {
}; };
NotebookStorage.setDefaultNotebook(openmct, notebookStorage, domainObject); NotebookStorage.setDefaultNotebook(openmct, notebookStorage, domainObject);
NotebookStorage.setDefaultNotebookSection(section); NotebookStorage.setDefaultNotebookSectionId(section.id);
const defaultNotebook = NotebookStorage.getDefaultNotebook(); const defaultNotebook = NotebookStorage.getDefaultNotebook();
const newSection = defaultNotebook.section; const defaultSectionId = defaultNotebook.defaultSectionId;
expect(JSON.stringify(section)).toBe(JSON.stringify(newSection)); expect(section.id).toBe(defaultSectionId);
}); });
it('has correct page on setDefaultNotebookPage', () => { it('has correct page on setDefaultNotebookPageId', () => {
const page = { const page = {
id: 'new-temp-page', id: 'new-temp-page',
isDefault: true, isDefault: true,
@ -132,10 +139,52 @@ describe('Notebook Storage:', () => {
}; };
NotebookStorage.setDefaultNotebook(openmct, notebookStorage, domainObject); NotebookStorage.setDefaultNotebook(openmct, notebookStorage, domainObject);
NotebookStorage.setDefaultNotebookPage(page); NotebookStorage.setDefaultNotebookPageId(page.id);
const defaultNotebook = NotebookStorage.getDefaultNotebook(); const defaultNotebook = NotebookStorage.getDefaultNotebook();
const newPage = defaultNotebook.page; const newPageId = defaultNotebook.defaultPageId;
expect(JSON.stringify(page)).toBe(JSON.stringify(newPage)); expect(page.id).toBe(newPageId);
});
describe('is getNotebookSectionAndPage function searches and returns correct,', () => {
let section;
let page;
beforeEach(() => {
const sectionId = 'temp-section';
const pageId = 'temp-page';
const sectionAndpage = NotebookStorage.getNotebookSectionAndPage(domainObject, sectionId, pageId);
section = sectionAndpage.section;
page = sectionAndpage.page;
});
it('id for section from notebook domain object', () => {
expect(section.id).toEqual('temp-section');
});
it('name for section from notebook domain object', () => {
expect(section.name).toEqual('section');
});
it('sectionTitle for section from notebook domain object', () => {
expect(section.sectionTitle).toEqual('Section');
});
it('number of pages for section from notebook domain object', () => {
expect(section.pages.length).toEqual(1);
});
it('id for page from notebook domain object', () => {
expect(page.id).toEqual('temp-page');
});
it('name for page from notebook domain object', () => {
expect(page.name).toEqual('page');
});
it('pageTitle for page from notebook domain object', () => {
expect(page.pageTitle).toEqual('Page');
});
}); });
}); });