kindeditor.js 97 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566
  1. /*******************************************************************************
  2. * KindEditor - WYSIWYG HTML Editor for Internet
  3. *
  4. * @author Roddy <luolonghao@gmail.com>
  5. * @site http://www.kindsoft.net/
  6. * @licence LGPL(http://www.opensource.org/licenses/lgpl-license.php)
  7. * @version 3.2.1
  8. *******************************************************************************/
  9. var KE = {};
  10. KE.version = '3.2.1';
  11. KE.lang = {
  12. source : '切换模式',
  13. preview : '预览',
  14. undo : '后退(Ctrl+Z)',
  15. redo : '前进(Ctrl+Y)',
  16. cut : '剪切(Ctrl+X)',
  17. copy : '复制(Ctrl+C)',
  18. paste : '粘贴(Ctrl+V)',
  19. plainpaste : '粘贴为无格式文本',
  20. wordpaste : '从Word粘贴',
  21. selectall : '全选',
  22. justifyleft : '左对齐',
  23. justifycenter : '居中',
  24. justifyright : '右对齐',
  25. justifyfull : '两端对齐',
  26. insertorderedlist : '编号',
  27. insertunorderedlist : '项目符号',
  28. indent : '增加缩进',
  29. outdent : '减少缩进',
  30. subscript : '下标',
  31. superscript : '上标',
  32. date : '插入当前日期',
  33. time : '插入当前时间',
  34. title : '标题',
  35. fontname : '字体',
  36. fontsize : '文字大小',
  37. textcolor : '文字颜色',
  38. bgcolor : '文字背景',
  39. bold : '粗体',
  40. italic : '斜体',
  41. underline : '下划线',
  42. strikethrough : '删除线',
  43. removeformat : '删除格式',
  44. image : '插入图片',
  45. flash : '插入Flash',
  46. media : '插入多媒体',
  47. layer : '插入层',
  48. table : '插入表格',
  49. specialchar : '插入特殊字符',
  50. hr : '插入横线',
  51. emoticons : '插入笑脸',
  52. link : '超级连接',
  53. unlink : '取消超级连接',
  54. fullscreen : '全屏显示',
  55. about : '关于',
  56. print : '打印',
  57. yes : '确定',
  58. no : '取消',
  59. close : '关闭',
  60. invalidImg : "请输入有效的URL地址。\n只允许jpg,gif,bmp,png格式。",
  61. invalidMedia : "请输入有效的URL地址。\n只允许mp3,wav,wma,wmv,mid,avi,mpg,asf,rm,rmvb格式。",
  62. invalidWidth : "宽度必须为数字。",
  63. invalidHeight : "高度必须为数字。",
  64. invalidBorder : "边框必须为数字。",
  65. invalidUrl : "URL不正确。",
  66. pleaseInput : "请输入内容"
  67. };
  68. KE.scriptPath = (function() {
  69. var elements = document.getElementsByTagName('script');
  70. for (var i = 0, len = elements.length; i < len; i++) {
  71. if (elements[i].src && elements[i].src.match(/kindeditor[\w\-\.]*\.js/)) {
  72. return elements[i].src.substring(0, elements[i].src.lastIndexOf('/') + 1);
  73. }
  74. }
  75. return "";
  76. })();
  77. KE.htmlPath = (function() {
  78. return location.href.substring(0, location.href.lastIndexOf('/') + 1);
  79. })();
  80. KE.browser = (function() {
  81. var ua = navigator.userAgent.toLowerCase();
  82. if (ua.indexOf("msie") > -1) return 'IE';
  83. else if (ua.indexOf("webkit") > -1) return 'WEBKIT';
  84. else if (ua.indexOf("gecko") > -1) return 'GECKO';
  85. else if (ua.indexOf("opera") > -1) return 'OPERA';
  86. else return "";
  87. })();
  88. KE.setting = {
  89. wyswygMode : true,
  90. autoOnsubmitMode : true,
  91. resizeMode : 2,
  92. filterMode : true,
  93. tagLineMode : false,
  94. skinType : 'default',
  95. cssPath : '',
  96. skinsPath : KE.scriptPath + 'skins/',
  97. pluginsPath : KE.scriptPath + 'plugins/',
  98. minWidth : 200,
  99. minHeight : 100,
  100. minChangeSize : 5,
  101. siteDomains : [],
  102. items : [
  103. 'source', 'preview', 'fullscreen', 'undo', 'redo', 'print', 'cut', 'copy', 'paste',
  104. 'plainpaste', 'wordpaste', 'justifyleft', 'justifycenter', 'justifyright',
  105. 'justifyfull', 'insertorderedlist', 'insertunorderedlist', 'indent', 'outdent', 'subscript',
  106. 'superscript', 'date', 'time', '-',
  107. 'title', 'fontname', 'fontsize', 'textcolor', 'bgcolor', 'bold',
  108. 'italic', 'underline', 'strikethrough', 'removeformat', 'selectall', 'image',
  109. 'flash', 'media', 'layer', 'table', 'specialchar', 'hr',
  110. 'emoticons', 'link', 'unlink', 'about'
  111. ],
  112. colorTable : [
  113. ["#FFFFFF","#E5E4E4","#D9D8D8","#C0BDBD","#A7A4A4","#8E8A8B","#827E7F","#767173","#5C585A","#000000"],
  114. ["#FEFCDF","#FEF4C4","#FEED9B","#FEE573","#FFED43","#F6CC0B","#E0B800","#C9A601","#AD8E00","#8C7301"],
  115. ["#FFDED3","#FFC4B0","#FF9D7D","#FF7A4E","#FF6600","#E95D00","#D15502","#BA4B01","#A44201","#8D3901"],
  116. ["#FFD2D0","#FFBAB7","#FE9A95","#FF7A73","#FF483F","#FE2419","#F10B00","#D40A00","#940000","#6D201B"],
  117. ["#FFDAED","#FFB7DC","#FFA1D1","#FF84C3","#FF57AC","#FD1289","#EC0078","#D6006D","#BB005F","#9B014F"],
  118. ["#FCD6FE","#FBBCFF","#F9A1FE","#F784FE","#F564FE","#F546FF","#F328FF","#D801E5","#C001CB","#8F0197"],
  119. ["#E2F0FE","#C7E2FE","#ADD5FE","#92C7FE","#6EB5FF","#48A2FF","#2690FE","#0162F4","#013ADD","#0021B0"],
  120. ["#D3FDFF","#ACFAFD","#7CFAFF","#4AF7FE","#1DE6FE","#01DEFF","#00CDEC","#01B6DE","#00A0C2","#0084A0"],
  121. ["#EDFFCF","#DFFEAA","#D1FD88","#BEFA5A","#A8F32A","#8FD80A","#79C101","#3FA701","#307F00","#156200"],
  122. ["#D4C89F","#DAAD88","#C49578","#C2877E","#AC8295","#C0A5C4","#969AC2","#92B7D7","#80ADAF","#9CA53B"]
  123. ],
  124. noEndTags : ['br', 'hr', 'img', 'area', 'col', 'embed', 'input', 'param'],
  125. inlineTags : ['b', 'del', 'em', 'font', 'i', 'span', 'strike', 'strong', 'sub', 'sup', 'u'],
  126. htmlTags : {
  127. font : ['color', 'size', 'face', '.background-color'],
  128. span : [
  129. '.color', '.background-color', '.font-size', '.font-family',
  130. '.font-weight', '.font-style', '.text-decoration', '.vertical-align'
  131. ],
  132. div : [
  133. 'class', 'align', '.border', '.margin', '.padding', '.text-align', '.color',
  134. '.background-color', '.font-size', '.font-family', '.font-weight',
  135. '.font-style', '.text-decoration', '.vertical-align'
  136. ],
  137. table: [
  138. 'class', 'border', 'cellspacing', 'cellpadding', 'width', 'height', 'align',
  139. '.padding', '.margin', '.border', 'bgcolor', '.text-align', '.color', '.background-color',
  140. '.font-size', '.font-family', '.font-weight', '.font-style', '.text-decoration'
  141. ],
  142. 'td,th': [
  143. 'class', 'align', 'valign', 'width', 'height', 'colspan', 'rowspan', 'bgcolor',
  144. '.text-align', '.color', '.background-color', '.font-size', '.font-family', '.font-weight',
  145. '.font-style', '.text-decoration', '.vertical-align'
  146. ],
  147. a : ['class', 'href', 'target', 'name'],
  148. embed : ['src', 'type', 'loop', 'autostart', 'quality', '.width', '.height', '/'],
  149. img : ['src', 'width', 'height', 'border', 'alt', 'title', '.width', '.height', '/'],
  150. hr : ['class', '/'],
  151. br : ['/'],
  152. 'p,ol,ul,li,blockquote,h1,h2,h3,h4,h5,h6' : [
  153. 'align', '.text-align', '.color', '.background-color', '.font-size', '.font-family',
  154. '.font-weight', '.font-style', '.text-decoration', '.vertical-align'
  155. ],
  156. 'tbody,tr,strong,b,sub,sup,em,i,u,strike' : []
  157. }
  158. };
  159. KE.g = {};
  160. KE.plugin = {};
  161. KE.$ = function(id, doc){
  162. var doc = doc || document;
  163. return doc.getElementById(id);
  164. };
  165. KE.$$ = function(name, doc){
  166. var doc = doc || document;
  167. return doc.createElement(name);
  168. };
  169. KE.event = {
  170. add : function(el, event, listener) {
  171. if (el.addEventListener){
  172. el.addEventListener(event, listener, false);
  173. } else if (el.attachEvent){
  174. el.attachEvent('on' + event, listener);
  175. }
  176. },
  177. remove : function(el, event, listener) {
  178. if (el.removeEventListener){
  179. el.removeEventListener(event, listener, false);
  180. } else if (el.detachEvent){
  181. el.detachEvent('on' + event, listener);
  182. }
  183. },
  184. input : function(el, func) {
  185. this.add(el, 'keyup', function(e) {
  186. if (!e.ctrlKey && !e.shiftKey && !e.altKey && (e.keyCode < 16 || e.keyCode > 18)) {
  187. func(e);
  188. return false;
  189. }
  190. });
  191. },
  192. ctrl : function(el, key, func) {
  193. this.add(el, 'keydown', function(e) {
  194. if (e.ctrlKey && e.keyCode == key.toUpperCase().charCodeAt(0) && !e.shiftKey && !e.altKey) {
  195. func(e);
  196. return false;
  197. }
  198. });
  199. }
  200. };
  201. KE.each = function(obj, func) {
  202. for (var key in obj) {
  203. if (obj.hasOwnProperty(key)) func(key, obj[key]);
  204. }
  205. };
  206. KE.eachNode = function(node, func) {
  207. var walkNodes = function(parent) {
  208. if (KE.util.getNodeType(parent) != 1) return true;
  209. var n = parent.firstChild;
  210. while (n != null) {
  211. var next = n.nextSibling;
  212. if (!func(n)) return false;
  213. if (!walkNodes(n)) return false;
  214. n = next;
  215. }
  216. return true;
  217. };
  218. walkNodes(node);
  219. };
  220. KE.selection = function(win, doc) {
  221. this.sel = null;
  222. this.range = null;
  223. this.keRange = null;
  224. this.init = function() {
  225. var sel = win.getSelection ? win.getSelection() : doc.selection;
  226. var range;
  227. try {
  228. if (sel.rangeCount > 0) range = sel.getRangeAt(0);
  229. else range = sel.createRange();
  230. } catch(e) {}
  231. if (!range) range = KE.util.createRange(doc);
  232. this.sel = sel;
  233. this.range = range;
  234. var startNode, startPos, endNode, endPos;
  235. if (KE.browser == 'IE') {
  236. if (range.item) {
  237. var el = range.item(0);
  238. startNode = endNode = el;
  239. startPos = endPos = 0;
  240. } else {
  241. var getStartEnd = function(isStart) {
  242. var pointRange = range.duplicate();
  243. pointRange.collapse(isStart);
  244. var parentNode = pointRange.parentElement();
  245. var nodes = parentNode.childNodes;
  246. if (nodes.length == 0) return {node: parentNode, pos: 0};
  247. var startNode;
  248. var endElement;
  249. var startPos = 0;
  250. var isEnd = false;
  251. var testRange = range.duplicate();
  252. testRange.moveToElementText(parentNode);
  253. for (var i = 0, len = nodes.length; i < len; i++) {
  254. var node = nodes[i];
  255. var cmp = testRange.compareEndPoints('StartToStart', pointRange);
  256. if (cmp > 0) {
  257. isEnd = true;
  258. } else if (cmp == 0) {
  259. if (node.nodeType == 1) {
  260. var keRange = new KE.range(doc);
  261. keRange.selectTextNode(node);
  262. return {node: keRange.startNode, pos: 0};
  263. } else {
  264. return {node: node, pos: 0};
  265. }
  266. }
  267. if (node.nodeType == 1) {
  268. var nodeRange = range.duplicate();
  269. nodeRange.moveToElementText(node);
  270. testRange.setEndPoint('StartToEnd', nodeRange);
  271. if (isEnd) startPos += nodeRange.text.length;
  272. else startPos = 0;
  273. } else if (node.nodeType == 3) {
  274. testRange.moveStart('character', node.nodeValue.length);
  275. startPos += node.nodeValue.length;
  276. }
  277. if (!isEnd) startNode = node;
  278. }
  279. if (!isEnd && startNode.nodeType == 1) {
  280. var startNode = parentNode.lastChild;
  281. return {node: startNode, pos: startNode.nodeType == 1 ? 1 : startNode.nodeValue.length};
  282. }
  283. testRange = range.duplicate();
  284. testRange.moveToElementText(parentNode);
  285. testRange.setEndPoint('StartToEnd', pointRange);
  286. startPos -= testRange.text.replace(/\r\n|\n|\r/g, '').length;
  287. return {node: startNode, pos: startPos};
  288. };
  289. var start = getStartEnd(true);
  290. var end = getStartEnd(false);
  291. startNode = start.node;
  292. startPos = start.pos;
  293. endNode = end.node;
  294. endPos = end.pos;
  295. }
  296. } else {
  297. startNode = range.startContainer;
  298. startPos = range.startOffset;
  299. endNode = range.endContainer;
  300. endPos = range.endOffset;
  301. if (startNode.nodeType == 1 && typeof startNode.childNodes[startPos] != "undefined") {
  302. startNode = startNode.childNodes[startPos];
  303. startPos = startNode.nodeType == 1 ? 0 : startNode.nodeValue.length;
  304. }
  305. if (endNode.nodeType == 1 && typeof endNode.childNodes[endPos] != "undefined") {
  306. endNode = endNode.childNodes[endPos];
  307. endPos = endNode.nodeType == 1 ? 0 : endNode.nodeValue.length;
  308. }
  309. }
  310. var keRange = new KE.range(doc);
  311. keRange.setTextStart(startNode, startPos);
  312. keRange.setTextEnd(endNode, endPos);
  313. this.keRange = keRange;
  314. };
  315. this.init();
  316. this.addRange = function(keRange) {
  317. this.keRange = keRange;
  318. if (KE.browser == 'IE') {
  319. var getEndRange = function(isStart) {
  320. var range = KE.util.createRange(doc);
  321. var node = isStart ? keRange.startNode : keRange.endNode;
  322. if (node.nodeType == 1) {
  323. range.moveToElementText(node);
  324. range.collapse(isStart);
  325. } else if (node.nodeType == 3) {
  326. range = KE.util.getNodeStartRange(doc, node);
  327. var pos = isStart ? keRange.startPos : keRange.endPos;
  328. range.moveStart('character', pos);
  329. }
  330. return range;
  331. }
  332. if (!this.range.item) {
  333. var node = keRange.startNode;
  334. if (node == keRange.endNode && KE.util.getNodeType(node) == 1 && KE.util.getNodeTextLength(node) == 0) {
  335. var temp = doc.createTextNode(" ");
  336. node.appendChild(temp);
  337. this.range.moveToElementText(node);
  338. this.range.collapse(false);
  339. this.range.select();
  340. node.removeChild(temp);
  341. } else {
  342. this.range.setEndPoint('StartToStart', getEndRange(true));
  343. this.range.setEndPoint('EndToStart', getEndRange(false));
  344. this.range.select();
  345. }
  346. }
  347. } else {
  348. var range = new KE.range(doc);
  349. range.setTextStart(keRange.startNode, keRange.startPos);
  350. range.setTextEnd(keRange.endNode, keRange.endPos);
  351. this.range.setStart(range.startNode, range.startPos);
  352. this.range.setEnd(range.endNode, range.endPos);
  353. this.sel.removeAllRanges();
  354. this.sel.addRange(this.range);
  355. }
  356. };
  357. this.focus = function() {
  358. if (KE.browser == 'IE' && this.range != null) this.range.select();
  359. }
  360. };
  361. KE.range = function(doc) {
  362. this.startNode = null;
  363. this.startPos = null;
  364. this.endNode = null;
  365. this.endPos = null;
  366. this.getParentElement = function() {
  367. var scanParent = function(node, func) {
  368. while (node != null && node.tagName != 'body') {
  369. node = node.parentNode;
  370. if (func(node)) return;
  371. }
  372. }
  373. var nodeList = [];
  374. scanParent(this.startNode, function(node) {
  375. nodeList.push(node);
  376. });
  377. var parentNode;
  378. scanParent(this.endNode, function(node) {
  379. if (KE.util.inArray(node, nodeList)) {
  380. parentNode = node;
  381. return true;
  382. }
  383. });
  384. return parentNode ? parentNode : doc.body;
  385. };
  386. this.getNodeList = function() {
  387. var parentNode = this.getParentElement();
  388. var nodeList = [];
  389. var keRange = this;
  390. var startNode = this.startNode;
  391. var isStarted = false;
  392. if (parentNode == startNode) isStarted = true;
  393. if (isStarted) nodeList.push(parentNode);
  394. KE.eachNode(parentNode, function(node) {
  395. if (node == startNode) isStarted = true;
  396. var range = new KE.range(doc);
  397. range.selectTextNode(node);
  398. if (range.comparePoints('START_TO_END', keRange) >= 0) return false;
  399. if (isStarted) nodeList.push(node);
  400. return true;
  401. });
  402. return nodeList;
  403. };
  404. this.comparePoints = function(how, range) {
  405. var compareNodes = function(nodeA, posA, nodeB, posB) {
  406. var cmp;
  407. if (KE.browser == 'IE') {
  408. var getStartRange = function(node, pos, isStart) {
  409. var range = KE.util.createRange(doc);
  410. var type = KE.util.getNodeType(node);
  411. if (type == 1) {
  412. range.moveToElementText(node);
  413. range.collapse(isStart);
  414. } else if (type == 3) {
  415. range = KE.util.getNodeStartRange(doc, node);
  416. range.moveStart('character', pos);
  417. range.collapse(true);
  418. }
  419. return range;
  420. }
  421. var rangeA, rangeB;
  422. if (how == 'START_TO_START' || how == 'START_TO_END') rangeA = getStartRange(nodeA, posA, true);
  423. else rangeA = getStartRange(nodeA, posA, false);
  424. if (how == 'START_TO_START' || how == 'END_TO_START') rangeB = getStartRange(nodeB, posB, true);
  425. else rangeB = getStartRange(nodeB, posB, false);
  426. return rangeA.compareEndPoints('StartToStart', rangeB);
  427. } else {
  428. var rangeA = KE.util.createRange(doc);
  429. rangeA.selectNode(nodeA);
  430. if (how == 'START_TO_START' || how == 'START_TO_END') rangeA.collapse(true);
  431. else rangeA.collapse(false);
  432. var rangeB = KE.util.createRange(doc);
  433. rangeB.selectNode(nodeB);
  434. if (how == 'START_TO_START' || how == 'END_TO_START') rangeB.collapse(true);
  435. else rangeB.collapse(false);
  436. if (rangeA.compareBoundaryPoints(Range.START_TO_START, rangeB) > 0) {
  437. cmp = 1;
  438. } else if (rangeA.compareBoundaryPoints(Range.START_TO_START, rangeB) == 0) {
  439. if (posA > posB) cmp = 1;
  440. else if (posA == posB) cmp = 0;
  441. else cmp = -1;
  442. } else {
  443. cmp = -1;
  444. }
  445. }
  446. return cmp;
  447. }
  448. if (how == 'START_TO_START') return compareNodes(this.startNode, this.startPos, range.startNode, range.startPos);
  449. if (how == 'START_TO_END') return compareNodes(this.startNode, this.startPos, range.endNode, range.endPos);
  450. if (how == 'END_TO_START') return compareNodes(this.endNode, this.endPos, range.startNode, range.startPos);
  451. if (how == 'END_TO_END') return compareNodes(this.endNode, this.endPos, range.endNode, range.endPos);
  452. };
  453. this.setTextStart = function(node, pos) {
  454. var textNode = node;
  455. KE.eachNode(node, function(n) {
  456. if (KE.util.getNodeType(n) == 3 && n.nodeValue.length > 0) {
  457. textNode = n;
  458. pos = 0;
  459. return false;
  460. }
  461. return true;
  462. });
  463. this.setStart(textNode, pos);
  464. };
  465. this.setStart = function(node, pos) {
  466. this.startNode = node;
  467. this.startPos = pos;
  468. if (this.endNode === null) {
  469. this.endNode = node;
  470. this.endPos = pos;
  471. }
  472. };
  473. this.setTextEnd = function(node, pos) {
  474. var textNode = node;
  475. KE.eachNode(node, function(n) {
  476. if (KE.util.getNodeType(n) == 3 && n.nodeValue.length > 0) {
  477. textNode = n;
  478. pos = n.nodeValue.length;
  479. }
  480. return true;
  481. });
  482. this.setEnd(textNode, pos);
  483. };
  484. this.setEnd = function(node, pos) {
  485. this.endNode = node;
  486. this.endPos = pos;
  487. if (this.startNode === null) {
  488. this.startNode = node;
  489. this.startPos = pos;
  490. }
  491. };
  492. this.selectNode = function(node) {
  493. this.setStart(node, 0);
  494. this.setEnd(node, node.nodeType == 1 ? 0 : node.nodeValue.length);
  495. };
  496. this.selectTextNode = function(node) {
  497. this.setTextStart(node, 0);
  498. this.setTextEnd(node, node.nodeType == 1 ? 0 : node.nodeValue.length);
  499. };
  500. this.extractContents = function(isDelete) {
  501. isDelete = (isDelete === false) ? false : true;
  502. var thisRange = this;
  503. var startNode = this.startNode;
  504. var startPos = this.startPos;
  505. var endNode = this.endNode;
  506. var endPos = this.endPos;
  507. var extractTextNode = function(node, startPos, endPos) {
  508. var length = node.nodeValue.length;
  509. var cloneNode = node.cloneNode(true);
  510. var centerNode = cloneNode.splitText(startPos);
  511. centerNode.splitText(endPos - startPos);
  512. if (isDelete) {
  513. var center = node;
  514. if (startPos > 0) center = node.splitText(startPos);
  515. if (endPos < length) center.splitText(endPos - startPos);
  516. center.parentNode.removeChild(center);
  517. }
  518. return centerNode;
  519. };
  520. var isStarted = false;
  521. var isEnd = false;
  522. var extractNodes = function(parent, frag) {
  523. if (KE.util.getNodeType(parent) != 1) return true;
  524. var node = parent.firstChild;
  525. while (node != null) {
  526. if (node == startNode) isStarted = true;
  527. if (node == endNode) isEnd = true;
  528. var nextNode = node.nextSibling;
  529. var type = KE.util.getNodeType(node);
  530. if (type == 1) {
  531. var range = new KE.range(doc);
  532. range.selectNode(node);
  533. if (isStarted && range.comparePoints('END_TO_END', thisRange) < 0) {
  534. var cloneNode = node.cloneNode(true);
  535. frag.appendChild(cloneNode);
  536. if (isDelete) {
  537. node.parentNode.removeChild(node);
  538. }
  539. } else {
  540. var childFlag = node.cloneNode(false);
  541. frag.appendChild(childFlag);
  542. if (!extractNodes(node, childFlag)) return false;
  543. }
  544. } else if (type == 3) {
  545. if (isStarted) {
  546. var textNode;
  547. if (node == startNode && node == endNode) {
  548. textNode = extractTextNode(node, startPos, endPos);
  549. frag.appendChild(textNode);
  550. return false;
  551. } else if (node == startNode) {
  552. textNode = extractTextNode(node, startPos, node.nodeValue.length);
  553. frag.appendChild(textNode);
  554. } else if (node == endNode) {
  555. textNode = extractTextNode(node, 0, endPos);
  556. frag.appendChild(textNode);
  557. return false;
  558. } else {
  559. textNode = extractTextNode(node, 0, node.nodeValue.length);
  560. frag.appendChild(textNode);
  561. }
  562. }
  563. }
  564. node = nextNode;
  565. if (isEnd) return false;
  566. }
  567. return true;
  568. }
  569. var parentNode = this.getParentElement();
  570. var docFrag = parentNode.cloneNode(false);
  571. extractNodes(parentNode, docFrag);
  572. return docFrag;
  573. };
  574. this.cloneContents = function() {
  575. return this.extractContents(false);
  576. };
  577. this.getText = function() {
  578. var html = this.cloneContents().innerHTML;
  579. return html.replace(/<.*?>/g, "");
  580. };
  581. };
  582. KE.cmd = function(id) {
  583. this.doc = KE.g[id].iframeDoc;
  584. this.keSel = KE.g[id].keSel;
  585. this.keRange = KE.g[id].keRange;
  586. this.mergeAttributes = function(el, attr) {
  587. for (var i = 0, len = attr.length; i < len; i++) {
  588. KE.each(attr[i], function(key, value) {
  589. if (key.charAt(0) == '.') {
  590. var jsKey = KE.util.getJsKey(key.substr(1));
  591. eval('el.style.' + jsKey + ' = value;');
  592. } else {
  593. el.setAttribute(key, value);
  594. }
  595. });
  596. }
  597. return el;
  598. };
  599. this.wrapTextNode = function(node, startPos, endPos, element, attributes) {
  600. var length = node.nodeValue.length;
  601. var isFull = (startPos == 0 && endPos == length);
  602. var range = new KE.range(this.doc);
  603. range.selectTextNode(node.parentNode);
  604. if (isFull &&
  605. node.parentNode.tagName == element.tagName &&
  606. range.comparePoints('END_TO_END', this.keRange) <= 0 &&
  607. range.comparePoints('START_TO_START', this.keRange) >= 0) {
  608. this.mergeAttributes(node.parentNode, attributes);
  609. return node;
  610. } else {
  611. var el = element.cloneNode(true);
  612. if (isFull) {
  613. var cloneNode = node.cloneNode(true);
  614. el.appendChild(cloneNode);
  615. node.parentNode.replaceChild(el, node);
  616. return cloneNode;
  617. } else {
  618. var centerNode = node;
  619. if (startPos < endPos) {
  620. if (startPos > 0) centerNode = node.splitText(startPos);
  621. if (endPos < length) centerNode.splitText(endPos - startPos);
  622. var cloneNode = centerNode.cloneNode(true);
  623. el.appendChild(cloneNode);
  624. centerNode.parentNode.replaceChild(el, centerNode);
  625. return cloneNode;
  626. } else {
  627. if (startPos < length) {
  628. centerNode = node.splitText(startPos);
  629. centerNode.parentNode.insertBefore(el, centerNode);
  630. } else {
  631. if (centerNode.nextSibling) {
  632. centerNode.parentNode.insertBefore(el, centerNode.nextSibling);
  633. } else {
  634. centerNode.parentNode.appendChild(el);
  635. }
  636. }
  637. return el;
  638. }
  639. }
  640. }
  641. };
  642. this.wrap = function(tagName, attributes) {
  643. var self = this;
  644. this.keSel.focus();
  645. var element = KE.$$(tagName, this.doc);
  646. this.mergeAttributes(element, attributes);
  647. var keRange = this.keRange;
  648. var startNode = keRange.startNode;
  649. var startPos = keRange.startPos;
  650. var endNode = keRange.endNode;
  651. var endPos = keRange.endPos;
  652. var parentNode = keRange.getParentElement();
  653. var isStarted = false;
  654. KE.eachNode(parentNode, function(node) {
  655. if (node == startNode) isStarted = true;
  656. if (node.nodeType == 1) {
  657. if (node == startNode && node == endNode) {
  658. if (KE.util.inArray(node.tagName.toLowerCase(), KE.setting.noEndTags)) {
  659. if (startPos > 0) node.parentNode.appendChild(element);
  660. else node.parentNode.insertBefore(element, node);
  661. } else {
  662. node.appendChild(element);
  663. }
  664. keRange.selectNode(element);
  665. return false;
  666. } else if (node == startNode) {
  667. keRange.setStart(node, 0);
  668. } else if (node == endNode) {
  669. keRange.setEnd(node, 0);
  670. return false;
  671. }
  672. } else if (node.nodeType == 3) {
  673. if (isStarted) {
  674. if (node == startNode && node == endNode) {
  675. var rangeNode = self.wrapTextNode(node, startPos, endPos, element, attributes);
  676. keRange.selectNode(rangeNode);
  677. return false;
  678. } else if (node == startNode) {
  679. var rangeNode = self.wrapTextNode(node, startPos, node.nodeValue.length, element, attributes);
  680. keRange.setStart(rangeNode, 0);
  681. } else if (node == endNode) {
  682. var rangeNode = self.wrapTextNode(node, 0, endPos, element, attributes);
  683. keRange.setEnd(rangeNode, rangeNode.nodeType == 1 ? 0 : rangeNode.nodeValue.length);
  684. return false;
  685. } else {
  686. self.wrapTextNode(node, 0, node.nodeValue.length, element, attributes);
  687. }
  688. }
  689. }
  690. return true;
  691. });
  692. this.keSel.addRange(keRange);
  693. };
  694. this.getTopParent = function(tagNames, node) {
  695. var parent = null;
  696. while (node != null) {
  697. node = node.parentNode;
  698. if (KE.util.inArray(node.tagName.toLowerCase(), tagNames)) {
  699. parent = node;
  700. } else {
  701. break;
  702. }
  703. }
  704. return parent;
  705. };
  706. this.splitNodeParent = function(parent, node, pos) {
  707. var leftRange = new KE.range(this.doc);
  708. leftRange.selectNode(parent.firstChild);
  709. leftRange.setEnd(node, pos);
  710. var leftFrag = leftRange.extractContents();
  711. parent.parentNode.insertBefore(leftFrag, parent);
  712. return {left : leftFrag, right : parent};
  713. };
  714. this.remove = function(tagNames, attributes) {
  715. var keRange = this.keRange;
  716. var startNode = keRange.startNode;
  717. var startPos = keRange.startPos;
  718. var endNode = keRange.endNode;
  719. var endPos = keRange.endPos;
  720. this.keSel.focus();
  721. if (keRange.getText().replace(/\s+/g, '') === '') return;
  722. var startParent = this.getTopParent(tagNames, startNode);
  723. var endParent = this.getTopParent(tagNames, endNode);
  724. if (startParent) {
  725. var startFrags = this.splitNodeParent(startParent, startNode, startPos);
  726. keRange.setStart(startFrags.right, 0);
  727. if (startNode == endNode && KE.util.getNodeTextLength(startFrags.right) > 0) {
  728. keRange.selectNode(startFrags.right);
  729. var range = new KE.range(this.doc);
  730. range.selectTextNode(startFrags.left);
  731. if (startPos > 0) endPos -= range.endNode.nodeValue.length;
  732. range.selectTextNode(startFrags.right);
  733. endNode = range.startNode;
  734. }
  735. }
  736. if (endParent) {
  737. var endFrags = this.splitNodeParent(endParent, endNode, endPos);
  738. this.keRange.setEnd(endFrags.left, 0);
  739. if (startParent == endParent) {
  740. keRange.setStart(endFrags.left, 0);
  741. }
  742. }
  743. var nodeList = keRange.getNodeList();
  744. keRange.setTextStart(keRange.startNode, keRange.startPos);
  745. keRange.setTextEnd(keRange.endNode, keRange.endPos);
  746. for (var i = 0; i < nodeList.length; i++) {
  747. var node = nodeList[i];
  748. if (node.nodeType == 1) {
  749. if (KE.util.inArray(node.tagName.toLowerCase(), tagNames)) {
  750. KE.util.removeParent(node);
  751. } else {
  752. for (var j = 0; j < attributes.length; j++) {
  753. node.removeAttribute(attributes[j]);
  754. }
  755. }
  756. }
  757. }
  758. try {
  759. this.keSel.addRange(keRange);
  760. } catch(e) {}
  761. };
  762. }
  763. KE.util = {
  764. getDocumentElement : function() {
  765. return (document.compatMode != "CSS1Compat") ? document.body : document.documentElement;
  766. },
  767. getDocumentHeight : function() {
  768. var el = this.getDocumentElement();
  769. return Math.max(el.scrollHeight, el.clientHeight);
  770. },
  771. getDocumentWidth : function() {
  772. var el = this.getDocumentElement();
  773. return Math.max(el.scrollWidth, el.clientWidth);
  774. },
  775. createTable : function() {
  776. var table = KE.$$('table');
  777. table.cellPadding = 0;
  778. table.cellSpacing = 0;
  779. table.border = 0;
  780. return {table: table, cell: table.insertRow(0).insertCell(0)};
  781. },
  782. loadStyle : function(path) {
  783. var link = KE.$$('link');
  784. link.setAttribute('type', 'text/css');
  785. link.setAttribute('rel', 'stylesheet');
  786. link.setAttribute('href', path);
  787. document.getElementsByTagName("head")[0].appendChild(link);
  788. },
  789. inArray : function(str, arr) {
  790. for (var i = 0; i < arr.length; i++) {if (str == arr[i]) return true;}
  791. return false;
  792. },
  793. getJsKey : function(key) {
  794. var arr = key.split('-');
  795. key = '';
  796. for (var i = 0, len = arr.length; i < len; i++) {
  797. key += (i > 0) ? arr[i].charAt(0).toUpperCase() + arr[i].substr(1) : arr[i];
  798. }
  799. return key;
  800. },
  801. escape : function(html) {
  802. html = html.replace(/&/g, "&amp;");
  803. html = html.replace(/</g, "&lt;");
  804. html = html.replace(/>/g, "&gt;");
  805. html = html.replace(/\xA0/g, "&nbsp;");
  806. html = html.replace(/\x20/g, " ");
  807. return html;
  808. },
  809. getElementPos : function(el) {
  810. var x = 0;
  811. var y = 0;
  812. if (KE.browser != "WEBKIT") {
  813. var box = el.getBoundingClientRect();
  814. var el = this.getDocumentElement();
  815. x = box.left + el.scrollLeft - el.clientLeft;
  816. y = box.top + el.scrollTop - el.clientTop;
  817. } else {
  818. x = el.offsetLeft;
  819. y = el.offsetTop;
  820. var parent = el.offsetParent;
  821. while (parent) {
  822. x += parent.offsetLeft;
  823. y += parent.offsetTop;
  824. parent = parent.offsetParent;
  825. }
  826. }
  827. return {'x' : x, 'y' : y};
  828. },
  829. getCoords : function(ev) {
  830. ev = ev || window.event;
  831. var el = this.getDocumentElement();
  832. if (ev.pageX) return { x : ev.pageX, y : ev.pageY};
  833. return {
  834. x : ev.clientX + el.scrollLeft - el.clientLeft,
  835. y : ev.clientY + el.scrollTop - el.clientTop
  836. };
  837. },
  838. setOpacity : function(el, opacity) {
  839. if (typeof el.style.opacity == "undefined") {
  840. el.style.filter = (opacity == 100) ? "" : "alpha(opacity=" + opacity + ")";
  841. } else {
  842. el.style.opacity = (opacity == 100) ? "" : "0." + opacity.toString();
  843. }
  844. },
  845. getIframeDoc : function(iframe) {
  846. return iframe.contentDocument || iframe.contentWindow.document;
  847. },
  848. rgbToHex : function(str) {
  849. function hex(s) {
  850. s = parseInt(s).toString(16);
  851. return s.length > 1 ? s : '0' + s;
  852. };
  853. return str.replace(/rgb\s*?\(\s*?([0-9]+)\s*?,\s*?([0-9]+)\s*?,\s*?([0-9]+)\s*?\)/ig,
  854. function($0, $1, $2, $3) {
  855. return '#' + hex($1) + hex($2) + hex($3);
  856. }
  857. );
  858. },
  859. getStyle : function(el, key) {
  860. var arr = key.split('-');
  861. key = "";
  862. for (var i = 0, len = arr.length; i < len; i++) {
  863. key += (i > 0) ? arr[i].charAt(0).toUpperCase() + arr[i].substr(1) : arr[i];
  864. }
  865. var val = el.style[key];
  866. if (!val) {
  867. var css = el.getAttribute("style");
  868. if (css) {
  869. var re = new RegExp("(^|[^\w\-])" + key + "\s*:\s*([^;]+)", "ig");
  870. var arr = re.exec(css);
  871. if (arr) val = arr[2];
  872. }
  873. }
  874. return KE.util.rgbToHex(val);
  875. },
  876. createRange : function(doc) {
  877. return doc.createRange ? doc.createRange() : doc.body.createTextRange();
  878. },
  879. getNodeType : function(node) {
  880. return (node.nodeType == 1 && KE.util.inArray(node.tagName.toLowerCase(), KE.setting.noEndTags)) ? 88 : node.nodeType;
  881. },
  882. getNodeTextLength : function(node) {
  883. var type = KE.util.getNodeType(node);
  884. if (type == 1) {
  885. var html = node.innerHTML;
  886. return html.replace(/<.*?>/ig, "").length;
  887. } else if (type == 3) {
  888. return node.nodeValue.length;
  889. }
  890. },
  891. getNodeStartRange : function(doc, node) {
  892. var range = KE.util.createRange(doc);
  893. var type = node.nodeType;
  894. if (type == 1) {
  895. range.moveToElementText(node);
  896. return range;
  897. } else if (type == 3) {
  898. var offset = 0;
  899. var sibling = node.previousSibling;
  900. while (sibling) {
  901. if (sibling.nodeType == 1) {
  902. var nodeRange = KE.util.createRange(doc);
  903. nodeRange.moveToElementText(sibling);
  904. range.setEndPoint('StartToEnd', nodeRange);
  905. range.moveStart('character', offset);
  906. return range;
  907. } else if (sibling.nodeType == 3) {
  908. offset += sibling.nodeValue.length;
  909. }
  910. sibling = sibling.previousSibling;
  911. }
  912. range.moveToElementText(node.parentNode);
  913. range.moveStart('character', offset);
  914. return range;
  915. }
  916. },
  917. trimNodes : function(parent) {
  918. if (KE.util.getNodeType(parent) != 1) return;
  919. if (KE.util.inArray(parent.tagName.toLowerCase(), KE.setting.inlineTags) && KE.util.getNodeTextLength(parent) == 0) {
  920. parent.parentNode.removeChild(parent);
  921. return;
  922. }
  923. KE.eachNode(parent, function(node) {
  924. KE.util.trimNodes(node);
  925. return true;
  926. });
  927. },
  928. removeParent : function(parent) {
  929. if (parent.hasChildNodes) {
  930. var node = parent.firstChild;
  931. while (node != null) {
  932. var nextNode = node.nextSibling;
  933. parent.parentNode.insertBefore(node, parent);
  934. node = nextNode;
  935. }
  936. }
  937. parent.parentNode.removeChild(parent);
  938. },
  939. drag : function(id, mousedownObj, moveObj, func, hideFlag) {
  940. var obj = KE.g[id];
  941. mousedownObj.onmousedown = function(event) {
  942. if (hideFlag && obj.wyswygMode) obj.iframe.style.display = 'none';
  943. if (KE.browser != 'IE') event.preventDefault();
  944. var ev = event || window.event;
  945. var pos = KE.util.getCoords(ev);
  946. var objTop = parseInt(moveObj.style.top);
  947. var objLeft = parseInt(moveObj.style.left);
  948. var objWidth = parseInt(moveObj.style.width);
  949. var objHeight = parseInt(moveObj.style.height);
  950. var mouseTop = pos.y;
  951. var mouseLeft = pos.x;
  952. var dragFlag = true;
  953. var moveListener = function(event) {
  954. if (dragFlag) {
  955. var ev = event || window.event;
  956. var pos = KE.util.getCoords(ev);
  957. var top = pos.y - mouseTop;
  958. var left = pos.x - mouseLeft;
  959. func(objTop, objLeft, objWidth, objHeight, top, left);
  960. }
  961. return false;
  962. };
  963. var upListener = function(event) {
  964. if (hideFlag && obj.wyswygMode) obj.iframe.style.display = '';
  965. dragFlag = false;
  966. KE.event.remove(document, 'mousemove', moveListener);
  967. KE.event.remove(document, 'mouseup', upListener);
  968. };
  969. KE.event.add(document, 'mousemove', moveListener);
  970. KE.event.add(document, 'mouseup', upListener);
  971. };
  972. },
  973. setDefaultPlugin : function(id) {
  974. var items = [
  975. 'cut', 'copy', 'paste', 'selectall', 'justifyleft', 'justifycenter', 'justifyright',
  976. 'justifyfull', 'insertorderedlist', 'insertunorderedlist', 'indent', 'outdent', 'subscript','superscript',
  977. 'bold', 'italic', 'underline', 'strikethrough', 'unlink'
  978. ];
  979. for (var i = 0; i < items.length; i++) {
  980. KE.plugin[items[i]] = {
  981. click : new Function('id', 'KE.util.execCommand(id, "' + items[i] + '", null);')
  982. };
  983. }
  984. },
  985. getFullHtml : function(id, tagLineMode) {
  986. var html = '<html>';
  987. html += '<head>';
  988. html += '<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />';
  989. html += '<title>KindEditor</title>';
  990. if (tagLineMode) {
  991. html += '<link href="' + KE.g[id].skinsPath + 'editor.css" rel="stylesheet" type="text/css" />';
  992. }
  993. if (KE.g[id].cssPath) {
  994. html += '<link href="' + KE.g[id].cssPath + '" rel="stylesheet" type="text/css" />';
  995. }
  996. html += '</head>';
  997. html += '<body class="ke-content"></body>';
  998. html += '</html>';
  999. return html;
  1000. },
  1001. resize : function(id, width, height, isCheck) {
  1002. var obj = KE.g[id];
  1003. if (width.match(/%$/)) width = obj.container.offsetWidth + 'px';
  1004. if (height.match(/%$/)) height = obj.container.offsetHeight + 'px';
  1005. if (isCheck && (parseInt(width) <= obj.minWidth || parseInt(height) <= obj.minHeight)) return;
  1006. obj.container.style.width = width;
  1007. obj.container.style.height = height;
  1008. var diff = parseInt(height) - obj.toolbarTable.offsetHeight - obj.bottom.offsetHeight;
  1009. if (diff >= 0) {
  1010. obj.textareaTable.style.height = diff + 'px';
  1011. obj.iframe.style.height = diff + 'px';
  1012. obj.newTextarea.style.height = diff + 'px';
  1013. }
  1014. },
  1015. getData : function(id) {
  1016. var data;
  1017. if (KE.g[id].wyswygMode) {
  1018. if (KE.g[id].filterMode) {
  1019. data = KE.util.outputHtml(id, KE.g[id].iframeDoc.body);
  1020. } else {
  1021. data = KE.util.htmlToXhtml(id, KE.g[id].iframeDoc.body);
  1022. }
  1023. } else {
  1024. data = KE.g[id].newTextarea.value;
  1025. }
  1026. return data;
  1027. },
  1028. getSrcData : function(id) {
  1029. var data;
  1030. if (KE.g[id].wyswygMode) {
  1031. data = KE.g[id].iframeDoc.body.innerHTML;
  1032. } else {
  1033. data = KE.g[id].newTextarea.value;
  1034. }
  1035. return data;
  1036. },
  1037. getPureData : function(id) {
  1038. var data = this.getSrcData(id);
  1039. data = data.replace(/<br[\s\/]{0,2}>/ig, "\r\n");
  1040. data = data.replace(/<.*?>/ig, "");
  1041. data = data.replace(/&nbsp;/ig, "");
  1042. return data;
  1043. },
  1044. setData : function(id) {
  1045. var data = this.getData(id);
  1046. KE.g[id].srcTextarea.value = data;
  1047. },
  1048. setPureData : function(id) {
  1049. var data = this.getPureData(id);
  1050. KE.g[id].srcTextarea.value = data;
  1051. },
  1052. focus : function(id) {
  1053. if (KE.g[id].wyswygMode) {
  1054. KE.g[id].iframeWin.focus();
  1055. } else {
  1056. KE.g[id].newTextarea.focus();
  1057. }
  1058. },
  1059. click : function(id, cmd) {
  1060. KE.layout.hide(id);
  1061. KE.util.focus(id);
  1062. KE.plugin[cmd].click(id);
  1063. },
  1064. selection : function(id) {
  1065. var win = KE.g[id].iframeWin;
  1066. var doc = KE.g[id].iframeDoc;
  1067. KE.g[id].keSel = new KE.selection(win, doc);
  1068. KE.g[id].keRange = KE.g[id].keSel.keRange;
  1069. KE.g[id].sel = KE.g[id].keSel.sel;
  1070. KE.g[id].range = KE.g[id].keSel.range;
  1071. },
  1072. select : function(id) {
  1073. if (KE.browser == 'IE') KE.g[id].range.select();
  1074. },
  1075. pToBr : function(id) {
  1076. if(KE.browser == 'IE') {
  1077. KE.event.add(KE.g[id].iframeDoc, 'keydown', function(e) {
  1078. if (e.keyCode == 13) {
  1079. KE.util.selection(id);
  1080. if(KE.g[id].range.parentElement().tagName != 'LI') {
  1081. KE.util.insertHtml(id, '<br />');
  1082. KE.util.select(id);
  1083. return false;
  1084. }
  1085. }
  1086. });
  1087. }
  1088. },
  1089. execCommand : function(id, cmd, value) {
  1090. try {
  1091. KE.g[id].iframeDoc.execCommand(cmd, false, value);
  1092. } catch(e) {}
  1093. KE.toolbar.updateState(id);
  1094. KE.history.add(id, false);
  1095. },
  1096. insertHtml : function(id, html) {
  1097. if (html == '') return;
  1098. KE.util.select(id);
  1099. if (KE.browser == 'IE') {
  1100. if (KE.g[id].sel.type.toLowerCase() == 'control') {
  1101. KE.g[id].range.item(0).outerHTML = html;
  1102. } else {
  1103. KE.g[id].range.pasteHTML(html);
  1104. }
  1105. KE.history.add(id, false);
  1106. } else {
  1107. this.execCommand(id, 'inserthtml', html);
  1108. }
  1109. },
  1110. removeDomain : function(id, tagName, key, url) {
  1111. if ((tagName == 'a' && key == 'href') || (tagName == 'img' && key == 'src') || (tagName == 'embed' && key == 'src')) {
  1112. var domains = KE.g[id].siteDomains;
  1113. for (var i = 0, len = domains.length; i < len; i++) {
  1114. var domain = "http://" + domains[i];
  1115. if (url.indexOf(domain) == 0) return url.substr(domain.length);
  1116. }
  1117. }
  1118. return url;
  1119. },
  1120. htmlToXhtml : function(id, element) {
  1121. KE.util.trimNodes(element);
  1122. var html = element.innerHTML;
  1123. var tags = KE.setting.noEndTags;
  1124. for (var i = 0, len = tags.length; i < len; i++) {
  1125. html = html.replace(new RegExp("<(" + tags[i] + ")\\s+(.*?[^\\/])>", "gi"), "<$1 $2 />");
  1126. html = html.replace(new RegExp("<(" + tags[i] + ")>", "gi"), "<$1 />");
  1127. }
  1128. html = html.replace(/<(\w+)(.*?)>/g, function($0, $1, $2) {
  1129. var tagName = $1.toLowerCase();
  1130. var attr = $2;
  1131. attr = attr.replace(/(\w+)=([^\s]+)/gi, function($0, $1, $2) {
  1132. var key = $1.toLowerCase();
  1133. var val = $2;
  1134. var first = $2.charAt(0);
  1135. var last = $2.charAt($2.length - 1);
  1136. if (first === '"' && last === '"') {
  1137. val = '"' + KE.util.removeDomain(id, tagName, key, val.substr(1, val.length - 2)) + '"';
  1138. } else if (first !== '"' && last !== '"') {
  1139. val = '"' + KE.util.removeDomain(id, tagName, key, val) + '"';
  1140. }
  1141. return key + '=' + val;
  1142. });
  1143. attr = attr.replace(/\s+style=".*?"/gi, function($0) {
  1144. return KE.util.rgbToHex($0.toLowerCase());
  1145. });
  1146. return '<' + tagName + attr + '>';
  1147. });
  1148. html = html.replace(/(<\/\w+>)/g, function($0, $1) {
  1149. return $1.toLowerCase();
  1150. });
  1151. return html;
  1152. },
  1153. outputHtml : function(id, element) {
  1154. var newHtmlTags = [];
  1155. KE.each(KE.g[id].htmlTags, function(key, val) {
  1156. var arr = key.split(',');
  1157. for (var i = 0, len = arr.length; i < len; i++) {
  1158. newHtmlTags[arr[i]] = val;
  1159. }
  1160. });
  1161. var htmlList = [];
  1162. KE.util.trimNodes(element);
  1163. var scanNodes = function(el) {
  1164. var startTags = [];
  1165. var setStartTag = function(tagName, attrStr, styleStr, isEnd) {
  1166. var html = '';
  1167. html += '<' + tagName;
  1168. if (attrStr) html += attrStr;
  1169. if (styleStr) html += ' style="' + styleStr + '"';
  1170. html += isEnd ? ' />' : '>';
  1171. if (KE.browser == 'IE' && isEnd && KE.util.inArray(tagName, ['br', 'hr'])) html += "\n";
  1172. if (typeof newHtmlTags[tagName] == 'object') htmlList.push(html);
  1173. if (!isEnd) startTags.push(tagName);
  1174. };
  1175. var setEndTag = function() {
  1176. if (startTags.length > 0) {
  1177. var tagName = startTags.pop();
  1178. if (typeof newHtmlTags[tagName] != 'object') return;
  1179. var html = '</' + tagName + '>';
  1180. if (KE.browser == 'IE' && KE.util.inArray(tagName, ['p', 'div', 'table', 'ol', 'ul'])) html += "\n";
  1181. htmlList.push(html);
  1182. }
  1183. };
  1184. var nodes = el.childNodes;
  1185. for (var i = 0, len = nodes.length; i < len; i++) {
  1186. var node = nodes[i];
  1187. switch (node.nodeType) {
  1188. case 1:
  1189. var tagName = node.tagName.toLowerCase();
  1190. var attrStr = '';
  1191. var styleStr = '';
  1192. var isEnd = false;
  1193. if (typeof newHtmlTags[tagName] == 'object') {
  1194. var attrList = newHtmlTags[tagName];
  1195. for (var j = 0, l = attrList.length; j < l; j++) {
  1196. var attr = attrList[j];
  1197. if (attr == '/') isEnd = true;
  1198. else if (attr.charAt(0) == '.') {
  1199. var key = attr.substr(1);
  1200. var val = KE.util.getStyle(node, key);
  1201. if (val) styleStr += key + ':' + val + ';';
  1202. } else {
  1203. var val = node.getAttribute(attr);
  1204. if (val !== null && val !== "") {
  1205. val = KE.util.removeDomain(id, tagName, attr, val);
  1206. attrStr += ' ' + attr + '="' + val + '"';
  1207. }
  1208. }
  1209. }
  1210. }
  1211. setStartTag(tagName, attrStr, styleStr, isEnd);
  1212. if (node.hasChildNodes()) {
  1213. scanNodes(node);
  1214. } else {
  1215. if (startTags.length > 0) {
  1216. var prevHtml = htmlList[htmlList.length - 1];
  1217. if (typeof prevHtml != "undefined" && prevHtml.match(/^<p|^<div/) != null) {
  1218. htmlList.push("&nbsp;");
  1219. }
  1220. }
  1221. }
  1222. break;
  1223. case 3:
  1224. htmlList.push(KE.util.escape(node.nodeValue));
  1225. break;
  1226. default:
  1227. break;
  1228. }
  1229. setEndTag();
  1230. }
  1231. setEndTag();
  1232. };
  1233. scanNodes(element);
  1234. var html = htmlList.join('');
  1235. return html;
  1236. }
  1237. };
  1238. KE.layout = {
  1239. show : function(id, div) {
  1240. KE.layout.hide(id);
  1241. KE.g[id].hideDiv.appendChild(div);
  1242. KE.g[id].hideDiv.style.display = 'block';
  1243. KE.g[id].layoutDiv = div;
  1244. },
  1245. hide : function(id) {
  1246. try {
  1247. KE.g[id].hideDiv.removeChild(KE.g[id].layoutDiv);
  1248. } catch (e) {}
  1249. KE.g[id].hideDiv.style.display = 'none';
  1250. KE.g[id].maskDiv.style.display = 'none';
  1251. KE.util.focus(id);
  1252. },
  1253. make : function(id) {
  1254. var div = KE.$$('div');
  1255. div.style.position = 'absolute';
  1256. div.style.zIndex = 19811214;
  1257. return div;
  1258. }
  1259. };
  1260. KE.menu = function(arg){
  1261. this.arg = arg;
  1262. var div = KE.layout.make(arg.id);
  1263. div.className = 'ke-menu';
  1264. var obj = KE.g[arg.id].toolbarIcon[arg.cmd];
  1265. var pos = KE.util.getElementPos(obj[0]);
  1266. div.style.top = pos.y + obj[0].offsetHeight + 'px';
  1267. div.style.left = pos.x + 'px';
  1268. this.div = div;
  1269. this.add = function(html, event) {
  1270. var cDiv = KE.$$('div');
  1271. cDiv.className = 'ke-menu-noselected';
  1272. cDiv.style.width = this.arg.width;
  1273. cDiv.onmouseover = function() { this.className = 'ke-menu-selected'; }
  1274. cDiv.onmouseout = function() { this.className = 'ke-menu-noselected'; }
  1275. cDiv.onclick = event;
  1276. cDiv.innerHTML = html;
  1277. this.append(cDiv);
  1278. };
  1279. this.append = function(el) {
  1280. this.div.appendChild(el);
  1281. };
  1282. this.insert = function(html) {
  1283. this.div.innerHTML = html;
  1284. };
  1285. this.show = function() {
  1286. KE.layout.show(this.arg.id, this.div);
  1287. };
  1288. this.picker = function() {
  1289. var colorTable = KE.setting.colorTable;
  1290. var table = KE.$$('table');
  1291. table.cellPadding = 0;
  1292. table.cellSpacing = 0;
  1293. table.border = 0;
  1294. table.style.margin = 0;
  1295. table.style.padding = 0;
  1296. table.style.borderCollapse = 'separate';
  1297. for (var i = 0; i < colorTable.length; i++) {
  1298. var row = table.insertRow(i);
  1299. for (var j = 0; j < colorTable[i].length; j++) {
  1300. var cell = row.insertCell(j);
  1301. cell.className = 'ke-picker-cell';
  1302. cell.style.backgroundColor = colorTable[i][j];
  1303. cell.title = colorTable[i][j];
  1304. cell.onmouseover = function() {this.style.borderColor = '#000000'; }
  1305. cell.onmouseout = function() {this.style.borderColor = '#F0F0EE'; }
  1306. cell.onclick = new Function('KE.plugin["' + this.arg.cmd + '"].exec("' +
  1307. this.arg.id + '", "' + colorTable[i][j] + '")');
  1308. cell.innerHTML = '&nbsp;';
  1309. }
  1310. }
  1311. this.append(table);
  1312. this.show();
  1313. };
  1314. };
  1315. KE.dialog = function(arg){
  1316. this.arg = arg;
  1317. this.topHeight = 20;
  1318. this.bottomHeight = 76;
  1319. this.getPos = function() {
  1320. var arg = this.arg;
  1321. var id = this.arg.id;
  1322. var pos = KE.util.getElementPos(KE.g[id].container);
  1323. var height = arg.height + this.topHeight + this.bottomHeight;
  1324. var xDiff = Math.round(parseInt(KE.g[id].container.style.width) / 2) - Math.round(arg.width / 2);
  1325. var yDiff = Math.round(parseInt(KE.g[id].container.style.height) / 2) - Math.round(height / 2);
  1326. var x = xDiff < 0 ? pos.x : pos.x + xDiff;
  1327. var y = yDiff < 0 ? pos.y : pos.y + yDiff;
  1328. return {'x' : x, 'y' : y};
  1329. };
  1330. this.show = function() {
  1331. var arg = this.arg;
  1332. var id = arg.id;
  1333. var div = KE.layout.make(arg.id);
  1334. div.className = 'ke-dialog';
  1335. var pos = this.getPos();
  1336. div.style.width = (arg.width + this.topHeight) + 'px';
  1337. div.style.height = (arg.height + this.bottomHeight) + 'px';
  1338. div.style.top = pos.y + 'px';
  1339. div.style.left = pos.x + 'px';
  1340. var titleDiv = KE.$$('div');
  1341. titleDiv.className = 'ke-dialog-title';
  1342. titleDiv.innerHTML = arg.title;
  1343. var span = KE.$$('span');
  1344. var url = KE.g[id].skinsPath + KE.g[id].skinType + '.gif';
  1345. span.style.backgroundImage = "url(" + url + ")";
  1346. span.className = "ke-toolbar-close";
  1347. span.alt = KE.lang['close'];
  1348. span.title = KE.lang['close'];
  1349. span.onclick = new Function("KE.layout.hide('" + id + "')");
  1350. titleDiv.appendChild(span);
  1351. KE.util.drag(id, titleDiv, div, function(objTop, objLeft, objWidth, objHeight, top, left) {
  1352. div.style.top = (objTop + top) + 'px';
  1353. div.style.left = (objLeft + left) + 'px';
  1354. });
  1355. div.appendChild(titleDiv);
  1356. var bodyDiv = KE.$$('div');
  1357. bodyDiv.className = 'ke-dialog-body';
  1358. var dialog = KE.$$('iframe');
  1359. if (arg.useFrameCSS) {
  1360. dialog.className = 'ke-dialog-iframe';
  1361. }
  1362. dialog.width = arg.width + 'px';
  1363. dialog.height = arg.height + 'px';
  1364. dialog.setAttribute("frameBorder", "0");
  1365. bodyDiv.appendChild(dialog);
  1366. div.appendChild(bodyDiv);
  1367. var bottomDiv = KE.$$('div');
  1368. bottomDiv.className = 'ke-dialog-bottom';
  1369. var noButton = null;
  1370. var yesButton = null;
  1371. var previewButton = null;
  1372. if (arg.noButton) {
  1373. noButton = KE.$$('input');
  1374. noButton.className = 'ke-dialog-no';
  1375. noButton.type = 'button';
  1376. noButton.name = 'noButton';
  1377. noButton.value = arg.noButton;
  1378. noButton.onclick = new Function("KE.layout.hide('" + id + "')");
  1379. bottomDiv.appendChild(noButton);
  1380. }
  1381. if (arg.yesButton) {
  1382. yesButton = KE.$$('input');
  1383. yesButton.className = 'ke-dialog-yes';
  1384. yesButton.type = 'button';
  1385. yesButton.name = 'yesButton';
  1386. yesButton.value = arg.yesButton;
  1387. yesButton.onclick = new Function("KE.plugin['" + arg.cmd + "'].exec('" + id + "')");
  1388. bottomDiv.appendChild(yesButton);
  1389. }
  1390. if (arg.previewButton) {
  1391. previewButton = KE.$$('input');
  1392. previewButton.className = 'ke-dialog-preview';
  1393. previewButton.type = 'button';
  1394. previewButton.name = 'previewButton';
  1395. previewButton.value = arg.previewButton;
  1396. previewButton.onclick = new Function("KE.plugin['" + arg.cmd + "'].preview('" + id + "')");
  1397. bottomDiv.appendChild(previewButton);
  1398. }
  1399. div.appendChild(bottomDiv);
  1400. KE.layout.show(id, div);
  1401. window.focus();
  1402. if (yesButton) yesButton.focus();
  1403. else if (noButton) noButton.focus();
  1404. if (typeof arg.html != "undefined") {
  1405. var dialogDoc = KE.util.getIframeDoc(dialog);
  1406. var html = KE.util.getFullHtml(id, false);
  1407. dialogDoc.open();
  1408. dialogDoc.write(html);
  1409. dialogDoc.close();
  1410. dialogDoc.body.innerHTML = arg.html;
  1411. } else {
  1412. dialog.src = KE.g[id].pluginsPath + arg.cmd + '.html';
  1413. }
  1414. KE.g[id].maskDiv.style.width = KE.util.getDocumentWidth() + 'px';
  1415. KE.g[id].maskDiv.style.height = KE.util.getDocumentHeight() + 'px';
  1416. KE.g[id].maskDiv.style.display = 'block';
  1417. KE.g[id].dialog = dialog;
  1418. KE.g[id].noButton = noButton;
  1419. KE.g[id].yesButton = yesButton;
  1420. KE.g[id].previewButton = previewButton;
  1421. };
  1422. };
  1423. KE.toolbar = {
  1424. updateState : function(id) {
  1425. var cmdList = [
  1426. 'justifyleft', 'justifycenter', 'justifyright',
  1427. 'justifyfull', 'insertorderedlist', 'insertunorderedlist', 'indent', 'outdent', 'subscript','superscript',
  1428. 'bold', 'italic', 'underline', 'strikethrough'
  1429. ];
  1430. for (var i = 0; i < cmdList.length; i++) {
  1431. var cmd = cmdList[i];
  1432. var state = false;
  1433. try {
  1434. state = KE.g[id].iframeDoc.queryCommandState(cmd);
  1435. } catch(e) {}
  1436. if (state) {
  1437. KE.toolbar.select(id, cmd);
  1438. } else {
  1439. KE.toolbar.unselect(id, cmd);
  1440. }
  1441. }
  1442. },
  1443. isSelected : function(id, cmd) {
  1444. if (KE.plugin[cmd] && KE.plugin[cmd].isSelected) return true;
  1445. else return false;
  1446. },
  1447. select : function(id, cmd) {
  1448. if (KE.g[id].toolbarIcon[cmd]) {
  1449. var a = KE.g[id].toolbarIcon[cmd][0];
  1450. a.className = "ke-icon-selected";
  1451. a.onmouseover = null;
  1452. a.onmouseout = null;
  1453. }
  1454. },
  1455. unselect : function(id, cmd) {
  1456. if (KE.g[id].toolbarIcon[cmd]) {
  1457. var a = KE.g[id].toolbarIcon[cmd][0];
  1458. a.className = "ke-icon";
  1459. a.onmouseover = function(){ this.className = "ke-icon-on"; };
  1460. a.onmouseout = function(){ this.className = "ke-icon"; };
  1461. }
  1462. },
  1463. able : function(id, arr) {
  1464. KE.each(KE.g[id].toolbarIcon, function(cmd, obj) {
  1465. if (!KE.util.inArray(cmd, arr)) {
  1466. var a = obj[0];
  1467. var span = obj[1];
  1468. a.className = 'ke-icon';
  1469. KE.util.setOpacity(span, 100);
  1470. a.onclick = new Function('KE.util.click("' + id + '", "' + cmd + '");');
  1471. a.onmouseover = function(){ this.className = "ke-icon-on"; };
  1472. a.onmouseout = function(){ this.className = "ke-icon"; };
  1473. }
  1474. });
  1475. },
  1476. disable : function(id, arr) {
  1477. KE.each(KE.g[id].toolbarIcon, function(cmd, obj) {
  1478. if (!KE.util.inArray(cmd, arr)) {
  1479. var a = obj[0];
  1480. var span = obj[1];
  1481. a.className = 'ke-icon-disabled';
  1482. KE.util.setOpacity(span, 50);
  1483. a.onclick = null;
  1484. a.onmouseover = null;
  1485. a.onmouseout = null;
  1486. }
  1487. });
  1488. },
  1489. create : function(id) {
  1490. KE.g[id].toolbarIcon = [];
  1491. var tableObj = KE.util.createTable();
  1492. var toolbar = tableObj.table;
  1493. toolbar.className = 'ke-toolbar';
  1494. toolbar.oncontextmenu = function() { return false; };
  1495. toolbar.onmousedown = function() { return false; };
  1496. toolbar.onmousemove = function() { return false; };
  1497. var toolbarCell = tableObj.cell;
  1498. var length = KE.g[id].items.length;
  1499. var cellNum = 0;
  1500. var row;
  1501. for (var i = 0; i < length; i++) {
  1502. var cmd = KE.g[id].items[i];
  1503. if (i == 0 || cmd == '-') {
  1504. var table = KE.util.createTable().table;
  1505. table.className = 'ke-toolbar-table';
  1506. row = table.insertRow(0);
  1507. cellNum = 0;
  1508. toolbarCell.appendChild(table);
  1509. if (cmd == '-') continue;
  1510. }
  1511. var cell = row.insertCell(cellNum);
  1512. cell.hideforcus = true;
  1513. cellNum++;
  1514. var a = KE.$$('a');
  1515. a.className = 'ke-icon';
  1516. a.href = 'javascript:;';
  1517. a.onclick = new Function('KE.util.click("' + id + '", "' + cmd + '");');
  1518. a.onmouseover = function(){ this.className = "ke-icon-on"; };
  1519. a.onmouseout = function(){ this.className = "ke-icon"; };
  1520. a.hidefocus = true;
  1521. a.title = KE.lang[cmd];
  1522. var span = KE.$$('span');
  1523. span.className = "ke-common-icon ke-icon-" + cmd;
  1524. if (KE.util.inArray(cmd, KE.g[id].defaultItems)) {
  1525. var url = KE.g[id].skinsPath + KE.g[id].skinType + '.gif';
  1526. span.style.backgroundImage = "url(" + url + ")";
  1527. }
  1528. a.appendChild(span);
  1529. cell.appendChild(a);
  1530. KE.g[id].toolbarIcon[cmd] = [a, span];
  1531. if (KE.toolbar.isSelected(id, cmd)) KE.toolbar.select(id, cmd);
  1532. }
  1533. return toolbar;
  1534. }
  1535. };
  1536. KE.history = {
  1537. add : function(id, minChangeFlag) {
  1538. var obj = KE.g[id];
  1539. var html = KE.util.getSrcData(id);
  1540. if (obj.undoStack.length > 0) {
  1541. var prevHtml = obj.undoStack[obj.undoStack.length - 1];
  1542. if (html == prevHtml) return;
  1543. if (minChangeFlag && Math.abs(html.length - prevHtml.length) < obj.minChangeSize) return;
  1544. }
  1545. obj.undoStack.push(html);
  1546. obj.redoStack = [];
  1547. },
  1548. undo : function(id) {
  1549. var obj = KE.g[id];
  1550. if (obj.undoStack.length == 0) return;
  1551. var html = KE.util.getSrcData(id);
  1552. obj.redoStack.push(html);
  1553. var prevHtml = obj.undoStack.pop();
  1554. if (html == prevHtml && obj.undoStack.length > 0) {
  1555. prevHtml = obj.undoStack.pop();
  1556. }
  1557. obj.iframeDoc.body.innerHTML = prevHtml;
  1558. obj.newTextarea.value = prevHtml;
  1559. },
  1560. redo : function(id) {
  1561. var obj = KE.g[id];
  1562. if (obj.redoStack.length == 0) return;
  1563. var html = KE.util.getSrcData(id);
  1564. obj.undoStack.push(html);
  1565. var nextHtml = obj.redoStack.pop();
  1566. obj.iframeDoc.body.innerHTML = nextHtml;
  1567. obj.newTextarea.value = nextHtml;
  1568. }
  1569. };
  1570. KE.remove = function(id, mode) {
  1571. mode = (typeof mode == "undefined") ? 0 : mode;
  1572. var container = KE.g[id].container;
  1573. if (mode == 1) {
  1574. document.body.removeChild(container);
  1575. } else {
  1576. var srcTextarea = KE.$(id);
  1577. srcTextarea.parentNode.removeChild(container);
  1578. }
  1579. document.body.removeChild(KE.g[id].hideDiv);
  1580. document.body.removeChild(KE.g[id].maskDiv);
  1581. KE.g[id].containner = null;
  1582. };
  1583. KE.create = function(id, mode) {
  1584. if (KE.browser == 'IE') try { document.execCommand('BackgroundImageCache', false, true); }catch(e){}
  1585. var srcTextarea = KE.$(id);
  1586. mode = (typeof mode == "undefined") ? 0 : mode;
  1587. if (mode == 0 && KE.g[id].container != null) return;
  1588. var width = KE.g[id].width || srcTextarea.style.width;
  1589. var height = KE.g[id].height || srcTextarea.style.height;
  1590. var tableObj = KE.util.createTable();
  1591. var container = tableObj.table;
  1592. container.className = 'ke-container';
  1593. container.style.width = width;
  1594. container.style.height = height;
  1595. var toolbarOuter = tableObj.cell;
  1596. toolbarOuter.className = 'ke-toolbar-outer';
  1597. var textareaOuter = container.insertRow(1).insertCell(0);
  1598. textareaOuter.className = 'ke-textarea-outer';
  1599. tableObj = KE.util.createTable();
  1600. var textareaTable = tableObj.table;
  1601. textareaTable.className = 'ke-textarea-table';
  1602. var textareaCell = tableObj.cell;
  1603. textareaOuter.appendChild(textareaTable);
  1604. var bottomOuter = container.insertRow(2).insertCell(0);
  1605. bottomOuter.className = 'ke-bottom-outer';
  1606. if (mode == 1) document.body.appendChild(container);
  1607. else srcTextarea.parentNode.insertBefore(container, srcTextarea);
  1608. var toolbarTable = KE.toolbar.create(id);
  1609. toolbarOuter.appendChild(toolbarTable);
  1610. var iframe = KE.$$('iframe');
  1611. iframe.className = 'ke-iframe';
  1612. iframe.setAttribute("frameBorder", "0");
  1613. var newTextarea = KE.$$('textarea');
  1614. newTextarea.className = 'ke-textarea';
  1615. newTextarea.style.display = 'none';
  1616. textareaCell.appendChild(iframe);
  1617. textareaCell.appendChild(newTextarea);
  1618. var bottom = KE.$$('table');
  1619. bottom.className = 'ke-bottom';
  1620. bottom.cellPadding = 0;
  1621. bottom.cellSpacing = 0;
  1622. bottom.border = 0;
  1623. var row = bottom.insertRow(0);
  1624. var bottomLeft = row.insertCell(0);
  1625. bottomLeft.className = 'ke-bottom-left';
  1626. var bottomRight = row.insertCell(1);
  1627. bottomRight.className = 'ke-bottom-right';
  1628. var span = KE.$$('span');
  1629. var url = KE.g[id].skinsPath + KE.g[id].skinType + '.gif';
  1630. span.style.backgroundImage = "url(" + url + ")";
  1631. span.className = 'ke-bottom-right-img';
  1632. bottomRight.appendChild(span);
  1633. bottomOuter.appendChild(bottom);
  1634. var hideDiv = KE.$$('div');
  1635. hideDiv.style.display = 'none';
  1636. var maskDiv = KE.$$('div');
  1637. maskDiv.className = 'ke-mask';
  1638. KE.util.setOpacity(maskDiv, 50);
  1639. document.body.appendChild(hideDiv);
  1640. document.body.appendChild(maskDiv);
  1641. srcTextarea.style.display = "none";
  1642. KE.util.setDefaultPlugin(id);
  1643. var iframeWin = iframe.contentWindow;
  1644. var iframeDoc = KE.util.getIframeDoc(iframe);
  1645. iframeDoc.designMode = "On";
  1646. var html = KE.util.getFullHtml(id, KE.g[id].tagLineMode);
  1647. iframeDoc.open();
  1648. iframeDoc.write(html);
  1649. iframeDoc.close();
  1650. if (!KE.g[id].wyswygMode) {
  1651. newTextarea.value = srcTextarea.value;
  1652. newTextarea.style.display = 'block';
  1653. iframe.style.display = 'none';
  1654. KE.toolbar.disable(id, ['source', 'preview', 'fullscreen']);
  1655. KE.toolbar.select(id, 'source');
  1656. }
  1657. if (KE.g[id].autoOnsubmitMode) {
  1658. var form = srcTextarea.parentNode;
  1659. while (form != null && form.tagName != 'FORM') { form = form.parentNode; }
  1660. if (form != null && form.tagName == 'FORM') {
  1661. KE.event.add(form, 'submit', new Function('KE.util.setData("' + id + '")'));
  1662. }
  1663. }
  1664. KE.event.add(iframeDoc, 'click', new Function('KE.layout.hide("' + id + '")'));
  1665. KE.event.add(iframeDoc, 'click', new Function('KE.toolbar.updateState("' + id + '")'));
  1666. KE.event.input(iframeDoc, new Function('KE.history.add("' + id + '", true)'));
  1667. KE.event.add(iframeDoc, 'keyup', new Function('KE.toolbar.updateState("' + id + '")'));
  1668. KE.event.add(newTextarea, 'click', new Function('KE.layout.hide("' + id + '")'));
  1669. KE.event.input(newTextarea, new Function('KE.history.add("' + id + '", true)'));
  1670. KE.g[id].container = container;
  1671. KE.g[id].toolbarTable = toolbarTable;
  1672. KE.g[id].textareaTable = textareaTable;
  1673. KE.g[id].iframe = iframe;
  1674. KE.g[id].newTextarea = newTextarea;
  1675. KE.g[id].srcTextarea = srcTextarea;
  1676. KE.g[id].bottom = bottom;
  1677. KE.g[id].hideDiv = hideDiv;
  1678. KE.g[id].maskDiv = maskDiv;
  1679. KE.g[id].iframeWin = iframeWin;
  1680. KE.g[id].iframeDoc = iframeDoc;
  1681. KE.g[id].width = width;
  1682. KE.g[id].height = height;
  1683. KE.util.resize(id, width, height);
  1684. KE.util.drag(id, bottomRight, container, function(objTop, objLeft, objWidth, objHeight, top, left) {
  1685. if (KE.g[id].resizeMode == 2) KE.util.resize(id, (objWidth + left) + 'px', (objHeight + top) + 'px', true);
  1686. else if (KE.g[id].resizeMode == 1) KE.util.resize(id, objWidth + 'px', (objHeight + top) + 'px', true);
  1687. }, true);
  1688. KE.util.drag(id, bottomLeft, container, function(objTop, objLeft, objWidth, objHeight, top, left) {
  1689. if (KE.g[id].resizeMode > 0) KE.util.resize(id, objWidth + 'px', (objHeight + top) + 'px', true);
  1690. }, true);
  1691. for (var i = 0, len = KE.g[id].items.length; i < len; i++) {
  1692. var cmd = KE.g[id].items[i];
  1693. if (KE.plugin[cmd] && KE.plugin[cmd].init) KE.plugin[cmd].init(id);
  1694. }
  1695. setTimeout(
  1696. function(){
  1697. if (srcTextarea.value !== "") iframeDoc.body.innerHTML = srcTextarea.value;
  1698. KE.history.add(id, false);
  1699. }, 1);
  1700. };
  1701. KE.init = function(config) {
  1702. config.wyswygMode = (typeof config.wyswygMode == "undefined") ? KE.setting.wyswygMode : config.wyswygMode;
  1703. config.autoOnsubmitMode = (typeof config.autoOnsubmitMode == "undefined") ? KE.setting.autoOnsubmitMode : config.autoOnsubmitMode;
  1704. config.resizeMode = (typeof config.resizeMode == "undefined") ? KE.setting.resizeMode : config.resizeMode;
  1705. config.filterMode = (typeof config.filterMode == "undefined") ? KE.setting.filterMode : config.filterMode;
  1706. config.tagLineMode = (typeof config.tagLineMode == "undefined") ? KE.setting.tagLineMode : config.tagLineMode;
  1707. config.skinType = config.skinType || KE.setting.skinType;
  1708. config.cssPath = config.cssPath || KE.setting.cssPath;
  1709. config.skinsPath = config.skinsPath || KE.setting.skinsPath;
  1710. config.pluginsPath = config.pluginsPath || KE.setting.pluginsPath;
  1711. config.minWidth = config.minWidth || KE.setting.minWidth;
  1712. config.minHeight = config.minHeight || KE.setting.minHeight;
  1713. config.minChangeSize = config.minChangeSize || KE.setting.minChangeSize;
  1714. config.siteDomains = config.siteDomains || KE.setting.siteDomains;
  1715. config.defaultItems = KE.setting.items;
  1716. config.items = config.items || KE.setting.items;
  1717. config.htmlTags = config.htmlTags || KE.setting.htmlTags;
  1718. KE.g[config.id] = config;
  1719. KE.g[config.id].undoStack = [];
  1720. KE.g[config.id].redoStack = [];
  1721. KE.util.loadStyle(config.skinsPath + config.skinType + '.css');
  1722. }
  1723. KE.show = function(config) {
  1724. KE.init(config);
  1725. KE.event.add(window, 'load', new Function('KE.create("' + config.id + '")'));
  1726. };
  1727. KE.plugin['about'] = {
  1728. click : function(id) {
  1729. var dialog = new KE.dialog({
  1730. id : id,
  1731. cmd : 'about',
  1732. width : 300,
  1733. height : 80,
  1734. title : KE.lang['about'],
  1735. noButton : KE.lang['close']
  1736. });
  1737. dialog.show();
  1738. }
  1739. };
  1740. KE.plugin['undo'] = {
  1741. init : function(id) {
  1742. KE.event.ctrl(KE.g[id].iframeDoc, 'Z', function(e) {
  1743. KE.plugin['undo'].click(id);
  1744. KE.util.focus(id);
  1745. });
  1746. },
  1747. click : function(id) {
  1748. KE.history.undo(id);
  1749. }
  1750. };
  1751. KE.plugin['redo'] = {
  1752. init : function(id) {
  1753. KE.event.ctrl(KE.g[id].iframeDoc, 'Y', function(e) {
  1754. KE.plugin['redo'].click(id);
  1755. KE.util.focus(id);
  1756. });
  1757. },
  1758. click : function(id) {
  1759. KE.history.redo(id);
  1760. }
  1761. };
  1762. KE.plugin['plainpaste'] = {
  1763. click : function(id) {
  1764. KE.util.selection(id);
  1765. var dialog = new KE.dialog({
  1766. id : id,
  1767. cmd : 'plainpaste',
  1768. width : 330,
  1769. height : 300,
  1770. title : KE.lang['plainpaste'],
  1771. yesButton : KE.lang['yes'],
  1772. noButton : KE.lang['no']
  1773. });
  1774. dialog.show();
  1775. },
  1776. exec : function(id) {
  1777. KE.util.select(id);
  1778. var dialogDoc = KE.util.getIframeDoc(KE.g[id].dialog);
  1779. var html = KE.$('textArea', dialogDoc).value;
  1780. html = KE.util.escape(html);
  1781. var re = new RegExp("\r\n|\n|\r", "g");
  1782. html = html.replace(re, "<br />$&");
  1783. KE.util.insertHtml(id, html);
  1784. KE.layout.hide(id);
  1785. KE.util.focus(id);
  1786. }
  1787. };
  1788. KE.plugin['wordpaste'] = {
  1789. click : function(id) {
  1790. KE.util.selection(id);
  1791. var dialog = new KE.dialog({
  1792. id : id,
  1793. cmd : 'wordpaste',
  1794. width : 330,
  1795. height : 300,
  1796. title : KE.lang['wordpaste'],
  1797. yesButton : KE.lang['yes'],
  1798. noButton : KE.lang['no']
  1799. });
  1800. dialog.show();
  1801. },
  1802. exec : function(id) {
  1803. KE.util.select(id);
  1804. var dialogDoc = KE.util.getIframeDoc(KE.g[id].dialog);
  1805. var wordIframe = KE.$('wordIframe', dialogDoc);
  1806. var wordDoc = KE.util.getIframeDoc(wordIframe);
  1807. KE.util.insertHtml(id, KE.util.outputHtml(id, wordDoc.body));
  1808. KE.layout.hide(id);
  1809. KE.util.focus(id);
  1810. }
  1811. };
  1812. KE.plugin['fullscreen'] = {
  1813. resetFull : function(id) {
  1814. var el = KE.util.getDocumentElement();
  1815. var width = el.clientWidth;
  1816. var height = el.clientHeight;
  1817. var left,top;
  1818. if (KE.browser == 'IE' || KE.browser == 'OPERA') {
  1819. left = document.body.parentNode.scrollLeft;
  1820. top = document.body.parentNode.scrollTop;
  1821. } else {
  1822. left = window.scrollX;
  1823. top = window.scrollY;
  1824. }
  1825. var div = KE.g[id].container;
  1826. div.style.left = left + 'px';
  1827. div.style.top = top + 'px';
  1828. div.style.zIndex = 19811211;
  1829. KE.util.resize(id, width + 'px', height + 'px');
  1830. },
  1831. click : function(id) {
  1832. var obj = KE.g[id];
  1833. var self = this;
  1834. var resizeListener = function(e) {
  1835. if (self.isSelected) {
  1836. KE.plugin["fullscreen"].resetFull(id);
  1837. }
  1838. }
  1839. if (this.isSelected) {
  1840. this.isSelected = false;
  1841. KE.util.setData(id);
  1842. KE.remove(id, 1);
  1843. KE.create(id, 2);
  1844. document.body.parentNode.style.overflow = 'auto';
  1845. KE.util.resize(id, this.width, this.height);
  1846. KE.event.remove(window, 'resize', resizeListener);
  1847. KE.toolbar.unselect(id, "fullscreen");
  1848. } else {
  1849. this.isSelected = true;
  1850. KE.util.setData(id);
  1851. this.width = KE.g[id].container.style.width;
  1852. this.height = KE.g[id].container.style.height;
  1853. KE.remove(id, 2);
  1854. KE.create(id, 1);
  1855. document.body.parentNode.style.overflow = 'hidden';
  1856. var div = KE.g[id].container;
  1857. div.style.position = 'absolute';
  1858. this.resetFull(id);
  1859. KE.event.add(window, 'resize', resizeListener);
  1860. KE.toolbar.select(id, "fullscreen");
  1861. }
  1862. }
  1863. };
  1864. KE.plugin['bgcolor'] = {
  1865. click : function(id) {
  1866. KE.util.selection(id);
  1867. var menu = new KE.menu({
  1868. id : id,
  1869. cmd : 'bgcolor'
  1870. });
  1871. menu.picker();
  1872. },
  1873. exec : function(id, value) {
  1874. var cmd = new KE.cmd(id);
  1875. cmd.wrap('span', [{'.background-color': value}]);
  1876. KE.history.add(id, false);
  1877. KE.layout.hide(id);
  1878. KE.util.focus(id);
  1879. }
  1880. };
  1881. KE.plugin['date'] = {
  1882. click : function(id) {
  1883. var date = new Date();
  1884. var year = date.getFullYear().toString(10);
  1885. var month = (date.getMonth() + 1).toString(10);
  1886. month = month.length < 2 ? '0' + month : month;
  1887. var day = date.getDate().toString(10);
  1888. day = day.length < 2 ? '0' + day : day;
  1889. var value = year + '-' + month + '-' + day;
  1890. KE.util.selection(id);
  1891. KE.util.insertHtml(id, value);
  1892. }
  1893. };
  1894. KE.plugin['fontname'] = {
  1895. click : function(id) {
  1896. var fontName = {
  1897. 'SimSun' : '宋体',
  1898. 'SimHei' : '黑体',
  1899. 'FangSong_GB2312' : '仿宋体',
  1900. 'KaiTi_GB2312' : '楷体',
  1901. 'NSimSun' : '新宋体',
  1902. 'Arial' : 'Arial',
  1903. 'Arial Black' : 'Arial Black',
  1904. 'Times New Roman' : 'Times New Roman',
  1905. 'Courier New' : 'Courier New',
  1906. 'Tahoma' : 'Tahoma',
  1907. 'Verdana' : 'Verdana'
  1908. };
  1909. var cmd = 'fontname';
  1910. KE.util.selection(id);
  1911. var menu = new KE.menu({
  1912. id : id,
  1913. cmd : cmd,
  1914. width : '160px'
  1915. });
  1916. KE.each(fontName, function(key, value) {
  1917. var html = '<span style="font-family: ' + key + ';">' + value + '</span>';
  1918. menu.add(html, new Function('KE.plugin["' + cmd + '"].exec("' + id + '", "' + key + '")'));
  1919. });
  1920. menu.show();
  1921. },
  1922. exec : function(id, value) {
  1923. var cmd = new KE.cmd(id);
  1924. cmd.wrap('span', [{'.font-family': value}]);
  1925. KE.history.add(id, false);
  1926. KE.layout.hide(id);
  1927. KE.util.focus(id);
  1928. }
  1929. };
  1930. KE.plugin['fontsize'] = {
  1931. click : function(id) {
  1932. var fontSize = ['9px', '10px', '12px', '14px', '16px', '18px', '24px', '32px'];
  1933. var cmd = 'fontsize';
  1934. KE.util.selection(id);
  1935. var menu = new KE.menu({
  1936. id : id,
  1937. cmd : cmd,
  1938. width : '100px'
  1939. });
  1940. for (var i = 0, len = fontSize.length; i < len; i++) {
  1941. var value = fontSize[i];
  1942. var html = '<span style="font-size: ' + value + ';">' + value + '</span>';
  1943. menu.add(html, new Function('KE.plugin["' + cmd + '"].exec("' + id + '", "' + value + '")'));
  1944. }
  1945. menu.show();
  1946. },
  1947. exec : function(id, value) {
  1948. var cmd = new KE.cmd(id);
  1949. cmd.wrap('span', [{'.font-size': value}]);
  1950. KE.history.add(id, false);
  1951. KE.layout.hide(id);
  1952. KE.util.focus(id);
  1953. }
  1954. };
  1955. KE.plugin['hr'] = {
  1956. click : function(id) {
  1957. KE.util.selection(id);
  1958. KE.util.insertHtml(id, '<hr />');
  1959. KE.util.focus(id);
  1960. }
  1961. };
  1962. KE.plugin['preview'] = {
  1963. click : function(id) {
  1964. var dialog = new KE.dialog({
  1965. id : id,
  1966. cmd : 'preview',
  1967. html : KE.util.getData(id),
  1968. width : 600,
  1969. height : 400,
  1970. useFrameCSS : true,
  1971. title : KE.lang['preview'],
  1972. noButton : KE.lang['close']
  1973. });
  1974. dialog.show();
  1975. }
  1976. };
  1977. KE.plugin['print'] = {
  1978. click : function(id) {
  1979. KE.util.selection(id);
  1980. KE.g[id].iframeWin.print();
  1981. }
  1982. };
  1983. KE.plugin['removeformat'] = {
  1984. click : function(id) {
  1985. KE.util.selection(id);
  1986. var cmd = new KE.cmd(id);
  1987. cmd.remove(KE.setting.inlineTags, ['class', 'style']);
  1988. KE.history.add(id, false);
  1989. KE.toolbar.updateState(id);
  1990. KE.util.focus(id);
  1991. }
  1992. };
  1993. KE.plugin['source'] = {
  1994. click : function(id) {
  1995. var obj = KE.g[id];
  1996. if (!obj.wyswygMode) {
  1997. obj.iframeDoc.body.innerHTML = obj.newTextarea.value;
  1998. obj.iframe.style.display = 'block';
  1999. obj.newTextarea.style.display = 'none';
  2000. KE.toolbar.able(id, ['source', 'preview', 'fullscreen']);
  2001. obj.wyswygMode = true;
  2002. this.isSelected = false;
  2003. KE.toolbar.unselect(id, "source");
  2004. } else {
  2005. KE.layout.hide(id);
  2006. if (KE.g[id].filterMode) {
  2007. obj.newTextarea.value = KE.util.outputHtml(id, obj.iframeDoc.body);
  2008. } else {
  2009. obj.newTextarea.value = KE.util.htmlToXhtml(id, obj.iframeDoc.body);
  2010. }
  2011. obj.iframe.style.display = 'none';
  2012. obj.newTextarea.style.display = 'block';
  2013. KE.toolbar.disable(id, ['source', 'preview', 'fullscreen']);
  2014. obj.wyswygMode = false;
  2015. this.isSelected = true;
  2016. KE.toolbar.select(id, "source");
  2017. }
  2018. KE.util.focus(id);
  2019. }
  2020. };
  2021. KE.plugin['textcolor'] = {
  2022. click : function(id) {
  2023. KE.util.selection(id);
  2024. var menu = new KE.menu({
  2025. id : id,
  2026. cmd : 'textcolor'
  2027. });
  2028. menu.picker();
  2029. },
  2030. exec : function(id, value) {
  2031. var cmd = new KE.cmd(id);
  2032. cmd.wrap('span', [{'.color': value}]);
  2033. KE.history.add(id, false);
  2034. KE.layout.hide(id);
  2035. KE.util.focus(id);
  2036. }
  2037. };
  2038. KE.plugin['time'] = {
  2039. click : function(id) {
  2040. var date = new Date();
  2041. var hour = date.getHours().toString(10);
  2042. hour = hour.length < 2 ? '0' + hour : hour;
  2043. var minute = date.getMinutes().toString(10);
  2044. minute = minute.length < 2 ? '0' + minute : minute;
  2045. var second = date.getSeconds().toString(10);
  2046. second = second.length < 2 ? '0' + second : second;
  2047. var value = hour + ':' + minute + ':' + second;
  2048. KE.util.selection(id);
  2049. KE.util.insertHtml(id, value);
  2050. }
  2051. };
  2052. KE.plugin['title'] = {
  2053. click : function(id) {
  2054. var title = {
  2055. 'H1' : '标题 1',
  2056. 'H2' : '标题 2',
  2057. 'H3' : '标题 3',
  2058. 'H4' : '标题 4',
  2059. 'H5' : '标题 5',
  2060. 'H6' : '标题 6'
  2061. };
  2062. var cmd = 'title';
  2063. KE.util.selection(id);
  2064. var menu = new KE.menu({
  2065. id : id,
  2066. cmd : cmd,
  2067. width : '120px'
  2068. });
  2069. KE.each(title, function(key, value) {
  2070. var html = '<' + key + ' style="margin:0px;">' + value + '</' + key + '>';
  2071. menu.add(html, new Function('KE.plugin["' + cmd + '"].exec("' + id + '", "<' + key + '>")'));
  2072. });
  2073. menu.show();
  2074. },
  2075. exec : function(id, value) {
  2076. KE.util.select(id);
  2077. KE.util.execCommand(id, 'formatblock', value);
  2078. KE.layout.hide(id);
  2079. KE.util.focus(id);
  2080. }
  2081. };
  2082. KE.plugin['emoticons'] = {
  2083. icon : 'emoticons.gif',
  2084. click : function(id) {
  2085. var emoticonTable = [
  2086. ['etc_01.gif','etc_02.gif','etc_03.gif','etc_04.gif','etc_05.gif','etc_06.gif'],
  2087. ['etc_07.gif','etc_08.gif','etc_09.gif','etc_10.gif','etc_11.gif','etc_12.gif'],
  2088. ['etc_13.gif','etc_14.gif','etc_15.gif','etc_16.gif','etc_17.gif','etc_18.gif'],
  2089. ['etc_19.gif','etc_20.gif','etc_21.gif','etc_22.gif','etc_23.gif','etc_24.gif'],
  2090. ['etc_25.gif','etc_26.gif','etc_27.gif','etc_28.gif','etc_29.gif','etc_30.gif'],
  2091. ['etc_31.gif','etc_32.gif','etc_33.gif','etc_34.gif','etc_35.gif','etc_36.gif']
  2092. ];
  2093. var cmd = 'emoticons';
  2094. KE.util.selection(id);
  2095. var table = KE.$$('table');
  2096. table.cellPadding = 0;
  2097. table.cellSpacing = 2;
  2098. table.border = 0;
  2099. table.style.margin = 0;
  2100. table.style.padding = 0;
  2101. table.style.borderCollapse = 'separate';
  2102. for (var i = 0; i < emoticonTable.length; i++) {
  2103. var row = table.insertRow(i);
  2104. for (var j = 0; j < emoticonTable[i].length; j++) {
  2105. var cell = row.insertCell(j);
  2106. cell.style.margin = 0;
  2107. cell.style.padding = '1px';
  2108. cell.style.border = '1px solid #F0F0EE';
  2109. cell.style.cursor = 'pointer';
  2110. cell.onmouseover = function() {this.style.borderColor = '#000000'; }
  2111. cell.onmouseout = function() {this.style.borderColor = '#F0F0EE'; }
  2112. cell.onclick = new Function('KE.plugin["' + cmd + '"].exec("' + id + '", "' + emoticonTable[i][j] + '")');
  2113. var img = KE.$$('img');
  2114. img.src = KE.g[id].pluginsPath + 'emoticons/' + emoticonTable[i][j];
  2115. cell.appendChild(img);
  2116. }
  2117. }
  2118. var menu = new KE.menu({
  2119. id : id,
  2120. cmd : cmd
  2121. });
  2122. menu.append(table);
  2123. menu.show();
  2124. },
  2125. exec : function(id, value) {
  2126. KE.util.select(id);
  2127. var html = '<img src="' + KE.g[id].pluginsPath + 'emoticons/' + value + '" border="0" />';
  2128. KE.util.insertHtml(id, html);
  2129. KE.layout.hide(id);
  2130. KE.util.focus(id);
  2131. }
  2132. };
  2133. KE.plugin['flash'] = {
  2134. click : function(id) {
  2135. KE.util.selection(id);
  2136. var dialog = new KE.dialog({
  2137. id : id,
  2138. cmd : 'flash',
  2139. width : 280,
  2140. height : 250,
  2141. title : "Flash",
  2142. previewButton : KE.lang['preview'],
  2143. yesButton : KE.lang['yes'],
  2144. noButton : KE.lang['no']
  2145. });
  2146. dialog.show();
  2147. },
  2148. check : function(id, url) {
  2149. if (url.match(/\w+:\/\/.{3,}/) == null) {
  2150. alert(KE.lang['invalidUrl']);
  2151. window.focus();
  2152. KE.g[id].yesButton.focus();
  2153. return false;
  2154. }
  2155. return true;
  2156. },
  2157. preview : function(id) {
  2158. var dialogDoc = KE.util.getIframeDoc(KE.g[id].dialog);
  2159. var url = KE.$('url', dialogDoc).value;
  2160. if (!this.check(id, url)) return false;
  2161. var embed = KE.$$('embed', dialogDoc);
  2162. embed.src = url;
  2163. embed.type = "application/x-shockwave-flash";
  2164. embed.quality = "high";
  2165. embed.width = 190;
  2166. embed.height = 190;
  2167. KE.$('previewDiv', dialogDoc).innerHTML = "";
  2168. KE.$('previewDiv', dialogDoc).appendChild(embed);
  2169. },
  2170. exec : function(id) {
  2171. KE.util.select(id);
  2172. var dialogDoc = KE.util.getIframeDoc(KE.g[id].dialog);
  2173. var url = KE.$('url', dialogDoc).value;
  2174. if (!this.check(id, url)) return false;
  2175. var html = '<embed src="' + url + '" type="application/x-shockwave-flash" quality="high" />';
  2176. KE.util.insertHtml(id, html);
  2177. KE.layout.hide(id);
  2178. KE.util.focus(id);
  2179. }
  2180. };
  2181. KE.plugin['image'] = {
  2182. click : function(id) {
  2183. KE.util.selection(id);
  2184. var dialog = new KE.dialog({
  2185. id : id,
  2186. cmd : 'image',
  2187. width : 310,
  2188. height : 90,
  2189. title : KE.lang['image'],
  2190. yesButton : KE.lang['yes'],
  2191. noButton : KE.lang['no']
  2192. });
  2193. dialog.show();
  2194. },
  2195. check : function(id) {
  2196. var dialogDoc = KE.util.getIframeDoc(KE.g[id].dialog);
  2197. var type = KE.$('type', dialogDoc).value;
  2198. var url = '';
  2199. if (type == 1) {
  2200. url = KE.$('imgFile', dialogDoc).value;
  2201. } else {
  2202. url = KE.$('url', dialogDoc).value;
  2203. }
  2204. var width = KE.$('imgWidth', dialogDoc).value;
  2205. var height = KE.$('imgHeight', dialogDoc).value;
  2206. var border = KE.$('imgBorder', dialogDoc).value;
  2207. if (!url.match(/\.(jpg|jpeg|gif|bmp|png)(\s|$)/i)) {
  2208. alert(KE.lang['invalidImg']);
  2209. window.focus();
  2210. KE.g[id].yesButton.focus();
  2211. return false;
  2212. }
  2213. if (width.match(/^\d+$/) == null) {
  2214. alert(KE.lang['invalidWidth']);
  2215. window.focus();
  2216. KE.g[id].yesButton.focus();
  2217. return false;
  2218. }
  2219. if (height.match(/^\d+$/) == null) {
  2220. alert(KE.lang['invalidHeight']);
  2221. window.focus();
  2222. KE.g[id].yesButton.focus();
  2223. return false;
  2224. }
  2225. if (border.match(/^\d+$/) == null) {
  2226. alert(KE.lang['invalidBorder']);
  2227. window.focus();
  2228. KE.g[id].yesButton.focus();
  2229. return false;
  2230. }
  2231. return true;
  2232. },
  2233. exec : function(id) {
  2234. KE.util.select(id);
  2235. var dialogDoc = KE.util.getIframeDoc(KE.g[id].dialog);
  2236. var type = KE.$('type', dialogDoc).value;
  2237. if (!this.check(id)) return false;
  2238. if (type == 1) {
  2239. KE.$('editorId', dialogDoc).value = id;
  2240. dialogDoc.uploadForm.submit();
  2241. return false;
  2242. } else {
  2243. var url = KE.$('url', dialogDoc).value;
  2244. var title = KE.$('imgTitle', dialogDoc).value;
  2245. var width = KE.$('imgWidth', dialogDoc).value;
  2246. var height = KE.$('imgHeight', dialogDoc).value;
  2247. var border = KE.$('imgBorder', dialogDoc).value;
  2248. this.insert(id, url, title, width, height, border);
  2249. }
  2250. },
  2251. insert : function(id, url, title, width, height, border) {
  2252. var html = '<img src="' + url + '" ';
  2253. if (width > 0) html += 'width="' + width + '" ';
  2254. if (height > 0) html += 'height="' + height + '" ';
  2255. if (title) html += 'title="' + title + '" ';
  2256. html += 'alt="' + title + '" ';
  2257. html += 'border="' + border + '" />';
  2258. KE.util.insertHtml(id, html);
  2259. KE.layout.hide(id);
  2260. KE.util.focus(id);
  2261. }
  2262. };
  2263. KE.plugin['layer'] = {
  2264. click : function(id) {
  2265. var cmd = 'layer';
  2266. var styles = [
  2267. 'margin:5px;border:1px solid #000000;',
  2268. 'margin:5px;border:2px solid #000000;',
  2269. 'margin:5px;border:1px dashed #000000;',
  2270. 'margin:5px;border:2px dashed #000000;',
  2271. 'margin:5px;border:1px dotted #000000;',
  2272. 'margin:5px;border:2px dotted #000000;'
  2273. ];
  2274. KE.util.selection(id);
  2275. var menu = new KE.menu({
  2276. id : id,
  2277. cmd : cmd,
  2278. width : '150px'
  2279. });
  2280. for (var i = 0; i < styles.length; i++) {
  2281. var html = '<div style="height:15px;' + styles[i] + '"></div>';
  2282. menu.add(html, new Function('KE.plugin["' + cmd + '"].exec("' + id + '", "padding:5px;' + styles[i] + '")'));
  2283. }
  2284. menu.show();
  2285. },
  2286. exec : function(id, value) {
  2287. KE.util.select(id);
  2288. var html = '<div style="' + value + '">' + KE.lang['pleaseInput'] + '</div>';
  2289. KE.util.insertHtml(id, html);
  2290. KE.layout.hide(id);
  2291. KE.util.focus(id);
  2292. }
  2293. };
  2294. KE.plugin['link'] = {
  2295. click : function(id) {
  2296. KE.util.selection(id);
  2297. var dialog = new KE.dialog({
  2298. id : id,
  2299. cmd : 'link',
  2300. width : 310,
  2301. height : 70,
  2302. title : KE.lang['link'],
  2303. yesButton : KE.lang['yes'],
  2304. noButton : KE.lang['no']
  2305. });
  2306. dialog.show();
  2307. },
  2308. exec : function(id) {
  2309. KE.util.select(id);
  2310. var iframeDoc = KE.g[id].iframeDoc;
  2311. var range = KE.g[id].range;
  2312. var dialogDoc = KE.util.getIframeDoc(KE.g[id].dialog);
  2313. var url = KE.$('hyperLink', dialogDoc).value;
  2314. var target = KE.$('linkType', dialogDoc).value;
  2315. if (url.match(/\w+:\/\/.{3,}/) == null) {
  2316. alert(KE.lang['invalidUrl']);
  2317. window.focus();
  2318. KE.g[id].yesButton.focus();
  2319. return false;
  2320. }
  2321. var node;
  2322. if (KE.browser == 'IE') {
  2323. node = range.item ? range.item(0).parentNode : iframeDoc.body;
  2324. } else {
  2325. node = (range.startContainer == range.endContainer) ? range.startContainer.parentNode : iframeDoc.body;
  2326. }
  2327. if (node && node.tagName == 'A') node = node.parentNode;
  2328. if (!node) node = iframeDoc.body;
  2329. iframeDoc.execCommand("createlink", false, "__ke_temp_url__");
  2330. var arr = node.getElementsByTagName('a');
  2331. for (var i = 0, l = arr.length; i < l; i++) {
  2332. if (arr[i].href.match(/\/?__ke_temp_url__$/) != null) {
  2333. arr[i].href = url;
  2334. if (target) arr[i].target = target;
  2335. }
  2336. }
  2337. KE.history.add(id);
  2338. KE.layout.hide(id);
  2339. KE.util.focus(id);
  2340. }
  2341. };
  2342. KE.plugin['media'] = {
  2343. click : function(id) {
  2344. KE.util.selection(id);
  2345. var dialog = new KE.dialog({
  2346. id : id,
  2347. cmd : 'media',
  2348. width : 280,
  2349. height : 250,
  2350. title : KE.lang['media'],
  2351. previewButton : KE.lang['preview'],
  2352. yesButton : KE.lang['yes'],
  2353. noButton : KE.lang['no']
  2354. });
  2355. dialog.show();
  2356. },
  2357. check : function(id, url) {
  2358. if (!url.match(/^\w+:\/\/.{3,}\.(mp3|wav|wma|wmv|mid|avi|mpg|mpeg|asf|rm|rmvb)(\?|$)/i)) {
  2359. alert(KE.lang['invalidMedia']);
  2360. window.focus();
  2361. KE.g[id].yesButton.focus();
  2362. return false;
  2363. }
  2364. return true;
  2365. },
  2366. preview : function(id) {
  2367. var dialogDoc = KE.util.getIframeDoc(KE.g[id].dialog);
  2368. var url = KE.$('url', dialogDoc).value;
  2369. if (!this.check(id, url)) return false;
  2370. var embed = KE.$$('embed', dialogDoc);
  2371. embed.src = url;
  2372. if (url.match(/\.(rm|rmvb)$/i) == null) {
  2373. embed.type = "video/x-ms-asf-plugin";
  2374. } else {
  2375. embed.type = "audio/x-pn-realaudio-plugin";
  2376. }
  2377. embed.loop = "true";
  2378. embed.autostart = "true";
  2379. embed.width = 260;
  2380. embed.height = 190;
  2381. KE.$('previewDiv', dialogDoc).innerHTML = "";
  2382. KE.$('previewDiv', dialogDoc).appendChild(embed);
  2383. },
  2384. exec : function(id) {
  2385. KE.util.select(id);
  2386. var dialogDoc = KE.util.getIframeDoc(KE.g[id].dialog);
  2387. var url = KE.$('url', dialogDoc).value;
  2388. if (!this.check(id, url)) return false;
  2389. var html;
  2390. if (url.match(/\.(rm|rmvb)$/i) == null) {
  2391. html = '<embed src="' + url + '" type="video/x-ms-asf-plugin" loop="true" autostart="true" />';
  2392. } else {
  2393. html = '<embed src="' + url + '" type="audio/x-pn-realaudio-plugin" loop="true" autostart="true" />';
  2394. }
  2395. KE.util.insertHtml(id, html);
  2396. KE.layout.hide(id);
  2397. KE.util.focus(id);
  2398. }
  2399. };
  2400. KE.plugin['specialchar'] = {
  2401. click : function(id) {
  2402. var charTable = [
  2403. ['§','№','☆','★','○','●','◎','◇','◆','□'],
  2404. ['℃','‰','■','△','▲','※','→','←','↑','↓'],
  2405. ['〓','¤','°','#','&','@','\','︿','_',' ̄'],
  2406. ['―','α','β','γ','δ','ε','ζ','η','θ','ι'],
  2407. ['κ','λ','μ','ν','ξ','ο','π','ρ','σ','τ'],
  2408. ['υ','φ','χ','ψ','ω','≈','≡','≠','=','≤'],
  2409. ['≥','<','>','≮','≯','∷','±','+','-','×'],
  2410. ['÷','/','∫','∮','∝','∞','∧','∨','∑','∏'],
  2411. ['∪','∩','∈','∵','∴','⊥','∥','∠','⌒','⊙'],
  2412. ['≌','∽','〖','〗','【','】','(',')','[',']']
  2413. ];
  2414. var cmd = 'specialchar';
  2415. KE.util.selection(id);
  2416. var table = KE.$$('table');
  2417. table.cellPadding = 0;
  2418. table.cellSpacing = 2;
  2419. table.border = 0;
  2420. table.style.margin = 0;
  2421. table.style.padding = 0;
  2422. table.style.borderCollapse = 'separate';
  2423. for (var i = 0; i < charTable.length; i++) {
  2424. var row = table.insertRow(i);
  2425. for (var j = 0; j < charTable[i].length; j++) {
  2426. var cell = row.insertCell(j);
  2427. cell.style.padding = '1px';
  2428. cell.style.margin = 0;
  2429. cell.style.border = '1px solid #AAAAAA';
  2430. cell.style.fontSize = '12px';
  2431. cell.style.cursor = 'pointer';
  2432. cell.onmouseover = function() {this.style.borderColor = '#000000'; }
  2433. cell.onmouseout = function() {this.style.borderColor = '#AAAAAA'; }
  2434. cell.onclick = new Function('KE.plugin["' + cmd + '"].exec("' + id + '", "' + charTable[i][j] + '")');
  2435. cell.innerHTML = charTable[i][j];
  2436. }
  2437. }
  2438. var menu = new KE.menu({
  2439. id : id,
  2440. cmd : cmd
  2441. });
  2442. menu.append(table);
  2443. menu.show();
  2444. },
  2445. exec : function(id, value) {
  2446. KE.util.select(id);
  2447. KE.util.insertHtml(id, value);
  2448. KE.layout.hide(id);
  2449. KE.util.focus(id);
  2450. }
  2451. };
  2452. KE.plugin['table'] = {
  2453. selected : function(id, i, j) {
  2454. var text = i.toString(10) + ' by ' + j.toString(10) + ' Table';
  2455. KE.$('tableLocation' + id).innerHTML = text;
  2456. var num = 10;
  2457. for (var m = 1; m <= num; m++) {
  2458. for (var n = 1; n <= num; n++) {
  2459. var td = KE.$('tableTd' + id + m.toString(10) + '_' + n.toString(10) + '');
  2460. if (m <= i && n <= j) {
  2461. td.style.backgroundColor = '#CCCCCC';
  2462. } else {
  2463. td.style.backgroundColor = '#FFFFFF';
  2464. }
  2465. }
  2466. }
  2467. },
  2468. click : function(id) {
  2469. var cmd = 'table';
  2470. KE.util.selection(id);
  2471. var num = 10;
  2472. var html = '<table cellpadding="0" cellspacing="0" border="0" style="width:130px;border-collapse:separate;padding:0;margin:0;">';
  2473. for (var i = 1; i <= num; i++) {
  2474. html += '<tr>';
  2475. for (var j = 1; j <= num; j++) {
  2476. var value = i.toString(10) + ',' + j.toString(10);
  2477. html += '<td id="tableTd' + id + i.toString(10) + '_' + j.toString(10) +
  2478. '" style="font-size:1px;width:12px;height:12px;background-color:#FFFFFF;' +
  2479. 'border:1px solid #DDDDDD;cursor:pointer;margin:0;padding:0;" ' +
  2480. 'onclick="javascript:KE.plugin[\'table\'].exec(\'' + id + '\', \'' + value + '\');" ' +
  2481. 'onmouseover="javascript:KE.plugin[\'table\'].selected(\'' + id + '\', \'' + i.toString(10) +
  2482. '\', \'' + j.toString(10) + '\');">&nbsp;</td>';
  2483. }
  2484. html += '</tr>';
  2485. }
  2486. html += '<tr><td colspan="10" id="tableLocation' + id +
  2487. '" style="font-size:12px;text-align:center;height:20px;margin:0;padding:0;border:0;"></td></tr>';
  2488. html += '</table>';
  2489. var menu = new KE.menu({
  2490. id : id,
  2491. cmd : cmd
  2492. });
  2493. menu.insert(html);
  2494. menu.show();
  2495. },
  2496. exec : function(id, value) {
  2497. KE.util.select(id);
  2498. var location = value.split(',');
  2499. var html = '<table border="1">';
  2500. for (var i = 0; i < location[0]; i++) {
  2501. html += '<tr>';
  2502. for (var j = 0; j < location[1]; j++) {
  2503. html += '<td>&nbsp;</td>';
  2504. }
  2505. html += '</tr>';
  2506. }
  2507. html += '</table>';
  2508. KE.util.insertHtml(id, html);
  2509. KE.layout.hide(id);
  2510. KE.util.focus(id);
  2511. }
  2512. };