2020-05-19 05:02:56 -04:00
|
|
|
import App from '../../../base'
|
|
|
|
let content = ''
|
|
|
|
App.directive('addCommentEntry', () => ({
|
|
|
|
restrict: 'E',
|
|
|
|
templateUrl: 'addCommentEntryTemplate',
|
|
|
|
scope: {
|
|
|
|
onStartNew: '&',
|
|
|
|
onSubmit: '&',
|
2021-04-27 03:52:58 -04:00
|
|
|
onCancel: '&',
|
2020-05-19 05:02:56 -04:00
|
|
|
},
|
|
|
|
link(scope, element, attrs) {
|
|
|
|
scope.state = {
|
|
|
|
isAdding: false,
|
2022-05-16 10:25:49 -04:00
|
|
|
content,
|
2020-05-19 05:02:56 -04:00
|
|
|
}
|
2018-11-05 05:06:39 -05:00
|
|
|
|
2020-05-19 05:02:56 -04:00
|
|
|
scope.$on('comment:start_adding', () => scope.startNewComment())
|
2021-04-14 09:17:21 -04:00
|
|
|
scope.$on('$destroy', function () {
|
2020-05-19 05:02:56 -04:00
|
|
|
content = scope.state.content
|
|
|
|
})
|
2018-11-05 05:06:39 -05:00
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
scope.startNewComment = function () {
|
2020-05-19 05:02:56 -04:00
|
|
|
scope.state.isAdding = true
|
|
|
|
scope.onStartNew()
|
|
|
|
setTimeout(() => scope.$broadcast('comment:new:open'))
|
|
|
|
}
|
2018-11-05 05:06:39 -05:00
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
scope.cancelNewComment = function () {
|
2020-05-19 05:02:56 -04:00
|
|
|
scope.state.isAdding = false
|
|
|
|
scope.state.content = ''
|
|
|
|
scope.onCancel()
|
|
|
|
}
|
2018-11-05 05:06:39 -05:00
|
|
|
|
2022-03-11 10:10:50 -05:00
|
|
|
const ignoreKeysInTextAreas = ['PageDown', 'PageUp']
|
|
|
|
|
|
|
|
scope.handleCommentKeyDown = function (ev) {
|
|
|
|
if (ignoreKeysInTextAreas.includes(ev.key)) {
|
|
|
|
if (ev.target.closest('textarea')) {
|
|
|
|
ev.preventDefault()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
scope.handleCommentKeyPress = function (ev) {
|
2020-05-19 05:02:56 -04:00
|
|
|
if (ev.keyCode === 13 && !ev.shiftKey && !ev.ctrlKey && !ev.metaKey) {
|
|
|
|
ev.preventDefault()
|
|
|
|
if (scope.state.content.length > 0) {
|
|
|
|
scope.submitNewComment()
|
2018-11-05 05:06:39 -05:00
|
|
|
}
|
|
|
|
}
|
2020-05-19 05:02:56 -04:00
|
|
|
}
|
2018-11-05 05:06:39 -05:00
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
scope.submitNewComment = function (event) {
|
2020-05-19 05:02:56 -04:00
|
|
|
scope.onSubmit({ content: scope.state.content })
|
|
|
|
content = scope.state.content
|
|
|
|
scope.state.isAdding = false
|
|
|
|
scope.state.content = ''
|
2018-11-05 05:06:39 -05:00
|
|
|
}
|
2021-04-27 03:52:58 -04:00
|
|
|
},
|
2020-05-19 05:02:56 -04:00
|
|
|
}))
|