ueditor.parse.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  1. (function(){
  2. // parse.js
  3. (function(){
  4. UE = window.UE || {};
  5. var isIE = !!window.ActiveXObject;
  6. //定义utils工具
  7. var utils = {
  8. removeLastbs : function(url){
  9. return url.replace(/\/$/,'')
  10. },
  11. extend : function(t,s){
  12. var a = arguments,
  13. notCover = this.isBoolean(a[a.length - 1]) ? a[a.length - 1] : false,
  14. len = this.isBoolean(a[a.length - 1]) ? a.length - 1 : a.length;
  15. for (var i = 1; i < len; i++) {
  16. var x = a[i];
  17. for (var k in x) {
  18. if (!notCover || !t.hasOwnProperty(k)) {
  19. t[k] = x[k];
  20. }
  21. }
  22. }
  23. return t;
  24. },
  25. isIE : isIE,
  26. cssRule : isIE ? function(key,style,doc){
  27. var indexList,index;
  28. doc = doc || document;
  29. if(doc.indexList){
  30. indexList = doc.indexList;
  31. }else{
  32. indexList = doc.indexList = {};
  33. }
  34. var sheetStyle;
  35. if(!indexList[key]){
  36. if(style === undefined){
  37. return ''
  38. }
  39. sheetStyle = doc.createStyleSheet('',index = doc.styleSheets.length);
  40. indexList[key] = index;
  41. }else{
  42. sheetStyle = doc.styleSheets[indexList[key]];
  43. }
  44. if(style === undefined){
  45. return sheetStyle.cssText
  46. }
  47. sheetStyle.cssText = sheetStyle.cssText + '\n' + (style || '')
  48. } : function(key,style,doc){
  49. doc = doc || document;
  50. var head = doc.getElementsByTagName('head')[0],node;
  51. if(!(node = doc.getElementById(key))){
  52. if(style === undefined){
  53. return ''
  54. }
  55. node = doc.createElement('style');
  56. node.id = key;
  57. head.appendChild(node)
  58. }
  59. if(style === undefined){
  60. return node.innerHTML
  61. }
  62. if(style !== ''){
  63. node.innerHTML = node.innerHTML + '\n' + style;
  64. }else{
  65. head.removeChild(node)
  66. }
  67. },
  68. domReady : function (onready) {
  69. var doc = window.document;
  70. if (doc.readyState === "complete") {
  71. onready();
  72. }else{
  73. if (isIE) {
  74. (function () {
  75. if (doc.isReady) return;
  76. try {
  77. doc.documentElement.doScroll("left");
  78. } catch (error) {
  79. setTimeout(arguments.callee, 0);
  80. return;
  81. }
  82. onready();
  83. })();
  84. window.attachEvent('onload', function(){
  85. onready()
  86. });
  87. } else {
  88. doc.addEventListener("DOMContentLoaded", function () {
  89. doc.removeEventListener("DOMContentLoaded", arguments.callee, false);
  90. onready();
  91. }, false);
  92. window.addEventListener('load', function(){onready()}, false);
  93. }
  94. }
  95. },
  96. each : function(obj, iterator, context) {
  97. if (obj == null) return;
  98. if (obj.length === +obj.length) {
  99. for (var i = 0, l = obj.length; i < l; i++) {
  100. if(iterator.call(context, obj[i], i, obj) === false)
  101. return false;
  102. }
  103. } else {
  104. for (var key in obj) {
  105. if (obj.hasOwnProperty(key)) {
  106. if(iterator.call(context, obj[key], key, obj) === false)
  107. return false;
  108. }
  109. }
  110. }
  111. },
  112. inArray : function(arr,item){
  113. var index = -1;
  114. this.each(arr,function(v,i){
  115. if(v === item){
  116. index = i;
  117. return false;
  118. }
  119. });
  120. return index;
  121. },
  122. pushItem : function(arr,item){
  123. if(this.inArray(arr,item)==-1){
  124. arr.push(item)
  125. }
  126. },
  127. trim: function (str) {
  128. return str.replace(/(^[ \t\n\r]+)|([ \t\n\r]+$)/g, '');
  129. },
  130. indexOf: function (array, item, start) {
  131. var index = -1;
  132. start = this.isNumber(start) ? start : 0;
  133. this.each(array, function (v, i) {
  134. if (i >= start && v === item) {
  135. index = i;
  136. return false;
  137. }
  138. });
  139. return index;
  140. },
  141. hasClass: function (element, className) {
  142. className = className.replace(/(^[ ]+)|([ ]+$)/g, '').replace(/[ ]{2,}/g, ' ').split(' ');
  143. for (var i = 0, ci, cls = element.className; ci = className[i++];) {
  144. if (!new RegExp('\\b' + ci + '\\b', 'i').test(cls)) {
  145. return false;
  146. }
  147. }
  148. return i - 1 == className.length;
  149. },
  150. addClass:function (elm, classNames) {
  151. if(!elm)return;
  152. classNames = this.trim(classNames).replace(/[ ]{2,}/g,' ').split(' ');
  153. for(var i = 0,ci,cls = elm.className;ci=classNames[i++];){
  154. if(!new RegExp('\\b' + ci + '\\b').test(cls)){
  155. cls += ' ' + ci;
  156. }
  157. }
  158. elm.className = utils.trim(cls);
  159. },
  160. removeClass:function (elm, classNames) {
  161. classNames = this.isArray(classNames) ? classNames :
  162. this.trim(classNames).replace(/[ ]{2,}/g,' ').split(' ');
  163. for(var i = 0,ci,cls = elm.className;ci=classNames[i++];){
  164. cls = cls.replace(new RegExp('\\b' + ci + '\\b'),'')
  165. }
  166. cls = this.trim(cls).replace(/[ ]{2,}/g,' ');
  167. elm.className = cls;
  168. !cls && elm.removeAttribute('className');
  169. },
  170. on: function (element, type, handler) {
  171. var types = this.isArray(type) ? type : type.split(/\s+/),
  172. k = types.length;
  173. if (k) while (k--) {
  174. type = types[k];
  175. if (element.addEventListener) {
  176. element.addEventListener(type, handler, false);
  177. } else {
  178. if (!handler._d) {
  179. handler._d = {
  180. els : []
  181. };
  182. }
  183. var key = type + handler.toString(),index = utils.indexOf(handler._d.els,element);
  184. if (!handler._d[key] || index == -1) {
  185. if(index == -1){
  186. handler._d.els.push(element);
  187. }
  188. if(!handler._d[key]){
  189. handler._d[key] = function (evt) {
  190. return handler.call(evt.srcElement, evt || window.event);
  191. };
  192. }
  193. element.attachEvent('on' + type, handler._d[key]);
  194. }
  195. }
  196. }
  197. element = null;
  198. },
  199. off: function (element, type, handler) {
  200. var types = this.isArray(type) ? type : type.split(/\s+/),
  201. k = types.length;
  202. if (k) while (k--) {
  203. type = types[k];
  204. if (element.removeEventListener) {
  205. element.removeEventListener(type, handler, false);
  206. } else {
  207. var key = type + handler.toString();
  208. try{
  209. element.detachEvent('on' + type, handler._d ? handler._d[key] : handler);
  210. }catch(e){}
  211. if (handler._d && handler._d[key]) {
  212. var index = utils.indexOf(handler._d.els,element);
  213. if(index!=-1){
  214. handler._d.els.splice(index,1);
  215. }
  216. handler._d.els.length == 0 && delete handler._d[key];
  217. }
  218. }
  219. }
  220. },
  221. loadFile : function () {
  222. var tmpList = [];
  223. function getItem(doc,obj){
  224. try{
  225. for(var i= 0,ci;ci=tmpList[i++];){
  226. if(ci.doc === doc && ci.url == (obj.src || obj.href)){
  227. return ci;
  228. }
  229. }
  230. }catch(e){
  231. return null;
  232. }
  233. }
  234. return function (doc, obj, fn) {
  235. var item = getItem(doc,obj);
  236. if (item) {
  237. if(item.ready){
  238. fn && fn();
  239. }else{
  240. item.funs.push(fn)
  241. }
  242. return;
  243. }
  244. tmpList.push({
  245. doc:doc,
  246. url:obj.src||obj.href,
  247. funs:[fn]
  248. });
  249. if (!doc.body) {
  250. var html = [];
  251. for(var p in obj){
  252. if(p == 'tag')continue;
  253. html.push(p + '="' + obj[p] + '"')
  254. }
  255. doc.write('<' + obj.tag + ' ' + html.join(' ') + ' ></'+obj.tag+'>');
  256. return;
  257. }
  258. if (obj.id && doc.getElementById(obj.id)) {
  259. return;
  260. }
  261. var element = doc.createElement(obj.tag);
  262. delete obj.tag;
  263. for (var p in obj) {
  264. element.setAttribute(p, obj[p]);
  265. }
  266. element.onload = element.onreadystatechange = function () {
  267. if (!this.readyState || /loaded|complete/.test(this.readyState)) {
  268. item = getItem(doc,obj);
  269. if (item.funs.length > 0) {
  270. item.ready = 1;
  271. for (var fi; fi = item.funs.pop();) {
  272. fi();
  273. }
  274. }
  275. element.onload = element.onreadystatechange = null;
  276. }
  277. };
  278. element.onerror = function(){
  279. throw Error('The load '+(obj.href||obj.src)+' fails,check the url')
  280. };
  281. doc.getElementsByTagName("head")[0].appendChild(element);
  282. }
  283. }()
  284. };
  285. utils.each(['String', 'Function', 'Array', 'Number', 'RegExp', 'Object','Boolean'], function (v) {
  286. utils['is' + v] = function (obj) {
  287. return Object.prototype.toString.apply(obj) == '[object ' + v + ']';
  288. }
  289. });
  290. var parselist = {};
  291. UE.parse = {
  292. register : function(parseName,fn){
  293. parselist[parseName] = fn;
  294. },
  295. load : function(opt){
  296. utils.each(parselist,function(v){
  297. v.call(opt,utils);
  298. })
  299. }
  300. };
  301. uParse = function(selector,opt){
  302. utils.domReady(function(){
  303. var contents;
  304. if(document.querySelectorAll){
  305. contents = document.querySelectorAll(selector)
  306. }else{
  307. if(/^#/.test(selector)){
  308. contents = [document.getElementById(selector.replace(/^#/,''))]
  309. }else if(/^\./.test(selector)){
  310. var contents = [];
  311. utils.each(document.getElementsByTagName('*'),function(node){
  312. if(node.className && new RegExp('\\b' + selector.replace(/^\./,'') + '\\b','i').test(node.className)){
  313. contents.push(node)
  314. }
  315. })
  316. }else{
  317. contents = document.getElementsByTagName(selector)
  318. }
  319. }
  320. utils.each(contents,function(v){
  321. UE.parse.load(utils.extend({root:v,selector:selector},opt))
  322. })
  323. })
  324. }
  325. })();
  326. // list.js
  327. UE.parse.register('list',function(utils){
  328. var customCss = [],
  329. customStyle = {
  330. 'cn' : 'cn-1-',
  331. 'cn1' : 'cn-2-',
  332. 'cn2' : 'cn-3-',
  333. 'num' : 'num-1-',
  334. 'num1' : 'num-2-',
  335. 'num2' : 'num-3-',
  336. 'dash' : 'dash',
  337. 'dot' : 'dot'
  338. };
  339. utils.extend(this,{
  340. liiconpath : 'http://bs.baidu.com/listicon/',
  341. listDefaultPaddingLeft : '20'
  342. });
  343. var root = this.root,
  344. ols = root.getElementsByTagName('ol'),
  345. uls = root.getElementsByTagName('ul'),
  346. selector = this.selector;
  347. if(ols.length){
  348. applyStyle.call(this,ols);
  349. }
  350. if(uls.length){
  351. applyStyle.call(this,uls);
  352. }
  353. if(ols.length || uls.length){
  354. customCss.push(selector +' .list-paddingleft-1{padding-left:0}');
  355. customCss.push(selector +' .list-paddingleft-2{padding-left:'+ this.listDefaultPaddingLeft+'px}');
  356. customCss.push(selector +' .list-paddingleft-3{padding-left:'+ this.listDefaultPaddingLeft*2+'px}');
  357. utils.cssRule('list', selector +' ol,'+selector +' ul{margin:0;padding:0;}li{clear:both;}'+customCss.join('\n'), document);
  358. }
  359. function applyStyle(nodes){
  360. var T = this;
  361. utils.each(nodes,function(list){
  362. if(list.className && /custom_/i.test(list.className)){
  363. var listStyle = list.className.match(/custom_(\w+)/)[1];
  364. if(listStyle == 'dash' || listStyle == 'dot'){
  365. utils.pushItem(customCss,selector +' li.list-' + customStyle[listStyle] + '{background-image:url(' + T.liiconpath +customStyle[listStyle]+'.gif)}');
  366. utils.pushItem(customCss,selector +' ul.custom_'+listStyle+'{list-style:none;} '+ selector +' ul.custom_'+listStyle+' li{background-position:0 3px;background-repeat:no-repeat}');
  367. }else{
  368. var index = 1;
  369. utils.each(list.childNodes,function(li){
  370. if(li.tagName == 'LI'){
  371. utils.pushItem(customCss,selector + ' li.list-' + customStyle[listStyle] + index + '{background-image:url(' + T.liiconpath + 'list-'+customStyle[listStyle] +index + '.gif)}');
  372. index++;
  373. }
  374. });
  375. utils.pushItem(customCss,selector + ' ol.custom_'+listStyle+'{list-style:none;}'+selector+' ol.custom_'+listStyle+' li{background-position:0 3px;background-repeat:no-repeat}');
  376. }
  377. switch(listStyle){
  378. case 'cn':
  379. utils.pushItem(customCss,selector + ' li.list-'+listStyle+'-paddingleft-1{padding-left:25px}');
  380. utils.pushItem(customCss,selector + ' li.list-'+listStyle+'-paddingleft-2{padding-left:40px}');
  381. utils.pushItem(customCss,selector + ' li.list-'+listStyle+'-paddingleft-3{padding-left:55px}');
  382. break;
  383. case 'cn1':
  384. utils.pushItem(customCss,selector + ' li.list-'+listStyle+'-paddingleft-1{padding-left:30px}');
  385. utils.pushItem(customCss,selector + ' li.list-'+listStyle+'-paddingleft-2{padding-left:40px}');
  386. utils.pushItem(customCss,selector + ' li.list-'+listStyle+'-paddingleft-3{padding-left:55px}');
  387. break;
  388. case 'cn2':
  389. utils.pushItem(customCss,selector + ' li.list-'+listStyle+'-paddingleft-1{padding-left:40px}');
  390. utils.pushItem(customCss,selector + ' li.list-'+listStyle+'-paddingleft-2{padding-left:55px}');
  391. utils.pushItem(customCss,selector + ' li.list-'+listStyle+'-paddingleft-3{padding-left:68px}');
  392. break;
  393. case 'num':
  394. case 'num1':
  395. utils.pushItem(customCss,selector + ' li.list-'+listStyle+'-paddingleft-1{padding-left:25px}');
  396. break;
  397. case 'num2':
  398. utils.pushItem(customCss,selector + ' li.list-'+listStyle+'-paddingleft-1{padding-left:35px}');
  399. utils.pushItem(customCss,selector + ' li.list-'+listStyle+'-paddingleft-2{padding-left:40px}');
  400. break;
  401. case 'dash':
  402. utils.pushItem(customCss,selector + ' li.list-'+listStyle+'-paddingleft{padding-left:35px}');
  403. break;
  404. case 'dot':
  405. utils.pushItem(customCss,selector + ' li.list-'+listStyle+'-paddingleft{padding-left:20px}');
  406. }
  407. }
  408. });
  409. }
  410. });
  411. })();