| 
									
										
										
										
											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'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const $dialog = $("#note-revisions-dialog"); | 
					
						
							|  |  |  | const $list = $("#note-revision-list"); | 
					
						
							|  |  |  | const $content = $("#note-revision-content"); | 
					
						
							|  |  |  | const $title = $("#note-revision-title"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | let revisionItems = []; | 
					
						
							| 
									
										
										
										
											2018-11-14 14:52:05 +01:00
										 |  |  | let note; | 
					
						
							| 
									
										
										
										
											2018-03-25 20:52:38 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | async function showCurrentNoteRevisions() { | 
					
						
							| 
									
										
										
										
											2019-03-14 20:21:27 +01:00
										 |  |  |     await showNoteRevisionsDialog(noteDetailService.getActiveNoteId()); | 
					
						
							| 
									
										
										
										
											2018-03-25 20:52:38 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 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
										 |  |  | 
 | 
					
						
							|  |  |  |     $list.empty(); | 
					
						
							|  |  |  |     $content.empty(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-14 20:21:27 +01:00
										 |  |  |     note = noteDetailService.getActiveNote(); | 
					
						
							| 
									
										
										
										
											2018-04-01 20:33:10 -04: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-04-01 21:49:27 +02:00
										 |  |  |             text: item.dateModifiedFrom | 
					
						
							| 
									
										
										
										
											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'); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     else { | 
					
						
							|  |  |  |         $title.text("No revisions for this note yet..."); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | $list.on('change', () => { | 
					
						
							|  |  |  |     const optVal = $list.find(":selected").val(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     const revisionItem = revisionItems.find(r => r.noteRevisionId === optVal); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     $title.html(revisionItem.title); | 
					
						
							| 
									
										
										
										
											2018-04-08 12:13:52 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-14 14:52:05 +01:00
										 |  |  |     if (note.type === 'text') { | 
					
						
							| 
									
										
										
										
											2018-04-08 12:13:52 -04:00
										 |  |  |         $content.html(revisionItem.content); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-11-14 14:52:05 +01:00
										 |  |  |     else if (note.type === 'code') { | 
					
						
							| 
									
										
										
										
											2018-04-08 12:13:52 -04:00
										 |  |  |         $content.html($("<pre>").text(revisionItem.content)); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-11-14 14:52:05 +01:00
										 |  |  |     else { | 
					
						
							|  |  |  |         $content.text("Preview isn't available for this note type."); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-03-25 20:52:38 -04:00
										 |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-15 10:14:14 +02:00
										 |  |  | $(document).on('click', "a[data-action='note-revision']", event => { | 
					
						
							| 
									
										
										
										
											2018-03-25 20:52:38 -04:00
										 |  |  |     const linkEl = $(event.target); | 
					
						
							| 
									
										
										
										
											2018-08-15 10:14:14 +02:00
										 |  |  |     const noteId = linkEl.attr('data-note-path'); | 
					
						
							|  |  |  |     const noteRevisionId = linkEl.attr('data-note-revision-id'); | 
					
						
							| 
									
										
										
										
											2018-03-25 20:52:38 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |     showNoteRevisionsDialog(noteId, noteRevisionId); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return false; | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | export default { | 
					
						
							|  |  |  |     showCurrentNoteRevisions | 
					
						
							|  |  |  | }; |