commentEditor.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. 'use strict';
  2. exports.__esModule = true;
  3. var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
  4. var _element = require('./../../helpers/dom/element');
  5. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  6. /**
  7. * Comment editor for the Comments plugin.
  8. *
  9. * @class CommentEditor
  10. * @plugin Comments
  11. */
  12. var CommentEditor = function () {
  13. _createClass(CommentEditor, null, [{
  14. key: 'CLASS_EDITOR_CONTAINER',
  15. get: function get() {
  16. return 'htCommentsContainer';
  17. }
  18. }, {
  19. key: 'CLASS_EDITOR',
  20. get: function get() {
  21. return 'htComments';
  22. }
  23. }, {
  24. key: 'CLASS_INPUT',
  25. get: function get() {
  26. return 'htCommentTextArea';
  27. }
  28. }, {
  29. key: 'CLASS_CELL',
  30. get: function get() {
  31. return 'htCommentCell';
  32. }
  33. }]);
  34. function CommentEditor() {
  35. _classCallCheck(this, CommentEditor);
  36. this.editor = this.createEditor();
  37. this.editorStyle = this.editor.style;
  38. this.hidden = true;
  39. this.hide();
  40. }
  41. /**
  42. * Set position of the comments editor according to the provided x and y coordinates.
  43. *
  44. * @param {Number} x X position (in pixels).
  45. * @param {Number} y Y position (in pixels).
  46. */
  47. _createClass(CommentEditor, [{
  48. key: 'setPosition',
  49. value: function setPosition(x, y) {
  50. this.editorStyle.left = x + 'px';
  51. this.editorStyle.top = y + 'px';
  52. }
  53. /**
  54. * Set the editor size according to the provided arguments.
  55. *
  56. * @param {Number} width Width in pixels.
  57. * @param {Number} height Height in pixels.
  58. */
  59. }, {
  60. key: 'setSize',
  61. value: function setSize(width, height) {
  62. if (width && height) {
  63. var input = this.getInputElement();
  64. input.style.width = width + 'px';
  65. input.style.height = height + 'px';
  66. }
  67. }
  68. /**
  69. * Reset the editor size to its initial state.
  70. */
  71. }, {
  72. key: 'resetSize',
  73. value: function resetSize() {
  74. var input = this.getInputElement();
  75. input.style.width = '';
  76. input.style.height = '';
  77. }
  78. /**
  79. * Set the read-only state for the comments editor.
  80. *
  81. * @param {Boolean} state The new read only state.
  82. */
  83. }, {
  84. key: 'setReadOnlyState',
  85. value: function setReadOnlyState(state) {
  86. var input = this.getInputElement();
  87. input.readOnly = state;
  88. }
  89. /**
  90. * Show the comments editor.
  91. */
  92. }, {
  93. key: 'show',
  94. value: function show() {
  95. this.editorStyle.display = 'block';
  96. this.hidden = false;
  97. }
  98. /**
  99. * Hide the comments editor.
  100. */
  101. }, {
  102. key: 'hide',
  103. value: function hide() {
  104. this.editorStyle.display = 'none';
  105. this.hidden = true;
  106. }
  107. /**
  108. * Checks if the editor is visible.
  109. *
  110. * @returns {Boolean}
  111. */
  112. }, {
  113. key: 'isVisible',
  114. value: function isVisible() {
  115. return this.editorStyle.display === 'block';
  116. }
  117. /**
  118. * Set the comment value.
  119. *
  120. * @param {String} [value] The value to use.
  121. */
  122. }, {
  123. key: 'setValue',
  124. value: function setValue() {
  125. var value = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : '';
  126. value = value || '';
  127. this.getInputElement().value = value;
  128. }
  129. /**
  130. * Get the comment value.
  131. *
  132. * @returns {String}
  133. */
  134. }, {
  135. key: 'getValue',
  136. value: function getValue() {
  137. return this.getInputElement().value;
  138. }
  139. /**
  140. * Checks if the comment input element is focused.
  141. *
  142. * @returns {Boolean}
  143. */
  144. }, {
  145. key: 'isFocused',
  146. value: function isFocused() {
  147. return document.activeElement === this.getInputElement();
  148. }
  149. /**
  150. * Focus the comments input element.
  151. */
  152. }, {
  153. key: 'focus',
  154. value: function focus() {
  155. this.getInputElement().focus();
  156. }
  157. /**
  158. * Create the `textarea` to be used as a comments editor.
  159. *
  160. * @returns {HTMLElement}
  161. */
  162. }, {
  163. key: 'createEditor',
  164. value: function createEditor() {
  165. var container = document.querySelector('.' + CommentEditor.CLASS_EDITOR_CONTAINER);
  166. var editor = void 0;
  167. var textArea = void 0;
  168. if (!container) {
  169. container = document.createElement('div');
  170. (0, _element.addClass)(container, CommentEditor.CLASS_EDITOR_CONTAINER);
  171. document.body.appendChild(container);
  172. }
  173. editor = document.createElement('div');
  174. (0, _element.addClass)(editor, CommentEditor.CLASS_EDITOR);
  175. textArea = document.createElement('textarea');
  176. (0, _element.addClass)(textArea, CommentEditor.CLASS_INPUT);
  177. editor.appendChild(textArea);
  178. container.appendChild(editor);
  179. return editor;
  180. }
  181. /**
  182. * Get the input element.
  183. *
  184. * @returns {HTMLElement}
  185. */
  186. }, {
  187. key: 'getInputElement',
  188. value: function getInputElement() {
  189. return this.editor.querySelector('.' + CommentEditor.CLASS_INPUT);
  190. }
  191. /**
  192. * Destroy the comments editor.
  193. */
  194. }, {
  195. key: 'destroy',
  196. value: function destroy() {
  197. this.editor.parentNode.removeChild(this.editor);
  198. this.editor = null;
  199. this.editorStyle = null;
  200. }
  201. }]);
  202. return CommentEditor;
  203. }();
  204. exports.default = CommentEditor;