| 
									
										
										
										
											2018-03-25 13:41:29 -04:00
										 |  |  | import linkService from '../services/link.js'; | 
					
						
							| 
									
										
										
										
											2018-03-25 13:02:39 -04:00
										 |  |  | import utils from '../services/utils.js'; | 
					
						
							| 
									
										
										
										
											2018-03-25 14:49:20 -04:00
										 |  |  | import server from '../services/server.js'; | 
					
						
							| 
									
										
										
										
											2017-11-04 11:32:05 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-25 11:09:17 -04:00
										 |  |  | const $dialog = $("#recent-changes-dialog"); | 
					
						
							| 
									
										
										
										
											2017-11-04 17:03:15 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-25 11:09:17 -04:00
										 |  |  | async function showDialog() { | 
					
						
							|  |  |  |     glob.activeDialog = $dialog; | 
					
						
							| 
									
										
										
										
											2017-11-04 11:32:05 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-25 11:09:17 -04:00
										 |  |  |     $dialog.dialog({ | 
					
						
							|  |  |  |         modal: true, | 
					
						
							|  |  |  |         width: 800, | 
					
						
							|  |  |  |         height: 700 | 
					
						
							|  |  |  |     }); | 
					
						
							| 
									
										
										
										
											2017-11-04 11:32:05 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-25 11:09:17 -04:00
										 |  |  |     const result = await server.get('recent-changes/'); | 
					
						
							| 
									
										
										
										
											2017-11-04 11:32:05 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-25 11:09:17 -04:00
										 |  |  |     $dialog.html(''); | 
					
						
							| 
									
										
										
										
											2017-11-04 11:32:05 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-25 11:09:17 -04:00
										 |  |  |     const groupedByDate = groupByDate(result); | 
					
						
							| 
									
										
										
										
											2017-11-04 11:32:05 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-25 11:09:17 -04:00
										 |  |  |     for (const [dateDay, dayChanges] of groupedByDate) { | 
					
						
							|  |  |  |         const changesListEl = $('<ul>'); | 
					
						
							| 
									
										
										
										
											2017-11-04 11:32:05 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-25 11:09:17 -04:00
										 |  |  |         const dayEl = $('<div>').append($('<b>').html(utils.formatDate(dateDay))).append(changesListEl); | 
					
						
							| 
									
										
										
										
											2017-10-06 22:46:30 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-25 11:09:17 -04:00
										 |  |  |         for (const change of dayChanges) { | 
					
						
							|  |  |  |             const formattedTime = utils.formatTime(utils.parseDate(change.dateModifiedTo)); | 
					
						
							| 
									
										
										
										
											2017-11-04 11:32:05 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-25 11:09:17 -04:00
										 |  |  |             const revLink = $("<a>", { | 
					
						
							|  |  |  |                 href: 'javascript:', | 
					
						
							|  |  |  |                 text: 'rev' | 
					
						
							| 
									
										
										
										
											2018-03-25 20:52:38 -04:00
										 |  |  |             }).attr('action', 'note-revision') | 
					
						
							| 
									
										
										
										
											2018-03-25 11:09:17 -04:00
										 |  |  |                 .attr('note-path', change.noteId) | 
					
						
							| 
									
										
										
										
											2018-03-25 20:52:38 -04:00
										 |  |  |                 .attr('note-revision-id', change.noteRevisionId); | 
					
						
							| 
									
										
										
										
											2017-12-03 17:46:56 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-25 11:09:17 -04:00
										 |  |  |             let noteLink; | 
					
						
							| 
									
										
										
										
											2017-12-03 17:46:56 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-25 11:09:17 -04:00
										 |  |  |             if (change.current_isDeleted) { | 
					
						
							|  |  |  |                 noteLink = change.current_title; | 
					
						
							| 
									
										
										
										
											2017-11-04 13:39:26 -04:00
										 |  |  |             } | 
					
						
							|  |  |  |             else { | 
					
						
							| 
									
										
										
										
											2018-03-25 13:41:29 -04:00
										 |  |  |                 noteLink = linkService.createNoteLink(change.noteId, change.title); | 
					
						
							| 
									
										
										
										
											2017-11-04 13:39:26 -04:00
										 |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-25 11:09:17 -04:00
										 |  |  |             changesListEl.append($('<li>') | 
					
						
							|  |  |  |                 .append(formattedTime + ' - ') | 
					
						
							|  |  |  |                 .append(noteLink) | 
					
						
							|  |  |  |                 .append(' (').append(revLink).append(')')); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $dialog.append(dayEl); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | function groupByDate(result) { | 
					
						
							|  |  |  |     const groupedByDate = new Map(); | 
					
						
							|  |  |  |     const dayCache = {}; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     for (const row of result) { | 
					
						
							|  |  |  |         let dateDay = utils.parseDate(row.dateModifiedTo); | 
					
						
							|  |  |  |         dateDay.setHours(0); | 
					
						
							|  |  |  |         dateDay.setMinutes(0); | 
					
						
							|  |  |  |         dateDay.setSeconds(0); | 
					
						
							|  |  |  |         dateDay.setMilliseconds(0); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // this stupidity is to make sure that we always use the same day object because Map uses only
 | 
					
						
							|  |  |  |         // reference equality
 | 
					
						
							|  |  |  |         if (dayCache[dateDay]) { | 
					
						
							|  |  |  |             dateDay = dayCache[dateDay]; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         else { | 
					
						
							|  |  |  |             dayCache[dateDay] = dateDay; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2017-11-04 13:39:26 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-25 11:09:17 -04:00
										 |  |  |         if (!groupedByDate.has(dateDay)) { | 
					
						
							|  |  |  |             groupedByDate.set(dateDay, []); | 
					
						
							| 
									
										
										
										
											2017-11-04 11:32:05 -04:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2018-03-25 11:09:17 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |         groupedByDate.get(dateDay).push(row); | 
					
						
							| 
									
										
										
										
											2017-11-04 11:32:05 -04:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-03-25 11:09:17 -04:00
										 |  |  |     return groupedByDate; | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2017-11-04 11:32:05 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-25 11:09:17 -04:00
										 |  |  | export default { | 
					
						
							|  |  |  |     showDialog | 
					
						
							|  |  |  | }; |