| 
									
										
										
										
											2021-12-20 17:30:47 +01:00
										 |  |  | import SwitchWidget from "./switch.js"; | 
					
						
							|  |  |  | import branchService from "../services/branches.js"; | 
					
						
							|  |  |  | import server from "../services/server.js"; | 
					
						
							|  |  |  | import utils from "../services/utils.js"; | 
					
						
							| 
									
										
										
										
											2022-01-10 17:15:21 +01:00
										 |  |  | import syncService from "../services/sync.js"; | 
					
						
							| 
									
										
										
										
											2022-11-25 15:29:57 +01:00
										 |  |  | import dialogService from "../services/dialog.js"; | 
					
						
							| 
									
										
										
										
											2021-12-20 17:30:47 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | export default class SharedSwitchWidget extends SwitchWidget { | 
					
						
							| 
									
										
										
										
											2021-12-24 22:34:15 +01:00
										 |  |  |     isEnabled() { | 
					
						
							| 
									
										
										
										
											2022-12-04 13:16:05 +01:00
										 |  |  |         return super.isEnabled() | 
					
						
							| 
									
										
										
										
											2022-12-21 16:11:00 +01:00
										 |  |  |             && !['root', '_share', '_hidden'].includes(this.noteId); | 
					
						
							| 
									
										
										
										
											2021-12-24 22:34:15 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-20 17:30:47 +01:00
										 |  |  |     doRender() { | 
					
						
							|  |  |  |         super.doRender(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         this.$switchOnName.text("Shared"); | 
					
						
							|  |  |  |         this.$switchOnButton.attr("title", "Share the note"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         this.$switchOffName.text("Shared"); | 
					
						
							|  |  |  |         this.$switchOffButton.attr("title", "Unshare the note"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         this.$helpButton.attr("data-help-page", "Sharing").show(); | 
					
						
							|  |  |  |         this.$helpButton.on('click', e => utils.openHelp(e)); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-10 17:15:21 +01:00
										 |  |  |     async switchOn() { | 
					
						
							| 
									
										
										
										
											2023-04-15 00:06:13 +02:00
										 |  |  |         await branchService.cloneNoteToParentNote(this.noteId, '_share'); | 
					
						
							| 
									
										
										
										
											2022-01-10 17:15:21 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-13 20:22:50 +01:00
										 |  |  |         syncService.syncNow(true); | 
					
						
							| 
									
										
										
										
											2021-12-20 17:30:47 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     async switchOff() { | 
					
						
							| 
									
										
										
										
											2022-12-21 16:11:00 +01:00
										 |  |  |         const shareBranch = this.note.getParentBranches().find(b => b.parentNoteId === '_share'); | 
					
						
							| 
									
										
										
										
											2021-12-20 17:30:47 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |         if (!shareBranch) { | 
					
						
							|  |  |  |             return; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (this.note.getParentBranches().length === 1) { | 
					
						
							|  |  |  |             const text = "This note exists only as a shared note, unsharing would delete it. Do you want to continue and thus delete this note?"; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-06-16 20:19:26 +02:00
										 |  |  |             if (!await dialogService.confirm(text)) { | 
					
						
							| 
									
										
										
										
											2021-12-20 17:30:47 +01:00
										 |  |  |                 return; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-21 16:12:59 +01:00
										 |  |  |         await server.remove(`branches/${shareBranch.branchId}?taskId=no-progress-reporting`); | 
					
						
							| 
									
										
										
										
											2022-01-10 17:15:21 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-13 20:22:50 +01:00
										 |  |  |         syncService.syncNow(true); | 
					
						
							| 
									
										
										
										
											2021-12-20 17:30:47 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     async refreshWithNote(note) { | 
					
						
							| 
									
										
										
										
											2022-12-21 16:11:00 +01:00
										 |  |  |         const isShared = note.hasAncestor('_share'); | 
					
						
							|  |  |  |         const canBeUnshared = isShared && note.getParentBranches().find(b => b.parentNoteId === '_share'); | 
					
						
							| 
									
										
										
										
											2021-12-20 17:30:47 +01:00
										 |  |  |         const switchDisabled = isShared && !canBeUnshared; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         this.$switchOn.toggle(!isShared); | 
					
						
							|  |  |  |         this.$switchOff.toggle(!!isShared); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (switchDisabled) { | 
					
						
							|  |  |  |             this.$widget.attr("title", "Note cannot be unshared here because it is shared through inheritance from an ancestor."); | 
					
						
							|  |  |  |             this.$switchOff.addClass("switch-disabled"); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         else { | 
					
						
							|  |  |  |             this.$widget.removeAttr("title"); | 
					
						
							|  |  |  |             this.$switchOff.removeClass("switch-disabled"); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     entitiesReloadedEvent({loadResults}) { | 
					
						
							| 
									
										
										
										
											2023-06-05 16:12:02 +02:00
										 |  |  |         if (loadResults.getBranchRows().find(b => b.noteId === this.noteId)) { | 
					
						
							| 
									
										
										
										
											2021-12-20 17:30:47 +01:00
										 |  |  |             this.refresh(); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } |