simditor-checklist.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. (function (root, factory) {
  2. if (typeof define === 'function' && define.amd) {
  3. // AMD. Register as an anonymous module unless amdModuleId is set
  4. define('simditor-checklist', ["jquery","simditor"], function (a0,b1) {
  5. return (root['ChecklistButton'] = factory(a0,b1));
  6. });
  7. } else if (typeof exports === 'object') {
  8. // Node. Does not work with strict CommonJS, but
  9. // only CommonJS-like environments that support module.exports,
  10. // like Node.
  11. module.exports = factory(require("jquery"),require("simditor"));
  12. } else {
  13. root['ChecklistButton'] = factory(jQuery,Simditor);
  14. }
  15. }(this, function ($, Simditor) {
  16. var ChecklistButton,
  17. extend = function(child, parent) { for (var key in parent) { if (hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; },
  18. hasProp = {}.hasOwnProperty,
  19. slice = [].slice;
  20. ChecklistButton = (function(superClass) {
  21. extend(ChecklistButton, superClass);
  22. ChecklistButton.prototype.type = 'ul.simditor-checklist';
  23. ChecklistButton.prototype.name = 'checklist';
  24. ChecklistButton.prototype.icon = 'checklist';
  25. ChecklistButton.prototype.htmlTag = 'li';
  26. ChecklistButton.prototype.disableTag = 'pre, table';
  27. function ChecklistButton() {
  28. var args;
  29. args = 1 <= arguments.length ? slice.call(arguments, 0) : [];
  30. ChecklistButton.__super__.constructor.apply(this, args);
  31. if ('input' && $.inArray('input', this.editor.formatter._allowedTags) < 0) {
  32. this.editor.formatter._allowedTags.push('input');
  33. }
  34. $.extend(this.editor.formatter._allowedAttributes, {
  35. input: ['type', 'checked']
  36. });
  37. }
  38. ChecklistButton.prototype._init = function() {
  39. ChecklistButton.__super__._init.call(this);
  40. this.editor.on('decorate', (function(_this) {
  41. return function(e, $el) {
  42. return $el.find('ul > li input[type=checkbox]').each(function(i, checkbox) {
  43. return _this._decorate($(checkbox));
  44. });
  45. };
  46. })(this));
  47. this.editor.on('undecorate', (function(_this) {
  48. return function(e, $el) {
  49. return $el.find('.simditor-checklist > li').each(function(i, node) {
  50. return _this._undecorate($(node));
  51. });
  52. };
  53. })(this));
  54. this.editor.body.on('click', '.simditor-checklist > li', (function(_this) {
  55. return function(e) {
  56. var $node, range;
  57. e.preventDefault();
  58. e.stopPropagation();
  59. $node = $(e.currentTarget);
  60. range = document.createRange();
  61. _this.editor.selection.save();
  62. range.setStart($node[0], 0);
  63. range.setEnd($node[0], _this.editor.util.getNodeLength($node[0]));
  64. _this.editor.selection.range(range);
  65. document.execCommand('strikethrough');
  66. $node.attr('checked', !$node.attr('checked'));
  67. _this.editor.selection.restore();
  68. return _this.editor.trigger('valuechanged');
  69. };
  70. })(this));
  71. return this.editor.keystroke.add('13', 'li', (function(_this) {
  72. return function(e, $node) {
  73. return setTimeout(function() {
  74. var $li;
  75. $li = _this.editor.selection.blockNodes().last().next();
  76. if ($li.length) {
  77. $li[0].removeAttribute('checked');
  78. if (document.queryCommandState('strikethrough')) {
  79. return document.execCommand('strikethrough');
  80. }
  81. }
  82. }, 0);
  83. };
  84. })(this));
  85. };
  86. ChecklistButton.prototype._status = function() {
  87. var $node;
  88. ChecklistButton.__super__._status.call(this);
  89. $node = this.editor.selection.rootNodes();
  90. if ($node.is('.simditor-checklist')) {
  91. this.editor.toolbar.findButton('ul').setActive(false);
  92. this.editor.toolbar.findButton('ol').setActive(false);
  93. this.editor.toolbar.findButton('ul').setDisabled(true);
  94. return this.editor.toolbar.findButton('ol').setDisabled(true);
  95. } else {
  96. return this.editor.toolbar.findButton('checklist').setActive(false);
  97. }
  98. };
  99. ChecklistButton.prototype.command = function(param) {
  100. var $list, $rootNodes;
  101. $rootNodes = this.editor.selection.blockNodes();
  102. this.editor.selection.save();
  103. $list = null;
  104. $rootNodes.each((function(_this) {
  105. return function(i, node) {
  106. var $node;
  107. $node = $(node);
  108. if ($node.is('blockquote, li') || $node.is(_this.disableTag) || !$.contains(document, node)) {
  109. return;
  110. }
  111. if ($node.is('.simditor-checklist')) {
  112. $node.children('li').each(function(i, li) {
  113. var $childList, $li;
  114. $li = $(li);
  115. $childList = $li.children('ul, ol').insertAfter($node);
  116. return $('<p/>').append($(li).html() || _this.editor.util.phBr).insertBefore($node);
  117. });
  118. return $node.remove();
  119. } else if ($node.is('ul, ol')) {
  120. return $('<ul class="simditor-checklist" />').append($node.contents()).replaceAll($node);
  121. } else if ($list && $node.prev().is($list)) {
  122. $('<li/>').append($node.html() || _this.editor.util.phBr).appendTo($list);
  123. return $node.remove();
  124. } else {
  125. $list = $('<ul class="simditor-checklist"><li></li></ul>');
  126. $list.find('li').append($node.html() || _this.editor.util.phBr);
  127. return $list.replaceAll($node);
  128. }
  129. };
  130. })(this));
  131. this.editor.selection.restore();
  132. return this.editor.trigger('valuechanged');
  133. };
  134. ChecklistButton.prototype._decorate = function($checkbox) {
  135. var $node, checked;
  136. checked = !!$checkbox.attr('checked');
  137. $node = $checkbox.closest('li');
  138. $checkbox.remove();
  139. $node.attr('checked', checked);
  140. return $node.closest('ul').addClass('simditor-checklist');
  141. };
  142. ChecklistButton.prototype._undecorate = function($node) {
  143. var $checkbox, checked;
  144. checked = !!$node.attr('checked');
  145. $checkbox = $('<input type="checkbox">').attr('checked', checked);
  146. return $node.attr('checked', '').prepend($checkbox);
  147. };
  148. return ChecklistButton;
  149. })(Simditor.Button);
  150. Simditor.Toolbar.addButton(ChecklistButton);
  151. return ChecklistButton;
  152. }));