edittable.js 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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. titleCol = $G("J_titleCol"),
  11. caption = $G("J_caption"),
  12. sorttable = $G("J_sorttable"),
  13. autoSizeContent = $G("J_autoSizeContent"),
  14. autoSizePage = $G("J_autoSizePage"),
  15. tone = $G("J_tone"),
  16. me,
  17. preview = $G("J_preview");
  18. var editTable = function () {
  19. me = this;
  20. me.init();
  21. };
  22. editTable.prototype = {
  23. init: function () {
  24. var colorPiker = new UE.ui.ColorPicker({
  25. editor: editor
  26. }),
  27. colorPop = new UE.ui.Popup({
  28. editor: editor,
  29. content: colorPiker
  30. });
  31. title.checked = editor.queryCommandState("inserttitle") == -1;
  32. titleCol.checked = editor.queryCommandState("inserttitlecol") == -1;
  33. caption.checked = editor.queryCommandState("insertcaption") == -1;
  34. sorttable.checked = editor.queryCommandState("enablesort") == 1;
  35. var enablesortState = editor.queryCommandState("enablesort"),
  36. disablesortState = editor.queryCommandState("disablesort");
  37. sorttable.checked = !!(enablesortState < 0 && disablesortState >= 0);
  38. sorttable.disabled = !!(enablesortState < 0 && disablesortState < 0);
  39. sorttable.title = enablesortState < 0 && disablesortState < 0 ? lang.errorMsg : '';
  40. me.createTable(title.checked, titleCol.checked, caption.checked);
  41. me.setAutoSize();
  42. me.setColor(me.getColor());
  43. domUtils.on(title, "click", me.titleHanler);
  44. domUtils.on(titleCol, "click", me.titleColHanler);
  45. domUtils.on(caption, "click", me.captionHanler);
  46. domUtils.on(sorttable, "click", me.sorttableHanler);
  47. domUtils.on(autoSizeContent, "click", me.autoSizeContentHanler);
  48. domUtils.on(autoSizePage, "click", me.autoSizePageHanler);
  49. domUtils.on(tone, "click", function () {
  50. colorPop.showAnchor(tone);
  51. });
  52. domUtils.on(document, 'mousedown', function () {
  53. colorPop.hide();
  54. });
  55. colorPiker.addListener("pickcolor", function () {
  56. me.setColor(arguments[1]);
  57. colorPop.hide();
  58. });
  59. colorPiker.addListener("picknocolor", function () {
  60. me.setColor("");
  61. colorPop.hide();
  62. });
  63. },
  64. createTable: function (hasTitle, hasTitleCol, hasCaption) {
  65. var arr = [],
  66. sortSpan = '<span>^</span>';
  67. arr.push("<table id='J_example'>");
  68. if (hasCaption) {
  69. arr.push("<caption>" + lang.captionName + "</caption>")
  70. }
  71. if (hasTitle) {
  72. arr.push("<tr>");
  73. if (hasTitleCol) {
  74. arr.push("<th>" + lang.titleName + "</th>");
  75. }
  76. for (var j = 0; j < 5; j++) {
  77. arr.push("<th>" + lang.titleName + "</th>");
  78. }
  79. arr.push("</tr>");
  80. }
  81. for (var i = 0; i < 6; i++) {
  82. arr.push("<tr>");
  83. if (hasTitleCol) {
  84. arr.push("<th>" + lang.titleName + "</th>")
  85. }
  86. for (var k = 0; k < 5; k++) {
  87. arr.push("<td>" + lang.cellsName + "</td>")
  88. }
  89. arr.push("</tr>");
  90. }
  91. arr.push("</table>");
  92. preview.innerHTML = arr.join("");
  93. this.updateSortSpan();
  94. },
  95. titleHanler: function () {
  96. var example = $G("J_example"),
  97. frg = document.createDocumentFragment(),
  98. color = domUtils.getComputedStyle(domUtils.getElementsByTagName(example, "td")[0], "border-color"),
  99. colCount = example.rows[0].children.length;
  100. if (title.checked) {
  101. example.insertRow(0);
  102. for (var i = 0, node; i < colCount; i++) {
  103. node = document.createElement("th");
  104. node.innerHTML = lang.titleName;
  105. frg.appendChild(node);
  106. }
  107. example.rows[0].appendChild(frg);
  108. } else {
  109. domUtils.remove(example.rows[0]);
  110. }
  111. me.setColor(color);
  112. me.updateSortSpan();
  113. },
  114. titleColHanler: function () {
  115. var example = $G("J_example"),
  116. color = domUtils.getComputedStyle(domUtils.getElementsByTagName(example, "td")[0], "border-color"),
  117. colArr = example.rows,
  118. colCount = colArr.length;
  119. if (titleCol.checked) {
  120. for (var i = 0, node; i < colCount; i++) {
  121. node = document.createElement("th");
  122. node.innerHTML = lang.titleName;
  123. colArr[i].insertBefore(node, colArr[i].children[0]);
  124. }
  125. } else {
  126. for (var i = 0; i < colCount; i++) {
  127. domUtils.remove(colArr[i].children[0]);
  128. }
  129. }
  130. me.setColor(color);
  131. me.updateSortSpan();
  132. },
  133. captionHanler: function () {
  134. var example = $G("J_example");
  135. if (caption.checked) {
  136. var row = document.createElement('caption');
  137. row.innerHTML = lang.captionName;
  138. example.insertBefore(row, example.firstChild);
  139. } else {
  140. domUtils.remove(domUtils.getElementsByTagName(example, 'caption')[0]);
  141. }
  142. },
  143. sorttableHanler: function () {
  144. me.updateSortSpan();
  145. },
  146. autoSizeContentHanler: function () {
  147. var example = $G("J_example");
  148. example.removeAttribute("width");
  149. },
  150. autoSizePageHanler: function () {
  151. var example = $G("J_example");
  152. var tds = example.getElementsByTagName(example, "td");
  153. utils.each(tds, function (td) {
  154. td.removeAttribute("width");
  155. });
  156. example.setAttribute('width', '100%');
  157. },
  158. updateSortSpan: function () {
  159. var example = $G("J_example"),
  160. row = example.rows[0];
  161. var spans = domUtils.getElementsByTagName(example, "span");
  162. utils.each(spans, function (span) {
  163. span.parentNode.removeChild(span);
  164. });
  165. if (sorttable.checked) {
  166. utils.each(row.cells, function (cell, i) {
  167. var span = document.createElement("span");
  168. span.innerHTML = "^";
  169. cell.appendChild(span);
  170. });
  171. }
  172. },
  173. getColor: function () {
  174. var start = editor.selection.getStart(), color,
  175. cell = domUtils.findParentByTagName(start, ["td", "th", "caption"], true);
  176. color = cell && domUtils.getComputedStyle(cell, "border-color");
  177. if (!color) color = "#DDDDDD";
  178. return color;
  179. },
  180. setColor: function (color) {
  181. var example = $G("J_example"),
  182. arr = domUtils.getElementsByTagName(example, "td").concat(
  183. domUtils.getElementsByTagName(example, "th"),
  184. domUtils.getElementsByTagName(example, "caption")
  185. );
  186. tone.value = color;
  187. utils.each(arr, function (node) {
  188. node.style.borderColor = color;
  189. });
  190. },
  191. setAutoSize: function () {
  192. var me = this;
  193. autoSizePage.checked = true;
  194. me.autoSizePageHanler();
  195. }
  196. };
  197. new editTable;
  198. dialog.onok = function () {
  199. editor.__hasEnterExecCommand = true;
  200. var checks = {
  201. title: "inserttitle deletetitle",
  202. titleCol: "inserttitlecol deletetitlecol",
  203. caption: "insertcaption deletecaption",
  204. sorttable: "enablesort disablesort"
  205. };
  206. editor.fireEvent('saveScene');
  207. for (var i in checks) {
  208. var cmds = checks[i].split(" "),
  209. input = $G("J_" + i);
  210. if (input["checked"]) {
  211. editor.queryCommandState(cmds[0]) != -1 && editor.execCommand(cmds[0]);
  212. } else {
  213. editor.queryCommandState(cmds[1]) != -1 && editor.execCommand(cmds[1]);
  214. }
  215. }
  216. editor.execCommand("edittable", tone.value);
  217. autoSizeContent.checked ? editor.execCommand('adaptbytext') : "";
  218. autoSizePage.checked ? editor.execCommand("adaptbywindow") : "";
  219. editor.fireEvent('saveScene');
  220. editor.__hasEnterExecCommand = false;
  221. };
  222. })();