sheet_common.js 47 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082
  1. /**
  2. * Created by Tony on 2017/4/28.
  3. */
  4. var sheetCommonObj = {
  5. // CSL.2017.06.05
  6. // createSpread、initSheet 在一个Spread多个Sheet分别调用时的情况下使用。
  7. createSpread: function(container, SheetCount){
  8. var me = this;
  9. var spreadBook = new GC.Spread.Sheets.Workbook(container, { sheetCount: SheetCount });
  10. spreadBook.options.tabStripVisible = false;
  11. spreadBook.options.showHorizontalScrollbar = true;
  12. spreadBook.options.showVerticalScrollbar = true;
  13. spreadBook.options.allowCopyPasteExcelStyle = false;
  14. spreadBook.options.allowUserDragDrop = true;
  15. spreadBook.options.allowContextMenu = false;
  16. spreadBook.options.allowUserEditFormula = false;
  17. spreadBook.options.showDragFillSmartTag = false;
  18. return spreadBook;
  19. },
  20. initSheet: function(sheet, setting, rowCount) {
  21. var me = this;
  22. var spreadNS = GC.Spread.Sheets;
  23. sheet.suspendPaint();
  24. sheet.suspendEvent();
  25. if(setting.frozenCols) sheet.frozenColumnCount(setting.frozenCols);//冻结列
  26. sheet.setRowCount(1, spreadNS.SheetArea.colHeader);
  27. sheet.setColumnCount(setting.header.length, spreadNS.SheetArea.viewport);
  28. if (setting && setting.view && setting.view.colHeaderHeight) {
  29. sheet.setRowHeight(0, setting.view.colHeaderHeight, spreadNS.SheetArea.colHeader);
  30. };
  31. if (setting && setting.view && setting.view.rowHeaderWidth) {
  32. sheet.setColumnWidth(0, setting.view.rowHeaderWidth, spreadNS.SheetArea.rowHeader);
  33. };
  34. if (setting.emptyRowHeader) {
  35. sheet.setColumnWidth(0, 1, GC.Spread.Sheets.SheetArea.rowHeader);
  36. }
  37. sheet.options.colHeaderAutoTextIndex = 1;
  38. sheet.options.colHeaderAutoText = spreadNS.HeaderAutoText.numbers;
  39. sheet.options.clipBoardOptions = GC.Spread.Sheets.ClipboardPasteOptions.values;
  40. sheet.options.protectionOptions = {
  41. allowResizeColumns: true
  42. };
  43. sheet.showRowOutline(false);
  44. sheet.options.allowCellOverflow = false;
  45. me.buildHeader(sheet, setting);
  46. if (rowCount > 0)
  47. sheet.setRowCount(rowCount);
  48. else
  49. sheet.setRowCount(1);
  50. sheet.resumeEvent();
  51. sheet.resumePaint();
  52. },
  53. // buildSheet 在一个Spread、一个Sheet的情况下使用。
  54. buildSheet: function(container, setting, rowCount) {
  55. var me = this;
  56. var spreadBook = me.createSpread(container, { sheetCount: 1 });
  57. var sheet = spreadBook.getSheet(0);
  58. me.initSheet(sheet, setting, rowCount);
  59. return spreadBook;
  60. },
  61. buildHeader: function(sheet, setting){
  62. var me = this, ch = GC.Spread.Sheets.SheetArea.colHeader;
  63. for (var i = 0; i < setting.header.length; i++) {
  64. sheet.setValue(0, i, setting.header[i].headerName, ch);
  65. sheet.getCell(0, i, ch).wordWrap(true);
  66. sheet.setColumnWidth(i, setting.header[i].headerWidth?setting.header[i].headerWidth:100);
  67. sheet.setColumnVisible(i,setting.header[i].visible === false ? false:true);
  68. }
  69. },
  70. cleanSheet: function(sheet, setting, rowCount) {
  71. sheet.suspendPaint();
  72. sheet.suspendEvent();
  73. sheet.clear(-1, 0, -1, setting.header.length, GC.Spread.Sheets.SheetArea.viewport, GC.Spread.Sheets.StorageType.data);
  74. if (rowCount > 0) sheet.setRowCount(rowCount);
  75. sheet.clearSelection();
  76. sheet.resumeEvent();
  77. sheet.resumePaint();
  78. },
  79. cleanData: function (sheet, setting, rowCount) {
  80. sheet.suspendPaint();
  81. sheet.suspendEvent();
  82. sheet.clear(-1, 0, -1, setting.header.length, GC.Spread.Sheets.SheetArea.viewport, GC.Spread.Sheets.StorageType.data);
  83. if (rowCount > 0) sheet.setRowCount(rowCount);
  84. sheet.resumeEvent();
  85. sheet.resumePaint();
  86. },
  87. setAreaAlign: function(area, hAlign, vAlign){
  88. if (!(hAlign) || hAlign === "left") {
  89. area.hAlign(GC.Spread.Sheets.HorizontalAlign.left);
  90. } else if (hAlign === "right") {
  91. area.hAlign(GC.Spread.Sheets.HorizontalAlign.right);
  92. } else if (hAlign === "center") {
  93. area.hAlign(GC.Spread.Sheets.HorizontalAlign.center);
  94. } else {
  95. area.hAlign(GC.Spread.Sheets.HorizontalAlign.left);
  96. }
  97. if (!(vAlign) || vAlign === "center") {
  98. area.vAlign(GC.Spread.Sheets.VerticalAlign.center);
  99. } else if (vAlign === "top") {
  100. area.vAlign(GC.Spread.Sheets.VerticalAlign.top);
  101. } else if (vAlign === "bottom") {
  102. area.vAlign(GC.Spread.Sheets.VerticalAlign.bottom);
  103. } else {
  104. area.vAlign(GC.Spread.Sheets.VerticalAlign.center);
  105. }
  106. },
  107. showData: function(sheet, setting, data,distTypeTree) {
  108. var me = this, ch = GC.Spread.Sheets.SheetArea.viewport;
  109. sheet.suspendPaint();
  110. sheet.suspendEvent();
  111. //sheet.addRows(row, 1);
  112. sheet.clear(0, 0, sheet.getRowCount(), sheet.getColumnCount(), GC.Spread.Sheets.SheetArea.viewport, GC.Spread.Sheets.StorageType.data);
  113. if(sheet.getRowCount()<data.length){
  114. data.length<30? sheet.setRowCount(30):sheet.setRowCount(data.length);
  115. }else if(sheet.getRowCount()==0){
  116. sheet.setRowCount(30);
  117. }
  118. for (var col = 0; col < setting.header.length; col++) {
  119. var hAlign = "left", vAlign = "center";
  120. if (setting.header[col].hAlign) {
  121. hAlign = setting.header[col].hAlign;
  122. } else if (setting.header[col].dataType !== "String"){
  123. hAlign = "right";
  124. }
  125. vAlign = setting.header[col].vAlign?setting.header[col].vAlign:vAlign;
  126. me.setAreaAlign(sheet.getRange(-1, col, -1, 1), hAlign, vAlign);
  127. if (setting.header[col].formatter) {
  128. sheet.setFormatter(-1, col, setting.header[col].formatter, GC.Spread.Sheets.SheetArea.viewport);
  129. }
  130. if(setting.header[col].cellType === "checkBox"||setting.header[col].cellType === "button"){//clear and reset
  131. var me = this, header = GC.Spread.Sheets.SheetArea.colHeader;
  132. sheet.deleteColumns(col,1);
  133. sheet.addColumns(col, 1);
  134. sheet.setValue(0, col, setting.header[col].headerName, header);
  135. sheet.setColumnWidth(col, setting.header[col].headerWidth?setting.header[col].headerWidth:100);
  136. }
  137. if(setting.header[col].visible!==null&&setting.header[col].visible!==undefined){
  138. sheet.setColumnVisible(col,setting.header[col].visible);
  139. }
  140. sheet.getCell(0, col, GC.Spread.Sheets.SheetArea.colHeader).wordWrap(true);
  141. }
  142. for (var row = 0; row < data.length; row++) {
  143. //var cell = sheet.getCell(row, col, GC.Spread.Sheets.SheetArea.viewport);
  144. this.showRowData(sheet,setting,row,data,distTypeTree);
  145. if(setting.getStyle && setting.getStyle(data[row])){
  146. sheet.setStyle(row, -1, setting.getStyle(data[row]));
  147. }
  148. }
  149. if(setting.emptyRowHeader){
  150. let rowCount = sheet.getRowCount();
  151. for (let row = 0; row < rowCount; row++) {
  152. sheet.setValue(row, 0, '', GC.Spread.Sheets.SheetArea.rowHeader);
  153. }
  154. }
  155. this.lockCells(sheet,setting);
  156. sheet.resumeEvent();
  157. sheet.resumePaint();
  158. //me.shieldAllCells(sheet);
  159. },
  160. showRowData:function (sheet,setting,row,data,distTypeTree=null) {
  161. let ch = GC.Spread.Sheets.SheetArea.viewport;
  162. for (var col = 0; col < setting.header.length; col++) {
  163. //var cell = sheet.getCell(row, col, GC.Spread.Sheets.SheetArea.viewport);
  164. var val = data[row][setting.header[col].dataCode];
  165. if(val&&setting.header[col].dataType === "Number"){
  166. if(setting.header[col].hasOwnProperty('tofix')){
  167. val =scMathUtil.roundToString(val,setting.header[col].tofix);
  168. }
  169. else if(setting.header[col].hasOwnProperty('decimalField')){
  170. var decimal = getDecimal(setting.header[col].decimalField);
  171. val =scMathUtil.roundToString(val,decimal);
  172. sheet.setFormatter(-1, col,getFormatter(decimal), GC.Spread.Sheets.SheetArea.viewport);
  173. }else {
  174. val =val+'';
  175. }
  176. }
  177. if(val!=null&&setting.header[col].cellType === "checkBox"){
  178. this.setCheckBoxCell(row,col,sheet,val)
  179. }
  180. if(setting.header[col].cellType === "comboBox"){
  181. this.setComboBox(row,col,sheet,setting.header[col].options,setting.header[col].editorValueType);
  182. }
  183. if(setting.header[col].cellType === "selectButton"){
  184. this.setSelectButton(row,col,sheet,setting.header[col]);
  185. }
  186. if(setting.header[col].cellType === "replaceButton"){
  187. this.setReplaceButton(row,col,sheet,setting.header[col]);
  188. }
  189. if(setting.header[col].cellType === "tipsCell"){
  190. this.setTipsCell(row,col,sheet,setting.header[col]);
  191. }
  192. if(setting.owner==='gljTree'){
  193. if(setting.header[col].cellType === "checkBox"){
  194. val==1?val:0;
  195. this.setCheckBoxCell(row,col,sheet,val);
  196. }
  197. if(setting.header[col].dataCode === 'gljType' && data[row].gljType){
  198. let distTypeVal = distTypeTree.distTypes[distTypeTree.prefix + data[row].gljType].data.fullName;
  199. val=distTypeVal;
  200. }
  201. }
  202. if(setting.header[col].getText){
  203. val = setting.getText[setting.header[col].getText](data[row],val)
  204. }
  205. sheet.setValue(row, col, val, ch);
  206. }
  207. this.setRowStyle(row,sheet,data[row].bgColour);
  208. if(setting.autoFit==true){//设置自动行高
  209. if(setting.fitRow && setting.fitRow.length > 0){//如果有设置特定的某些列才需要自动行高就按设置的来,没有设置就默认所有列
  210. for(let dataCode of setting.fitRow){
  211. let col = _.findIndex(setting.header,{dataCode:dataCode})
  212. sheet.getCell(row,col).wordWrap(true);
  213. }
  214. }else {
  215. sheet.getRange(row, -1, 1, -1, GC.Spread.Sheets.SheetArea.viewport).wordWrap(true);
  216. }
  217. sheet.autoFitRow(row);
  218. }
  219. },
  220. checkData : function(col,setting, value) {
  221. let result = true;
  222. let validator = setting.header[col].validator !== undefined ? setting.header[col].validator : null;
  223. if (validator === null) {
  224. return result;
  225. }
  226. switch (validator) {
  227. case 'number':
  228. let regular = /^\d+(\.\d+)?$/;
  229. result = regular.test(value);
  230. break;
  231. case 'boolean':
  232. let booleanValue = [true, false];
  233. result = booleanValue.indexOf(value) >= 0;
  234. break;
  235. }
  236. return result;
  237. },
  238. //todo
  239. analyzePasteData: function(setting, pastedInfo) {
  240. var rst = [], propId = pastedInfo.cellRange.col, preStrIdx = 0, itemObj = {};//propId = 0 to proId = pastedInfo.cellRange.col, update by zhong
  241. for (var i = 0; i < pastedInfo.pasteData.text.length; i++) {
  242. if (pastedInfo.pasteData.text[i] === "\n") {
  243. propId = pastedInfo.cellRange.col;//propId = 0 to proId = pastedInfo.cellRange.col, update by zhong
  244. preStrIdx = i + 1;
  245. rst.push(itemObj);
  246. if (i < pastedInfo.pasteData.text.length - 1) {
  247. itemObj = {};
  248. }
  249. } else if (pastedInfo.pasteData.text[i] === "\t" || pastedInfo.pasteData.text[i] === "\r") {
  250. itemObj[setting.header[propId].dataCode] = pastedInfo.pasteData.text.slice(preStrIdx, i);
  251. propId++;
  252. preStrIdx = i + 1;
  253. //if the last copied-cell were empty, should check whether the end of text
  254. if (i == pastedInfo.pasteData.text.length - 1) {
  255. itemObj[setting.header[propId].dataCode] = pastedInfo.pasteData.text.slice(preStrIdx);
  256. rst.push(itemObj);
  257. }
  258. } else if (i == pastedInfo.pasteData.text.length - 1) {
  259. itemObj[setting.header[propId].dataCode] = pastedInfo.pasteData.text.slice(preStrIdx);
  260. rst.push(itemObj);
  261. }
  262. }
  263. return rst;
  264. },
  265. combineRowData: function(sheet, setting, row) {
  266. var rst = {};
  267. for (var col = 0; col < setting.header.length; col++) {
  268. rst[setting.header[col].dataCode] = sheet.getValue(row, col);
  269. }
  270. return rst;
  271. },
  272. shieldAllCells: function(sheet) {
  273. sheet.options.isProtected = true;
  274. },
  275. unShieldAllCells: function(sheet) {
  276. sheet.options.isProtected = false;
  277. },
  278. lockCells: function(sheet, setting){
  279. if (setting && setting.view.lockColumns && setting.view.lockColumns.length > 0) {
  280. sheet.options.isProtected = true;
  281. sheet.getRange(-1, 0, -1, setting.header.length, GC.Spread.Sheets.SheetArea.viewport).locked(false);
  282. for (var i = 0; i < setting.view.lockColumns.length; i++) {
  283. let col = setting.view.lockColumns[i];
  284. if(_.isString(col)){//如果是dataCode 进行转换
  285. col = _.findIndex(setting.header,{dataCode:col})
  286. }
  287. sheet.getRange(-1,col, -1, 1, GC.Spread.Sheets.SheetArea.viewport).locked(true);
  288. }
  289. }
  290. },
  291. setCheckBoxCell(row,col,sheet,val){
  292. var c = new GC.Spread.Sheets.CellTypes.CheckBox();
  293. c.isThreeState(false);
  294. sheet.setCellType(row, col,c,GC.Spread.Sheets.SheetArea.viewport);
  295. sheet.getCell(row, col).value(val);
  296. sheet.getCell(row, col).hAlign(GC.Spread.Sheets.HorizontalAlign.center);
  297. },
  298. setComboBox(row,col,sheet,options,editorValueType){
  299. //let combo = new GC.Spread.Sheets.CellTypes.ComboBox();
  300. let dynamicCombo = sheetCommonObj.getDynamicCombo(true);
  301. if(options){
  302. dynamicCombo.items(options);
  303. if(editorValueType==true){
  304. dynamicCombo.editorValueType(GC.Spread.Sheets.CellTypes.EditorValueType.value);
  305. }
  306. }
  307. sheet.setCellType(row, col,dynamicCombo,GC.Spread.Sheets.SheetArea.viewport);
  308. },
  309. setRowStyle(row,sheet,bgColour) {
  310. if(bgColour){
  311. let style = new GC.Spread.Sheets.Style();
  312. style.backColor = bgColour;
  313. style.borderLeft = new GC.Spread.Sheets.LineBorder("#D4D4D4", GC.Spread.Sheets.LineStyle.thin);
  314. style.borderTop = new GC.Spread.Sheets.LineBorder("#D4D4D4", GC.Spread.Sheets.LineStyle.thin);
  315. style.borderRight = new GC.Spread.Sheets.LineBorder("#D4D4D4", GC.Spread.Sheets.LineStyle.thin);
  316. style.borderBottom = new GC.Spread.Sheets.LineBorder("#D4D4D4", GC.Spread.Sheets.LineStyle.thin);
  317. sheet.setStyle(row, -1, style);
  318. }
  319. },
  320. getCustomerCoeCellType: function (htmlGenerator,setEditorValue,updateCallback) {
  321. let me = this;
  322. function CustomerCoeCellType() {
  323. this.isEscKey=false;
  324. this.displayText='';
  325. }
  326. CustomerCoeCellType.prototype = new GC.Spread.Sheets.CellTypes.Base();
  327. CustomerCoeCellType.prototype.createEditorElement = function (context) {
  328. console.log("create editor")
  329. let element = document.createElement("div");//这里创建的,会自动销毁
  330. return element
  331. };
  332. CustomerCoeCellType.prototype.activateEditor = function (editorContext, cellStyle, cellRect, context) {
  333. if (editorContext) {
  334. $editor = $(editorContext);
  335. $editor.css("position", "fixed");
  336. $editor.css("background", "white");
  337. //$editor.css("width", cellRect.width); 2018-11-15 改成固定列宽
  338. $editor.css("width", 160);
  339. $editor.attr("gcUIElement", "gcEditingInput");
  340. if(htmlGenerator) htmlGenerator(context,cellRect,$editor);
  341. }
  342. }
  343. CustomerCoeCellType.prototype.deactivateEditor = function (editorContext, context) {
  344. };
  345. CustomerCoeCellType.prototype.setEditorValue = function (editor, value, context) {
  346. console.log("set editor value");
  347. this.displayText = value;
  348. };
  349. CustomerCoeCellType.prototype.getEditorValue = function (editor, context) {
  350. console.log("get value");
  351. if(this.isEscKey !=true&& updateCallback){
  352. updateCallback();
  353. }
  354. this.isEscKey = false;
  355. return this.displayText;
  356. };
  357. CustomerCoeCellType.prototype.updateEditor = function (editorContext, cellStyle, cellRect, context) {
  358. console.log(" update editor");
  359. if( setEditorValue){//不是esc时才更新
  360. setEditorValue(context);
  361. }
  362. };
  363. CustomerCoeCellType.prototype.isReservedKey = function (e, context) {
  364. //cell type handle tab key by itself
  365. this.isEscKey = e.keyCode === GC.Spread.Commands.Key.esc;
  366. return false;
  367. };
  368. return new CustomerCoeCellType();
  369. },
  370. scrollSheetForOption:function (sheet,cxt,cellRect,row,options){
  371. let topRow = sheet.getViewportTopRow(1);
  372. if(row == topRow) return;//已经是最顶行了
  373. let length = options&&options.length>0?options.length:1;
  374. let height = cxt.canvas.height;
  375. let startY = cellRect.y+cellRect.height;//下拉框的起始显示位置
  376. let endY = startY + length * cellRect.height;//下拉框的结束显示位置
  377. if(endY <= height) return; //如果没有超出显示位置,直接返回
  378. let overRow = Math.ceil((endY - height)/cellRect.height);//超出的行数
  379. let showRow = topRow + overRow > row?row:topRow + overRow;
  380. sheet.showRow(showRow, GC.Spread.Sheets.VerticalPosition.top);
  381. },
  382. setSelectButton(row,col,sheet,header){
  383. /* let getSelectButton = function (cellWidth=100) {
  384. function moreButton() {
  385. }
  386. moreButton.prototype = new GC.Spread.Sheets.CellTypes.Button();
  387. moreButton.prototype.paint = function (ctx, value, x, y, w, h, style, options){
  388. GC.Spread.Sheets.CellTypes.Button.prototype.paint.call(this, ctx, value, x, y, w, h, style, options);
  389. let buttonW = cellWidth/5;
  390. let endX = x+w-2;
  391. if(value){
  392. let textWidth = ctx.measureText(value).width;
  393. let spaceWidth = cellWidth - buttonW;
  394. let textEndX = x+2+textWidth;
  395. if(spaceWidth<textWidth){
  396. for(let i = value.length-1;i>1;i--){
  397. let newValue = value.substr(0,i);
  398. let newTestWidth = ctx.measureText(newValue).width;
  399. if(spaceWidth>newTestWidth){
  400. value = newValue;
  401. textEndX = x+2+newTestWidth;
  402. break;
  403. }
  404. }
  405. }
  406. ctx.fillText(value,textEndX,y+h-5);
  407. }
  408. //画三个点
  409. ctx.save();
  410. ctx.beginPath();
  411. ctx.arc(endX-buttonW/2,y+h/2,1,0,360,false);
  412. ctx.arc(endX-buttonW/2-4,y+h/2,1,0,360,false);
  413. ctx.arc(endX-buttonW/2+4,y+h/2,1,0,360,false);
  414. ctx.fillStyle="black";//填充颜色,默认是黑色
  415. ctx.fill();//画实心圆
  416. ctx.closePath();
  417. ctx.restore();
  418. };
  419. moreButton.prototype.processMouseLeave= function (hitinfo) {
  420. let newCell = new selectButton();
  421. hitinfo.sheet.setCellType(hitinfo.row, hitinfo.col, newCell, GC.Spread.Sheets.SheetArea.viewport);
  422. hitinfo.sheet.getCell(hitinfo.row, hitinfo.col).locked(false);
  423. };
  424. function selectButton() {
  425. }
  426. selectButton.prototype = new GC.Spread.Sheets.CellTypes.Text();
  427. selectButton.prototype.paint = function (ctx, value, x, y, w, h, style, options){
  428. GC.Spread.Sheets.CellTypes.Text.prototype.paint.apply(this,arguments);
  429. };
  430. selectButton.prototype.getHitInfo = function (x, y, cellStyle, cellRect, context) {
  431. return {
  432. x: x,
  433. y: y,
  434. row: context.row,
  435. col: context.col,
  436. cellStyle: cellStyle,
  437. cellRect: cellRect,
  438. sheetArea: context.sheetArea
  439. };
  440. };
  441. selectButton.prototype.processMouseDown = function (hitinfo){
  442. if(hitinfo.sheet.getCell(hitinfo.row,hitinfo.col).locked()!=true){
  443. let b1 = new moreButton();
  444. b1.marginLeft(cellWidth*4/5);
  445. hitinfo.sheet.setCellType(hitinfo.row, hitinfo.col, b1, GC.Spread.Sheets.SheetArea.viewport);
  446. hitinfo.sheet.getCell(hitinfo.row, hitinfo.col).locked(true);
  447. }
  448. };
  449. return new selectButton();
  450. };*/
  451. sheet.setCellType(row, col,this.getSelectButton(header.headerWidth),GC.Spread.Sheets.SheetArea.viewport);
  452. },
  453. getCusButtonCellType:function (callback,readOnly=false) {
  454. var ns = GC.Spread.Sheets;
  455. function CusButtonCellType() {
  456. }
  457. CusButtonCellType.prototype = new ns.CellTypes.Text();
  458. CusButtonCellType.prototype.paint = function (ctx, value, x, y, w, h, style, options) {
  459. if(!readOnly){
  460. if(options.sheet.getActiveRowIndex()==options.row&&options.sheet.getActiveColumnIndex()==options.col){
  461. var image = document.getElementById('f_btn'),imageMagin = 3;
  462. var imageHeight = 15;
  463. var imageWidth = 25;
  464. var imageX = x + w - imageWidth- imageMagin, imageY = y + h / 2 - imageHeight / 2;
  465. ctx.save();
  466. ctx.fillStyle = style.backColor;
  467. ctx.fillRect(x,y,w,h);
  468. ctx.drawImage(image, imageX, imageY,imageWidth,imageHeight);
  469. ctx.beginPath();
  470. ctx.arc(imageX+imageWidth/2,imageY+imageHeight/2,1,0,360,false);
  471. ctx.arc(imageX+imageWidth/2-4,imageY+imageHeight/2,1,0,360,false);
  472. ctx.arc(imageX+imageWidth/2+4,imageY+imageHeight/2,1,0,360,false);
  473. ctx.fillStyle="black";//填充颜色,默认是黑色
  474. ctx.fill();//画实心圆
  475. ctx.closePath();
  476. ctx.restore();
  477. //这里的左对齐的,当显示的字长度超过空白地方时,要改成右对齐
  478. if(style.hAlign == 0){
  479. w = w - imageWidth - imageMagin;
  480. if(value){
  481. let textWidth = ctx.measureText(value).width;
  482. let spaceWidth = w;
  483. if(spaceWidth<textWidth){
  484. style.hAlign = 2;
  485. }
  486. }
  487. }else if(style.hAlign == 2){//如果是右对齐的,往左边挪,给按钮留出空间
  488. x = x - imageWidth - imageMagin;
  489. }
  490. }
  491. }
  492. GC.Spread.Sheets.CellTypes.Text.prototype.paint.apply(this, arguments);
  493. };
  494. CusButtonCellType.prototype.getHitInfo = function (x, y, cellStyle, cellRect, context) {
  495. return {
  496. x: x,
  497. y: y,
  498. row: context.row,
  499. col: context.col,
  500. cellStyle: cellStyle,
  501. cellRect: cellRect,
  502. sheetArea: context.sheetArea
  503. };
  504. };
  505. CusButtonCellType.prototype.processMouseDown = function (hitinfo) {
  506. if(hitinfo.sheet.getActiveRowIndex()==hitinfo.row&&hitinfo.sheet.getActiveColumnIndex()==hitinfo.col){
  507. var offset=hitinfo.cellRect.x+hitinfo.cellRect.width-6;
  508. var imageWidth = 25;
  509. if(hitinfo.x<offset&&hitinfo.x>offset-imageWidth){
  510. if(!readOnly){
  511. if(callback) callback(hitinfo)
  512. }
  513. }
  514. }
  515. };
  516. return new CusButtonCellType();
  517. },
  518. getSelectButton(cellWidth=100){
  519. function moreButton() {
  520. }
  521. moreButton.prototype = new GC.Spread.Sheets.CellTypes.Button();
  522. moreButton.prototype.paint = function (ctx, value, x, y, w, h, style, options){
  523. GC.Spread.Sheets.CellTypes.Button.prototype.paint.call(this, ctx, value, x, y, w, h, style, options);
  524. ctx.font = '14px Calibri';
  525. let buttonW = cellWidth/5;
  526. let endX = x+w-2;
  527. if(value){
  528. let textWidth = ctx.measureText(value).width;
  529. let spaceWidth = cellWidth - buttonW;
  530. let textEndX = x+textWidth+2;
  531. if(spaceWidth<textWidth){
  532. for(let i = value.length-1;i>1;i--){
  533. let newValue = value.substr(0,i);
  534. let newTestWidth = ctx.measureText(newValue).width;
  535. if(spaceWidth>newTestWidth){
  536. value = newValue;
  537. textEndX = x+newTestWidth+2;
  538. break;
  539. }
  540. }
  541. }
  542. ctx.fillText(value,textEndX,y+h-6);
  543. }
  544. //画三个点
  545. ctx.save();
  546. ctx.beginPath();
  547. ctx.arc(endX-buttonW/2,y+h/2,1,0,360,false);
  548. ctx.arc(endX-buttonW/2-4,y+h/2,1,0,360,false);
  549. ctx.arc(endX-buttonW/2+4,y+h/2,1,0,360,false);
  550. ctx.fillStyle="black";//填充颜色,默认是黑色
  551. ctx.fill();//画实心圆
  552. ctx.closePath();
  553. ctx.restore();
  554. };
  555. moreButton.prototype.processMouseLeave= function (hitinfo) {
  556. let newCell = new selectButton();
  557. hitinfo.sheet.setCellType(hitinfo.row, hitinfo.col, newCell, GC.Spread.Sheets.SheetArea.viewport);
  558. hitinfo.sheet.getCell(hitinfo.row, hitinfo.col).locked(false);
  559. };
  560. function selectButton() {
  561. }
  562. selectButton.prototype = new GC.Spread.Sheets.CellTypes.Text();
  563. selectButton.prototype.paint = function (ctx, value, x, y, w, h, style, options){
  564. GC.Spread.Sheets.CellTypes.Text.prototype.paint.apply(this,arguments);
  565. };
  566. selectButton.prototype.getHitInfo = function (x, y, cellStyle, cellRect, context) {
  567. return {
  568. x: x,
  569. y: y,
  570. row: context.row,
  571. col: context.col,
  572. cellStyle: cellStyle,
  573. cellRect: cellRect,
  574. sheetArea: context.sheetArea
  575. };
  576. };
  577. selectButton.prototype.processMouseDown = function (hitinfo){
  578. if(hitinfo.sheet.getCell(hitinfo.row,hitinfo.col).locked()!=true){
  579. let b1 = new moreButton();
  580. b1.marginLeft(cellWidth*4/5);
  581. hitinfo.sheet.setCellType(hitinfo.row, hitinfo.col, b1, GC.Spread.Sheets.SheetArea.viewport);
  582. hitinfo.sheet.getCell(hitinfo.row, hitinfo.col).locked(true);
  583. }
  584. };
  585. return new selectButton();
  586. },
  587. setReplaceButton(row,col,sheet){
  588. let replaceButton = function(){
  589. };
  590. replaceButton.prototype = new GC.Spread.Sheets.CellTypes.Button();
  591. replaceButton.prototype.paint = function (ctx, value, x, y, w, h, style, options){
  592. GC.Spread.Sheets.CellTypes.Button.prototype.paint.apply(this,arguments);
  593. if(value){
  594. ctx.save();
  595. ctx.fillStyle = "white";
  596. let fh = options.fontInfo&&options.fontInfo.fontSize?options.fontInfo.fontSize:h-6;
  597. ctx.fillText(value,x+(w+ctx.measureText(value).width)/2,y+(h+fh)/2-2);
  598. ctx.restore();
  599. }
  600. };
  601. let cellType = new replaceButton();
  602. cellType.buttonBackColor("#07A0FF");
  603. sheet.setCellType(row, col,cellType,GC.Spread.Sheets.SheetArea.viewport);
  604. },
  605. setTipsCell(row,col,sheet,header){
  606. let TipCellType = function () {};
  607. TipCellType.prototype = new GC.Spread.Sheets.CellTypes.Text();
  608. TipCellType.prototype.getHitInfo = function (x, y, cellStyle, cellRect, context) {
  609. return {
  610. x: x,
  611. y: y,
  612. row: context.row,
  613. col: context.col,
  614. cellStyle: cellStyle,
  615. cellRect: cellRect,
  616. sheet: context.sheet,
  617. sheetArea: context.sheetArea
  618. };
  619. };
  620. TipCellType.prototype.processMouseEnter = function (hitinfo) {
  621. let text = hitinfo.sheet.getText(hitinfo.row, hitinfo.col);
  622. let value = hitinfo.sheet.getValue(hitinfo.row, hitinfo.col);
  623. let tag = hitinfo.sheet.getTag(hitinfo.row, hitinfo.col);
  624. let acStyle = hitinfo.sheet.getActualStyle(hitinfo.row, hitinfo.col),
  625. zoom = hitinfo.sheet.zoom();
  626. let textLength = this.getAutoFitWidth(value, text, acStyle, zoom, {sheet: hitinfo.sheet, row: hitinfo.row, col: hitinfo.col, sheetArea: GC.Spread.Sheets.SheetArea.viewport});
  627. let cellWidth = hitinfo.sheet.getCell(-1, hitinfo.col).width();
  628. let setting = {};
  629. if(textLength <= cellWidth){
  630. return;
  631. }
  632. if(sheet && sheet.getParent().qo){
  633. setting.pos = SheetDataHelper.getObjPos(sheet.getParent().qo);
  634. }
  635. TREE_SHEET_HELPER.showTipsDiv(text,setting,hitinfo);
  636. };
  637. TipCellType.prototype.processMouseLeave = function (hitinfo) {
  638. TREE_SHEET_HELPER.tipDiv = 'hide';
  639. if (TREE_SHEET_HELPER._toolTipElement) {
  640. $(TREE_SHEET_HELPER._toolTipElement).hide();
  641. TREE_SHEET_HELPER._toolTipElement = null;
  642. };
  643. TREE_SHEET_HELPER.tipDivCheck();//延时检查:当tips正在show的时候,就调用了hide方法,会导致tips一直存在,所以设置一个超时处理
  644. };
  645. sheet.setCellType(row, col,new TipCellType(),GC.Spread.Sheets.SheetArea.viewport);
  646. },
  647. chkIfEmpty: function(rObj, setting) {
  648. var rst = true;
  649. if (rObj) {
  650. for (var i = 0; i < setting.header.length; i++) {
  651. if (rObj[setting.header[i].dataCode]) {
  652. rst = false;
  653. break;
  654. }
  655. }
  656. }
  657. return rst;
  658. },
  659. //add by zhong 2017-10-10
  660. //动态下拉框,配合EnterCell, args.sheet.repaint();
  661. getDynamicCombo: function (forLocked) {
  662. let ComboCellForActiveCell = function () { };
  663. ComboCellForActiveCell.prototype = new GC.Spread.Sheets.CellTypes.ComboBox();
  664. ComboCellForActiveCell.prototype.paintValue = function (ctx, value, x, y, w, h, style, options) {
  665. let sheet = options.sheet;
  666. if (options.row === sheet.getActiveRowIndex() && options.col === sheet.getActiveColumnIndex() && (!forLocked || forLocked && !sheet.getCell(options.row, options.col).locked())) {
  667. GC.Spread.Sheets.CellTypes.ComboBox.prototype.paintValue.apply(this, arguments);
  668. } else {
  669. GC.Spread.Sheets.CellTypes.Base.prototype.paintValue.apply(this, arguments);
  670. }
  671. };
  672. ComboCellForActiveCell.prototype.getHitInfo = function (x, y, cellStyle, cellRect, options) {
  673. let sheet = options.sheet;
  674. if (options.row === sheet.getActiveRowIndex() && options.col === sheet.getActiveColumnIndex() && (!forLocked || forLocked && !sheet.getCell(options.row, options.col).locked())) {
  675. return GC.Spread.Sheets.CellTypes.ComboBox.prototype.getHitInfo.apply(this, arguments);
  676. } else {
  677. return {
  678. x: x,
  679. y: y,
  680. row: options.row,
  681. col: options.col,
  682. cellStyle: cellStyle,
  683. cellRect: cellRect,
  684. sheetArea: options.sheetArea
  685. };//GC.Spread.Sheets.CellTypes.Text.prototype.getHitInfo.apply(this, arguments);
  686. }
  687. };
  688. return new ComboCellForActiveCell();
  689. },
  690. getTipsCombo:function (forLocked,tips,setting,node) {
  691. let getTipsCombo = function () {
  692. this.clickCom=false;
  693. };
  694. getTipsCombo.prototype = sheetCommonObj.getDynamicCombo(forLocked);
  695. if(tips && tips !=""){
  696. getTipsCombo.prototype.processMouseEnter = function(hitinfo){
  697. if(this.clickCom == true){ //点击了下拉框的三角形,则不用再显示悬浮框了
  698. this.clickCom = false;
  699. return;
  700. }
  701. let text = typeof tips == 'function'?tips(node):tips;
  702. TREE_SHEET_HELPER.delayShowTips(hitinfo,setting,text);
  703. };
  704. getTipsCombo.prototype.processMouseLeave = function (hitinfo) {
  705. TREE_SHEET_HELPER.hideTipsDiv();
  706. };
  707. getTipsCombo.prototype.processMouseDown = function (hitinfo){
  708. if(hitinfo.isReservedLocation == true){//这里是点击了下拉框的三角形才会有这个属性
  709. TREE_SHEET_HELPER.hideTipsDiv();
  710. this.clickCom = true;
  711. }
  712. GC.Spread.Sheets.CellTypes.ComboBox.prototype.processMouseDown.apply(this, arguments);
  713. };
  714. getTipsCombo.prototype.updateEditor = function (editorContext, cellStyle, cellRect, context){
  715. TREE_SHEET_HELPER.hideTipsDiv();
  716. GC.Spread.Sheets.CellTypes.ComboBox.prototype.updateEditor.apply(this, arguments);
  717. };
  718. }
  719. return new getTipsCombo();
  720. },
  721. setDynamicCombo: function (sheet, beginRow, col, rowCount, items, itemsHeight, itemsType) {
  722. let me = this;
  723. sheet.suspendPaint();
  724. let combo = me.getDynamicCombo();
  725. for(let i = 0, len = rowCount; i < len; i++){
  726. if(itemsHeight) {
  727. combo.itemHeight(itemsHeight);
  728. combo._maxDropDownItems = itemsHeight + 5;
  729. }
  730. if(itemsType === 'value') combo.items(items).editorValueType(GC.Spread.Sheets.CellTypes.EditorValueType.value);
  731. else if(itemsType === 'text') combo.items(items).editorValueType(GC.Spread.Sheets.CellTypes.EditorValueType.text);
  732. else combo.items(items);
  733. sheet.getCell(beginRow + i, col).cellType(combo);
  734. }
  735. sheet.resumePaint();
  736. },
  737. setStaticCombo: function (sheet, beginRow, col, rowCount, items, itemsHeight, itemsType) {
  738. sheet.suspendPaint();
  739. let combo = new GC.Spread.Sheets.CellTypes.ComboBox();
  740. for(let i = 0, len = rowCount; i < len; i++){
  741. if(itemsHeight) combo.itemHeight(itemsHeight);
  742. if(itemsType === 'value') combo.items(items).editorValueType(GC.Spread.Sheets.CellTypes.EditorValueType.value);
  743. else if(itemsType === 'text') combo.items(items).editorValueType(GC.Spread.Sheets.CellTypes.EditorValueType.text);
  744. else combo.items(items);
  745. sheet.getCell(beginRow + i, col).cellType(combo);
  746. }
  747. sheet.resumePaint();
  748. },
  749. //设置系统粘贴板数据,需要用户触发事件,直接调用会失败
  750. copyTextToClipboard: function(text) {
  751. let textArea = document.createElement("textarea");
  752. textArea.style.position = 'fixed';
  753. textArea.style.top = 0;
  754. textArea.style.left = 0;
  755. textArea.style.width = '2em';
  756. textArea.style.height = '2em';
  757. textArea.style.padding = 0;
  758. textArea.style.border = 'none';
  759. textArea.style.outline = 'none';
  760. textArea.style.boxShadow = 'none';
  761. textArea.style.background = 'transparent';
  762. textArea.value = text;
  763. document.body.appendChild(textArea);
  764. textArea.select();
  765. try {
  766. let successful = document.execCommand('copy');
  767. let msg = successful ? 'successful' : 'unsuccessful';
  768. console.log('Copying text command was ' + msg);
  769. } catch (err) {
  770. console.log('Oops, unable to copy');
  771. }
  772. document.body.removeChild(textArea);
  773. },
  774. //获取选中区域的表格类型数据(可粘贴到excel)
  775. getTableData: function (sheet, colSettings = null) {
  776. let rst = '';
  777. let sel = sheet.getSelections()[0];
  778. let pasteText = [];
  779. for(let row = sel.row; row < sel.row + sel.rowCount; row++){
  780. if(!sheet.getCell(row, -1).visible())
  781. continue;
  782. let rowText = [];
  783. for(let j = 0; j < sel.colCount; j++){
  784. let col = sel.col + j;
  785. if(!sheet.getCell(-1, col).visible())
  786. continue;
  787. if(colSettings && (colSettings[col]['data']['field'] === 'itemCharacterText' || colSettings[col]['data']['field'] === 'jobContentText'))
  788. rowText.push(sheet.getText(row, col) ? `"${sheet.getText(row, col)}"` : '');
  789. else
  790. rowText.push(sheet.getText(row, col) ? sheet.getText(row, col): '');
  791. }
  792. pasteText.push(rowText.join('\t'));
  793. }
  794. return pasteText.join('\n');
  795. },
  796. transferToTreeSetting:function(setting,treeSetting,treeCol){
  797. for(let h of setting.header){
  798. treeSetting.cols.push(getSettingCol(h))
  799. }
  800. for(let l of setting.view.lockColumns){
  801. treeSetting.cols[l].readOnly = true;
  802. }
  803. return treeSetting;
  804. function getSettingCol(header) {
  805. let aMap ={left:0,center:1,right:2};
  806. let hAlign = header.hAlign?aMap[header.hAlign]:0;
  807. let col = {
  808. "width":header.headerWidth?header.headerWidth:100,
  809. "head":{
  810. "titleNames":Array.isArray(header.headerName)?header.headerName:[header.headerName],
  811. "spanCols":header.spanCols?header.spanCols:[1],
  812. "spanRows":header.spanRows?header.spanRows:[1],
  813. "vAlign":[1],
  814. "hAlign":[1],
  815. "font":["Arial"]
  816. },
  817. "data": {
  818. "field": header.dataCode,
  819. "vAlign": 1,
  820. "hAlign": hAlign,
  821. "font": "Arial"
  822. }
  823. };
  824. if(header.showHint == true){
  825. col.showHint = true;
  826. }
  827. if(header.cellType){
  828. col.data.cellType = getCellType(header);
  829. }
  830. if(header.decimalField){//设置formatter
  831. let decimal = getDecimal(header.decimalField);
  832. col.formatter = getFormatter(decimal);
  833. }
  834. if(header.getText && treeCol){
  835. col.data.getText = treeCol.getEvent(header.getText);
  836. }
  837. /*col.readOnly = function (node) {
  838. if(node.data.ParentID == -1 || node.data.id == 'GJ'){//三材类别项不能编辑)
  839. return true;
  840. }
  841. return false;
  842. };*/
  843. return col;
  844. }
  845. function getCellType(header) {
  846. return function () {
  847. if(header.cellType === "checkBox"){
  848. return new GC.Spread.Sheets.CellTypes.CheckBox();
  849. }
  850. if(header.cellType === "comboBox"){
  851. let dynamicCombo = sheetCommonObj.getDynamicCombo(true);
  852. if(header.options){
  853. dynamicCombo.itemHeight(header.options.length).items(header.options);
  854. if(header.editorValueType==true){
  855. dynamicCombo.editorValueType(GC.Spread.Sheets.CellTypes.EditorValueType.value);
  856. }
  857. }
  858. return dynamicCombo
  859. }
  860. }
  861. }
  862. },
  863. //注册自定义回车键事件
  864. bindEnterKey: function (workBook, operation) {
  865. workBook.commandManager().register('myEnter', operation);
  866. workBook.commandManager().setShortcutKey(null, GC.Spread.Commands.Key.enter, false, false, false, false);
  867. workBook.commandManager().setShortcutKey('myEnter', GC.Spread.Commands.Key.enter, false, false, false, false);
  868. },
  869. //解决esc后触发了编辑结束的保存事件,显示与实际数据不同问题
  870. bindEscKey: function (workBook, sheets) {
  871. function isDef(v){
  872. return typeof v !== 'undefined' && v !== null;
  873. }
  874. workBook.commandManager().register('myEsc', function () {
  875. let activeSheet = workBook.getActiveSheet();
  876. let hasTheSheet = false;
  877. for(let sheetObj of sheets){
  878. let sheet = sheetObj.sheet;
  879. if(sheet === activeSheet){
  880. hasTheSheet = true;
  881. let editStarting = sheetObj.editStarting;
  882. let editEnded = sheetObj.editEnded;
  883. if(editStarting){
  884. sheet.unbind(GC.Spread.Sheets.Events.EditStarting);
  885. }
  886. if(editEnded){
  887. sheet.unbind(GC.Spread.Sheets.Events.EditEnded);
  888. }
  889. let row = sheet.getActiveRowIndex();
  890. let col = sheet.getActiveColumnIndex();
  891. let orgV = sheet.getValue(row, col);
  892. let orgText = sheet.getText(row, col);
  893. if(!isDef(orgV)){
  894. orgV = '';
  895. }
  896. if(sheet.isEditing()){
  897. sheet.endEdit();
  898. sheet.setValue(row, col, orgV);
  899. }
  900. if(editStarting){
  901. sheet.bind(GC.Spread.Sheets.Events.EditStarting, editStarting);
  902. }
  903. if(editEnded){
  904. sheet.bind(GC.Spread.Sheets.Events.EditEnded, editEnded);
  905. }
  906. }
  907. }
  908. //容错处理,以防没把所有工作簿的表格信息传入参数
  909. if(!hasTheSheet){
  910. if(activeSheet.isEditing()){
  911. activeSheet.endEdit();
  912. }
  913. }
  914. });
  915. workBook.commandManager().setShortcutKey(null, GC.Spread.Commands.Key.esc, false, false, false, false);
  916. workBook.commandManager().setShortcutKey('myEsc', GC.Spread.Commands.Key.esc, false, false, false, false);
  917. },
  918. //设置默认样式
  919. spreadDefaultStyle: function (workBook) {
  920. let defaultStyle = new GC.Spread.Sheets.Style();
  921. defaultStyle.font = '14px Calibri';
  922. let sheetCount = workBook.getSheetCount();
  923. for(let i = 0; i < sheetCount; i++){
  924. let sheet = workBook.getSheet(i);
  925. sheet.setDefaultStyle(defaultStyle, GC.Spread.Sheets.SheetArea.viewport);
  926. sheet.setDefaultStyle(defaultStyle, GC.Spread.Sheets.SheetArea.colHeader);
  927. sheet.setDefaultStyle(defaultStyle, GC.Spread.Sheets.SheetArea.rowHeader);
  928. }
  929. },
  930. //动态根据工作簿宽度和各列宽度比例设置宽度
  931. setColumnWidthByRate: function (workBookWidth, workBook, headers){
  932. if(workBook){
  933. const sheet = workBook.getActiveSheet();
  934. sheet.suspendEvent();
  935. sheet.suspendPaint();
  936. for(let col = 0; col < headers.length; col++){
  937. if(headers[col]['rateWidth'] !== undefined && headers[col]['rateWidth'] !== null && headers[col]['rateWidth'] !== ''){
  938. sheet.setColumnWidth(col, workBookWidth * headers[col]['rateWidth'], GC.Spread.Sheets.SheetArea.colHeader)
  939. }
  940. else {
  941. if(headers[col]['headerWidth'] !== undefined && headers[col]['headerWidth'] !== null && headers[col]['headerWidth'] !== ''){
  942. sheet.setColumnWidth(col, headers[col]['headerWidth'], GC.Spread.Sheets.SheetArea.colHeader)
  943. }
  944. }
  945. }
  946. sheet.resumeEvent();
  947. sheet.resumePaint();
  948. }
  949. },
  950. drowRect:function (ctx, x, y, w, h,rectW,rectH,margin) {
  951. ctx.save();
  952. ctx.strokeStyle = "gray";
  953. ctx.translate(0.5, 0.5);
  954. ctx.beginPath();
  955. let rectX = x + margin;
  956. let rectY = y + Math.round(h / 2) - rectH / 2;
  957. ctx.moveTo(rectX, rectY);
  958. ctx.lineTo(rectX, rectY + rectH);
  959. ctx.lineTo(rectX + rectW, rectY + rectH);
  960. ctx.lineTo(rectX + rectW, rectY);
  961. ctx.lineTo(rectX, rectY);
  962. ctx.moveTo(rectX + rectW, y + Math.round(h / 2));
  963. ctx.lineTo(rectX + rectW + 5, y + Math.round(h / 2));
  964. ctx.stroke();
  965. ctx.restore();
  966. },
  967. drawLine : function (ctx, x1, y1, x2, y2, color) {
  968. let l_color = color?color:"gray";
  969. ctx.save();
  970. ctx.translate(0.5, 0.5);
  971. ctx.beginPath();
  972. ctx.moveTo(x1, y1);
  973. ctx.lineTo(x2, y2);
  974. ctx.strokeStyle = l_color;
  975. ctx.stroke();
  976. ctx.restore();
  977. },
  978. drowSymbol :function (ctx, x, y, w, h,rectW,rectH,margin,collapsed) {
  979. ctx.save();
  980. ctx.strokeStyle = "#000000";
  981. ctx.translate(0.5, 0.5);
  982. ctx.beginPath();
  983. ctx.moveTo(x + margin + 2, y + Math.round(h / 2));
  984. ctx.lineTo(x + margin + 8, y + Math.round(h / 2));
  985. let rectY = y + Math.round(h / 2) - rectH / 2;
  986. if (collapsed) {
  987. ctx.moveTo(x + margin + rectW / 2, rectY + 2);
  988. ctx.lineTo(x + margin + rectW / 2, rectY + 2 + 6);
  989. }
  990. ctx.stroke();
  991. ctx.restore();
  992. },
  993. drowTriangle:function(ctx,x,y){//画向下三角形
  994. ctx.save();
  995. ctx.fillStyle = "black";
  996. ctx.beginPath();
  997. ctx.moveTo(x, y);
  998. ctx.lineTo(x-3, y -6);
  999. ctx.lineTo(x+3, y -6);
  1000. ctx.fill();
  1001. ctx.restore();
  1002. },
  1003. drowSubItem: function (ctx, x, y, w, h, offset, hasNext,step) {
  1004. let t_step = step?step:6;
  1005. offset += t_step;
  1006. ctx.save();
  1007. ctx.strokeStyle = "gray";
  1008. ctx.translate(0.5, 0.5);
  1009. ctx.beginPath();
  1010. ctx.moveTo(x + offset, y);
  1011. ctx.lineTo(x + offset, y + Math.round(h / 2));
  1012. offset += 9;
  1013. ctx.lineTo(x + offset, y + Math.round(h / 2));
  1014. if (hasNext) {
  1015. ctx.moveTo(x + offset - 9, y + Math.round(h / 2));
  1016. ctx.lineTo(x + offset - 9, y + h);
  1017. }
  1018. ctx.stroke();
  1019. ctx.restore();
  1020. return offset;
  1021. },
  1022. checkData:function(col,setting, value) {
  1023. let result = true;
  1024. let validator = setting.header[col].validator !== undefined ? setting.header[col].validator : null;
  1025. if (validator === null) {
  1026. return result;
  1027. }
  1028. switch (validator) {
  1029. case 'number':
  1030. let regular = /^\d+(\.\d+)?$/;
  1031. result = regular.test(value);
  1032. break;
  1033. case 'boolean':
  1034. let booleanValue = [true, false];
  1035. result = booleanValue.indexOf(value) >= 0;
  1036. break;
  1037. }
  1038. return result;
  1039. }
  1040. }