| 
									
										
										
										
											2017-10-23 16:50:48 -07:00
										 |  |  | import Component from '@ember/component'; | 
					
						
							|  |  |  | import ComputedProperty from '@ember/object/computed'; | 
					
						
							|  |  |  | import { computed, getProperties, get, set } from '@ember/object'; | 
					
						
							|  |  |  | import { run, schedule } from '@ember/runloop'; | 
					
						
							|  |  |  | import { inject } from '@ember/service'; | 
					
						
							| 
									
										
										
										
											2017-09-10 19:31:54 -07:00
										 |  |  | import { baseCommentEditorOptions } from 'wherehows-web/constants'; | 
					
						
							|  |  |  | import { IDatasetComment } from 'wherehows-web/typings/api/datasets/comments'; | 
					
						
							| 
									
										
										
										
											2017-10-23 16:50:48 -07:00
										 |  |  | import Notifications, { NotificationEvent } from 'wherehows-web/services/notifications'; | 
					
						
							| 
									
										
										
										
											2018-06-25 23:42:28 -07:00
										 |  |  | import { noop } from 'wherehows-web/utils/helpers/functions'; | 
					
						
							| 
									
										
										
										
											2017-09-10 19:31:54 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | export default Component.extend({ | 
					
						
							|  |  |  |   tagName: 'li', | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   classNames: ['comment-item', 'nacho-container'], | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /** | 
					
						
							|  |  |  |    * Reference to the application notifications Service | 
					
						
							| 
									
										
										
										
											2017-10-23 16:50:48 -07:00
										 |  |  |    * @type {ComputedProperty<Notifications>} | 
					
						
							|  |  |  |    */ | 
					
						
							|  |  |  |   notifications: <ComputedProperty<Notifications>>inject(), | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /** | 
					
						
							|  |  |  |    * The comment currently being edited | 
					
						
							|  |  |  |    * @type {IDatasetComment} | 
					
						
							|  |  |  |    */ | 
					
						
							|  |  |  |   comment: <IDatasetComment>{}, | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /** | 
					
						
							|  |  |  |    * A copy of the comment in the editor before the user updates it | 
					
						
							|  |  |  |    * @type {IDatasetComment} | 
					
						
							|  |  |  |    */ | 
					
						
							|  |  |  |   previousComment: <IDatasetComment>{}, | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /** | 
					
						
							|  |  |  |    * Default handler to delete comment | 
					
						
							|  |  |  |    * @type {Function} | 
					
						
							|  |  |  |    */ | 
					
						
							|  |  |  |   deleteComment: noop, | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /** | 
					
						
							|  |  |  |    * Default handler to update a comment | 
					
						
							|  |  |  |    * @type {Function} | 
					
						
							| 
									
										
										
										
											2017-09-10 19:31:54 -07:00
										 |  |  |    */ | 
					
						
							| 
									
										
										
										
											2017-10-23 16:50:48 -07:00
										 |  |  |   updateComment: noop, | 
					
						
							| 
									
										
										
										
											2017-09-10 19:31:54 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |   /** | 
					
						
							|  |  |  |    * Flag indicating the comment is in edit mode | 
					
						
							|  |  |  |    * @type {boolean} | 
					
						
							|  |  |  |    */ | 
					
						
							|  |  |  |   isEditing: false, | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /** | 
					
						
							|  |  |  |    * Editor options to set on the comment editor | 
					
						
							|  |  |  |    * @type {object} | 
					
						
							|  |  |  |    */ | 
					
						
							|  |  |  |   editorOptions: computed(() => baseCommentEditorOptions).readOnly(), | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /** | 
					
						
							|  |  |  |    * Applies focus to the comment editor | 
					
						
							|  |  |  |    */ | 
					
						
							|  |  |  |   focusEditor(): void { | 
					
						
							| 
									
										
										
										
											2017-10-23 16:50:48 -07:00
										 |  |  |     //TODO: FIX use component context
 | 
					
						
							| 
									
										
										
										
											2017-09-10 19:31:54 -07:00
										 |  |  |     this.$('.comment-new__content').focus(); | 
					
						
							|  |  |  |   }, | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /** | 
					
						
							|  |  |  |    * Sets the edit flag to false | 
					
						
							|  |  |  |    */ | 
					
						
							|  |  |  |   stopEditing(): void { | 
					
						
							|  |  |  |     set(this, 'isEditing', false); | 
					
						
							|  |  |  |   }, | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   actions: { | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Handles delete initiation by passing comment id upstream for deletion | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     async onDelete(): Promise<void> { | 
					
						
							|  |  |  |       const { id } = get(this, 'comment'); | 
					
						
							|  |  |  |       const deleteComment = this.deleteComment; | 
					
						
							|  |  |  |       const dialogActions: { [prop: string]: () => void } = {}; | 
					
						
							|  |  |  |       const confirmHandler = new Promise((resolve, reject) => { | 
					
						
							|  |  |  |         dialogActions['didConfirm'] = () => resolve(); | 
					
						
							|  |  |  |         dialogActions['didDismiss'] = () => reject(); | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-23 16:50:48 -07:00
										 |  |  |       get(this, 'notifications').notify(NotificationEvent.confirm, { | 
					
						
							| 
									
										
										
										
											2017-09-10 19:31:54 -07:00
										 |  |  |         header: 'Delete comment', | 
					
						
							|  |  |  |         content: 'Are you sure you want to delete this comment?', | 
					
						
							|  |  |  |         dialogActions: dialogActions | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       try { | 
					
						
							|  |  |  |         await confirmHandler; | 
					
						
							|  |  |  |         deleteComment(id); | 
					
						
							|  |  |  |       } catch (e) { | 
					
						
							|  |  |  |         //no-op
 | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |     }, | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Places the comment in an edit state | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     onEdit(): void { | 
					
						
							|  |  |  |       set(this, 'isEditing', true); | 
					
						
							|  |  |  |       set(this, 'previousComment', { ...get(this, 'comment') }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       run(() => { | 
					
						
							|  |  |  |         schedule('afterRender', this, 'focusEditor'); | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  |     }, | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Cancels component out of edit more | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     onCancel(): void { | 
					
						
							|  |  |  |       this.stopEditing(); | 
					
						
							|  |  |  |     }, | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Publishes the updated comment upstream | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     updateComment(): void { | 
					
						
							| 
									
										
										
										
											2017-10-23 16:50:48 -07:00
										 |  |  |       const { comment, previousComment } = getProperties(this, ['comment', 'previousComment']); | 
					
						
							| 
									
										
										
										
											2017-09-10 19:31:54 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |       // Ensure that we have a change in the comment text
 | 
					
						
							|  |  |  |       if (comment.text !== previousComment.text) { | 
					
						
							|  |  |  |         this.stopEditing(); | 
					
						
							| 
									
										
										
										
											2017-10-23 16:50:48 -07:00
										 |  |  |         const updateComment = this.updateComment; | 
					
						
							| 
									
										
										
										
											2017-09-10 19:31:54 -07:00
										 |  |  |         updateComment(comment.id, comment); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return; | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-23 16:50:48 -07:00
										 |  |  |       get(this, 'notifications').notify(NotificationEvent.info, { content: 'Nothing seems to have been changed?' }); | 
					
						
							| 
									
										
										
										
											2017-09-10 19:31:54 -07:00
										 |  |  |     } | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | }); |