8732ce1d65fb82f0491a8edfd43f3e05ea621b27.svn-base 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656
  1. /*
  2. * Ext JS Library 2.0 Copyright(c) 2006-2007, Ext JS, LLC. licensing@extjs.com
  3. *
  4. * http://extjs.com/license
  5. */
  6. Ext.form.HtmlEditor = Ext.extend(Ext.form.Field, {
  7. enableFormat : true,
  8. enableFontSize : true,
  9. enableColors : true,
  10. enableAlignments : true,
  11. enableLists : true,
  12. enableSourceEdit : true,
  13. enableLinks : true,
  14. enableFont : true,
  15. createLinkText : "Please enter the URL for the link:",
  16. defaultLinkValue : "http:/" + "/",
  17. fontFamilies : ["Arial", "Courier New", "Tahoma", "Times New Roman",
  18. "Verdana"],
  19. defaultFont : "tahoma",
  20. validationEvent : false,
  21. deferHeight : true,
  22. initialized : false,
  23. activated : false,
  24. sourceEditMode : false,
  25. onFocus : Ext.emptyFn,
  26. iframePad : 3,
  27. hideMode : "offsets",
  28. defaultAutoCreate : {
  29. tag : "textarea",
  30. style : "width:500px;height:300px;",
  31. autocomplete : "off"
  32. },
  33. initComponent : function() {
  34. this.addEvents("initialize", "activate", "beforesync", "beforepush",
  35. "sync", "push", "editmodechange")
  36. },
  37. createFontOptions : function() {
  38. var D = [], B = this.fontFamilies, C, F;
  39. for (var E = 0, A = B.length; E < A; E++) {
  40. C = B[E];
  41. F = C.toLowerCase();
  42. D.push("<option value=\"", F, "\" style=\"font-family:", C, ";\"",
  43. (this.defaultFont == F ? " selected=\"true\">" : ">"), C,
  44. "</option>")
  45. }
  46. return D.join("")
  47. },
  48. createToolbar : function(C) {
  49. function B(F, D, E) {
  50. return {
  51. itemId : F,
  52. cls : "x-btn-icon x-edit-" + F,
  53. enableToggle : D !== false,
  54. scope : C,
  55. handler : E || C.relayBtnCmd,
  56. clickEvent : "mousedown",
  57. tooltip : C.buttonTips[F] || undefined,
  58. tabIndex : -1
  59. }
  60. }
  61. var A = new Ext.Toolbar({
  62. renderTo : this.wrap.dom.firstChild
  63. });
  64. A.el.on("click", function(D) {
  65. D.preventDefault()
  66. });
  67. if (this.enableFont && !Ext.isSafari) {
  68. this.fontSelect = A.el.createChild({
  69. tag : "select",
  70. cls : "x-font-select",
  71. html : this.createFontOptions()
  72. });
  73. this.fontSelect.on("change", function() {
  74. var D = this.fontSelect.dom.value;
  75. this.relayCmd("fontname", D);
  76. this.deferFocus()
  77. }, this);
  78. A.add(this.fontSelect.dom, "-")
  79. }
  80. if (this.enableFormat) {
  81. A.add(B("bold"), B("italic"), B("underline"))
  82. }
  83. if (this.enableFontSize) {
  84. A.add("-", B("increasefontsize", false, this.adjustFont), B(
  85. "decreasefontsize", false, this.adjustFont))
  86. }
  87. if (this.enableColors) {
  88. A.add("-", {
  89. itemId : "forecolor",
  90. cls : "x-btn-icon x-edit-forecolor",
  91. clickEvent : "mousedown",
  92. tooltip : C.buttonTips["forecolor"] || undefined,
  93. tabIndex : -1,
  94. menu : new Ext.menu.ColorMenu({
  95. allowReselect : true,
  96. focus : Ext.emptyFn,
  97. value : "000000",
  98. plain : true,
  99. selectHandler : function(E, D) {
  100. this.execCmd("forecolor", Ext.isSafari
  101. || Ext.isIE
  102. ? "#" + D
  103. : D);
  104. this.deferFocus()
  105. },
  106. scope : this,
  107. clickEvent : "mousedown"
  108. })
  109. }, {
  110. itemId : "backcolor",
  111. cls : "x-btn-icon x-edit-backcolor",
  112. clickEvent : "mousedown",
  113. tooltip : C.buttonTips["backcolor"] || undefined,
  114. tabIndex : -1,
  115. menu : new Ext.menu.ColorMenu({
  116. focus : Ext.emptyFn,
  117. value : "FFFFFF",
  118. plain : true,
  119. allowReselect : true,
  120. selectHandler : function(E, D) {
  121. if (Ext.isGecko) {
  122. this.execCmd("useCSS", false);
  123. this.execCmd("hilitecolor", D);
  124. this.execCmd("useCSS", true);
  125. this.deferFocus()
  126. } else {
  127. this.execCmd(Ext.isOpera
  128. ? "hilitecolor"
  129. : "backcolor",
  130. Ext.isSafari || Ext.isIE
  131. ? "#" + D
  132. : D);
  133. this.deferFocus()
  134. }
  135. },
  136. scope : this,
  137. clickEvent : "mousedown"
  138. })
  139. })
  140. }
  141. if (this.enableAlignments) {
  142. A.add("-", B("justifyleft"), B("justifycenter"), B("justifyright"))
  143. }
  144. if (!Ext.isSafari) {
  145. if (this.enableLinks) {
  146. A.add("-", B("createlink", false, this.createLink))
  147. }
  148. if (this.enableLists) {
  149. A.add("-", B("insertorderedlist"), B("insertunorderedlist"))
  150. }
  151. if (this.enableSourceEdit) {
  152. A.add("-", B("sourceedit", true, function(D) {
  153. this.toggleSourceEdit(D.pressed)
  154. }))
  155. }
  156. }
  157. this.tb = A
  158. },
  159. getDocMarkup : function() {
  160. return "<html><head><style type=\"text/css\">body{border:0;margin:0;padding:3px;height:98%;cursor:text;}</style></head><body></body></html>"
  161. },
  162. getEditorBody : function() {
  163. return this.doc.body || this.doc.documentElement
  164. },
  165. onRender : function(C, A) {
  166. Ext.form.HtmlEditor.superclass.onRender.call(this, C, A);
  167. this.el.dom.style.border = "0 none";
  168. this.el.dom.setAttribute("tabIndex", -1);
  169. this.el.addClass("x-hidden");
  170. if (Ext.isIE) {
  171. this.el.applyStyles("margin-top:-1px;margin-bottom:-1px;")
  172. }
  173. this.wrap = this.el.wrap({
  174. cls : "x-html-editor-wrap",
  175. cn : {
  176. cls : "x-html-editor-tb"
  177. }
  178. });
  179. this.createToolbar(this);
  180. this.tb.items.each(function(E) {
  181. if (E.itemId != "sourceedit") {
  182. E.disable()
  183. }
  184. });
  185. var D = document.createElement("iframe");
  186. D.name = Ext.id();
  187. D.frameBorder = "no";
  188. D.src = (Ext.SSL_SECURE_URL || "javascript:false");
  189. this.wrap.dom.appendChild(D);
  190. this.iframe = D;
  191. if (Ext.isIE) {
  192. D.contentWindow.document.designMode = "on";
  193. this.doc = D.contentWindow.document;
  194. this.win = D.contentWindow
  195. } else {
  196. this.doc = (D.contentDocument || window.frames[D.name].document);
  197. this.win = window.frames[D.name];
  198. this.doc.designMode = "on"
  199. }
  200. this.doc.open();
  201. this.doc.write(this.getDocMarkup());
  202. this.doc.close();
  203. var B = {
  204. run : function() {
  205. if (this.doc.body || this.doc.readyState == "complete") {
  206. Ext.TaskMgr.stop(B);
  207. this.doc.designMode = "on";
  208. this.initEditor.defer(10, this)
  209. }
  210. },
  211. interval : 10,
  212. duration : 10000,
  213. scope : this
  214. };
  215. Ext.TaskMgr.start(B);
  216. if (!this.width) {
  217. this.setSize(this.el.getSize())
  218. }
  219. },
  220. onResize : function(B, C) {
  221. Ext.form.HtmlEditor.superclass.onResize.apply(this, arguments);
  222. if (this.el && this.iframe) {
  223. if (typeof B == "number") {
  224. var D = B - this.wrap.getFrameWidth("lr");
  225. this.el.setWidth(this.adjustWidth("textarea", D));
  226. this.iframe.style.width = D + "px"
  227. }
  228. if (typeof C == "number") {
  229. var A = C - this.wrap.getFrameWidth("tb")
  230. - this.tb.el.getHeight();
  231. this.el.setHeight(this.adjustWidth("textarea", A));
  232. this.iframe.style.height = A + "px";
  233. if (this.doc) {
  234. this.getEditorBody().style.height = (A - (this.iframePad * 2))
  235. + "px"
  236. }
  237. }
  238. }
  239. },
  240. toggleSourceEdit : function(A) {
  241. if (A === undefined) {
  242. A = !this.sourceEditMode
  243. }
  244. this.sourceEditMode = A === true;
  245. var C = this.tb.items.get("sourceedit");
  246. if (C.pressed !== this.sourceEditMode) {
  247. C.toggle(this.sourceEditMode);
  248. return
  249. }
  250. if (this.sourceEditMode) {
  251. this.tb.items.each(function(D) {
  252. if (D.itemId != "sourceedit") {
  253. D.disable()
  254. }
  255. });
  256. this.syncValue();
  257. this.iframe.className = "x-hidden";
  258. this.el.removeClass("x-hidden");
  259. this.el.dom.removeAttribute("tabIndex");
  260. this.el.focus()
  261. } else {
  262. if (this.initialized) {
  263. this.tb.items.each(function(D) {
  264. D.enable()
  265. })
  266. }
  267. this.pushValue();
  268. this.iframe.className = "";
  269. this.el.addClass("x-hidden");
  270. this.el.dom.setAttribute("tabIndex", -1);
  271. this.deferFocus()
  272. }
  273. var B = this.lastSize;
  274. if (B) {
  275. delete this.lastSize;
  276. this.setSize(B)
  277. }
  278. this.fireEvent("editmodechange", this, this.sourceEditMode)
  279. },
  280. createLink : function() {
  281. var A = prompt(this.createLinkText, this.defaultLinkValue);
  282. if (A && A != "http:/" + "/") {
  283. this.relayCmd("createlink", A)
  284. }
  285. },
  286. adjustSize : Ext.BoxComponent.prototype.adjustSize,
  287. getResizeEl : function() {
  288. return this.wrap
  289. },
  290. getPositionEl : function() {
  291. return this.wrap
  292. },
  293. initEvents : function() {
  294. this.originalValue = this.getValue()
  295. },
  296. markInvalid : Ext.emptyFn,
  297. clearInvalid : Ext.emptyFn,
  298. setValue : function(A) {
  299. Ext.form.HtmlEditor.superclass.setValue.call(this, A);
  300. this.pushValue()
  301. },
  302. cleanHtml : function(A) {
  303. A = String(A);
  304. if (A.length > 5) {
  305. if (Ext.isSafari) {
  306. A = A
  307. .replace(
  308. /\sclass="(?:Apple-style-span|khtml-block-placeholder)"/gi,
  309. "")
  310. }
  311. }
  312. if (A == "&nbsp;") {
  313. A = ""
  314. }
  315. return A
  316. },
  317. syncValue : function() {
  318. if (this.initialized) {
  319. var D = this.getEditorBody();
  320. var C = D.innerHTML;
  321. if (Ext.isSafari) {
  322. var B = D.getAttribute("style");
  323. var A = B.match(/text-align:(.*?);/i);
  324. if (A && A[1]) {
  325. C = "<div style=\"" + A[0] + "\">" + C + "</div>"
  326. }
  327. }
  328. C = this.cleanHtml(C);
  329. if (this.fireEvent("beforesync", this, C) !== false) {
  330. this.el.dom.value = C;
  331. this.fireEvent("sync", this, C)
  332. }
  333. }
  334. },
  335. pushValue : function() {
  336. if (this.initialized) {
  337. var A = this.el.dom.value;
  338. if (!this.activated && A.length < 1) {
  339. A = "&nbsp;"
  340. }
  341. if (this.fireEvent("beforepush", this, A) !== false) {
  342. this.getEditorBody().innerHTML = A;
  343. this.fireEvent("push", this, A)
  344. }
  345. }
  346. },
  347. deferFocus : function() {
  348. this.focus.defer(10, this)
  349. },
  350. focus : function() {
  351. if (this.win && !this.sourceEditMode) {
  352. this.win.focus()
  353. } else {
  354. this.el.focus()
  355. }
  356. },
  357. initEditor : function() {
  358. var B = this.getEditorBody();
  359. var A = this.el.getStyles("font-size", "font-family",
  360. "background-image", "background-repeat");
  361. A["background-attachment"] = "fixed";
  362. B.bgProperties = "fixed";
  363. Ext.DomHelper.applyStyles(B, A);
  364. Ext.EventManager.on(this.doc, {
  365. "mousedown" : this.onEditorEvent,
  366. "dblclick" : this.onEditorEvent,
  367. "click" : this.onEditorEvent,
  368. "keyup" : this.onEditorEvent,
  369. buffer : 100,
  370. scope : this
  371. });
  372. if (Ext.isGecko) {
  373. Ext.EventManager.on(this.doc, "keypress", this.applyCommand, this)
  374. }
  375. if (Ext.isIE || Ext.isSafari || Ext.isOpera) {
  376. Ext.EventManager.on(this.doc, "keydown", this.fixKeys, this)
  377. }
  378. this.initialized = true;
  379. this.fireEvent("initialize", this);
  380. this.pushValue()
  381. },
  382. onDestroy : function() {
  383. if (this.rendered) {
  384. this.tb.items.each(function(A) {
  385. if (A.menu) {
  386. A.menu.removeAll();
  387. if (A.menu.el) {
  388. A.menu.el.destroy()
  389. }
  390. }
  391. A.destroy()
  392. });
  393. this.wrap.dom.innerHTML = "";
  394. this.wrap.remove()
  395. }
  396. },
  397. onFirstFocus : function() {
  398. this.activated = true;
  399. this.tb.items.each(function(D) {
  400. D.enable()
  401. });
  402. if (Ext.isGecko) {
  403. this.win.focus();
  404. var A = this.win.getSelection();
  405. if (!A.focusNode || A.focusNode.nodeType != 3) {
  406. var B = A.getRangeAt(0);
  407. B.selectNodeContents(this.getEditorBody());
  408. B.collapse(true);
  409. this.deferFocus()
  410. }
  411. try {
  412. this.execCmd("useCSS", true);
  413. this.execCmd("styleWithCSS", false)
  414. } catch (C) {
  415. }
  416. }
  417. this.fireEvent("activate", this)
  418. },
  419. adjustFont : function(B) {
  420. var C = B.itemId == "increasefontsize" ? 1 : -1;
  421. if (Ext.isSafari) {
  422. C *= 2
  423. }
  424. var A = parseInt(this.doc.queryCommandValue("FontSize") || 3, 10);
  425. A = Math.max(1, A + C);
  426. this.execCmd("FontSize", A + (Ext.isSafari ? "px" : 0))
  427. },
  428. onEditorEvent : function(A) {
  429. this.updateToolbar()
  430. },
  431. updateToolbar : function() {
  432. if (!this.activated) {
  433. this.onFirstFocus();
  434. return
  435. }
  436. var B = this.tb.items.map, C = this.doc;
  437. if (this.enableFont && !Ext.isSafari) {
  438. var A = (this.doc.queryCommandValue("FontName") || this.defaultFont)
  439. .toLowerCase();
  440. if (A != this.fontSelect.dom.value) {
  441. this.fontSelect.dom.value = A
  442. }
  443. }
  444. if (this.enableFormat) {
  445. B.bold.toggle(C.queryCommandState("bold"));
  446. B.italic.toggle(C.queryCommandState("italic"));
  447. B.underline.toggle(C.queryCommandState("underline"))
  448. }
  449. if (this.enableAlignments) {
  450. B.justifyleft.toggle(C.queryCommandState("justifyleft"));
  451. B.justifycenter.toggle(C.queryCommandState("justifycenter"));
  452. B.justifyright.toggle(C.queryCommandState("justifyright"))
  453. }
  454. if (!Ext.isSafari && this.enableLists) {
  455. B.insertorderedlist
  456. .toggle(C.queryCommandState("insertorderedlist"));
  457. B.insertunorderedlist.toggle(C
  458. .queryCommandState("insertunorderedlist"))
  459. }
  460. Ext.menu.MenuMgr.hideAll();
  461. this.syncValue()
  462. },
  463. relayBtnCmd : function(A) {
  464. this.relayCmd(A.itemId)
  465. },
  466. relayCmd : function(B, A) {
  467. this.win.focus();
  468. this.execCmd(B, A);
  469. this.updateToolbar();
  470. this.deferFocus()
  471. },
  472. execCmd : function(B, A) {
  473. this.doc.execCommand(B, false, A === undefined ? null : A);
  474. this.syncValue()
  475. },
  476. applyCommand : function(B) {
  477. if (B.ctrlKey) {
  478. var C = B.getCharCode(), A;
  479. if (C > 0) {
  480. C = String.fromCharCode(C);
  481. switch (C) {
  482. case "b" :
  483. A = "bold";
  484. break;
  485. case "i" :
  486. A = "italic";
  487. break;
  488. case "u" :
  489. A = "underline";
  490. break
  491. }
  492. if (A) {
  493. this.win.focus();
  494. this.execCmd(A);
  495. this.deferFocus();
  496. B.preventDefault()
  497. }
  498. }
  499. }
  500. },
  501. insertAtCursor : function(B) {
  502. if (!this.activated) {
  503. return
  504. }
  505. if (Ext.isIE) {
  506. this.win.focus();
  507. var A = this.doc.selection.createRange();
  508. if (A) {
  509. A.collapse(true);
  510. A.pasteHTML(B);
  511. this.syncValue();
  512. this.deferFocus()
  513. }
  514. } else {
  515. if (Ext.isGecko || Ext.isOpera) {
  516. this.win.focus();
  517. this.execCmd("InsertHTML", B);
  518. this.deferFocus()
  519. } else {
  520. if (Ext.isSafari) {
  521. this.execCmd("InsertText", B);
  522. this.deferFocus()
  523. }
  524. }
  525. }
  526. },
  527. fixKeys : function() {
  528. if (Ext.isIE) {
  529. return function(D) {
  530. var A = D.getKey(), B;
  531. if (A == D.TAB) {
  532. D.stopEvent();
  533. B = this.doc.selection.createRange();
  534. if (B) {
  535. B.collapse(true);
  536. B.pasteHTML("&nbsp;&nbsp;&nbsp;&nbsp;");
  537. this.deferFocus()
  538. }
  539. } else {
  540. if (A == D.ENTER) {
  541. B = this.doc.selection.createRange();
  542. if (B) {
  543. var C = B.parentElement();
  544. if (!C || C.tagName.toLowerCase() != "li") {
  545. D.stopEvent();
  546. B.pasteHTML("<br />");
  547. B.collapse(false);
  548. B.select()
  549. }
  550. }
  551. }
  552. }
  553. }
  554. } else {
  555. if (Ext.isOpera) {
  556. return function(B) {
  557. var A = B.getKey();
  558. if (A == B.TAB) {
  559. B.stopEvent();
  560. this.win.focus();
  561. this.execCmd("InsertHTML", "&nbsp;&nbsp;&nbsp;&nbsp;");
  562. this.deferFocus()
  563. }
  564. }
  565. } else {
  566. if (Ext.isSafari) {
  567. return function(B) {
  568. var A = B.getKey();
  569. if (A == B.TAB) {
  570. B.stopEvent();
  571. this.execCmd("InsertText", "\t");
  572. this.deferFocus()
  573. }
  574. }
  575. }
  576. }
  577. }
  578. }(),
  579. getToolbar : function() {
  580. return this.tb
  581. },
  582. buttonTips : {
  583. bold : {
  584. title : "Bold (Ctrl+B)",
  585. text : "Make the selected text bold.",
  586. cls : "x-html-editor-tip"
  587. },
  588. italic : {
  589. title : "Italic (Ctrl+I)",
  590. text : "Make the selected text italic.",
  591. cls : "x-html-editor-tip"
  592. },
  593. underline : {
  594. title : "Underline (Ctrl+U)",
  595. text : "Underline the selected text.",
  596. cls : "x-html-editor-tip"
  597. },
  598. increasefontsize : {
  599. title : "Grow Text",
  600. text : "Increase the font size.",
  601. cls : "x-html-editor-tip"
  602. },
  603. decreasefontsize : {
  604. title : "Shrink Text",
  605. text : "Decrease the font size.",
  606. cls : "x-html-editor-tip"
  607. },
  608. backcolor : {
  609. title : "Text Highlight Color",
  610. text : "Change the background color of the selected text.",
  611. cls : "x-html-editor-tip"
  612. },
  613. forecolor : {
  614. title : "Font Color",
  615. text : "Change the color of the selected text.",
  616. cls : "x-html-editor-tip"
  617. },
  618. justifyleft : {
  619. title : "Align Text Left",
  620. text : "Align text to the left.",
  621. cls : "x-html-editor-tip"
  622. },
  623. justifycenter : {
  624. title : "Center Text",
  625. text : "Center text in the editor.",
  626. cls : "x-html-editor-tip"
  627. },
  628. justifyright : {
  629. title : "Align Text Right",
  630. text : "Align text to the right.",
  631. cls : "x-html-editor-tip"
  632. },
  633. insertunorderedlist : {
  634. title : "Bullet List",
  635. text : "Start a bulleted list.",
  636. cls : "x-html-editor-tip"
  637. },
  638. insertorderedlist : {
  639. title : "Numbered List",
  640. text : "Start a numbered list.",
  641. cls : "x-html-editor-tip"
  642. },
  643. createlink : {
  644. title : "Hyperlink",
  645. text : "Make the selected text a hyperlink.",
  646. cls : "x-html-editor-tip"
  647. },
  648. sourceedit : {
  649. title : "Source Edit",
  650. text : "Switch to source editing mode.",
  651. cls : "x-html-editor-tip"
  652. }
  653. }
  654. });
  655. Ext.reg("htmleditor", Ext.form.HtmlEditor);