ScoreCardTable.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /**
  2. * Created by Jacky.gao on 2016/9/18.
  3. */
  4. import AttributeRow from './AttributeRow.js';
  5. import CustomCol from './CustomCol.js';
  6. import PropertyConfig from './PropertyConfig.js';
  7. import TableAction from './TableAction.js';
  8. import AttributeCol from './AttributeCol.js';
  9. import ConditionCol from './ConditionCol.js';
  10. import ScoreCol from './ScoreCol.js';
  11. export default class ScoreCardTable{
  12. constructor(config){
  13. var remarkContainer=$("<div></div>");
  14. config.container.append(remarkContainer);
  15. this.remark=new Remark(remarkContainer);
  16. this.weightSupport=false;
  17. const configContainer=$(`<div style="margin: 5px;"></div>`);
  18. this.weightSupportContainer=$(`<span style="padding: 8px;margin-right:5px;border:solid 1px #9E9E9E">权重:</span>`);
  19. this.initWeightSupportOptions();
  20. configContainer.append(this.weightSupportContainer);
  21. const propertyContainer=$(`<span></span>`);
  22. config.container.append(configContainer);
  23. configContainer.append(propertyContainer);
  24. this.propertyConfig=new PropertyConfig(propertyContainer);
  25. this.attributeRows=[];
  26. this.customCols=[];
  27. this.config=config;
  28. this.table=$(`<table class="table table-bordered" style="width: auto;max-width: none;font-size: 12px"></table>`);
  29. config.container.append(this.table);
  30. const actionContainer=$(`<div style="border: solid 1px #ddd;border-radius:5px;padding:10px"></div>`);
  31. config.container.append(actionContainer);
  32. this.tableAction=new TableAction(actionContainer);
  33. }
  34. initWeightSupportOptions(){
  35. const container=$(`<span></span>`);
  36. this.weightSupportContainer.append(container);
  37. const supportLabel=$(`<label class="checkbox-inline" style="padding-left: 8px;"></label>`);
  38. container.append(supportLabel);
  39. this.weightSupportOption=$(`<input type="radio" name="weightSupport">支持</input>`);
  40. supportLabel.append(this.weightSupportOption);
  41. const nonsupportLabel=$(`<label class="checkbox-inline" style="padding-left: 8px;"></label>`);
  42. container.append(nonsupportLabel);
  43. this.weightNonsupportOption=$(`<input type="radio" name="weightSupport">不支持</input>`);
  44. nonsupportLabel.append(this.weightNonsupportOption);
  45. const _this=this;
  46. this.weightSupportOption.change(function () {
  47. if($(this).val()==='on'){
  48. for(let row of _this.attributeRows){
  49. row.attributeCell.showWeight();
  50. }
  51. _this.weightSupport=true;
  52. }
  53. });
  54. this.weightNonsupportOption.change(function () {
  55. if($(this).val()==='on'){
  56. for(let row of _this.attributeRows){
  57. row.attributeCell.hideWeight();
  58. }
  59. _this.weightSupport=false;
  60. }
  61. });
  62. }
  63. init(data){
  64. this.data=data || {};
  65. if(this.data.weightSupport){
  66. this.weightSupport=true;
  67. this.weightSupportOption.prop("checked",true);
  68. }else{
  69. this.weightSupport=false;
  70. this.weightNonsupportOption.prop("checked",true);
  71. }
  72. this.remark.setData(data["remark"]);
  73. this.propertyConfig.initData(data);
  74. this.tableAction.initData(data);
  75. const header=$(`<thead></thead>`);
  76. this.table.append(header);
  77. this.headerRow=$(`<tr></tr>`);
  78. header.append(this.headerRow);
  79. this.body=$(`<tbody></tbody>`);
  80. this.table.append(this.body);
  81. this.initAttributeCol(data);
  82. this.initConditionCol(data);
  83. this.initScoreCol(data);
  84. const customCols=data.customCols || [];
  85. for(let colData of customCols){
  86. this.addCustomCol(colData);
  87. }
  88. const rows=data.rows || [];
  89. for(let rowData of rows){
  90. this.addAttributeRow(rowData);
  91. }
  92. }
  93. getCell(row,col){
  94. if(!this.data){
  95. return null;
  96. }
  97. const cells=this.data.cells;
  98. for(let cell of cells){
  99. if(cell.row===row && cell.col===col){
  100. return cell;
  101. }
  102. }
  103. throw "Cell ["+row+","+col+"] not exist.";
  104. }
  105. initAttributeCol(data){
  106. const width=data.attributeColWidth || '200';
  107. const name=data.attributeColName || '属性';
  108. const variableCategory=data.attributeColVariableCategory;
  109. this.attributeCol=new AttributeCol(this,name,width,variableCategory);
  110. }
  111. initConditionCol(data){
  112. const width=data.conditionColWidth || '220';
  113. const name=data.conditionColName || '条件';
  114. this.conditionCol=new ConditionCol(this,name,width);
  115. }
  116. initScoreCol(data){
  117. const width=data.scoreColWidth || '180';
  118. const name=data.scoreColName || '分值';
  119. this.scoreCol=new ScoreCol(this,name,width);
  120. }
  121. addAttributeRow(rowData){
  122. const attributeRow=new AttributeRow(this,rowData);
  123. this.attributeRows.push(attributeRow);
  124. this.body.append(attributeRow.tr);
  125. if(rowData){
  126. attributeRow.initConditionRows(rowData);
  127. }
  128. window._setDirty();
  129. }
  130. addCustomCol(colData){
  131. if(colData){
  132. const col=new CustomCol(this,colData.name,colData.width);
  133. this.customCols.push(col);
  134. window._setDirty();
  135. }else{
  136. const _this=this;
  137. bootbox.prompt("请输入列名",function (name) {
  138. if(!name || name.length<1){
  139. return;
  140. }
  141. const col=new CustomCol(_this,name);
  142. _this.customCols.push(col);
  143. window._setDirty();
  144. });
  145. }
  146. }
  147. toXml(){
  148. if(this.attributeRows.length===0){
  149. throw "属性至少要有一行";
  150. }
  151. var xml="<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
  152. xml+="<scorecard weight-support=\""+this.weightSupport+"\" "+this.propertyConfig.toXml()+this.attributeCol.toXml()+this.conditionCol.toXml()+this.scoreCol.toXml()+this.tableAction.toXml()+">";
  153. xml+=this.remark.toXml();
  154. $.each(window.parameterLibraries,function(index,item){
  155. xml+="<import-parameter-library path=\""+item+"\"/>";
  156. });
  157. $.each(window.variableLibraries,function(index,item){
  158. xml+="<import-variable-library path=\""+item+"\"/>";
  159. });
  160. $.each(window.constantLibraries,function(index,item){
  161. xml+="<import-constant-library path=\""+item+"\"/>";
  162. });
  163. $.each(window.actionLibraries,function(index,item){
  164. xml+="<import-action-library path=\""+item+"\"/>";
  165. });
  166. for(let row of this.attributeRows){
  167. xml+=row.cellsToXml();
  168. }
  169. for(let row of this.attributeRows){
  170. xml+=row.toXml();
  171. }
  172. for(let col of this.customCols){
  173. xml+=col.toXml();
  174. }
  175. xml+="</scorecard>";
  176. return xml;
  177. }
  178. };