| 
									
										
										
										
											2018-03-25 20:52:38 -04:00
										 |  |  | import noteDetailService from '../services/note_detail.js'; | 
					
						
							|  |  |  | import utils from '../services/utils.js'; | 
					
						
							|  |  |  | import server from '../services/server.js'; | 
					
						
							| 
									
										
										
										
											2019-11-09 15:21:14 +01:00
										 |  |  | import toastService from "../services/toast.js"; | 
					
						
							| 
									
										
										
										
											2018-03-25 20:52:38 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | const $dialog = $("#note-revisions-dialog"); | 
					
						
							|  |  |  | const $list = $("#note-revision-list"); | 
					
						
							|  |  |  | const $content = $("#note-revision-content"); | 
					
						
							|  |  |  | const $title = $("#note-revision-title"); | 
					
						
							| 
									
										
										
										
											2019-11-09 08:53:13 +01:00
										 |  |  | const $titleButtons = $("#note-revision-title-buttons"); | 
					
						
							| 
									
										
										
										
											2019-11-09 15:21:14 +01:00
										 |  |  | const $eraseAllRevisionsButton = $("#note-revisions-erase-all-revisions-button"); | 
					
						
							| 
									
										
										
										
											2018-03-25 20:52:38 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | let revisionItems = []; | 
					
						
							| 
									
										
										
										
											2018-11-14 14:52:05 +01:00
										 |  |  | let note; | 
					
						
							| 
									
										
										
										
											2018-03-25 20:52:38 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-02 19:56:52 +02:00
										 |  |  | export async function showCurrentNoteRevisions() { | 
					
						
							| 
									
										
										
										
											2019-10-20 12:29:34 +02:00
										 |  |  |     await showNoteRevisionsDialog(noteDetailService.getActiveTabNoteId()); | 
					
						
							| 
									
										
										
										
											2018-03-25 20:52:38 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-20 21:40:47 +02:00
										 |  |  | export async function showNoteRevisionsDialog(noteId, noteRevisionId) { | 
					
						
							| 
									
										
										
										
											2019-06-10 22:45:03 +02:00
										 |  |  |     utils.closeActiveDialog(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-25 20:52:38 -04:00
										 |  |  |     glob.activeDialog = $dialog; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-06 17:47:40 +01:00
										 |  |  |     $dialog.modal(); | 
					
						
							| 
									
										
										
										
											2018-03-25 20:52:38 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-11-09 15:21:14 +01:00
										 |  |  |     await loadNoteRevisions(noteId, noteRevisionId); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | async function loadNoteRevisions(noteId, noteRevisionId) { | 
					
						
							| 
									
										
										
										
											2018-03-25 20:52:38 -04:00
										 |  |  |     $list.empty(); | 
					
						
							|  |  |  |     $content.empty(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-20 12:29:34 +02:00
										 |  |  |     note = noteDetailService.getActiveTabNote(); | 
					
						
							| 
									
										
										
										
											2019-11-01 19:21:48 +01:00
										 |  |  |     revisionItems = await server.get(`notes/${noteId}/revisions`); | 
					
						
							| 
									
										
										
										
											2018-03-25 20:52:38 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |     for (const item of revisionItems) { | 
					
						
							|  |  |  |         $list.append($('<option>', { | 
					
						
							|  |  |  |             value: item.noteRevisionId, | 
					
						
							| 
									
										
										
										
											2019-11-09 15:21:14 +01:00
										 |  |  |             text: item.dateLastEdited.substr(0, 16) + (item.isErased ? ' (erased)' : '') | 
					
						
							| 
									
										
										
										
											2018-03-25 20:52:38 -04:00
										 |  |  |         })); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (revisionItems.length > 0) { | 
					
						
							|  |  |  |         if (!noteRevisionId) { | 
					
						
							|  |  |  |             noteRevisionId = $list.find("option:first").val(); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $list.val(noteRevisionId).trigger('change'); | 
					
						
							| 
									
										
										
										
											2019-11-09 15:21:14 +01:00
										 |  |  |     } else { | 
					
						
							| 
									
										
										
										
											2018-03-25 20:52:38 -04:00
										 |  |  |         $title.text("No revisions for this note yet..."); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-11-01 19:21:48 +01:00
										 |  |  | $list.on('change', async () => { | 
					
						
							| 
									
										
										
										
											2018-03-25 20:52:38 -04:00
										 |  |  |     const optVal = $list.find(":selected").val(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     const revisionItem = revisionItems.find(r => r.noteRevisionId === optVal); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-11-09 15:21:14 +01:00
										 |  |  |     $titleButtons.empty(); | 
					
						
							|  |  |  |     $content.empty(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (revisionItem.isErased) { | 
					
						
							|  |  |  |         $title.text('This revision has been erased'); | 
					
						
							|  |  |  |         return; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-25 20:52:38 -04:00
										 |  |  |     $title.html(revisionItem.title); | 
					
						
							| 
									
										
										
										
											2018-04-08 12:13:52 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-11-09 15:21:14 +01:00
										 |  |  |     const $eraseRevisionButton = $('<button class="btn btn-sm" type="button">Erase this revision</button>'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     $eraseRevisionButton.on('click', async () => { | 
					
						
							|  |  |  |         const confirmDialog = await import('../dialogs/confirm.js'); | 
					
						
							|  |  |  |         const text = 'Do you want to erase this revision? This action will erase revision title and content, but still preserve revision metadata.'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (await confirmDialog.confirm(text)) { | 
					
						
							|  |  |  |             await server.remove(`notes/${revisionItem.noteId}/revisions/${revisionItem.noteRevisionId}`); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             loadNoteRevisions(revisionItem.noteId); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             toastService.showMessage('Note revision has been erased.'); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     $titleButtons | 
					
						
							|  |  |  |         .append($eraseRevisionButton) | 
					
						
							|  |  |  |         .append('   '); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-11-09 08:53:13 +01:00
										 |  |  |     const $downloadButton = $('<button class="btn btn-sm btn-primary" type="button">Download</button>'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     $downloadButton.on('click', () => { | 
					
						
							|  |  |  |         utils.download(utils.getHost() + `/api/notes/${revisionItem.noteId}/revisions/${revisionItem.noteRevisionId}/download`); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-11-09 15:21:14 +01:00
										 |  |  |     $titleButtons.append($downloadButton); | 
					
						
							| 
									
										
										
										
											2019-11-09 08:53:13 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-11-01 19:21:48 +01:00
										 |  |  |     const fullNoteRevision = await server.get(`notes/${revisionItem.noteId}/revisions/${revisionItem.noteRevisionId}`); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-11-09 13:01:05 +01:00
										 |  |  |     if (revisionItem.type === 'text') { | 
					
						
							| 
									
										
										
										
											2019-11-01 19:21:48 +01:00
										 |  |  |         $content.html(fullNoteRevision.content); | 
					
						
							| 
									
										
										
										
											2018-04-08 12:13:52 -04:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-11-09 13:01:05 +01:00
										 |  |  |     else if (revisionItem.type === 'code') { | 
					
						
							| 
									
										
										
										
											2019-11-01 19:21:48 +01:00
										 |  |  |         $content.html($("<pre>").text(fullNoteRevision.content)); | 
					
						
							| 
									
										
										
										
											2018-04-08 12:13:52 -04:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-11-09 13:01:05 +01:00
										 |  |  |     else if (revisionItem.type === 'image') { | 
					
						
							| 
									
										
										
										
											2019-11-08 23:09:57 +01:00
										 |  |  |         $content.html($("<img>") | 
					
						
							| 
									
										
										
										
											2019-11-09 08:53:13 +01:00
										 |  |  |             // reason why we put this inline as base64 is that we do not want to let user to copy this
 | 
					
						
							|  |  |  |             // as a URL to be used in a note. Instead if they copy and paste it into a note, it will be a uploaded as a new note
 | 
					
						
							| 
									
										
										
										
											2019-11-08 23:09:57 +01:00
										 |  |  |             .attr("src", `data:${note.mime};base64,` + fullNoteRevision.content) | 
					
						
							|  |  |  |             .css("width", "100%")); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-11-09 13:01:05 +01:00
										 |  |  |     else if (revisionItem.type === 'file') { | 
					
						
							|  |  |  |         const $table = $("<table cellpadding='10'>") | 
					
						
							|  |  |  |             .append($("<tr>").append( | 
					
						
							|  |  |  |                 $("<th>").text("MIME: "), | 
					
						
							|  |  |  |                 $("<td>").text(revisionItem.mime) | 
					
						
							|  |  |  |             )) | 
					
						
							|  |  |  |             .append($("<tr>").append( | 
					
						
							|  |  |  |                 $("<th>").text("File size:"), | 
					
						
							|  |  |  |                 $("<td>").text(revisionItem.contentLength + " bytes") | 
					
						
							|  |  |  |             )); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (fullNoteRevision.content) { | 
					
						
							|  |  |  |             $table.append($("<tr>").append( | 
					
						
							|  |  |  |                 $("<th>").text("Preview:"), | 
					
						
							|  |  |  |                 $("<td>").append( | 
					
						
							|  |  |  |                     $('<pre class="file-preview-content"></pre>') | 
					
						
							|  |  |  |                         .text(fullNoteRevision.content) | 
					
						
							|  |  |  |                 ) | 
					
						
							|  |  |  |             )); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $content.html($table); | 
					
						
							| 
									
										
										
										
											2019-11-09 11:58:52 +01:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-11-14 14:52:05 +01:00
										 |  |  |     else { | 
					
						
							|  |  |  |         $content.text("Preview isn't available for this note type."); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-11-09 15:21:14 +01:00
										 |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | $eraseAllRevisionsButton.on('click', async () => { | 
					
						
							|  |  |  |     const confirmDialog = await import('../dialogs/confirm.js'); | 
					
						
							|  |  |  |     const text = 'Do you want to erase all revision of this note? This action will erase revision title and content, but still preserve revision metadata.'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (await confirmDialog.confirm(text)) { | 
					
						
							|  |  |  |         await server.remove(`notes/${note.noteId}/revisions`); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $dialog.modal('hide'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         toastService.showMessage('Note revisions has been erased.'); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-10-10 20:00:06 +02:00
										 |  |  | }); |