| 
									
										
										
										
											2020-01-18 19:46:30 +01:00
										 |  |  | import TabAwareWidget from "./tab_aware_widget.js"; | 
					
						
							|  |  |  | import treeService from "../services/tree.js"; | 
					
						
							|  |  |  | import linkService from "../services/link.js"; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const TPL = `
 | 
					
						
							| 
									
										
										
										
											2020-04-08 10:13:11 +02:00
										 |  |  | <div class="note-paths-widget"> | 
					
						
							| 
									
										
										
										
											2020-01-18 19:46:30 +01:00
										 |  |  |     <style> | 
					
						
							| 
									
										
										
										
											2020-04-08 10:13:11 +02:00
										 |  |  |     .note-paths-widget { | 
					
						
							|  |  |  |         display: flex;  | 
					
						
							|  |  |  |         flex-direction: row; | 
					
						
							|  |  |  |         border-bottom: 1px solid var(--main-border-color); | 
					
						
							|  |  |  |         padding: 5px 10px 5px 10px; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |      | 
					
						
							| 
									
										
										
										
											2020-01-18 19:46:30 +01:00
										 |  |  |     .note-path-list a.current { | 
					
						
							|  |  |  |         font-weight: bold; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2020-04-07 22:53:03 +02:00
										 |  |  |      | 
					
						
							|  |  |  |     .note-path-list-button { | 
					
						
							|  |  |  |         padding: 0; | 
					
						
							|  |  |  |         width: 24px; | 
					
						
							|  |  |  |         height: 24px; | 
					
						
							|  |  |  |         margin-left: 5px; | 
					
						
							|  |  |  |         position: relative; | 
					
						
							|  |  |  |         top: -2px; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |      | 
					
						
							|  |  |  |     .note-path-list-button::after { | 
					
						
							|  |  |  |         display: none !important; // disabling the standard caret
 | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |      | 
					
						
							|  |  |  |     .current-path { | 
					
						
							|  |  |  |         flex-grow: 1; | 
					
						
							|  |  |  |         white-space: nowrap;  | 
					
						
							|  |  |  |         overflow: hidden;  | 
					
						
							|  |  |  |         text-overflow: ellipsis; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2020-01-18 19:46:30 +01:00
										 |  |  |     </style> | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-07 22:53:03 +02:00
										 |  |  |     <div class="current-path"></div> | 
					
						
							| 
									
										
										
										
											2020-01-18 19:46:30 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-07 22:53:03 +02:00
										 |  |  |     <div class="dropdown hide-in-zen-mode"> | 
					
						
							|  |  |  |         <button class="btn btn-sm dropdown-toggle note-path-list-button bx bx-caret-down" type="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false"></button> | 
					
						
							|  |  |  |         <ul class="note-path-list dropdown-menu dropdown-menu-right" aria-labelledby="note-path-list-button"> | 
					
						
							|  |  |  |         </ul> | 
					
						
							|  |  |  |     </div> | 
					
						
							| 
									
										
										
										
											2020-01-18 19:46:30 +01:00
										 |  |  | </div>`; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | export default class NotePathsWidget extends TabAwareWidget { | 
					
						
							|  |  |  |     doRender() { | 
					
						
							|  |  |  |         this.$widget = $(TPL); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-07 22:53:03 +02:00
										 |  |  |         this.$currentPath = this.$widget.find('.current-path'); | 
					
						
							|  |  |  |         this.$dropdown = this.$widget.find(".dropdown"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         this.$notePathList = this.$dropdown.find(".note-path-list"); | 
					
						
							| 
									
										
										
										
											2020-01-18 19:46:30 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-07 22:53:03 +02:00
										 |  |  |         this.$dropdown.on('show.bs.dropdown', () => this.renderDropdown()); | 
					
						
							| 
									
										
										
										
											2020-02-03 21:16:33 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-18 19:46:30 +01:00
										 |  |  |         return this.$widget; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-19 15:36:42 +01:00
										 |  |  |     async refreshWithNote(note, notePath) { | 
					
						
							| 
									
										
										
										
											2020-04-08 10:19:15 +02:00
										 |  |  |         const noteIdsPath = treeService.parseNotePath(notePath); | 
					
						
							| 
									
										
										
										
											2020-04-07 22:53:03 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         this.$currentPath.empty(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         let parentNoteId = 'root'; | 
					
						
							|  |  |  |         let curPath = ''; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         for (let i = 0; i < noteIdsPath.length; i++) { | 
					
						
							|  |  |  |             const noteId = noteIdsPath[i]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             curPath += (curPath ? '/' : '') + noteId; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-08 10:13:11 +02:00
										 |  |  |             if (noteId !== 'root' || noteIdsPath.length < 3) { | 
					
						
							|  |  |  |                 this.$currentPath.append( | 
					
						
							|  |  |  |                     $("<a>") | 
					
						
							|  |  |  |                         .attr('href', '#' + curPath) | 
					
						
							|  |  |  |                         .addClass('no-tooltip-preview') | 
					
						
							|  |  |  |                         .text(await treeService.getNoteTitle(noteId, parentNoteId)) | 
					
						
							|  |  |  |                 ); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 if (i !== noteIdsPath.length - 1) { | 
					
						
							|  |  |  |                     this.$currentPath.append(' / '); | 
					
						
							|  |  |  |                 } | 
					
						
							| 
									
										
										
										
											2020-04-07 22:53:03 +02:00
										 |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             parentNoteId = noteId; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2020-02-03 21:16:33 +01:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2020-01-18 19:46:30 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-03 21:16:33 +01:00
										 |  |  |     async renderDropdown() { | 
					
						
							|  |  |  |         this.$notePathList.empty(); | 
					
						
							| 
									
										
										
										
											2020-04-08 10:13:11 +02:00
										 |  |  |         this.$notePathList.append( | 
					
						
							|  |  |  |             $("<div>") | 
					
						
							|  |  |  |                 .addClass("dropdown-header") | 
					
						
							|  |  |  |                 .text('This note is placed into the following paths:') | 
					
						
							|  |  |  |         ); | 
					
						
							| 
									
										
										
										
											2020-01-18 19:46:30 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-03 21:16:33 +01:00
										 |  |  |         if (this.noteId === 'root') { | 
					
						
							| 
									
										
										
										
											2020-01-18 19:46:30 +01:00
										 |  |  |             await this.addPath('root', true); | 
					
						
							| 
									
										
										
										
											2020-02-03 21:16:33 +01:00
										 |  |  |             return; | 
					
						
							| 
									
										
										
										
											2020-01-18 19:46:30 +01:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-07 22:53:03 +02:00
										 |  |  |         const pathSegments = treeService.parseNotePath(this.notePath); | 
					
						
							| 
									
										
										
										
											2020-02-03 21:16:33 +01:00
										 |  |  |         const activeNoteParentNoteId = pathSegments[pathSegments.length - 2]; // we know this is not root so there must be a parent
 | 
					
						
							| 
									
										
										
										
											2020-01-18 19:46:30 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-18 22:35:54 +01:00
										 |  |  |         for (const parentNote of this.note.getParentNotes()) { | 
					
						
							| 
									
										
										
										
											2020-02-03 21:16:33 +01:00
										 |  |  |             const parentNotePath = await treeService.getSomeNotePath(parentNote); | 
					
						
							|  |  |  |             // this is to avoid having root notes leading '/'
 | 
					
						
							|  |  |  |             const notePath = parentNotePath ? (parentNotePath + '/' + this.noteId) : this.noteId; | 
					
						
							|  |  |  |             const isCurrent = activeNoteParentNoteId === parentNote.noteId; | 
					
						
							| 
									
										
										
										
											2020-01-18 19:46:30 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-03 21:16:33 +01:00
										 |  |  |             await this.addPath(notePath, isCurrent); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2020-01-18 19:46:30 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-03 21:16:33 +01:00
										 |  |  |         const cloneLink = $("<div>") | 
					
						
							| 
									
										
										
										
											2020-04-08 10:13:11 +02:00
										 |  |  |             .addClass("dropdown-header") | 
					
						
							| 
									
										
										
										
											2020-02-03 21:16:33 +01:00
										 |  |  |             .append( | 
					
						
							|  |  |  |                 $('<button class="btn btn-sm">') | 
					
						
							|  |  |  |                     .text('Clone note to new location...') | 
					
						
							|  |  |  |                     .on('click', () => import("../dialogs/clone_to.js").then(d => d.showDialog([this.noteId]))) | 
					
						
							|  |  |  |             ); | 
					
						
							| 
									
										
										
										
											2020-01-18 19:46:30 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-03 21:16:33 +01:00
										 |  |  |         this.$notePathList.append(cloneLink); | 
					
						
							| 
									
										
										
										
											2020-01-18 19:46:30 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     async addPath(notePath, isCurrent) { | 
					
						
							| 
									
										
										
										
											2020-01-25 09:56:08 +01:00
										 |  |  |         const title = await treeService.getNotePathTitle(notePath); | 
					
						
							| 
									
										
										
										
											2020-01-18 19:46:30 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |         const noteLink = await linkService.createNoteLink(notePath, {title}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         noteLink | 
					
						
							|  |  |  |             .addClass("dropdown-item"); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-03 21:16:33 +01:00
										 |  |  |         noteLink | 
					
						
							|  |  |  |             .find('a') | 
					
						
							|  |  |  |             .addClass("no-tooltip-preview"); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-18 19:46:30 +01:00
										 |  |  |         if (isCurrent) { | 
					
						
							|  |  |  |             noteLink.addClass("current"); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         this.$notePathList.append(noteLink); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2020-02-17 22:47:50 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     entitiesReloadedEvent({loadResults}) { | 
					
						
							|  |  |  |         if (loadResults.getBranches().find(branch => branch.noteId === this.noteId)) { | 
					
						
							|  |  |  |             this.refresh(); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2020-01-18 19:46:30 +01:00
										 |  |  | } |