table.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  1. /*
  2. Copyright (c) 2003-2019, CKSource - Frederico Knabben. All rights reserved.
  3. For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
  4. */
  5. (function() {
  6. function w(a) {
  7. for (var f = 0, p = 0, n = 0, q, e = a.$.rows.length; n < e; n++) {
  8. q = a.$.rows[n];
  9. for (var d = (f = 0), b, c = q.cells.length; d < c; d++)
  10. (b = q.cells[d]), (f += b.colSpan);
  11. f > p && (p = f);
  12. }
  13. return p;
  14. }
  15. function t(a) {
  16. return function() {
  17. var f = this.getValue(),
  18. f = !!(CKEDITOR.dialog.validate.integer().call(this, f) && 0 < f);
  19. f || (alert(a), this.select());
  20. return f;
  21. };
  22. }
  23. function r(a, f) {
  24. var p = function(e) {
  25. return new CKEDITOR.dom.element(e, a.document);
  26. },
  27. r = a.editable(),
  28. q = a.plugins.dialogadvtab;
  29. return {
  30. title: a.lang.table.title,
  31. minWidth: 310,
  32. minHeight: CKEDITOR.env.ie ? 310 : 280,
  33. onLoad: function() {
  34. var e = this,
  35. a = e.getContentElement("advanced", "advStyles");
  36. if (a)
  37. a.on("change", function() {
  38. var a = this.getStyle("width", ""),
  39. c = e.getContentElement("info", "txtWidth");
  40. c && c.setValue(a, !0);
  41. a = this.getStyle("height", "");
  42. (c = e.getContentElement("info", "txtHeight")) && c.setValue(a, !0);
  43. });
  44. },
  45. onShow: function() {
  46. var e = a.getSelection(),
  47. d = e.getRanges(),
  48. b,
  49. c = this.getContentElement("info", "txtRows"),
  50. h = this.getContentElement("info", "txtCols"),
  51. u = this.getContentElement("info", "txtWidth"),
  52. l = this.getContentElement("info", "txtHeight");
  53. "tableProperties" == f &&
  54. ((e = e.getSelectedElement()) && e.is("table")
  55. ? (b = e)
  56. : 0 < d.length &&
  57. (CKEDITOR.env.webkit && d[0].shrink(CKEDITOR.NODE_ELEMENT),
  58. (b = a
  59. .elementPath(d[0].getCommonAncestor(!0))
  60. .contains("table", 1))),
  61. (this._.selectedElement = b));
  62. b
  63. ? (this.setupContent(b), c && c.disable(), h && h.disable())
  64. : (c && c.enable(), h && h.enable());
  65. u && u.onChange();
  66. l && l.onChange();
  67. },
  68. onOk: function() {
  69. var e = a.getSelection(),
  70. d = this._.selectedElement && e.createBookmarks(),
  71. b = this._.selectedElement || p("table"),
  72. c = {};
  73. this.commitContent(c, b);
  74. if (c.info) {
  75. c = c.info;
  76. if (!this._.selectedElement)
  77. for (
  78. var h = b.append(p("tbody")),
  79. f = parseInt(c.txtRows, 10) || 0,
  80. l = parseInt(c.txtCols, 10) || 0,
  81. k = 0;
  82. k < f;
  83. k++
  84. )
  85. for (var g = h.append(p("tr")), m = 0; m < l; m++)
  86. g.append(p("td")).appendBogus();
  87. f = c.selHeaders;
  88. if (!b.$.tHead && ("row" == f || "both" == f)) {
  89. g = b.getElementsByTag("thead").getItem(0);
  90. h = b.getElementsByTag("tbody").getItem(0);
  91. l = h.getElementsByTag("tr").getItem(0);
  92. g || ((g = new CKEDITOR.dom.element("thead")), g.insertBefore(h));
  93. for (k = 0; k < l.getChildCount(); k++)
  94. (h = l.getChild(k)),
  95. h.type != CKEDITOR.NODE_ELEMENT ||
  96. h.data("cke-bookmark") ||
  97. (h.renameNode("th"), h.setAttribute("scope", "col"));
  98. g.append(l.remove());
  99. }
  100. if (null !== b.$.tHead && "row" != f && "both" != f) {
  101. g = new CKEDITOR.dom.element(b.$.tHead);
  102. for (
  103. h = b.getElementsByTag("tbody").getItem(0);
  104. 0 < g.getChildCount();
  105. ) {
  106. l = g.getFirst();
  107. for (k = 0; k < l.getChildCount(); k++)
  108. (m = l.getChild(k)),
  109. m.type == CKEDITOR.NODE_ELEMENT &&
  110. (m.renameNode("td"), m.removeAttribute("scope"));
  111. h.append(l, !0);
  112. }
  113. g.remove();
  114. }
  115. if (!this.hasColumnHeaders && ("col" == f || "both" == f))
  116. for (g = 0; g < b.$.rows.length; g++)
  117. (m = new CKEDITOR.dom.element(b.$.rows[g].cells[0])),
  118. m.renameNode("th"),
  119. m.setAttribute("scope", "row");
  120. if (this.hasColumnHeaders && "col" != f && "both" != f)
  121. for (k = 0; k < b.$.rows.length; k++)
  122. (g = new CKEDITOR.dom.element(b.$.rows[k])),
  123. "tbody" == g.getParent().getName() &&
  124. ((m = new CKEDITOR.dom.element(g.$.cells[0])),
  125. m.renameNode("td"),
  126. m.removeAttribute("scope"));
  127. c.txtHeight
  128. ? b.setStyle("height", c.txtHeight)
  129. : b.removeStyle("height");
  130. c.txtWidth ? b.setStyle("width", c.txtWidth) : b.removeStyle("width");
  131. b.getAttribute("style") || b.removeAttribute("style");
  132. }
  133. if (this._.selectedElement)
  134. try {
  135. e.selectBookmarks(d);
  136. } catch (n) {}
  137. else
  138. a.insertElement(b),
  139. setTimeout(function() {
  140. var e = new CKEDITOR.dom.element(b.$.rows[0].cells[0]),
  141. c = a.createRange();
  142. c.moveToPosition(e, CKEDITOR.POSITION_AFTER_START);
  143. c.select();
  144. }, 0);
  145. },
  146. contents: [
  147. {
  148. id: "info",
  149. label: a.lang.table.title,
  150. elements: [
  151. {
  152. type: "hbox",
  153. widths: [null, null],
  154. styles: ["vertical-align:top"],
  155. children: [
  156. {
  157. type: "vbox",
  158. padding: 0,
  159. children: [
  160. {
  161. type: "text",
  162. id: "txtRows",
  163. default: 3,
  164. label: a.lang.table.rows,
  165. required: !0,
  166. controlStyle: "width:5em",
  167. validate: t(a.lang.table.invalidRows),
  168. setup: function(e) {
  169. this.setValue(e.$.rows.length);
  170. },
  171. commit: n
  172. },
  173. {
  174. type: "text",
  175. id: "txtCols",
  176. default: 2,
  177. label: a.lang.table.columns,
  178. required: !0,
  179. controlStyle: "width:5em",
  180. validate: t(a.lang.table.invalidCols),
  181. setup: function(e) {
  182. this.setValue(w(e));
  183. },
  184. commit: n
  185. },
  186. { type: "html", html: "\x26nbsp;" },
  187. {
  188. type: "select",
  189. id: "selHeaders",
  190. requiredContent: "th",
  191. default: "",
  192. label: a.lang.table.headers,
  193. items: [
  194. [a.lang.table.headersNone, ""],
  195. [a.lang.table.headersRow, "row"],
  196. [a.lang.table.headersColumn, "col"],
  197. [a.lang.table.headersBoth, "both"]
  198. ],
  199. setup: function(e) {
  200. var a = this.getDialog();
  201. a.hasColumnHeaders = !0;
  202. for (var b = 0; b < e.$.rows.length; b++) {
  203. var c = e.$.rows[b].cells[0];
  204. if (c && "th" != c.nodeName.toLowerCase()) {
  205. a.hasColumnHeaders = !1;
  206. break;
  207. }
  208. }
  209. null !== e.$.tHead
  210. ? this.setValue(a.hasColumnHeaders ? "both" : "row")
  211. : this.setValue(a.hasColumnHeaders ? "col" : "");
  212. },
  213. commit: n
  214. },
  215. {
  216. type: "text",
  217. id: "txtBorder",
  218. requiredContent: "table[border]",
  219. default: a.filter.check("table[border]") ? 1 : 0,
  220. label: a.lang.table.border,
  221. controlStyle: "width:3em",
  222. validate: CKEDITOR.dialog.validate.number(
  223. a.lang.table.invalidBorder
  224. ),
  225. setup: function(a) {
  226. this.setValue(a.getAttribute("border") || "");
  227. },
  228. commit: function(a, d) {
  229. this.getValue()
  230. ? d.setAttribute("border", this.getValue())
  231. : d.removeAttribute("border");
  232. }
  233. },
  234. {
  235. id: "cmbAlign",
  236. type: "select",
  237. requiredContent: "table[align]",
  238. default: "",
  239. label: a.lang.common.align,
  240. items: [
  241. [a.lang.common.notSet, ""],
  242. [a.lang.common.left, "left"],
  243. [a.lang.common.center, "center"],
  244. [a.lang.common.right, "right"]
  245. ],
  246. setup: function(a) {
  247. this.setValue(a.getAttribute("align") || "");
  248. },
  249. commit: function(a, d) {
  250. this.getValue()
  251. ? d.setAttribute("align", this.getValue())
  252. : d.removeAttribute("align");
  253. }
  254. }
  255. ]
  256. },
  257. {
  258. type: "vbox",
  259. padding: 0,
  260. children: [
  261. {
  262. type: "hbox",
  263. widths: ["5em"],
  264. children: [
  265. {
  266. type: "text",
  267. id: "txtWidth",
  268. requiredContent: "table{width}",
  269. controlStyle: "width:5em",
  270. label: a.lang.common.width,
  271. title: a.lang.common.cssLengthTooltip,
  272. default: a.filter.check("table{width}")
  273. ? 500 > r.getSize("width")
  274. ? "100%"
  275. : 500
  276. : 0,
  277. getValue: v,
  278. validate: CKEDITOR.dialog.validate.cssLength(
  279. a.lang.common.invalidCssLength.replace(
  280. "%1",
  281. a.lang.common.width
  282. )
  283. ),
  284. onChange: function() {
  285. var a = this.getDialog().getContentElement(
  286. "advanced",
  287. "advStyles"
  288. );
  289. a && a.updateStyle("width", this.getValue());
  290. },
  291. setup: function(a) {
  292. a = a.getStyle("width");
  293. this.setValue(a);
  294. },
  295. commit: n
  296. }
  297. ]
  298. },
  299. {
  300. type: "hbox",
  301. widths: ["5em"],
  302. children: [
  303. {
  304. type: "text",
  305. id: "txtHeight",
  306. requiredContent: "table{height}",
  307. controlStyle: "width:5em",
  308. label: a.lang.common.height,
  309. title: a.lang.common.cssLengthTooltip,
  310. default: "",
  311. getValue: v,
  312. validate: CKEDITOR.dialog.validate.cssLength(
  313. a.lang.common.invalidCssLength.replace(
  314. "%1",
  315. a.lang.common.height
  316. )
  317. ),
  318. onChange: function() {
  319. var a = this.getDialog().getContentElement(
  320. "advanced",
  321. "advStyles"
  322. );
  323. a && a.updateStyle("height", this.getValue());
  324. },
  325. setup: function(a) {
  326. (a = a.getStyle("height")) && this.setValue(a);
  327. },
  328. commit: n
  329. }
  330. ]
  331. },
  332. { type: "html", html: "\x26nbsp;" },
  333. {
  334. type: "text",
  335. id: "txtCellSpace",
  336. requiredContent: "table[cellspacing]",
  337. controlStyle: "width:3em",
  338. label: a.lang.table.cellSpace,
  339. default: a.filter.check("table[cellspacing]") ? 1 : 0,
  340. validate: CKEDITOR.dialog.validate.number(
  341. a.lang.table.invalidCellSpacing
  342. ),
  343. setup: function(a) {
  344. this.setValue(a.getAttribute("cellSpacing") || "");
  345. },
  346. commit: function(a, d) {
  347. this.getValue()
  348. ? d.setAttribute("cellSpacing", this.getValue())
  349. : d.removeAttribute("cellSpacing");
  350. }
  351. },
  352. {
  353. type: "text",
  354. id: "txtCellPad",
  355. requiredContent: "table[cellpadding]",
  356. controlStyle: "width:3em",
  357. label: a.lang.table.cellPad,
  358. default: a.filter.check("table[cellpadding]") ? 1 : 0,
  359. validate: CKEDITOR.dialog.validate.number(
  360. a.lang.table.invalidCellPadding
  361. ),
  362. setup: function(a) {
  363. this.setValue(a.getAttribute("cellPadding") || "");
  364. },
  365. commit: function(a, d) {
  366. this.getValue()
  367. ? d.setAttribute("cellPadding", this.getValue())
  368. : d.removeAttribute("cellPadding");
  369. }
  370. }
  371. ]
  372. }
  373. ]
  374. },
  375. { type: "html", align: "right", html: "" },
  376. {
  377. type: "vbox",
  378. padding: 0,
  379. children: [
  380. {
  381. type: "text",
  382. id: "txtCaption",
  383. requiredContent: "caption",
  384. label: a.lang.table.caption,
  385. setup: function(a) {
  386. this.enable();
  387. a = a.getElementsByTag("caption");
  388. if (0 < a.count()) {
  389. a = a.getItem(0);
  390. var d = a.getFirst(
  391. CKEDITOR.dom.walker.nodeType(CKEDITOR.NODE_ELEMENT)
  392. );
  393. d && !d.equals(a.getBogus())
  394. ? (this.disable(), this.setValue(a.getText()))
  395. : ((a = CKEDITOR.tools.trim(a.getText())),
  396. this.setValue(a));
  397. }
  398. },
  399. commit: function(e, d) {
  400. if (this.isEnabled()) {
  401. var b = this.getValue(),
  402. c = d.getElementsByTag("caption");
  403. if (b)
  404. 0 < c.count()
  405. ? ((c = c.getItem(0)), c.setHtml(""))
  406. : ((c = new CKEDITOR.dom.element(
  407. "caption",
  408. a.document
  409. )),
  410. d.append(c, !0)),
  411. c.append(new CKEDITOR.dom.text(b, a.document));
  412. else if (0 < c.count())
  413. for (b = c.count() - 1; 0 <= b; b--)
  414. c.getItem(b).remove();
  415. }
  416. }
  417. },
  418. {
  419. type: "text",
  420. id: "txtSummary",
  421. bidi: !0,
  422. requiredContent: "table[summary]",
  423. label: a.lang.table.summary,
  424. setup: function(a) {
  425. this.setValue(a.getAttribute("summary") || "");
  426. },
  427. commit: function(a, d) {
  428. this.getValue()
  429. ? d.setAttribute("summary", this.getValue())
  430. : d.removeAttribute("summary");
  431. }
  432. }
  433. ]
  434. }
  435. ]
  436. },
  437. q && q.createAdvancedTab(a, null, "table")
  438. ]
  439. };
  440. }
  441. var v = CKEDITOR.tools.cssLength,
  442. n = function(a) {
  443. var f = this.id;
  444. a.info || (a.info = {});
  445. a.info[f] = this.getValue();
  446. };
  447. CKEDITOR.dialog.add("table", function(a) {
  448. return r(a, "table");
  449. });
  450. CKEDITOR.dialog.add("tableProperties", function(a) {
  451. return r(a, "tableProperties");
  452. });
  453. })();