table.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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>这</td><td>是</td><td>用</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. }
  102. function setMax(value,max){
  103. return value>max? max:value;
  104. }
  105. function filter(value,property,des){
  106. var maxPreviewValue = 5,
  107. maxValue = 10;
  108. if(!isNum(value) && value!=""){
  109. $G(property).value = "";
  110. $G("message").style.display ="";
  111. $G("messageContent").innerHTML= "请输入正确的数值!";
  112. return property=="border"?1:0;
  113. }
  114. if(value > maxPreviewValue){
  115. $G("message").style.display ="";
  116. $G("messageContent").innerHTML= des+"超过" + maxPreviewValue+"px时不再提供实时预览!";
  117. if(value>maxValue){
  118. $G("messageContent").innerHTML = des+"最大值不能超过"+maxValue+"px!";
  119. $G(property).value = maxValue;
  120. return maxValue;
  121. }
  122. }
  123. return value;
  124. }
  125. /**
  126. * 绑定取色器监听事件
  127. */
  128. function addColorPickListener(){
  129. var colorPicker = getColorPicker(),
  130. ids = ["bgColor","borderColor"];
  131. for(var i=0,ci;ci = $G(ids[i++]); ){
  132. domUtils.on(ci,"click",function(){
  133. var me = this;
  134. showColorPicker(colorPicker,me);
  135. colorPicker.content.onpickcolor = function(t, color){
  136. me.value = color.toUpperCase();
  137. colorPicker.hide();
  138. createTable();
  139. };
  140. colorPicker.content.onpicknocolor = function(){
  141. me.value = '';
  142. colorPicker.hide();
  143. createTable();
  144. };
  145. });
  146. domUtils.on(ci,"keyup",function(){
  147. colorPicker.hide();
  148. createTable();
  149. });
  150. }
  151. domUtils.on(document, 'mousedown', function (){
  152. UE.ui.Popup.postHide(this);
  153. });
  154. }
  155. /**
  156. * 实例化一个colorpicker对象
  157. */
  158. function getColorPicker(){
  159. return new UE.ui.Popup({
  160. content: new UE.ui.ColorPicker({
  161. noColorText: '清除颜色'
  162. })
  163. });
  164. }
  165. /**
  166. * 在anchorObj上显示colorpicker
  167. * @param anchorObj
  168. */
  169. function showColorPicker(colorPicker,anchorObj){
  170. colorPicker.showAnchor(anchorObj);
  171. }