edittable.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /**
  2. * Created with JetBrains PhpStorm.
  3. * User: xuheng
  4. * Date: 12-12-19
  5. * Time: 下午4:55
  6. * To change this template use File | Settings | File Templates.
  7. */
  8. (function () {
  9. var title = $G("J_title"),
  10. caption = $G("J_caption"),
  11. autoSizeContent = $G("J_autoSizeContent"),
  12. autoSizePage = $G("J_autoSizePage"),
  13. tone = $G("J_tone"),
  14. me,
  15. preview = $G("J_preview");
  16. var editTable = function () {
  17. me = this;
  18. me.init();
  19. };
  20. editTable.prototype = {
  21. init:function () {
  22. var colorPiker = new UE.ui.ColorPicker({
  23. editor:editor
  24. }),
  25. colorPop = new UE.ui.Popup({
  26. editor:editor,
  27. content:colorPiker
  28. });
  29. title.checked = editor.queryCommandState("inserttitle") == -1;
  30. caption.checked = editor.queryCommandState("insertcaption") == -1;
  31. me.createTable(title.checked, caption.checked);
  32. me.setAutoSize();
  33. me.setColor(me.getColor());
  34. domUtils.on(title, "click", me.titleHanler);
  35. domUtils.on(caption, "click", me.captionHanler);
  36. domUtils.on(autoSizeContent, "click", me.autoSizeContentHanler);
  37. domUtils.on(autoSizePage, "click", me.autoSizePageHanler);
  38. domUtils.on(tone, "click", function () {
  39. colorPop.showAnchor(tone);
  40. });
  41. domUtils.on(document, 'mousedown', function () {
  42. colorPop.hide();
  43. });
  44. colorPiker.addListener("pickcolor", function () {
  45. me.setColor(arguments[1]);
  46. colorPop.hide();
  47. });
  48. colorPiker.addListener("picknocolor", function () {
  49. me.setColor("");
  50. colorPop.hide();
  51. });
  52. },
  53. createTable:function (hasTitle, hasCaption) {
  54. var arr = [];
  55. arr.push("<table id='J_example'>");
  56. if (hasCaption) {
  57. arr.push("<caption>" + lang.captionName + "</caption>")
  58. }
  59. if (hasTitle) {
  60. arr.push("<tr>");
  61. for (var j = 0; j < 5; j++) {
  62. arr.push("<th>" + lang.titleName + "</th>")
  63. }
  64. arr.push("</tr>");
  65. }
  66. for (var i = 0; i < 6; i++) {
  67. arr.push("<tr>");
  68. for (var k = 0; k < 5; k++) {
  69. arr.push("<td>" + lang.cellsName + "</td>")
  70. }
  71. arr.push("</tr>");
  72. }
  73. arr.push("</table>");
  74. preview.innerHTML = arr.join("");
  75. },
  76. titleHanler:function () {
  77. var example = $G("J_example"),
  78. frg=document.createDocumentFragment(),
  79. color = domUtils.getComputedStyle(domUtils.getElementsByTagName(example, "td")[0], "border-color");
  80. if (title.checked) {
  81. example.insertRow(0);
  82. for (var i = 0, node; i < 5; i++) {
  83. node = document.createElement("th");
  84. node.innerHTML = lang.titleName;
  85. frg.appendChild(node);
  86. }
  87. example.rows[0].appendChild(frg);
  88. } else {
  89. domUtils.remove(example.rows[0]);
  90. }
  91. me.setColor(color);
  92. },
  93. captionHanler:function () {
  94. var example = $G("J_example");
  95. if (caption.checked) {
  96. var row = document.createElement('caption');
  97. row.innerHTML = lang.captionName;
  98. example.insertBefore(row, example.firstChild);
  99. } else {
  100. domUtils.remove(domUtils.getElementsByTagName(example, 'caption')[0]);
  101. }
  102. },
  103. autoSizeContentHanler:function () {
  104. var example = $G("J_example");
  105. example.removeAttribute("width");
  106. },
  107. autoSizePageHanler:function () {
  108. var example = $G("J_example");
  109. var tds = example.getElementsByTagName(example, "td");
  110. utils.each(tds, function (td) {
  111. td.removeAttribute("width");
  112. });
  113. example.setAttribute('width', '100%');
  114. },
  115. getColor:function () {
  116. var start = editor.selection.getStart(), color,
  117. cell = domUtils.findParentByTagName(start, ["td", "th", "caption"], true);
  118. color = domUtils.getComputedStyle(cell, "border-color");
  119. if (!color) color = "#DDDDDD";
  120. return color;
  121. },
  122. setColor:function (color) {
  123. var example = $G("J_example"),
  124. arr = domUtils.getElementsByTagName(example, "td").concat(
  125. domUtils.getElementsByTagName(example, "th"),
  126. domUtils.getElementsByTagName(example, "caption")
  127. );
  128. tone.value = color;
  129. utils.each(arr, function (node) {
  130. node.style.borderColor = color;
  131. });
  132. },
  133. setAutoSize:function () {
  134. var me = this,
  135. start = editor.selection.getStart(),
  136. wt = domUtils.findParentByTagName(start, "table", true).width,
  137. flag = !wt;
  138. if (flag) {
  139. autoSizeContent.checked = flag;
  140. me.autoSizeContentHanler();
  141. } else {
  142. autoSizePage.checked = !flag;
  143. me.autoSizePageHanler();
  144. }
  145. }
  146. };
  147. new editTable;
  148. dialog.onok = function () {
  149. editor.__hasEnterExecCommand = true;
  150. if (title.checked) {
  151. editor.queryCommandState("inserttitle") != -1 && editor.execCommand("inserttitle")
  152. } else {
  153. editor.queryCommandState("deletetitle") != -1 && editor.execCommand("deletetitle");
  154. }
  155. if (caption.checked) {
  156. editor.queryCommandState("insertcaption") != -1 && editor.execCommand("insertcaption")
  157. } else {
  158. editor.queryCommandState("deletecaption") != -1 && editor.execCommand("deletecaption");
  159. }
  160. editor.execCommand("edittable", tone.value);
  161. autoSizeContent.checked ?editor.execCommand('adaptbytext') : "";
  162. autoSizePage.checked ? editor.execCommand("adaptbywindow") : "";
  163. editor.__hasEnterExecCommand = false;
  164. };
  165. })();