background.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  1. var me = editor,
  2. doc = me.document,
  3. bodyStyle,
  4. cp = $G( "colorPicker" ),
  5. bkbodyStyle = "",
  6. bkcolor = "";
  7. var popup = new UE.ui.Popup( {
  8. content:new UE.ui.ColorPicker( {
  9. noColorText:me.getLang( "clearColor" ),
  10. editor:me,
  11. onpickcolor:function ( t, color ) {
  12. domUtils.setStyle( cp, "background-color", color );
  13. bkcolor = color;
  14. UE.ui.Popup.postHide();
  15. },
  16. onpicknocolor:function ( t, color ) {
  17. domUtils.setStyle( cp, "background-color", "transparent" );
  18. bkcolor = "";
  19. UE.ui.Popup.postHide();
  20. }
  21. } ),
  22. editor:me,
  23. onhide:function(){
  24. setBody();
  25. }
  26. } );
  27. domUtils.on( cp, "click", function () {
  28. popup.showAnchor( this );
  29. } );
  30. domUtils.on( document, 'mousedown', function ( evt ) {
  31. var el = evt.target || evt.srcElement;
  32. UE.ui.Popup.postHide( el );
  33. } );
  34. domUtils.on( window, 'scroll', function () {
  35. UE.ui.Popup.postHide();
  36. } );
  37. //获得head
  38. var getHead = function(){
  39. return domUtils.getElementsByTagName($G("tabhead"),"span");
  40. };
  41. //给head绑定事件
  42. var bindClick = function(){
  43. var heads = getHead();
  44. for(var i=0,head;head=heads[i++];){
  45. head.onclick = function(){
  46. var bodyid = this.getAttribute("tabsrc");
  47. toggleHead(this);
  48. toggleBody(bodyid);
  49. if(bodyid == "imgManager"){
  50. ajax.request( editor.options.imageManagerUrl, {
  51. timeout:100000,
  52. action:"get",
  53. onsuccess:function ( xhr ) {
  54. var tmp = utils.trim(xhr.responseText),
  55. imageUrls = !tmp ? [] : tmp.split( "ue_separate_ue" ),
  56. length = imageUrls.length,
  57. imgList = $G( "imageList" );
  58. imgList.innerHTML = !length ? "  "+lang.noUploadImage : "";
  59. for ( var k = 0, ci; ci = imageUrls[k++]; ) {
  60. var img = document.createElement( "img" );
  61. var div = document.createElement( "div" );
  62. div.appendChild( img );
  63. div.style.display = "none";
  64. imgList.appendChild( div );
  65. img.onclick = function () {
  66. var nodes = imgList.childNodes;
  67. for(var i=0,node;node=nodes[i++];){
  68. node.firstChild.removeAttribute( "selected" );
  69. node.firstChild.style.cssText = "filter:alpha(Opacity=100);-moz-opacity:1;opacity: 1;border: 2px solid #fff";
  70. }
  71. changeSelected( this );
  72. };
  73. img.onload = function () {
  74. this.parentNode.style.display = "";
  75. var w = this.width, h = this.height;
  76. scale( this, 95, 120, 80 );
  77. this.title = lang.toggleSelect + w + "X" + h;
  78. };
  79. img.setAttribute( k < 35 ? "src" : "lazy_src", editor.options.imageManagerPath + ci.replace(/\s+|\s+/ig,"") );
  80. img.setAttribute( "data_ue_src", editor.options.imageManagerPath + ci.replace(/\s+|\s+/ig,"") );
  81. }
  82. },
  83. onerror:function () {
  84. $G( "imageList" ).innerHTML = lang.imageLoadError;
  85. }
  86. } );
  87. }else{
  88. var radios = document.getElementsByName("t");
  89. for(var i=0,r;r=radios[i++];){
  90. if(r.checked&&r.value!="none"){
  91. $G("repeatType").style.display="";
  92. }
  93. }
  94. }
  95. }
  96. }
  97. };
  98. /**
  99. * 改变o的选中状态
  100. * @param o
  101. */
  102. function changeSelected( o ) {
  103. if ( o.getAttribute( "selected" ) ) {
  104. o.removeAttribute( "selected" );
  105. o.style.cssText = "filter:alpha(Opacity=100);-moz-opacity:1;opacity: 1;border: 2px solid #fff";
  106. } else {
  107. o.setAttribute( "selected", "true" );
  108. o.style.cssText = "filter:alpha(Opacity=50);-moz-opacity:0.5;opacity: 0.5;border:2px solid blue;";
  109. }
  110. $G("url").value = o.getAttribute("src")
  111. }
  112. /**
  113. * 图片缩放
  114. * @param img
  115. * @param max
  116. */
  117. function scale( img, max, oWidth, oHeight ) {
  118. var width = 0, height = 0, percent, ow = img.width || oWidth, oh = img.height || oHeight;
  119. if ( ow > max || oh > max ) {
  120. if ( ow >= oh ) {
  121. if ( width = ow - max ) {
  122. percent = (width / ow).toFixed( 2 );
  123. img.height = oh - oh * percent;
  124. img.width = max;
  125. }
  126. } else {
  127. if ( height = oh - max ) {
  128. percent = (height / oh).toFixed( 2 );
  129. img.width = ow - ow * percent;
  130. img.height = max;
  131. }
  132. }
  133. }
  134. }
  135. //切换body
  136. var toggleBody = function(id){
  137. var bodys = ["normal","imgManager"];
  138. for(var i=0,body;body=bodys[i++];){
  139. $G(body).style.zIndex = body == id?200:1;
  140. }
  141. };
  142. //切换head
  143. var toggleHead = function(obj){
  144. var heads = getHead();
  145. for(var i=0,head;head=heads[i++];){
  146. domUtils.removeClasses(head,["focus"]);
  147. $G("repeatType").style.display = "none";
  148. }
  149. domUtils.addClass(obj,"focus");
  150. };
  151. //获得当前选中的tab
  152. var getCheckedTab = function(){
  153. var heads = getHead();
  154. for(var i=0,head;head=heads[i++];){
  155. if(domUtils.hasClass(head,"focus")){
  156. return head;
  157. }
  158. }
  159. };
  160. var init = function(){
  161. bindClick();
  162. getHead()[0].click();
  163. $G("alignment").style.display = "none";
  164. $G("custom").style.display = "none";
  165. //初始化颜色
  166. domUtils.setStyle(cp,"background-color",domUtils.getComputedStyle(doc.body,"background-color"));
  167. var color = domUtils.getComputedStyle(doc.body,"background-color");
  168. if((color && color!="#ffffff" && color!="transparent")||domUtils.getComputedStyle(doc.body,"background-image")!="none"){
  169. setTimeout(function(){
  170. document.getElementsByName("t")[1].click();
  171. },200);
  172. }
  173. initImgUrl();
  174. initSelfPos();
  175. initAlign();
  176. };
  177. //初始化自定义位置
  178. function initSelfPos(){
  179. var x,y;
  180. if(browser.ie){
  181. x = domUtils.getComputedStyle(doc.body,"background-position-x").replace(/50%|%|px|center/ig,"");
  182. y = domUtils.getComputedStyle(doc.body,"background-position-y").replace(/50%|%|px|center/ig,"");
  183. }else{
  184. var arr = domUtils.getComputedStyle(doc.body,"background-position").match(/\s?(\d*)px/ig);
  185. if(arr&&arr.length){
  186. x = arr[0].replace("px","");
  187. y = arr[1].replace("px","");
  188. }
  189. }
  190. $G("x").value = x || 0;
  191. $G("y").value = y || 0;
  192. }
  193. //初始化图片地址
  194. function initImgUrl(){
  195. var su = domUtils.getComputedStyle(doc.body,"background-image"),
  196. url="";
  197. if(su.indexOf(me.options.imagePath)>0){
  198. url = su.match(/url\("?(.*[^\)"])"?/i);
  199. if(url && url.length){
  200. url = url[1].substring(url[1].indexOf(me.options.imagePath),url[1].length);
  201. }
  202. }else{
  203. url = su!="none" ? su.replace(/url\("?|"?\)/ig,""):"";
  204. }
  205. $G("url").value = url;
  206. }
  207. //初始化定位
  208. function initAlign(){
  209. var align = domUtils.getComputedStyle(doc.body,"background-repeat"),
  210. alignType = $G("repeatType");
  211. if(align == "no-repeat"){
  212. var pos = domUtils.getComputedStyle(doc.body,browser.ie?"background-position-x":"background-position");
  213. alignType.value = pos&&pos.match(/\s?(\d*)px/ig) ? "self":"center";
  214. if(pos == "center"){
  215. alignType.value = "center";
  216. }
  217. $G("custom").style.display = alignType.value=="self" ? "" : "none";
  218. }else{
  219. alignType.value = align;
  220. }
  221. }
  222. init();
  223. //样式表工具
  224. function Stylesheet(ss){
  225. if(typeof ss == "number")
  226. ss = doc.styleSheets[ss];
  227. this.ss = ss;
  228. }
  229. Stylesheet.prototype.getRules = function(){
  230. return this.ss.cssRules?this.ss.cssRules:this.ss.rules;
  231. };
  232. Stylesheet.prototype.getRule = function(s){
  233. var rules = this.getRules();
  234. if(!rules)return null;
  235. if(typeof s == 'number')return rules[s];
  236. s = s.toLowerCase();
  237. for(var i=rules.length-1;i>=0;i--){
  238. if(rules[i].selectorText.toLowerCase() == s)return rules[i];
  239. }
  240. return null;
  241. };
  242. Stylesheet.prototype.getStyleText = function(s){
  243. var rule = this.getRule(s);
  244. if(rule&&rule.style&&rule.style.cssText) return rule.style.cssText;
  245. else return "";
  246. };
  247. Stylesheet.prototype.insertRule = function(selector,styles,n){
  248. if(!n){
  249. var rules = this.getRules();
  250. rules.length&&this.deleteRule(rules.length-1);
  251. }
  252. if(this.ss.insertRule)
  253. this.ss.insertRule(selector+"{"+styles+"}",0);
  254. else if(this.ss.addRule)
  255. this.ss.addRule(selector,styles,0);
  256. };
  257. Stylesheet.prototype.deleteRule = function(s){
  258. if(!s){
  259. var rules = this.getRules();
  260. s = rules.length-1;
  261. }
  262. if(typeof s!="number"){
  263. s = s.toLowerCase();
  264. var rules = this.getRules();
  265. for(var i=rules.length-1;i>=0;i--){
  266. if(rules[i].selectorText.toLowerCase()==s){
  267. s = i;
  268. break;
  269. }
  270. }
  271. if(i==-1)return;
  272. }
  273. if(this.ss.deleteRule)this.ss.deleteRule(s);
  274. else if(this.ss.removeRule)this.ss.removeRule(s);
  275. };
  276. //获得选中的类型
  277. function getCheckIpt(){
  278. var ipts = document.getElementsByName("t");
  279. for(var i=0,ipt;ipt=ipts[i++];){
  280. if(ipt.checked){
  281. return ipt.value;
  282. }
  283. }
  284. }
  285. var net = function(obj){
  286. var align = $G("alignment"),
  287. url = $G("url"),
  288. custom = $G("custom");
  289. if(obj.value == "none"){
  290. align.style.display = "none";
  291. custom.style.display = "none";
  292. if ( browser.ie ) {
  293. url.onpropertychange = null;
  294. }else{
  295. url.removeEventListener("input",setBody);
  296. }
  297. }else{
  298. bindSelfPos();
  299. $G("repeatType").style.display="";
  300. align.style.display = "";
  301. if ( browser.ie ) {
  302. url.onpropertychange = setBody;
  303. } else {
  304. url.addEventListener( "input", setBody, false );
  305. }
  306. }
  307. setBody();
  308. };
  309. //给自定义位置绑定事件
  310. var bindSelfPos = function(){
  311. var x = $G("x"),
  312. y = $G("y");
  313. domUtils.on(x,["propertychange","input","keydown"],function(evt){
  314. bindkeydown(evt,this);
  315. });
  316. domUtils.on(y,["propertychange","input","keydown"],function(evt){
  317. bindkeydown(evt,this);
  318. });
  319. function bindkeydown(evt,obj){
  320. evt = evt || event;
  321. if(evt.keyCode==38||evt.keyCode==40){
  322. obj.value = evt.keyCode == 38 ? parseInt(obj.value)+1 : parseInt(obj.value)-1;
  323. if(obj.value<0){
  324. obj.value = 0;
  325. }
  326. }else{
  327. if(evt.keyCode<48&&evt.keyCode>57){
  328. domUtils.preventDefault(evt);
  329. }
  330. }
  331. setBody();
  332. }
  333. };
  334. var showAlign = function(){
  335. $G("alignment").style.display = "";
  336. };
  337. var selectAlign = function(obj){
  338. $G("custom").style.display = obj.value == "self" ? "" : "none";
  339. setBody();
  340. };
  341. //给body增加样式和背景图片
  342. var setBody=function(){
  343. var color = domUtils.getStyle(cp,"background-color"),
  344. bgimg = $G("url").value,
  345. align = $G("repeatType").value,
  346. alignObj = {
  347. "background-repeat":"no-repeat",
  348. "background-position":"center center"
  349. },
  350. outstr = [];
  351. if(color)
  352. alignObj["background-color"] = color;
  353. if(bgimg)
  354. alignObj["background-image"] = 'url("'+bgimg+'")';
  355. switch(align){
  356. case "repeat-x":
  357. alignObj["background-repeat"] = "repeat-x;";
  358. break;
  359. case "repeat-y":
  360. alignObj["background-repeat"] = "repeat-y;";
  361. break;
  362. case "repeat":
  363. alignObj["background-repeat"] = "repeat;";
  364. break;
  365. case "self":
  366. alignObj["background-position"] = $G("x").value+"px "+$G("y").value+"px";
  367. break;
  368. }
  369. for ( var name in alignObj ) {
  370. if ( alignObj.hasOwnProperty( name ) ) {
  371. outstr.push(name+":"+alignObj[name]);
  372. }
  373. }
  374. if(getCheckIpt()!="none"){
  375. setStyleSheet(outstr.join(";"));
  376. }else{
  377. setStyleSheet(" ");
  378. }
  379. };
  380. function getStyleSheet(){
  381. bodyStyle = doc.getElementsByTagName("head")[0].lastChild;
  382. if(/style/ig.test(bodyStyle.tagName)){
  383. var len = doc.styleSheets.length-1;
  384. var ss = new Stylesheet(len);
  385. bkbodyStyle = ss.getStyleText(0);
  386. }else{
  387. bodyStyle = null;
  388. }
  389. }
  390. var setStyleSheet = function(stylestr){
  391. if(bodyStyle){
  392. var len = doc.styleSheets.length-1;
  393. var ss = new Stylesheet(len);
  394. ss.insertRule("body",stylestr);
  395. }else{
  396. if(browser.ie){
  397. bodyStyle = doc.createStyleSheet();
  398. bodyStyle.cssText = stylestr;
  399. }else{
  400. bodyStyle = domUtils.creElm(doc,"style",{"type":"text/css"});
  401. bodyStyle.innerHTML = stylestr;
  402. doc.head.appendChild(bodyStyle);
  403. }
  404. }
  405. bkbodyStyle = bkbodyStyle||stylestr;
  406. };
  407. dialog.onok = function(){
  408. setBody();
  409. };
  410. dialog.oncancel = function(){
  411. var len = doc.styleSheets.length-1;
  412. var ss = new Stylesheet(len);
  413. ss.insertRule("body",bkbodyStyle||" ");
  414. };
  415. getStyleSheet();