You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

78 lines
2.1 KiB

  1. (function($K)
  2. {
  3. $K.add('module', 'editable', {
  4. init: function(app, context)
  5. {
  6. this.app = app;
  7. // defaults
  8. var defaults = {
  9. classname: 'editable',
  10. focus: false
  11. };
  12. // context
  13. this.context = context;
  14. this.params = context.getParams(defaults);
  15. this.$element = context.getElement();
  16. },
  17. // public
  18. start: function()
  19. {
  20. this.$element.addClass(this.params.classname).attr('contenteditable', true);
  21. this._setFocus();
  22. this._setEvents();
  23. },
  24. stop: function()
  25. {
  26. this.$element.removeClass(this.params.classname).removeAttr('contenteditable');
  27. this.$element.off('.kube.editable');
  28. },
  29. // private
  30. _setEvents: function()
  31. {
  32. this.$element.on('keydown.kube.editable', this._keydown.bind(this));
  33. this.$element.on('paste.kube.editable', this._paste.bind(this));
  34. this.$element.on('blur.kube.editable', this._blur.bind(this));
  35. },
  36. _setFocus: function()
  37. {
  38. if (this.params.focus) this.$element.focus();
  39. },
  40. _checkEmpty: function()
  41. {
  42. if (!this.$element.text().replace(" ", "").length)
  43. {
  44. this.$element.empty();
  45. }
  46. },
  47. _paste: function(e)
  48. {
  49. e.preventDefault();
  50. var event = (e.originalEvent || e);
  51. var text = '';
  52. if (event.clipboardData)
  53. {
  54. text = event.clipboardData.getData('text/plain');
  55. document.execCommand('insertText', false, text);
  56. }
  57. else if (window.clipboardData)
  58. {
  59. text = window.clipboardData.getData('Text');
  60. document.selection.createRange().pasteHTML(text);
  61. }
  62. },
  63. _blur: function(e)
  64. {
  65. this._checkEmpty();
  66. },
  67. _keydown: function(e)
  68. {
  69. // disable enter key
  70. if (e.which === 13) e.preventDefault();
  71. }
  72. });
  73. })(Kube);