PageRenderTime 116ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 1ms

/source/ueditor/umeditor.js

https://github.com/ywl890227/longphp
JavaScript | 9737 lines | 8379 code | 673 blank | 685 comment | 923 complexity | c1e751c883a0788646a35e7bd90903ad MD5 | raw file
Possible License(s): LGPL-3.0
  1. /*!
  2. * UEditor Mini版本
  3. * version: 1.0.0
  4. * build: Tue Sep 17 2013 15:57:30 GMT+0800 (CST)
  5. */
  6. (function(){
  7. UMEDITOR_CONFIG = window.UMEDITOR_CONFIG || {};
  8. window.UM = {
  9. plugins : {},
  10. commands : {},
  11. I18N : {},
  12. version : "1.0.0"
  13. };
  14. var dom = UM.dom = {};
  15. /**
  16. * @file
  17. * @name UM.browser
  18. * @short Browser
  19. * @desc UEditor中采用的浏览器判断模块
  20. */
  21. var browser = UM.browser = function(){
  22. var agent = navigator.userAgent.toLowerCase(),
  23. opera = window.opera,
  24. browser = {
  25. /**
  26. * 检测浏览器是否为IE
  27. * @name ie
  28. * @grammar UM.browser.ie => true|false
  29. */
  30. ie : !!window.ActiveXObject,
  31. /**
  32. * 检测浏览器是否为Opera
  33. * @name opera
  34. * @grammar UM.browser.opera => true|false
  35. */
  36. opera : ( !!opera && opera.version ),
  37. /**
  38. * 检测浏览器是否为webkit内核
  39. * @name webkit
  40. * @grammar UM.browser.webkit => true|false
  41. */
  42. webkit : ( agent.indexOf( ' applewebkit/' ) > -1 ),
  43. /**
  44. * 检测浏览器是否为mac系统下的浏览器
  45. * @name mac
  46. * @grammar UM.browser.mac => true|false
  47. */
  48. mac : ( agent.indexOf( 'macintosh' ) > -1 ),
  49. /**
  50. * 检测浏览器是否处于怪异模式
  51. * @name quirks
  52. * @grammar UM.browser.quirks => true|false
  53. */
  54. quirks : ( document.compatMode == 'BackCompat' )
  55. };
  56. /**
  57. * 检测浏览器是否处为gecko内核
  58. * @name gecko
  59. * @grammar UM.browser.gecko => true|false
  60. */
  61. browser.gecko =( navigator.product == 'Gecko' && !browser.webkit && !browser.opera );
  62. var version = 0;
  63. // Internet Explorer 6.0+
  64. if ( browser.ie ){
  65. version = parseFloat( agent.match( /msie (\d+)/ )[1] );
  66. /**
  67. * 检测浏览器是否为 IE9 模式
  68. * @name ie9Compat
  69. * @grammar UM.browser.ie9Compat => true|false
  70. */
  71. browser.ie9Compat = document.documentMode == 9;
  72. /**
  73. * 检测浏览器是否为 IE8 浏览器
  74. * @name ie8
  75. * @grammar UM.browser.ie8 => true|false
  76. */
  77. browser.ie8 = !!document.documentMode;
  78. /**
  79. * 检测浏览器是否为 IE8 模式
  80. * @name ie8Compat
  81. * @grammar UM.browser.ie8Compat => true|false
  82. */
  83. browser.ie8Compat = document.documentMode == 8;
  84. /**
  85. * 检测浏览器是否运行在 兼容IE7模式
  86. * @name ie7Compat
  87. * @grammar UM.browser.ie7Compat => true|false
  88. */
  89. browser.ie7Compat = ( ( version == 7 && !document.documentMode )
  90. || document.documentMode == 7 );
  91. /**
  92. * 检测浏览器是否IE6模式或怪异模式
  93. * @name ie6Compat
  94. * @grammar UM.browser.ie6Compat => true|false
  95. */
  96. browser.ie6Compat = ( version < 7 || browser.quirks );
  97. browser.ie9above = version > 8;
  98. browser.ie9below = version < 9
  99. }
  100. // Gecko.
  101. if ( browser.gecko ){
  102. var geckoRelease = agent.match( /rv:([\d\.]+)/ );
  103. if ( geckoRelease )
  104. {
  105. geckoRelease = geckoRelease[1].split( '.' );
  106. version = geckoRelease[0] * 10000 + ( geckoRelease[1] || 0 ) * 100 + ( geckoRelease[2] || 0 ) * 1;
  107. }
  108. }
  109. /**
  110. * 检测浏览器是否为chrome
  111. * @name chrome
  112. * @grammar UM.browser.chrome => true|false
  113. */
  114. if (/chrome\/(\d+\.\d)/i.test(agent)) {
  115. browser.chrome = + RegExp['\x241'];
  116. }
  117. /**
  118. * 检测浏览器是否为safari
  119. * @name safari
  120. * @grammar UM.browser.safari => true|false
  121. */
  122. if(/(\d+\.\d)?(?:\.\d)?\s+safari\/?(\d+\.\d+)?/i.test(agent) && !/chrome/i.test(agent)){
  123. browser.safari = + (RegExp['\x241'] || RegExp['\x242']);
  124. }
  125. // Opera 9.50+
  126. if ( browser.opera )
  127. version = parseFloat( opera.version() );
  128. // WebKit 522+ (Safari 3+)
  129. if ( browser.webkit )
  130. version = parseFloat( agent.match( / applewebkit\/(\d+)/ )[1] );
  131. /**
  132. * 浏览器版本判断
  133. * IE系列返回值为5,6,7,8,9,10等
  134. * gecko系列会返回10900,158900等.
  135. * webkit系列会返回其build号 (如 522等).
  136. * @name version
  137. * @grammar UM.browser.version => number
  138. * @example
  139. * if ( UM.browser.ie && UM.browser.version == 6 ){
  140. * alert( "Ouch!居然是万恶的IE6!" );
  141. * }
  142. */
  143. browser.version = version;
  144. /**
  145. * 是否是兼容模式的浏览器
  146. * @name isCompatible
  147. * @grammar UM.browser.isCompatible => true|false
  148. * @example
  149. * if ( UM.browser.isCompatible ){
  150. * alert( "你的浏览器相当不错哦!" );
  151. * }
  152. */
  153. browser.isCompatible =
  154. !browser.mobile && (
  155. ( browser.ie && version >= 6 ) ||
  156. ( browser.gecko && version >= 10801 ) ||
  157. ( browser.opera && version >= 9.5 ) ||
  158. ( browser.air && version >= 1 ) ||
  159. ( browser.webkit && version >= 522 ) ||
  160. false );
  161. return browser;
  162. }();
  163. //快捷方式
  164. var ie = browser.ie,
  165. webkit = browser.webkit,
  166. gecko = browser.gecko,
  167. opera = browser.opera;
  168. /**
  169. * @file
  170. * @name UM.Utils
  171. * @short Utils
  172. * @desc UEditor封装使用的静态工具函数
  173. * @import editor.js
  174. */
  175. var utils = UM.utils = {
  176. /**
  177. * 遍历数组,对象,nodeList
  178. * @name each
  179. * @grammar UM.utils.each(obj,iterator,[context])
  180. * @since 1.2.4+
  181. * @desc
  182. * * obj 要遍历的对象
  183. * * iterator 遍历的方法,方法的第一个是遍历的值,第二个是索引,第三个是obj
  184. * * context iterator的上下文
  185. * @example
  186. * UM.utils.each([1,2],function(v,i){
  187. * console.log(v)//值
  188. * console.log(i)//索引
  189. * })
  190. * UM.utils.each(document.getElementsByTagName('*'),function(n){
  191. * console.log(n.tagName)
  192. * })
  193. */
  194. each : function(obj, iterator, context) {
  195. if (obj == null) return;
  196. if (obj.length === +obj.length) {
  197. for (var i = 0, l = obj.length; i < l; i++) {
  198. if(iterator.call(context, obj[i], i, obj) === false)
  199. return false;
  200. }
  201. } else {
  202. for (var key in obj) {
  203. if (obj.hasOwnProperty(key)) {
  204. if(iterator.call(context, obj[key], key, obj) === false)
  205. return false;
  206. }
  207. }
  208. }
  209. },
  210. makeInstance:function (obj) {
  211. var noop = new Function();
  212. noop.prototype = obj;
  213. obj = new noop;
  214. noop.prototype = null;
  215. return obj;
  216. },
  217. /**
  218. * 将source对象中的属性扩展到target对象上
  219. * @name extend
  220. * @grammar UM.utils.extend(target,source) => Object //覆盖扩展
  221. * @grammar UM.utils.extend(target,source,true) ==> Object //保留扩展
  222. */
  223. extend:function (t, s, b) {
  224. if (s) {
  225. for (var k in s) {
  226. if (!b || !t.hasOwnProperty(k)) {
  227. t[k] = s[k];
  228. }
  229. }
  230. }
  231. return t;
  232. },
  233. extend2:function (t) {
  234. var a = arguments;
  235. for (var i = 1; i < a.length; i++) {
  236. var x = a[i];
  237. for (var k in x) {
  238. if (!t.hasOwnProperty(k)) {
  239. t[k] = x[k];
  240. }
  241. }
  242. }
  243. return t;
  244. },
  245. /**
  246. * 模拟继承机制,subClass继承superClass
  247. * @name inherits
  248. * @grammar UM.utils.inherits(subClass,superClass) => subClass
  249. * @example
  250. * function SuperClass(){
  251. * this.name = "小李";
  252. * }
  253. * SuperClass.prototype = {
  254. * hello:function(str){
  255. * console.log(this.name + str);
  256. * }
  257. * }
  258. * function SubClass(){
  259. * this.name = "小张";
  260. * }
  261. * UM.utils.inherits(SubClass,SuperClass);
  262. * var sub = new SubClass();
  263. * sub.hello("早上好!"); ==> "小张早上好!"
  264. */
  265. inherits:function (subClass, superClass) {
  266. var oldP = subClass.prototype,
  267. newP = utils.makeInstance(superClass.prototype);
  268. utils.extend(newP, oldP, true);
  269. subClass.prototype = newP;
  270. return (newP.constructor = subClass);
  271. },
  272. /**
  273. * 用指定的context作为fn上下文,也就是this
  274. * @name bind
  275. * @grammar UM.utils.bind(fn,context) => fn
  276. */
  277. bind:function (fn, context) {
  278. return function () {
  279. return fn.apply(context, arguments);
  280. };
  281. },
  282. /**
  283. * 创建延迟delay执行的函数fn
  284. * @name defer
  285. * @grammar UM.utils.defer(fn,delay) =>fn //延迟delay毫秒执行fn,返回fn
  286. * @grammar UM.utils.defer(fn,delay,exclusion) =>fn //延迟delay毫秒执行fn,若exclusion为真,则互斥执行fn
  287. * @example
  288. * function test(){
  289. * console.log("延迟输出!");
  290. * }
  291. * //非互斥延迟执行
  292. * var testDefer = UM.utils.defer(test,1000);
  293. * testDefer(); => "延迟输出!";
  294. * testDefer(); => "延迟输出!";
  295. * //互斥延迟执行
  296. * var testDefer1 = UM.utils.defer(test,1000,true);
  297. * testDefer1(); => //本次不执行
  298. * testDefer1(); => "延迟输出!";
  299. */
  300. defer:function (fn, delay, exclusion) {
  301. var timerID;
  302. return function () {
  303. if (exclusion) {
  304. clearTimeout(timerID);
  305. }
  306. timerID = setTimeout(fn, delay);
  307. };
  308. },
  309. /**
  310. * 查找元素item在数组array中的索引, 若找不到返回-1
  311. * @name indexOf
  312. * @grammar UM.utils.indexOf(array,item) => index|-1 //默认从数组开头部开始搜索
  313. * @grammar UM.utils.indexOf(array,item,start) => index|-1 //start指定开始查找的位置
  314. */
  315. indexOf:function (array, item, start) {
  316. var index = -1;
  317. start = this.isNumber(start) ? start : 0;
  318. this.each(array, function (v, i) {
  319. if (i >= start && v === item) {
  320. index = i;
  321. return false;
  322. }
  323. });
  324. return index;
  325. },
  326. /**
  327. * 移除数组array中的元素item
  328. * @name removeItem
  329. * @grammar UM.utils.removeItem(array,item)
  330. */
  331. removeItem:function (array, item) {
  332. for (var i = 0, l = array.length; i < l; i++) {
  333. if (array[i] === item) {
  334. array.splice(i, 1);
  335. i--;
  336. }
  337. }
  338. },
  339. /**
  340. * 删除字符串str的首尾空格
  341. * @name trim
  342. * @grammar UM.utils.trim(str) => String
  343. */
  344. trim:function (str) {
  345. return str.replace(/(^[ \t\n\r]+)|([ \t\n\r]+$)/g, '');
  346. },
  347. /**
  348. * 将字符串list(以','分隔)或者数组list转成哈希对象
  349. * @name listToMap
  350. * @grammar UM.utils.listToMap(list) => Object //Object形如{test:1,br:1,textarea:1}
  351. */
  352. listToMap:function (list) {
  353. if (!list)return {};
  354. list = utils.isArray(list) ? list : list.split(',');
  355. for (var i = 0, ci, obj = {}; ci = list[i++];) {
  356. obj[ci.toUpperCase()] = obj[ci] = 1;
  357. }
  358. return obj;
  359. },
  360. /**
  361. * 将str中的html符号转义,默认将转义''&<">''四个字符,可自定义reg来确定需要转义的字符
  362. * @name unhtml
  363. * @grammar UM.utils.unhtml(str); => String
  364. * @grammar UM.utils.unhtml(str,reg) => String
  365. * @example
  366. * var html = '<body>You say:"你好!Baidu & UEditor!"</body>';
  367. * UM.utils.unhtml(html); ==> &lt;body&gt;You say:&quot;你好!Baidu &amp; UEditor!&quot;&lt;/body&gt;
  368. * UM.utils.unhtml(html,/[<>]/g) ==> &lt;body&gt;You say:"你好!Baidu & UEditor!"&lt;/body&gt;
  369. */
  370. unhtml:function (str, reg) {
  371. return str ? str.replace(reg || /[&<">'](?:(amp|lt|quot|gt|#39|nbsp);)?/g, function (a, b) {
  372. if (b) {
  373. return a;
  374. } else {
  375. return {
  376. '<':'&lt;',
  377. '&':'&amp;',
  378. '"':'&quot;',
  379. '>':'&gt;',
  380. "'":'&#39;'
  381. }[a]
  382. }
  383. }) : '';
  384. },
  385. /**
  386. * 将str中的转义字符还原成html字符
  387. * @name html
  388. * @grammar UM.utils.html(str) => String //详细参见<code><a href = '#unhtml'>unhtml</a></code>
  389. */
  390. html:function (str) {
  391. return str ? str.replace(/&((g|l|quo)t|amp|#39);/g, function (m) {
  392. return {
  393. '&lt;':'<',
  394. '&amp;':'&',
  395. '&quot;':'"',
  396. '&gt;':'>',
  397. '&#39;':"'"
  398. }[m]
  399. }) : '';
  400. },
  401. /**
  402. * 将css样式转换为驼峰的形式。如font-size => fontSize
  403. * @name cssStyleToDomStyle
  404. * @grammar UM.utils.cssStyleToDomStyle(cssName) => String
  405. */
  406. cssStyleToDomStyle:function () {
  407. var test = document.createElement('div').style,
  408. cache = {
  409. 'float':test.cssFloat != undefined ? 'cssFloat' : test.styleFloat != undefined ? 'styleFloat' : 'float'
  410. };
  411. return function (cssName) {
  412. return cache[cssName] || (cache[cssName] = cssName.toLowerCase().replace(/-./g, function (match) {
  413. return match.charAt(1).toUpperCase();
  414. }));
  415. };
  416. }(),
  417. /**
  418. * 动态加载文件到doc中,并依据obj来设置属性,加载成功后执行回调函数fn
  419. * @name loadFile
  420. * @grammar UM.utils.loadFile(doc,obj)
  421. * @grammar UM.utils.loadFile(doc,obj,fn)
  422. * @example
  423. * //指定加载到当前document中一个script文件,加载成功后执行function
  424. * utils.loadFile( document, {
  425. * src:"test.js",
  426. * tag:"script",
  427. * type:"text/javascript",
  428. * defer:"defer"
  429. * }, function () {
  430. * console.log('加载成功!')
  431. * });
  432. */
  433. loadFile:function () {
  434. var tmpList = [];
  435. function getItem(doc, obj) {
  436. try {
  437. for (var i = 0, ci; ci = tmpList[i++];) {
  438. if (ci.doc === doc && ci.url == (obj.src || obj.href)) {
  439. return ci;
  440. }
  441. }
  442. } catch (e) {
  443. return null;
  444. }
  445. }
  446. return function (doc, obj, fn) {
  447. var item = getItem(doc, obj);
  448. if (item) {
  449. if (item.ready) {
  450. fn && fn();
  451. } else {
  452. item.funs.push(fn)
  453. }
  454. return;
  455. }
  456. tmpList.push({
  457. doc:doc,
  458. url:obj.src || obj.href,
  459. funs:[fn]
  460. });
  461. if (!doc.body) {
  462. var html = [];
  463. for (var p in obj) {
  464. if (p == 'tag')continue;
  465. html.push(p + '="' + obj[p] + '"')
  466. }
  467. doc.write('<' + obj.tag + ' ' + html.join(' ') + ' ></' + obj.tag + '>');
  468. return;
  469. }
  470. if (obj.id && doc.getElementById(obj.id)) {
  471. return;
  472. }
  473. var element = doc.createElement(obj.tag);
  474. delete obj.tag;
  475. for (var p in obj) {
  476. element.setAttribute(p, obj[p]);
  477. }
  478. element.onload = element.onreadystatechange = function () {
  479. if (!this.readyState || /loaded|complete/.test(this.readyState)) {
  480. item = getItem(doc, obj);
  481. if (item.funs.length > 0) {
  482. item.ready = 1;
  483. for (var fi; fi = item.funs.pop();) {
  484. fi();
  485. }
  486. }
  487. element.onload = element.onreadystatechange = null;
  488. }
  489. };
  490. element.onerror = function () {
  491. throw Error('The load ' + (obj.href || obj.src) + ' fails,check the url settings of file umeditor.config.js ')
  492. };
  493. doc.getElementsByTagName("head")[0].appendChild(element);
  494. }
  495. }(),
  496. /**
  497. * 判断obj对象是否为空
  498. * @name isEmptyObject
  499. * @grammar UM.utils.isEmptyObject(obj) => true|false
  500. * @example
  501. * UM.utils.isEmptyObject({}) ==>true
  502. * UM.utils.isEmptyObject([]) ==>true
  503. * UM.utils.isEmptyObject("") ==>true
  504. */
  505. isEmptyObject:function (obj) {
  506. if (obj == null) return true;
  507. if (this.isArray(obj) || this.isString(obj)) return obj.length === 0;
  508. for (var key in obj) if (obj.hasOwnProperty(key)) return false;
  509. return true;
  510. },
  511. /**
  512. * 统一将颜色值使用16进制形式表示
  513. * @name fixColor
  514. * @grammar UM.utils.fixColor(name,value) => value
  515. * @example
  516. * rgb(255,255,255) => "#ffffff"
  517. */
  518. fixColor:function (name, value) {
  519. if (/color/i.test(name) && /rgba?/.test(value)) {
  520. var array = value.split(",");
  521. if (array.length > 3)
  522. return "";
  523. value = "#";
  524. for (var i = 0, color; color = array[i++];) {
  525. color = parseInt(color.replace(/[^\d]/gi, ''), 10).toString(16);
  526. value += color.length == 1 ? "0" + color : color;
  527. }
  528. value = value.toUpperCase();
  529. }
  530. return value;
  531. },
  532. /**
  533. * 深度克隆对象,从source到target
  534. * @name clone
  535. * @grammar UM.utils.clone(source) => anthorObj 新的对象是完整的source的副本
  536. * @grammar UM.utils.clone(source,target) => target包含了source的所有内容,重名会覆盖
  537. */
  538. clone:function (source, target) {
  539. var tmp;
  540. target = target || {};
  541. for (var i in source) {
  542. if (source.hasOwnProperty(i)) {
  543. tmp = source[i];
  544. if (typeof tmp == 'object') {
  545. target[i] = utils.isArray(tmp) ? [] : {};
  546. utils.clone(source[i], target[i])
  547. } else {
  548. target[i] = tmp;
  549. }
  550. }
  551. }
  552. return target;
  553. },
  554. /**
  555. * 转换cm/pt到px
  556. * @name transUnitToPx
  557. * @grammar UM.utils.transUnitToPx('20pt') => '27px'
  558. * @grammar UM.utils.transUnitToPx('0pt') => '0'
  559. */
  560. transUnitToPx:function (val) {
  561. if (!/(pt|cm)/.test(val)) {
  562. return val
  563. }
  564. var unit;
  565. val.replace(/([\d.]+)(\w+)/, function (str, v, u) {
  566. val = v;
  567. unit = u;
  568. });
  569. switch (unit) {
  570. case 'cm':
  571. val = parseFloat(val) * 25;
  572. break;
  573. case 'pt':
  574. val = Math.round(parseFloat(val) * 96 / 72);
  575. }
  576. return val + (val ? 'px' : '');
  577. },
  578. /**
  579. * DomReady方法,回调函数将在dom树ready完成后执行
  580. * @name domReady
  581. * @grammar UM.utils.domReady(fn) => fn //返回一个延迟执行的方法
  582. */
  583. domReady:function () {
  584. var fnArr = [];
  585. function doReady(doc) {
  586. //确保onready只执行一次
  587. doc.isReady = true;
  588. for (var ci; ci = fnArr.pop(); ci()) {
  589. }
  590. }
  591. return function (onready, win) {
  592. win = win || window;
  593. var doc = win.document;
  594. onready && fnArr.push(onready);
  595. if (doc.readyState === "complete") {
  596. doReady(doc);
  597. } else {
  598. doc.isReady && doReady(doc);
  599. if (browser.ie) {
  600. (function () {
  601. if (doc.isReady) return;
  602. try {
  603. doc.documentElement.doScroll("left");
  604. } catch (error) {
  605. setTimeout(arguments.callee, 0);
  606. return;
  607. }
  608. doReady(doc);
  609. })();
  610. win.attachEvent('onload', function () {
  611. doReady(doc)
  612. });
  613. } else {
  614. doc.addEventListener("DOMContentLoaded", function () {
  615. doc.removeEventListener("DOMContentLoaded", arguments.callee, false);
  616. doReady(doc);
  617. }, false);
  618. win.addEventListener('load', function () {
  619. doReady(doc)
  620. }, false);
  621. }
  622. }
  623. }
  624. }(),
  625. /**
  626. * 动态添加css样式
  627. * @name cssRule
  628. * @grammar UM.utils.cssRule('添加的样式的节点名称',['样式','放到哪个document上'])
  629. * @grammar UM.utils.cssRule('body','body{background:#ccc}') => null //给body添加背景颜色
  630. * @grammar UM.utils.cssRule('body') =>样式的字符串 //取得key值为body的样式的内容,如果没有找到key值先关的样式将返回空,例如刚才那个背景颜色,将返回 body{background:#ccc}
  631. * @grammar UM.utils.cssRule('body','') =>null //清空给定的key值的背景颜色
  632. */
  633. cssRule:browser.ie ? function (key, style, doc) {
  634. var indexList, index;
  635. doc = doc || document;
  636. if (doc.indexList) {
  637. indexList = doc.indexList;
  638. } else {
  639. indexList = doc.indexList = {};
  640. }
  641. var sheetStyle;
  642. if (!indexList[key]) {
  643. if (style === undefined) {
  644. return ''
  645. }
  646. sheetStyle = doc.createStyleSheet('', index = doc.styleSheets.length);
  647. indexList[key] = index;
  648. } else {
  649. sheetStyle = doc.styleSheets[indexList[key]];
  650. }
  651. if (style === undefined) {
  652. return sheetStyle.cssText
  653. }
  654. sheetStyle.cssText = style || ''
  655. } : function (key, style, doc) {
  656. doc = doc || document;
  657. var head = doc.getElementsByTagName('head')[0], node;
  658. if (!(node = doc.getElementById(key))) {
  659. if (style === undefined) {
  660. return ''
  661. }
  662. node = doc.createElement('style');
  663. node.id = key;
  664. head.appendChild(node)
  665. }
  666. if (style === undefined) {
  667. return node.innerHTML
  668. }
  669. if (style !== '') {
  670. node.innerHTML = style;
  671. } else {
  672. head.removeChild(node)
  673. }
  674. }
  675. };
  676. /**
  677. * 判断str是否为字符串
  678. * @name isString
  679. * @grammar UM.utils.isString(str) => true|false
  680. */
  681. /**
  682. * 判断array是否为数组
  683. * @name isArray
  684. * @grammar UM.utils.isArray(obj) => true|false
  685. */
  686. /**
  687. * 判断obj对象是否为方法
  688. * @name isFunction
  689. * @grammar UM.utils.isFunction(obj) => true|false
  690. */
  691. /**
  692. * 判断obj对象是否为数字
  693. * @name isNumber
  694. * @grammar UM.utils.isNumber(obj) => true|false
  695. */
  696. utils.each(['String', 'Function', 'Array', 'Number', 'RegExp', 'Object'], function (v) {
  697. UM.utils['is' + v] = function (obj) {
  698. return Object.prototype.toString.apply(obj) == '[object ' + v + ']';
  699. }
  700. });
  701. /**
  702. * @file
  703. * @name UM.EventBase
  704. * @short EventBase
  705. * @import editor.js,core/utils.js
  706. * @desc UE采用的事件基类,继承此类的对应类将获取addListener,removeListener,fireEvent方法。
  707. * 在UE中,Editor以及所有ui实例都继承了该类,故可以在对应的ui对象以及editor对象上使用上述方法。
  708. */
  709. var EventBase = UM.EventBase = function () {};
  710. EventBase.prototype = {
  711. /**
  712. * 注册事件监听器
  713. * @name addListener
  714. * @grammar editor.addListener(types,fn) //types为事件名称,多个可用空格分隔
  715. * @example
  716. * editor.addListener('selectionchange',function(){
  717. * console.log("选区已经变化!");
  718. * })
  719. * editor.addListener('beforegetcontent aftergetcontent',function(type){
  720. * if(type == 'beforegetcontent'){
  721. * //do something
  722. * }else{
  723. * //do something
  724. * }
  725. * console.log(this.getContent) // this是注册的事件的编辑器实例
  726. * })
  727. */
  728. addListener:function (types, listener) {
  729. types = utils.trim(types).split(' ');
  730. for (var i = 0, ti; ti = types[i++];) {
  731. getListener(this, ti, true).push(listener);
  732. }
  733. },
  734. /**
  735. * 移除事件监听器
  736. * @name removeListener
  737. * @grammar editor.removeListener(types,fn) //types为事件名称,多个可用空格分隔
  738. * @example
  739. * //changeCallback为方法体
  740. * editor.removeListener("selectionchange",changeCallback);
  741. */
  742. removeListener:function (types, listener) {
  743. types = utils.trim(types).split(' ');
  744. for (var i = 0, ti; ti = types[i++];) {
  745. utils.removeItem(getListener(this, ti) || [], listener);
  746. }
  747. },
  748. /**
  749. * 触发事件
  750. * @name fireEvent
  751. * @grammar editor.fireEvent(types) //types为事件名称,多个可用空格分隔
  752. * @example
  753. * editor.fireEvent("selectionchange");
  754. */
  755. fireEvent:function () {
  756. var types = arguments[0];
  757. types = utils.trim(types).split(' ');
  758. for (var i = 0, ti; ti = types[i++];) {
  759. var listeners = getListener(this, ti),
  760. r, t, k;
  761. if (listeners) {
  762. k = listeners.length;
  763. while (k--) {
  764. if(!listeners[k])continue;
  765. t = listeners[k].apply(this, arguments);
  766. if(t === true){
  767. return t;
  768. }
  769. if (t !== undefined) {
  770. r = t;
  771. }
  772. }
  773. }
  774. if (t = this['on' + ti.toLowerCase()]) {
  775. r = t.apply(this, arguments);
  776. }
  777. }
  778. return r;
  779. }
  780. };
  781. /**
  782. * 获得对象所拥有监听类型的所有监听器
  783. * @public
  784. * @function
  785. * @param {Object} obj 查询监听器的对象
  786. * @param {String} type 事件类型
  787. * @param {Boolean} force 为true且当前所有type类型的侦听器不存在时,创建一个空监听器数组
  788. * @returns {Array} 监听器数组
  789. */
  790. function getListener(obj, type, force) {
  791. var allListeners;
  792. type = type.toLowerCase();
  793. return ( ( allListeners = ( obj.__allListeners || force && ( obj.__allListeners = {} ) ) )
  794. && ( allListeners[type] || force && ( allListeners[type] = [] ) ) );
  795. }
  796. ///import editor.js
  797. ///import core/dom/dom.js
  798. ///import core/utils.js
  799. /**
  800. * dtd html语义化的体现类
  801. * @constructor
  802. * @namespace dtd
  803. */
  804. var dtd = dom.dtd = (function() {
  805. function _( s ) {
  806. for (var k in s) {
  807. s[k.toUpperCase()] = s[k];
  808. }
  809. return s;
  810. }
  811. var X = utils.extend2;
  812. var A = _({isindex:1,fieldset:1}),
  813. B = _({input:1,button:1,select:1,textarea:1,label:1}),
  814. C = X( _({a:1}), B ),
  815. D = X( {iframe:1}, C ),
  816. E = _({hr:1,ul:1,menu:1,div:1,blockquote:1,noscript:1,table:1,center:1,address:1,dir:1,pre:1,h5:1,dl:1,h4:1,noframes:1,h6:1,ol:1,h1:1,h3:1,h2:1}),
  817. F = _({ins:1,del:1,script:1,style:1}),
  818. G = X( _({b:1,acronym:1,bdo:1,'var':1,'#':1,abbr:1,code:1,br:1,i:1,cite:1,kbd:1,u:1,strike:1,s:1,tt:1,strong:1,q:1,samp:1,em:1,dfn:1,span:1}), F ),
  819. H = X( _({sub:1,img:1,embed:1,object:1,sup:1,basefont:1,map:1,applet:1,font:1,big:1,small:1}), G ),
  820. I = X( _({p:1}), H ),
  821. J = X( _({iframe:1}), H, B ),
  822. K = _({img:1,embed:1,noscript:1,br:1,kbd:1,center:1,button:1,basefont:1,h5:1,h4:1,samp:1,h6:1,ol:1,h1:1,h3:1,h2:1,form:1,font:1,'#':1,select:1,menu:1,ins:1,abbr:1,label:1,code:1,table:1,script:1,cite:1,input:1,iframe:1,strong:1,textarea:1,noframes:1,big:1,small:1,span:1,hr:1,sub:1,bdo:1,'var':1,div:1,object:1,sup:1,strike:1,dir:1,map:1,dl:1,applet:1,del:1,isindex:1,fieldset:1,ul:1,b:1,acronym:1,a:1,blockquote:1,i:1,u:1,s:1,tt:1,address:1,q:1,pre:1,p:1,em:1,dfn:1}),
  823. L = X( _({a:0}), J ),//a不能被切开,所以把他
  824. M = _({tr:1}),
  825. N = _({'#':1}),
  826. O = X( _({param:1}), K ),
  827. P = X( _({form:1}), A, D, E, I ),
  828. Q = _({li:1,ol:1,ul:1}),
  829. R = _({style:1,script:1}),
  830. S = _({base:1,link:1,meta:1,title:1}),
  831. T = X( S, R ),
  832. U = _({head:1,body:1}),
  833. V = _({html:1});
  834. var block = _({address:1,blockquote:1,center:1,dir:1,div:1,dl:1,fieldset:1,form:1,h1:1,h2:1,h3:1,h4:1,h5:1,h6:1,hr:1,isindex:1,menu:1,noframes:1,ol:1,p:1,pre:1,table:1,ul:1}),
  835. empty = _({area:1,base:1,basefont:1,br:1,col:1,command:1,dialog:1,embed:1,hr:1,img:1,input:1,isindex:1,keygen:1,link:1,meta:1,param:1,source:1,track:1,wbr:1});
  836. return _({
  837. // $ 表示自定的属性
  838. // body外的元素列表.
  839. $nonBodyContent: X( V, U, S ),
  840. //块结构元素列表
  841. $block : block,
  842. //内联元素列表
  843. $inline : L,
  844. $inlineWithA : X(_({a:1}),L),
  845. $body : X( _({script:1,style:1}), block ),
  846. $cdata : _({script:1,style:1}),
  847. //自闭和元素
  848. $empty : empty,
  849. //不是自闭合,但不能让range选中里边
  850. $nonChild : _({iframe:1,textarea:1}),
  851. //列表元素列表
  852. $listItem : _({dd:1,dt:1,li:1}),
  853. //列表根元素列表
  854. $list: _({ul:1,ol:1,dl:1}),
  855. //不能认为是空的元素
  856. $isNotEmpty : _({table:1,ul:1,ol:1,dl:1,iframe:1,area:1,base:1,col:1,hr:1,img:1,embed:1,input:1,link:1,meta:1,param:1,h1:1,h2:1,h3:1,h4:1,h5:1,h6:1}),
  857. //如果没有子节点就可以删除的元素列表,像span,a
  858. $removeEmpty : _({a:1,abbr:1,acronym:1,address:1,b:1,bdo:1,big:1,cite:1,code:1,del:1,dfn:1,em:1,font:1,i:1,ins:1,label:1,kbd:1,q:1,s:1,samp:1,small:1,span:1,strike:1,strong:1,sub:1,sup:1,tt:1,u:1,'var':1}),
  859. $removeEmptyBlock : _({'p':1,'div':1}),
  860. //在table元素里的元素列表
  861. $tableContent : _({caption:1,col:1,colgroup:1,tbody:1,td:1,tfoot:1,th:1,thead:1,tr:1,table:1}),
  862. //不转换的标签
  863. $notTransContent : _({pre:1,script:1,style:1,textarea:1}),
  864. html: U,
  865. head: T,
  866. style: N,
  867. script: N,
  868. body: P,
  869. base: {},
  870. link: {},
  871. meta: {},
  872. title: N,
  873. col : {},
  874. tr : _({td:1,th:1}),
  875. img : {},
  876. embed: {},
  877. colgroup : _({thead:1,col:1,tbody:1,tr:1,tfoot:1}),
  878. noscript : P,
  879. td : P,
  880. br : {},
  881. th : P,
  882. center : P,
  883. kbd : L,
  884. button : X( I, E ),
  885. basefont : {},
  886. h5 : L,
  887. h4 : L,
  888. samp : L,
  889. h6 : L,
  890. ol : Q,
  891. h1 : L,
  892. h3 : L,
  893. option : N,
  894. h2 : L,
  895. form : X( A, D, E, I ),
  896. select : _({optgroup:1,option:1}),
  897. font : L,
  898. ins : L,
  899. menu : Q,
  900. abbr : L,
  901. label : L,
  902. table : _({thead:1,col:1,tbody:1,tr:1,colgroup:1,caption:1,tfoot:1}),
  903. code : L,
  904. tfoot : M,
  905. cite : L,
  906. li : P,
  907. input : {},
  908. iframe : P,
  909. strong : L,
  910. textarea : N,
  911. noframes : P,
  912. big : L,
  913. small : L,
  914. //trace:
  915. span :_({'#':1,br:1,b:1,strong:1,u:1,i:1,em:1,sub:1,sup:1,strike:1,span:1}),
  916. hr : L,
  917. dt : L,
  918. sub : L,
  919. optgroup : _({option:1}),
  920. param : {},
  921. bdo : L,
  922. 'var' : L,
  923. div : P,
  924. object : O,
  925. sup : L,
  926. dd : P,
  927. strike : L,
  928. area : {},
  929. dir : Q,
  930. map : X( _({area:1,form:1,p:1}), A, F, E ),
  931. applet : O,
  932. dl : _({dt:1,dd:1}),
  933. del : L,
  934. isindex : {},
  935. fieldset : X( _({legend:1}), K ),
  936. thead : M,
  937. ul : Q,
  938. acronym : L,
  939. b : L,
  940. a : X( _({a:1}), J ),
  941. blockquote :X(_({td:1,tr:1,tbody:1,li:1}),P),
  942. caption : L,
  943. i : L,
  944. u : L,
  945. tbody : M,
  946. s : L,
  947. address : X( D, I ),
  948. tt : L,
  949. legend : L,
  950. q : L,
  951. pre : X( G, C ),
  952. p : X(_({'a':1}),L),
  953. em :L,
  954. dfn : L
  955. });
  956. })();
  957. /**
  958. * @file
  959. * @name UM.dom.domUtils
  960. * @short DomUtils
  961. * @import editor.js, core/utils.js,core/browser.js,core/dom/dtd.js
  962. * @desc UEditor封装的底层dom操作库
  963. */
  964. var attrFix = ie && browser.version < 9 ? {
  965. tabindex: "tabIndex",
  966. readonly: "readOnly",
  967. "for": "htmlFor",
  968. "class": "className",
  969. maxlength: "maxLength",
  970. cellspacing: "cellSpacing",
  971. cellpadding: "cellPadding",
  972. rowspan: "rowSpan",
  973. colspan: "colSpan",
  974. usemap: "useMap",
  975. frameborder: "frameBorder"
  976. } : {
  977. tabindex: "tabIndex",
  978. readonly: "readOnly"
  979. },
  980. styleBlock = utils.listToMap([
  981. '-webkit-box', '-moz-box', 'block' ,
  982. 'list-item' , 'table' , 'table-row-group' ,
  983. 'table-header-group', 'table-footer-group' ,
  984. 'table-row' , 'table-column-group' , 'table-column' ,
  985. 'table-cell' , 'table-caption'
  986. ]);
  987. var domUtils = dom.domUtils = {
  988. //节点常量
  989. NODE_ELEMENT: 1,
  990. NODE_DOCUMENT: 9,
  991. NODE_TEXT: 3,
  992. NODE_COMMENT: 8,
  993. NODE_DOCUMENT_FRAGMENT: 11,
  994. //位置关系
  995. POSITION_IDENTICAL: 0,
  996. POSITION_DISCONNECTED: 1,
  997. POSITION_FOLLOWING: 2,
  998. POSITION_PRECEDING: 4,
  999. POSITION_IS_CONTAINED: 8,
  1000. POSITION_CONTAINS: 16,
  1001. //ie6使用其他的会有一段空白出现
  1002. fillChar: ie && browser.version == '6' ? '\ufeff' : '\u200B',
  1003. //-------------------------Node部分--------------------------------
  1004. keys: {
  1005. /*Backspace*/ 8: 1, /*Delete*/ 46: 1,
  1006. /*Shift*/ 16: 1, /*Ctrl*/ 17: 1, /*Alt*/ 18: 1,
  1007. 37: 1, 38: 1, 39: 1, 40: 1,
  1008. 13: 1 /*enter*/
  1009. },
  1010. breakParent:function (node, parent) {
  1011. var tmpNode,
  1012. parentClone = node,
  1013. clone = node,
  1014. leftNodes,
  1015. rightNodes;
  1016. do {
  1017. parentClone = parentClone.parentNode;
  1018. if (leftNodes) {
  1019. tmpNode = parentClone.cloneNode(false);
  1020. tmpNode.appendChild(leftNodes);
  1021. leftNodes = tmpNode;
  1022. tmpNode = parentClone.cloneNode(false);
  1023. tmpNode.appendChild(rightNodes);
  1024. rightNodes = tmpNode;
  1025. } else {
  1026. leftNodes = parentClone.cloneNode(false);
  1027. rightNodes = leftNodes.cloneNode(false);
  1028. }
  1029. while (tmpNode = clone.previousSibling) {
  1030. leftNodes.insertBefore(tmpNode, leftNodes.firstChild);
  1031. }
  1032. while (tmpNode = clone.nextSibling) {
  1033. rightNodes.appendChild(tmpNode);
  1034. }
  1035. clone = parentClone;
  1036. } while (parent !== parentClone);
  1037. tmpNode = parent.parentNode;
  1038. tmpNode.insertBefore(leftNodes, parent);
  1039. tmpNode.insertBefore(rightNodes, parent);
  1040. tmpNode.insertBefore(node, rightNodes);
  1041. domUtils.remove(parent);
  1042. return node;
  1043. },
  1044. trimWhiteTextNode:function (node) {
  1045. function remove(dir) {
  1046. var child;
  1047. while ((child = node[dir]) && child.nodeType == 3 && domUtils.isWhitespace(child)) {
  1048. node.removeChild(child);
  1049. }
  1050. }
  1051. remove('firstChild');
  1052. remove('lastChild');
  1053. },
  1054. /**
  1055. * 获取节点A相对于节点B的位置关系
  1056. * @name getPosition
  1057. * @grammar UM.dom.domUtils.getPosition(nodeA,nodeB) => Number
  1058. * @example
  1059. * switch (returnValue) {
  1060. * case 0: //相等,同一节点
  1061. * case 1: //无关,节点不相连
  1062. * case 2: //跟随,即节点A头部位于节点B头部的后面
  1063. * case 4: //前置,即节点A头部位于节点B头部的前面
  1064. * case 8: //被包含,即节点A被节点B包含
  1065. * case 10://组合类型,即节点A满足跟随节点B且被节点B包含。实际上,如果被包含,必定跟随,所以returnValue事实上不会存在8的情况。
  1066. * case 16://包含,即节点A包含节点B
  1067. * case 20://组合类型,即节点A满足前置节点A且包含节点B。同样,如果包含,必定前置,所以returnValue事实上也不会存在16的情况
  1068. * }
  1069. */
  1070. getPosition: function (nodeA, nodeB) {
  1071. // 如果两个节点是同一个节点
  1072. if (nodeA === nodeB) {
  1073. // domUtils.POSITION_IDENTICAL
  1074. return 0;
  1075. }
  1076. var node,
  1077. parentsA = [nodeA],
  1078. parentsB = [nodeB];
  1079. node = nodeA;
  1080. while (node = node.parentNode) {
  1081. // 如果nodeB是nodeA的祖先节点
  1082. if (node === nodeB) {
  1083. // domUtils.POSITION_IS_CONTAINED + domUtils.POSITION_FOLLOWING
  1084. return 10;
  1085. }
  1086. parentsA.push(node);
  1087. }
  1088. node = nodeB;
  1089. while (node = node.parentNode) {
  1090. // 如果nodeA是nodeB的祖先节点
  1091. if (node === nodeA) {
  1092. // domUtils.POSITION_CONTAINS + domUtils.POSITION_PRECEDING
  1093. return 20;
  1094. }
  1095. parentsB.push(node);
  1096. }
  1097. parentsA.reverse();
  1098. parentsB.reverse();
  1099. if (parentsA[0] !== parentsB[0]) {
  1100. // domUtils.POSITION_DISCONNECTED
  1101. return 1;
  1102. }
  1103. var i = -1;
  1104. while (i++, parentsA[i] === parentsB[i]) {
  1105. }
  1106. nodeA = parentsA[i];
  1107. nodeB = parentsB[i];
  1108. while (nodeA = nodeA.nextSibling) {
  1109. if (nodeA === nodeB) {
  1110. // domUtils.POSITION_PRECEDING
  1111. return 4
  1112. }
  1113. }
  1114. // domUtils.POSITION_FOLLOWING
  1115. return 2;
  1116. },
  1117. /**
  1118. * 返回节点node在父节点中的索引位置
  1119. * @name getNodeIndex
  1120. * @grammar UM.dom.domUtils.getNodeIndex(node) => Number //索引值从0开始
  1121. */
  1122. getNodeIndex: function (node, ignoreTextNode) {
  1123. var preNode = node,
  1124. i = 0;
  1125. while (preNode = preNode.previousSibling) {
  1126. if (ignoreTextNode && preNode.nodeType == 3) {
  1127. if (preNode.nodeType != preNode.nextSibling.nodeType) {
  1128. i++;
  1129. }
  1130. continue;
  1131. }
  1132. i++;
  1133. }
  1134. return i;
  1135. },
  1136. /**
  1137. * 检测节点node是否在节点doc的树上,实质上是检测是否被doc包含
  1138. * @name inDoc
  1139. * @grammar UM.dom.domUtils.inDoc(node,doc) => true|false
  1140. */
  1141. inDoc: function (node, doc) {
  1142. return domUtils.getPosition(node, doc) == 10;
  1143. },
  1144. /**
  1145. * 查找node节点的祖先节点
  1146. * @name findParent
  1147. * @grammar UM.dom.domUtils.findParent(node) => Element // 直接返回node节点的父节点
  1148. * @grammar UM.dom.domUtils.findParent(node,filterFn) => Element //filterFn为过滤函数,node作为参数,返回true时才会将node作为符合要求的节点返回
  1149. * @grammar UM.dom.domUtils.findParent(node,filterFn,includeSelf) => Element //includeSelf指定是否包含自身
  1150. */
  1151. findParent: function (node, filterFn, includeSelf) {
  1152. if (node && !domUtils.isBody(node)) {
  1153. node = includeSelf ? node : node.parentNode;
  1154. while (node) {
  1155. if (!filterFn || filterFn(node) || domUtils.isBody(node)) {
  1156. return filterFn && !filterFn(node) && domUtils.isBody(node) ? null : node;
  1157. }
  1158. node = node.parentNode;
  1159. }
  1160. }
  1161. return null;
  1162. },
  1163. /**
  1164. * 通过tagName查找node节点的祖先节点
  1165. * @name findParentByTagName
  1166. * @grammar UM.dom.domUtils.findParentByTagName(node,tagNames) => Element //tagNames支持数组,区分大小写
  1167. * @grammar UM.dom.domUtils.findParentByTagName(node,tagNames,includeSelf) => Element //includeSelf指定是否包含自身
  1168. * @grammar UM.dom.domUtils.findParentByTagName(node,tagNames,includeSelf,excludeFn) => Element //excludeFn指定例外过滤条件,返回true时忽略该节点
  1169. */
  1170. findParentByTagName: function (node, tagNames, includeSelf, excludeFn) {
  1171. tagNames = utils.listToMap(utils.isArray(tagNames) ? tagNames : [tagNames]);
  1172. return domUtils.findParent(node, function (node) {
  1173. return tagNames[node.tagName] && !(excludeFn && excludeFn(node));
  1174. }, includeSelf);
  1175. },
  1176. /**
  1177. * 查找节点node的祖先节点集合
  1178. * @name findParents
  1179. * @grammar UM.dom.domUtils.findParents(node) => Array //返回一个祖先节点数组集合,不包含自身
  1180. * @grammar UM.dom.domUtils.findParents(node,includeSelf) => Array //返回一个祖先节点数组集合,includeSelf指定是否包含自身
  1181. * @grammar UM.dom.domUtils.findParents(node,includeSelf,filterFn) => Array //返回一个祖先节点数组集合,filterFn指定过滤条件,返回true的node将被选取
  1182. * @grammar UM.dom.domUtils.findParents(node,includeSelf,filterFn,closerFirst) => Array //返回一个祖先节点数组集合,closerFirst为true的话,node的直接父亲节点是数组的第0个
  1183. */
  1184. findParents: function (node, includeSelf, filterFn, closerFirst) {
  1185. var parents = includeSelf && ( filterFn && filterFn(node) || !filterFn ) ? [node] : [];
  1186. while (node = domUtils.findParent(node, filterFn)) {
  1187. parents.push(node);
  1188. }
  1189. return closerFirst ? parents : parents.reverse();
  1190. },
  1191. /**
  1192. * 在节点node后面插入新节点newNode
  1193. * @name insertAfter
  1194. * @grammar UM.dom.domUtils.insertAfter(node,newNode) => newNode
  1195. */
  1196. insertAfter: function (node, newNode) {
  1197. return node.parentNode.insertBefore(newNode, node.nextSibling);
  1198. },
  1199. /**
  1200. * 删除节点node,并根据keepChildren指定是否保留子节点
  1201. * @name remove
  1202. * @grammar UM.dom.domUtils.remove(node) => node
  1203. * @grammar UM.dom.domUtils.remove(node,keepChildren) => node
  1204. */
  1205. remove: function (node, keepChildren) {
  1206. var parent = node.parentNode,
  1207. child;
  1208. if (parent) {
  1209. if (keepChildren && node.hasChildNodes()) {
  1210. while (child = node.firstChild) {
  1211. parent.insertBefore(child, node);
  1212. }
  1213. }
  1214. parent.removeChild(node);
  1215. }
  1216. return node;
  1217. },
  1218. /**
  1219. * 检测节点node是否属于bookmark节点
  1220. * @name isBookmarkNode
  1221. * @grammar UM.dom.domUtils.isBookmarkNode(node) => true|false
  1222. */
  1223. isBookmarkNode: function (node) {
  1224. return node.nodeType == 1 && node.id && /^_baidu_bookmark_/i.test(node.id);
  1225. },
  1226. /**
  1227. * 获取节点node所在的window对象
  1228. * @name getWindow
  1229. * @grammar UM.dom.domUtils.getWindow(node) => window对象
  1230. */
  1231. getWindow: function (node) {
  1232. var doc = node.ownerDocument || node;
  1233. return doc.defaultView || doc.parentWindow;
  1234. },
  1235. /**
  1236. * 将一个文本节点node拆分成两个文本节点,offset指定拆分位置
  1237. * @name split
  1238. * @grammar UM.dom.domUtils.split(node,offset) => TextNode //返回从切分位置开始的后一个文本节点
  1239. */
  1240. split: function (node, offset) {
  1241. var doc = node.ownerDocument;
  1242. if (browser.ie && offset == node.nodeValue.length) {
  1243. var next = doc.createTextNode('');
  1244. return domUtils.insertAfter(node, next);
  1245. }
  1246. var retval = node.splitText(offset);
  1247. //ie8下splitText不会跟新childNodes,我们手动触发他的更新
  1248. if (browser.ie8) {
  1249. var tmpNode = doc.createTextNode('');
  1250. domUtils.insertAfter(retval, tmpNode);
  1251. domUtils.remove(tmpNode);
  1252. }
  1253. return retval;
  1254. },
  1255. /**
  1256. * 检测节点node是否为空节点(包括空格、换行、占位符等字符)
  1257. * @name isWhitespace
  1258. * @grammar UM.dom.domUtils.isWhitespace(node) => true|false
  1259. */
  1260. isWhitespace: function (node) {
  1261. return !new RegExp('[^ \t\n\r' + domUtils.fillChar + ']').test(node.nodeValue);
  1262. },
  1263. /**
  1264. * 获取元素element相对于viewport的位置坐标
  1265. * @name getXY
  1266. * @grammar UM.dom.domUtils.getXY(element) => Object //返回坐标对象{x:left,y:top}
  1267. */
  1268. getXY: function (element) {
  1269. var x = 0, y = 0;
  1270. while (element.offsetParent) {
  1271. y += element.offsetTop;
  1272. x += element.offsetLeft;
  1273. element = element.offsetParent;
  1274. }
  1275. return { 'x': x, 'y': y};
  1276. },
  1277. /**
  1278. * 为元素element绑定原生DOM事件,type为事件类型,handler为处理函数
  1279. * @name on
  1280. * @grammar UM.dom.domUtils.on(element,type,handler) //type支持数组传入
  1281. * @example
  1282. * UM.dom.domUtils.on(document.body,"click",function(e){
  1283. * //e为事件对象,this为被点击元素对戏那个
  1284. * })
  1285. * @example
  1286. * UM.dom.domUtils.on(document.body,["click","mousedown"],function(evt){
  1287. * //evt为事件对象,this为被点击元素对象
  1288. * })
  1289. */
  1290. on: function (element, type, handler) {
  1291. var types = utils.isArray(type) ? type : [type],
  1292. k = types.length;
  1293. if (k) while (k--) {
  1294. type = types[k];
  1295. if (element.addEventListener) {
  1296. element.addEventListener(type, handler, false);
  1297. } else {
  1298. if (!handler._d) {
  1299. handler._d = {
  1300. els: []
  1301. };
  1302. }
  1303. var key = type + handler.toString(), index = utils.indexOf(handler._d.els, element);
  1304. if (!handler._d[key] || index == -1) {
  1305. if (index == -1) {
  1306. handler._d.els.push(element);
  1307. }
  1308. if (!handler._d[key]) {
  1309. handler._d[key] = function (evt) {
  1310. return handler.call(evt.srcElement, evt || window.event);
  1311. };
  1312. }
  1313. element.attachEvent('on' + type, handler._d[key]);
  1314. }
  1315. }
  1316. }
  1317. element = null;
  1318. },
  1319. /**
  1320. * 解除原生DOM事件绑定
  1321. * @name un
  1322. * @grammar UM.dom.donUtils.un(element,type,handler) //参见<code><a href="#on">on</a></code>
  1323. */
  1324. un: function (element, type, handler) {
  1325. var types = utils.isArray(type) ? type : [type],
  1326. k = types.length;
  1327. if (k) while (k--) {
  1328. type = types[k];
  1329. if (element.removeEventListener) {
  1330. element.removeEventListener(type, handler, false);
  1331. } else {
  1332. var key = type + handler.toString();
  1333. try {
  1334. element.detachEvent('on' + type, handler._d ? handler._d[key] : handler);
  1335. } catch (e) {
  1336. }
  1337. if (handler._d && handler._d[key]) {
  1338. var index = utils.indexOf(handler._d.els, element);
  1339. if (index != -1) {
  1340. handler._d.els.splice(index, 1);
  1341. }
  1342. handler._d.els.length == 0 && delete handler._d[key];
  1343. }
  1344. }
  1345. }
  1346. },
  1347. /**
  1348. * 检查节点node是否是空inline节点
  1349. * @name isEmptyInlineElement
  1350. * @grammar UM.dom.domUtils.isEmptyInlineElement(node) => 1|0
  1351. * @example
  1352. * <b><i></i></b> => 1
  1353. * <b><i></i><u></u></b> => 1
  1354. * <b></b> => 1
  1355. * <b>xx<i></i></b> => 0
  1356. */
  1357. isEmptyInlineElement: function (node) {
  1358. if (node.nodeType != 1 || !dtd.$removeEmpty[ node.tagName ]) {
  1359. return 0;
  1360. }
  1361. node = node.firstChild;
  1362. while (node) {
  1363. //如果是创建的bookmark就跳过
  1364. if (domUtils.isBookmarkNode(node)) {
  1365. return 0;
  1366. }
  1367. if (node.nodeType == 1 && !domUtils.isEmptyInlineElement(node) ||
  1368. node.nodeType == 3 && !domUtils.isWhitespace(node)
  1369. ) {
  1370. return 0;
  1371. }
  1372. node = node.nextSibling;
  1373. }
  1374. return 1;
  1375. },
  1376. /**
  1377. * 检查节点node是否为块元素
  1378. * @name isBlockElm
  1379. * @grammar UM.dom.domUtils.isBlockElm(node) => true|false
  1380. */
  1381. isBlockElm: function (node) {
  1382. return node.nodeType == 1 && (dtd.$block[node.tagName] || styleBlock[domUtils.getComputedStyle(node, 'display')]) && !dtd.$nonChild[node.tagName];
  1383. },
  1384. /**
  1385. * 原生方法getElementsByTagName的封装
  1386. * @name getElementsByTagName
  1387. * @grammar UM.dom.domUtils.getElementsByTagName(node,tagName) => Array //节点集合数组
  1388. */
  1389. getElementsByTagName: function (node, name, filter) {
  1390. if (filter && utils.isString(filter)) {
  1391. var className = filter;
  1392. filter = function (node) {
  1393. var result = false;
  1394. $.each(utils.trim(className).replace(/[ ]{2,}/g, ' ').split(' '), function (i, v) {
  1395. if ($(node).hasClass(v)) {
  1396. result = true;
  1397. return false;
  1398. }
  1399. })
  1400. return result;
  1401. }
  1402. }
  1403. name = utils.trim(name).replace(/[ ]{2,}/g, ' ').split(' ');
  1404. var arr = [];
  1405. for (var n = 0, ni; ni = name[n++];) {
  1406. var list = node.getElementsByTagName(ni);
  1407. for (var i = 0, ci; ci = list[i++];) {
  1408. if (!filter || filter(ci))
  1409. arr.push(ci);
  1410. }
  1411. }
  1412. return arr;
  1413. },
  1414. /**
  1415. * 设置节点node及其子节点不会被选中
  1416. * @name unSelectable
  1417. * @grammar UM.dom.domUtils.unSelectable(node)
  1418. */
  1419. unSelectable: ie || browser.opera ? function (node) {
  1420. //for ie9
  1421. node.onselectstart = function () {
  1422. return false;
  1423. };
  1424. node.onclick = node.onkeyup = node.onkeydown = function () {
  1425. return false;
  1426. };
  1427. node.unselectable = 'on';
  1428. node.setAttribute("unselectable", "on");
  1429. for (var i = 0, ci; ci = node.all[i++];) {
  1430. switch (ci.tagName.toLowerCase()) {
  1431. case 'iframe' :
  1432. case 'textarea' :
  1433. case 'input' :
  1434. case 'select' :
  1435. break;
  1436. default :
  1437. ci.unselectable = 'on';
  1438. node.setAttribute("unselectable", "on");
  1439. }
  1440. }
  1441. } : function (node) {
  1442. node.style.MozUserSelect =
  1443. node.style.webkitUserSelect =
  1444. node.style.KhtmlUserSelect = 'none';
  1445. },
  1446. /**
  1447. * 删除节点node上的属性attrNames,attrNames为属性名称数组
  1448. * @name removeAttributes
  1449. * @grammar UM.dom.domUtils.removeAttributes(node,attrNames)
  1450. * @example
  1451. * //Before remove
  1452. * <span style="font-size:14px;" id="test" name="followMe">xxxxx</span>
  1453. * //Remove
  1454. * UM.dom.domUtils.removeAttributes(node,["id","name"]);
  1455. * //After remove
  1456. * <span style="font-size:14px;">xxxxx</span>
  1457. */
  1458. removeAttributes: function (node, attrNames) {
  1459. attrNames = utils.isArray(attrNames) ? attrNames : utils.trim(attrNames).replace(/[ ]{2,}/g, ' ').split(' ');
  1460. for (var i = 0, ci; ci = attrNames[i++];) {
  1461. ci = attrFix[ci] || ci;
  1462. switch (ci) {
  1463. case 'className':
  1464. node[ci] = '';
  1465. break;
  1466. case 'style':
  1467. node.style.cssText = '';
  1468. !browser.ie && node.removeAttributeNode(node.getAttributeNode('style'))
  1469. }
  1470. node.removeAttribute(ci);
  1471. }
  1472. },
  1473. /**
  1474. * 在doc下创建一个标签名为tag,属性为attrs的元素
  1475. * @name createElement
  1476. * @grammar UM.dom.domUtils.createElement(doc,tag,attrs) => Node //返回创建的节点
  1477. */
  1478. createElement: function (doc, tag, attrs) {
  1479. return domUtils.setAttributes(doc.createElement(tag), attrs)
  1480. },
  1481. /**
  1482. * 为节点node添加属性attrs,attrs为属性键值对
  1483. * @name setAttributes
  1484. * @grammar UM.dom.domUtils.setAttributes(node,attrs) => node
  1485. */
  1486. setAttributes: function (node, attrs) {
  1487. for (var attr in attrs) {
  1488. if (attrs.hasOwnProperty(attr)) {
  1489. var value = attrs[attr];
  1490. switch (attr) {
  1491. case 'class':
  1492. //ie下要这样赋值,setAttribute不起作用
  1493. node.className = value;
  1494. break;
  1495. case 'style' :
  1496. node.style.cssText = node.style.cssText + ";" + value;
  1497. break;
  1498. case 'innerHTML':
  1499. node[attr] = value;
  1500. break;
  1501. case 'value':
  1502. node.value = value;
  1503. break;
  1504. default:
  1505. node.setAttribute(attrFix[attr] || attr, value);
  1506. }
  1507. }
  1508. }
  1509. return node;
  1510. },
  1511. /**
  1512. * 获取元素element的计算样式
  1513. * @name getComputedStyle
  1514. * @grammar UM.dom.domUtils.getComputedStyle(element,styleName) => String //返回对应样式名称的样式值
  1515. * @example
  1516. * getComputedStyle(document.body,"font-size") => "15px"
  1517. * getComputedStyle(form,"color") => "#ffccdd"
  1518. */
  1519. getComputedStyle: function (element, styleName) {
  1520. return utils.transUnitToPx(utils.fixColor(styleName, $(element).css(styleName)));
  1521. },
  1522. /**
  1523. * 阻止事件默认行为
  1524. * @param {Event} evt 需要组织的事件对象
  1525. */
  1526. preventDefault: function (evt) {
  1527. evt.preventDefault ? evt.preventDefault() : (evt.returnValue = false);
  1528. },
  1529. /**
  1530. * 获取元素element的某个样式值
  1531. * @name getStyle
  1532. * @grammar UM.dom.domUtils.getStyle(element,name) => String
  1533. */
  1534. getStyle: function (element, name) {
  1535. var value = element.style[ utils.cssStyleToDomStyle(name) ];
  1536. return utils.fixColor(name, value);
  1537. },
  1538. /**
  1539. * 为元素element设置样式属性值
  1540. * @name setStyle
  1541. * @grammar UM.dom.domUtils.setStyle(element,name,value)
  1542. */
  1543. setStyle: function (element, name, value) {
  1544. element.style[utils.cssStyleToDomStyle(name)] = value;
  1545. if (!utils.trim(element.style.cssText)) {
  1546. this.removeAttributes(element, 'style')
  1547. }
  1548. },
  1549. /**
  1550. * 删除_moz_dirty属性
  1551. * @function
  1552. */
  1553. removeDirtyAttr: function (node) {
  1554. for (var i = 0, ci, nodes = node.getElementsByTagName('*'); ci = nodes[i++];) {
  1555. ci.removeAttribute('_moz_dirty');
  1556. }
  1557. node.removeAttribute('_moz_dirty');
  1558. },
  1559. /**
  1560. * 返回子节点的数量
  1561. * @function
  1562. * @param {Node} node 父节点
  1563. * @param {Function} fn 过滤子节点的规则,若为空,则得到所有子节点的数量
  1564. * @return {Number} 符合条件子节点的数量
  1565. */
  1566. getChildCount: function (node, fn) {
  1567. var count = 0, first = node.firstChild;
  1568. fn = fn || function () {
  1569. return 1;
  1570. };
  1571. while (first) {
  1572. if (fn(first)) {
  1573. count++;
  1574. }
  1575. first = first.nextSibling;
  1576. }
  1577. return count;
  1578. },
  1579. /**
  1580. * 判断是否为空节点
  1581. * @function
  1582. * @param {Node} node 节点
  1583. * @return {Boolean} 是否为空节点
  1584. */
  1585. isEmptyNode: function (node) {
  1586. return !node.firstChild || domUtils.getChildCount(node, function (node) {
  1587. return !domUtils.isBr(node) && !domUtils.isBookmarkNode(node) && !domUtils.isWhitespace(node)
  1588. }) == 0
  1589. },
  1590. /**
  1591. * 判断节点是否为br
  1592. * @function
  1593. * @param {Node} node 节点
  1594. */
  1595. isBr: function (node) {
  1596. return node.nodeType == 1 && node.tagName == 'BR';
  1597. },
  1598. isFillChar: function (node, isInStart) {
  1599. return node.nodeType == 3 && !node.nodeValue.replace(new RegExp((isInStart ? '^' : '' ) + domUtils.fillChar), '').length
  1600. },
  1601. isEmptyBlock: function (node, reg) {
  1602. if (node.nodeType != 1)
  1603. return 0;
  1604. reg = reg || new RegExp('[ \t\r\n' + domUtils.fillChar + ']', 'g');
  1605. if (node[browser.ie ? 'innerText' : 'textContent'].replace(reg, '').length > 0) {
  1606. return 0;
  1607. }
  1608. for (var n in dtd.$isNotEmpty) {
  1609. if (node.getElementsByTagName(n).length) {
  1610. return 0;
  1611. }
  1612. }
  1613. return 1;
  1614. },
  1615. //判断是否是编辑器自定义的参数
  1616. isCustomeNode: function (node) {
  1617. return node.nodeType == 1 && node.getAttribute('_ue_custom_node_');
  1618. },
  1619. fillNode: function (doc, node) {
  1620. var tmpNode = browser.ie ? doc.createTextNode(domUtils.fillChar) : doc.createElement('br');
  1621. node.innerHTML = '';
  1622. node.appendChild(tmpNode);
  1623. },
  1624. isBoundaryNode: function (node, dir) {
  1625. var tmp;
  1626. while (!domUtils.isBody(node)) {
  1627. tmp = node;
  1628. node = node.parentNode;
  1629. if (tmp !== node[dir]) {
  1630. return false;
  1631. }
  1632. }
  1633. return true;
  1634. },
  1635. isFillChar: function (node, isInStart) {
  1636. return node.nodeType == 3 && !node.nodeValue.replace(new RegExp((isInStart ? '^' : '' ) + domUtils.fillChar), '').length
  1637. }
  1638. };
  1639. var fillCharReg = new RegExp(domUtils.fillChar, 'g');
  1640. ///import editor.js
  1641. ///import core/utils.js
  1642. ///import core/browser.js
  1643. ///import core/dom/dom.js
  1644. ///import core/dom/dtd.js
  1645. ///import core/dom/domUtils.js
  1646. /**
  1647. * @file
  1648. * @name UM.dom.Range
  1649. * @anthor zhanyi
  1650. * @short Range
  1651. * @import editor.js,core/utils.js,core/browser.js,core/dom/domUtils.js,core/dom/dtd.js
  1652. * @desc Range范围实现类,本类是UEditor底层核心类,统一w3cRange和ieRange之间的差异,包括接口和属性
  1653. */
  1654. (function () {
  1655. var guid = 0,
  1656. fillChar = domUtils.fillChar,
  1657. fillData;
  1658. /**
  1659. * 更新range的collapse状态
  1660. * @param {Range} range range对象
  1661. */
  1662. function updateCollapse(range) {
  1663. range.collapsed =
  1664. range.startContainer && range.endContainer &&
  1665. range.startContainer === range.endContainer &&
  1666. range.startOffset == range.endOffset;
  1667. }
  1668. function selectOneNode(rng){
  1669. return !rng.collapsed && rng.startContainer.nodeType == 1 && rng.startContainer === rng.endContainer && rng.endOffset - rng.startOffset == 1
  1670. }
  1671. function setEndPoint(toStart, node, offset, range) {
  1672. //如果node是自闭合标签要处理
  1673. if (node.nodeType == 1 && (dtd.$empty[node.tagName] || dtd.$nonChild[node.tagName])) {
  1674. offset = domUtils.getNodeIndex(node) + (toStart ? 0 : 1);
  1675. node = node.parentNode;
  1676. }
  1677. if (toStart) {
  1678. range.startContainer = node;
  1679. range.startOffset = offset;
  1680. if (!range.endContainer) {
  1681. range.collapse(true);
  1682. }
  1683. } else {
  1684. range.endContainer = node;
  1685. range.endOffset = offset;
  1686. if (!range.startContainer) {
  1687. range.collapse(false);
  1688. }
  1689. }
  1690. updateCollapse(range);
  1691. return range;
  1692. }
  1693. /**
  1694. * @name Range
  1695. * @grammar new UM.dom.Range(document) => Range 实例
  1696. * @desc 创建一个跟document绑定的空的Range实例
  1697. * - ***startContainer*** 开始边界的容器节点,可以是elementNode或者是textNode
  1698. * - ***startOffset*** 容器节点中的偏移量,如果是elementNode就是childNodes中的第几个,如果是textNode就是nodeValue的第几个字符
  1699. * - ***endContainer*** 结束边界的容器节点,可以是elementNode或者是textNode
  1700. * - ***endOffset*** 容器节点中的偏移量,如果是elementNode就是childNodes中的第几个,如果是textNode就是nodeValue的第几个字符
  1701. * - ***document*** 跟range关联的document对象
  1702. * - ***collapsed*** 是否是闭合状态
  1703. */
  1704. var Range = dom.Range = function (document,body) {
  1705. var me = this;
  1706. me.startContainer =
  1707. me.startOffset =
  1708. me.endContainer =
  1709. me.endOffset = null;
  1710. me.document = document;
  1711. me.collapsed = true;
  1712. me.body = body;
  1713. };
  1714. /**
  1715. * 删除fillData
  1716. * @param doc
  1717. * @param excludeNode
  1718. */
  1719. function removeFillData(doc, excludeNode) {
  1720. try {
  1721. if (fillData && domUtils.inDoc(fillData, doc)) {
  1722. if (!fillData.nodeValue.replace(fillCharReg, '').length) {
  1723. var tmpNode = fillData.parentNode;
  1724. domUtils.remove(fillData);
  1725. while (tmpNode && domUtils.isEmptyInlineElement(tmpNode) &&
  1726. //safari的contains有bug
  1727. (browser.safari ? !(domUtils.getPosition(tmpNode,excludeNode) & domUtils.POSITION_CONTAINS) : !tmpNode.contains(excludeNode))
  1728. ) {
  1729. fillData = tmpNode.parentNode;
  1730. domUtils.remove(tmpNode);
  1731. tmpNode = fillData;
  1732. }
  1733. } else {
  1734. fillData.nodeValue = fillData.nodeValue.replace(fillCharReg, '');
  1735. }
  1736. }
  1737. } catch (e) {
  1738. }
  1739. }
  1740. /**
  1741. *
  1742. * @param node
  1743. * @param dir
  1744. */
  1745. function mergeSibling(node, dir) {
  1746. var tmpNode;
  1747. node = node[dir];
  1748. while (node && domUtils.isFillChar(node)) {
  1749. tmpNode = node[dir];
  1750. domUtils.remove(node);
  1751. node = tmpNode;
  1752. }
  1753. }
  1754. function execContentsAction(range, action) {
  1755. //调整边界
  1756. //range.includeBookmark();
  1757. var start = range.startContainer,
  1758. end = range.endContainer,
  1759. startOffset = range.startOffset,
  1760. endOffset = range.endOffset,
  1761. doc = range.document,
  1762. frag = doc.createDocumentFragment(),
  1763. tmpStart, tmpEnd;
  1764. if (start.nodeType == 1) {
  1765. start = start.childNodes[startOffset] || (tmpStart = start.appendChild(doc.createTextNode('')));
  1766. }
  1767. if (end.nodeType == 1) {
  1768. end = end.childNodes[endOffset] || (tmpEnd = end.appendChild(doc.createTextNode('')));
  1769. }
  1770. if (start === end && start.nodeType == 3) {
  1771. frag.appendChild(doc.createTextNode(start.substringData(startOffset, endOffset - startOffset)));
  1772. //is not clone
  1773. if (action) {
  1774. start.deleteData(startOffset, endOffset - startOffset);
  1775. range.collapse(true);
  1776. }
  1777. return frag;
  1778. }
  1779. var current, currentLevel, clone = frag,
  1780. startParents = domUtils.findParents(start, true), endParents = domUtils.findParents(end, true);
  1781. for (var i = 0; startParents[i] == endParents[i];) {
  1782. i++;
  1783. }
  1784. for (var j = i, si; si = startParents[j]; j++) {
  1785. current = si.nextSibling;
  1786. if (si == start) {
  1787. if (!tmpStart) {
  1788. if (range.startContainer.nodeType == 3) {
  1789. clone.appendChild(doc.createTextNode(start.nodeValue.slice(startOffset)));
  1790. //is not clone
  1791. if (action) {
  1792. start.deleteData(startOffset, start.nodeValue.length - startOffset);
  1793. }
  1794. } else {
  1795. clone.appendChild(!action ? start.cloneNode(true) : start);
  1796. }
  1797. }
  1798. } else {
  1799. currentLevel = si.cloneNode(false);
  1800. clone.appendChild(currentLevel);
  1801. }
  1802. while (current) {
  1803. if (current === end || current === endParents[j]) {
  1804. break;
  1805. }
  1806. si = current.nextSibling;
  1807. clone.appendChild(!action ? current.cloneNode(true) : current);
  1808. current = si;
  1809. }
  1810. clone = currentLevel;
  1811. }
  1812. clone = frag;
  1813. if (!startParents[i]) {
  1814. clone.appendChild(startParents[i - 1].cloneNode(false));
  1815. clone = clone.firstChild;
  1816. }
  1817. for (var j = i, ei; ei = endParents[j]; j++) {
  1818. current = ei.previousSibling;
  1819. if (ei == end) {
  1820. if (!tmpEnd && range.endContainer.nodeType == 3) {
  1821. clone.appendChild(doc.createTextNode(end.substringData(0, endOffset)));
  1822. //is not clone
  1823. if (action) {
  1824. end.deleteData(0, endOffset);
  1825. }
  1826. }
  1827. } else {
  1828. currentLevel = ei.cloneNode(false);
  1829. clone.appendChild(currentLevel);
  1830. }
  1831. //如果两端同级,右边第一次已经被开始做了
  1832. if (j != i || !startParents[i]) {
  1833. while (current) {
  1834. if (current === start) {
  1835. break;
  1836. }
  1837. ei = current.previousSibling;
  1838. clone.insertBefore(!action ? current.cloneNode(true) : current, clone.firstChild);
  1839. current = ei;
  1840. }
  1841. }
  1842. clone = currentLevel;
  1843. }
  1844. if (action) {
  1845. range.setStartBefore(!endParents[i] ? endParents[i - 1] : !startParents[i] ? startParents[i - 1] : endParents[i]).collapse(true);
  1846. }
  1847. tmpStart && domUtils.remove(tmpStart);
  1848. tmpEnd && domUtils.remove(tmpEnd);
  1849. return frag;
  1850. }
  1851. Range.prototype = {
  1852. /**
  1853. * @name deleteContents
  1854. * @grammar range.deleteContents() => Range
  1855. * @desc 删除当前选区范围中的所有内容并返回range实例,这时的range已经变成了闭合状态
  1856. * @example
  1857. * DOM Element :
  1858. * <b>x<i>x[x<i>xx]x</b>
  1859. * //执行方法后
  1860. * <b>x<i>x<i>|x</b>
  1861. * 注意range改变了
  1862. * range.startContainer => b
  1863. * range.startOffset => 2
  1864. * range.endContainer => b
  1865. * range.endOffset => 2
  1866. * range.collapsed => true
  1867. */
  1868. deleteContents:function () {
  1869. var txt;
  1870. if (!this.collapsed) {
  1871. execContentsAction(this, 1);
  1872. }
  1873. if (browser.webkit) {
  1874. txt = this.startContainer;
  1875. if (txt.nodeType == 3 && !txt.nodeValue.length) {
  1876. this.setStartBefore(txt).collapse(true);
  1877. domUtils.remove(txt);
  1878. }
  1879. }
  1880. return this;
  1881. },
  1882. inFillChar : function(){
  1883. var start = this.startContainer;
  1884. if(this.collapsed && start.nodeType == 3
  1885. && start.nodeValue.replace(new RegExp('^' + domUtils.fillChar),'').length + 1 == start.nodeValue.length
  1886. ){
  1887. return true;
  1888. }
  1889. return false;
  1890. },
  1891. /**
  1892. * @name setStart
  1893. * @grammar range.setStart(node,offset) => Range
  1894. * @desc 设置range的开始位置位于node节点内,偏移量为offset
  1895. * 如果node是elementNode那offset指的是childNodes中的第几个,如果是textNode那offset指的是nodeValue的第几个字符
  1896. */
  1897. setStart:function (node, offset) {
  1898. return setEndPoint(true, node, offset, this);
  1899. },
  1900. /**
  1901. * 设置range的结束位置位于node节点,偏移量为offset
  1902. * 如果node是elementNode那offset指的是childNodes中的第几个,如果是textNode那offset指的是nodeValue的第几个字符
  1903. * @name setEnd
  1904. * @grammar range.setEnd(node,offset) => Range
  1905. */
  1906. setEnd:function (node, offset) {
  1907. return setEndPoint(false, node, offset, this);
  1908. },
  1909. /**
  1910. * 将Range开始位置设置到node节点之后
  1911. * @name setStartAfter
  1912. * @grammar range.setStartAfter(node) => Range
  1913. * @example
  1914. * <b>xx<i>x|x</i>x</b>
  1915. * 执行setStartAfter(i)后
  1916. * range.startContainer =>b
  1917. * range.startOffset =>2
  1918. */
  1919. setStartAfter:function (node) {
  1920. return this.setStart(node.parentNode, domUtils.getNodeIndex(node) + 1);
  1921. },
  1922. /**
  1923. * 将Range开始位置设置到node节点之前
  1924. * @name setStartBefore
  1925. * @grammar range.setStartBefore(node) => Range
  1926. * @example
  1927. * <b>xx<i>x|x</i>x</b>
  1928. * 执行setStartBefore(i)后
  1929. * range.startContainer =>b
  1930. * range.startOffset =>1
  1931. */
  1932. setStartBefore:function (node) {
  1933. return this.setStart(node.parentNode, domUtils.getNodeIndex(node));
  1934. },
  1935. /**
  1936. * 将Range结束位置设置到node节点之后
  1937. * @name setEndAfter
  1938. * @grammar range.setEndAfter(node) => Range
  1939. * @example
  1940. * <b>xx<i>x|x</i>x</b>
  1941. * setEndAfter(i)后
  1942. * range.endContainer =>b
  1943. * range.endtOffset =>2
  1944. */
  1945. setEndAfter:function (node) {
  1946. return this.setEnd(node.parentNode, domUtils.getNodeIndex(node) + 1);
  1947. },
  1948. /**
  1949. * 将Range结束位置设置到node节点之前
  1950. * @name setEndBefore
  1951. * @grammar range.setEndBefore(node) => Range
  1952. * @example
  1953. * <b>xx<i>x|x</i>x</b>
  1954. * 执行setEndBefore(i)后
  1955. * range.endContainer =>b
  1956. * range.endtOffset =>1
  1957. */
  1958. setEndBefore:function (node) {
  1959. return this.setEnd(node.parentNode, domUtils.getNodeIndex(node));
  1960. },
  1961. /**
  1962. * 将Range开始位置设置到node节点内的开始位置
  1963. * @name setStartAtFirst
  1964. * @grammar range.setStartAtFirst(node) => Range
  1965. */
  1966. setStartAtFirst:function (node) {
  1967. return this.setStart(node, 0);
  1968. },
  1969. /**
  1970. * 将Range开始位置设置到node节点内的结束位置
  1971. * @name setStartAtLast
  1972. * @grammar range.setStartAtLast(node) => Range
  1973. */
  1974. setStartAtLast:function (node) {
  1975. return this.setStart(node, node.nodeType == 3 ? node.nodeValue.length : node.childNodes.length);
  1976. },
  1977. /**
  1978. * 将Range结束位置设置到node节点内的开始位置
  1979. * @name setEndAtFirst
  1980. * @grammar range.setEndAtFirst(node) => Range
  1981. */
  1982. setEndAtFirst:function (node) {
  1983. return this.setEnd(node, 0);
  1984. },
  1985. /**
  1986. * 将Range结束位置设置到node节点内的结束位置
  1987. * @name setEndAtLast
  1988. * @grammar range.setEndAtLast(node) => Range
  1989. */
  1990. setEndAtLast:function (node) {
  1991. return this.setEnd(node, node.nodeType == 3 ? node.nodeValue.length : node.childNodes.length);
  1992. },
  1993. /**
  1994. * 选中完整的指定节点,并返回包含该节点的range
  1995. * @name selectNode
  1996. * @grammar range.selectNode(node) => Range
  1997. */
  1998. selectNode:function (node) {
  1999. return this.setStartBefore(node).setEndAfter(node);
  2000. },
  2001. /**
  2002. * 选中node内部的所有节点,并返回对应的range
  2003. * @name selectNodeContents
  2004. * @grammar range.selectNodeContents(node) => Range
  2005. * @example
  2006. * <b>xx[x<i>xxx</i>]xxx</b>
  2007. * 执行后
  2008. * <b>[xxx<i>xxx</i>xxx]</b>
  2009. * range.startContainer =>b
  2010. * range.startOffset =>0
  2011. * range.endContainer =>b
  2012. * range.endOffset =>3
  2013. */
  2014. selectNodeContents:function (node) {
  2015. return this.setStart(node, 0).setEndAtLast(node);
  2016. },
  2017. /**
  2018. * 克隆一个新的range对象
  2019. * @name cloneRange
  2020. * @grammar range.cloneRange() => Range
  2021. */
  2022. cloneRange:function () {
  2023. var me = this;
  2024. return new Range(me.document).setStart(me.startContainer, me.startOffset).setEnd(me.endContainer, me.endOffset);
  2025. },
  2026. /**
  2027. * 让选区闭合到尾部,若toStart为真,则闭合到头部
  2028. * @name collapse
  2029. * @grammar range.collapse() => Range
  2030. * @grammar range.collapse(true) => Range //闭合选区到头部
  2031. */
  2032. collapse:function (toStart) {
  2033. var me = this;
  2034. if (toStart) {
  2035. me.endContainer = me.startContainer;
  2036. me.endOffset = me.startOffset;
  2037. } else {
  2038. me.startContainer = me.endContainer;
  2039. me.startOffset = me.endOffset;
  2040. }
  2041. me.collapsed = true;
  2042. return me;
  2043. },
  2044. /**
  2045. * 调整range的边界,使其"收缩"到最小的位置
  2046. * @name shrinkBoundary
  2047. * @grammar range.shrinkBoundary() => Range //range开始位置和结束位置都调整,参见<code><a href="#adjustmentboundary">adjustmentBoundary</a></code>
  2048. * @grammar range.shrinkBoundary(true) => Range //仅调整开始位置,忽略结束位置
  2049. * @example
  2050. * <b>xx[</b>xxxxx] ==> <b>xx</b>[xxxxx]
  2051. * <b>x[xx</b><i>]xxx</i> ==> <b>x[xx]</b><i>xxx</i>
  2052. * [<b><i>xxxx</i>xxxxxxx</b>] ==> <b><i>[xxxx</i>xxxxxxx]</b>
  2053. */
  2054. shrinkBoundary:function (ignoreEnd) {
  2055. var me = this, child,
  2056. collapsed = me.collapsed;
  2057. function check(node){
  2058. return node.nodeType == 1 && !domUtils.isBookmarkNode(node) && !dtd.$empty[node.tagName] && !dtd.$nonChild[node.tagName]
  2059. }
  2060. while (me.startContainer.nodeType == 1 //是element
  2061. && (child = me.startContainer.childNodes[me.startOffset]) //子节点也是element
  2062. && check(child)) {
  2063. me.setStart(child, 0);
  2064. }
  2065. if (collapsed) {
  2066. return me.collapse(true);
  2067. }
  2068. if (!ignoreEnd) {
  2069. while (me.endContainer.nodeType == 1//是element
  2070. && me.endOffset > 0 //如果是空元素就退出 endOffset=0那么endOffst-1为负值,childNodes[endOffset]报错
  2071. && (child = me.endContainer.childNodes[me.endOffset - 1]) //子节点也是element
  2072. && check(child)) {
  2073. me.setEnd(child, child.childNodes.length);
  2074. }
  2075. }
  2076. return me;
  2077. },
  2078. /**
  2079. * 调整边界容器,如果是textNode,就调整到elementNode上
  2080. * @name trimBoundary
  2081. * @grammar range.trimBoundary([ignoreEnd]) => Range //true忽略结束边界
  2082. * @example
  2083. * DOM Element :
  2084. * <b>|xxx</b>
  2085. * startContainer = xxx; startOffset = 0
  2086. * //执行后本方法后
  2087. * startContainer = <b>; startOffset = 0
  2088. * @example
  2089. * Dom Element :
  2090. * <b>xx|x</b>
  2091. * startContainer = xxx; startOffset = 2
  2092. * //执行本方法后,xxx被实实在在地切分成两个TextNode
  2093. * startContainer = <b>; startOffset = 1
  2094. */
  2095. trimBoundary:function (ignoreEnd) {
  2096. this.txtToElmBoundary();
  2097. var start = this.startContainer,
  2098. offset = this.startOffset,
  2099. collapsed = this.collapsed,
  2100. end = this.endContainer;
  2101. if (start.nodeType == 3) {
  2102. if (offset == 0) {
  2103. this.setStartBefore(start);
  2104. } else {
  2105. if (offset >= start.nodeValue.length) {
  2106. this.setStartAfter(start);
  2107. } else {
  2108. var textNode = domUtils.split(start, offset);
  2109. //跟新结束边界
  2110. if (start === end) {
  2111. this.setEnd(textNode, this.endOffset - offset);
  2112. } else if (start.parentNode === end) {
  2113. this.endOffset += 1;
  2114. }
  2115. this.setStartBefore(textNode);
  2116. }
  2117. }
  2118. if (collapsed) {
  2119. return this.collapse(true);
  2120. }
  2121. }
  2122. if (!ignoreEnd) {
  2123. offset = this.endOffset;
  2124. end = this.endContainer;
  2125. if (end.nodeType == 3) {
  2126. if (offset == 0) {
  2127. this.setEndBefore(end);
  2128. } else {
  2129. offset < end.nodeValue.length && domUtils.split(end, offset);
  2130. this.setEndAfter(end);
  2131. }
  2132. }
  2133. }
  2134. return this;
  2135. },
  2136. /**
  2137. * 如果选区在文本的边界上,就扩展选区到文本的父节点上
  2138. * @name txtToElmBoundary
  2139. * @example
  2140. * Dom Element :
  2141. * <b> |xxx</b>
  2142. * startContainer = xxx; startOffset = 0
  2143. * //本方法执行后
  2144. * startContainer = <b>; startOffset = 0
  2145. * @example
  2146. * Dom Element :
  2147. * <b> xxx| </b>
  2148. * startContainer = xxx; startOffset = 3
  2149. * //本方法执行后
  2150. * startContainer = <b>; startOffset = 1
  2151. */
  2152. txtToElmBoundary:function (ignoreCollapsed) {
  2153. function adjust(r, c) {
  2154. var container = r[c + 'Container'],
  2155. offset = r[c + 'Offset'];
  2156. if (container.nodeType == 3) {
  2157. if (!offset) {
  2158. r['set' + c.replace(/(\w)/, function (a) {
  2159. return a.toUpperCase();
  2160. }) + 'Before'](container);
  2161. } else if (offset >= container.nodeValue.length) {
  2162. r['set' + c.replace(/(\w)/, function (a) {
  2163. return a.toUpperCase();
  2164. }) + 'After' ](container);
  2165. }
  2166. }
  2167. }
  2168. if (ignoreCollapsed || !this.collapsed) {
  2169. adjust(this, 'start');
  2170. adjust(this, 'end');
  2171. }
  2172. return this;
  2173. },
  2174. /**
  2175. * 在当前选区的开始位置前插入一个节点或者fragment,range的开始位置会在插入节点的前边
  2176. * @name insertNode
  2177. * @grammar range.insertNode(node) => Range //node可以是textNode,elementNode,fragment
  2178. * @example
  2179. * Range :
  2180. * xxx[x<p>xxxx</p>xxxx]x<p>sdfsdf</p>
  2181. * 待插入Node :
  2182. * <p>ssss</p>
  2183. * 执行本方法后的Range :
  2184. * xxx[<p>ssss</p>x<p>xxxx</p>xxxx]x<p>sdfsdf</p>
  2185. */
  2186. insertNode:function (node) {
  2187. var first = node, length = 1;
  2188. if (node.nodeType == 11) {
  2189. first = node.firstChild;
  2190. length = node.childNodes.length;
  2191. }
  2192. this.trimBoundary(true);
  2193. var start = this.startContainer,
  2194. offset = this.startOffset;
  2195. var nextNode = start.childNodes[ offset ];
  2196. if (nextNode) {
  2197. start.insertBefore(node, nextNode);
  2198. } else {
  2199. start.appendChild(node);
  2200. }
  2201. if (first.parentNode === this.endContainer) {
  2202. this.endOffset = this.endOffset + length;
  2203. }
  2204. return this.setStartBefore(first);
  2205. },
  2206. /**
  2207. * 设置光标闭合位置,toEnd设置为true时光标将闭合到选区的结尾
  2208. * @name setCursor
  2209. * @grammar range.setCursor([toEnd]) => Range //toEnd为true时,光标闭合到选区的末尾
  2210. */
  2211. setCursor:function (toEnd, noFillData) {
  2212. return this.collapse(!toEnd).select(noFillData);
  2213. },
  2214. /**
  2215. * 创建当前range的一个书签,记录下当前range的位置,方便当dom树改变时,还能找回原来的选区位置
  2216. * @name createBookmark
  2217. * @grammar range.createBookmark([serialize]) => Object //{start:开始标记,end:结束标记,id:serialize} serialize为真时,开始结束标记是插入节点的id,否则是插入节点的引用
  2218. */
  2219. createBookmark:function (serialize, same) {
  2220. var endNode,
  2221. startNode = this.document.createElement('span');
  2222. startNode.style.cssText = 'display:none;line-height:0px;';
  2223. startNode.appendChild(this.document.createTextNode('\u200D'));
  2224. startNode.id = '_baidu_bookmark_start_' + (same ? '' : guid++);
  2225. if (!this.collapsed) {
  2226. endNode = startNode.cloneNode(true);
  2227. endNode.id = '_baidu_bookmark_end_' + (same ? '' : guid++);
  2228. }
  2229. this.insertNode(startNode);
  2230. if (endNode) {
  2231. this.collapse().insertNode(endNode).setEndBefore(endNode);
  2232. }
  2233. this.setStartAfter(startNode);
  2234. return {
  2235. start:serialize ? startNode.id : startNode,
  2236. end:endNode ? serialize ? endNode.id : endNode : null,
  2237. id:serialize
  2238. }
  2239. },
  2240. /**
  2241. * 移动边界到书签位置,并删除插入的书签节点
  2242. * @name moveToBookmark
  2243. * @grammar range.moveToBookmark(bookmark) => Range //让当前的range选到给定bookmark的位置,bookmark对象是由range.createBookmark创建的
  2244. */
  2245. moveToBookmark:function (bookmark) {
  2246. var start = bookmark.id ? this.document.getElementById(bookmark.start) : bookmark.start,
  2247. end = bookmark.end && bookmark.id ? this.document.getElementById(bookmark.end) : bookmark.end;
  2248. this.setStartBefore(start);
  2249. domUtils.remove(start);
  2250. if (end) {
  2251. this.setEndBefore(end);
  2252. domUtils.remove(end);
  2253. } else {
  2254. this.collapse(true);
  2255. }
  2256. return this;
  2257. },
  2258. /**
  2259. * 调整Range的边界,使其"缩小"到最合适的位置
  2260. * @name adjustmentBoundary
  2261. * @grammar range.adjustmentBoundary() => Range //参见<code><a href="#shrinkboundary">shrinkBoundary</a></code>
  2262. * @example
  2263. * <b>xx[</b>xxxxx] ==> <b>xx</b>[xxxxx]
  2264. * <b>x[xx</b><i>]xxx</i> ==> <b>x[xx</b>]<i>xxx</i>
  2265. */
  2266. adjustmentBoundary:function () {
  2267. if (!this.collapsed) {
  2268. while (!domUtils.isBody(this.startContainer) &&
  2269. this.startOffset == this.startContainer[this.startContainer.nodeType == 3 ? 'nodeValue' : 'childNodes'].length &&
  2270. this.startContainer[this.startContainer.nodeType == 3 ? 'nodeValue' : 'childNodes'].length
  2271. ) {
  2272. this.setStartAfter(this.startContainer);
  2273. }
  2274. while (!domUtils.isBody(this.endContainer) && !this.endOffset &&
  2275. this.endContainer[this.endContainer.nodeType == 3 ? 'nodeValue' : 'childNodes'].length
  2276. ) {
  2277. this.setEndBefore(this.endContainer);
  2278. }
  2279. }
  2280. return this;
  2281. },
  2282. /**
  2283. * 得到一个自闭合的节点,常用于获取自闭和的节点,例如图片节点
  2284. * @name getClosedNode
  2285. * @grammar range.getClosedNode() => node|null
  2286. * @example
  2287. * <b>xxxx[<img />]xxx</b>
  2288. */
  2289. getClosedNode:function () {
  2290. var node;
  2291. if (!this.collapsed) {
  2292. var range = this.cloneRange().adjustmentBoundary().shrinkBoundary();
  2293. if (selectOneNode(range)) {
  2294. var child = range.startContainer.childNodes[range.startOffset];
  2295. if (child && child.nodeType == 1 && (dtd.$empty[child.tagName] || dtd.$nonChild[child.tagName])) {
  2296. node = child;
  2297. }
  2298. }
  2299. }
  2300. return node;
  2301. },
  2302. /**
  2303. * 根据当前range选中内容节点(在页面上表现为反白显示)
  2304. * @name select
  2305. * @grammar range.select(); => Range
  2306. */
  2307. select:browser.ie ? function (noFillData, textRange) {
  2308. var nativeRange;
  2309. if (!this.collapsed)
  2310. this.shrinkBoundary();
  2311. var node = this.getClosedNode();
  2312. if (node && !textRange) {
  2313. try {
  2314. nativeRange = this.document.body.createControlRange();
  2315. nativeRange.addElement(node);
  2316. nativeRange.select();
  2317. } catch (e) {}
  2318. return this;
  2319. }
  2320. var bookmark = this.createBookmark(),
  2321. start = bookmark.start,
  2322. end;
  2323. nativeRange = this.document.body.createTextRange();
  2324. nativeRange.moveToElementText(start);
  2325. nativeRange.moveStart('character', 1);
  2326. if (!this.collapsed) {
  2327. var nativeRangeEnd = this.document.body.createTextRange();
  2328. end = bookmark.end;
  2329. nativeRangeEnd.moveToElementText(end);
  2330. nativeRange.setEndPoint('EndToEnd', nativeRangeEnd);
  2331. } else {
  2332. if (!noFillData && this.startContainer.nodeType != 3) {
  2333. //使用<span>|x<span>固定住光标
  2334. var tmpText = this.document.createTextNode(fillChar),
  2335. tmp = this.document.createElement('span');
  2336. tmp.appendChild(this.document.createTextNode(fillChar));
  2337. start.parentNode.insertBefore(tmp, start);
  2338. start.parentNode.insertBefore(tmpText, start);
  2339. //当点b,i,u时,不能清除i上边的b
  2340. removeFillData(this.document, tmpText);
  2341. fillData = tmpText;
  2342. mergeSibling(tmp, 'previousSibling');
  2343. mergeSibling(start, 'nextSibling');
  2344. nativeRange.moveStart('character', -1);
  2345. nativeRange.collapse(true);
  2346. }
  2347. }
  2348. this.moveToBookmark(bookmark);
  2349. tmp && domUtils.remove(tmp);
  2350. //IE在隐藏状态下不支持range操作,catch一下
  2351. try {
  2352. nativeRange.select();
  2353. } catch (e) {
  2354. }
  2355. return this;
  2356. } : function (notInsertFillData) {
  2357. function checkOffset(rng){
  2358. function check(node,offset,dir){
  2359. if(node.nodeType == 3 && node.nodeValue.length < offset){
  2360. rng[dir + 'Offset'] = node.nodeValue.length
  2361. }
  2362. }
  2363. check(rng.startContainer,rng.startOffset,'start');
  2364. check(rng.endContainer,rng.endOffset,'end');
  2365. }
  2366. var win = domUtils.getWindow(this.document),
  2367. sel = win.getSelection(),
  2368. txtNode;
  2369. //FF下关闭自动长高时滚动条在关闭dialog时会跳
  2370. //ff下如果不body.focus将不能定位闭合光标到编辑器内
  2371. browser.gecko ? this.body.focus() : win.focus();
  2372. if (sel) {
  2373. sel.removeAllRanges();
  2374. // trace:870 chrome/safari后边是br对于闭合得range不能定位 所以去掉了判断
  2375. // this.startContainer.nodeType != 3 &&! ((child = this.startContainer.childNodes[this.startOffset]) && child.nodeType == 1 && child.tagName == 'BR'
  2376. if (this.collapsed && !notInsertFillData) {
  2377. // //opear如果没有节点接着,原生的不能够定位,不能在body的第一级插入空白节点
  2378. // if (notInsertFillData && browser.opera && !domUtils.isBody(this.startContainer) && this.startContainer.nodeType == 1) {
  2379. // var tmp = this.document.createTextNode('');
  2380. // this.insertNode(tmp).setStart(tmp, 0).collapse(true);
  2381. // }
  2382. //
  2383. //处理光标落在文本节点的情况
  2384. //处理以下的情况
  2385. //<b>|xxxx</b>
  2386. //<b>xxxx</b>|xxxx
  2387. //xxxx<b>|</b>
  2388. var start = this.startContainer,child = start;
  2389. if(start.nodeType == 1){
  2390. child = start.childNodes[this.startOffset];
  2391. }
  2392. if( !(start.nodeType == 3 && this.startOffset) &&
  2393. (child ?
  2394. (!child.previousSibling || child.previousSibling.nodeType != 3)
  2395. :
  2396. (!start.lastChild || start.lastChild.nodeType != 3)
  2397. )
  2398. ){
  2399. txtNode = this.document.createTextNode(fillChar);
  2400. //跟着前边走
  2401. this.insertNode(txtNode);
  2402. removeFillData(this.document, txtNode);
  2403. mergeSibling(txtNode, 'previousSibling');
  2404. mergeSibling(txtNode, 'nextSibling');
  2405. fillData = txtNode;
  2406. this.setStart(txtNode, browser.webkit ? 1 : 0).collapse(true);
  2407. }
  2408. }
  2409. var nativeRange = this.document.createRange();
  2410. if(this.collapsed && browser.opera && this.startContainer.nodeType == 1){
  2411. var child = this.startContainer.childNodes[this.startOffset];
  2412. if(!child){
  2413. //往前靠拢
  2414. child = this.startContainer.lastChild;
  2415. if( child && domUtils.isBr(child)){
  2416. this.setStartBefore(child).collapse(true);
  2417. }
  2418. }else{
  2419. //向后靠拢
  2420. while(child && domUtils.isBlockElm(child)){
  2421. if(child.nodeType == 1 && child.childNodes[0]){
  2422. child = child.childNodes[0]
  2423. }else{
  2424. break;
  2425. }
  2426. }
  2427. child && this.setStartBefore(child).collapse(true)
  2428. }
  2429. }
  2430. //是createAddress最后一位算的不准,现在这里进行微调
  2431. checkOffset(this);
  2432. nativeRange.setStart(this.startContainer, this.startOffset);
  2433. nativeRange.setEnd(this.endContainer, this.endOffset);
  2434. sel.addRange(nativeRange);
  2435. }
  2436. return this;
  2437. },
  2438. createAddress : function(ignoreEnd,ignoreTxt){
  2439. var addr = {},me = this;
  2440. function getAddress(isStart){
  2441. var node = isStart ? me.startContainer : me.endContainer;
  2442. var parents = domUtils.findParents(node,true,function(node){return !domUtils.isBody(node)}),
  2443. addrs = [];
  2444. for(var i = 0,ci;ci = parents[i++];){
  2445. addrs.push(domUtils.getNodeIndex(ci,ignoreTxt));
  2446. }
  2447. var firstIndex = 0;
  2448. if(ignoreTxt){
  2449. if(node.nodeType == 3){
  2450. var tmpNode = node.previousSibling;
  2451. while(tmpNode && tmpNode.nodeType == 3){
  2452. firstIndex += tmpNode.nodeValue.replace(fillCharReg,'').length;
  2453. tmpNode = tmpNode.previousSibling;
  2454. }
  2455. firstIndex += (isStart ? me.startOffset : me.endOffset)// - (fillCharReg.test(node.nodeValue) ? 1 : 0 )
  2456. }else{
  2457. node = node.childNodes[ isStart ? me.startOffset : me.endOffset];
  2458. if(node){
  2459. firstIndex = domUtils.getNodeIndex(node,ignoreTxt);
  2460. }else{
  2461. node = isStart ? me.startContainer : me.endContainer;
  2462. var first = node.firstChild;
  2463. while(first){
  2464. if(domUtils.isFillChar(first)){
  2465. first = first.nextSibling;
  2466. continue;
  2467. }
  2468. firstIndex++;
  2469. if(first.nodeType == 3){
  2470. while( first && first.nodeType == 3){
  2471. first = first.nextSibling;
  2472. }
  2473. }else{
  2474. first = first.nextSibling;
  2475. }
  2476. }
  2477. }
  2478. }
  2479. }else{
  2480. firstIndex = isStart ? domUtils.isFillChar(node) ? 0 : me.startOffset : me.endOffset
  2481. }
  2482. if(firstIndex < 0){
  2483. firstIndex = 0;
  2484. }
  2485. addrs.push(firstIndex);
  2486. return addrs;
  2487. }
  2488. addr.startAddress = getAddress(true);
  2489. if(!ignoreEnd){
  2490. addr.endAddress = me.collapsed ? [].concat(addr.startAddress) : getAddress();
  2491. }
  2492. return addr;
  2493. },
  2494. moveToAddress : function(addr,ignoreEnd){
  2495. var me = this;
  2496. function getNode(address,isStart){
  2497. var tmpNode = me.body,
  2498. parentNode,offset;
  2499. for(var i= 0,ci,l=address.length;i<l;i++){
  2500. ci = address[i];
  2501. parentNode = tmpNode;
  2502. tmpNode = tmpNode.childNodes[ci];
  2503. if(!tmpNode){
  2504. offset = ci;
  2505. break;
  2506. }
  2507. }
  2508. if(isStart){
  2509. if(tmpNode){
  2510. me.setStartBefore(tmpNode)
  2511. }else{
  2512. me.setStart(parentNode,offset)
  2513. }
  2514. }else{
  2515. if(tmpNode){
  2516. me.setEndBefore(tmpNode)
  2517. }else{
  2518. me.setEnd(parentNode,offset)
  2519. }
  2520. }
  2521. }
  2522. getNode(addr.startAddress,true);
  2523. !ignoreEnd && addr.endAddress && getNode(addr.endAddress);
  2524. return me;
  2525. },
  2526. equals : function(rng){
  2527. for(var p in this){
  2528. if(this.hasOwnProperty(p)){
  2529. if(this[p] !== rng[p])
  2530. return false
  2531. }
  2532. }
  2533. return true;
  2534. },
  2535. scrollIntoView : function(){
  2536. var $span = $('<span style="padding:0;margin:0;display:block;border:0">&nbsp;</span>');
  2537. this.cloneRange().insertNode($span.get(0));
  2538. var winScrollTop = $(window).scrollTop(),
  2539. winHeight = $(window).height(),
  2540. spanTop = $span.offset().top;
  2541. if(spanTop < winScrollTop-winHeight || spanTop > winScrollTop + winHeight ){
  2542. if(spanTop > winScrollTop + winHeight){
  2543. window.scrollTo(0,spanTop - winHeight + $span.height())
  2544. }else{
  2545. window.scrollTo(0,winScrollTop - spanTop)
  2546. }
  2547. }
  2548. $span.remove();
  2549. }
  2550. };
  2551. })();
  2552. ///import editor.js
  2553. ///import core/browser.js
  2554. ///import core/dom/dom.js
  2555. ///import core/dom/dtd.js
  2556. ///import core/dom/domUtils.js
  2557. ///import core/dom/Range.js
  2558. /**
  2559. * @class UM.dom.Selection Selection类
  2560. */
  2561. (function () {
  2562. function getBoundaryInformation( range, start ) {
  2563. var getIndex = domUtils.getNodeIndex;
  2564. range = range.duplicate();
  2565. range.collapse( start );
  2566. var parent = range.parentElement();
  2567. //如果节点里没有子节点,直接退出
  2568. if ( !parent.hasChildNodes() ) {
  2569. return {container:parent, offset:0};
  2570. }
  2571. var siblings = parent.children,
  2572. child,
  2573. testRange = range.duplicate(),
  2574. startIndex = 0, endIndex = siblings.length - 1, index = -1,
  2575. distance;
  2576. while ( startIndex <= endIndex ) {
  2577. index = Math.floor( (startIndex + endIndex) / 2 );
  2578. child = siblings[index];
  2579. testRange.moveToElementText( child );
  2580. var position = testRange.compareEndPoints( 'StartToStart', range );
  2581. if ( position > 0 ) {
  2582. endIndex = index - 1;
  2583. } else if ( position < 0 ) {
  2584. startIndex = index + 1;
  2585. } else {
  2586. //trace:1043
  2587. return {container:parent, offset:getIndex( child )};
  2588. }
  2589. }
  2590. if ( index == -1 ) {
  2591. testRange.moveToElementText( parent );
  2592. testRange.setEndPoint( 'StartToStart', range );
  2593. distance = testRange.text.replace( /(\r\n|\r)/g, '\n' ).length;
  2594. siblings = parent.childNodes;
  2595. if ( !distance ) {
  2596. child = siblings[siblings.length - 1];
  2597. return {container:child, offset:child.nodeValue.length};
  2598. }
  2599. var i = siblings.length;
  2600. while ( distance > 0 ){
  2601. distance -= siblings[ --i ].nodeValue.length;
  2602. }
  2603. return {container:siblings[i], offset:-distance};
  2604. }
  2605. testRange.collapse( position > 0 );
  2606. testRange.setEndPoint( position > 0 ? 'StartToStart' : 'EndToStart', range );
  2607. distance = testRange.text.replace( /(\r\n|\r)/g, '\n' ).length;
  2608. if ( !distance ) {
  2609. return dtd.$empty[child.tagName] || dtd.$nonChild[child.tagName] ?
  2610. {container:parent, offset:getIndex( child ) + (position > 0 ? 0 : 1)} :
  2611. {container:child, offset:position > 0 ? 0 : child.childNodes.length}
  2612. }
  2613. while ( distance > 0 ) {
  2614. try {
  2615. var pre = child;
  2616. child = child[position > 0 ? 'previousSibling' : 'nextSibling'];
  2617. distance -= child.nodeValue.length;
  2618. } catch ( e ) {
  2619. return {container:parent, offset:getIndex( pre )};
  2620. }
  2621. }
  2622. return {container:child, offset:position > 0 ? -distance : child.nodeValue.length + distance}
  2623. }
  2624. /**
  2625. * 将ieRange转换为Range对象
  2626. * @param {Range} ieRange ieRange对象
  2627. * @param {Range} range Range对象
  2628. * @return {Range} range 返回转换后的Range对象
  2629. */
  2630. function transformIERangeToRange( ieRange, range ) {
  2631. if ( ieRange.item ) {
  2632. range.selectNode( ieRange.item( 0 ) );
  2633. } else {
  2634. var bi = getBoundaryInformation( ieRange, true );
  2635. range.setStart( bi.container, bi.offset );
  2636. if ( ieRange.compareEndPoints( 'StartToEnd', ieRange ) != 0 ) {
  2637. bi = getBoundaryInformation( ieRange, false );
  2638. range.setEnd( bi.container, bi.offset );
  2639. }
  2640. }
  2641. return range;
  2642. }
  2643. /**
  2644. * 获得ieRange
  2645. * @param {Selection} sel Selection对象
  2646. * @return {ieRange} 得到ieRange
  2647. */
  2648. function _getIERange( sel,txtRange ) {
  2649. var ieRange;
  2650. //ie下有可能报错
  2651. try {
  2652. ieRange = sel.getNative(txtRange).createRange();
  2653. } catch ( e ) {
  2654. return null;
  2655. }
  2656. var el = ieRange.item ? ieRange.item( 0 ) : ieRange.parentElement();
  2657. if ( ( el.ownerDocument || el ) === sel.document ) {
  2658. return ieRange;
  2659. }
  2660. return null;
  2661. }
  2662. var Selection = dom.Selection = function ( doc,body ) {
  2663. var me = this;
  2664. me.document = doc;
  2665. me.body = body;
  2666. if ( browser.ie9below ) {
  2667. domUtils.on( body, 'beforedeactivate', function () {
  2668. me._bakIERange = me.getIERange();
  2669. } );
  2670. domUtils.on( body, 'activate', function () {
  2671. try {
  2672. var ieNativRng = _getIERange( me );
  2673. if ( (!ieNativRng || !me.rangeInBody(ieNativRng)) && me._bakIERange ) {
  2674. me._bakIERange.select();
  2675. }
  2676. } catch ( ex ) {
  2677. }
  2678. me._bakIERange = null;
  2679. } );
  2680. }
  2681. };
  2682. Selection.prototype = {
  2683. hasNativeRange : function(){
  2684. var rng;
  2685. if(!browser.ie || browser.ie9above){
  2686. var nativeSel = this.getNative();
  2687. if(!nativeSel.rangeCount){
  2688. return false;
  2689. }
  2690. rng = nativeSel.getRangeAt(0);
  2691. }else{
  2692. rng = _getIERange(this);
  2693. }
  2694. return this.rangeInBody(rng);
  2695. },
  2696. /**
  2697. * 获取原生seleciton对象
  2698. * @public
  2699. * @function
  2700. * @name UM.dom.Selection.getNative
  2701. * @return {Selection} 获得selection对象
  2702. */
  2703. getNative:function (txtRange) {
  2704. var doc = this.document;
  2705. try {
  2706. return !doc ? null : browser.ie9below || txtRange? doc.selection : domUtils.getWindow( doc ).getSelection();
  2707. } catch ( e ) {
  2708. return null;
  2709. }
  2710. },
  2711. /**
  2712. * 获得ieRange
  2713. * @public
  2714. * @function
  2715. * @name UM.dom.Selection.getIERange
  2716. * @return {ieRange} 返回ie原生的Range
  2717. */
  2718. getIERange:function (txtRange) {
  2719. var ieRange = _getIERange( this,txtRange );
  2720. if ( !ieRange || !this.rangeInBody(ieRange,txtRange)) {
  2721. if ( this._bakIERange ) {
  2722. return this._bakIERange;
  2723. }
  2724. }
  2725. return ieRange;
  2726. },
  2727. rangeInBody : function(rng,txtRange){
  2728. var node = browser.ie9below || txtRange ? rng.item ? rng.item() : rng.parentElement() : rng.startContainer;
  2729. return node === this.body || domUtils.inDoc(node,this.body);
  2730. },
  2731. /**
  2732. * 缓存当前选区的range和选区的开始节点
  2733. * @public
  2734. * @function
  2735. * @name UM.dom.Selection.cache
  2736. */
  2737. cache:function () {
  2738. this.clear();
  2739. this._cachedRange = this.getRange();
  2740. this._cachedStartElement = this.getStart();
  2741. this._cachedStartElementPath = this.getStartElementPath();
  2742. },
  2743. getStartElementPath:function () {
  2744. if ( this._cachedStartElementPath ) {
  2745. return this._cachedStartElementPath;
  2746. }
  2747. var start = this.getStart();
  2748. if ( start ) {
  2749. return domUtils.findParents( start, true, null, true )
  2750. }
  2751. return [];
  2752. },
  2753. /**
  2754. * 清空缓存
  2755. * @public
  2756. * @function
  2757. * @name UM.dom.Selection.clear
  2758. */
  2759. clear:function () {
  2760. this._cachedStartElementPath = this._cachedRange = this._cachedStartElement = null;
  2761. },
  2762. /**
  2763. * 编辑器是否得到了选区
  2764. */
  2765. isFocus:function () {
  2766. return this.hasNativeRange()
  2767. },
  2768. /**
  2769. * 获取选区对应的Range
  2770. * @public
  2771. * @function
  2772. * @name UM.dom.Selection.getRange
  2773. * @returns {UM.dom.Range} 得到Range对象
  2774. */
  2775. getRange:function () {
  2776. var me = this;
  2777. function optimze( range ) {
  2778. var child = me.body.firstChild,
  2779. collapsed = range.collapsed;
  2780. while ( child && child.firstChild ) {
  2781. range.setStart( child, 0 );
  2782. child = child.firstChild;
  2783. }
  2784. if ( !range.startContainer ) {
  2785. range.setStart( me.body, 0 )
  2786. }
  2787. if ( collapsed ) {
  2788. range.collapse( true );
  2789. }
  2790. }
  2791. if ( me._cachedRange != null ) {
  2792. return this._cachedRange;
  2793. }
  2794. var range = new dom.Range( me.document,me.body );
  2795. if ( browser.ie9below ) {
  2796. var nativeRange = me.getIERange();
  2797. if ( nativeRange && this.rangeInBody(nativeRange)) {
  2798. try{
  2799. transformIERangeToRange( nativeRange, range );
  2800. }catch(e){
  2801. optimze( range );
  2802. }
  2803. } else {
  2804. optimze( range );
  2805. }
  2806. } else {
  2807. var sel = me.getNative();
  2808. if ( sel && sel.rangeCount && me.rangeInBody(sel.getRangeAt( 0 ))) {
  2809. var firstRange = sel.getRangeAt( 0 );
  2810. var lastRange = sel.getRangeAt( sel.rangeCount - 1 );
  2811. range.setStart( firstRange.startContainer, firstRange.startOffset ).setEnd( lastRange.endContainer, lastRange.endOffset );
  2812. if ( range.collapsed && domUtils.isBody( range.startContainer ) && !range.startOffset ) {
  2813. optimze( range );
  2814. }
  2815. } else {
  2816. //trace:1734 有可能已经不在dom树上了,标识的节点
  2817. if ( this._bakRange && (this._bakRange.startContainer === this.body || domUtils.inDoc( this._bakRange.startContainer, this.body )) ){
  2818. return this._bakRange;
  2819. }
  2820. optimze( range );
  2821. }
  2822. }
  2823. return this._bakRange = range;
  2824. },
  2825. /**
  2826. * 获取开始元素,用于状态反射
  2827. * @public
  2828. * @function
  2829. * @name UM.dom.Selection.getStart
  2830. * @return {Element} 获得开始元素
  2831. */
  2832. getStart:function () {
  2833. if ( this._cachedStartElement ) {
  2834. return this._cachedStartElement;
  2835. }
  2836. var range = browser.ie9below ? this.getIERange() : this.getRange(),
  2837. tmpRange,
  2838. start, tmp, parent;
  2839. if ( browser.ie9below ) {
  2840. if ( !range ) {
  2841. //todo 给第一个值可能会有问题
  2842. return this.document.body.firstChild;
  2843. }
  2844. //control元素
  2845. if ( range.item ){
  2846. return range.item( 0 );
  2847. }
  2848. tmpRange = range.duplicate();
  2849. //修正ie下<b>x</b>[xx] 闭合后 <b>x|</b>xx
  2850. tmpRange.text.length > 0 && tmpRange.moveStart( 'character', 1 );
  2851. tmpRange.collapse( 1 );
  2852. start = tmpRange.parentElement();
  2853. parent = tmp = range.parentElement();
  2854. while ( tmp = tmp.parentNode ) {
  2855. if ( tmp == start ) {
  2856. start = parent;
  2857. break;
  2858. }
  2859. }
  2860. } else {
  2861. start = range.startContainer;
  2862. if ( start.nodeType == 1 && start.hasChildNodes() ){
  2863. start = start.childNodes[Math.min( start.childNodes.length - 1, range.startOffset )];
  2864. }
  2865. if ( start.nodeType == 3 ){
  2866. return start.parentNode;
  2867. }
  2868. }
  2869. return start;
  2870. },
  2871. /**
  2872. * 得到选区中的文本
  2873. * @public
  2874. * @function
  2875. * @name UM.dom.Selection.getText
  2876. * @return {String} 选区中包含的文本
  2877. */
  2878. getText:function () {
  2879. var nativeSel, nativeRange;
  2880. if ( this.isFocus() && (nativeSel = this.getNative()) ) {
  2881. nativeRange = browser.ie9below ? nativeSel.createRange() : nativeSel.getRangeAt( 0 );
  2882. return browser.ie9below ? nativeRange.text : nativeRange.toString();
  2883. }
  2884. return '';
  2885. }
  2886. };
  2887. })();
  2888. /**
  2889. * @file
  2890. * @name UM.Editor
  2891. * @short Editor
  2892. * @import editor.js,core/utils.js,core/EventBase.js,core/browser.js,core/dom/dtd.js,core/dom/domUtils.js,core/dom/Range.js,core/dom/Selection.js,plugins/serialize.js
  2893. * @desc 编辑器主类,包含编辑器提供的大部分公用接口
  2894. */
  2895. (function () {
  2896. var uid = 0, _selectionChangeTimer;
  2897. /**
  2898. * @private
  2899. * @ignore
  2900. * @param form 编辑器所在的form元素
  2901. * @param editor 编辑器实例对象
  2902. */
  2903. function setValue(form, editor) {
  2904. var textarea;
  2905. if (editor.textarea) {
  2906. if (utils.isString(editor.textarea)) {
  2907. for (var i = 0, ti, tis = domUtils.getElementsByTagName(form, 'textarea'); ti = tis[i++];) {
  2908. if (ti.id == 'umeditor_textarea_' + editor.options.textarea) {
  2909. textarea = ti;
  2910. break;
  2911. }
  2912. }
  2913. } else {
  2914. textarea = editor.textarea;
  2915. }
  2916. }
  2917. if (!textarea) {
  2918. form.appendChild(textarea = domUtils.createElement(document, 'textarea', {
  2919. 'name': editor.options.textarea,
  2920. 'id': 'umeditor_textarea_' + editor.options.textarea,
  2921. 'style': "display:none"
  2922. }));
  2923. //不要产生多个textarea
  2924. editor.textarea = textarea;
  2925. }
  2926. textarea.value = editor.hasContents() ?
  2927. (editor.options.allHtmlEnabled ? editor.getAllHtml() : editor.getContent(null, null, true)) :
  2928. ''
  2929. }
  2930. function loadPlugins(me){
  2931. //初始化插件
  2932. for (var pi in UM.plugins) {
  2933. UM.plugins[pi].call(me);
  2934. }
  2935. me.langIsReady = true;
  2936. me.fireEvent("langReady");
  2937. }
  2938. function checkCurLang(I18N){
  2939. for(var lang in I18N){
  2940. return lang
  2941. }
  2942. }
  2943. /**
  2944. * UEditor编辑器类
  2945. * @name Editor
  2946. * @desc 创建一个跟编辑器实例
  2947. * - ***container*** 编辑器容器对象
  2948. * - ***iframe*** 编辑区域所在的iframe对象
  2949. * - ***window*** 编辑区域所在的window
  2950. * - ***document*** 编辑区域所在的document对象
  2951. * - ***body*** 编辑区域所在的body对象
  2952. * - ***selection*** 编辑区域的选区对象
  2953. */
  2954. var Editor = UM.Editor = function (options) {
  2955. var me = this;
  2956. me.uid = uid++;
  2957. EventBase.call(me);
  2958. me.commands = {};
  2959. me.options = utils.extend(utils.clone(options || {}), UMEDITOR_CONFIG, true);
  2960. me.shortcutkeys = {};
  2961. me.inputRules = [];
  2962. me.outputRules = [];
  2963. //设置默认的常用属性
  2964. me.setOpt({
  2965. isShow: true,
  2966. initialContent: '',
  2967. initialStyle:'',
  2968. autoClearinitialContent: false,
  2969. textarea: 'editorValue',
  2970. focus: false,
  2971. focusInEnd: true,
  2972. autoClearEmptyNode: true,
  2973. fullscreen: false,
  2974. readonly: false,
  2975. zIndex: 999,
  2976. enterTag: 'p',
  2977. lang: 'zh-cn',
  2978. langPath: me.options.UMEDITOR_HOME_URL + 'lang/',
  2979. theme: 'default',
  2980. themePath: me.options.UMEDITOR_HOME_URL + 'themes/',
  2981. allHtmlEnabled: false,
  2982. autoSyncData : true,
  2983. autoHeightEnabled : true
  2984. });
  2985. if(!utils.isEmptyObject(UM.I18N)){
  2986. //修改默认的语言类型
  2987. me.options.lang = checkCurLang(UM.I18N);
  2988. loadPlugins(me)
  2989. }else{
  2990. utils.loadFile(document, {
  2991. src: me.options.langPath + me.options.lang + "/" + me.options.lang + ".js",
  2992. tag: "script",
  2993. type: "text/javascript",
  2994. defer: "defer"
  2995. }, function () {
  2996. loadPlugins(me)
  2997. });
  2998. }
  2999. };
  3000. Editor.prototype = {
  3001. /**
  3002. * 当编辑器ready后执行传入的fn,如果编辑器已经完成ready,就马上执行fn,fn的中的this是编辑器实例。
  3003. * 大部分的实例接口都需要放在该方法内部执行,否则在IE下可能会报错。
  3004. * @name ready
  3005. * @grammar editor.ready(fn) fn是当编辑器渲染好后执行的function
  3006. * @example
  3007. * var editor = new UM.ui.Editor();
  3008. * editor.render("myEditor");
  3009. * editor.ready(function(){
  3010. * editor.setContent("欢迎使用UEditor!");
  3011. * })
  3012. */
  3013. ready: function (fn) {
  3014. var me = this;
  3015. if (fn) {
  3016. me.isReady ? fn.apply(me) : me.addListener('ready', fn);
  3017. }
  3018. },
  3019. /**
  3020. * 为编辑器设置默认参数值。若用户配置为空,则以默认配置为准
  3021. * @grammar editor.setOpt(key,value); //传入一个键、值对
  3022. * @grammar editor.setOpt({ key:value}); //传入一个json对象
  3023. */
  3024. setOpt: function (key, val) {
  3025. var obj = {};
  3026. if (utils.isString(key)) {
  3027. obj[key] = val
  3028. } else {
  3029. obj = key;
  3030. }
  3031. utils.extend(this.options, obj, true);
  3032. },
  3033. getOpt:function(key){
  3034. return this.options[key] || ''
  3035. },
  3036. /**
  3037. * 销毁编辑器实例对象
  3038. * @name destroy
  3039. * @grammar editor.destroy();
  3040. */
  3041. destroy: function () {
  3042. var me = this;
  3043. me.fireEvent('destroy');
  3044. var container = me.container.parentNode;
  3045. if(container === document.body){
  3046. container = me.container;
  3047. }
  3048. var textarea = me.textarea;
  3049. if (!textarea) {
  3050. textarea = document.createElement('textarea');
  3051. container.parentNode.insertBefore(textarea, container);
  3052. } else {
  3053. textarea.style.display = ''
  3054. }
  3055. textarea.style.width = me.body.offsetWidth + 'px';
  3056. textarea.style.height = me.body.offsetHeight + 'px';
  3057. textarea.value = me.getContent();
  3058. textarea.id = me.key;
  3059. if(container.contains(textarea)){
  3060. $(textarea).insertBefore(container);
  3061. }
  3062. container.innerHTML = '';
  3063. domUtils.remove(container);
  3064. UM.clearCache(me.id);
  3065. //trace:2004
  3066. for (var p in me) {
  3067. if (me.hasOwnProperty(p)) {
  3068. delete this[p];
  3069. }
  3070. }
  3071. },
  3072. initialCont : function(holder){
  3073. if(holder){
  3074. holder.getAttribute('name') && ( this.options.textarea = holder.getAttribute('name'));
  3075. if (holder && /script|textarea/ig.test(holder.tagName)) {
  3076. var newDiv = document.createElement('div');
  3077. holder.parentNode.insertBefore(newDiv, holder);
  3078. this.options.initialContent = UM.htmlparser(holder.value || holder.innerHTML|| this.options.initialContent).toHtml();
  3079. holder.className && (newDiv.className = holder.className);
  3080. holder.style.cssText && (newDiv.style.cssText = holder.style.cssText);
  3081. if (/textarea/i.test(holder.tagName)) {
  3082. this.textarea = holder;
  3083. this.textarea.style.display = 'none';
  3084. } else {
  3085. holder.parentNode.removeChild(holder);
  3086. holder.id && (newDiv.id = holder.id);
  3087. }
  3088. holder = newDiv;
  3089. holder.innerHTML = '';
  3090. }
  3091. return holder;
  3092. }else{
  3093. return null;
  3094. }
  3095. },
  3096. /**
  3097. * 渲染编辑器的DOM到指定容器,必须且只能调用一次
  3098. * @name render
  3099. * @grammar editor.render(containerId); //可以指定一个容器ID
  3100. * @grammar editor.render(containerDom); //也可以直接指定容器对象
  3101. */
  3102. render: function (container) {
  3103. var me = this,
  3104. options = me.options,
  3105. getStyleValue=function(attr){
  3106. return parseInt($(container).css(attr));
  3107. };
  3108. if (utils.isString(container)) {
  3109. container = document.getElementById(container);
  3110. }
  3111. if (container) {
  3112. this.id = container.getAttribute('id');
  3113. UM.setEditor(this);
  3114. utils.cssRule('umeditor_body_css',me.options.initialStyle,document);
  3115. container = this.initialCont(container);
  3116. container.className += ' edui-body-container';
  3117. if(options.initialFrameWidth){
  3118. options.minFrameWidth = options.initialFrameWidth
  3119. }else{
  3120. //都没给值,先写死了
  3121. options.minFrameWidth = options.initialFrameWidth = $(container).width() || UM.defaultWidth;
  3122. }
  3123. if(options.initialFrameHeight){
  3124. options.minFrameHeight = options.initialFrameHeight
  3125. }else{
  3126. options.initialFrameHeight = options.minFrameHeight = $(container).height() || UM.defaultHeight;
  3127. }
  3128. container.style.width = /%$/.test(options.initialFrameWidth) ? '100%' : options.initialFrameWidth -
  3129. getStyleValue("padding-left")-
  3130. getStyleValue("padding-right") +'px';
  3131. var height = /%$/.test(options.initialFrameHeight) ? '100%' : (options.initialFrameHeight - getStyleValue("padding-top")- getStyleValue("padding-bottom") );
  3132. if(this.options.autoHeightEnabled){
  3133. container.style.minHeight = height +'px';
  3134. container.style.height = '';
  3135. if(browser.ie && browser.version <= 6){
  3136. container.style.height = height ;
  3137. container.style.setExpression('height', 'this.scrollHeight <= ' + height + ' ? "' + height + 'px" : "auto"');
  3138. }
  3139. }else{
  3140. $(container).height(height)
  3141. }
  3142. container.style.zIndex = options.zIndex;
  3143. this._setup(container);
  3144. }
  3145. },
  3146. /**
  3147. * 编辑器初始化
  3148. * @private
  3149. * @ignore
  3150. * @param {Element} doc 编辑器Iframe中的文档对象
  3151. */
  3152. _setup: function (cont) {
  3153. var me = this,
  3154. options = me.options;
  3155. cont.contentEditable = true;
  3156. document.body.spellcheck = false;
  3157. me.document = document;
  3158. me.window = document.defaultView || document.parentWindow;
  3159. me.body = cont;
  3160. me.$body = $(cont);
  3161. //扩展isBody方法
  3162. domUtils.isBody = function (node) {
  3163. return node === cont;
  3164. };
  3165. me.selection = new dom.Selection(document,me.body);
  3166. //gecko初始化就能得到range,无法判断isFocus了
  3167. var geckoSel;
  3168. if (browser.gecko && (geckoSel = this.selection.getNative())) {
  3169. geckoSel.removeAllRanges();
  3170. }
  3171. this._initEvents();
  3172. //为form提交提供一个隐藏的textarea
  3173. for (var form = cont.parentNode; form && !domUtils.isBody(form); form = form.parentNode) {
  3174. if (form.tagName == 'FORM') {
  3175. me.form = form;
  3176. if(me.options.autoSyncData){
  3177. domUtils.on(cont,'blur',function(){
  3178. setValue(form,me);
  3179. });
  3180. }else{
  3181. domUtils.on(form, 'submit', function () {
  3182. setValue(this, me);
  3183. });
  3184. }
  3185. break;
  3186. }
  3187. }
  3188. if (options.initialContent) {
  3189. if (options.autoClearinitialContent) {
  3190. var oldExecCommand = me.execCommand;
  3191. me.execCommand = function () {
  3192. me.fireEvent('firstBeforeExecCommand');
  3193. return oldExecCommand.apply(me, arguments);
  3194. };
  3195. this._setDefaultContent(options.initialContent);
  3196. } else
  3197. this.setContent(options.initialContent, false, true);
  3198. }
  3199. //编辑器不能为空内容
  3200. if (domUtils.isEmptyNode(me.body)) {
  3201. me.body.innerHTML = '<p>' + (browser.ie ? '' : '<br/>') + '</p>';
  3202. }
  3203. //如果要求focus, 就把光标定位到内容开始
  3204. if (options.focus) {
  3205. setTimeout(function () {
  3206. me.focus(me.options.focusInEnd);
  3207. //如果自动清除开着,就不需要做selectionchange;
  3208. !me.options.autoClearinitialContent && me._selectionChange();
  3209. }, 0);
  3210. }
  3211. if (!me.container) {
  3212. me.container = cont.parentNode;
  3213. }
  3214. me._bindshortcutKeys();
  3215. me.isReady = 1;
  3216. me.fireEvent('ready');
  3217. options.onready && options.onready.call(me);
  3218. if(!browser.ie || browser.ie9above){
  3219. domUtils.on(me.body, ['blur', 'focus'], function (e) {
  3220. var nSel = me.selection.getNative();
  3221. //chrome下会出现alt+tab切换时,导致选区位置不对
  3222. if (e.type == 'blur') {
  3223. if(nSel.rangeCount > 0 ){
  3224. me._bakRange = nSel.getRangeAt(0);
  3225. }
  3226. } else {
  3227. try {
  3228. me._bakRange && nSel.addRange(me._bakRange)
  3229. } catch (e) {
  3230. }
  3231. me._bakRange = null;
  3232. }
  3233. });
  3234. }
  3235. !options.isShow && me.setHide();
  3236. options.readonly && me.setDisabled();
  3237. },
  3238. /**
  3239. * 同步编辑器的数据,为提交数据做准备,主要用于你是手动提交的情况
  3240. * @name sync
  3241. * @grammar editor.sync(); //从编辑器的容器向上查找,如果找到就同步数据
  3242. * @grammar editor.sync(formID); //formID制定一个要同步数据的form的id,编辑器的数据会同步到你指定form下
  3243. * @desc
  3244. * 后台取得数据得键值使用你容器上得''name''属性,如果没有就使用参数传入的''textarea''
  3245. * @example
  3246. * editor.sync();
  3247. * form.sumbit(); //form变量已经指向了form元素
  3248. *
  3249. */
  3250. sync: function (formId) {
  3251. var me = this,
  3252. form = formId ? document.getElementById(formId) :
  3253. domUtils.findParent(me.body.parentNode, function (node) {
  3254. return node.tagName == 'FORM'
  3255. }, true);
  3256. form && setValue(form, me);
  3257. },
  3258. /**
  3259. * 设置编辑器高度
  3260. * @name setHeight
  3261. * @grammar editor.setHeight(number); //纯数值,不带单位
  3262. */
  3263. setHeight: function (height,notSetHeight) {
  3264. !notSetHeight && (this.options.initialFrameHeight = height);
  3265. if(this.options.autoHeightEnabled){
  3266. $(this.body).css({
  3267. 'min-height':height + 'px'
  3268. });
  3269. if(browser.ie && browser.version <= 6 && this.container){
  3270. this.container.style.height = height ;
  3271. this.container.style.setExpression('height', 'this.scrollHeight <= ' + height + ' ? "' + height + 'px" : "auto"');
  3272. }
  3273. }else{
  3274. $(this.body).height(height)
  3275. }
  3276. },
  3277. setWidth:function(width){
  3278. this.$container && this.$container.width(width);
  3279. $(this.body).width(width - $(this.body).css('padding-left').replace('px','') * 1 - $(this.body).css('padding-right').replace('px','') * 1)
  3280. },
  3281. addshortcutkey: function (cmd, keys) {
  3282. var obj = {};
  3283. if (keys) {
  3284. obj[cmd] = keys
  3285. } else {
  3286. obj = cmd;
  3287. }
  3288. utils.extend(this.shortcutkeys, obj)
  3289. },
  3290. _bindshortcutKeys: function () {
  3291. var me = this, shortcutkeys = this.shortcutkeys;
  3292. me.addListener('keydown', function (type, e) {
  3293. var keyCode = e.keyCode || e.which;
  3294. for (var i in shortcutkeys) {
  3295. var tmp = shortcutkeys[i].split(',');
  3296. for (var t = 0, ti; ti = tmp[t++];) {
  3297. ti = ti.split(':');
  3298. var key = ti[0], param = ti[1];
  3299. if (/^(ctrl)(\+shift)?\+(\d+)$/.test(key.toLowerCase()) || /^(\d+)$/.test(key)) {
  3300. if (( (RegExp.$1 == 'ctrl' ? (e.ctrlKey || e.metaKey) : 0)
  3301. && (RegExp.$2 != "" ? e[RegExp.$2.slice(1) + "Key"] : 1)
  3302. && keyCode == RegExp.$3
  3303. ) ||
  3304. keyCode == RegExp.$1
  3305. ) {
  3306. if (me.queryCommandState(i,param) != -1)
  3307. me.execCommand(i, param);
  3308. domUtils.preventDefault(e);
  3309. }
  3310. }
  3311. }
  3312. }
  3313. });
  3314. },
  3315. /**
  3316. * 获取编辑器内容
  3317. * @name getContent
  3318. * @grammar editor.getContent() => String //若编辑器中只包含字符"&lt;p&gt;&lt;br /&gt;&lt;/p/&gt;"会返回空。
  3319. * @grammar editor.getContent(fn) => String
  3320. * @example
  3321. * getContent默认是会现调用hasContents来判断编辑器是否为空,如果是,就直接返回空字符串
  3322. * 你也可以传入一个fn来接替hasContents的工作,定制判断的规则
  3323. * editor.getContent(function(){
  3324. * return false //编辑器没有内容 ,getContent直接返回空
  3325. * })
  3326. */
  3327. getContent: function (cmd, fn,notSetCursor,ignoreBlank,formatter) {
  3328. var me = this;
  3329. if (cmd && utils.isFunction(cmd)) {
  3330. fn = cmd;
  3331. cmd = '';
  3332. }
  3333. if (fn ? !fn() : !this.hasContents()) {
  3334. return '';
  3335. }
  3336. me.fireEvent('beforegetcontent');
  3337. var root = UM.htmlparser(me.body.innerHTML,ignoreBlank);
  3338. me.filterOutputRule(root);
  3339. me.fireEvent('aftergetcontent',root);
  3340. return root.toHtml(formatter);
  3341. },
  3342. /**
  3343. * 取得完整的html代码,可以直接显示成完整的html文档
  3344. * @name getAllHtml
  3345. * @grammar editor.getAllHtml() => String
  3346. */
  3347. getAllHtml: function () {
  3348. var me = this,
  3349. headHtml = [],
  3350. html = '';
  3351. me.fireEvent('getAllHtml', headHtml);
  3352. if (browser.ie && browser.version > 8) {
  3353. var headHtmlForIE9 = '';
  3354. utils.each(me.document.styleSheets, function (si) {
  3355. headHtmlForIE9 += ( si.href ? '<link rel="stylesheet" type="text/css" href="' + si.href + '" />' : '<style>' + si.cssText + '</style>');
  3356. });
  3357. utils.each(me.document.getElementsByTagName('script'), function (si) {
  3358. headHtmlForIE9 += si.outerHTML;
  3359. });
  3360. }
  3361. return '<html><head>' + (me.options.charset ? '<meta http-equiv="Content-Type" content="text/html; charset=' + me.options.charset + '"/>' : '')
  3362. + (headHtmlForIE9 || me.document.getElementsByTagName('head')[0].innerHTML) + headHtml.join('\n') + '</head>'
  3363. + '<body ' + (ie && browser.version < 9 ? 'class="view"' : '') + '>' + me.getContent(null, null, true) + '</body></html>';
  3364. },
  3365. /**
  3366. * 得到编辑器的纯文本内容,但会保留段落格式
  3367. * @name getPlainTxt
  3368. * @grammar editor.getPlainTxt() => String
  3369. */
  3370. getPlainTxt: function () {
  3371. var reg = new RegExp(domUtils.fillChar, 'g'),
  3372. html = this.body.innerHTML.replace(/[\n\r]/g, '');//ie要先去了\n在处理
  3373. html = html.replace(/<(p|div)[^>]*>(<br\/?>|&nbsp;)<\/\1>/gi, '\n')
  3374. .replace(/<br\/?>/gi, '\n')
  3375. .replace(/<[^>/]+>/g, '')
  3376. .replace(/(\n)?<\/([^>]+)>/g, function (a, b, c) {
  3377. return dtd.$block[c] ? '\n' : b ? b : '';
  3378. });
  3379. //取出来的空格会有c2a0会变成乱码,处理这种情况\u00a0
  3380. return html.replace(reg, '').replace(/\u00a0/g, ' ').replace(/&nbsp;/g, ' ');
  3381. },
  3382. /**
  3383. * 获取编辑器中的纯文本内容,没有段落格式
  3384. * @name getContentTxt
  3385. * @grammar editor.getContentTxt() => String
  3386. */
  3387. getContentTxt: function () {
  3388. var reg = new RegExp(domUtils.fillChar, 'g');
  3389. //取出来的空格会有c2a0会变成乱码,处理这种情况\u00a0
  3390. return this.body[browser.ie ? 'innerText' : 'textContent'].replace(reg, '').replace(/\u00a0/g, ' ');
  3391. },
  3392. /**
  3393. * 将html设置到编辑器中, 如果是用于初始化时给编辑器赋初值,则必须放在ready方法内部执行
  3394. * @name setContent
  3395. * @grammar editor.setContent(html)
  3396. * @example
  3397. * var editor = new UM.ui.Editor()
  3398. * editor.ready(function(){
  3399. * //需要ready后执行,否则可能报错
  3400. * editor.setContent("欢迎使用UEditor!");
  3401. * })
  3402. */
  3403. setContent: function (html, isAppendTo, notFireSelectionchange) {
  3404. var me = this;
  3405. me.fireEvent('beforesetcontent', html);
  3406. var root = UM.htmlparser(html);
  3407. me.filterInputRule(root);
  3408. html = root.toHtml();
  3409. me.body.innerHTML = (isAppendTo ? me.body.innerHTML : '') + html;
  3410. function isCdataDiv(node){
  3411. return node.tagName == 'DIV' && node.getAttribute('cdata_tag');
  3412. }
  3413. //给文本或者inline节点套p标签
  3414. if (me.options.enterTag == 'p') {
  3415. var child = this.body.firstChild, tmpNode;
  3416. if (!child || child.nodeType == 1 &&
  3417. (dtd.$cdata[child.tagName] || isCdataDiv(child) ||
  3418. domUtils.isCustomeNode(child)
  3419. )
  3420. && child === this.body.lastChild) {
  3421. this.body.innerHTML = '<p>' + (browser.ie ? '&nbsp;' : '<br/>') + '</p>' + this.body.innerHTML;
  3422. } else {
  3423. var p = me.document.createElement('p');
  3424. while (child) {
  3425. while (child && (child.nodeType == 3 || child.nodeType == 1 && dtd.p[child.tagName] && !dtd.$cdata[child.tagName])) {
  3426. tmpNode = child.nextSibling;
  3427. p.appendChild(child);
  3428. child = tmpNode;
  3429. }
  3430. if (p.firstChild) {
  3431. if (!child) {
  3432. me.body.appendChild(p);
  3433. break;
  3434. } else {
  3435. child.parentNode.insertBefore(p, child);
  3436. p = me.document.createElement('p');
  3437. }
  3438. }
  3439. child = child.nextSibling;
  3440. }
  3441. }
  3442. }
  3443. me.fireEvent('aftersetcontent');
  3444. me.fireEvent('contentchange');
  3445. !notFireSelectionchange && me._selectionChange();
  3446. //清除保存的选区
  3447. me._bakRange = me._bakIERange = me._bakNativeRange = null;
  3448. //trace:1742 setContent后gecko能得到焦点问题
  3449. var geckoSel;
  3450. if (browser.gecko && (geckoSel = this.selection.getNative())) {
  3451. geckoSel.removeAllRanges();
  3452. }
  3453. if(me.options.autoSyncData){
  3454. me.form && setValue(me.form,me);
  3455. }
  3456. },
  3457. /**
  3458. * 让编辑器获得焦点,toEnd确定focus位置
  3459. * @name focus
  3460. * @grammar editor.focus([toEnd]) //默认focus到编辑器头部,toEnd为true时focus到内容尾部
  3461. */
  3462. focus: function (toEnd) {
  3463. try {
  3464. var me = this,
  3465. rng = me.selection.getRange();
  3466. if (toEnd) {
  3467. rng.setStartAtLast(me.body.lastChild).setCursor(false, true);
  3468. } else {
  3469. rng.select(true);
  3470. }
  3471. this.fireEvent('focus');
  3472. } catch (e) {
  3473. }
  3474. },
  3475. /**
  3476. * 使编辑区域失去焦点
  3477. */
  3478. blur:function(){
  3479. var sel = this.selection.getNative();
  3480. sel.empty ? sel.empty() : sel.removeAllRanges();
  3481. this.fireEvent('blur')
  3482. },
  3483. /**
  3484. * 判断编辑器当前是否获得了焦点
  3485. */
  3486. isFocus : function(){
  3487. return this.selection.isFocus()
  3488. },
  3489. /**
  3490. * 初始化UE事件及部分事件代理
  3491. * @private
  3492. * @ignore
  3493. */
  3494. _initEvents: function () {
  3495. var me = this,
  3496. cont = me.body;
  3497. me._proxyDomEvent = utils.bind(me._proxyDomEvent, me);
  3498. domUtils.on(cont, ['click', 'contextmenu', 'mousedown', 'keydown', 'keyup', 'keypress', 'mouseup', 'mouseover', 'mouseout', 'selectstart'], me._proxyDomEvent);
  3499. domUtils.on(cont, ['focus', 'blur'], me._proxyDomEvent);
  3500. domUtils.on(cont, ['mouseup', 'keydown'], function (evt) {
  3501. //特殊键不触发selectionchange
  3502. if (evt.type == 'keydown' && (evt.ctrlKey || evt.metaKey || evt.shiftKey || evt.altKey)) {
  3503. return;
  3504. }
  3505. if (evt.button == 2)return;
  3506. me._selectionChange(250, evt);
  3507. });
  3508. },
  3509. /**
  3510. * 触发事件代理
  3511. * @private
  3512. * @ignore
  3513. */
  3514. _proxyDomEvent: function (evt) {
  3515. return this.fireEvent(evt.type.replace(/^on/, ''), evt);
  3516. },
  3517. /**
  3518. * 变化选区
  3519. * @private
  3520. * @ignore
  3521. */
  3522. _selectionChange: function (delay, evt) {
  3523. var me = this;
  3524. //有光标才做selectionchange 为了解决未focus时点击source不能触发更改工具栏状态的问题(source命令notNeedUndo=1)
  3525. // if ( !me.selection.isFocus() ){
  3526. // return;
  3527. // }
  3528. var hackForMouseUp = false;
  3529. var mouseX, mouseY;
  3530. if (browser.ie && browser.version < 9 && evt && evt.type == 'mouseup') {
  3531. var range = this.selection.getRange();
  3532. if (!range.collapsed) {
  3533. hackForMouseUp = true;
  3534. mouseX = evt.clientX;
  3535. mouseY = evt.clientY;
  3536. }
  3537. }
  3538. clearTimeout(_selectionChangeTimer);
  3539. _selectionChangeTimer = setTimeout(function () {
  3540. if (!me.selection.getNative()) {
  3541. return;
  3542. }
  3543. //修复一个IE下的bug: 鼠标点击一段已选择的文本中间时,可能在mouseup后的一段时间内取到的range是在selection的type为None下的错误值.
  3544. //IE下如果用户是拖拽一段已选择文本,则不会触发mouseup事件,所以这里的特殊处理不会对其有影响
  3545. var ieRange;
  3546. if (hackForMouseUp && me.selection.getNative().type == 'None') {
  3547. ieRange = me.document.body.createTextRange();
  3548. try {
  3549. ieRange.moveToPoint(mouseX, mouseY);
  3550. } catch (ex) {
  3551. ieRange = null;
  3552. }
  3553. }
  3554. var bakGetIERange;
  3555. if (ieRange) {
  3556. bakGetIERange = me.selection.getIERange;
  3557. me.selection.getIERange = function () {
  3558. return ieRange;
  3559. };
  3560. }
  3561. me.selection.cache();
  3562. if (bakGetIERange) {
  3563. me.selection.getIERange = bakGetIERange;
  3564. }
  3565. if (me.selection._cachedRange && me.selection._cachedStartElement) {
  3566. me.fireEvent('beforeselectionchange');
  3567. // 第二个参数causeByUi为true代表由用户交互造成的selectionchange.
  3568. me.fireEvent('selectionchange', !!evt);
  3569. me.fireEvent('afterselectionchange');
  3570. me.selection.clear();
  3571. }
  3572. }, delay || 50);
  3573. },
  3574. _callCmdFn: function (fnName, args) {
  3575. args = Array.prototype.slice.call(args,0);
  3576. var cmdName = args.shift().toLowerCase(),
  3577. cmd, cmdFn;
  3578. cmd = this.commands[cmdName] || UM.commands[cmdName];
  3579. cmdFn = cmd && cmd[fnName];
  3580. //没有querycommandstate或者没有command的都默认返回0
  3581. if ((!cmd || !cmdFn) && fnName == 'queryCommandState') {
  3582. return 0;
  3583. } else if (cmdFn) {
  3584. return cmdFn.apply(this, [cmdName].concat(args));
  3585. }
  3586. },
  3587. /**
  3588. * 执行编辑命令cmdName,完成富文本编辑效果
  3589. * @name execCommand
  3590. * @grammar editor.execCommand(cmdName) => {*}
  3591. */
  3592. execCommand: function (cmdName) {
  3593. if(!this.isFocus()){
  3594. var bakRange = this.selection._bakRange;
  3595. if(bakRange){
  3596. bakRange.select()
  3597. }else{
  3598. this.focus(true)
  3599. }
  3600. }
  3601. cmdName = cmdName.toLowerCase();
  3602. var me = this,
  3603. result,
  3604. cmd = me.commands[cmdName] || UM.commands[cmdName];
  3605. if (!cmd || !cmd.execCommand) {
  3606. return null;
  3607. }
  3608. if (!cmd.notNeedUndo && !me.__hasEnterExecCommand) {
  3609. me.__hasEnterExecCommand = true;
  3610. if (me.queryCommandState.apply(me,arguments) != -1) {
  3611. me.fireEvent('beforeexeccommand', cmdName);
  3612. result = this._callCmdFn('execCommand', arguments);
  3613. !me._ignoreContentChange && me.fireEvent('contentchange');
  3614. me.fireEvent('afterexeccommand', cmdName);
  3615. }
  3616. me.__hasEnterExecCommand = false;
  3617. } else {
  3618. result = this._callCmdFn('execCommand', arguments);
  3619. !me._ignoreContentChange && me.fireEvent('contentchange')
  3620. }
  3621. !me._ignoreContentChange && me._selectionChange();
  3622. return result;
  3623. },
  3624. /**
  3625. * 根据传入的command命令,查选编辑器当前的选区,返回命令的状态
  3626. * @name queryCommandState
  3627. * @grammar editor.queryCommandState(cmdName) => (-1|0|1)
  3628. * @desc
  3629. * * ''-1'' 当前命令不可用
  3630. * * ''0'' 当前命令可用
  3631. * * ''1'' 当前命令已经执行过了
  3632. */
  3633. queryCommandState: function (cmdName) {
  3634. try{
  3635. return this._callCmdFn('queryCommandState', arguments);
  3636. }catch(e){
  3637. return 0
  3638. }
  3639. },
  3640. /**
  3641. * 根据传入的command命令,查选编辑器当前的选区,根据命令返回相关的值
  3642. * @name queryCommandValue
  3643. * @grammar editor.queryCommandValue(cmdName) => {*}
  3644. */
  3645. queryCommandValue: function (cmdName) {
  3646. try{
  3647. return this._callCmdFn('queryCommandValue', arguments);
  3648. }catch(e){
  3649. return null
  3650. }
  3651. },
  3652. /**
  3653. * 检查编辑区域中是否有内容,若包含tags中的节点类型,直接返回true
  3654. * @name hasContents
  3655. * @desc
  3656. * 默认有文本内容,或者有以下节点都不认为是空
  3657. * <code>{table:1,ul:1,ol:1,dl:1,iframe:1,area:1,base:1,col:1,hr:1,img:1,embed:1,input:1,link:1,meta:1,param:1}</code>
  3658. * @grammar editor.hasContents() => (true|false)
  3659. * @grammar editor.hasContents(tags) => (true|false) //若文档中包含tags数组里对应的tag,直接返回true
  3660. * @example
  3661. * editor.hasContents(['span']) //如果编辑器里有这些,不认为是空
  3662. */
  3663. hasContents: function (tags) {
  3664. if (tags) {
  3665. for (var i = 0, ci; ci = tags[i++];) {
  3666. if (this.body.getElementsByTagName(ci).length > 0) {
  3667. return true;
  3668. }
  3669. }
  3670. }
  3671. if (!domUtils.isEmptyBlock(this.body)) {
  3672. return true
  3673. }
  3674. //随时添加,定义的特殊标签如果存在,不能认为是空
  3675. tags = ['div'];
  3676. for (i = 0; ci = tags[i++];) {
  3677. var nodes = domUtils.getElementsByTagName(this.body, ci);
  3678. for (var n = 0, cn; cn = nodes[n++];) {
  3679. if (domUtils.isCustomeNode(cn)) {
  3680. return true;
  3681. }
  3682. }
  3683. }
  3684. return false;
  3685. },
  3686. /**
  3687. * 重置编辑器,可用来做多个tab使用同一个编辑器实例
  3688. * @name reset
  3689. * @desc
  3690. * * 清空编辑器内容
  3691. * * 清空回退列表
  3692. * @grammar editor.reset()
  3693. */
  3694. reset: function () {
  3695. this.fireEvent('reset');
  3696. },
  3697. setEnabled: function () {
  3698. var me = this, range;
  3699. if (me.body.contentEditable == 'false') {
  3700. me.body.contentEditable = true;
  3701. range = me.selection.getRange();
  3702. //有可能内容丢失了
  3703. try {
  3704. range.moveToBookmark(me.lastBk);
  3705. delete me.lastBk
  3706. } catch (e) {
  3707. range.setStartAtFirst(me.body).collapse(true)
  3708. }
  3709. range.select(true);
  3710. if (me.bkqueryCommandState) {
  3711. me.queryCommandState = me.bkqueryCommandState;
  3712. delete me.bkqueryCommandState;
  3713. }
  3714. me.fireEvent('selectionchange');
  3715. }
  3716. },
  3717. /**
  3718. * 设置当前编辑区域可以编辑
  3719. * @name enable
  3720. * @grammar editor.enable()
  3721. */
  3722. enable: function () {
  3723. return this.setEnabled();
  3724. },
  3725. setDisabled: function (except) {
  3726. var me = this;
  3727. except = except ? utils.isArray(except) ? except : [except] : [];
  3728. if (me.body.contentEditable == 'true') {
  3729. if (!me.lastBk) {
  3730. me.lastBk = me.selection.getRange().createBookmark(true);
  3731. }
  3732. me.body.contentEditable = false;
  3733. me.bkqueryCommandState = me.queryCommandState;
  3734. me.queryCommandState = function (type) {
  3735. if (utils.indexOf(except, type) != -1) {
  3736. return me.bkqueryCommandState.apply(me, arguments);
  3737. }
  3738. return -1;
  3739. };
  3740. me.fireEvent('selectionchange');
  3741. }
  3742. },
  3743. /** 设置当前编辑区域不可编辑,except中的命令除外
  3744. * @name disable
  3745. * @grammar editor.disable()
  3746. * @grammar editor.disable(except) //例外的命令,也即即使设置了disable,此处配置的命令仍然可以执行
  3747. * @example
  3748. * //禁用工具栏中除加粗和插入图片之外的所有功能
  3749. * editor.disable(['bold','insertimage']);//可以是单一的String,也可以是Array
  3750. */
  3751. disable: function (except) {
  3752. return this.setDisabled(except);
  3753. },
  3754. /**
  3755. * 设置默认内容
  3756. * @ignore
  3757. * @private
  3758. * @param {String} cont 要存入的内容
  3759. */
  3760. _setDefaultContent: function () {
  3761. function clear() {
  3762. var me = this;
  3763. if (me.document.getElementById('initContent')) {
  3764. me.body.innerHTML = '<p>' + (ie ? '' : '<br/>') + '</p>';
  3765. me.removeListener('firstBeforeExecCommand focus', clear);
  3766. setTimeout(function () {
  3767. me.focus();
  3768. me._selectionChange();
  3769. }, 0)
  3770. }
  3771. }
  3772. return function (cont) {
  3773. var me = this;
  3774. me.body.innerHTML = '<p id="initContent">' + cont + '</p>';
  3775. me.addListener('firstBeforeExecCommand focus', clear);
  3776. }
  3777. }(),
  3778. /**
  3779. * show方法的兼容版本
  3780. * @private
  3781. * @ignore
  3782. */
  3783. setShow: function () {
  3784. var me = this, range = me.selection.getRange();
  3785. if (me.container.style.display == 'none') {
  3786. //有可能内容丢失了
  3787. try {
  3788. range.moveToBookmark(me.lastBk);
  3789. delete me.lastBk
  3790. } catch (e) {
  3791. range.setStartAtFirst(me.body).collapse(true)
  3792. }
  3793. //ie下focus实效,所以做了个延迟
  3794. setTimeout(function () {
  3795. range.select(true);
  3796. }, 100);
  3797. me.container.style.display = '';
  3798. }
  3799. },
  3800. /**
  3801. * 显示编辑器
  3802. * @name show
  3803. * @grammar editor.show()
  3804. */
  3805. show: function () {
  3806. return this.setShow();
  3807. },
  3808. /**
  3809. * hide方法的兼容版本
  3810. * @private
  3811. * @ignore
  3812. */
  3813. setHide: function () {
  3814. var me = this;
  3815. if (!me.lastBk) {
  3816. me.lastBk = me.selection.getRange().createBookmark(true);
  3817. }
  3818. me.container.style.display = 'none'
  3819. },
  3820. /**
  3821. * 隐藏编辑器
  3822. * @name hide
  3823. * @grammar editor.hide()
  3824. */
  3825. hide: function () {
  3826. return this.setHide();
  3827. },
  3828. /**
  3829. * 根据制定的路径,获取对应的语言资源
  3830. * @name getLang
  3831. * @grammar editor.getLang(path) => (JSON|String) 路径根据的是lang目录下的语言文件的路径结构
  3832. * @example
  3833. * editor.getLang('contextMenu.delete') //如果当前是中文,那返回是的是删除
  3834. */
  3835. getLang: function (path) {
  3836. var lang = UM.I18N[this.options.lang];
  3837. if (!lang) {
  3838. throw Error("not import language file");
  3839. }
  3840. path = (path || "").split(".");
  3841. for (var i = 0, ci; ci = path[i++];) {
  3842. lang = lang[ci];
  3843. if (!lang)break;
  3844. }
  3845. return lang;
  3846. },
  3847. /**
  3848. * 计算编辑器当前内容的长度
  3849. * @name getContentLength
  3850. * @grammar editor.getContentLength(ingoneHtml,tagNames) =>
  3851. * @example
  3852. * editor.getLang(true)
  3853. */
  3854. getContentLength: function (ingoneHtml, tagNames) {
  3855. var count = this.getContent(false,false,true).length;
  3856. if (ingoneHtml) {
  3857. tagNames = (tagNames || []).concat([ 'hr', 'img', 'iframe']);
  3858. count = this.getContentTxt().replace(/[\t\r\n]+/g, '').length;
  3859. for (var i = 0, ci; ci = tagNames[i++];) {
  3860. count += this.body.getElementsByTagName(ci).length;
  3861. }
  3862. }
  3863. return count;
  3864. },
  3865. addInputRule: function (rule,ignoreUndo) {
  3866. rule.ignoreUndo = ignoreUndo;
  3867. this.inputRules.push(rule);
  3868. },
  3869. filterInputRule: function (root,isUndoLoad) {
  3870. for (var i = 0, ci; ci = this.inputRules[i++];) {
  3871. if(isUndoLoad && ci.ignoreUndo){
  3872. continue;
  3873. }
  3874. ci.call(this, root)
  3875. }
  3876. },
  3877. addOutputRule: function (rule,ignoreUndo) {
  3878. rule.ignoreUndo = ignoreUndo;
  3879. this.outputRules.push(rule)
  3880. },
  3881. filterOutputRule: function (root,isUndoLoad) {
  3882. for (var i = 0, ci; ci = this.outputRules[i++];) {
  3883. if(isUndoLoad && ci.ignoreUndo){
  3884. continue;
  3885. }
  3886. ci.call(this, root)
  3887. }
  3888. }
  3889. };
  3890. utils.inherits(Editor, EventBase);
  3891. })();
  3892. /**
  3893. * @file
  3894. * @name UM.filterWord
  3895. * @short filterWord
  3896. * @desc 用来过滤word粘贴过来的字符串
  3897. * @import editor.js,core/utils.js
  3898. * @anthor zhanyi
  3899. */
  3900. var filterWord = UM.filterWord = function () {
  3901. //是否是word过来的内容
  3902. function isWordDocument( str ) {
  3903. return /(class="?Mso|style="[^"]*\bmso\-|w:WordDocument|<v:)/ig.test( str );
  3904. }
  3905. //去掉小数
  3906. function transUnit( v ) {
  3907. v = v.replace( /[\d.]+\w+/g, function ( m ) {
  3908. return utils.transUnitToPx(m);
  3909. } );
  3910. return v;
  3911. }
  3912. function filterPasteWord( str ) {
  3913. return str.replace( /[\t\r\n]+/g, "" )
  3914. .replace( /<!--[\s\S]*?-->/ig, "" )
  3915. //转换图片
  3916. .replace(/<v:shape [^>]*>[\s\S]*?.<\/v:shape>/gi,function(str){
  3917. //opera能自己解析出image所这里直接返回空
  3918. if(browser.opera){
  3919. return '';
  3920. }
  3921. try{
  3922. var width = str.match(/width:([ \d.]*p[tx])/i)[1],
  3923. height = str.match(/height:([ \d.]*p[tx])/i)[1],
  3924. src = str.match(/src=\s*"([^"]*)"/i)[1];
  3925. return '<img width="'+ transUnit(width) +'" height="'+transUnit(height) +'" src="' + src + '" />';
  3926. } catch(e){
  3927. return '';
  3928. }
  3929. })
  3930. //针对wps添加的多余标签处理
  3931. .replace(/<\/?div[^>]*>/g,'')
  3932. //去掉多余的属性
  3933. .replace( /v:\w+=(["']?)[^'"]+\1/g, '' )
  3934. .replace( /<(!|script[^>]*>.*?<\/script(?=[>\s])|\/?(\?xml(:\w+)?|xml|meta|link|style|\w+:\w+)(?=[\s\/>]))[^>]*>/gi, "" )
  3935. .replace( /<p [^>]*class="?MsoHeading"?[^>]*>(.*?)<\/p>/gi, "<p><strong>$1</strong></p>" )
  3936. //去掉多余的属性
  3937. .replace( /\s+(class|lang|align)\s*=\s*(['"]?)([\w-]+)\2/ig, function(str,name,marks,val){
  3938. //保留list的标示
  3939. return name == 'class' && val == 'MsoListParagraph' ? str : ''
  3940. })
  3941. //清除多余的font/span不能匹配&nbsp;有可能是空格
  3942. .replace( /<(font|span)[^>]*>\s*<\/\1>/gi, '' )
  3943. //处理style的问题
  3944. .replace( /(<[a-z][^>]*)\sstyle=(["'])([^\2]*?)\2/gi, function( str, tag, tmp, style ) {
  3945. var n = [],
  3946. s = style.replace( /^\s+|\s+$/, '' )
  3947. .replace(/&#39;/g,'\'')
  3948. .replace( /&quot;/gi, "'" )
  3949. .split( /;\s*/g );
  3950. for ( var i = 0,v; v = s[i];i++ ) {
  3951. var name, value,
  3952. parts = v.split( ":" );
  3953. if ( parts.length == 2 ) {
  3954. name = parts[0].toLowerCase();
  3955. value = parts[1].toLowerCase();
  3956. if(/^(background)\w*/.test(name) && value.replace(/(initial|\s)/g,'').length == 0
  3957. ||
  3958. /^(margin)\w*/.test(name) && /^0\w+$/.test(value)
  3959. ){
  3960. continue;
  3961. }
  3962. switch ( name ) {
  3963. case "mso-padding-alt":
  3964. case "mso-padding-top-alt":
  3965. case "mso-padding-right-alt":
  3966. case "mso-padding-bottom-alt":
  3967. case "mso-padding-left-alt":
  3968. case "mso-margin-alt":
  3969. case "mso-margin-top-alt":
  3970. case "mso-margin-right-alt":
  3971. case "mso-margin-bottom-alt":
  3972. case "mso-margin-left-alt":
  3973. //ie下会出现挤到一起的情况
  3974. //case "mso-table-layout-alt":
  3975. case "mso-height":
  3976. case "mso-width":
  3977. case "mso-vertical-align-alt":
  3978. //trace:1819 ff下会解析出padding在table上
  3979. if(!/<table/.test(tag))
  3980. n[i] = name.replace( /^mso-|-alt$/g, "" ) + ":" + transUnit( value );
  3981. continue;
  3982. case "horiz-align":
  3983. n[i] = "text-align:" + value;
  3984. continue;
  3985. case "vert-align":
  3986. n[i] = "vertical-align:" + value;
  3987. continue;
  3988. case "font-color":
  3989. case "mso-foreground":
  3990. n[i] = "color:" + value;
  3991. continue;
  3992. case "mso-background":
  3993. case "mso-highlight":
  3994. n[i] = "background:" + value;
  3995. continue;
  3996. case "mso-default-height":
  3997. n[i] = "min-height:" + transUnit( value );
  3998. continue;
  3999. case "mso-default-width":
  4000. n[i] = "min-width:" + transUnit( value );
  4001. continue;
  4002. case "mso-padding-between-alt":
  4003. n[i] = "border-collapse:separate;border-spacing:" + transUnit( value );
  4004. continue;
  4005. case "text-line-through":
  4006. if ( (value == "single") || (value == "double") ) {
  4007. n[i] = "text-decoration:line-through";
  4008. }
  4009. continue;
  4010. case "mso-zero-height":
  4011. if ( value == "yes" ) {
  4012. n[i] = "display:none";
  4013. }
  4014. continue;
  4015. case 'background':
  4016. break;
  4017. case 'margin':
  4018. if ( !/[1-9]/.test( value ) ) {
  4019. continue;
  4020. }
  4021. }
  4022. if ( /^(mso|column|font-emph|lang|layout|line-break|list-image|nav|panose|punct|row|ruby|sep|size|src|tab-|table-border|text-(?:decor|trans)|top-bar|version|vnd|word-break)/.test( name )
  4023. ||
  4024. /text\-indent|padding|margin/.test(name) && /\-[\d.]+/.test(value)
  4025. ) {
  4026. continue;
  4027. }
  4028. n[i] = name + ":" + parts[1];
  4029. }
  4030. }
  4031. return tag + (n.length ? ' style="' + n.join( ';').replace(/;{2,}/g,';') + '"' : '');
  4032. })
  4033. .replace(/[\d.]+(cm|pt)/g,function(str){
  4034. return utils.transUnitToPx(str)
  4035. })
  4036. }
  4037. return function ( html ) {
  4038. return (isWordDocument( html ) ? filterPasteWord( html ) : html);
  4039. };
  4040. }();
  4041. ///import editor.js
  4042. ///import core/utils.js
  4043. ///import core/dom/dom.js
  4044. ///import core/dom/dtd.js
  4045. ///import core/htmlparser.js
  4046. //模拟的节点类
  4047. //by zhanyi
  4048. (function () {
  4049. var uNode = UM.uNode = function (obj) {
  4050. this.type = obj.type;
  4051. this.data = obj.data;
  4052. this.tagName = obj.tagName;
  4053. this.parentNode = obj.parentNode;
  4054. this.attrs = obj.attrs || {};
  4055. this.children = obj.children;
  4056. };
  4057. var indentChar = ' ',
  4058. breakChar = '\n';
  4059. function insertLine(arr, current, begin) {
  4060. arr.push(breakChar);
  4061. return current + (begin ? 1 : -1);
  4062. }
  4063. function insertIndent(arr, current) {
  4064. //插入缩进
  4065. for (var i = 0; i < current; i++) {
  4066. arr.push(indentChar);
  4067. }
  4068. }
  4069. //创建uNode的静态方法
  4070. //支持标签和html
  4071. uNode.createElement = function (html) {
  4072. if (/[<>]/.test(html)) {
  4073. return UM.htmlparser(html).children[0]
  4074. } else {
  4075. return new uNode({
  4076. type:'element',
  4077. children:[],
  4078. tagName:html
  4079. })
  4080. }
  4081. };
  4082. uNode.createText = function (data,noTrans) {
  4083. return new UM.uNode({
  4084. type:'text',
  4085. 'data':noTrans ? data : utils.unhtml(data || '')
  4086. })
  4087. };
  4088. function nodeToHtml(node, arr, formatter, current) {
  4089. switch (node.type) {
  4090. case 'root':
  4091. for (var i = 0, ci; ci = node.children[i++];) {
  4092. //插入新行
  4093. if (formatter && ci.type == 'element' && !dtd.$inlineWithA[ci.tagName] && i > 1) {
  4094. insertLine(arr, current, true);
  4095. insertIndent(arr, current)
  4096. }
  4097. nodeToHtml(ci, arr, formatter, current)
  4098. }
  4099. break;
  4100. case 'text':
  4101. isText(node, arr);
  4102. break;
  4103. case 'element':
  4104. isElement(node, arr, formatter, current);
  4105. break;
  4106. case 'comment':
  4107. isComment(node, arr, formatter);
  4108. }
  4109. return arr;
  4110. }
  4111. function isText(node, arr) {
  4112. arr.push(node.parentNode.tagName == 'pre' ? node.data : node.data.replace(/[ ]{2}/g,' &nbsp;'))
  4113. }
  4114. function isElement(node, arr, formatter, current) {
  4115. var attrhtml = '';
  4116. if (node.attrs) {
  4117. attrhtml = [];
  4118. var attrs = node.attrs;
  4119. for (var a in attrs) {
  4120. attrhtml.push(a + (attrs[a] !== undefined ? '="' + attrs[a] + '"' : ''))
  4121. }
  4122. attrhtml = attrhtml.join(' ');
  4123. }
  4124. arr.push('<' + node.tagName +
  4125. (attrhtml ? ' ' + attrhtml : '') +
  4126. (dtd.$empty[node.tagName] ? '\/' : '' ) + '>'
  4127. );
  4128. //插入新行
  4129. if (formatter && !dtd.$inlineWithA[node.tagName] && node.tagName != 'pre') {
  4130. if(node.children && node.children.length){
  4131. current = insertLine(arr, current, true);
  4132. insertIndent(arr, current)
  4133. }
  4134. }
  4135. if (node.children && node.children.length) {
  4136. for (var i = 0, ci; ci = node.children[i++];) {
  4137. if (formatter && ci.type == 'element' && !dtd.$inlineWithA[ci.tagName] && i > 1) {
  4138. insertLine(arr, current);
  4139. insertIndent(arr, current)
  4140. }
  4141. nodeToHtml(ci, arr, formatter, current)
  4142. }
  4143. }
  4144. if (!dtd.$empty[node.tagName]) {
  4145. if (formatter && !dtd.$inlineWithA[node.tagName] && node.tagName != 'pre') {
  4146. if(node.children && node.children.length){
  4147. current = insertLine(arr, current);
  4148. insertIndent(arr, current)
  4149. }
  4150. }
  4151. arr.push('<\/' + node.tagName + '>');
  4152. }
  4153. }
  4154. function isComment(node, arr) {
  4155. arr.push('<!--' + node.data + '-->');
  4156. }
  4157. function getNodeById(root, id) {
  4158. var node;
  4159. if (root.type == 'element' && root.getAttr('id') == id) {
  4160. return root;
  4161. }
  4162. if (root.children && root.children.length) {
  4163. for (var i = 0, ci; ci = root.children[i++];) {
  4164. if (node = getNodeById(ci, id)) {
  4165. return node;
  4166. }
  4167. }
  4168. }
  4169. }
  4170. function getNodesByTagName(node, tagName, arr) {
  4171. if (node.type == 'element' && node.tagName == tagName) {
  4172. arr.push(node);
  4173. }
  4174. if (node.children && node.children.length) {
  4175. for (var i = 0, ci; ci = node.children[i++];) {
  4176. getNodesByTagName(ci, tagName, arr)
  4177. }
  4178. }
  4179. }
  4180. function nodeTraversal(root,fn){
  4181. if(root.children && root.children.length){
  4182. for(var i= 0,ci;ci=root.children[i];){
  4183. nodeTraversal(ci,fn);
  4184. //ci被替换的情况,这里就不再走 fn了
  4185. if(ci.parentNode ){
  4186. if(ci.children && ci.children.length){
  4187. fn(ci)
  4188. }
  4189. if(ci.parentNode) i++
  4190. }
  4191. }
  4192. }else{
  4193. fn(root)
  4194. }
  4195. }
  4196. uNode.prototype = {
  4197. toHtml:function (formatter) {
  4198. var arr = [];
  4199. nodeToHtml(this, arr, formatter, 0);
  4200. return arr.join('')
  4201. },
  4202. innerHTML:function (htmlstr) {
  4203. if (this.type != 'element' || dtd.$empty[this.tagName]) {
  4204. return this;
  4205. }
  4206. if (utils.isString(htmlstr)) {
  4207. if(this.children){
  4208. for (var i = 0, ci; ci = this.children[i++];) {
  4209. ci.parentNode = null;
  4210. }
  4211. }
  4212. this.children = [];
  4213. var tmpRoot = UM.htmlparser(htmlstr);
  4214. for (var i = 0, ci; ci = tmpRoot.children[i++];) {
  4215. this.children.push(ci);
  4216. ci.parentNode = this;
  4217. }
  4218. return this;
  4219. } else {
  4220. var tmpRoot = new UM.uNode({
  4221. type:'root',
  4222. children:this.children
  4223. });
  4224. return tmpRoot.toHtml();
  4225. }
  4226. },
  4227. innerText:function (textStr,noTrans) {
  4228. if (this.type != 'element' || dtd.$empty[this.tagName]) {
  4229. return this;
  4230. }
  4231. if (textStr) {
  4232. if(this.children){
  4233. for (var i = 0, ci; ci = this.children[i++];) {
  4234. ci.parentNode = null;
  4235. }
  4236. }
  4237. this.children = [];
  4238. this.appendChild(uNode.createText(textStr,noTrans));
  4239. return this;
  4240. } else {
  4241. return this.toHtml().replace(/<[^>]+>/g, '');
  4242. }
  4243. },
  4244. getData:function () {
  4245. if (this.type == 'element')
  4246. return '';
  4247. return this.data
  4248. },
  4249. firstChild:function () {
  4250. // if (this.type != 'element' || dtd.$empty[this.tagName]) {
  4251. // return this;
  4252. // }
  4253. return this.children ? this.children[0] : null;
  4254. },
  4255. lastChild:function () {
  4256. // if (this.type != 'element' || dtd.$empty[this.tagName] ) {
  4257. // return this;
  4258. // }
  4259. return this.children ? this.children[this.children.length - 1] : null;
  4260. },
  4261. previousSibling : function(){
  4262. var parent = this.parentNode;
  4263. for (var i = 0, ci; ci = parent.children[i]; i++) {
  4264. if (ci === this) {
  4265. return i == 0 ? null : parent.children[i-1];
  4266. }
  4267. }
  4268. },
  4269. nextSibling : function(){
  4270. var parent = this.parentNode;
  4271. for (var i = 0, ci; ci = parent.children[i++];) {
  4272. if (ci === this) {
  4273. return parent.children[i];
  4274. }
  4275. }
  4276. },
  4277. replaceChild:function (target, source) {
  4278. if (this.children) {
  4279. if(target.parentNode){
  4280. target.parentNode.removeChild(target);
  4281. }
  4282. for (var i = 0, ci; ci = this.children[i]; i++) {
  4283. if (ci === source) {
  4284. this.children.splice(i, 1, target);
  4285. source.parentNode = null;
  4286. target.parentNode = this;
  4287. return target;
  4288. }
  4289. }
  4290. }
  4291. },
  4292. appendChild:function (node) {
  4293. if (this.type == 'root' || (this.type == 'element' && !dtd.$empty[this.tagName])) {
  4294. if (!this.children) {
  4295. this.children = []
  4296. }
  4297. if(node.parentNode){
  4298. node.parentNode.removeChild(node);
  4299. }
  4300. for (var i = 0, ci; ci = this.children[i]; i++) {
  4301. if (ci === node) {
  4302. this.children.splice(i, 1);
  4303. break;
  4304. }
  4305. }
  4306. this.children.push(node);
  4307. node.parentNode = this;
  4308. return node;
  4309. }
  4310. },
  4311. insertBefore:function (target, source) {
  4312. if (this.children) {
  4313. if(target.parentNode){
  4314. target.parentNode.removeChild(target);
  4315. }
  4316. for (var i = 0, ci; ci = this.children[i]; i++) {
  4317. if (ci === source) {
  4318. this.children.splice(i, 0, target);
  4319. target.parentNode = this;
  4320. return target;
  4321. }
  4322. }
  4323. }
  4324. },
  4325. insertAfter:function (target, source) {
  4326. if (this.children) {
  4327. if(target.parentNode){
  4328. target.parentNode.removeChild(target);
  4329. }
  4330. for (var i = 0, ci; ci = this.children[i]; i++) {
  4331. if (ci === source) {
  4332. this.children.splice(i + 1, 0, target);
  4333. target.parentNode = this;
  4334. return target;
  4335. }
  4336. }
  4337. }
  4338. },
  4339. removeChild:function (node,keepChildren) {
  4340. if (this.children) {
  4341. for (var i = 0, ci; ci = this.children[i]; i++) {
  4342. if (ci === node) {
  4343. this.children.splice(i, 1);
  4344. ci.parentNode = null;
  4345. if(keepChildren && ci.children && ci.children.length){
  4346. for(var j= 0,cj;cj=ci.children[j];j++){
  4347. this.children.splice(i+j,0,cj);
  4348. cj.parentNode = this;
  4349. }
  4350. }
  4351. return ci;
  4352. }
  4353. }
  4354. }
  4355. },
  4356. getAttr:function (attrName) {
  4357. return this.attrs && this.attrs[attrName.toLowerCase()]
  4358. },
  4359. setAttr:function (attrName, attrVal) {
  4360. if (!attrName) {
  4361. delete this.attrs;
  4362. return;
  4363. }
  4364. if(!this.attrs){
  4365. this.attrs = {};
  4366. }
  4367. if (utils.isObject(attrName)) {
  4368. for (var a in attrName) {
  4369. if (!attrName[a]) {
  4370. delete this.attrs[a]
  4371. } else {
  4372. this.attrs[a.toLowerCase()] = attrName[a];
  4373. }
  4374. }
  4375. } else {
  4376. if (!attrVal) {
  4377. delete this.attrs[attrName]
  4378. } else {
  4379. this.attrs[attrName.toLowerCase()] = attrVal;
  4380. }
  4381. }
  4382. },
  4383. getIndex:function(){
  4384. var parent = this.parentNode;
  4385. for(var i= 0,ci;ci=parent.children[i];i++){
  4386. if(ci === this){
  4387. return i;
  4388. }
  4389. }
  4390. return -1;
  4391. },
  4392. getNodeById:function (id) {
  4393. var node;
  4394. if (this.children && this.children.length) {
  4395. for (var i = 0, ci; ci = this.children[i++];) {
  4396. if (node = getNodeById(ci, id)) {
  4397. return node;
  4398. }
  4399. }
  4400. }
  4401. },
  4402. getNodesByTagName:function (tagNames) {
  4403. tagNames = utils.trim(tagNames).replace(/[ ]{2,}/g, ' ').split(' ');
  4404. var arr = [], me = this;
  4405. utils.each(tagNames, function (tagName) {
  4406. if (me.children && me.children.length) {
  4407. for (var i = 0, ci; ci = me.children[i++];) {
  4408. getNodesByTagName(ci, tagName, arr)
  4409. }
  4410. }
  4411. });
  4412. return arr;
  4413. },
  4414. getStyle:function (name) {
  4415. var cssStyle = this.getAttr('style');
  4416. if (!cssStyle) {
  4417. return ''
  4418. }
  4419. var reg = new RegExp('(^|;)\\s*' + name + ':([^;]+)','i');
  4420. var match = cssStyle.match(reg);
  4421. if (match && match[0]) {
  4422. return match[2]
  4423. }
  4424. return '';
  4425. },
  4426. setStyle:function (name, val) {
  4427. function exec(name, val) {
  4428. var reg = new RegExp('(^|;)\\s*' + name + ':([^;]+;?)', 'gi');
  4429. cssStyle = cssStyle.replace(reg, '$1');
  4430. if (val) {
  4431. cssStyle = name + ':' + utils.unhtml(val) + ';' + cssStyle
  4432. }
  4433. }
  4434. var cssStyle = this.getAttr('style');
  4435. if (!cssStyle) {
  4436. cssStyle = '';
  4437. }
  4438. if (utils.isObject(name)) {
  4439. for (var a in name) {
  4440. exec(a, name[a])
  4441. }
  4442. } else {
  4443. exec(name, val)
  4444. }
  4445. this.setAttr('style', utils.trim(cssStyle))
  4446. },
  4447. traversal:function(fn){
  4448. if(this.children && this.children.length){
  4449. nodeTraversal(this,fn);
  4450. }
  4451. return this;
  4452. }
  4453. }
  4454. })();
  4455. //html字符串转换成uNode节点
  4456. //by zhanyi
  4457. var htmlparser = UM.htmlparser = function (htmlstr,ignoreBlank) {
  4458. var re_tag = /<(?:(?:\/([^>]+)>)|(?:!--([\S|\s]*?)-->)|(?:([^\s\/>]+)\s*((?:(?:"[^"]*")|(?:'[^']*')|[^"'<>])*)\/?>))/g,
  4459. re_attr = /([\w\-:.]+)(?:(?:\s*=\s*(?:(?:"([^"]*)")|(?:'([^']*)')|([^\s>]+)))|(?=\s|$))/g;
  4460. //ie下取得的html可能会有\n存在,要去掉,在处理replace(/[\t\r\n]*/g,'');代码高量的\n不能去除
  4461. var allowEmptyTags = {
  4462. b:1,code:1,i:1,u:1,strike:1,s:1,tt:1,strong:1,q:1,samp:1,em:1,span:1,
  4463. sub:1,img:1,sup:1,font:1,big:1,small:1,iframe:1,a:1,br:1,pre:1
  4464. };
  4465. htmlstr = htmlstr.replace(new RegExp(domUtils.fillChar, 'g'), '');
  4466. if(!ignoreBlank){
  4467. htmlstr = htmlstr.replace(new RegExp('[\\r\\t\\n'+(ignoreBlank?'':' ')+']*<\/?(\\w+)\\s*(?:[^>]*)>[\\r\\t\\n'+(ignoreBlank?'':' ')+']*','g'), function(a,b){
  4468. //br暂时单独处理
  4469. if(b && allowEmptyTags[b.toLowerCase()]){
  4470. return a.replace(/(^[\n\r]+)|([\n\r]+$)/g,'');
  4471. }
  4472. return a.replace(new RegExp('^[\\r\\n'+(ignoreBlank?'':' ')+']+'),'').replace(new RegExp('[\\r\\n'+(ignoreBlank?'':' ')+']+$'),'');
  4473. });
  4474. }
  4475. var uNode = UM.uNode,
  4476. needParentNode = {
  4477. 'td':'tr',
  4478. 'tr':['tbody','thead','tfoot'],
  4479. 'tbody':'table',
  4480. 'th':'tr',
  4481. 'thead':'table',
  4482. 'tfoot':'table',
  4483. 'caption':'table',
  4484. 'li':['ul', 'ol'],
  4485. 'dt':'dl',
  4486. 'dd':'dl',
  4487. 'option':'select'
  4488. },
  4489. needChild = {
  4490. 'ol':'li',
  4491. 'ul':'li'
  4492. };
  4493. function text(parent, data) {
  4494. if(needChild[parent.tagName]){
  4495. var tmpNode = uNode.createElement(needChild[parent.tagName]);
  4496. parent.appendChild(tmpNode);
  4497. tmpNode.appendChild(uNode.createText(data));
  4498. parent = tmpNode;
  4499. }else{
  4500. parent.appendChild(uNode.createText(data));
  4501. }
  4502. }
  4503. function element(parent, tagName, htmlattr) {
  4504. var needParentTag;
  4505. if (needParentTag = needParentNode[tagName]) {
  4506. var tmpParent = parent,hasParent;
  4507. while(tmpParent.type != 'root'){
  4508. if(utils.isArray(needParentTag) ? utils.indexOf(needParentTag, tmpParent.tagName) != -1 : needParentTag == tmpParent.tagName){
  4509. parent = tmpParent;
  4510. hasParent = true;
  4511. break;
  4512. }
  4513. tmpParent = tmpParent.parentNode;
  4514. }
  4515. if(!hasParent){
  4516. parent = element(parent, utils.isArray(needParentTag) ? needParentTag[0] : needParentTag)
  4517. }
  4518. }
  4519. //按dtd处理嵌套
  4520. // if(parent.type != 'root' && !dtd[parent.tagName][tagName])
  4521. // parent = parent.parentNode;
  4522. var elm = new uNode({
  4523. parentNode:parent,
  4524. type:'element',
  4525. tagName:tagName.toLowerCase(),
  4526. //是自闭合的处理一下
  4527. children:dtd.$empty[tagName] ? null : []
  4528. });
  4529. //如果属性存在,处理属性
  4530. if (htmlattr) {
  4531. var attrs = {}, match;
  4532. while (match = re_attr.exec(htmlattr)) {
  4533. attrs[match[1].toLowerCase()] = utils.unhtml(match[2] || match[3] || match[4])
  4534. }
  4535. elm.attrs = attrs;
  4536. }
  4537. parent.children.push(elm);
  4538. //如果是自闭合节点返回父亲节点
  4539. return dtd.$empty[tagName] ? parent : elm
  4540. }
  4541. function comment(parent, data) {
  4542. parent.children.push(new uNode({
  4543. type:'comment',
  4544. data:data,
  4545. parentNode:parent
  4546. }));
  4547. }
  4548. var match, currentIndex = 0, nextIndex = 0;
  4549. //设置根节点
  4550. var root = new uNode({
  4551. type:'root',
  4552. children:[]
  4553. });
  4554. var currentParent = root;
  4555. while (match = re_tag.exec(htmlstr)) {
  4556. currentIndex = match.index;
  4557. try{
  4558. if (currentIndex > nextIndex) {
  4559. //text node
  4560. text(currentParent, htmlstr.slice(nextIndex, currentIndex));
  4561. }
  4562. if (match[3]) {
  4563. if(dtd.$cdata[currentParent.tagName]){
  4564. text(currentParent, match[0]);
  4565. }else{
  4566. //start tag
  4567. currentParent = element(currentParent, match[3].toLowerCase(), match[4]);
  4568. }
  4569. } else if (match[1]) {
  4570. if(currentParent.type != 'root'){
  4571. if(dtd.$cdata[currentParent.tagName] && !dtd.$cdata[match[1]]){
  4572. text(currentParent, match[0]);
  4573. }else{
  4574. var tmpParent = currentParent;
  4575. while(currentParent.type == 'element' && currentParent.tagName != match[1].toLowerCase()){
  4576. currentParent = currentParent.parentNode;
  4577. if(currentParent.type == 'root'){
  4578. currentParent = tmpParent;
  4579. throw 'break'
  4580. }
  4581. }
  4582. //end tag
  4583. currentParent = currentParent.parentNode;
  4584. }
  4585. }
  4586. } else if (match[2]) {
  4587. //comment
  4588. comment(currentParent, match[2])
  4589. }
  4590. }catch(e){}
  4591. nextIndex = re_tag.lastIndex;
  4592. }
  4593. //如果结束是文本,就有可能丢掉,所以这里手动判断一下
  4594. //例如 <li>sdfsdfsdf<li>sdfsdfsdfsdf
  4595. if (nextIndex < htmlstr.length) {
  4596. text(currentParent, htmlstr.slice(nextIndex));
  4597. }
  4598. return root;
  4599. };
  4600. /**
  4601. * @file
  4602. * @name UM.filterNode
  4603. * @short filterNode
  4604. * @desc 根据给定的规则过滤节点
  4605. * @import editor.js,core/utils.js
  4606. * @anthor zhanyi
  4607. */
  4608. var filterNode = UM.filterNode = function () {
  4609. function filterNode(node,rules){
  4610. switch (node.type) {
  4611. case 'text':
  4612. break;
  4613. case 'element':
  4614. var val;
  4615. if(val = rules[node.tagName]){
  4616. if(val === '-'){
  4617. node.parentNode.removeChild(node)
  4618. }else if(utils.isFunction(val)){
  4619. var parentNode = node.parentNode,
  4620. index = node.getIndex();
  4621. val(node);
  4622. if(node.parentNode){
  4623. if(node.children){
  4624. for(var i = 0,ci;ci=node.children[i];){
  4625. filterNode(ci,rules);
  4626. if(ci.parentNode){
  4627. i++;
  4628. }
  4629. }
  4630. }
  4631. }else{
  4632. for(var i = index,ci;ci=parentNode.children[i];){
  4633. filterNode(ci,rules);
  4634. if(ci.parentNode){
  4635. i++;
  4636. }
  4637. }
  4638. }
  4639. }else{
  4640. var attrs = val['$'];
  4641. if(attrs && node.attrs){
  4642. var tmpAttrs = {},tmpVal;
  4643. for(var a in attrs){
  4644. tmpVal = node.getAttr(a);
  4645. //todo 只先对style单独处理
  4646. if(a == 'style' && utils.isArray(attrs[a])){
  4647. var tmpCssStyle = [];
  4648. utils.each(attrs[a],function(v){
  4649. var tmp;
  4650. if(tmp = node.getStyle(v)){
  4651. tmpCssStyle.push(v + ':' + tmp);
  4652. }
  4653. });
  4654. tmpVal = tmpCssStyle.join(';')
  4655. }
  4656. if(tmpVal){
  4657. tmpAttrs[a] = tmpVal;
  4658. }
  4659. }
  4660. node.attrs = tmpAttrs;
  4661. }
  4662. if(node.children){
  4663. for(var i = 0,ci;ci=node.children[i];){
  4664. filterNode(ci,rules);
  4665. if(ci.parentNode){
  4666. i++;
  4667. }
  4668. }
  4669. }
  4670. }
  4671. }else{
  4672. //如果不在名单里扣出子节点并删除该节点,cdata除外
  4673. if(dtd.$cdata[node.tagName]){
  4674. node.parentNode.removeChild(node)
  4675. }else{
  4676. var parentNode = node.parentNode,
  4677. index = node.getIndex();
  4678. node.parentNode.removeChild(node,true);
  4679. for(var i = index,ci;ci=parentNode.children[i];){
  4680. filterNode(ci,rules);
  4681. if(ci.parentNode){
  4682. i++;
  4683. }
  4684. }
  4685. }
  4686. }
  4687. break;
  4688. case 'comment':
  4689. node.parentNode.removeChild(node)
  4690. }
  4691. }
  4692. return function(root,rules){
  4693. if(utils.isEmptyObject(rules)){
  4694. return root;
  4695. }
  4696. var val;
  4697. if(val = rules['-']){
  4698. utils.each(val.split(' '),function(k){
  4699. rules[k] = '-'
  4700. })
  4701. }
  4702. for(var i= 0,ci;ci=root.children[i];){
  4703. filterNode(ci,rules);
  4704. if(ci.parentNode){
  4705. i++;
  4706. }
  4707. }
  4708. return root;
  4709. }
  4710. }();
  4711. ///import core
  4712. /**
  4713. * @description 插入内容
  4714. * @name baidu.editor.execCommand
  4715. * @param {String} cmdName inserthtml插入内容的命令
  4716. * @param {String} html 要插入的内容
  4717. * @author zhanyi
  4718. */
  4719. UM.commands['inserthtml'] = {
  4720. execCommand: function (command,html,notNeedFilter){
  4721. var me = this,
  4722. range,
  4723. div;
  4724. if(!html){
  4725. return;
  4726. }
  4727. if(me.fireEvent('beforeinserthtml',html) === true){
  4728. return;
  4729. }
  4730. range = me.selection.getRange();
  4731. div = range.document.createElement( 'div' );
  4732. div.style.display = 'inline';
  4733. if (!notNeedFilter) {
  4734. var root = UM.htmlparser(html);
  4735. //如果给了过滤规则就先进行过滤
  4736. if(me.options.filterRules){
  4737. UM.filterNode(root,me.options.filterRules);
  4738. }
  4739. //执行默认的处理
  4740. me.filterInputRule(root);
  4741. html = root.toHtml()
  4742. }
  4743. div.innerHTML = utils.trim( html );
  4744. if ( !range.collapsed ) {
  4745. var tmpNode = range.startContainer;
  4746. if(domUtils.isFillChar(tmpNode)){
  4747. range.setStartBefore(tmpNode)
  4748. }
  4749. tmpNode = range.endContainer;
  4750. if(domUtils.isFillChar(tmpNode)){
  4751. range.setEndAfter(tmpNode)
  4752. }
  4753. range.txtToElmBoundary();
  4754. //结束边界可能放到了br的前边,要把br包含进来
  4755. // x[xxx]<br/>
  4756. if(range.endContainer && range.endContainer.nodeType == 1){
  4757. tmpNode = range.endContainer.childNodes[range.endOffset];
  4758. if(tmpNode && domUtils.isBr(tmpNode)){
  4759. range.setEndAfter(tmpNode);
  4760. }
  4761. }
  4762. if(range.startOffset == 0){
  4763. tmpNode = range.startContainer;
  4764. if(domUtils.isBoundaryNode(tmpNode,'firstChild') ){
  4765. tmpNode = range.endContainer;
  4766. if(range.endOffset == (tmpNode.nodeType == 3 ? tmpNode.nodeValue.length : tmpNode.childNodes.length) && domUtils.isBoundaryNode(tmpNode,'lastChild')){
  4767. me.body.innerHTML = '<p>'+(browser.ie ? '' : '<br/>')+'</p>';
  4768. range.setStart(me.body.firstChild,0).collapse(true)
  4769. }
  4770. }
  4771. }
  4772. !range.collapsed && range.deleteContents();
  4773. if(range.startContainer.nodeType == 1){
  4774. var child = range.startContainer.childNodes[range.startOffset],pre;
  4775. if(child && domUtils.isBlockElm(child) && (pre = child.previousSibling) && domUtils.isBlockElm(pre)){
  4776. range.setEnd(pre,pre.childNodes.length).collapse();
  4777. while(child.firstChild){
  4778. pre.appendChild(child.firstChild);
  4779. }
  4780. domUtils.remove(child);
  4781. }
  4782. }
  4783. }
  4784. var child,parent,pre,tmp,hadBreak = 0, nextNode;
  4785. //如果当前位置选中了fillchar要干掉,要不会产生空行
  4786. if(range.inFillChar()){
  4787. child = range.startContainer;
  4788. if(domUtils.isFillChar(child)){
  4789. range.setStartBefore(child).collapse(true);
  4790. domUtils.remove(child);
  4791. }else if(domUtils.isFillChar(child,true)){
  4792. child.nodeValue = child.nodeValue.replace(fillCharReg,'');
  4793. range.startOffset--;
  4794. range.collapsed && range.collapse(true)
  4795. }
  4796. }
  4797. while ( child = div.firstChild ) {
  4798. if(hadBreak){
  4799. var p = me.document.createElement('p');
  4800. while(child && (child.nodeType == 3 || !dtd.$block[child.tagName])){
  4801. nextNode = child.nextSibling;
  4802. p.appendChild(child);
  4803. child = nextNode;
  4804. }
  4805. if(p.firstChild){
  4806. child = p
  4807. }
  4808. }
  4809. range.insertNode( child );
  4810. nextNode = child.nextSibling;
  4811. if ( !hadBreak && child.nodeType == domUtils.NODE_ELEMENT && domUtils.isBlockElm( child ) ){
  4812. parent = domUtils.findParent( child,function ( node ){ return domUtils.isBlockElm( node ); } );
  4813. if ( parent && parent.tagName.toLowerCase() != 'body' && !(dtd[parent.tagName][child.nodeName] && child.parentNode === parent)){
  4814. if(!dtd[parent.tagName][child.nodeName]){
  4815. pre = parent;
  4816. }else{
  4817. tmp = child.parentNode;
  4818. while (tmp !== parent){
  4819. pre = tmp;
  4820. tmp = tmp.parentNode;
  4821. }
  4822. }
  4823. domUtils.breakParent( child, pre || tmp );
  4824. //去掉break后前一个多余的节点 <p>|<[p> ==> <p></p><div></div><p>|</p>
  4825. var pre = child.previousSibling;
  4826. domUtils.trimWhiteTextNode(pre);
  4827. if(!pre.childNodes.length){
  4828. domUtils.remove(pre);
  4829. }
  4830. //trace:2012,在非ie的情况,切开后剩下的节点有可能不能点入光标添加br占位
  4831. if(!browser.ie &&
  4832. (next = child.nextSibling) &&
  4833. domUtils.isBlockElm(next) &&
  4834. next.lastChild &&
  4835. !domUtils.isBr(next.lastChild)){
  4836. next.appendChild(me.document.createElement('br'));
  4837. }
  4838. hadBreak = 1;
  4839. }
  4840. }
  4841. var next = child.nextSibling;
  4842. if(!div.firstChild && next && domUtils.isBlockElm(next)){
  4843. range.setStart(next,0).collapse(true);
  4844. break;
  4845. }
  4846. range.setEndAfter( child ).collapse();
  4847. }
  4848. child = range.startContainer;
  4849. if(nextNode && domUtils.isBr(nextNode)){
  4850. domUtils.remove(nextNode)
  4851. }
  4852. //用chrome可能有空白展位符
  4853. if(domUtils.isBlockElm(child) && domUtils.isEmptyNode(child)){
  4854. if(nextNode = child.nextSibling){
  4855. domUtils.remove(child);
  4856. if(nextNode.nodeType == 1 && dtd.$block[nextNode.tagName]){
  4857. range.setStart(nextNode,0).collapse(true).shrinkBoundary()
  4858. }
  4859. }else{
  4860. try{
  4861. child.innerHTML = browser.ie ? domUtils.fillChar : '<br/>';
  4862. }catch(e){
  4863. range.setStartBefore(child);
  4864. domUtils.remove(child)
  4865. }
  4866. }
  4867. }
  4868. //加上true因为在删除表情等时会删两次,第一次是删的fillData
  4869. try{
  4870. if(browser.ie9below && range.startContainer.nodeType == 1 && !range.startContainer.childNodes[range.startOffset]){
  4871. var start = range.startContainer,pre = start.childNodes[range.startOffset-1];
  4872. if(pre && pre.nodeType == 1 && dtd.$empty[pre.tagName]){
  4873. var txt = this.document.createTextNode(domUtils.fillChar);
  4874. range.insertNode(txt).setStart(txt,0).collapse(true);
  4875. }
  4876. }
  4877. range.select(true);
  4878. }catch(e){}
  4879. setTimeout(function(){
  4880. range = me.selection.getRange();
  4881. range.scrollIntoView();
  4882. me.fireEvent('afterinserthtml');
  4883. },200);
  4884. }
  4885. };
  4886. ///import core
  4887. ///import plugins\inserthtml.js
  4888. ///commands 插入图片,操作图片的对齐方式
  4889. ///commandsName InsertImage,ImageNone,ImageLeft,ImageRight,ImageCenter
  4890. ///commandsTitle 图片,默认,居左,居右,居中
  4891. ///commandsDialog dialogs\image
  4892. /**
  4893. * Created by .
  4894. * User: zhanyi
  4895. * for image
  4896. */
  4897. UM.commands['insertimage'] = {
  4898. execCommand:function (cmd, opt) {
  4899. opt = utils.isArray(opt) ? opt : [opt];
  4900. if (!opt.length) {
  4901. return;
  4902. }
  4903. var me = this;
  4904. var html = [], str = '', ci;
  4905. ci = opt[0];
  4906. if (opt.length == 1) {
  4907. str = '<img src="' + ci.src + '" ' + (ci._src ? ' _src="' + ci._src + '" ' : '') +
  4908. (ci.width ? 'width="' + ci.width + '" ' : '') +
  4909. (ci.height ? ' height="' + ci.height + '" ' : '') +
  4910. (ci['floatStyle'] == 'left' || ci['floatStyle'] == 'right' ? ' style="float:' + ci['floatStyle'] + ';"' : '') +
  4911. (ci.title && ci.title != "" ? ' title="' + ci.title + '"' : '') +
  4912. (ci.border && ci.border != "0" ? ' border="' + ci.border + '"' : '') +
  4913. (ci.alt && ci.alt != "" ? ' alt="' + ci.alt + '"' : '') +
  4914. (ci.hspace && ci.hspace != "0" ? ' hspace = "' + ci.hspace + '"' : '') +
  4915. (ci.vspace && ci.vspace != "0" ? ' vspace = "' + ci.vspace + '"' : '') + '/>';
  4916. if (ci['floatStyle'] == 'center') {
  4917. str = '<p style="text-align: center">' + str + '</p>';
  4918. }
  4919. html.push(str);
  4920. } else {
  4921. for (var i = 0; ci = opt[i++];) {
  4922. str = '<p ' + (ci['floatStyle'] == 'center' ? 'style="text-align: center" ' : '') + '><img src="' + ci.src + '" ' +
  4923. (ci.width ? 'width="' + ci.width + '" ' : '') + (ci._src ? ' _src="' + ci._src + '" ' : '') +
  4924. (ci.height ? ' height="' + ci.height + '" ' : '') +
  4925. ' style="' + (ci['floatStyle'] && ci['floatStyle'] != 'center' ? 'float:' + ci['floatStyle'] + ';' : '') +
  4926. (ci.border || '') + '" ' +
  4927. (ci.title ? ' title="' + ci.title + '"' : '') + ' /></p>';
  4928. html.push(str);
  4929. }
  4930. }
  4931. me.execCommand('insertHtml', html.join(''), true);
  4932. }
  4933. };
  4934. ///import core
  4935. ///commands 段落格式,居左,居右,居中,两端对齐
  4936. ///commandsName JustifyLeft,JustifyCenter,JustifyRight,JustifyJustify
  4937. ///commandsTitle 居左对齐,居中对齐,居右对齐,两端对齐
  4938. /**
  4939. * @description 居左右中
  4940. * @name UM.execCommand
  4941. * @param {String} cmdName justify执行对齐方式的命令
  4942. * @param {String} align 对齐方式:left居左,right居右,center居中,justify两端对齐
  4943. * @author zhanyi
  4944. */
  4945. UM.plugins['justify']=function(){
  4946. $.each('justifyleft justifyright justifycenter justifyfull'.split(' '),function(i,cmdName){
  4947. UM.commands[cmdName] = {
  4948. execCommand:function (cmdName) {
  4949. return this.document.execCommand(cmdName);
  4950. },
  4951. queryCommandValue: function (cmdName) {
  4952. var val = this.document.queryCommandValue(cmdName);
  4953. return val === true || val === 'true' ? cmdName.replace(/justify/,'') : '';
  4954. },
  4955. queryCommandState: function (cmdName) {
  4956. return this.document.queryCommandState(cmdName) ? 1 : 0
  4957. }
  4958. };
  4959. })
  4960. };
  4961. ///import core
  4962. ///import plugins\removeformat.js
  4963. ///commands 字体颜色,背景色,字号,字体,下划线,删除线
  4964. ///commandsName ForeColor,BackColor,FontSize,FontFamily,Underline,StrikeThrough
  4965. ///commandsTitle 字体颜色,背景色,字号,字体,下划线,删除线
  4966. /**
  4967. * @description 字体
  4968. * @name UM.execCommand
  4969. * @param {String} cmdName 执行的功能名称
  4970. * @param {String} value 传入的值
  4971. */
  4972. UM.plugins['font'] = function () {
  4973. var me = this,
  4974. fonts = {
  4975. 'forecolor': 'forecolor',
  4976. 'backcolor': 'backcolor',
  4977. 'fontsize': 'fontsize',
  4978. 'fontfamily': 'fontname'
  4979. },
  4980. cmdNameToStyle = {
  4981. 'forecolor': 'color',
  4982. 'backcolor': 'background-color',
  4983. 'fontsize': 'font-size',
  4984. 'fontfamily': 'font-family'
  4985. },
  4986. cmdNameToAttr = {
  4987. 'forecolor': 'color',
  4988. 'fontsize': 'size',
  4989. 'fontfamily': 'face'
  4990. };
  4991. me.setOpt({
  4992. 'fontfamily': [
  4993. { name: 'songti', val: '宋体,SimSun'},
  4994. { name: 'yahei', val: '微软雅黑,Microsoft YaHei'},
  4995. { name: 'kaiti', val: '楷体,楷体_GB2312, SimKai'},
  4996. { name: 'heiti', val: '黑体, SimHei'},
  4997. { name: 'lishu', val: '隶书, SimLi'},
  4998. { name: 'andaleMono', val: 'andale mono'},
  4999. { name: 'arial', val: 'arial, helvetica,sans-serif'},
  5000. { name: 'arialBlack', val: 'arial black,avant garde'},
  5001. { name: 'comicSansMs', val: 'comic sans ms'},
  5002. { name: 'impact', val: 'impact,chicago'},
  5003. { name: 'timesNewRoman', val: 'times new roman'},
  5004. { name: 'sans-serif',val:'sans-serif'}
  5005. ],
  5006. 'fontsize': [10, 12, 16, 18,24, 32,48]
  5007. });
  5008. me.addOutputRule(function (root) {
  5009. utils.each(root.getNodesByTagName('font'), function (node) {
  5010. if (node.tagName == 'font') {
  5011. var cssStyle = [];
  5012. for (var p in node.attrs) {
  5013. switch (p) {
  5014. case 'size':
  5015. var val = node.attrs[p];
  5016. $.each({
  5017. '10':'1',
  5018. '12':'2',
  5019. '16':'3',
  5020. '18':'4',
  5021. '24':'5',
  5022. '32':'6',
  5023. '48':'7'
  5024. },function(k,v){
  5025. if(v == val){
  5026. val = k;
  5027. return false;
  5028. }
  5029. });
  5030. cssStyle.push('font-size:' + val + 'px');
  5031. break;
  5032. case 'color':
  5033. cssStyle.push('color:' + node.attrs[p]);
  5034. break;
  5035. case 'face':
  5036. cssStyle.push('font-family:' + node.attrs[p]);
  5037. break;
  5038. case 'style':
  5039. cssStyle.push(node.attrs[p]);
  5040. }
  5041. }
  5042. node.attrs = {
  5043. 'style': cssStyle.join(';')
  5044. };
  5045. }
  5046. node.tagName = 'span';
  5047. if(node.parentNode.tagName == 'span' && node.parentNode.children.length == 1){
  5048. $.each(node.attrs,function(k,v){
  5049. node.parentNode.attrs[k] = k == 'style' ? node.parentNode.attrs[k] + v : v;
  5050. })
  5051. node.parentNode.removeChild(node,true);
  5052. }
  5053. });
  5054. });
  5055. for(var p in fonts){
  5056. (function (cmd) {
  5057. UM.commands[cmd] = {
  5058. execCommand: function (cmdName,value) {
  5059. if(value == 'transparent'){
  5060. return;
  5061. }
  5062. var rng = this.selection.getRange();
  5063. if(rng.collapsed){
  5064. var span = $('<span></span>').css(cmdNameToStyle[cmdName],value)[0];
  5065. rng.insertNode(span).setStart(span,0).setCursor();
  5066. }else{
  5067. if(cmdName == 'fontsize'){
  5068. value = {
  5069. '10':'1',
  5070. '12':'2',
  5071. '16':'3',
  5072. '18':'4',
  5073. '24':'5',
  5074. '32':'6',
  5075. '48':'7'
  5076. }[(value+"").replace(/px/,'')]
  5077. }
  5078. this.document.execCommand(fonts[cmdName],false, value);
  5079. if(browser.gecko){
  5080. $.each(this.$body.find('a'),function(i,a){
  5081. var parent = a.parentNode;
  5082. if(parent.lastChild === parent.firstChild && /FONT|SPAN/.test(parent.tagName)){
  5083. var cloneNode = parent.cloneNode(false);
  5084. cloneNode.innerHTML = a.innerHTML;
  5085. $(a).html('').append(cloneNode).insertBefore(parent);
  5086. $(parent).remove();
  5087. }
  5088. });
  5089. }
  5090. if(!browser.ie){
  5091. var nativeRange = this.selection.getNative().getRangeAt(0);
  5092. var common = nativeRange.commonAncestorContainer;
  5093. var rng = this.selection.getRange(),
  5094. bk = rng.createBookmark(true);
  5095. $(common).find('a').each(function(i,n){
  5096. var parent = n.parentNode;
  5097. if(parent.nodeName == 'FONT'){
  5098. var font = parent.cloneNode(false);
  5099. font.innerHTML = n.innerHTML;
  5100. $(n).html('').append(font);
  5101. }
  5102. });
  5103. rng.moveToBookmark(bk).select()
  5104. }
  5105. return true
  5106. }
  5107. },
  5108. queryCommandValue: function (cmdName) {
  5109. var start = me.selection.getStart();
  5110. var val = $(start).css(cmdNameToStyle[cmdName]);
  5111. if(val === undefined){
  5112. val = $(start).attr(cmdNameToAttr[cmdName])
  5113. }
  5114. return val ? utils.fixColor(cmdName,val).replace(/px/,'') : '';
  5115. },
  5116. queryCommandState: function (cmdName) {
  5117. return this.queryCommandValue(cmdName)
  5118. }
  5119. };
  5120. })(p);
  5121. }
  5122. };
  5123. ///import core
  5124. ///commands 超链接,取消链接
  5125. ///commandsName Link,Unlink
  5126. ///commandsTitle 超链接,取消链接
  5127. ///commandsDialog dialogs\link
  5128. /**
  5129. * 超链接
  5130. * @function
  5131. * @name UM.execCommand
  5132. * @param {String} cmdName link插入超链接
  5133. * @param {Object} options url地址,title标题,target是否打开新页
  5134. * @author zhanyi
  5135. */
  5136. /**
  5137. * 取消链接
  5138. * @function
  5139. * @name UM.execCommand
  5140. * @param {String} cmdName unlink取消链接
  5141. * @author zhanyi
  5142. */
  5143. UM.plugins['link'] = function(){
  5144. this.setOpt('autourldetectinie',false);
  5145. //在ie下禁用autolink
  5146. if(browser.ie && this.options.autourldetectinie === false){
  5147. this.addListener('keyup',function(cmd,evt){
  5148. var me = this,keyCode = evt.keyCode;
  5149. if(keyCode == 13 || keyCode == 32){
  5150. var rng = me.selection.getRange();
  5151. var start = rng.startContainer;
  5152. if(keyCode == 13){
  5153. if(start.nodeName == 'P'){
  5154. var pre = start.previousSibling;
  5155. if(pre && pre.nodeType == 1){
  5156. var pre = pre.lastChild;
  5157. if(pre && pre.nodeName == 'A' && !pre.getAttribute('_href')){
  5158. domUtils.remove(pre,true);
  5159. }
  5160. }
  5161. }
  5162. }else if(keyCode == 32){
  5163. if(start.nodeType == 3 && /^\s$/.test(start.nodeValue)){
  5164. start = start.previousSibling;
  5165. if(start && start.nodeName == 'A' && !start.getAttribute('_href')){
  5166. domUtils.remove(start,true);
  5167. }
  5168. }
  5169. }
  5170. }
  5171. });
  5172. }
  5173. this.addOutputRule(function(root){
  5174. $.each(root.getNodesByTagName('a'),function(i,a){
  5175. var _href = utils.html(a.getAttr('_href'));
  5176. if(!/^(ftp|https?|\/|file)/.test(_href)){
  5177. _href = 'http://' + _href;
  5178. }
  5179. a.setAttr('href', _href);
  5180. a.setAttr('_href')
  5181. if(a.getAttr('title')==''){
  5182. a.setAttr('title')
  5183. }
  5184. })
  5185. });
  5186. this.addInputRule(function(root){
  5187. $.each(root.getNodesByTagName('a'),function(i,a){
  5188. a.setAttr('_href', utils.html(a.getAttr('href')));
  5189. })
  5190. });
  5191. UM.commands['link'] = {
  5192. execCommand : function( cmdName, opt ) {
  5193. var me = this;
  5194. var rng = me.selection.getRange();
  5195. if(rng.collapsed){
  5196. var start = rng.startContainer;
  5197. if(start = domUtils.findParentByTagName(start,'a',true)){
  5198. $(start).attr(opt);
  5199. rng.selectNode(start).select()
  5200. }else{
  5201. rng.insertNode($('<a>' +opt.href+'</a>').attr(opt)[0]).select()
  5202. }
  5203. }else{
  5204. me.document.execCommand('createlink',false,'_umeditor_link');
  5205. utils.each(domUtils.getElementsByTagName(me.body,'a',function(n){
  5206. return n.getAttribute('href') == '_umeditor_link'
  5207. }),function(l){
  5208. if($(l).text() == '_umeditor_link'){
  5209. $(l).text(opt.href);
  5210. }
  5211. domUtils.setAttributes(l,opt);
  5212. rng.selectNode(l).select()
  5213. })
  5214. }
  5215. },
  5216. queryCommandState:function(){
  5217. return this.queryCommandValue('link') ? 1 : 0;
  5218. },
  5219. queryCommandValue:function(){
  5220. var path = this.selection.getStartElementPath();
  5221. var result;
  5222. $.each(path,function(i,n){
  5223. if(n.nodeName == "A"){
  5224. result = n;
  5225. return false;
  5226. }
  5227. })
  5228. return result;
  5229. }
  5230. };
  5231. UM.commands['unlink'] = {
  5232. execCommand : function() {
  5233. this.document.execCommand('unlink');
  5234. }
  5235. };
  5236. };
  5237. ///import core
  5238. ///commands 打印
  5239. ///commandsName Print
  5240. ///commandsTitle 打印
  5241. /**
  5242. * @description 打印
  5243. * @name baidu.editor.execCommand
  5244. * @param {String} cmdName print打印编辑器内容
  5245. * @author zhanyi
  5246. */
  5247. UM.commands['print'] = {
  5248. execCommand : function(){
  5249. var me = this,
  5250. id = 'editor_print_' + +new Date();
  5251. $('<iframe src="" id="' + id + '" name="' + id + '" frameborder="0"></iframe>').attr('id', id)
  5252. .css({
  5253. width:'0px',
  5254. height:'0px',
  5255. 'overflow':'hidden',
  5256. 'float':'left',
  5257. 'position':'absolute',
  5258. top:'-10000px',
  5259. left:'-10000px'
  5260. })
  5261. .appendTo(me.$container.find('.edui-dialog-container'));
  5262. var w = window.open('', id, ''),
  5263. d = w.document;
  5264. d.open();
  5265. d.write('<html><head></head><body><div>'+this.getContent(null,null,true)+'</div><script>' +
  5266. "setTimeout(function(){" +
  5267. "window.print();" +
  5268. "setTimeout(function(){" +
  5269. "window.parent.$('#" + id + "').remove();" +
  5270. "},100);" +
  5271. "},200);" +
  5272. '</script></body></html>');
  5273. d.close();
  5274. },
  5275. notNeedUndo : 1
  5276. };
  5277. ///import core
  5278. ///commands 格式
  5279. ///commandsName Paragraph
  5280. ///commandsTitle 段落格式
  5281. /**
  5282. * 段落样式
  5283. * @function
  5284. * @name UM.execCommand
  5285. * @param {String} cmdName paragraph插入段落执行命令
  5286. * @param {String} style 标签值为:'p', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6'
  5287. * @param {String} attrs 标签的属性
  5288. * @author zhanyi
  5289. */
  5290. UM.plugins['paragraph'] = function() {
  5291. var me = this;
  5292. me.setOpt('paragraph',{'p':'', 'h1':'', 'h2':'', 'h3':'', 'h4':'', 'h5':'', 'h6':''});
  5293. me.commands['paragraph'] = {
  5294. execCommand : function( cmdName, style ) {
  5295. return this.document.execCommand('formatBlock',false,'<' + style + '>');
  5296. },
  5297. queryCommandValue : function() {
  5298. try{
  5299. var val = this.document.queryCommandValue('formatBlock')
  5300. }catch(e){
  5301. }
  5302. return val ;
  5303. }
  5304. };
  5305. };
  5306. ///import core
  5307. ///import plugins\inserthtml.js
  5308. ///commands 分割线
  5309. ///commandsName Horizontal
  5310. ///commandsTitle 分隔线
  5311. /**
  5312. * 分割线
  5313. * @function
  5314. * @name UM.execCommand
  5315. * @param {String} cmdName horizontal插入分割线
  5316. */
  5317. UM.plugins['horizontal'] = function(){
  5318. var me = this;
  5319. me.commands['horizontal'] = {
  5320. execCommand : function( ) {
  5321. this.document.execCommand('insertHorizontalRule');
  5322. var rng = me.selection.getRange().txtToElmBoundary(true),
  5323. start = rng.startContainer;
  5324. if(domUtils.isBody(rng.startContainer)){
  5325. var next = rng.startContainer.childNodes[rng.startOffset];
  5326. if(!next){
  5327. next = $('<p></p>').appendTo(rng.startContainer).html(browser.ie ? '&nbsp;' : '<br/>')[0]
  5328. }
  5329. rng.setStart(next,0).setCursor()
  5330. }else{
  5331. while(dtd.$inline[start.tagName] && start.lastChild === start.firstChild){
  5332. var parent = start.parentNode;
  5333. parent.appendChild(start.firstChild);
  5334. parent.removeChild(start);
  5335. start = parent;
  5336. }
  5337. while(dtd.$inline[start.tagName]){
  5338. start = start.parentNode;
  5339. }
  5340. if(start.childNodes.length == 1 && start.lastChild.nodeName == 'HR'){
  5341. var hr = start.lastChild;
  5342. $(hr).insertBefore(start);
  5343. rng.setStart(start,0).setCursor();
  5344. }else{
  5345. hr = $('hr',start)[0];
  5346. domUtils.breakParent(hr,start);
  5347. var pre = hr.previousSibling;
  5348. if(pre && domUtils.isEmptyBlock(pre)){
  5349. $(pre).remove()
  5350. }
  5351. rng.setStart(hr.nextSibling,0).setCursor();
  5352. }
  5353. }
  5354. }
  5355. };
  5356. };
  5357. ///import core
  5358. ///commands 清空文档
  5359. ///commandsName ClearDoc
  5360. ///commandsTitle 清空文档
  5361. /**
  5362. *
  5363. * 清空文档
  5364. * @function
  5365. * @name UM.execCommand
  5366. * @param {String} cmdName cleardoc清空文档
  5367. */
  5368. UM.commands['cleardoc'] = {
  5369. execCommand : function() {
  5370. var me = this,
  5371. range = me.selection.getRange();
  5372. me.body.innerHTML = "<p>"+(ie ? "" : "<br/>")+"</p>";
  5373. range.setStart(me.body.firstChild,0).setCursor(false,true);
  5374. setTimeout(function(){
  5375. me.fireEvent("clearDoc");
  5376. },0);
  5377. }
  5378. };
  5379. ///import core
  5380. ///commands 撤销和重做
  5381. ///commandsName Undo,Redo
  5382. ///commandsTitle 撤销,重做
  5383. /**
  5384. * @description 回退
  5385. * @author zhanyi
  5386. */
  5387. UM.plugins['undo'] = function () {
  5388. var saveSceneTimer;
  5389. var me = this,
  5390. maxUndoCount = me.options.maxUndoCount || 20,
  5391. maxInputCount = me.options.maxInputCount || 20,
  5392. fillchar = new RegExp(domUtils.fillChar + '|<\/hr>', 'gi');// ie会产生多余的</hr>
  5393. var noNeedFillCharTags = {
  5394. ol:1,ul:1,table:1,tbody:1,tr:1,body:1
  5395. };
  5396. var orgState = me.options.autoClearEmptyNode;
  5397. function compareAddr(indexA, indexB) {
  5398. if (indexA.length != indexB.length)
  5399. return 0;
  5400. for (var i = 0, l = indexA.length; i < l; i++) {
  5401. if (indexA[i] != indexB[i])
  5402. return 0
  5403. }
  5404. return 1;
  5405. }
  5406. function compareRangeAddress(rngAddrA, rngAddrB) {
  5407. if (rngAddrA.collapsed != rngAddrB.collapsed) {
  5408. return 0;
  5409. }
  5410. if (!compareAddr(rngAddrA.startAddress, rngAddrB.startAddress) || !compareAddr(rngAddrA.endAddress, rngAddrB.endAddress)) {
  5411. return 0;
  5412. }
  5413. return 1;
  5414. }
  5415. function UndoManager() {
  5416. this.list = [];
  5417. this.index = 0;
  5418. this.hasUndo = false;
  5419. this.hasRedo = false;
  5420. this.undo = function () {
  5421. if (this.hasUndo) {
  5422. if (!this.list[this.index - 1] && this.list.length == 1) {
  5423. this.reset();
  5424. return;
  5425. }
  5426. while (this.list[this.index].content == this.list[this.index - 1].content) {
  5427. this.index--;
  5428. if (this.index == 0) {
  5429. return this.restore(0);
  5430. }
  5431. }
  5432. this.restore(--this.index);
  5433. }
  5434. };
  5435. this.redo = function () {
  5436. if (this.hasRedo) {
  5437. while (this.list[this.index].content == this.list[this.index + 1].content) {
  5438. this.index++;
  5439. if (this.index == this.list.length - 1) {
  5440. return this.restore(this.index);
  5441. }
  5442. }
  5443. this.restore(++this.index);
  5444. }
  5445. };
  5446. this.restore = function () {
  5447. var me = this.editor;
  5448. var scene = this.list[this.index];
  5449. var root = UM.htmlparser(scene.content.replace(fillchar, ''));
  5450. me.options.autoClearEmptyNode = false;
  5451. me.filterInputRule(root,true);
  5452. me.options.autoClearEmptyNode = orgState;
  5453. //trace:873
  5454. //去掉展位符
  5455. me.body.innerHTML = root.toHtml();
  5456. me.fireEvent('afterscencerestore');
  5457. //处理undo后空格不展位的问题
  5458. if (browser.ie) {
  5459. utils.each(domUtils.getElementsByTagName(me.document,'td th caption p'),function(node){
  5460. if(domUtils.isEmptyNode(node)){
  5461. domUtils.fillNode(me.document, node);
  5462. }
  5463. })
  5464. }
  5465. try{
  5466. var rng = new dom.Range(me.document,me.body).moveToAddress(scene.address);
  5467. if(browser.ie && rng.collapsed && rng.startContainer.nodeType == 1){
  5468. var tmpNode = rng.startContainer.childNodes[rng.startOffset];
  5469. if( !tmpNode || tmpNode.nodeType == 1 && dtd.$empty[tmpNode]){
  5470. rng.insertNode(me.document.createTextNode(' ')).collapse(true);
  5471. }
  5472. }
  5473. rng.select(noNeedFillCharTags[rng.startContainer.nodeName.toLowerCase()]);
  5474. }catch(e){}
  5475. this.update();
  5476. this.clearKey();
  5477. //不能把自己reset了
  5478. me.fireEvent('reset', true);
  5479. };
  5480. this.getScene = function () {
  5481. var me = this.editor;
  5482. var rng = me.selection.getRange(),
  5483. rngAddress = rng.createAddress(false,true);
  5484. me.fireEvent('beforegetscene');
  5485. var root = UM.htmlparser(me.body.innerHTML,true);
  5486. me.options.autoClearEmptyNode = false;
  5487. me.filterOutputRule(root,true);
  5488. me.options.autoClearEmptyNode = orgState;
  5489. var cont = root.toHtml();
  5490. browser.ie && (cont = cont.replace(/>&nbsp;</g, '><').replace(/\s*</g, '<').replace(/>\s*/g, '>'));
  5491. me.fireEvent('aftergetscene');
  5492. return {
  5493. address:rngAddress,
  5494. content:cont
  5495. }
  5496. };
  5497. this.save = function (notCompareRange,notSetCursor) {
  5498. clearTimeout(saveSceneTimer);
  5499. var currentScene = this.getScene(notSetCursor),
  5500. lastScene = this.list[this.index];
  5501. //内容相同位置相同不存
  5502. if (lastScene && lastScene.content == currentScene.content &&
  5503. ( notCompareRange ? 1 : compareRangeAddress(lastScene.address, currentScene.address) )
  5504. ) {
  5505. return;
  5506. }
  5507. this.list = this.list.slice(0, this.index + 1);
  5508. this.list.push(currentScene);
  5509. //如果大于最大数量了,就把最前的剔除
  5510. if (this.list.length > maxUndoCount) {
  5511. this.list.shift();
  5512. }
  5513. this.index = this.list.length - 1;
  5514. this.clearKey();
  5515. //跟新undo/redo状态
  5516. this.update();
  5517. };
  5518. this.update = function () {
  5519. this.hasRedo = !!this.list[this.index + 1];
  5520. this.hasUndo = !!this.list[this.index - 1];
  5521. };
  5522. this.reset = function () {
  5523. this.list = [];
  5524. this.index = 0;
  5525. this.hasUndo = false;
  5526. this.hasRedo = false;
  5527. this.clearKey();
  5528. };
  5529. this.clearKey = function () {
  5530. keycont = 0;
  5531. lastKeyCode = null;
  5532. };
  5533. }
  5534. me.undoManger = new UndoManager();
  5535. me.undoManger.editor = me;
  5536. function saveScene() {
  5537. this.undoManger.save();
  5538. }
  5539. me.addListener('saveScene', function () {
  5540. var args = Array.prototype.splice.call(arguments,1);
  5541. this.undoManger.save.apply(this.undoManger,args);
  5542. });
  5543. me.addListener('beforeexeccommand', saveScene);
  5544. me.addListener('afterexeccommand', saveScene);
  5545. me.addListener('reset', function (type, exclude) {
  5546. if (!exclude) {
  5547. this.undoManger.reset();
  5548. }
  5549. });
  5550. me.commands['redo'] = me.commands['undo'] = {
  5551. execCommand:function (cmdName) {
  5552. this.undoManger[cmdName]();
  5553. },
  5554. queryCommandState:function (cmdName) {
  5555. return this.undoManger['has' + (cmdName.toLowerCase() == 'undo' ? 'Undo' : 'Redo')] ? 0 : -1;
  5556. },
  5557. notNeedUndo:1
  5558. };
  5559. var keys = {
  5560. // /*Backspace*/ 8:1, /*Delete*/ 46:1,
  5561. /*Shift*/ 16:1, /*Ctrl*/ 17:1, /*Alt*/ 18:1,
  5562. 37:1, 38:1, 39:1, 40:1
  5563. },
  5564. keycont = 0,
  5565. lastKeyCode;
  5566. //输入法状态下不计算字符数
  5567. var inputType = false;
  5568. me.addListener('ready', function () {
  5569. domUtils.on(this.body, 'compositionstart', function () {
  5570. inputType = true;
  5571. });
  5572. domUtils.on(this.body, 'compositionend', function () {
  5573. inputType = false;
  5574. })
  5575. });
  5576. //快捷键
  5577. me.addshortcutkey({
  5578. "Undo":"ctrl+90", //undo
  5579. "Redo":"ctrl+89,shift+ctrl+z" //redo
  5580. });
  5581. var isCollapsed = true;
  5582. me.addListener('keydown', function (type, evt) {
  5583. var me = this;
  5584. var keyCode = evt.keyCode || evt.which;
  5585. if (!keys[keyCode] && !evt.ctrlKey && !evt.metaKey && !evt.shiftKey && !evt.altKey) {
  5586. if (inputType)
  5587. return;
  5588. if(!me.selection.getRange().collapsed){
  5589. me.undoManger.save(false,true);
  5590. isCollapsed = false;
  5591. return;
  5592. }
  5593. if (me.undoManger.list.length == 0) {
  5594. me.undoManger.save(true);
  5595. }
  5596. clearTimeout(saveSceneTimer);
  5597. function save(cont){
  5598. if (cont.selection.getRange().collapsed)
  5599. cont.fireEvent('contentchange');
  5600. cont.undoManger.save(false,true);
  5601. cont.fireEvent('selectionchange');
  5602. }
  5603. saveSceneTimer = setTimeout(function(){
  5604. if(inputType){
  5605. var interalTimer = setInterval(function(){
  5606. if(!inputType){
  5607. save(me);
  5608. clearInterval(interalTimer)
  5609. }
  5610. },300)
  5611. return;
  5612. }
  5613. save(me);
  5614. },200);
  5615. lastKeyCode = keyCode;
  5616. keycont++;
  5617. if (keycont >= maxInputCount ) {
  5618. save(me)
  5619. }
  5620. }
  5621. });
  5622. me.addListener('keyup', function (type, evt) {
  5623. var keyCode = evt.keyCode || evt.which;
  5624. if (!keys[keyCode] && !evt.ctrlKey && !evt.metaKey && !evt.shiftKey && !evt.altKey) {
  5625. if (inputType)
  5626. return;
  5627. if(!isCollapsed){
  5628. this.undoManger.save(false,true);
  5629. isCollapsed = true;
  5630. }
  5631. }
  5632. });
  5633. };
  5634. ///import core
  5635. ///import plugins/inserthtml.js
  5636. ///import plugins/undo.js
  5637. ///import plugins/serialize.js
  5638. ///commands 粘贴
  5639. ///commandsName PastePlain
  5640. ///commandsTitle 纯文本粘贴模式
  5641. /*
  5642. ** @description 粘贴
  5643. * @author zhanyi
  5644. */
  5645. UM.plugins['paste'] = function () {
  5646. function getClipboardData(callback) {
  5647. var doc = this.document;
  5648. if (doc.getElementById('baidu_pastebin')) {
  5649. return;
  5650. }
  5651. var range = this.selection.getRange(),
  5652. bk = range.createBookmark(),
  5653. //创建剪贴的容器div
  5654. pastebin = doc.createElement('div');
  5655. pastebin.id = 'baidu_pastebin';
  5656. // Safari 要求div必须有内容,才能粘贴内容进来
  5657. browser.webkit && pastebin.appendChild(doc.createTextNode(domUtils.fillChar + domUtils.fillChar));
  5658. this.body.appendChild(pastebin);
  5659. //trace:717 隐藏的span不能得到top
  5660. //bk.start.innerHTML = '&nbsp;';
  5661. bk.start.style.display = '';
  5662. pastebin.style.cssText = "position:absolute;width:1px;height:1px;overflow:hidden;left:-1000px;white-space:nowrap;top:" +
  5663. //要在现在光标平行的位置加入,否则会出现跳动的问题
  5664. $(bk.start).position().top + 'px';
  5665. range.selectNodeContents(pastebin).select(true);
  5666. setTimeout(function () {
  5667. if (browser.webkit) {
  5668. for (var i = 0, pastebins = doc.querySelectorAll('#baidu_pastebin'), pi; pi = pastebins[i++];) {
  5669. if (domUtils.isEmptyNode(pi)) {
  5670. domUtils.remove(pi);
  5671. } else {
  5672. pastebin = pi;
  5673. break;
  5674. }
  5675. }
  5676. }
  5677. try {
  5678. pastebin.parentNode.removeChild(pastebin);
  5679. } catch (e) {
  5680. }
  5681. range.moveToBookmark(bk).select(true);
  5682. callback(pastebin);
  5683. }, 0);
  5684. }
  5685. var me = this;
  5686. function filter(div) {
  5687. var html;
  5688. if (div.firstChild) {
  5689. //去掉cut中添加的边界值
  5690. var nodes = domUtils.getElementsByTagName(div, 'span');
  5691. for (var i = 0, ni; ni = nodes[i++];) {
  5692. if (ni.id == '_baidu_cut_start' || ni.id == '_baidu_cut_end') {
  5693. domUtils.remove(ni);
  5694. }
  5695. }
  5696. if (browser.webkit) {
  5697. var brs = div.querySelectorAll('div br');
  5698. for (var i = 0, bi; bi = brs[i++];) {
  5699. var pN = bi.parentNode;
  5700. if (pN.tagName == 'DIV' && pN.childNodes.length == 1) {
  5701. pN.innerHTML = '<p><br/></p>';
  5702. domUtils.remove(pN);
  5703. }
  5704. }
  5705. var divs = div.querySelectorAll('#baidu_pastebin');
  5706. for (var i = 0, di; di = divs[i++];) {
  5707. var tmpP = me.document.createElement('p');
  5708. di.parentNode.insertBefore(tmpP, di);
  5709. while (di.firstChild) {
  5710. tmpP.appendChild(di.firstChild);
  5711. }
  5712. domUtils.remove(di);
  5713. }
  5714. var metas = div.querySelectorAll('meta');
  5715. for (var i = 0, ci; ci = metas[i++];) {
  5716. domUtils.remove(ci);
  5717. }
  5718. var brs = div.querySelectorAll('br');
  5719. for (i = 0; ci = brs[i++];) {
  5720. if (/^apple-/i.test(ci.className)) {
  5721. domUtils.remove(ci);
  5722. }
  5723. }
  5724. }
  5725. if (browser.gecko) {
  5726. var dirtyNodes = div.querySelectorAll('[_moz_dirty]');
  5727. for (i = 0; ci = dirtyNodes[i++];) {
  5728. ci.removeAttribute('_moz_dirty');
  5729. }
  5730. }
  5731. if (!browser.ie) {
  5732. var spans = div.querySelectorAll('span.Apple-style-span');
  5733. for (var i = 0, ci; ci = spans[i++];) {
  5734. domUtils.remove(ci, true);
  5735. }
  5736. }
  5737. //ie下使用innerHTML会产生多余的\r\n字符,也会产生&nbsp;这里过滤掉
  5738. html = div.innerHTML;//.replace(/>(?:(\s|&nbsp;)*?)</g,'><');
  5739. //过滤word粘贴过来的冗余属性
  5740. html = UM.filterWord(html);
  5741. //取消了忽略空白的第二个参数,粘贴过来的有些是有空白的,会被套上相关的标签
  5742. var root = UM.htmlparser(html);
  5743. //如果给了过滤规则就先进行过滤
  5744. if (me.options.filterRules) {
  5745. UM.filterNode(root, me.options.filterRules);
  5746. }
  5747. //执行默认的处理
  5748. me.filterInputRule(root);
  5749. //针对chrome的处理
  5750. if (browser.webkit) {
  5751. var br = root.lastChild();
  5752. if (br && br.type == 'element' && br.tagName == 'br') {
  5753. root.removeChild(br)
  5754. }
  5755. utils.each(me.body.querySelectorAll('div'), function (node) {
  5756. if (domUtils.isEmptyBlock(node)) {
  5757. domUtils.remove(node)
  5758. }
  5759. })
  5760. }
  5761. html = {'html': root.toHtml()};
  5762. me.fireEvent('beforepaste', html, root);
  5763. //抢了默认的粘贴,那后边的内容就不执行了,比如表格粘贴
  5764. if(!html.html){
  5765. return;
  5766. }
  5767. me.execCommand('insertHtml', html.html, true);
  5768. me.fireEvent("afterpaste", html);
  5769. }
  5770. }
  5771. me.addListener('ready', function () {
  5772. domUtils.on(me.body, 'cut', function () {
  5773. var range = me.selection.getRange();
  5774. if (!range.collapsed && me.undoManger) {
  5775. me.undoManger.save();
  5776. }
  5777. });
  5778. //ie下beforepaste在点击右键时也会触发,所以用监控键盘才处理
  5779. domUtils.on(me.body, browser.ie || browser.opera ? 'keydown' : 'paste', function (e) {
  5780. if ((browser.ie || browser.opera) && ((!e.ctrlKey && !e.metaKey) || e.keyCode != '86')) {
  5781. return;
  5782. }
  5783. getClipboardData.call(me, function (div) {
  5784. filter(div);
  5785. });
  5786. });
  5787. });
  5788. };
  5789. ///import core
  5790. ///commands 有序列表,无序列表
  5791. ///commandsName InsertOrderedList,InsertUnorderedList
  5792. ///commandsTitle 有序列表,无序列表
  5793. /**
  5794. * 有序列表
  5795. * @function
  5796. * @name UM.execCommand
  5797. * @param {String} cmdName insertorderlist插入有序列表
  5798. * @param {String} style 值为:decimal,lower-alpha,lower-roman,upper-alpha,upper-roman
  5799. * @author zhanyi
  5800. */
  5801. /**
  5802. * 无序链接
  5803. * @function
  5804. * @name UM.execCommand
  5805. * @param {String} cmdName insertunorderlist插入无序列表
  5806. * * @param {String} style 值为:circle,disc,square
  5807. * @author zhanyi
  5808. */
  5809. UM.plugins['list'] = function () {
  5810. var me = this;
  5811. me.setOpt( {
  5812. 'insertorderedlist':{
  5813. 'decimal':'',
  5814. 'lower-alpha':'',
  5815. 'lower-roman':'',
  5816. 'upper-alpha':'',
  5817. 'upper-roman':''
  5818. },
  5819. 'insertunorderedlist':{
  5820. 'circle':'',
  5821. 'disc':'',
  5822. 'square':''
  5823. }
  5824. } );
  5825. this.addInputRule(function(root){
  5826. utils.each(root.getNodesByTagName('li'), function (node) {
  5827. if(node.children.length == 0){
  5828. node.parentNode.removeChild(node)
  5829. }
  5830. })
  5831. });
  5832. me.commands['insertorderedlist'] =
  5833. me.commands['insertunorderedlist'] = {
  5834. execCommand:function (cmdName) {
  5835. this.document.execCommand(cmdName);
  5836. var rng = this.selection.getRange(),
  5837. bk = rng.createBookmark(true);
  5838. this.$body.find('ol,ul').each(function(i,n){
  5839. var parent = n.parentNode;
  5840. if(parent.tagName == 'P' && parent.lastChild === parent.firstChild){
  5841. $(n).children().each(function(j,li){
  5842. var p = parent.cloneNode(false);
  5843. $(p).append(li.innerHTML);
  5844. $(li).html('').append(p)
  5845. })
  5846. $(n).insertBefore(parent);
  5847. $(parent).remove();
  5848. }
  5849. });
  5850. rng.moveToBookmark(bk).select();
  5851. return true;
  5852. },
  5853. queryCommandState:function (cmdName) {
  5854. return this.document.queryCommandState(cmdName)
  5855. }
  5856. };
  5857. };
  5858. ///import core
  5859. ///import plugins/serialize.js
  5860. ///import plugins/undo.js
  5861. ///commands 查看源码
  5862. ///commandsName Source
  5863. ///commandsTitle 查看源码
  5864. (function (){
  5865. var sourceEditors = {
  5866. textarea: function (editor, holder){
  5867. var textarea = holder.ownerDocument.createElement('textarea');
  5868. textarea.style.cssText = 'resize:none;border:0;padding:0;margin:0;overflow-y:auto;outline:0';
  5869. // todo: IE下只有onresize属性可用... 很纠结
  5870. if (browser.ie && browser.version < 8) {
  5871. textarea.style.width = holder.offsetWidth + 'px';
  5872. textarea.style.height = holder.offsetHeight + 'px';
  5873. holder.onresize = function (){
  5874. textarea.style.width = holder.offsetWidth + 'px';
  5875. textarea.style.height = holder.offsetHeight + 'px';
  5876. };
  5877. }
  5878. holder.appendChild(textarea);
  5879. return {
  5880. container : textarea,
  5881. setContent: function (content){
  5882. textarea.value = content;
  5883. },
  5884. getContent: function (){
  5885. return textarea.value;
  5886. },
  5887. select: function (){
  5888. var range;
  5889. if (browser.ie) {
  5890. range = textarea.createTextRange();
  5891. range.collapse(true);
  5892. range.select();
  5893. } else {
  5894. //todo: chrome下无法设置焦点
  5895. textarea.setSelectionRange(0, 0);
  5896. textarea.focus();
  5897. }
  5898. },
  5899. dispose: function (){
  5900. holder.removeChild(textarea);
  5901. // todo
  5902. holder.onresize = null;
  5903. textarea = null;
  5904. holder = null;
  5905. }
  5906. };
  5907. }
  5908. };
  5909. UM.plugins['source'] = function (){
  5910. var me = this;
  5911. var opt = this.options;
  5912. var sourceMode = false;
  5913. var sourceEditor;
  5914. opt.sourceEditor = 'textarea';
  5915. me.setOpt({
  5916. sourceEditorFirst:false
  5917. });
  5918. function createSourceEditor(holder){
  5919. return sourceEditors.textarea(me, holder);
  5920. }
  5921. var bakCssText;
  5922. //解决在源码模式下getContent不能得到最新的内容问题
  5923. var oldGetContent = me.getContent,
  5924. bakAddress;
  5925. me.commands['source'] = {
  5926. execCommand: function (){
  5927. sourceMode = !sourceMode;
  5928. if (sourceMode) {
  5929. bakAddress = me.selection.getRange().createAddress(false,true);
  5930. me.undoManger && me.undoManger.save(true);
  5931. if(browser.gecko){
  5932. me.body.contentEditable = false;
  5933. }
  5934. // bakCssText = me.body.style.cssText;
  5935. me.body.style.cssText += ';position:absolute;left:-32768px;top:-32768px;';
  5936. me.fireEvent('beforegetcontent');
  5937. var root = UM.htmlparser(me.body.innerHTML);
  5938. me.filterOutputRule(root);
  5939. root.traversal(function (node) {
  5940. if (node.type == 'element') {
  5941. switch (node.tagName) {
  5942. case 'td':
  5943. case 'th':
  5944. case 'caption':
  5945. if(node.children && node.children.length == 1){
  5946. if(node.firstChild().tagName == 'br' ){
  5947. node.removeChild(node.firstChild())
  5948. }
  5949. };
  5950. break;
  5951. case 'pre':
  5952. node.innerText(node.innerText().replace(/&nbsp;/g,' '))
  5953. }
  5954. }
  5955. });
  5956. me.fireEvent('aftergetcontent');
  5957. var content = root.toHtml(true);
  5958. sourceEditor = createSourceEditor(me.body.parentNode);
  5959. sourceEditor.setContent(content);
  5960. var getStyleValue=function(attr){
  5961. return parseInt($(me.body).css(attr));
  5962. };
  5963. $(sourceEditor.container).width($(me.body).width()+getStyleValue("padding-left")+getStyleValue("padding-right"))
  5964. .height($(me.body).height());
  5965. setTimeout(function (){
  5966. sourceEditor.select();
  5967. });
  5968. //重置getContent,源码模式下取值也能是最新的数据
  5969. me.getContent = function (){
  5970. return sourceEditor.getContent() || '<p>' + (browser.ie ? '' : '<br/>')+'</p>';
  5971. };
  5972. } else {
  5973. me.$body.css({
  5974. 'position':'',
  5975. 'left':'',
  5976. 'top':''
  5977. });
  5978. // me.body.style.cssText = bakCssText;
  5979. var cont = sourceEditor.getContent() || '<p>' + (browser.ie ? '' : '<br/>')+'</p>';
  5980. //处理掉block节点前后的空格,有可能会误命中,暂时不考虑
  5981. cont = cont.replace(new RegExp('[\\r\\t\\n ]*<\/?(\\w+)\\s*(?:[^>]*)>','g'), function(a,b){
  5982. if(b && !dtd.$inlineWithA[b.toLowerCase()]){
  5983. return a.replace(/(^[\n\r\t ]*)|([\n\r\t ]*$)/g,'');
  5984. }
  5985. return a.replace(/(^[\n\r\t]*)|([\n\r\t]*$)/g,'')
  5986. });
  5987. me.setContent(cont);
  5988. sourceEditor.dispose();
  5989. sourceEditor = null;
  5990. //还原getContent方法
  5991. me.getContent = oldGetContent;
  5992. var first = me.body.firstChild;
  5993. //trace:1106 都删除空了,下边会报错,所以补充一个p占位
  5994. if(!first){
  5995. me.body.innerHTML = '<p>'+(browser.ie?'':'<br/>')+'</p>';
  5996. }
  5997. //要在ifm为显示时ff才能取到selection,否则报错
  5998. //这里不能比较位置了
  5999. me.undoManger && me.undoManger.save(true);
  6000. if(browser.gecko){
  6001. me.body.contentEditable = true;
  6002. }
  6003. try{
  6004. me.selection.getRange().moveToAddress(bakAddress).select();
  6005. }catch(e){}
  6006. }
  6007. this.fireEvent('sourcemodechanged', sourceMode);
  6008. },
  6009. queryCommandState: function (){
  6010. return sourceMode|0;
  6011. },
  6012. notNeedUndo : 1
  6013. };
  6014. var oldQueryCommandState = me.queryCommandState;
  6015. me.queryCommandState = function (cmdName){
  6016. cmdName = cmdName.toLowerCase();
  6017. if (sourceMode) {
  6018. //源码模式下可以开启的命令
  6019. return cmdName in {
  6020. 'source' : 1,
  6021. 'fullscreen' : 1
  6022. } ? oldQueryCommandState.apply(this, arguments) : -1
  6023. }
  6024. return oldQueryCommandState.apply(this, arguments);
  6025. };
  6026. };
  6027. })();
  6028. ///import core
  6029. ///import plugins/undo.js
  6030. ///commands 设置回车标签p或br
  6031. ///commandsName EnterKey
  6032. ///commandsTitle 设置回车标签p或br
  6033. /**
  6034. * @description 处理回车
  6035. * @author zhanyi
  6036. */
  6037. UM.plugins['enterkey'] = function() {
  6038. var hTag,
  6039. me = this,
  6040. tag = me.options.enterTag;
  6041. me.addListener('keyup', function(type, evt) {
  6042. var keyCode = evt.keyCode || evt.which;
  6043. if (keyCode == 13) {
  6044. var range = me.selection.getRange(),
  6045. start = range.startContainer,
  6046. doSave;
  6047. //修正在h1-h6里边回车后不能嵌套p的问题
  6048. if (!browser.ie) {
  6049. if (/h\d/i.test(hTag)) {
  6050. if (browser.gecko) {
  6051. var h = domUtils.findParentByTagName(start, [ 'h1', 'h2', 'h3', 'h4', 'h5', 'h6','blockquote','caption','table'], true);
  6052. if (!h) {
  6053. me.document.execCommand('formatBlock', false, '<p>');
  6054. doSave = 1;
  6055. }
  6056. } else {
  6057. //chrome remove div
  6058. if (start.nodeType == 1) {
  6059. var tmp = me.document.createTextNode(''),div;
  6060. range.insertNode(tmp);
  6061. div = domUtils.findParentByTagName(tmp, 'div', true);
  6062. if (div) {
  6063. var p = me.document.createElement('p');
  6064. while (div.firstChild) {
  6065. p.appendChild(div.firstChild);
  6066. }
  6067. div.parentNode.insertBefore(p, div);
  6068. domUtils.remove(div);
  6069. range.setStartBefore(tmp).setCursor();
  6070. doSave = 1;
  6071. }
  6072. domUtils.remove(tmp);
  6073. }
  6074. }
  6075. if (me.undoManger && doSave) {
  6076. me.undoManger.save();
  6077. }
  6078. }
  6079. //没有站位符,会出现多行的问题
  6080. browser.opera && range.select();
  6081. }else{
  6082. me.fireEvent('saveScene',true,true)
  6083. }
  6084. }
  6085. });
  6086. me.addListener('keydown', function(type, evt) {
  6087. var keyCode = evt.keyCode || evt.which;
  6088. if (keyCode == 13) {//回车
  6089. if(me.fireEvent('beforeenterkeydown')){
  6090. domUtils.preventDefault(evt);
  6091. return;
  6092. }
  6093. me.fireEvent('saveScene',true,true);
  6094. hTag = '';
  6095. var range = me.selection.getRange();
  6096. if (!range.collapsed) {
  6097. //跨td不能删
  6098. var start = range.startContainer,
  6099. end = range.endContainer,
  6100. startTd = domUtils.findParentByTagName(start, 'td', true),
  6101. endTd = domUtils.findParentByTagName(end, 'td', true);
  6102. if (startTd && endTd && startTd !== endTd || !startTd && endTd || startTd && !endTd) {
  6103. evt.preventDefault ? evt.preventDefault() : ( evt.returnValue = false);
  6104. return;
  6105. }
  6106. }
  6107. if (tag == 'p') {
  6108. if (!browser.ie) {
  6109. start = domUtils.findParentByTagName(range.startContainer, ['ol','ul','p', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6','blockquote','caption'], true);
  6110. //opera下执行formatblock会在table的场景下有问题,回车在opera原生支持很好,所以暂时在opera去掉调用这个原生的command
  6111. //trace:2431
  6112. if (!start && !browser.opera) {
  6113. me.document.execCommand('formatBlock', false, '<p>');
  6114. if (browser.gecko) {
  6115. range = me.selection.getRange();
  6116. start = domUtils.findParentByTagName(range.startContainer, 'p', true);
  6117. start && domUtils.removeDirtyAttr(start);
  6118. }
  6119. } else {
  6120. hTag = start.tagName;
  6121. start.tagName.toLowerCase() == 'p' && browser.gecko && domUtils.removeDirtyAttr(start);
  6122. }
  6123. }
  6124. }
  6125. }
  6126. });
  6127. };
  6128. ///import core
  6129. ///commands 预览
  6130. ///commandsName Preview
  6131. ///commandsTitle 预览
  6132. /**
  6133. * 预览
  6134. * @function
  6135. * @name UM.execCommand
  6136. * @param {String} cmdName preview预览编辑器内容
  6137. */
  6138. UM.commands['preview'] = {
  6139. execCommand : function(){
  6140. var w = window.open('', '_blank', ''),
  6141. d = w.document;
  6142. d.open();
  6143. d.write('<html><head></head><body><div>'+this.getContent(null,null,true)+'</div></body></html>');
  6144. d.close();
  6145. },
  6146. notNeedUndo : 1
  6147. };
  6148. ///import core
  6149. ///commands 加粗,斜体,上标,下标
  6150. ///commandsName Bold,Italic,Subscript,Superscript
  6151. ///commandsTitle 加粗,加斜,下标,上标
  6152. /**
  6153. * b u i等基础功能实现
  6154. * @function
  6155. * @name UM.execCommands
  6156. * @param {String} cmdName bold加粗。italic斜体。subscript上标。superscript下标。
  6157. */
  6158. UM.plugins['basestyle'] = function(){
  6159. var basestyles = ['bold','underline','superscript','subscript','italic','strikethrough'],
  6160. me = this;
  6161. //添加快捷键
  6162. me.addshortcutkey({
  6163. "Bold" : "ctrl+66",//^B
  6164. "Italic" : "ctrl+73", //^I
  6165. "Underline" : "ctrl+shift+85",//^U
  6166. "strikeThrough" : 'ctrl+shift+83' //^s
  6167. });
  6168. //过滤最后的产出数据
  6169. me.addOutputRule(function(root){
  6170. $.each(root.getNodesByTagName('b i u strike s'),function(i,node){
  6171. switch (node.tagName){
  6172. case 'b':
  6173. node.tagName = 'strong';
  6174. break;
  6175. case 'i':
  6176. node.tagName = 'em';
  6177. break;
  6178. case 'u':
  6179. node.tagName = 'span';
  6180. node.setStyle('text-decoration','underline');
  6181. break;
  6182. case 's':
  6183. case 'strike':
  6184. node.tagName = 'span';
  6185. node.setStyle('text-decoration','line-through')
  6186. }
  6187. });
  6188. });
  6189. $.each(basestyles,function(i,cmd){
  6190. me.commands[cmd] = {
  6191. execCommand : function( cmdName ) {
  6192. var rng = this.selection.getRange();
  6193. if(rng.collapsed && this.queryCommandState(cmdName) != 1){
  6194. var node = this.document.createElement({
  6195. 'bold':'strong',
  6196. 'underline':'u',
  6197. 'superscript':'sup',
  6198. 'subscript':'sub',
  6199. 'italic':'em',
  6200. 'strikethrough':'strike'
  6201. }[cmdName]);
  6202. rng.insertNode(node).setStart(node,0).setCursor(false);
  6203. return true;
  6204. }else{
  6205. return this.document.execCommand(cmdName)
  6206. }
  6207. },
  6208. queryCommandState : function(cmdName) {
  6209. if(browser.gecko){
  6210. return this.document.queryCommandState(cmdName)
  6211. }
  6212. var path = this.selection.getStartElementPath(),result = false;
  6213. $.each(path,function(i,n){
  6214. switch (cmdName){
  6215. case 'bold':
  6216. if(n.nodeName == 'STRONG' || n.nodeName == 'B'){
  6217. result = 1;
  6218. return false;
  6219. }
  6220. break;
  6221. case 'underline':
  6222. if(n.nodeName == 'U' || n.nodeName == 'SPAN' && $(n).css('text-decoration') == 'underline'){
  6223. result = 1;
  6224. return false;
  6225. }
  6226. break;
  6227. case 'superscript':
  6228. if(n.nodeName == 'SUP'){
  6229. result = 1;
  6230. return false;
  6231. }
  6232. break;
  6233. case 'subscript':
  6234. if(n.nodeName == 'SUB'){
  6235. result = 1;
  6236. return false;
  6237. }
  6238. break;
  6239. case 'italic':
  6240. if(n.nodeName == 'EM' || n.nodeName == 'I'){
  6241. result = 1;
  6242. return false;
  6243. }
  6244. break;
  6245. case 'strikethrough':
  6246. if(n.nodeName == 'S' || n.nodeName == 'STRIKE' || n.nodeName == 'SPAN' && $(n).css('text-decoration') == 'line-through'){
  6247. result = 1;
  6248. return false;
  6249. }
  6250. break;
  6251. }
  6252. })
  6253. return result
  6254. }
  6255. };
  6256. })
  6257. };
  6258. ///import core
  6259. ///import plugins/inserthtml.js
  6260. ///commands 视频
  6261. ///commandsName InsertVideo
  6262. ///commandsTitle 插入视频
  6263. ///commandsDialog dialogs\video
  6264. UM.plugins['video'] = function (){
  6265. var me =this,
  6266. div;
  6267. /**
  6268. * 创建插入视频字符窜
  6269. * @param url 视频地址
  6270. * @param width 视频宽度
  6271. * @param height 视频高度
  6272. * @param align 视频对齐
  6273. * @param toEmbed 是否以flash代替显示
  6274. * @param addParagraph 是否需要添加P 标签
  6275. */
  6276. function creatInsertStr(url,width,height,id,align,toEmbed){
  6277. return !toEmbed ?
  6278. '<img ' + (id ? 'id="' + id+'"' : '') + ' width="'+ width +'" height="' + height + '" _url="'+url+'" class="edui-faked-video"' +
  6279. ' src="' + me.options.UMEDITOR_HOME_URL+'themes/default/images/spacer.gif" style="background:url('+me.options.UMEDITOR_HOME_URL+'themes/default/images/videologo.gif) no-repeat center center; border:1px solid gray;'+(align ? 'float:' + align + ';': '')+'" />'
  6280. :
  6281. '<embed type="application/x-shockwave-flash" class="edui-faked-video" pluginspage="http://www.macromedia.com/go/getflashplayer"' +
  6282. ' src="' + url + '" width="' + width + '" height="' + height + '"' + (align ? ' style="float:' + align + '"': '') +
  6283. ' wmode="transparent" play="true" loop="false" menu="false" allowscriptaccess="never" allowfullscreen="true" >';
  6284. }
  6285. function switchImgAndEmbed(root,img2embed){
  6286. utils.each(root.getNodesByTagName(img2embed ? 'img' : 'embed'),function(node){
  6287. if(node.getAttr('class') == 'edui-faked-video'){
  6288. var html = creatInsertStr( img2embed ? node.getAttr('_url') : node.getAttr('src'),node.getAttr('width'),node.getAttr('height'),null,node.getStyle('float') || '',img2embed);
  6289. node.parentNode.replaceChild(UM.uNode.createElement(html),node)
  6290. }
  6291. })
  6292. }
  6293. me.addOutputRule(function(root){
  6294. switchImgAndEmbed(root,true)
  6295. });
  6296. me.addInputRule(function(root){
  6297. switchImgAndEmbed(root)
  6298. });
  6299. me.commands["insertvideo"] = {
  6300. execCommand: function (cmd, videoObjs){
  6301. videoObjs = utils.isArray(videoObjs)?videoObjs:[videoObjs];
  6302. var html = [],id = 'tmpVedio';
  6303. for(var i=0,vi,len = videoObjs.length;i<len;i++){
  6304. vi = videoObjs[i];
  6305. html.push(creatInsertStr( vi.url, vi.width || 420, vi.height || 280, id + i,vi.align,false));
  6306. }
  6307. me.execCommand("inserthtml",html.join(""),true);
  6308. },
  6309. queryCommandState : function(){
  6310. var img = me.selection.getRange().getClosedNode(),
  6311. flag = img && (img.className == "edui-faked-video");
  6312. return flag ? 1 : 0;
  6313. }
  6314. };
  6315. };
  6316. ///import core
  6317. ///commands 全选
  6318. ///commandsName SelectAll
  6319. ///commandsTitle 全选
  6320. /**
  6321. * 选中所有
  6322. * @function
  6323. * @name UM.execCommand
  6324. * @param {String} cmdName selectall选中编辑器里的所有内容
  6325. * @author zhanyi
  6326. */
  6327. UM.plugins['selectall'] = function(){
  6328. var me = this;
  6329. me.commands['selectall'] = {
  6330. execCommand : function(){
  6331. //去掉了原生的selectAll,因为会出现报错和当内容为空时,不能出现闭合状态的光标
  6332. var me = this,body = me.body,
  6333. range = me.selection.getRange();
  6334. range.selectNodeContents(body);
  6335. if(domUtils.isEmptyBlock(body)){
  6336. //opera不能自动合并到元素的里边,要手动处理一下
  6337. if(browser.opera && body.firstChild && body.firstChild.nodeType == 1){
  6338. range.setStartAtFirst(body.firstChild);
  6339. }
  6340. range.collapse(true);
  6341. }
  6342. range.select(true);
  6343. },
  6344. notNeedUndo : 1
  6345. };
  6346. //快捷键
  6347. me.addshortcutkey({
  6348. "selectAll" : "ctrl+65"
  6349. });
  6350. };
  6351. UM.plugins['removeformat'] = function(){
  6352. var me = this;
  6353. me.commands['removeformat'] = {
  6354. execCommand : function( ) {
  6355. this.document.execCommand('removeformat');
  6356. }
  6357. };
  6358. };
  6359. /*
  6360. * 处理特殊键的兼容性问题
  6361. */
  6362. UM.plugins['keystrokes'] = function() {
  6363. var me = this;
  6364. var collapsed = true;
  6365. me.addListener('keydown', function(type, evt) {
  6366. var keyCode = evt.keyCode || evt.which,
  6367. rng = me.selection.getRange();
  6368. //处理全选的情况
  6369. if(!rng.collapsed && !(evt.ctrlKey || evt.shiftKey || evt.altKey || evt.metaKey) && (keyCode >= 65 && keyCode <=90
  6370. || keyCode >= 48 && keyCode <= 57 ||
  6371. keyCode >= 96 && keyCode <= 111 || {
  6372. 13:1,
  6373. 8:1,
  6374. 46:1
  6375. }[keyCode])
  6376. ){
  6377. var tmpNode = rng.startContainer;
  6378. if(domUtils.isFillChar(tmpNode)){
  6379. rng.setStartBefore(tmpNode)
  6380. }
  6381. tmpNode = rng.endContainer;
  6382. if(domUtils.isFillChar(tmpNode)){
  6383. rng.setEndAfter(tmpNode)
  6384. }
  6385. rng.txtToElmBoundary();
  6386. //结束边界可能放到了br的前边,要把br包含进来
  6387. // x[xxx]<br/>
  6388. if(rng.endContainer && rng.endContainer.nodeType == 1){
  6389. tmpNode = rng.endContainer.childNodes[rng.endOffset];
  6390. if(tmpNode && domUtils.isBr(tmpNode)){
  6391. rng.setEndAfter(tmpNode);
  6392. }
  6393. }
  6394. if(rng.startOffset == 0){
  6395. tmpNode = rng.startContainer;
  6396. if(domUtils.isBoundaryNode(tmpNode,'firstChild') ){
  6397. tmpNode = rng.endContainer;
  6398. if(rng.endOffset == (tmpNode.nodeType == 3 ? tmpNode.nodeValue.length : tmpNode.childNodes.length) && domUtils.isBoundaryNode(tmpNode,'lastChild')){
  6399. me.fireEvent('saveScene');
  6400. me.body.innerHTML = '<p>'+(browser.ie ? '' : '<br/>')+'</p>';
  6401. rng.setStart(me.body.firstChild,0).setCursor(false,true);
  6402. me._selectionChange();
  6403. return;
  6404. }
  6405. }
  6406. }
  6407. }
  6408. //处理backspace
  6409. if (keyCode == 8) {
  6410. rng = me.selection.getRange();
  6411. collapsed = rng.collapsed;
  6412. if(me.fireEvent('delkeydown',evt)){
  6413. return;
  6414. }
  6415. var start,end;
  6416. //避免按两次删除才能生效的问题
  6417. if(rng.collapsed && rng.inFillChar()){
  6418. start = rng.startContainer;
  6419. if(domUtils.isFillChar(start)){
  6420. rng.setStartBefore(start).shrinkBoundary(true).collapse(true);
  6421. domUtils.remove(start)
  6422. }else{
  6423. start.nodeValue = start.nodeValue.replace(new RegExp('^' + domUtils.fillChar ),'');
  6424. rng.startOffset--;
  6425. rng.collapse(true).select(true)
  6426. }
  6427. }
  6428. //解决选中control元素不能删除的问题
  6429. if (start = rng.getClosedNode()) {
  6430. me.fireEvent('saveScene');
  6431. rng.setStartBefore(start);
  6432. domUtils.remove(start);
  6433. rng.setCursor();
  6434. me.fireEvent('saveScene');
  6435. domUtils.preventDefault(evt);
  6436. return;
  6437. }
  6438. //阻止在table上的删除
  6439. if (!browser.ie) {
  6440. start = domUtils.findParentByTagName(rng.startContainer, 'table', true);
  6441. end = domUtils.findParentByTagName(rng.endContainer, 'table', true);
  6442. if (start && !end || !start && end || start !== end) {
  6443. evt.preventDefault();
  6444. return;
  6445. }
  6446. }
  6447. start = rng.startContainer;
  6448. if(rng.collapsed && start.nodeType == 1){
  6449. var currentNode = start.childNodes[rng.startOffset-1];
  6450. if(currentNode && currentNode.nodeType == 1 && currentNode.tagName == 'BR'){
  6451. me.fireEvent('saveScene');
  6452. rng.setStartBefore(currentNode).collapse(true);
  6453. domUtils.remove(currentNode);
  6454. rng.select();
  6455. me.fireEvent('saveScene');
  6456. }
  6457. }
  6458. //trace:3613
  6459. if(browser.chrome){
  6460. if(rng.collapsed){
  6461. while(rng.startOffset == 0 && !domUtils.isEmptyBlock(rng.startContainer)){
  6462. rng.setStartBefore(rng.startContainer)
  6463. }
  6464. var pre = rng.startContainer.childNodes[rng.startOffset-1];
  6465. if(pre && pre.nodeName == 'BR'){
  6466. rng.setStartBefore(pre);
  6467. me.fireEvent('saveScene');
  6468. $(pre).remove();
  6469. rng.setCursor();
  6470. me.fireEvent('saveScene');
  6471. }
  6472. }
  6473. }
  6474. }
  6475. //trace:1634
  6476. //ff的del键在容器空的时候,也会删除
  6477. if(browser.gecko && keyCode == 46){
  6478. var range = me.selection.getRange();
  6479. if(range.collapsed){
  6480. start = range.startContainer;
  6481. if(domUtils.isEmptyBlock(start)){
  6482. var parent = start.parentNode;
  6483. while(domUtils.getChildCount(parent) == 1 && !domUtils.isBody(parent)){
  6484. start = parent;
  6485. parent = parent.parentNode;
  6486. }
  6487. if(start === parent.lastChild)
  6488. evt.preventDefault();
  6489. return;
  6490. }
  6491. }
  6492. }
  6493. });
  6494. me.addListener('keyup', function(type, evt) {
  6495. var keyCode = evt.keyCode || evt.which,
  6496. rng,me = this;
  6497. if(keyCode == 8){
  6498. if(me.fireEvent('delkeyup')){
  6499. return;
  6500. }
  6501. rng = me.selection.getRange();
  6502. if(rng.collapsed){
  6503. var tmpNode,
  6504. autoClearTagName = ['h1','h2','h3','h4','h5','h6'];
  6505. if(tmpNode = domUtils.findParentByTagName(rng.startContainer,autoClearTagName,true)){
  6506. if(domUtils.isEmptyBlock(tmpNode)){
  6507. var pre = tmpNode.previousSibling;
  6508. if(pre && pre.nodeName != 'TABLE'){
  6509. domUtils.remove(tmpNode);
  6510. rng.setStartAtLast(pre).setCursor(false,true);
  6511. return;
  6512. }else{
  6513. var next = tmpNode.nextSibling;
  6514. if(next && next.nodeName != 'TABLE'){
  6515. domUtils.remove(tmpNode);
  6516. rng.setStartAtFirst(next).setCursor(false,true);
  6517. return;
  6518. }
  6519. }
  6520. }
  6521. }
  6522. //处理当删除到body时,要重新给p标签展位
  6523. if(domUtils.isBody(rng.startContainer)){
  6524. var tmpNode = domUtils.createElement(me.document,'p',{
  6525. 'innerHTML' : browser.ie ? domUtils.fillChar : '<br/>'
  6526. });
  6527. rng.insertNode(tmpNode).setStart(tmpNode,0).setCursor(false,true);
  6528. }
  6529. }
  6530. //chrome下如果删除了inline标签,浏览器会有记忆,在输入文字还是会套上刚才删除的标签,所以这里再选一次就不会了
  6531. if( !collapsed && (rng.startContainer.nodeType == 3 || rng.startContainer.nodeType == 1 && domUtils.isEmptyBlock(rng.startContainer))){
  6532. if(browser.ie){
  6533. var span = rng.document.createElement('span');
  6534. rng.insertNode(span).setStartBefore(span).collapse(true);
  6535. rng.select();
  6536. domUtils.remove(span)
  6537. }else{
  6538. rng.select()
  6539. }
  6540. }
  6541. }
  6542. })
  6543. };
  6544. /*
  6545. * 拖放文件到编辑器,上传并插入
  6546. */
  6547. UM.plugins['dropfile'] = function() {
  6548. var me = this;
  6549. me.setOpt('dropFileEnabled', true);
  6550. if( me.getOpt('dropFileEnabled') && window.FormData && window.FileReader) {
  6551. me.addListener('ready', function(){
  6552. me.$body.on('drop',function (e) {
  6553. try{
  6554. //获取文件列表
  6555. var fileList = e.originalEvent.dataTransfer.files;
  6556. var hasImg = false;
  6557. if(fileList) {
  6558. $.each(fileList, function (i, f) {
  6559. if (/^image/.test(f.type)) {
  6560. var xhr = new XMLHttpRequest();
  6561. xhr.open("post", me.getOpt('imageUrl'), true);
  6562. xhr.setRequestHeader("X-Requested-With", "XMLHttpRequest");
  6563. //模拟数据
  6564. var fd = new FormData();
  6565. fd.append(me.getOpt('imageFieldName') || 'upfile', f);
  6566. fd.append('type', 'ajax');
  6567. xhr.send(fd);
  6568. xhr.addEventListener('load', function (e) {
  6569. var picLink = me.getOpt('imagePath') + e.target.response;
  6570. if(picLink) {
  6571. me.execCommand('insertimage', {
  6572. src: picLink,
  6573. _src: picLink
  6574. });
  6575. }
  6576. });
  6577. hasImg = true;
  6578. }
  6579. });
  6580. if(hasImg) e.preventDefault();
  6581. }
  6582. }catch(e){}
  6583. }).on('dragover', function (e) {
  6584. if(e.originalEvent.dataTransfer.types[0] == 'Files') {
  6585. e.preventDefault();
  6586. }
  6587. });
  6588. });
  6589. }
  6590. };
  6591. (function ($) {
  6592. //对jquery的扩展
  6593. $.parseTmpl = function parse(str, data) {
  6594. var tmpl = 'var __p=[],print=function(){__p.push.apply(__p,arguments);};' + 'with(obj||{}){__p.push(\'' + str.replace(/\\/g, '\\\\').replace(/'/g, "\\'").replace(/<%=([\s\S]+?)%>/g,function (match, code) {
  6595. return "'," + code.replace(/\\'/g, "'") + ",'";
  6596. }).replace(/<%([\s\S]+?)%>/g,function (match, code) {
  6597. return "');" + code.replace(/\\'/g, "'").replace(/[\r\n\t]/g, ' ') + "__p.push('";
  6598. }).replace(/\r/g, '\\r').replace(/\n/g, '\\n').replace(/\t/g, '\\t') + "');}return __p.join('');";
  6599. var func = new Function('obj', tmpl);
  6600. return data ? func(data) : func;
  6601. };
  6602. $.extend2 = function (t, s) {
  6603. var a = arguments,
  6604. notCover = $.type(a[a.length - 1]) == 'boolean' ? a[a.length - 1] : false,
  6605. len = $.type(a[a.length - 1]) == 'boolean' ? a.length - 1 : a.length;
  6606. for (var i = 1; i < len; i++) {
  6607. var x = a[i];
  6608. for (var k in x) {
  6609. if (!notCover || !t.hasOwnProperty(k)) {
  6610. t[k] = x[k];
  6611. }
  6612. }
  6613. }
  6614. return t;
  6615. };
  6616. $.IE6 = !!window.ActiveXObject && parseFloat(navigator.userAgent.match(/msie (\d+)/i)[1]) == 6;
  6617. //所有ui的基类
  6618. var _eventHandler = [];
  6619. var _widget = function () {
  6620. };
  6621. var _prefix = 'edui';
  6622. _widget.prototype = {
  6623. on: function (ev, cb) {
  6624. this.root().on(ev, $.proxy(cb, this));
  6625. return this;
  6626. },
  6627. off: function (ev, cb) {
  6628. this.root().off(ev, $.proxy(cb, this));
  6629. return this;
  6630. },
  6631. trigger: function (ev, data) {
  6632. return this.root().trigger(ev, data) === false ? false : this;
  6633. },
  6634. root: function ($el) {
  6635. return this._$el || (this._$el = $el);
  6636. },
  6637. destroy: function () {
  6638. },
  6639. data: function (key, val) {
  6640. if (val !== undefined) {
  6641. this.root().data(_prefix + key, val);
  6642. return this;
  6643. } else {
  6644. return this.root().data(_prefix + key)
  6645. }
  6646. },
  6647. register: function (eventName, $el, fn) {
  6648. _eventHandler.push({
  6649. 'evtname': eventName,
  6650. '$els': $.isArray($el) ? $el : [$el],
  6651. handler: $.proxy(fn, $el)
  6652. })
  6653. }
  6654. };
  6655. //从jq实例上拿到绑定的widget实例
  6656. $.fn.edui = function (obj) {
  6657. return obj ? this.data('eduiwidget', obj) : this.data('eduiwidget');
  6658. };
  6659. function _createClass(ClassObj, properties, supperClass) {
  6660. ClassObj.prototype = $.extend2(
  6661. $.extend({}, properties),
  6662. (UM.ui[supperClass] || _widget).prototype,
  6663. true
  6664. );
  6665. ClassObj.prototype.supper = (UM.ui[supperClass] || _widget).prototype;
  6666. //父class的defaultOpt 合并
  6667. if( UM.ui[supperClass] && UM.ui[supperClass].prototype.defaultOpt ) {
  6668. var parentDefaultOptions = UM.ui[supperClass].prototype.defaultOpt,
  6669. subDefaultOptions = ClassObj.prototype.defaultOpt;
  6670. ClassObj.prototype.defaultOpt = $.extend( {}, parentDefaultOptions, subDefaultOptions || {} );
  6671. }
  6672. return ClassObj
  6673. }
  6674. var _guid = 1;
  6675. function mergeToJQ(ClassObj, className) {
  6676. $[_prefix + className] = ClassObj;
  6677. $.fn[_prefix + className] = function (opt) {
  6678. var result, args = Array.prototype.slice.call(arguments, 1);
  6679. this.each(function (i, el) {
  6680. var $this = $(el);
  6681. var obj = $this.edui();
  6682. if (!obj) {
  6683. ClassObj(!opt || !$.isPlainObject(opt) ? {} : opt, $this);
  6684. $this.edui(obj)
  6685. }
  6686. if ($.type(opt) == 'string') {
  6687. if (opt == 'this') {
  6688. result = obj;
  6689. } else {
  6690. result = obj[opt].apply(obj, args);
  6691. if (result !== obj && result !== undefined) {
  6692. return false;
  6693. }
  6694. result = null;
  6695. }
  6696. }
  6697. });
  6698. return result !== null ? result : this;
  6699. }
  6700. }
  6701. UM.ui = {
  6702. define: function (className, properties, supperClass) {
  6703. var ClassObj = UM.ui[className] = _createClass(function (options, $el) {
  6704. var _obj = function () {
  6705. };
  6706. $.extend(_obj.prototype, ClassObj.prototype, {
  6707. guid: className + _guid++,
  6708. widgetName: className
  6709. }
  6710. );
  6711. var obj = new _obj;
  6712. if ($.type(options) == 'string') {
  6713. obj.init && obj.init({});
  6714. obj.root().edui(obj);
  6715. obj.root().find('a').click(function (evt) {
  6716. evt.preventDefault()
  6717. });
  6718. return obj.root()[_prefix + className].apply(obj.root(), arguments)
  6719. } else {
  6720. $el && obj.root($el);
  6721. obj.init && obj.init(!options || $.isPlainObject(options) ? $.extend2(options || {}, obj.defaultOpt || {}, true) : options);
  6722. try{
  6723. obj.root().find('a').click(function (evt) {
  6724. evt.preventDefault()
  6725. });
  6726. }catch(e){
  6727. }
  6728. return obj.root().edui(obj);
  6729. }
  6730. },properties, supperClass);
  6731. mergeToJQ(ClassObj, className);
  6732. }
  6733. };
  6734. $(function () {
  6735. $(document).on('click mouseup mousedown dblclick mouseover', function (evt) {
  6736. $.each(_eventHandler, function (i, obj) {
  6737. if (obj.evtname == evt.type) {
  6738. $.each(obj.$els, function (i, $el) {
  6739. if ($el[0] !== evt.target && !$.contains($el[0], evt.target)) {
  6740. obj.handler(evt);
  6741. }
  6742. })
  6743. }
  6744. })
  6745. })
  6746. })
  6747. })(jQuery);
  6748. //button 类
  6749. UM.ui.define('button', {
  6750. tpl: '<<%if(!texttype){%>div class="edui-btn edui-btn-<%=icon%> <%if(name){%>edui-btn-name-<%=name%><%}%>" unselectable="on" onmousedown="return false" <%}else{%>a class="edui-text-btn"<%}%><% if(title) {%> data-original-title="<%=title%>" <%};%>> ' +
  6751. '<% if(icon) {%><div unselectable="on" class="edui-icon-<%=icon%> edui-icon"></div><% }; %><%if(text) {%><span unselectable="on" onmousedown="return false" class="edui-button-label"><%=text%></span><%}%>' +
  6752. '<%if(caret && text){%><span class="edui-button-spacing"></span><%}%>' +
  6753. '<% if(caret) {%><span unselectable="on" onmousedown="return false" class="edui-caret"></span><% };%></<%if(!texttype){%>div<%}else{%>a<%}%>>',
  6754. defaultOpt: {
  6755. text: '',
  6756. title: '',
  6757. icon: '',
  6758. width: '',
  6759. caret: false,
  6760. texttype: false,
  6761. click: function () {
  6762. }
  6763. },
  6764. init: function (options) {
  6765. var me = this;
  6766. me.root($($.parseTmpl(me.tpl, options)))
  6767. .click(function (evt) {
  6768. me.wrapclick(options.click, evt)
  6769. });
  6770. me.root().hover(function () {
  6771. if(!me.root().hasClass("disabled")){
  6772. me.root().toggleClass('hover')
  6773. }
  6774. })
  6775. return me;
  6776. },
  6777. wrapclick: function (fn, evt) {
  6778. if (!this.disabled()) {
  6779. this.root().trigger('wrapclick');
  6780. $.proxy(fn, this, evt)()
  6781. }
  6782. return this;
  6783. },
  6784. label: function (text) {
  6785. if (text === undefined) {
  6786. return this.root().find('.edui-button-label').text();
  6787. } else {
  6788. this.root().find('.edui-button-label').text(text);
  6789. return this;
  6790. }
  6791. },
  6792. disabled: function (state) {
  6793. if (state === undefined) {
  6794. return this.root().hasClass('disabled')
  6795. }
  6796. this.root().toggleClass('disabled', state);
  6797. if(this.root().hasClass('disabled')){
  6798. this.root().removeClass('hover')
  6799. }
  6800. return this;
  6801. },
  6802. active: function (state) {
  6803. if (state === undefined) {
  6804. return this.root().hasClass('active')
  6805. }
  6806. this.root().toggleClass('active', state)
  6807. return this;
  6808. },
  6809. mergeWith: function ($obj) {
  6810. var me = this;
  6811. me.data('$mergeObj', $obj);
  6812. $obj.edui().data('$mergeObj', me.root());
  6813. if (!$.contains(document.body, $obj[0])) {
  6814. $obj.appendTo(me.root());
  6815. }
  6816. me.on('click',function () {
  6817. me.wrapclick(function () {
  6818. $obj.edui().show();
  6819. })
  6820. }).register('click', me.root(), function (evt) {
  6821. $obj.hide()
  6822. });
  6823. }
  6824. });
  6825. //toolbar 类
  6826. (function () {
  6827. UM.ui.define('toolbar', {
  6828. tpl: '<div class="edui-toolbar" ><div class="edui-btn-toolbar" unselectable="on" onmousedown="return false" ></div></div>'
  6829. ,
  6830. init: function () {
  6831. var $root = this.root($(this.tpl));
  6832. this.data('$btnToolbar', $root.find('.edui-btn-toolbar'))
  6833. },
  6834. appendToBtnmenu : function(data){
  6835. var $cont = this.data('$btnToolbar');
  6836. data = $.isArray(data) ? data : [data];
  6837. $.each(data,function(i,$item){
  6838. $cont.append($item)
  6839. })
  6840. }
  6841. });
  6842. })();
  6843. //menu 类
  6844. UM.ui.define('menu',{
  6845. show : function($obj,dir,fnname,topOffset,leftOffset){
  6846. fnname = fnname || 'position';
  6847. if(this.trigger('beforeshow') === false){
  6848. return;
  6849. }else{
  6850. this.root().css($.extend({display:'block'},$obj ? {
  6851. top : $obj[fnname]().top + ( dir == 'right' ? 0 : $obj.outerHeight()) - (topOffset || 0),
  6852. left : $obj[fnname]().left + (dir == 'right' ? $obj.outerWidth() : 0) - (leftOffset || 0)
  6853. }:{}))
  6854. this.trigger('aftershow');
  6855. }
  6856. },
  6857. hide : function(all){
  6858. var $parentmenu;
  6859. if(this.trigger('beforehide') === false){
  6860. return;
  6861. } else {
  6862. if($parentmenu = this.root().data('parentmenu')){
  6863. if($parentmenu.data('parentmenu')|| all)
  6864. $parentmenu.edui().hide();
  6865. }
  6866. this.root().css('display','none');
  6867. this.trigger('afterhide');
  6868. }
  6869. },
  6870. attachTo : function($obj){
  6871. var me = this;
  6872. if(!$obj.data('$mergeObj')){
  6873. $obj.data('$mergeObj',me.root());
  6874. $obj.on('wrapclick',function(evt){
  6875. me.show()
  6876. });
  6877. me.register('click',$obj,function(evt){
  6878. me.hide()
  6879. });
  6880. me.data('$mergeObj',$obj)
  6881. }
  6882. }
  6883. });
  6884. //dropmenu 类
  6885. UM.ui.define('dropmenu', {
  6886. tmpl: '<ul class="edui-dropdown-menu" aria-labelledby="dropdownMenu" >' +
  6887. '<%for(var i=0,ci;ci=data[i++];){%>' +
  6888. '<%if(ci.divider){%><li class="edui-divider"></li><%}else{%>' +
  6889. '<li <%if(ci.active||ci.disabled){%>class="<%= ci.active|| \'\' %> <%=ci.disabled||\'\' %>" <%}%> data-value="<%= ci.value%>">' +
  6890. '<a href="#" tabindex="-1"><em class="edui-dropmenu-checkbox"><i class="edui-icon-ok"></i></em><%= ci.label%></a>' +
  6891. '</li><%}%>' +
  6892. '<%}%>' +
  6893. '</ul>',
  6894. defaultOpt: {
  6895. data: [],
  6896. click: function () {
  6897. }
  6898. },
  6899. init: function (options) {
  6900. var me = this;
  6901. var eventName = {
  6902. click: 1,
  6903. mouseover: 1,
  6904. mouseout: 1
  6905. };
  6906. this.root($($.parseTmpl(this.tmpl, options))).on('click', 'li[class!="disabled divider dropdown-submenu"]',function (evt) {
  6907. $.proxy(options.click, me, evt, $(this).data('value'), $(this))()
  6908. }).find('li').each(function (i, el) {
  6909. var $this = $(this);
  6910. if (!$this.hasClass("disabled divider dropdown-submenu")) {
  6911. var data = options.data[i];
  6912. $.each(eventName, function (k) {
  6913. data[k] && $this[k](function (evt) {
  6914. $.proxy(data[k], el)(evt, data, me.root)
  6915. })
  6916. })
  6917. }
  6918. })
  6919. },
  6920. disabled: function (cb) {
  6921. $('li[class!=divider]', this.root()).each(function () {
  6922. var $el = $(this);
  6923. if (cb === true) {
  6924. $el.addClass('disabled')
  6925. } else if ($.isFunction(cb)) {
  6926. $el.toggleClass('disabled', cb(li))
  6927. } else {
  6928. $el.removeClass('disabled')
  6929. }
  6930. });
  6931. },
  6932. val: function (val) {
  6933. var currentVal;
  6934. $('li[class!="divider disabled dropdown-submenu"]', this.root()).each(function () {
  6935. var $el = $(this);
  6936. if (val === undefined) {
  6937. if ($el.find('em.edui-dropmenu-checked').length) {
  6938. currentVal = $el.data('value');
  6939. return false
  6940. }
  6941. } else {
  6942. $el.find('em').toggleClass('edui-dropmenu-checked', $el.data('value') == val)
  6943. }
  6944. });
  6945. if (val === undefined) {
  6946. return currentVal
  6947. }
  6948. },
  6949. addSubmenu: function (label, menu, index) {
  6950. index = index || 0;
  6951. var $list = $('li[class!=divider]', this.root());
  6952. var $node = $('<li class="dropdown-submenu"><a tabindex="-1" href="#">' + label + '</a></li>').append(menu);
  6953. if (index >= 0 && index < $list.length) {
  6954. $node.insertBefore($list[index]);
  6955. } else if (index < 0) {
  6956. $node.insertBefore($list[0]);
  6957. } else if (index >= $list.length) {
  6958. $node.appendTo($list);
  6959. }
  6960. }
  6961. }, 'menu');
  6962. //splitbutton 类
  6963. ///import button
  6964. UM.ui.define('splitbutton',{
  6965. tpl :'<div class="edui-splitbutton <%if (name){%>edui-splitbutton-<%= name %><%}%>" unselectable="on" <%if(title){%>data-original-title="<%=title%>"<%}%>><div class="edui-btn" unselectable="on" ><%if(icon){%><div unselectable="on" class="edui-icon-<%=icon%> edui-icon"></div><%}%><%if(text){%><%=text%><%}%></div>'+
  6966. '<div unselectable="on" class="edui-btn edui-dropdown-toggle" >'+
  6967. '<div unselectable="on" class="edui-caret"><\/div>'+
  6968. '</div>'+
  6969. '</div>',
  6970. defaultOpt:{
  6971. text:'',
  6972. title:'',
  6973. click:function(){}
  6974. },
  6975. init : function(options){
  6976. var me = this;
  6977. me.root( $($.parseTmpl(me.tpl,options)));
  6978. me.root().find('.edui-btn:first').click(function(evt){
  6979. if(!me.disabled()){
  6980. $.proxy(options.click,me)();
  6981. }
  6982. });
  6983. me.root().find('.edui-dropdown-toggle').click(function(){
  6984. if(!me.disabled()){
  6985. me.trigger('arrowclick')
  6986. }
  6987. });
  6988. me.root().hover(function () {
  6989. if(!me.root().hasClass("disabled")){
  6990. me.root().toggleClass('hover')
  6991. }
  6992. });
  6993. return me;
  6994. },
  6995. wrapclick:function(fn,evt){
  6996. if(!this.disabled()){
  6997. $.proxy(fn,this,evt)()
  6998. }
  6999. return this;
  7000. },
  7001. disabled : function(state){
  7002. if(state === undefined){
  7003. return this.root().hasClass('disabled')
  7004. }
  7005. this.root().toggleClass('disabled',state).find('.edui-btn').toggleClass('disabled',state);
  7006. return this;
  7007. },
  7008. active:function(state){
  7009. if(state === undefined){
  7010. return this.root().hasClass('active')
  7011. }
  7012. this.root().toggleClass('active',state).find('.edui-btn:first').toggleClass('active',state);
  7013. return this;
  7014. },
  7015. mergeWith:function($obj){
  7016. var me = this;
  7017. me.data('$mergeObj',$obj);
  7018. $obj.edui().data('$mergeObj',me.root());
  7019. if(!$.contains(document.body,$obj[0])){
  7020. $obj.appendTo(me.root());
  7021. }
  7022. me.root().delegate('.edui-dropdown-toggle','click',function(){
  7023. me.wrapclick(function(){
  7024. $obj.edui().show();
  7025. })
  7026. });
  7027. me.register('click',me.root().find('.edui-dropdown-toggle'),function(evt){
  7028. $obj.hide()
  7029. });
  7030. }
  7031. });
  7032. /**
  7033. * Created with JetBrains PhpStorm.
  7034. * User: hn
  7035. * Date: 13-7-10
  7036. * Time: 下午3:07
  7037. * To change this template use File | Settings | File Templates.
  7038. */
  7039. UM.ui.define('colorsplitbutton',{
  7040. tpl : '<div class="edui-splitbutton <%if (name){%>edui-splitbutton-<%= name %><%}%>" unselectable="on" <%if(title){%>data-original-title="<%=title%>"<%}%>><div class="edui-btn" unselectable="on" ><%if(icon){%><div unselectable="on" class="edui-icon-<%=icon%> edui-icon"></div><%}%><div class="edui-splitbutton-color-label" <%if (color) {%>style="background: <%=color%>"<%}%>></div><%if(text){%><%=text%><%}%></div>'+
  7041. '<div unselectable="on" class="edui-btn edui-dropdown-toggle" >'+
  7042. '<div unselectable="on" class="edui-caret"><\/div>'+
  7043. '</div>'+
  7044. '</div>',
  7045. defaultOpt: {
  7046. color: ''
  7047. },
  7048. init: function( options ){
  7049. var me = this;
  7050. me.supper.init.call( me, options );
  7051. },
  7052. colorLabel: function(){
  7053. return this.root().find('.edui-splitbutton-color-label');
  7054. }
  7055. }, 'splitbutton');
  7056. //popup 类
  7057. UM.ui.define('popup', {
  7058. tpl: '<div class="edui-dropdown-menu edui-popup"'+
  7059. '<%if(!<%=stopprop%>){%>onmousedown="return false"<%}%>'+
  7060. '><div class="edui-popup-body" unselectable="on" onmousedown="return false"><%=subtpl%></div>' +
  7061. '<div class="edui-popup-caret"></div>' +
  7062. '</div>',
  7063. defaultOpt: {
  7064. stopprop:false,
  7065. subtpl: '',
  7066. width: '',
  7067. height: ''
  7068. },
  7069. init: function (options) {
  7070. this.root($($.parseTmpl(this.tpl, options)));
  7071. return this;
  7072. },
  7073. mergeTpl: function (data) {
  7074. return $.parseTmpl(this.tpl, {subtpl: data});
  7075. },
  7076. show: function ($obj, posObj) {
  7077. if (!posObj) posObj = {};
  7078. var fnname = posObj.fnname || 'position';
  7079. if (this.trigger('beforeshow') === false) {
  7080. return;
  7081. } else {
  7082. this.root().css($.extend({display: 'block'}, $obj ? {
  7083. top: $obj[fnname]().top + ( posObj.dir == 'right' ? 0 : $obj.outerHeight()) - (posObj.offsetTop || 0),
  7084. left: $obj[fnname]().left + (posObj.dir == 'right' ? $obj.outerWidth() : 0) - (posObj.offsetLeft || 0),
  7085. position: 'absolute'
  7086. } : {}));
  7087. this.root().find('.edui-popup-caret').css({
  7088. top: posObj.caretTop || 0,
  7089. left: posObj.caretLeft || 0,
  7090. position: 'absolute'
  7091. }).addClass(posObj.caretDir || "up")
  7092. }
  7093. this.trigger("aftershow");
  7094. },
  7095. hide: function () {
  7096. this.root().css('display', 'none');
  7097. this.trigger('afterhide')
  7098. },
  7099. attachTo: function ($obj, posObj) {
  7100. var me = this
  7101. if (!$obj.data('$mergeObj')) {
  7102. $obj.data('$mergeObj', me.root());
  7103. $obj.on('wrapclick', function (evt) {
  7104. me.show($obj, posObj)
  7105. });
  7106. me.register('click', $obj, function (evt) {
  7107. me.hide()
  7108. });
  7109. me.data('$mergeObj', $obj)
  7110. }
  7111. },
  7112. getBodyContainer: function () {
  7113. return this.root().find(".edui-popup-body");
  7114. }
  7115. });
  7116. //scale 类
  7117. UM.ui.define('scale', {
  7118. tpl: '<div class="edui-scale" unselectable="on">' +
  7119. '<span class="edui-scale-hand0"></span>' +
  7120. '<span class="edui-scale-hand1"></span>' +
  7121. '<span class="edui-scale-hand2"></span>' +
  7122. '<span class="edui-scale-hand3"></span>' +
  7123. '<span class="edui-scale-hand4"></span>' +
  7124. '<span class="edui-scale-hand5"></span>' +
  7125. '<span class="edui-scale-hand6"></span>' +
  7126. '<span class="edui-scale-hand7"></span>' +
  7127. '</div>',
  7128. defaultOpt: {
  7129. $doc: $(document),
  7130. $wrap: $(document)
  7131. },
  7132. init: function (options) {
  7133. if(options.$doc) this.defaultOpt.$doc = options.$doc;
  7134. if(options.$wrap) this.defaultOpt.$wrap = options.$wrap;
  7135. this.root($($.parseTmpl(this.tpl, options)));
  7136. this.initStyle();
  7137. this.startPos = this.prePos = {x: 0, y: 0};
  7138. this.dragId = -1;
  7139. return this;
  7140. },
  7141. initStyle: function () {
  7142. utils.cssRule('scale', '.edui-scale{display:none;position:absolute;border:1px solid #38B2CE;cursor:hand;}' +
  7143. '.edui-scale span{position:absolute;left:0;top:0;width:7px;height:7px;overflow:hidden;font-size:0px;display:block;background-color:#3C9DD0;}'
  7144. + '.edui-scale .edui-scale-hand0{cursor:nw-resize;top:0;margin-top:-4px;left:0;margin-left:-4px;}'
  7145. + '.edui-scale .edui-scale-hand1{cursor:n-resize;top:0;margin-top:-4px;left:50%;margin-left:-4px;}'
  7146. + '.edui-scale .edui-scale-hand2{cursor:ne-resize;top:0;margin-top:-4px;left:100%;margin-left:-3px;}'
  7147. + '.edui-scale .edui-scale-hand3{cursor:w-resize;top:50%;margin-top:-4px;left:0;margin-left:-4px;}'
  7148. + '.edui-scale .edui-scale-hand4{cursor:e-resize;top:50%;margin-top:-4px;left:100%;margin-left:-3px;}'
  7149. + '.edui-scale .edui-scale-hand5{cursor:sw-resize;top:100%;margin-top:-3px;left:0;margin-left:-4px;}'
  7150. + '.edui-scale .edui-scale-hand6{cursor:s-resize;top:100%;margin-top:-3px;left:50%;margin-left:-4px;}'
  7151. + '.edui-scale .edui-scale-hand7{cursor:se-resize;top:100%;margin-top:-3px;left:100%;margin-left:-3px;}');
  7152. },
  7153. _eventHandler: function (e) {
  7154. var me = this,
  7155. $doc = me.defaultOpt.$doc;
  7156. switch (e.type) {
  7157. case 'mousedown':
  7158. var hand = e.target || e.srcElement, hand;
  7159. if (hand.className.indexOf('edui-scale-hand') != -1) {
  7160. me.dragId = hand.className.slice(-1);
  7161. me.startPos.x = me.prePos.x = e.clientX;
  7162. me.startPos.y = me.prePos.y = e.clientY;
  7163. $doc.bind('mousemove', $.proxy(me._eventHandler, me));
  7164. }
  7165. break;
  7166. case 'mousemove':
  7167. if (me.dragId != -1) {
  7168. me.updateContainerStyle(me.dragId, {x: e.clientX - me.prePos.x, y: e.clientY - me.prePos.y});
  7169. me.prePos.x = e.clientX;
  7170. me.prePos.y = e.clientY;
  7171. me.updateTargetElement();
  7172. }
  7173. break;
  7174. case 'mouseup':
  7175. if (me.dragId != -1) {
  7176. me.dragId = -1;
  7177. me.updateTargetElement();
  7178. var $target = me.data('$scaleTarget');
  7179. if ($target.parent()) me.attachTo(me.data('$scaleTarget'));
  7180. }
  7181. $doc.unbind('mousemove', $.proxy(me._eventHandler, me));
  7182. break;
  7183. default:
  7184. break;
  7185. }
  7186. },
  7187. updateTargetElement: function () {
  7188. var me = this,
  7189. $root = me.root(),
  7190. $target = me.data('$scaleTarget');
  7191. $target.css({width: $root.width(), height: $root.height()});
  7192. me.attachTo($target);
  7193. },
  7194. updateContainerStyle: function (dir, offset) {
  7195. var me = this,
  7196. $dom = me.root(),
  7197. tmp,
  7198. rect = [
  7199. //[left, top, width, height]
  7200. [0, 0, -1, -1],
  7201. [0, 0, 0, -1],
  7202. [0, 0, 1, -1],
  7203. [0, 0, -1, 0],
  7204. [0, 0, 1, 0],
  7205. [0, 0, -1, 1],
  7206. [0, 0, 0, 1],
  7207. [0, 0, 1, 1]
  7208. ];
  7209. if (rect[dir][0] != 0) {
  7210. tmp = parseInt($dom.offset().left) + offset.x;
  7211. $dom.css('left', me._validScaledProp('left', tmp));
  7212. }
  7213. if (rect[dir][1] != 0) {
  7214. tmp = parseInt($dom.offset().top) + offset.y;
  7215. $dom.css('top', me._validScaledProp('top', tmp));
  7216. }
  7217. if (rect[dir][2] != 0) {
  7218. tmp = $dom.width() + rect[dir][2] * offset.x;
  7219. $dom.css('width', me._validScaledProp('width', tmp));
  7220. }
  7221. if (rect[dir][3] != 0) {
  7222. tmp = $dom.height() + rect[dir][3] * offset.y;
  7223. $dom.css('height', me._validScaledProp('height', tmp));
  7224. }
  7225. },
  7226. _validScaledProp: function (prop, value) {
  7227. var $ele = this.root(),
  7228. $wrap = this.defaultOpt.$doc,
  7229. calc = function(val, a, b){
  7230. return (val + a) > b ? b - a : value;
  7231. };
  7232. value = isNaN(value) ? 0 : value;
  7233. switch (prop) {
  7234. case 'left':
  7235. return value < 0 ? 0 : calc(value, $ele.width(), $wrap.width());
  7236. case 'top':
  7237. return value < 0 ? 0 : calc(value, $ele.height(),$wrap.height());
  7238. case 'width':
  7239. return value <= 0 ? 1 : calc(value, $ele.offset().left, $wrap.width());
  7240. case 'height':
  7241. return value <= 0 ? 1 : calc(value, $ele.offset().top, $wrap.height());
  7242. }
  7243. },
  7244. show: function ($obj) {
  7245. var me = this;
  7246. if ($obj) me.attachTo($obj);
  7247. me.root().bind('mousedown', $.proxy(me._eventHandler, me));
  7248. me.defaultOpt.$doc.bind('mouseup', $.proxy(me._eventHandler, me));
  7249. me.root().show();
  7250. me.trigger("aftershow");
  7251. },
  7252. hide: function () {
  7253. var me = this;
  7254. me.root().unbind('mousedown', $.proxy(me._eventHandler, me));
  7255. me.defaultOpt.$doc.unbind('mouseup', $.proxy(me._eventHandler, me));
  7256. me.root().hide();
  7257. me.trigger('afterhide')
  7258. },
  7259. attachTo: function ($obj) {
  7260. var me = this,
  7261. imgPos = $obj.offset(),
  7262. $root = me.root(),
  7263. $wrap = me.defaultOpt.$wrap,
  7264. posObj = $wrap.offset();
  7265. me.data('$scaleTarget', $obj);
  7266. me.root().css({
  7267. position: 'absolute',
  7268. width: $obj.width(),
  7269. height: $obj.height(),
  7270. left: imgPos.left - posObj.left - parseInt($wrap.css('border-left-width')) - parseInt($root.css('border-left-width')),
  7271. top: imgPos.top - posObj.top - parseInt($wrap.css('border-top-width')) - parseInt($root.css('border-top-width'))
  7272. });
  7273. },
  7274. getScaleTarget: function () {
  7275. return this.data('$scaleTarget')[0];
  7276. }
  7277. });
  7278. //colorpicker 类
  7279. UM.ui.define('colorpicker', {
  7280. tpl: function (opt) {
  7281. var COLORS = (
  7282. 'ffffff,000000,eeece1,1f497d,4f81bd,c0504d,9bbb59,8064a2,4bacc6,f79646,' +
  7283. 'f2f2f2,7f7f7f,ddd9c3,c6d9f0,dbe5f1,f2dcdb,ebf1dd,e5e0ec,dbeef3,fdeada,' +
  7284. 'd8d8d8,595959,c4bd97,8db3e2,b8cce4,e5b9b7,d7e3bc,ccc1d9,b7dde8,fbd5b5,' +
  7285. 'bfbfbf,3f3f3f,938953,548dd4,95b3d7,d99694,c3d69b,b2a2c7,92cddc,fac08f,' +
  7286. 'a5a5a5,262626,494429,17365d,366092,953734,76923c,5f497a,31859b,e36c09,' +
  7287. '7f7f7f,0c0c0c,1d1b10,0f243e,244061,632423,4f6128,3f3151,205867,974806,' +
  7288. 'c00000,ff0000,ffc000,ffff00,92d050,00b050,00b0f0,0070c0,002060,7030a0,').split(',');
  7289. var html = '<div unselectable="on" onmousedown="return false" class="edui-colorpicker<%if (name){%> edui-colorpicker-<%=name%><%}%>" >' +
  7290. '<table unselectable="on" onmousedown="return false">' +
  7291. '<tr><td colspan="10">'+opt.lang_themeColor+'</td> </tr>' +
  7292. '<tr class="edui-colorpicker-firstrow" >';
  7293. for (var i = 0; i < COLORS.length; i++) {
  7294. if (i && i % 10 === 0) {
  7295. html += '</tr>' + (i == 60 ? '<tr><td colspan="10">'+opt.lang_standardColor+'</td></tr>' : '') + '<tr' + (i == 60 ? ' class="edui-colorpicker-firstrow"' : '') + '>';
  7296. }
  7297. html += i < 70 ? '<td><a unselectable="on" onmousedown="return false" title="' + COLORS[i] + '" class="edui-colorpicker-colorcell"' +
  7298. ' data-color="#' + COLORS[i] + '"' +
  7299. ' style="background-color:#' + COLORS[i] + ';border:solid #ccc;' +
  7300. (i < 10 || i >= 60 ? 'border-width:1px;' :
  7301. i >= 10 && i < 20 ? 'border-width:1px 1px 0 1px;' :
  7302. 'border-width:0 1px 0 1px;') +
  7303. '"' +
  7304. '></a></td>' : '';
  7305. }
  7306. html += '</tr></table></div>';
  7307. return html;
  7308. },
  7309. init: function (options) {
  7310. var me = this;
  7311. me.root($($.parseTmpl(me.supper.mergeTpl(me.tpl(options)),options)));
  7312. me.root().on("click",function (e) {
  7313. me.trigger('pickcolor', $(e.target).data('color'));
  7314. });
  7315. }
  7316. }, 'popup');
  7317. /**
  7318. * Created with JetBrains PhpStorm.
  7319. * User: hn
  7320. * Date: 13-5-29
  7321. * Time: 下午8:01
  7322. * To change this template use File | Settings | File Templates.
  7323. */
  7324. (function(){
  7325. var widgetName = 'combobox',
  7326. itemClassName = 'edui-combobox-item',
  7327. HOVER_CLASS = 'edui-combobox-item-hover',
  7328. ICON_CLASS = 'edui-combobox-checked-icon',
  7329. labelClassName = 'edui-combobox-item-label';
  7330. UM.ui.define( widgetName, ( function(){
  7331. return {
  7332. tpl: "<ul class=\"dropdown-menu edui-combobox-menu<%if (comboboxName!=='') {%> edui-combobox-<%=comboboxName%><%}%>\" unselectable=\"on\" onmousedown=\"return false\" role=\"menu\" aria-labelledby=\"dropdownMenu\">" +
  7333. "<%if(autoRecord) {%>" +
  7334. "<%for( var i=0, len = recordStack.length; i<len; i++ ) {%>" +
  7335. "<%var index = recordStack[i];%>" +
  7336. "<li class=\"<%=itemClassName%><%if( selected == index ) {%> edui-combobox-checked<%}%>\" data-item-index=\"<%=index%>\" unselectable=\"on\" onmousedown=\"return false\">" +
  7337. "<span class=\"edui-combobox-icon\" unselectable=\"on\" onmousedown=\"return false\"></span>" +
  7338. "<label class=\"<%=labelClassName%>\" style=\"<%=itemStyles[ index ]%>\" unselectable=\"on\" onmousedown=\"return false\"><%=items[index]%></label>" +
  7339. "</li>" +
  7340. "<%}%>" +
  7341. "<%if( i ) {%>" +
  7342. "<li class=\"edui-combobox-item-separator\"></li>" +
  7343. "<%}%>" +
  7344. "<%}%>" +
  7345. "<%for( var i=0, label; label = items[i]; i++ ) {%>" +
  7346. "<li class=\"<%=itemClassName%><%if( selected == i ) {%> edui-combobox-checked<%}%> edui-combobox-item-<%=i%>\" data-item-index=\"<%=i%>\" unselectable=\"on\" onmousedown=\"return false\">" +
  7347. "<span class=\"edui-combobox-icon\" unselectable=\"on\" onmousedown=\"return false\"></span>" +
  7348. "<label class=\"<%=labelClassName%>\" style=\"<%=itemStyles[ i ]%>\" unselectable=\"on\" onmousedown=\"return false\"><%=label%></label>" +
  7349. "</li>" +
  7350. "<%}%>" +
  7351. "</ul>",
  7352. defaultOpt: {
  7353. //记录栈初始列表
  7354. recordStack: [],
  7355. //可用项列表
  7356. items: [],
  7357. //item对应的值列表
  7358. value: [],
  7359. comboboxName: '',
  7360. selected: '',
  7361. //自动记录
  7362. autoRecord: true,
  7363. //最多记录条数
  7364. recordCount: 5
  7365. },
  7366. init: function( options ){
  7367. var me = this;
  7368. $.extend( me._optionAdaptation( options ), me._createItemMapping( options.recordStack, options.items ), {
  7369. itemClassName: itemClassName,
  7370. iconClass: ICON_CLASS,
  7371. labelClassName: labelClassName
  7372. } );
  7373. this._transStack( options );
  7374. me.root( $( $.parseTmpl( me.tpl, options ) ) );
  7375. this.data( 'options', options ).initEvent();
  7376. },
  7377. initEvent: function(){
  7378. var me = this;
  7379. me.initSelectItem();
  7380. this.initItemActive();
  7381. },
  7382. /**
  7383. * 初始化选择项
  7384. */
  7385. initSelectItem: function(){
  7386. var me = this,
  7387. labelClass = "."+labelClassName;
  7388. me.root().delegate('.' + itemClassName, 'click', function(){
  7389. var $li = $(this),
  7390. index = $li.attr('data-item-index');
  7391. me.trigger('comboboxselect', {
  7392. index: index,
  7393. label: $li.find(labelClass).text(),
  7394. value: me.data('options').value[ index ]
  7395. }).select( index );
  7396. me.hide();
  7397. return false;
  7398. });
  7399. },
  7400. initItemActive: function(){
  7401. var fn = {
  7402. mouseenter: 'addClass',
  7403. mouseleave: 'removeClass'
  7404. };
  7405. if ($.IE6) {
  7406. this.root().delegate( '.'+itemClassName, 'mouseenter mouseleave', function( evt ){
  7407. $(this)[ fn[ evt.type ] ]( HOVER_CLASS );
  7408. }).one('afterhide', function(){
  7409. });
  7410. }
  7411. },
  7412. /**
  7413. * 选择给定索引的项
  7414. * @param index 项索引
  7415. * @returns {*} 如果存在对应索引的项,则返回该项;否则返回null
  7416. */
  7417. select: function( index ){
  7418. var itemCount = this.data('options').itemCount,
  7419. items = this.data('options').autowidthitem;
  7420. if ( items && !items.length ) {
  7421. items = this.data('options').items;
  7422. }
  7423. if( itemCount == 0 ) {
  7424. return null;
  7425. }
  7426. if( index < 0 ) {
  7427. index = itemCount + index % itemCount;
  7428. } else if ( index >= itemCount ) {
  7429. index = itemCount-1;
  7430. }
  7431. this.trigger( 'changebefore', items[ index ] );
  7432. this._update( index );
  7433. this.trigger( 'changeafter', items[ index ] );
  7434. return null;
  7435. },
  7436. selectItemByLabel: function( label ){
  7437. var itemMapping = this.data('options').itemMapping,
  7438. me = this,
  7439. index = null;
  7440. !$.isArray( label ) && ( label = [ label ] );
  7441. $.each( label, function( i, item ){
  7442. index = itemMapping[ item ];
  7443. if( index !== undefined ) {
  7444. me.select( index );
  7445. return false;
  7446. }
  7447. } );
  7448. },
  7449. /**
  7450. * 转换记录栈
  7451. */
  7452. _transStack: function( options ) {
  7453. var temp = [],
  7454. itemIndex = -1,
  7455. selected = -1;
  7456. $.each( options.recordStack, function( index, item ){
  7457. itemIndex = options.itemMapping[ item ];
  7458. if( $.isNumeric( itemIndex ) ) {
  7459. temp.push( itemIndex );
  7460. //selected的合法性检测
  7461. if( item == options.selected ) {
  7462. selected = itemIndex;
  7463. }
  7464. }
  7465. } );
  7466. options.recordStack = temp;
  7467. options.selected = selected;
  7468. temp = null;
  7469. },
  7470. _optionAdaptation: function( options ) {
  7471. if( !( 'itemStyles' in options ) ) {
  7472. options.itemStyles = [];
  7473. for( var i = 0, len = options.items.length; i < len; i++ ) {
  7474. options.itemStyles.push('');
  7475. }
  7476. }
  7477. options.autowidthitem = options.autowidthitem || options.items;
  7478. options.itemCount = options.items.length;
  7479. return options;
  7480. },
  7481. _createItemMapping: function( stackItem, items ){
  7482. var temp = {},
  7483. result = {
  7484. recordStack: [],
  7485. mapping: {}
  7486. };
  7487. $.each( items, function( index, item ){
  7488. temp[ item ] = index;
  7489. } );
  7490. result.itemMapping = temp;
  7491. $.each( stackItem, function( index, item ){
  7492. if( temp[ item ] !== undefined ) {
  7493. result.recordStack.push( temp[ item ] );
  7494. result.mapping[ item ] = temp[ item ];
  7495. }
  7496. } );
  7497. return result;
  7498. },
  7499. _update: function ( index ) {
  7500. var options = this.data("options"),
  7501. newStack = [],
  7502. newChilds = null;
  7503. $.each( options.recordStack, function( i, item ){
  7504. if( item != index ) {
  7505. newStack.push( item );
  7506. }
  7507. } );
  7508. //压入最新的记录
  7509. newStack.unshift( index );
  7510. if( newStack.length > options.recordCount ) {
  7511. newStack.length = options.recordCount;
  7512. }
  7513. options.recordStack = newStack;
  7514. options.selected = index;
  7515. newChilds = $( $.parseTmpl( this.tpl, options ) );
  7516. //重新渲染
  7517. this.root().html( newChilds.html() );
  7518. newChilds = null;
  7519. newStack = null;
  7520. }
  7521. };
  7522. } )(), 'menu' );
  7523. })();
  7524. /**
  7525. * Combox 抽象基类
  7526. * User: hn
  7527. * Date: 13-5-29
  7528. * Time: 下午8:01
  7529. * To change this template use File | Settings | File Templates.
  7530. */
  7531. (function(){
  7532. var widgetName = 'buttoncombobox';
  7533. UM.ui.define( widgetName, ( function(){
  7534. return {
  7535. defaultOpt: {
  7536. //按钮初始文字
  7537. label: '',
  7538. title: ''
  7539. },
  7540. init: function( options ) {
  7541. var me = this;
  7542. var btnWidget = $.eduibutton({
  7543. caret: true,
  7544. name: options.comboboxName,
  7545. title: options.title,
  7546. text: options.label,
  7547. click: function(){
  7548. me.show( this.root() );
  7549. }
  7550. });
  7551. me.supper.init.call( me, options );
  7552. //监听change, 改变button显示内容
  7553. me.on('changebefore', function( e, label ){
  7554. btnWidget.eduibutton('label', label );
  7555. });
  7556. me.data( 'button', btnWidget );
  7557. me.attachTo(btnWidget)
  7558. },
  7559. button: function(){
  7560. return this.data( 'button' );
  7561. }
  7562. }
  7563. } )(), 'combobox' );
  7564. })();
  7565. /*modal 类*/
  7566. UM.ui.define('modal', {
  7567. tpl: '<div class="edui-modal" tabindex="-1" >' +
  7568. '<div class="edui-modal-header">' +
  7569. '<div class="edui-close" data-hide="modal"></div>' +
  7570. '<h3 class="edui-title"><%=title%></h3>' +
  7571. '</div>' +
  7572. '<div class="edui-modal-body" style="<%if(width){%>width:<%=width%>px;<%}%>' +
  7573. '<%if(height){%>height:<%=height%>px;<%}%>">' +
  7574. ' </div>' +
  7575. '<% if(cancellabel || oklabel) {%>' +
  7576. '<div class="edui-modal-footer">' +
  7577. '<div class="edui-modal-tip"></div>' +
  7578. '<%if(oklabel){%><div class="edui-btn edui-btn-primary" data-ok="modal"><%=oklabel%></div><%}%>' +
  7579. '<%if(cancellabel){%><div class="edui-btn" data-hide="modal"><%=cancellabel%></div><%}%>' +
  7580. '</div>' +
  7581. '<%}%></div>',
  7582. defaultOpt: {
  7583. title: "",
  7584. cancellabel: "",
  7585. oklabel: "",
  7586. width: '',
  7587. height: '',
  7588. backdrop: true,
  7589. keyboard: true
  7590. },
  7591. init: function (options) {
  7592. var me = this;
  7593. me.root($($.parseTmpl(me.tpl, options || {})));
  7594. me.data("options", options);
  7595. if (options.okFn) {
  7596. me.on('ok', $.proxy(options.okFn, me))
  7597. }
  7598. if (options.cancelFn) {
  7599. me.on('beforehide', $.proxy(options.cancelFn, me))
  7600. }
  7601. me.root().delegate('[data-hide="modal"]', 'click', $.proxy(me.hide, me))
  7602. .delegate('[data-ok="modal"]', 'click', $.proxy(me.ok, me));
  7603. $('[data-hide="modal"],[data-ok="modal"]',me.root()).hover(function(){
  7604. $(this).toggleClass('hover')
  7605. });
  7606. },
  7607. toggle: function () {
  7608. var me = this;
  7609. return me[!me.data("isShown") ? 'show' : 'hide']();
  7610. },
  7611. show: function () {
  7612. var me = this;
  7613. me.trigger("beforeshow");
  7614. if (me.data("isShown")) return;
  7615. me.data("isShown", true);
  7616. me.escape();
  7617. me.backdrop(function () {
  7618. me.autoCenter();
  7619. me.root()
  7620. .show()
  7621. .focus()
  7622. .trigger('aftershow');
  7623. })
  7624. },
  7625. showTip: function ( text ) {
  7626. $( '.edui-modal-tip', this.root() ).html( text ).fadeIn();
  7627. },
  7628. hideTip: function ( text ) {
  7629. $( '.edui-modal-tip', this.root() ).fadeOut( function (){
  7630. $(this).html('');
  7631. } );
  7632. },
  7633. autoCenter: function () {
  7634. //ie6下不用处理了
  7635. !$.IE6 && this.root().css("margin-left", -(this.root().width() / 2));
  7636. },
  7637. hide: function () {
  7638. var me = this;
  7639. me.trigger("beforehide");
  7640. if (!me.data("isShown")) return;
  7641. me.data("isShown", false);
  7642. me.escape();
  7643. me.hideModal();
  7644. },
  7645. escape: function () {
  7646. var me = this;
  7647. if (me.data("isShown") && me.data("options").keyboard) {
  7648. me.root().on('keyup', function (e) {
  7649. e.which == 27 && me.hide();
  7650. })
  7651. }
  7652. else if (!me.data("isShown")) {
  7653. me.root().off('keyup');
  7654. }
  7655. },
  7656. hideModal: function () {
  7657. var me = this;
  7658. me.root().hide();
  7659. me.backdrop(function () {
  7660. me.removeBackdrop();
  7661. me.trigger('afterhide');
  7662. })
  7663. },
  7664. removeBackdrop: function () {
  7665. this.$backdrop && this.$backdrop.remove();
  7666. this.$backdrop = null;
  7667. },
  7668. backdrop: function (callback) {
  7669. var me = this;
  7670. if (me.data("isShown") && me.data("options").backdrop) {
  7671. me.$backdrop = $('<div class="edui-modal-backdrop" />').click(
  7672. me.data("options").backdrop == 'static' ?
  7673. $.proxy(me.root()[0].focus, me.root()[0])
  7674. : $.proxy(me.hide, me)
  7675. )
  7676. }
  7677. me.trigger('afterbackdrop');
  7678. callback && callback();
  7679. },
  7680. attachTo: function ($obj) {
  7681. var me = this
  7682. if (!$obj.data('$mergeObj')) {
  7683. $obj.data('$mergeObj', me.root());
  7684. $obj.on('click', function () {
  7685. me.toggle($obj)
  7686. });
  7687. me.data('$mergeObj', $obj)
  7688. }
  7689. },
  7690. ok: function () {
  7691. var me = this;
  7692. me.trigger('beforeok');
  7693. if (me.trigger("ok", me) === false) {
  7694. return;
  7695. }
  7696. me.hide();
  7697. },
  7698. getBodyContainer: function () {
  7699. return this.root().find('.edui-modal-body')
  7700. }
  7701. });
  7702. /*tooltip 类*/
  7703. UM.ui.define('tooltip', {
  7704. tpl: '<div class="edui-tooltip" unselectable="on" onmousedown="return false"><div class="edui-tooltip-arrow" unselectable="on" onmousedown="return false"></div><div class="edui-tooltip-inner" unselectable="on" onmousedown="return false"></div></div>',
  7705. init: function (options) {
  7706. var me = this;
  7707. me.root($($.parseTmpl(me.tpl, options || {})));
  7708. },
  7709. content: function (e) {
  7710. var me = this,
  7711. title = $(e.currentTarget).attr("data-original-title");
  7712. me.root().find('.edui-tooltip-inner')['text'](title);
  7713. },
  7714. position: function (e) {
  7715. var me = this,
  7716. $obj = $(e.currentTarget);
  7717. me.root().css($.extend({display: 'block'}, $obj ? {
  7718. top: $obj.outerHeight(),
  7719. left: (($obj.outerWidth() - me.root().outerWidth()) / 2)
  7720. } : {}))
  7721. },
  7722. show: function (e) {
  7723. if ($(e.currentTarget).hasClass('disabled')) return;
  7724. var me = this;
  7725. me.content(e);
  7726. me.root().appendTo($(e.currentTarget));
  7727. me.position(e);
  7728. me.root().css('display', 'block').addClass("in bottom")
  7729. },
  7730. hide: function () {
  7731. var me = this;
  7732. me.root().removeClass("in bottom").css('display', 'none')
  7733. },
  7734. attachTo: function ($obj) {
  7735. var me = this;
  7736. function tmp($obj) {
  7737. var me = this;
  7738. if (!$.contains(document.body, me.root()[0])) {
  7739. me.root().appendTo($obj);
  7740. }
  7741. me.data('tooltip', me.root());
  7742. $obj.each(function () {
  7743. if ($(this).attr("data-original-title")) {
  7744. $(this).on('mouseenter', $.proxy(me.show, me))
  7745. .on('mouseleave click', $.proxy(me.hide, me))
  7746. }
  7747. });
  7748. }
  7749. if ($.type($obj) === "undefined") {
  7750. $("[data-original-title]").each(function (i, el) {
  7751. tmp.call(me, $(el));
  7752. })
  7753. } else {
  7754. if (!$obj.data('tooltip')) {
  7755. tmp.call(me, $obj);
  7756. }
  7757. }
  7758. }
  7759. });
  7760. /*tab 类*/
  7761. UM.ui.define('tab', {
  7762. init: function (options) {
  7763. var me = this,
  7764. slr = options.selector;
  7765. if ($.type(slr)) {
  7766. me.root($(slr, options.context));
  7767. me.data("context", options.context);
  7768. $(slr, me.data("context")).on('click', function (e) {
  7769. me.show(e);
  7770. });
  7771. }
  7772. },
  7773. show: function (e) {
  7774. var me = this,
  7775. $cur = $(e.target),
  7776. $ul = $cur.closest('ul'),
  7777. selector,
  7778. previous,
  7779. $target,
  7780. e;
  7781. selector = $cur.attr('href');
  7782. selector = selector && selector.replace(/.*(?=#[^\s]*$)/, '');
  7783. var $tmp = $cur.parent('li');
  7784. if (!$tmp.length || $tmp.hasClass('active')) return;
  7785. previous = $ul.find('.active:last a')[0];
  7786. e = $.Event('beforeshow', {
  7787. target: $cur[0],
  7788. relatedTarget: previous
  7789. });
  7790. me.trigger(e);
  7791. if (e.isDefaultPrevented()) return;
  7792. $target = $(selector, me.data("context"));
  7793. me.activate($cur.parent('li'), $ul);
  7794. me.activate($target, $target.parent(), function () {
  7795. me.trigger({
  7796. type: 'aftershow', relatedTarget: previous
  7797. })
  7798. });
  7799. },
  7800. activate: function (element, container, callback) {
  7801. if (element === undefined) {
  7802. return $(".edui-tab-item.active",this.root()).index();
  7803. }
  7804. var $active = container.find('> .active');
  7805. $active.removeClass('active');
  7806. element.addClass('active');
  7807. callback && callback();
  7808. }
  7809. });
  7810. //button 类
  7811. UM.ui.define('separator', {
  7812. tpl: '<div class="edui-separator" unselectable="on" onmousedown="return false" ></div>',
  7813. init: function (options) {
  7814. var me = this;
  7815. me.root($($.parseTmpl(me.tpl, options)));
  7816. return me;
  7817. }
  7818. });
  7819. /**
  7820. * @file adapter.js
  7821. * @desc adapt ui to editor
  7822. * @import core/Editor.js, core/utils.js
  7823. */
  7824. (function () {
  7825. var _editorUI = {},
  7826. _editors = {},
  7827. _readyFn = [],
  7828. _activeWidget = null,
  7829. _widgetData = {},
  7830. _widgetCallBack = {},
  7831. _cacheUI = {};
  7832. utils.extend(UM, {
  7833. defaultWidth : 500,
  7834. defaultHeight : 500,
  7835. registerUI: function (name, fn) {
  7836. utils.each(name.split(/\s+/), function (uiname) {
  7837. _editorUI[uiname] = fn;
  7838. })
  7839. },
  7840. setEditor : function(editor){
  7841. !_editors[editor.id] && (_editors[editor.id] = editor);
  7842. },
  7843. registerWidget : function(name,pro,cb){
  7844. _widgetData[name] = $.extend2(pro,{
  7845. $root : '',
  7846. _preventDefault:false,
  7847. root:function($el){
  7848. return this.$root || (this.$root = $el);
  7849. },
  7850. preventDefault:function(){
  7851. this._preventDefault = true;
  7852. },
  7853. clear:false
  7854. });
  7855. if(cb){
  7856. _widgetCallBack[name] = cb;
  7857. }
  7858. },
  7859. getWidgetData : function(name){
  7860. return _widgetData[name]
  7861. },
  7862. setWidgetBody : function(name,$widget,editor){
  7863. if(!editor._widgetData){
  7864. editor._widgetData = {};
  7865. editor.getWidgetData = function(name){
  7866. return this._widgetData[name];
  7867. };
  7868. editor.getWidgetCallback = function(widgetName){
  7869. return _widgetCallBack[widgetName];
  7870. }
  7871. }
  7872. var pro = _widgetData[name];
  7873. if(!pro){
  7874. return null;
  7875. }
  7876. pro = editor._widgetData[name];
  7877. if(!pro){
  7878. pro = _widgetData[name];
  7879. pro = editor._widgetData[name] = $.type(pro) == 'function' ? pro : utils.clone(pro);
  7880. }
  7881. pro.root($widget.edui().getBodyContainer());
  7882. pro.initContent(editor,$widget);
  7883. if(!pro._preventDefault){
  7884. pro.initEvent(editor,$widget);
  7885. }
  7886. pro.width && $widget.width(pro.width);
  7887. //为回调进行参数绑定
  7888. var cb = _widgetCallBack[name];
  7889. if(cb && !cb.init){
  7890. _widgetCallBack[name] = function(){
  7891. var args = Array.prototype.slice.call(arguments,0);
  7892. cb.apply(editor,[editor,$widget].concat(args));
  7893. }
  7894. _widgetCallBack[name].init = true;
  7895. }
  7896. },
  7897. setActiveWidget : function($widget){
  7898. _activeWidget = $widget;
  7899. },
  7900. getEditor: function (id, options) {
  7901. return _editors[id] || (_editors[id] = this.createEditor(id, options))
  7902. },
  7903. clearCache : function(id){
  7904. if ( _editors[id]) {
  7905. delete _editors[id]
  7906. }
  7907. },
  7908. delEditor: function (id) {
  7909. var editor;
  7910. if (editor = _editors[id]) {
  7911. editor.destroy();
  7912. }
  7913. },
  7914. ready: function( fn ){
  7915. _readyFn.push( fn );
  7916. },
  7917. createEditor: function (id, opt) {
  7918. var editor = new UM.Editor(opt);
  7919. var T = this;
  7920. editor.langIsReady ? $.proxy(renderUI,T)() : editor.addListener("langReady", $.proxy(renderUI,T));
  7921. function renderUI(){
  7922. var $container = this.createUI('#' + id, editor);
  7923. editor.key=id;
  7924. editor.ready(function(){
  7925. $.each( _readyFn, function( index, fn ){
  7926. $.proxy( fn, editor )();
  7927. } );
  7928. });
  7929. var options = editor.options;
  7930. if(options.initialFrameWidth){
  7931. options.minFrameWidth = options.initialFrameWidth
  7932. }else{
  7933. options.minFrameWidth = options.initialFrameWidth = editor.$body.width() || UM.defaultWidth;
  7934. }
  7935. $container.css({
  7936. width: options.initialFrameWidth,
  7937. zIndex:editor.getOpt('zIndex')
  7938. });
  7939. //ie6下缓存图片
  7940. UM.browser.ie && UM.browser.version === 6 && document.execCommand("BackgroundImageCache", false, true);
  7941. editor.render(id);
  7942. //添加tooltip;
  7943. $.eduitooltip && $.eduitooltip('attachTo').css('z-index',editor.getOpt('zIndex')+1);
  7944. $container.find('a').click(function(evt){
  7945. evt.preventDefault()
  7946. });
  7947. editor.fireEvent("afteruiready");
  7948. }
  7949. return editor;
  7950. },
  7951. createUI: function (id, editor) {
  7952. var $editorCont = $(id),
  7953. $container = $('<div class="edui-container"><div class="edui-editor-body"></div></div>').insertBefore($editorCont);
  7954. editor.$container = $container;
  7955. editor.container = $container[0];
  7956. editor.$body = $editorCont;
  7957. //修正在ie9+以上的版本中,自动长高收起时的,残影问题
  7958. if(browser.ie && browser.ie9above){
  7959. var $span = $('<span style="padding:0;margin:0;height:0;width:0"></span>');
  7960. $span.insertAfter($container);
  7961. }
  7962. //初始化注册的ui组件
  7963. $.each(_editorUI,function(n,v){
  7964. var widget = v.call(editor,n);
  7965. if(widget){
  7966. _cacheUI[n] = widget;
  7967. }
  7968. });
  7969. $container.find('.edui-editor-body').append($editorCont).before(this.createToolbar(editor.options, editor));
  7970. $container.find('.edui-toolbar').append($('<div class="edui-dialog-container"></div>'));
  7971. return $container;
  7972. },
  7973. createToolbar: function (options, editor) {
  7974. var $toolbar = $.eduitoolbar(), toolbar = $toolbar.edui();
  7975. //创建下来菜单列表
  7976. if (options.toolbar && options.toolbar.length) {
  7977. var btns = [];
  7978. $.each(options.toolbar,function(i,uiNames){
  7979. $.each(uiNames.split(/\s+/),function(index,name){
  7980. if(name == '|'){
  7981. $.eduiseparator && btns.push($.eduiseparator());
  7982. }else{
  7983. var ui = _cacheUI[name];
  7984. if(name=="fullscreen"){
  7985. ui&&btns.unshift(ui);
  7986. }else{
  7987. ui && btns.push(ui);
  7988. }
  7989. }
  7990. });
  7991. btns.length && toolbar.appendToBtnmenu(btns);
  7992. });
  7993. } else {
  7994. $toolbar.find('.edui-btn-toolbar').remove()
  7995. }
  7996. return $toolbar;
  7997. }
  7998. })
  7999. })();
  8000. UM.registerUI('bold italic redo undo underline strikethrough superscript subscript insertorderedlist insertunorderedlist ' +
  8001. 'cleardoc selectall link unlink print preview justifyleft justifycenter justifyright justifyfull removeformat horizontal',
  8002. function(name) {
  8003. var me = this;
  8004. var $btn = $.eduibutton({
  8005. icon : name,
  8006. click : function(){
  8007. me.execCommand(name)
  8008. },
  8009. title: this.getLang('labelMap')[name] || ''
  8010. });
  8011. this.addListener('selectionchange',function(){
  8012. var state = this.queryCommandState(name);
  8013. $btn.edui().disabled(state == -1).active(state == 1)
  8014. });
  8015. return $btn;
  8016. }
  8017. );
  8018. /**
  8019. * 全屏组件
  8020. */
  8021. (function(){
  8022. //状态缓存
  8023. var STATUS_CACHE = {},
  8024. //状态值列表
  8025. STATUS_LIST = [ 'width', 'height', 'position', 'top', 'left', 'margin', 'padding', 'overflowX', 'overflowY' ],
  8026. CONTENT_AREA_STATUS = {},
  8027. //页面状态
  8028. DOCUMENT_STATUS = {},
  8029. DOCUMENT_ELEMENT_STATUS = {},
  8030. FULLSCREENS = {};
  8031. UM.registerUI('fullscreen', function( name ){
  8032. var me = this,
  8033. $button = $.eduibutton({
  8034. 'icon': 'fullscreen',
  8035. 'title': (me.options.labelMap && me.options.labelMap[name]) || me.getLang("labelMap." + name),
  8036. 'click': function(){
  8037. //切换
  8038. me.execCommand( name );
  8039. }
  8040. });
  8041. me.addListener( "selectionchange", function () {
  8042. var state = this.queryCommandState( name );
  8043. $button.edui().disabled( state == -1 ).active( state == 1 );
  8044. } );
  8045. //切换至全屏
  8046. me.addListener('ready', function(){
  8047. me.options.fullscreen && Fullscreen.getInstance( me ).toggle();
  8048. });
  8049. return $button;
  8050. });
  8051. UM.commands[ 'fullscreen' ] = {
  8052. execCommand: function (cmdName) {
  8053. Fullscreen.getInstance( this ).toggle();
  8054. },
  8055. queryCommandState: function (cmdName) {
  8056. return this._edui_fullscreen_status;
  8057. },
  8058. notNeedUndo: 1
  8059. };
  8060. function Fullscreen( editor ) {
  8061. var me = this;
  8062. if( !editor ) {
  8063. throw new Error('invalid params, notfound editor');
  8064. }
  8065. me.editor = editor;
  8066. //记录初始化的全屏组件
  8067. FULLSCREENS[ editor.uid ] = this;
  8068. editor.addListener('destroy', function(){
  8069. delete FULLSCREENS[ editor.uid ];
  8070. me.editor = null;
  8071. });
  8072. }
  8073. Fullscreen.prototype = {
  8074. /**
  8075. * 全屏状态切换
  8076. */
  8077. toggle: function(){
  8078. var editor = this.editor,
  8079. //当前编辑器的缩放状态
  8080. _edui_fullscreen_status = this.isFullState();
  8081. editor.fireEvent('beforefullscreenchange', !_edui_fullscreen_status );
  8082. //更新状态
  8083. this.update( !_edui_fullscreen_status );
  8084. !_edui_fullscreen_status ? this.enlarge() : this.revert();
  8085. editor.fireEvent('afterfullscreenchange', !_edui_fullscreen_status );
  8086. if(editor.body.contentEditable === 'true'){
  8087. editor.fireEvent( 'fullscreenchanged', !_edui_fullscreen_status );
  8088. }
  8089. editor.fireEvent( 'selectionchange' );
  8090. },
  8091. /**
  8092. * 执行放大
  8093. */
  8094. enlarge: function(){
  8095. this.saveSataus();
  8096. this.setDocumentStatus();
  8097. this.resize();
  8098. },
  8099. /**
  8100. * 全屏还原
  8101. */
  8102. revert: function(){
  8103. //还原CSS表达式
  8104. var options = this.editor.options,
  8105. height = /%$/.test(options.initialFrameHeight) ? '100%' : (options.initialFrameHeight - this.getStyleValue("padding-top")- this.getStyleValue("padding-bottom") - this.getStyleValue('border-width'));
  8106. $.IE6 && this.getEditorHolder().style.setExpression('height', 'this.scrollHeight <= ' + height + ' ? "' + height + 'px" : "auto"');
  8107. //还原容器状态
  8108. this.revertContainerStatus();
  8109. this.revertContentAreaStatus();
  8110. this.revertDocumentStatus();
  8111. },
  8112. /**
  8113. * 更新状态
  8114. * @param isFull 当前状态是否是全屏状态
  8115. */
  8116. update: function( isFull ) {
  8117. this.editor._edui_fullscreen_status = isFull;
  8118. },
  8119. /**
  8120. * 调整当前编辑器的大小, 如果当前编辑器不处于全屏状态, 则不做调整
  8121. */
  8122. resize: function(){
  8123. var $win = null,
  8124. height = 0,
  8125. width = 0,
  8126. borderWidth = 0,
  8127. paddingWidth = 0,
  8128. editor = this.editor,
  8129. me = this,
  8130. bound = null,
  8131. editorBody = null;
  8132. if( !this.isFullState() ) {
  8133. return;
  8134. }
  8135. $win = $( window );
  8136. width = $win.width();
  8137. height = $win.height();
  8138. editorBody = this.getEditorHolder();
  8139. //文本编辑区border宽度
  8140. borderWidth = parseInt( domUtils.getComputedStyle( editorBody, 'border-width' ), 10 ) || 0;
  8141. //容器border宽度
  8142. borderWidth += parseInt( domUtils.getComputedStyle( editor.container, 'border-width' ), 10 ) || 0;
  8143. //容器padding
  8144. paddingWidth += parseInt( domUtils.getComputedStyle( editorBody, 'padding-left' ), 10 ) + parseInt( domUtils.getComputedStyle( editorBody, 'padding-right' ), 10 ) || 0;
  8145. //干掉css表达式
  8146. $.IE6 && editorBody.style.setExpression( 'height', null );
  8147. bound = this.getBound();
  8148. $( editor.container ).css( {
  8149. width: width + 'px',
  8150. height: height + 'px',
  8151. position: !$.IE6 ? 'fixed' : 'absolute',
  8152. top: bound.top,
  8153. left: bound.left,
  8154. margin: 0,
  8155. padding: 0,
  8156. overflowX: 'hidden',
  8157. overflowY: 'hidden'
  8158. } );
  8159. $( editorBody ).css({
  8160. width: width - 2*borderWidth - paddingWidth + 'px',
  8161. height: height - 2*borderWidth - ( editor.options.withoutToolbar ? 0 : $( '.edui-toolbar', editor.container ).outerHeight() ) - $( '.edui-bottombar', editor.container).outerHeight() + 'px',
  8162. overflowX: 'hidden',
  8163. overflowY: 'auto'
  8164. });
  8165. },
  8166. /**
  8167. * 保存状态
  8168. */
  8169. saveSataus: function(){
  8170. var styles = this.editor.container.style,
  8171. tmp = null,
  8172. cache = {};
  8173. for( var i= 0, len = STATUS_LIST.length; i<len; i++ ) {
  8174. tmp = STATUS_LIST[ i ];
  8175. cache[ tmp ] = styles[ tmp ];
  8176. }
  8177. STATUS_CACHE[ this.editor.uid ] = cache;
  8178. this.saveContentAreaStatus();
  8179. this.saveDocumentStatus();
  8180. },
  8181. saveContentAreaStatus: function(){
  8182. var $holder = $(this.getEditorHolder());
  8183. CONTENT_AREA_STATUS[ this.editor.uid ] = {
  8184. width: $holder.css("width"),
  8185. overflowX: $holder.css("overflowX"),
  8186. overflowY: $holder.css("overflowY"),
  8187. height: $holder.css("height")
  8188. };
  8189. },
  8190. /**
  8191. * 保存与指定editor相关的页面的状态
  8192. */
  8193. saveDocumentStatus: function(){
  8194. var $doc = $( this.getEditorDocumentBody() );
  8195. DOCUMENT_STATUS[ this.editor.uid ] = {
  8196. overflowX: $doc.css( 'overflowX' ),
  8197. overflowY: $doc.css( 'overflowY' )
  8198. };
  8199. DOCUMENT_ELEMENT_STATUS[ this.editor.uid ] = {
  8200. overflowX: $( this.getEditorDocumentElement() ).css( 'overflowX'),
  8201. overflowY: $( this.getEditorDocumentElement() ).css( 'overflowY' )
  8202. };
  8203. },
  8204. /**
  8205. * 恢复容器状态
  8206. */
  8207. revertContainerStatus: function(){
  8208. $( this.editor.container ).css( this.getEditorStatus() );
  8209. },
  8210. /**
  8211. * 恢复编辑区状态
  8212. */
  8213. revertContentAreaStatus: function(){
  8214. var holder = this.getEditorHolder(),
  8215. state = this.getContentAreaStatus();
  8216. if ( this.supportMin() ) {
  8217. delete state.height;
  8218. holder.style.height = null;
  8219. }
  8220. $( holder ).css( state );
  8221. },
  8222. /**
  8223. * 恢复页面状态
  8224. */
  8225. revertDocumentStatus: function() {
  8226. var status = this.getDocumentStatus();
  8227. $( this.getEditorDocumentBody() ).css( 'overflowX', status.body.overflowX );
  8228. $( this.getEditorDocumentElement() ).css( 'overflowY', status.html.overflowY );
  8229. },
  8230. setDocumentStatus: function(){
  8231. $(this.getEditorDocumentBody()).css( {
  8232. overflowX: 'hidden',
  8233. overflowY: 'hidden'
  8234. } );
  8235. $(this.getEditorDocumentElement()).css( {
  8236. overflowX: 'hidden',
  8237. overflowY: 'hidden'
  8238. } );
  8239. },
  8240. /**
  8241. * 检测当前编辑器是否处于全屏状态全屏状态
  8242. * @returns {boolean} 是否处于全屏状态
  8243. */
  8244. isFullState: function(){
  8245. return !!this.editor._edui_fullscreen_status;
  8246. },
  8247. /**
  8248. * 获取编辑器状态
  8249. */
  8250. getEditorStatus: function(){
  8251. return STATUS_CACHE[ this.editor.uid ];
  8252. },
  8253. getContentAreaStatus: function(){
  8254. return CONTENT_AREA_STATUS[ this.editor.uid ];
  8255. },
  8256. getEditorDocumentElement: function(){
  8257. return this.editor.container.ownerDocument.documentElement;
  8258. },
  8259. getEditorDocumentBody: function(){
  8260. return this.editor.container.ownerDocument.body;
  8261. },
  8262. /**
  8263. * 获取编辑区包裹对象
  8264. */
  8265. getEditorHolder: function(){
  8266. return this.editor.body;
  8267. },
  8268. /**
  8269. * 获取编辑器状态
  8270. * @returns {*}
  8271. */
  8272. getDocumentStatus: function(){
  8273. return {
  8274. 'body': DOCUMENT_STATUS[ this.editor.uid ],
  8275. 'html': DOCUMENT_ELEMENT_STATUS[ this.editor.uid ]
  8276. };
  8277. },
  8278. supportMin: function () {
  8279. var node = null;
  8280. if ( !this._support ) {
  8281. node = document.createElement("div");
  8282. this._support = "minWidth" in node.style;
  8283. node = null;
  8284. }
  8285. return this._support;
  8286. },
  8287. getBound: function () {
  8288. var tags = {
  8289. html: true,
  8290. body: true
  8291. },
  8292. result = {
  8293. top: 0,
  8294. left: 0
  8295. },
  8296. offsetParent = null;
  8297. if ( !$.IE6 ) {
  8298. return result;
  8299. }
  8300. offsetParent = this.editor.container.offsetParent;
  8301. if( offsetParent && !tags[ offsetParent.nodeName.toLowerCase() ] ) {
  8302. tags = offsetParent.getBoundingClientRect();
  8303. result.top = -tags.top;
  8304. result.left = -tags.left;
  8305. }
  8306. return result;
  8307. },
  8308. getStyleValue: function (attr) {
  8309. return parseInt(domUtils.getComputedStyle( this.getEditorHolder() ,attr));
  8310. }
  8311. };
  8312. $.extend( Fullscreen, {
  8313. /**
  8314. * 监听resize
  8315. */
  8316. listen: function(){
  8317. var timer = null;
  8318. if( Fullscreen._hasFullscreenListener ) {
  8319. return;
  8320. }
  8321. Fullscreen._hasFullscreenListener = true;
  8322. $( window ).on( 'resize', function(){
  8323. if( timer !== null ) {
  8324. window.clearTimeout( timer );
  8325. timer = null;
  8326. }
  8327. timer = window.setTimeout(function(){
  8328. for( var key in FULLSCREENS ) {
  8329. FULLSCREENS[ key ].resize();
  8330. }
  8331. timer = null;
  8332. }, 50);
  8333. } );
  8334. },
  8335. getInstance: function ( editor ) {
  8336. if ( !FULLSCREENS[editor.uid ] ) {
  8337. new Fullscreen( editor );
  8338. }
  8339. return FULLSCREENS[editor.uid ];
  8340. }
  8341. });
  8342. //开始监听
  8343. Fullscreen.listen();
  8344. })();
  8345. UM.registerUI('link image map insertvideo',function(name){
  8346. var me = this, currentRange, $dialog,
  8347. dialogUrl = {
  8348. insertvideo: 'video'
  8349. },
  8350. curDialogUrl = dialogUrl[ name ] || name,
  8351. opt = {
  8352. title: (me.options.labelMap && me.options.labelMap[name]) || me.getLang("labelMap." + name),
  8353. url: me.options.UMEDITOR_HOME_URL + 'dialogs/' + curDialogUrl + '/' + curDialogUrl + '.js'
  8354. };
  8355. var $btn = $.eduibutton({
  8356. icon: name,
  8357. title: this.getLang('labelMap')[name] || ''
  8358. });
  8359. //加载模版数据
  8360. utils.loadFile(document,{
  8361. src: opt.url,
  8362. tag: "script",
  8363. type: "text/javascript",
  8364. defer: "defer"
  8365. },function(){
  8366. //调整数据
  8367. var data = UM.getWidgetData(name);
  8368. if(data.buttons){
  8369. var ok = data.buttons.ok;
  8370. if(ok){
  8371. opt.oklabel = ok.label || me.getLang('ok');
  8372. if(ok.exec){
  8373. opt.okFn = function(){
  8374. return $.proxy(ok.exec,null,me,$dialog)()
  8375. }
  8376. }
  8377. }
  8378. var cancel = data.buttons.cancel;
  8379. if(cancel){
  8380. opt.cancellabel = cancel.label || me.getLang('cancel');
  8381. if(cancel.exec){
  8382. opt.cancelFn = function(){
  8383. return $.proxy(cancel.exec,null,me,$dialog)()
  8384. }
  8385. }
  8386. }
  8387. }
  8388. data.width && (opt.width = data.width);
  8389. data.height && (opt.height = data.height);
  8390. $dialog = $.eduimodal(opt);
  8391. $dialog.attr('id', 'edui-dialog-' + name)
  8392. .find('.edui-modal-body').addClass('edui-dialog-' + name + '-body');
  8393. $dialog.edui().on('beforehide',function () {
  8394. var rng = me.selection.getRange();
  8395. if (rng.equals(currentRange)) {
  8396. rng.select()
  8397. }
  8398. }).on('beforeshow', function () {
  8399. var $root = this.root(),
  8400. win = null,
  8401. offset = null;
  8402. currentRange = me.selection.getRange();
  8403. if (!$root.parent()[0]) {
  8404. me.$container.find('.edui-dialog-container').append($root);
  8405. }
  8406. //IE6下 特殊处理, 通过计算进行定位
  8407. if( $.IE6 ) {
  8408. win = {
  8409. width: $( window ).width(),
  8410. height: $( window ).height()
  8411. };
  8412. offset = $root.parents(".edui-toolbar")[0].getBoundingClientRect();
  8413. $root.css({
  8414. position: 'absolute',
  8415. margin: 0,
  8416. left: ( win.width - $root.width() ) / 2 - offset.left,
  8417. top: 100 - offset.top
  8418. });
  8419. }
  8420. UM.setWidgetBody(name,$dialog,me);
  8421. }).on('afterbackdrop',function(){
  8422. this.$backdrop.css('zIndex',me.getOpt('zIndex')+1).appendTo(me.$container.find('.edui-dialog-container'))
  8423. $dialog.css('zIndex',me.getOpt('zIndex')+2)
  8424. }).on('beforeok',function(){
  8425. try{
  8426. currentRange.select()
  8427. }catch(e){}
  8428. }).attachTo($btn)
  8429. });
  8430. me.addListener('selectionchange', function () {
  8431. var state = this.queryCommandState(name);
  8432. $btn.edui().disabled(state == -1).active(state == 1)
  8433. });
  8434. return $btn;
  8435. });
  8436. UM.registerUI( 'emotion', function( name ){
  8437. var me = this,
  8438. url = me.options.UMEDITOR_HOME_URL + 'dialogs/' +name+ '/'+name+'.js';
  8439. var $btn = $.eduibutton({
  8440. icon: name,
  8441. title: this.getLang('labelMap')[name] || ''
  8442. });
  8443. //加载模版数据
  8444. utils.loadFile(document,{
  8445. src: url,
  8446. tag: "script",
  8447. type: "text/javascript",
  8448. defer: "defer"
  8449. },function(){
  8450. var opt = {
  8451. url : url
  8452. };
  8453. //调整数据
  8454. var data = UM.getWidgetData(name);
  8455. data.width && (opt.width = data.width);
  8456. data.height && (opt.height = data.height);
  8457. $.eduipopup(opt).css('zIndex',me.options.zIndex + 1)
  8458. .edui()
  8459. .on('beforeshow',function(){
  8460. var $root = this.root();
  8461. if(!$root.parent().length){
  8462. me.$container.find('.edui-dialog-container').append($root);
  8463. }
  8464. UM.setWidgetBody(name,$root,me);
  8465. }).attachTo($btn,{
  8466. offsetTop:-5,
  8467. offsetLeft:10,
  8468. caretLeft:11,
  8469. caretTop:-8
  8470. });
  8471. me.addListener('selectionchange', function () {
  8472. var state = this.queryCommandState('emotion');
  8473. $btn.edui().disabled(state == -1).active(state == 1);
  8474. });
  8475. });
  8476. return $btn;
  8477. } );
  8478. UM.registerUI('imagescale',function () {
  8479. var me = this,
  8480. $imagescale;
  8481. me.setOpt('imageScaleEnabled', true);
  8482. if (browser.webkit && me.getOpt('imageScaleEnabled')) {
  8483. me.addListener('click', function () {
  8484. var range = me.selection.getRange(),
  8485. img = range.getClosedNode();
  8486. if (img && img.tagName == 'IMG') {
  8487. if (!$imagescale) {
  8488. $imagescale = $.eduiscale({'$wrap':me.$container}).css('zIndex', me.options.zIndex);
  8489. me.$container.append($imagescale);
  8490. var _keyDownHandler = function () {
  8491. $imagescale.edui().hide();
  8492. }, _mouseDownHandler = function (e) {
  8493. var ele = e.target || e.srcElement;
  8494. if (ele && ele.className.indexOf('edui-scale') == -1) {
  8495. _keyDownHandler(e);
  8496. }
  8497. }, timer;
  8498. $imagescale.edui()
  8499. .on('aftershow', function () {
  8500. $(document).bind('keydown', _keyDownHandler);
  8501. $(document).bind('mousedown', _mouseDownHandler);
  8502. me.selection.getNative().removeAllRanges();
  8503. })
  8504. .on('afterhide', function () {
  8505. $(document).unbind('keydown', _keyDownHandler);
  8506. $(document).unbind('mousedown', _mouseDownHandler);
  8507. var target = $imagescale.edui().getScaleTarget();
  8508. if (target.parentNode) {
  8509. me.selection.getRange().selectNode(target).select();
  8510. }
  8511. })
  8512. .on('mousedown', function (e) {
  8513. me.selection.getNative().removeAllRanges();
  8514. var ele = e.target || e.srcElement;
  8515. if (ele && ele.className.indexOf('edui-scale-hand') == -1) {
  8516. timer = setTimeout(function() {
  8517. $imagescale.edui().hide();
  8518. }, 200);
  8519. }
  8520. })
  8521. .on('mouseup', function (e) {
  8522. var ele = e.target || e.srcElement;
  8523. if (ele && ele.className.indexOf('edui-scale-hand') == -1) {
  8524. clearTimeout(timer);
  8525. }
  8526. });
  8527. }
  8528. $imagescale.edui().show($(img));
  8529. } else {
  8530. if ($imagescale && $imagescale.css('display') != 'none') $imagescale.edui().hide();
  8531. }
  8532. });
  8533. me.addListener('click', function (type, e) {
  8534. if (e.target.tagName == 'IMG') {
  8535. var range = new dom.Range(me.document, me.body);
  8536. range.selectNode(e.target).select();
  8537. }
  8538. });
  8539. }
  8540. });
  8541. UM.registerUI('autofloat',function(){
  8542. var me = this,
  8543. lang = me.getLang();
  8544. me.setOpt({
  8545. topOffset:0
  8546. });
  8547. var optsAutoFloatEnabled = me.options.autoFloatEnabled !== false,
  8548. topOffset = me.options.topOffset;
  8549. //如果不固定toolbar的位置,则直接退出
  8550. if(!optsAutoFloatEnabled){
  8551. return;
  8552. }
  8553. me.ready(function(){
  8554. var LteIE6 = browser.ie && browser.version <= 6,
  8555. quirks = browser.quirks;
  8556. function checkHasUI(){
  8557. if(!UM.ui){
  8558. alert(lang.autofloatMsg);
  8559. return 0;
  8560. }
  8561. return 1;
  8562. }
  8563. function fixIE6FixedPos(){
  8564. var docStyle = document.body.style;
  8565. docStyle.backgroundImage = 'url("about:blank")';
  8566. docStyle.backgroundAttachment = 'fixed';
  8567. }
  8568. var bakCssText,
  8569. placeHolder = document.createElement('div'),
  8570. toolbarBox,orgTop,
  8571. getPosition=function(element){
  8572. var bcr;
  8573. //trace IE6下在控制编辑器显隐时可能会报错,catch一下
  8574. try{
  8575. bcr = element.getBoundingClientRect();
  8576. }catch(e){
  8577. bcr={left:0,top:0,height:0,width:0}
  8578. }
  8579. var rect = {
  8580. left: Math.round(bcr.left),
  8581. top: Math.round(bcr.top),
  8582. height: Math.round(bcr.bottom - bcr.top),
  8583. width: Math.round(bcr.right - bcr.left)
  8584. };
  8585. var doc;
  8586. while ((doc = element.ownerDocument) !== document &&
  8587. (element = domUtils.getWindow(doc).frameElement)) {
  8588. bcr = element.getBoundingClientRect();
  8589. rect.left += bcr.left;
  8590. rect.top += bcr.top;
  8591. }
  8592. rect.bottom = rect.top + rect.height;
  8593. rect.right = rect.left + rect.width;
  8594. return rect;
  8595. };
  8596. var isFullScreening = false;
  8597. function setFloating(){
  8598. if(isFullScreening){
  8599. return;
  8600. }
  8601. var toobarBoxPos = domUtils.getXY(toolbarBox),
  8602. origalFloat = domUtils.getComputedStyle(toolbarBox,'position'),
  8603. origalLeft = domUtils.getComputedStyle(toolbarBox,'left');
  8604. toolbarBox.style.width = toolbarBox.offsetWidth + 'px';
  8605. toolbarBox.style.zIndex = me.options.zIndex * 1 + 1;
  8606. toolbarBox.parentNode.insertBefore(placeHolder, toolbarBox);
  8607. if (LteIE6 || (quirks && browser.ie)) {
  8608. if(toolbarBox.style.position != 'absolute'){
  8609. toolbarBox.style.position = 'absolute';
  8610. }
  8611. toolbarBox.style.top = (document.body.scrollTop||document.documentElement.scrollTop) - orgTop + topOffset + 'px';
  8612. } else {
  8613. if(toolbarBox.style.position != 'fixed'){
  8614. toolbarBox.style.position = 'fixed';
  8615. toolbarBox.style.top = topOffset +"px";
  8616. ((origalFloat == 'absolute' || origalFloat == 'relative') && parseFloat(origalLeft)) && (toolbarBox.style.left = toobarBoxPos.x + 'px');
  8617. }
  8618. }
  8619. }
  8620. function unsetFloating(){
  8621. if(placeHolder.parentNode){
  8622. placeHolder.parentNode.removeChild(placeHolder);
  8623. }
  8624. toolbarBox.style.cssText = bakCssText;
  8625. }
  8626. function updateFloating(){
  8627. var rect3 = getPosition(me.container);
  8628. var offset=me.options.toolbarTopOffset||0;
  8629. if (rect3.top < 0 && rect3.bottom - toolbarBox.offsetHeight > offset) {
  8630. setFloating();
  8631. }else{
  8632. unsetFloating();
  8633. }
  8634. }
  8635. var defer_updateFloating = utils.defer(function(){
  8636. updateFloating();
  8637. },browser.ie ? 200 : 100,true);
  8638. me.addListener('destroy',function(){
  8639. domUtils.un(window, ['scroll','resize'], updateFloating);
  8640. me.removeListener('keydown', defer_updateFloating);
  8641. });
  8642. if(checkHasUI(me)){
  8643. toolbarBox = $('.edui-toolbar',me.container)[0];
  8644. me.addListener("afteruiready",function(){
  8645. setTimeout(function(){
  8646. orgTop = $(toolbarBox).offset().top;
  8647. },100);
  8648. });
  8649. bakCssText = toolbarBox.style.cssText;
  8650. placeHolder.style.height = toolbarBox.offsetHeight + 'px';
  8651. if(LteIE6){
  8652. fixIE6FixedPos();
  8653. }
  8654. domUtils.on(window, ['scroll','resize'], updateFloating);
  8655. me.addListener('keydown', defer_updateFloating);
  8656. me.addListener('beforefullscreenchange', function (t, enabled){
  8657. if (enabled) {
  8658. unsetFloating();
  8659. isFullScreening = enabled;
  8660. }
  8661. });
  8662. me.addListener('fullscreenchanged', function (t, enabled){
  8663. if (!enabled) {
  8664. updateFloating();
  8665. }
  8666. isFullScreening = enabled;
  8667. });
  8668. me.addListener('sourcemodechanged', function (t, enabled){
  8669. setTimeout(function (){
  8670. updateFloating();
  8671. },0);
  8672. });
  8673. me.addListener("clearDoc",function(){
  8674. setTimeout(function(){
  8675. updateFloating();
  8676. },0);
  8677. })
  8678. }
  8679. })
  8680. })
  8681. UM.registerUI('source',function(name){
  8682. var me = this;
  8683. me.addListener('fullscreenchanged',function(){
  8684. me.$container.find('textarea').width(me.$body.width() - 10).height(me.$body.height())
  8685. });
  8686. var $btn = $.eduibutton({
  8687. icon : name,
  8688. click : function(){
  8689. me.execCommand(name)
  8690. },
  8691. title: this.getLang('labelMap')[name] || ''
  8692. });
  8693. this.addListener('selectionchange',function(){
  8694. var state = this.queryCommandState(name);
  8695. $btn.edui().disabled(state == -1).active(state == 1)
  8696. });
  8697. return $btn;
  8698. });
  8699. UM.registerUI('paragraph fontfamily fontsize', function( name ) {
  8700. var me = this,
  8701. label = (me.options.labelMap && me.options.labelMap[name]) || me.getLang("labelMap." + name),
  8702. options = {
  8703. label: label,
  8704. title: label,
  8705. comboboxName: name,
  8706. items: me.options[ name ] || [],
  8707. itemStyles: [],
  8708. value: [],
  8709. autowidthitem: []
  8710. },
  8711. $combox = null,
  8712. comboboxWidget = null;
  8713. switch ( name ) {
  8714. case 'paragraph':
  8715. options = transForParagraph( options );
  8716. break;
  8717. case 'fontfamily':
  8718. options = transForFontfamily( options );
  8719. break;
  8720. case 'fontsize':
  8721. options = transForFontsize( options );
  8722. break;
  8723. }
  8724. //实例化
  8725. $combox = $.eduibuttoncombobox(options).css('zIndex',me.getOpt('zIndex') + 1);
  8726. comboboxWidget = $combox.edui();
  8727. comboboxWidget.on('comboboxselect', function( evt, res ){
  8728. me.execCommand( name, res.value );
  8729. }).on("beforeshow", function(){
  8730. if( $combox.parent().length === 0 ) {
  8731. $combox.appendTo( me.$container.find('.edui-dialog-container') );
  8732. }
  8733. });
  8734. //状态反射
  8735. this.addListener('selectionchange',function( evt ){
  8736. var state = this.queryCommandState( name ),
  8737. value = this.queryCommandValue( name );
  8738. //设置按钮状态
  8739. comboboxWidget.button().edui().disabled( state == -1 ).active( state == 1 );
  8740. if(value){
  8741. //设置label
  8742. value = value.replace(/['"]/g, '').toLowerCase().split(/['|"]?\s*,\s*[\1]?/);
  8743. comboboxWidget.selectItemByLabel( value );
  8744. }
  8745. });
  8746. return comboboxWidget.button().addClass('edui-combobox');
  8747. /**
  8748. * 宽度自适应工具函数
  8749. * @param word 单词内容
  8750. * @param hasSuffix 是否含有后缀
  8751. */
  8752. function wordCountAdaptive ( word, hasSuffix ) {
  8753. var $tmpNode = $('<span>' ).html( word ).css( {
  8754. display: 'inline',
  8755. position: 'absolute',
  8756. top: -10000000,
  8757. left: -100000
  8758. } ).appendTo( document.body),
  8759. width = $tmpNode.width();
  8760. $tmpNode.remove();
  8761. $tmpNode = null;
  8762. if( width < 50 ) {
  8763. return word;
  8764. } else {
  8765. word = word.slice( 0, hasSuffix ? -4 : -1 );
  8766. if( !word.length ) {
  8767. return '...';
  8768. }
  8769. return wordCountAdaptive( word + '...', true );
  8770. }
  8771. }
  8772. //段落参数转换
  8773. function transForParagraph ( options ) {
  8774. var tempItems = [];
  8775. for( var key in options.items ) {
  8776. options.value.push( key );
  8777. tempItems.push( key );
  8778. options.autowidthitem.push( wordCountAdaptive( key ) );
  8779. }
  8780. options.items = tempItems;
  8781. options.autoRecord = false;
  8782. return options;
  8783. }
  8784. //字体参数转换
  8785. function transForFontfamily ( options ) {
  8786. var temp = null,
  8787. tempItems = [];
  8788. for( var i = 0, len = options.items.length; i < len; i++ ) {
  8789. temp = options.items[ i ].val;
  8790. tempItems.push( temp.split(/\s*,\s*/)[0] );
  8791. options.itemStyles.push('font-family: ' + temp);
  8792. options.value.push( temp );
  8793. options.autowidthitem.push( wordCountAdaptive( tempItems[ i ] ) );
  8794. }
  8795. options.items = tempItems;
  8796. return options;
  8797. }
  8798. //字体大小参数转换
  8799. function transForFontsize ( options ) {
  8800. var temp = null,
  8801. tempItems = [];
  8802. options.itemStyles = [];
  8803. options.value = [];
  8804. for( var i = 0, len = options.items.length; i < len; i++ ) {
  8805. temp = options.items[ i ];
  8806. tempItems.push( temp );
  8807. options.itemStyles.push('font-size: ' + temp +'px');
  8808. }
  8809. options.value = options.items;
  8810. options.items = tempItems;
  8811. options.autoRecord = false;
  8812. return options;
  8813. }
  8814. });
  8815. UM.registerUI('forecolor backcolor', function( name ) {
  8816. function getCurrentColor() {
  8817. return domUtils.getComputedStyle( $colorLabel[0], 'background-color' );
  8818. }
  8819. var me = this,
  8820. $colorPickerWidget = null,
  8821. $colorLabel = null,
  8822. $btn = null;
  8823. //querycommand
  8824. this.addListener('selectionchange', function(){
  8825. var state = this.queryCommandState( name );
  8826. $btn.edui().disabled( state == -1 ).active( state == 1 );
  8827. });
  8828. $btn = $.eduicolorsplitbutton({
  8829. icon: name,
  8830. caret: true,
  8831. name: name,
  8832. title: me.getLang("labelMap")[name],
  8833. click: function() {
  8834. me.execCommand( name, getCurrentColor() );
  8835. }
  8836. });
  8837. $colorLabel = $btn.edui().colorLabel();
  8838. $colorPickerWidget = $.eduicolorpicker({
  8839. name: name,
  8840. lang_clearColor: me.getLang('clearColor') || '',
  8841. lang_themeColor: me.getLang('themeColor') || '',
  8842. lang_standardColor: me.getLang('standardColor') || ''
  8843. })
  8844. .on('pickcolor', function( evt, color ){
  8845. window.setTimeout( function(){
  8846. $colorLabel.css("backgroundColor", color);
  8847. me.execCommand( name, color );
  8848. }, 0 );
  8849. })
  8850. .on('show',function(){
  8851. UM.setActiveWidget( colorPickerWidget.root() );
  8852. }).css('zIndex',me.getOpt('zIndex') + 1);
  8853. $btn.edui().on('arrowclick',function(){
  8854. if(!$colorPickerWidget.parent().length){
  8855. me.$container.find('.edui-dialog-container').append($colorPickerWidget);
  8856. }
  8857. $colorPickerWidget.edui().show($btn,{
  8858. caretDir:"down",
  8859. offsetTop:-5,
  8860. offsetLeft:8,
  8861. caretLeft:11,
  8862. caretTop:-8
  8863. });
  8864. }).register('click', $btn, function () {
  8865. $colorPickerWidget.edui().hide()
  8866. });
  8867. return $btn;
  8868. });
  8869. })()