addCommentEntry.js 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. import App from '../../../base'
  2. let content = ''
  3. App.directive('addCommentEntry', () => ({
  4. restrict: 'E',
  5. templateUrl: 'addCommentEntryTemplate',
  6. scope: {
  7. onStartNew: '&',
  8. onSubmit: '&',
  9. onCancel: '&'
  10. },
  11. link(scope, element, attrs) {
  12. scope.state = {
  13. isAdding: false,
  14. content: content
  15. }
  16. scope.$on('comment:start_adding', () => scope.startNewComment())
  17. scope.$on('$destroy', function() {
  18. content = scope.state.content
  19. })
  20. scope.startNewComment = function() {
  21. scope.state.isAdding = true
  22. scope.onStartNew()
  23. setTimeout(() => scope.$broadcast('comment:new:open'))
  24. }
  25. scope.cancelNewComment = function() {
  26. scope.state.isAdding = false
  27. scope.state.content = ''
  28. scope.onCancel()
  29. }
  30. scope.handleCommentKeyPress = function(ev) {
  31. if (ev.keyCode === 13 && !ev.shiftKey && !ev.ctrlKey && !ev.metaKey) {
  32. ev.preventDefault()
  33. if (scope.state.content.length > 0) {
  34. scope.submitNewComment()
  35. }
  36. }
  37. }
  38. scope.submitNewComment = function(event) {
  39. scope.onSubmit({ content: scope.state.content })
  40. content = scope.state.content
  41. scope.state.isAdding = false
  42. scope.state.content = ''
  43. }
  44. }
  45. }))