table.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. /**
  2. * Created by JetBrains PhpStorm.
  3. * User: taoqili
  4. * Date: 12-2-23
  5. * Time: 上午11:32
  6. * To change this template use File | Settings | File Templates.
  7. */
  8. var init = function(){
  9. addColorPickListener();
  10. addPxChangeListener();
  11. addFloatListener();
  12. addBorderTypeChangeListener();
  13. };
  14. function addBorderTypeChangeListener(){
  15. domUtils.on($G("borderType"),"change",createTable);
  16. }
  17. function addFloatListener(){
  18. domUtils.on($G("align"),"change",function(){
  19. setTablePosition(this.value);
  20. })
  21. }
  22. /**
  23. * 根据传入的value值变更table的位置
  24. * @param value
  25. */
  26. function setTablePosition(value){
  27. var table = $G("preview").children[0],
  28. margin = (table.parentNode.offsetWidth - table.offsetWidth)/2;
  29. if(value=="center"){
  30. table.style.marginLeft = margin +"px";
  31. }else if(value=="right"){
  32. table.style.marginLeft = 2*margin +"px";
  33. }else{
  34. table.style.marginLeft = "5px";
  35. }
  36. }
  37. /**
  38. * 绑定border、spaceing等更改事件
  39. */
  40. function addPxChangeListener(){
  41. var ids = ["border","cellPadding","cellSpacing"];
  42. for(var i=0,ci;ci=$G(ids[i++]);){
  43. domUtils.on(ci,"keyup",function(){
  44. $G("message").style.display="none";
  45. switch(this.id){
  46. case "border":
  47. $G("border").value = filter(this.value,"border");
  48. break;
  49. case "cellPadding":
  50. $G("cellPadding").value = filter(this.value,"cellPadding");
  51. break;
  52. case "cellSpacing":
  53. $G("cellSpacing").value = filter(this.value,"cellSpacing");
  54. break;
  55. default:
  56. }
  57. createTable();
  58. //setTablePosition($G("align").value);
  59. });
  60. }
  61. }
  62. function isNum(str){
  63. return /^(0|[1-9][0-9]*)$/.test( str );
  64. }
  65. /**
  66. * 依据属性框中的属性值创建table对象
  67. */
  68. function createTable(){
  69. var border=$G("border").value || 1,
  70. borderColor=$G("borderColor").value || "#000000",
  71. cellPadding=$G("cellPadding").value || 0,
  72. cellSpacing=$G("cellSpacing").value || 0,
  73. bgColor=$G("bgColor").value || "#FFFFFF",
  74. align=$G("align").value || "",
  75. borderType=$G("borderType").value || 0;
  76. border = setMax(border,5);
  77. cellPadding = setMax(cellPadding,5);
  78. cellSpacing = setMax(cellSpacing,5);
  79. var html = ["<table "];
  80. if(cellSpacing>0){
  81. html.push(' style="border-collapse:separate;" ')
  82. }else{
  83. html.push(' style="border-collapse:collapse;" ')
  84. }
  85. cellSpacing>0 && html.push(' cellSpacing="' + cellSpacing + '" ');
  86. html.push(' border="' + (border||1) +'" borderColor="' + (borderColor||'#000000') +'"');
  87. bgColor && html.push(' bgColor="' + bgColor + '"');
  88. html.push(' ><tr><td colspan="3"><var id="lang_forPreview">'+lang.static.lang_forPreview+'</var></td></tr><tr><td></td><td></td><td></td></tr><tr><td></td><td></td><td></td></tr></table>');
  89. var preview = $G("preview");
  90. preview.innerHTML = html.join("");
  91. //如果针对每个单元格
  92. var table = preview.firstChild;
  93. if(borderType=="1"){
  94. for(var i =0,td,tds = domUtils.getElementsByTagName(table,"td");td = tds[i++];){
  95. td.style.border = border + "px solid " + borderColor;
  96. }
  97. }
  98. for(var i =0,td,tds = domUtils.getElementsByTagName(table,"td");td = tds[i++];){
  99. td.style.padding = cellPadding + "px";
  100. }
  101. setTablePosition(align.toLowerCase());
  102. }
  103. function setMax(value,max){
  104. return value>max? max:value;
  105. }
  106. function filter(value,property){
  107. var maxPreviewValue = 5,
  108. maxValue = 10;
  109. if(!isNum(value) && value!=""){
  110. $G(property).value = "";
  111. $G("message").style.display ="";
  112. $G("messageContent").innerHTML= lang.errorNum;
  113. return property=="border"?1:0;
  114. }
  115. if(value > maxPreviewValue){
  116. $G("message").style.display ="";
  117. $G("messageContent").innerHTML= lang.overflowPreviewMsg.replace("{#value}",maxPreviewValue);
  118. if(value>maxValue){
  119. $G("messageContent").innerHTML = lang.overflowMsg.replace("{#value}",maxValue);
  120. $G(property).value = maxValue;
  121. return maxValue;
  122. }
  123. }
  124. return value;
  125. }
  126. /**
  127. * 绑定取色器监听事件
  128. */
  129. function addColorPickListener(){
  130. var colorPicker = getColorPicker(),
  131. ids = ["bgColor","borderColor"];
  132. for(var i=0,ci;ci = $G(ids[i++]); ){
  133. domUtils.on(ci,"click",function(){
  134. var me = this;
  135. showColorPicker(colorPicker,me);
  136. colorPicker.content.onpickcolor = function(t, color){
  137. me.value = color.toUpperCase();
  138. colorPicker.hide();
  139. createTable();
  140. };
  141. colorPicker.content.onpicknocolor = function(){
  142. me.value = '';
  143. colorPicker.hide();
  144. createTable();
  145. };
  146. });
  147. domUtils.on(ci,"keyup",function(){
  148. colorPicker.hide();
  149. createTable();
  150. });
  151. }
  152. domUtils.on(document, 'mousedown', function (){
  153. UE.ui.Popup.postHide(this);
  154. });
  155. }
  156. /**
  157. * 实例化一个colorpicker对象
  158. */
  159. function getColorPicker(){
  160. return new UE.ui.Popup({
  161. editor:editor,
  162. content: new UE.ui.ColorPicker({
  163. noColorText: lang.noColor,
  164. editor:editor
  165. })
  166. });
  167. }
  168. /**
  169. * 在anchorObj上显示colorpicker
  170. * @param anchorObj
  171. */
  172. function showColorPicker(colorPicker,anchorObj){
  173. colorPicker.showAnchor(anchorObj);
  174. }