spreadjs_zh.js 56 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307
  1. /**
  2. * Spreadjs 通用方法集
  3. *
  4. * @author Mai
  5. * @date 2018/02/06
  6. * @version
  7. */
  8. // 定义画点线的方法
  9. const proto = window.CanvasRenderingContext2D && CanvasRenderingContext2D.prototype;
  10. proto.dottedLine = function (x1, y1, x2, y2, interval = 4) {
  11. const isHorizontal = x1 == x2 ? false : true;
  12. const dotLen = 1;
  13. let len = isHorizontal ? x2 - x1 : y2 - y1;
  14. this.moveTo(x1, y1);
  15. let progress = 0;
  16. while (len > progress) {
  17. if (progress > len) { progress = len; }
  18. if (isHorizontal) {
  19. this.moveTo(x1 + progress, y1);
  20. this.lineTo(x1 + progress + dotLen, y1);
  21. } else {
  22. this.moveTo(x1, y1 + progress);
  23. this.lineTo(x1, y1 + progress + dotLen);
  24. }
  25. progress += interval;
  26. }
  27. };
  28. // 简写Spread常量
  29. const spreadNS = GC.Spread.Sheets;
  30. // SpreadJs常用方法
  31. const SpreadJsObj = {
  32. initSpreadSettingEvents: function (setting, events) {
  33. const getEvent = function (eventName) {
  34. const names = eventName.split('.');
  35. let event = events;
  36. for (let name of names) {
  37. if (event[name]) {
  38. event = event[name];
  39. } else {
  40. return null;
  41. }
  42. }
  43. if (event && Object.prototype.toString.apply(event) !== "[object Function]") {
  44. return null;
  45. } else {
  46. return event;
  47. }
  48. };
  49. for (const col of setting.cols) {
  50. if (col.readOnly && Object.prototype.toString.apply(col.readOnly) === "[object String]") {
  51. col.readOnly = getEvent(col.readOnly);
  52. }
  53. if (col.getValue && Object.prototype.toString.apply(col.getValue) === "[object String]") {
  54. col.getValue = getEvent(col.getValue);
  55. }
  56. }
  57. },
  58. DataType: {
  59. Data: 'data',
  60. Tree: 'tree',
  61. },
  62. /**
  63. * 创建Spread(默认1张表,3行数据)
  64. * @param obj 用于创建spreadjs的Dom元素
  65. * @returns {GC.Spread.Sheets.Workbook}
  66. */
  67. createNewSpread: function (obj) {
  68. const spread = new spreadNS.Workbook(obj, {sheetCount: 1});
  69. spread.options.tabStripVisible = false;
  70. spread.options.scrollbarMaxAlign = true;
  71. spread.options.cutCopyIndicatorVisible = false;
  72. spread.options.allowCopyPasteExcelStyle = false;
  73. spread.options.allowUserDragDrop = false;
  74. spread.options.allowUserEditFormula = false;
  75. spread.getActiveSheet().options.clipBoardOptions = GC.Spread.Sheets.ClipboardPasteOptions.values;//设置粘贴时只粘贴值
  76. spread.getActiveSheet().setRowCount(3);
  77. return spread;
  78. },
  79. /**
  80. * 保护sheet(需设置保护后, 单元格的locked等属性方可生效)
  81. * @param {GC.Spread.Sheets.Worksheet} sheet
  82. */
  83. protectedSheet: function (sheet) {
  84. const option = {
  85. allowSelectLockedCells: true,
  86. allowSelectUnlockedCells: true,
  87. allowResizeRows: true,
  88. allowResizeColumns: true
  89. };
  90. sheet.options.protectionOptions = option;
  91. sheet.options.isProtected = true;
  92. sheet.options.allowCellOverflow = false;
  93. },
  94. /**
  95. * sheet批量操作优化(sheet操作大批量数据时, 屏蔽数据刷新, 可优化大量时间)
  96. * @param {GC.Spread.Sheets.Worksheet} sheet
  97. * @param {function} operation
  98. */
  99. beginMassOperation: function (sheet) {
  100. sheet.suspendPaint();
  101. sheet.suspendEvent();
  102. },
  103. endMassOperation: function (sheet) {
  104. sheet.resumeEvent();
  105. sheet.resumePaint();
  106. },
  107. massOperationSheet: function (sheet, operation) {
  108. this.beginMassOperation(sheet);
  109. operation();
  110. this.endMassOperation(sheet);
  111. },
  112. /**
  113. * 获取Obj左顶点位置(部分功能需通过spreadjs左顶点位置计算)
  114. * @param obj
  115. * @returns {{x: number, y: number}}
  116. */
  117. getObjPos: function (obj) {
  118. let target = obj;
  119. let pos = {x: obj.offsetLeft, y: obj.offsetTop};
  120. target = obj.offsetParent;
  121. while (target) {
  122. pos.x += target.offsetLeft;
  123. pos.y += target.offsetTop;
  124. target = target.offsetParent;
  125. }
  126. return pos;
  127. },
  128. /**
  129. * 以下四个方法来自Spread示例, 参见官网或文档
  130. */
  131. getHitTest: function (obj, e, sheet) {
  132. var offset = obj.offset(),
  133. x = e.pageX - offset.left,
  134. y = e.pageY - offset.top;
  135. return sheet.hitTest(x, y);
  136. },
  137. getTargetSelection: function (sheet, target) {
  138. if (target.hitTestType === spreadNS.SheetArea.colHeader) {
  139. return sheet.getRange(-1, target.col, sheet.getRowCount(), 1);
  140. } else if (target.hitTestType === spreadNS.SheetArea.rowHeader) {
  141. return sheet.getRange(target.row, -1, 1, sheet.getColumnCount());
  142. } else if (target.hitTestType === spreadNS.SheetArea.viewport) {
  143. return sheet.getRange(target.row, target.col, 1, 1);
  144. } else if (target.hitTestType === spreadNS.SheetArea.corner) {
  145. return sheet.getRange(-1, -1, sheet.getRowCount(), sheet.getColumnCount());
  146. };
  147. },
  148. getCellInSelections: function (selections, row, col) {
  149. const count = selections.length;
  150. let range;
  151. for (var i = 0; i < count; i++) {
  152. range = selections[i];
  153. if (range.contains(row, col)) {
  154. return range;
  155. }
  156. }
  157. return null;
  158. },
  159. checkTargetInSelection: function (selections, range) {
  160. var count = selections.length, sel;
  161. for (var i = 0; i < count; i++) {
  162. sel = selections[i];
  163. if (sel.containsRange(range)) {
  164. return true;
  165. }
  166. }
  167. return false;
  168. },
  169. /**
  170. * 获取 spread 在鼠标右键时, spread的选中区域
  171. * viewport, 选中鼠标点击单元格X, X不在原选中区域内, 则选中X
  172. * colHeader, 选中整列
  173. * rowHeader, 选中整行
  174. * corner, 全选
  175. * 该方法返回不符合需求时,可通过getHitTest/getTargetSelection/getCellInSelections/checkTargetInSelection来确定鼠标右键点击时,spread当前Sheet应选中的单元格
  176. * @param obj: 创建Spread的Dom元素(jquery-contextmenu.build方法的第一个变量可读取)
  177. * @param e: jquery-contextmenu.build方法的第二个变量
  178. * @param {GC.Spread.Sheets.Workbook} spread
  179. * @returns {*}
  180. */
  181. safeRightClickSelection: function (obj, e, spread) {
  182. const sheet = spread.getActiveSheet();
  183. const selections = sheet.getSelections(), target = this.getHitTest(obj, e, sheet), range = this.getTargetSelection(sheet, target);
  184. if (!this.checkTargetInSelection(selections, range)) {
  185. sheet.setSelection(range.row, range.col, range.rowCount, range.colCount);
  186. }
  187. return target;
  188. },
  189. /**
  190. * 获取写入sheet的数据序列
  191. * data:sheet.zh_data, tree: sheet.zh_tree.nodes
  192. * @param sheet
  193. * @returns {*}
  194. */
  195. getSortData: function (sheet) {
  196. if (sheet.zh_dataType) {
  197. if (sheet.zh_dataType === this.DataType.Data) {
  198. return sheet.zh_data;
  199. } else if (sheet.zh_dataType === this.DataType.Tree) {
  200. return sheet.zh_tree.nodes;
  201. } else {
  202. return null;
  203. }
  204. } else {
  205. return null;
  206. }
  207. },
  208. /**
  209. * sheet中 使用delete键,触发EndEdited事件
  210. * @param {GC.Spreads.Sheets.Workbook} spread
  211. * @param {function} fun
  212. */
  213. addDeleteBind: function (spread, fun) {
  214. spread.commandManager().register('deleteEvent', function () {
  215. fun(spread.getActiveSheet());
  216. });
  217. spread.commandManager().setShortcutKey(null, GC.Spread.Commands.Key.del, false, false, false, false);
  218. spread.commandManager().setShortcutKey('deleteEvent', GC.Spread.Commands.Key.del, false, false, false, false);
  219. },
  220. _initSheetDeafult: function (sheet) {
  221. if (sheet.zh_setting.headerFont) {
  222. const vStyle = new spreadNS.Style();
  223. vStyle.font = sheet.zh_setting.headerFont;
  224. sheet.setDefaultStyle(vStyle, spreadNS.SheetArea.colHeader);
  225. }
  226. if (sheet.zh_setting.font) {
  227. const vStyle = new spreadNS.Style();
  228. vStyle.font = sheet.zh_setting.font;
  229. sheet.setDefaultStyle(vStyle, spreadNS.SheetArea.viewport);
  230. }
  231. },
  232. /**
  233. * 根据sheet.zh_setting初始化sheet表头
  234. * @param {GC.Spread.Sheets.Worksheet} sheet
  235. */
  236. _initSheetHeader: function (sheet) {
  237. if (!sheet.zh_setting) { return; }
  238. sheet.setColumnCount(sheet.zh_setting.cols.length);
  239. sheet.setRowCount(sheet.zh_setting.headRows, spreadNS.SheetArea.colHeader);
  240. for (let iRow = 0; iRow < sheet.zh_setting.headRowHeight.length; iRow ++) {
  241. sheet.setRowHeight(iRow, sheet.zh_setting.headRowHeight[iRow], spreadNS.SheetArea.colHeader);
  242. }
  243. for (let iCol = 0; iCol < sheet.zh_setting.cols.length; iCol++) {
  244. const col = sheet.zh_setting.cols[iCol];
  245. const title = col.title.split('|');
  246. const colSpan = col.colSpan ? col.colSpan.split('|'): ['1'], rowSpan = col.rowSpan ? col.rowSpan.split('|'): ['1'];
  247. for (let i = 0; i < title.length; i++) {
  248. const cell = sheet.getCell(i, iCol, spreadNS.SheetArea.colHeader);
  249. cell.text(title[i]).wordWrap(true);
  250. if ((colSpan[i] !== '' && colSpan[i] !== '1') || (rowSpan[i] !== '' && rowSpan[i] !== '1')) {
  251. sheet.addSpan(i, iCol, parseInt(rowSpan[i]), parseInt(colSpan[i]), spreadNS.SheetArea.colHeader);
  252. }
  253. }
  254. sheet.setColumnWidth(iCol, col.width);
  255. if (col.visible !== undefined && col.visible !== null) {
  256. sheet.setColumnVisible(iCol, col.visible);
  257. }
  258. }
  259. sheet.rowOutlines.direction(spreadNS.Outlines.OutlineDirection.backward);
  260. sheet.showRowOutline(false);
  261. if (sheet.zh_setting.defaultRowHeight) {
  262. sheet.defaults.rowHeight = sheet.zh_setting.defaultRowHeight;
  263. }
  264. },
  265. /**
  266. * 初始化sheet, 设置sheet.zh_setting, 并初始化表头
  267. * @param {GC.Spread.Sheets.Worksheet} sheet
  268. * @param setting
  269. */
  270. initSheet: function (sheet, setting) {
  271. this.beginMassOperation(sheet);
  272. sheet.zh_setting = setting;
  273. this._initSheetDeafult(sheet);
  274. this._initSheetHeader(sheet);
  275. sheet.setRowCount(sheet.zh_setting.emptyRows);
  276. sheet.extendCellType = {};
  277. sheet.getRange(0, 0, sheet.getRowCount(), sheet.getColumnCount()).locked(setting.readOnly);
  278. this.endMassOperation(sheet);
  279. },
  280. _loadRowData: function (sheet, data, row) {
  281. // 单元格重新写入数据
  282. if (!data) { return }
  283. sheet.zh_setting.cols.forEach(function (col, j) {
  284. const cell = sheet.getCell(row, j);
  285. if (col.getValue && Object.prototype.toString.apply(col.getValue) === "[object Function]") {
  286. cell.value(col.getValue(data));
  287. } else if (col.field !== '' && data[col.field]) {
  288. cell.value(data[col.field]);
  289. }
  290. if (col.font) {
  291. cell.font(col.font);
  292. }
  293. if (col.foreColor) {
  294. cell.foreColor(col.foreColor);
  295. }
  296. if (col.readOnly && Object.prototype.toString.apply(col.readOnly) === "[object Function]") {
  297. cell.locked(col.readOnly(data) || sheet.zh_setting.readOnly || false).vAlign(1).hAlign(col.hAlign);
  298. } else {
  299. cell.locked(col.readOnly || sheet.zh_setting.readOnly || false).vAlign(1).hAlign(col.hAlign);
  300. }
  301. if (col.formatter) {
  302. cell.formatter(col.formatter);
  303. }
  304. if (sheet.zh_setting.getColor && Object.prototype.toString.apply(sheet.zh_setting.getColor) === "[object Function]") {
  305. cell.backColor(sheet.zh_setting.getColor(data, col, sheet.getDefaultStyle().backColor));
  306. }
  307. });
  308. },
  309. _defineColCellType: function (sheet, col, colSetting) {
  310. if(colSetting.cellType === 'ellipsis') {
  311. if (!sheet.extendCellType.ellipsis) {
  312. sheet.extendCellType.ellipsis = this.CellType.getEllipsisTextCellType();
  313. }
  314. sheet.getRange(-1, col, -1, 1).cellType(sheet.extendCellType.ellipsis);
  315. }
  316. if(colSetting.cellType === 'html') {
  317. if (!sheet.extendCellType.html) {
  318. sheet.extendCellType.html = this.CellType.getHtmlCellType();
  319. }
  320. sheet.getRange(-1, col, -1, 1).cellType(sheet.extendCellType.html);
  321. }
  322. if (colSetting.cellType === 'image') {
  323. if (!sheet.extendCellType.image) {
  324. sheet.extendCellType.image = this.CellType.getImageCellType();
  325. }
  326. sheet.getRange(-1, col, -1, 1).cellType(sheet.extendCellType.image);
  327. }
  328. if (colSetting.cellType === 'imageBtn') {
  329. if (!sheet.extendCellType.image) {
  330. sheet.extendCellType.imageBtn = this.CellType.getImageButtonCellType();
  331. }
  332. sheet.getRange(-1, col, -1, 1).cellType(sheet.extendCellType.imageBtn);
  333. }
  334. if (colSetting.cellType === 'tree') {
  335. if (!sheet.extendCellType.tree) {
  336. sheet.extendCellType.tree = this.CellType.getTreeNodeCellType();
  337. }
  338. sheet.getRange(-1, col, -1, 1).cellType(sheet.extendCellType.tree);
  339. }
  340. if (colSetting.cellType === 'tip') {
  341. if (!sheet.extendCellType.tip) {
  342. sheet.extendCellType.tip = this.CellType.getTipCellType();
  343. }
  344. sheet.getRange(-1, col, -1, 1).cellType(sheet.extendCellType.tip);
  345. }
  346. if (colSetting.cellType === 'checkbox') {
  347. if (!sheet.extendCellType.checkbox) {
  348. sheet.extendCellType.checkbox = new spreadNS.CellTypes.CheckBox();
  349. }
  350. sheet.getRange(-1, col, -1, 1).cellType(sheet.extendCellType.checkbox);
  351. }
  352. if (colSetting.formatter) {
  353. sheet.getRange(-1, col, -1, 1).formatter(colSetting.formatter);
  354. }
  355. },
  356. /**
  357. * 整个sheet重新加载数据
  358. * @param {GC.Spread.Sheets.Worksheet} sheet
  359. */
  360. reLoadSheetData: function (sheet) {
  361. const self = this;
  362. const sortData = sheet.zh_dataType === 'tree' ? sheet.zh_tree.nodes : sheet.zh_data;
  363. this.beginMassOperation(sheet);
  364. try {
  365. sheet.clear(0, 0, sheet.getRowCount(), sheet.getColumnCount(), spreadNS.SheetArea.viewport, spreadNS.StorageType.data)
  366. // 设置总行数
  367. const totalRow = sortData.length + sheet.zh_setting.emptyRows;
  368. sheet.setRowCount(totalRow, spreadNS.SheetArea.viewport);
  369. // 控制空白行
  370. const emptyRows = sheet.getRange(sortData.length, -1, sheet.zh_setting.emptyRows, -1);
  371. emptyRows.locked(sheet.zh_dataType === 'tree');
  372. // 单元格写入数据
  373. sortData.forEach(function (data, i) {
  374. self._loadRowData(sheet, data, i);
  375. sheet.setRowVisible(i, data.visible);
  376. });
  377. // 设置列单元格格式
  378. sheet.zh_setting.cols.forEach(function (col, j) {
  379. //if (!col.cellType) { return; }
  380. self._defineColCellType(sheet, j, col);
  381. });
  382. this.endMassOperation(sheet);
  383. } catch (err) {
  384. this.endMassOperation(sheet);
  385. }
  386. },
  387. /**
  388. * 重新加载部分数据行
  389. * @param {GC.Spread.Sheets.Worksheet} sheet
  390. * @param {Number} row
  391. * @param {Number} count
  392. */
  393. reLoadRowData: function (sheet, row, count = 1) {
  394. //if (row < 0) { return; }
  395. const self = this;
  396. const sortData = sheet.zh_dataType === 'tree' ? sheet.zh_tree.nodes : sheet.zh_data;
  397. this.beginMassOperation(sheet);
  398. try {
  399. // 清空原单元格数据
  400. sheet.clear(row, -1, count, -1, spreadNS.SheetArea.viewport, spreadNS.StorageType.data);
  401. // 单元格重新写入数据
  402. for (let i = row; i < row + count; i++) {
  403. const data = sortData[i];
  404. if (!data) { continue; }
  405. this._loadRowData(sheet, data, i);
  406. }
  407. this.endMassOperation(sheet);
  408. } catch (err) {
  409. this.endMassOperation(sheet);
  410. }
  411. },
  412. /**
  413. * 重新加载部分行数据
  414. * @param {GC.Spread.Sheets.Worksheet} sheet
  415. * @param {Array} rows
  416. */
  417. reLoadRowsData: function (sheet, rows) {
  418. const self = this;
  419. const sortData = sheet.zh_dataType === 'tree' ? sheet.zh_tree.nodes : sheet.zh_data;
  420. this.beginMassOperation(sheet);
  421. try {
  422. for (const row of rows) {
  423. if (row < 0) { continue; }
  424. // 清空原单元格数据
  425. sheet.clear(row, -1, 1, -1, spreadNS.SheetArea.viewport, spreadNS.StorageType.data);
  426. const data = sortData[row];
  427. // 单元格重新写入数据
  428. this._loadRowData(sheet, data, row);
  429. };
  430. this.endMassOperation(sheet);
  431. } catch (err) {
  432. this.endMassOperation(sheet);
  433. }
  434. },
  435. /**
  436. * 重新加载部分列数据
  437. * @param {GC.Spread.Sheets.Worksheet} sheet
  438. * @param {Array} cols
  439. */
  440. reLoadColsData: function (sheet, cols) {
  441. const self = this;
  442. const sortData = sheet.zh_dataType === 'tree' ? sheet.zh_tree.nodes : sheet.zh_data;
  443. this.beginMassOperation(sheet);
  444. try {
  445. for (const iCol of cols) {
  446. // 清空原单元格数据
  447. sheet.clear(-1, iCol, -1, 1, spreadNS.SheetArea.viewport, spreadNS.StorageType.data);
  448. const col = sheet.zh_setting.cols[iCol];
  449. sortData.forEach(function (data, i) {
  450. // 设置值
  451. const cell = sheet.getCell(i, iCol);
  452. if (col.field !== '' && data[col.field]) {
  453. cell.value(data[col.field]).locked(col.readOnly || sheet.zh_setting.readOnly || false).vAlign(1).hAlign(col.hAlign);
  454. } else {
  455. cell.locked(col.readOnly || sheet.zh_setting.readOnly || false).vAlign(1).hAlign(col.hAlign);
  456. }
  457. // 设置单元格格式
  458. if (col.formatter) {
  459. cell.formatter(col.formatter);
  460. }
  461. });
  462. }
  463. this.endMassOperation(sheet);
  464. } catch (err) {
  465. this.endMassOperation(sheet);
  466. }
  467. },
  468. reLoadNodesData: function (sheet, nodes) {
  469. this.beginMassOperation(sheet);
  470. nodes = nodes instanceof Array ? nodes : [nodes];
  471. for (const node of nodes) {
  472. const sortData = sheet.zh_dataType === 'tree' ? sheet.zh_tree.nodes : sheet.zh_data;
  473. this._loadRowData(sheet, node, sortData.indexOf(node));
  474. }
  475. this.endMassOperation(sheet);
  476. },
  477. /**
  478. * 根据data加载sheet数据,合并了一般数据和树结构数据的加载
  479. * @param {GC.Spread.Sheets.Worksheet} sheet
  480. * @param {String} dataType - 1.'zh_data' 2.'zh_tree'
  481. * @param {Array|PathTree} data - 对dataType对应
  482. */
  483. loadSheetData: function (sheet, dataType, data){
  484. sheet.zh_dataType = dataType;
  485. if (dataType === 'tree') {
  486. sheet.zh_tree = data;
  487. } else {
  488. sheet.zh_data = data;
  489. }
  490. this.protectedSheet(sheet);
  491. this.reLoadSheetData(sheet);
  492. },
  493. /**
  494. * 获取复制数据HTML格式(过滤不可见单元格)
  495. * @param {GC.Spread.Sheets.Worksheet} sheet
  496. * @returns {string}
  497. */
  498. getFilterCopyHTML: function (sheet) {
  499. const sel = sheet.getSelections()[0];
  500. const html = [];
  501. html.push('<table>');
  502. for (let i = sel.row, iLen = sel.row + sel.rowCount; i < iLen; i++) {
  503. // 跳过隐藏行
  504. if (!sheet.getCell(i, -1).visible()) { continue; }
  505. const rowHtml = [];
  506. rowHtml.push('<tr>');
  507. for (let j = sel.col, jLen = sel.col + sel.colCount; j < jLen; j++) {
  508. const data = sheet.getText(i, j);
  509. rowHtml.push('<td>' + data + '</td>');
  510. }
  511. rowHtml.push('</tr>');
  512. html.push(rowHtml.join(''));
  513. }
  514. html.push('</table>');
  515. return html.join('');
  516. },
  517. /**
  518. * 获取复制数据Text格式(过滤不可见单元格)
  519. * @param {GC.Spread.Sheets.Worksheet} sheet
  520. * @returns {string}
  521. */
  522. getFilterCopyText: function (sheet) {
  523. const copyData = [];
  524. const sel = sheet.getSelections()[0];
  525. for(let i = sel.row, iLen = sel.row + sel.rowCount; i < iLen; i++) {
  526. // 跳过隐藏行
  527. if (!sheet.getCell(i, -1).visible()) { continue; }
  528. const rowText = [];
  529. for (let j = sel.col, jLen = sel.col + sel.colCount; j < jLen; j++) {
  530. const data = sheet.getText(i, j);
  531. rowText.push(data);
  532. }
  533. copyData.push(rowText.join('\t'));
  534. }
  535. return copyData.join('\n');
  536. },
  537. /**
  538. * 树表结构,定位至指定的节点
  539. * @param {GC.Spread.Sheets.Worksheet} sheet - 需要定位的sheet
  540. * @param {Number} id - 定位节点的id
  541. */
  542. locateTreeNode: function (sheet, id) {
  543. const tree = sheet.zh_tree;
  544. if (!tree) { return }
  545. const node = tree.getItems(id);
  546. if (!node) { return }
  547. const index = tree.nodes.indexOf(node);
  548. const sels = sheet.getSelections();
  549. sheet.setSelection(index, sels[0].col, 1, 1);
  550. sheet.showRow(index, spreadNS.VerticalPosition.center);
  551. },
  552. /**
  553. * 获取当前选行的数据对象
  554. * @param {GC.Spread.Sheets.Worksheet} sheet
  555. * @returns {Object}
  556. */
  557. getSelectObject: function (sheet) {
  558. if (!sheet) {
  559. return null;
  560. } else if (sheet.zh_dataType) {
  561. const sel = sheet.getSelections()[0];
  562. if (sheet.zh_dataType === this.DataType.Tree) {
  563. return sheet.zh_tree.nodes[sel.row];
  564. } else if (sheet.zh_dataType === this.DataType.Data) {
  565. return sheet.zh_data[sel.row];
  566. } else {
  567. return null;
  568. }
  569. }
  570. },
  571. /**
  572. * 刷新列显示
  573. * @param sheet
  574. */
  575. refreshColumnVisible: function (sheet) {
  576. if(sheet.zh_setting) {
  577. sheet.zh_setting.cols.forEach(function (col, index) {
  578. if (col.visible !== undefined && col.visible !== null) {
  579. sheet.setColumnVisible(index, col.visible);
  580. }
  581. });
  582. }
  583. },
  584. /**
  585. * 刷新行显示
  586. * @param sheet
  587. */
  588. refreshTreeRowVisible: function (sheet) {
  589. this.beginMassOperation(sheet);
  590. if (sheet.zh_tree) {
  591. for (const iRow in sheet.zh_tree.nodes) {
  592. const node = sheet.zh_tree.nodes[iRow];
  593. if (node.visible !== undefined && node.visible !== null) {
  594. sheet.setRowVisible(iRow, node.visible);
  595. } else {
  596. sheet.setRowVisible(iRow, true);
  597. }
  598. }
  599. }
  600. this.endMassOperation(sheet);
  601. },
  602. /**
  603. * 刷新列是否只读
  604. * @param sheet
  605. * @param field
  606. * @param readonly
  607. */
  608. resetFieldReadOnly: function (sheet, field, readonly) {
  609. const fields = field instanceof Array ? field : [field];
  610. if (sheet.zh_setting) {
  611. sheet.zh_setting.cols.forEach(function (col, i) {
  612. if (fields.indexOf(col.field) !== -1) {
  613. col.readOnly = readonly;
  614. sheet.getRange(-1, i, -1, 1).locked(col.readOnly || sheet.zh_setting.readOnly || false);
  615. }
  616. });
  617. }
  618. },
  619. CellType: {
  620. /**
  621. * 获取树结构CellType
  622. * 通过SpreadJsObj.loadSheetData(sheet, 'tree', tree)加载树结构数据
  623. * 要求tree类型为PathTree, 节点必须含有{id, pid, level, order, is_leaf}数据
  624. * @returns {TreeNodeCellType}
  625. */
  626. getTreeNodeCellType: function () {
  627. const indent = 20;
  628. const levelIndent = -5;
  629. const halfBoxLength = 5;
  630. const halfExpandLength = 3;
  631. /**
  632. * 画一条点线段
  633. * @param canvas - 画布
  634. * @param x1 - 线段起点 x
  635. * @param y1 - 线段起点 y
  636. * @param x2 - 线段终点 x
  637. * @param y2 - 线段终点 y
  638. * @param color - 线段颜色
  639. */
  640. const drawDotLine = function (canvas, x1, y1, x2, y2, color) {
  641. canvas.save();
  642. // 设置偏移量
  643. canvas.translate(0.5, 0.5);
  644. canvas.beginPath();
  645. canvas.strokeStyle = color;
  646. canvas.dottedLine(x1, y1, x2, y2);
  647. canvas.stroke();
  648. canvas.restore();
  649. };
  650. /**
  651. * 画一条线段
  652. * @param canvas - 画布
  653. * @param x1 - 线段起点 x
  654. * @param y1 - 线段起点 y
  655. * @param x2 - 线段终点 x
  656. * @param y2 - 线段终点 y
  657. * @param color - 线段颜色
  658. */
  659. const drawLine = function (canvas, x1, y1, x2, y2, color) {
  660. canvas.save();
  661. // 设置偏移量
  662. canvas.translate(0.5, 0.5);
  663. canvas.beginPath();
  664. canvas.moveTo(x1, y1);
  665. canvas.lineTo(x2, y2);
  666. canvas.strokeStyle = color;
  667. canvas.stroke();
  668. canvas.restore();
  669. };
  670. /**
  671. * 画一个方框
  672. * @param {Object} canvas - 画布
  673. * @param {Object} rect - 方框区域
  674. * @param {String} lineColor - 画线颜色
  675. * @param {String} fillColor - 填充颜色
  676. */
  677. const drawBox = function (canvas, rect, lineColor, fillColor) {
  678. canvas.save();
  679. // 设置偏移量
  680. canvas.translate(0.5, 0.5);
  681. canvas.strokeStyle = lineColor;
  682. canvas.beginPath();
  683. canvas.moveTo(rect.left, rect.top);
  684. canvas.lineTo(rect.left, rect.bottom);
  685. canvas.lineTo(rect.right, rect.bottom);
  686. canvas.lineTo(rect.right, rect.top);
  687. canvas.lineTo(rect.left, rect.top);
  688. canvas.stroke();
  689. canvas.fillStyle = fillColor;
  690. canvas.fill();
  691. canvas.restore();
  692. };
  693. /**
  694. * 画树结构-展开收起按钮
  695. * @param {Object} canvas - 画布
  696. * @param {Number} x - 单元格左顶点坐标 x
  697. * @param {Number} y - 单元格左顶点坐标 y
  698. * @param {Number} w - 单元格宽度
  699. * @param {Number} h - 单元格高度
  700. * @param {Number} centerX - 按钮中央坐标
  701. * @param {Number} centerY - 按钮中央坐标
  702. * @param {Boolean} expanded - 当前节点展开收起状态
  703. */
  704. const drawExpandBox = function (canvas, x, y, w, h, centerX, centerY, expanded) {
  705. let rect = {
  706. top: centerY - halfBoxLength,
  707. bottom: centerY + halfBoxLength,
  708. left: centerX - halfBoxLength,
  709. right: centerX + halfBoxLength
  710. };
  711. let h1, h2, offset = 1;
  712. if (rect.left < x + w) {
  713. // 方框超出单元格宽度时,超出部分不画。
  714. rect.right = Math.min(rect.right, x + w);
  715. drawBox(canvas, rect, '#808080', 'white');
  716. // 画中心十字
  717. // 画十字横线
  718. h1 = centerX - halfExpandLength;
  719. h2 = Math.min(centerX + halfExpandLength, x + w);
  720. if (h2 > h1) {
  721. drawLine(canvas, h1, centerY, h2, centerY, '#808080');
  722. }
  723. // 画十字竖线
  724. if (!expanded && (centerX < x + w)) {
  725. drawLine(canvas, centerX, centerY - halfExpandLength, centerX, centerY + halfExpandLength, '#808080');
  726. }
  727. }
  728. };
  729. let TreeNodeCellType = function (){};
  730. TreeNodeCellType.prototype = new spreadNS.CellTypes.Text();
  731. const proto = TreeNodeCellType.prototype;
  732. /**
  733. * 绘制方法
  734. * @param {Object} canvas - 画布
  735. * @param value - cell.value
  736. * @param {Number} x - 单元格左顶点坐标 x
  737. * @param {Number} y - 单元格左顶点坐标 y
  738. * @param {Number} w - 单元格宽度
  739. * @param {Number} h - 单元格高度
  740. * @param {Object} style - cell.style
  741. * @param {Object} options
  742. */
  743. proto.paint = function (canvas, value, x, y, w, h, style, options) {
  744. // 清理 画布--单元格范围 旧数据
  745. if (style.backColor) {
  746. canvas.save();
  747. canvas.fillStyle = style.backColor;
  748. canvas.fillRect(x, y, w, h);
  749. canvas.restore();
  750. } else {
  751. canvas.clearRect(x, y, w, h);
  752. }
  753. const tree = options.sheet.zh_tree;
  754. // 使用TreeCellType前,需定义sheet.tree
  755. if (tree) {
  756. const node = options.row < tree.nodes.length ? tree.nodes[options.row] : null;
  757. if (node) {
  758. const showTreeLine = true;
  759. const centerX = Math.floor(x) + (node.level) * indent + (node.level) * levelIndent + indent / 2;
  760. const centerY = Math.floor((y + (y + h)) / 2);
  761. // Draw Sibling Line
  762. if (showTreeLine) {
  763. // Draw Horizontal Line
  764. if (centerX < x + w) {
  765. const x1 = centerX + indent / 2;
  766. //drawLine(canvas, centerX, centerY, Math.min(x1, x + w), centerY, 'gray');
  767. drawDotLine(canvas, centerX, centerY, Math.min(x1, x + w), centerY, '#b8b8b8');
  768. }
  769. // Draw Vertical Line
  770. if (centerX < x + w) {
  771. const y1 = tree.isLastSibling(node) ? centerY : y + h;
  772. const parent = tree.getParent(node);
  773. const y2 = y1 - centerY;
  774. if (node.order === 1 && !parent) {
  775. //drawLine(canvas, centerX, centerY, centerX, y1, 'gray');
  776. drawDotLine(canvas, centerX, centerY, centerX, y1, '#b8b8b8');
  777. } else {
  778. //drawLine(canvas, centerX, y, centerX, y1, 'gray');
  779. drawDotLine(canvas, centerX, y, centerX, y1, '#b8b8b8');
  780. }
  781. }
  782. }
  783. // Draw Expand Box
  784. if (!node.is_leaf) {
  785. drawExpandBox(canvas, x, y, w, h, centerX, centerY, node.expanded);
  786. }
  787. // Draw Parent Line
  788. if (showTreeLine) {
  789. let parent = tree.getParent(node), parentCenterX = centerX - indent - levelIndent;
  790. while (parent) {
  791. if (!tree.isLastSibling(parent)) {
  792. if (parentCenterX < x + w) {
  793. //drawLine(canvas, parentCenterX, y, parentCenterX, y + h, 'gray');
  794. drawDotLine(canvas, parentCenterX, y, parentCenterX, y + h, '#b8b8b8');
  795. }
  796. }
  797. parent = tree.getParent(parent);
  798. parentCenterX -= (indent + levelIndent);
  799. }
  800. };
  801. // 重定位x
  802. const move = (node.level + 1) * indent + (node.level) * levelIndent;
  803. x = x + move;
  804. w = w - move;
  805. }
  806. }
  807. // Drawing Text
  808. spreadNS.CellTypes.Text.prototype.paint.apply(this, [canvas, value, x, y, w, h, style, options]);
  809. };
  810. /**
  811. * 获取点击信息
  812. * @param {Number} x
  813. * @param {Number} y
  814. * @param {Object} cellStyle
  815. * @param {Object} cellRect
  816. * @param {Object} context
  817. * @returns {{x: *, y: *, row: *, col: *|boolean|*[]|number|{}|UE.dom.dtd.col, cellStyle: *, cellRect: *, sheet: *|StyleSheet, sheetArea: *}}
  818. */
  819. proto.getHitInfo = function (x, y, cellStyle, cellRect, context) {
  820. return {
  821. x: x,
  822. y: y,
  823. row: context.row,
  824. col: context.col,
  825. cellStyle: cellStyle,
  826. cellRect: cellRect,
  827. sheet: context.sheet,
  828. sheetArea: context.sheetArea
  829. };
  830. };
  831. /**
  832. * 鼠标点击 树结构按钮 响应展开收起(未加载子节点时,先加载子节点)
  833. * @param {Object} hitinfo - 见getHitInfo
  834. */
  835. proto.processMouseDown = function (hitinfo) {
  836. const offset = -1;
  837. const tree = hitinfo.sheet.zh_tree;
  838. if (!tree) { return; }
  839. const node = tree.nodes[hitinfo.row];
  840. if (!node) { return; }
  841. let centerX = hitinfo.cellRect.x + offset + (node.level) * indent + (node.level) * levelIndent + indent / 2;
  842. let centerY = (hitinfo.cellRect.y + offset + (hitinfo.cellRect.y + offset + hitinfo.cellRect.height)) / 2;
  843. // 点击展开节点时,如果已加载子项,则展开,反之这加载子项,展开
  844. if (Math.abs(hitinfo.x - centerX) < halfBoxLength && Math.abs(hitinfo.y - centerY) < halfBoxLength) {
  845. const children = tree.getChildren(node);
  846. if (!node.expanded && !node.is_leaf && children.length === 0 && tree.loadChildren) {
  847. tree.loadChildren(node, function () {
  848. node.expanded = true;
  849. const children = tree.getChildren(node);
  850. hitinfo.sheet.addRows(hitinfo.row + 1, children.length);
  851. SpreadJsObj.reLoadRowData(hitinfo.sheet, hitinfo.row + 1, children.length);
  852. });
  853. } else {
  854. tree.setExpanded(node, !node.expanded);
  855. SpreadJsObj.massOperationSheet(hitinfo.sheet, function () {
  856. const posterity = tree.getPosterity(node);
  857. for (const child of posterity) {
  858. hitinfo.sheet.setRowVisible(tree.nodes.indexOf(child), child.visible, hitinfo.sheetArea);
  859. }
  860. });
  861. hitinfo.sheet.repaint();
  862. }
  863. }
  864. };
  865. return new TreeNodeCellType();
  866. },
  867. /**
  868. * 获取 带悬浮提示的CellType
  869. * @returns {TipCellType}
  870. */
  871. getTipCellType: function () {
  872. const TipCellType = function () {};
  873. // 继承 SpreadJs定义的 普通的TextCellType
  874. TipCellType.prototype = new spreadNS.CellTypes.Text();
  875. const proto = TipCellType.prototype;
  876. /**
  877. * 获取点击信息
  878. * @param {Number} x
  879. * @param {Number} y
  880. * @param {Object} cellStyle
  881. * @param {Object} cellRect
  882. * @param {Object} context
  883. * @returns {{x: *, y: *, row: *, col: *|boolean|*[]|number|{}|UE.dom.dtd.col, cellStyle: *, cellRect: *, sheet: *|StyleSheet, sheetArea: *}}
  884. */
  885. proto.getHitInfo = function (x, y, cellStyle, cellRect, context) {
  886. return {
  887. x: x,
  888. y: y,
  889. row: context.row,
  890. col: context.col,
  891. cellStyle: cellStyle,
  892. cellRect: cellRect,
  893. sheet: context.sheet,
  894. sheetArea: context.sheetArea
  895. };
  896. };
  897. /**
  898. * 鼠标进入单元格事件 - 显示悬浮提示
  899. * @param {Object} hitinfo - 见getHitInfo返回值
  900. */
  901. proto.processMouseEnter = function (hitinfo) {
  902. const text = hitinfo.sheet.getText(hitinfo.row, hitinfo.col);
  903. const setting = hitinfo.sheet.setting;
  904. if (setting.pos && text && text !== '') {
  905. if (!this._toolTipElement) {
  906. let div = $('#autoTip')[0];
  907. if (!div) {
  908. div = document.createElement("div");
  909. $(div).css("position", "absolute")
  910. .css("border", "1px #C0C0C0 solid")
  911. .css("box-shadow", "1px 2px 5px rgba(0,0,0,0.4)")
  912. .css("font", "9pt Arial")
  913. .css("background", "white")
  914. .css("padding", 5)
  915. .attr("id", 'autoTip');
  916. $(div).hide();
  917. document.body.insertBefore(div, null);
  918. }
  919. this._toolTipElement = div;
  920. $(this._toolTipElement).text(text).css("top", setting.pos.y + hitinfo.y + 15).css("left", setting.pos.x + hitinfo.x + 15);
  921. $(this._toolTipElement).show("fast");
  922. }
  923. }
  924. };
  925. /**
  926. * 鼠标移出单元格事件 - 隐藏悬浮提示
  927. * @param {Object} hitinfo - 见getHitInfo返回值
  928. */
  929. proto.processMouseLeave = function (hitinfo) {
  930. if (this._toolTipElement) {
  931. $(this._toolTipElement).hide();
  932. this._toolTipElement = null;
  933. }
  934. };
  935. return new TipCellType();
  936. },
  937. /**
  938. * 获取 带图片的cellType(图片需在document中定义好img,并写入col的img属性)
  939. *
  940. * img:
  941. * 1. 整列固定,则传入img的select
  942. * e.g. {title: '附件', field: 'attachment', cellType: 'image', img = '#attachment-img'}
  943. *
  944. * 2. 各单元格自定义,则
  945. * e.g. {title: '附件', field: 'attachment', cellType: 'image', img = getAttachmentImage}
  946. * function getAttachmentImage (data) {
  947. * $('#attachment-img').url = data.attachmentImageUrl;
  948. * return $('#attachment-img')[0];
  949. * }
  950. *
  951. * @returns {ImageCellType}
  952. */
  953. getImageCellType: function () {
  954. const indent = 10;
  955. const ImageCellType = function (){};
  956. ImageCellType.prototype = new spreadNS.CellTypes.Text();
  957. const proto = ImageCellType.prototype;
  958. proto.getImage = function (sheet, iRow, iCol) {
  959. const col = sheet.zh_setting.cols[iCol];
  960. let imgSource = col.img;
  961. if (imgSource && Object.prototype.toString.apply(imgSource) === "[object Function]") {
  962. const sortData = SpreadJsObj.getSortData(sheet);
  963. const data = sortData ? sortData[iRow] : null;
  964. return data ? imgSource(data) : null;
  965. } else {
  966. return $(imgSource)[0] ? $(imgSource)[0] : null;
  967. }
  968. };
  969. proto.paint = function (canvas, value, x, y, w, h, style, options) {
  970. const img = this.getImage(options.sheet, options.row, options.col);
  971. if (img) {
  972. if (style.backColor) {
  973. canvas.save();
  974. canvas.fillStyle = style.backColor;
  975. canvas.fillRect(x, y, indent + img.width, h);
  976. canvas.restore();
  977. }
  978. canvas.drawImage(img, x + 10, y + (h - img.height) / 2);
  979. if (style.hAlign !== spreadNS.HorizontalAlign.left) {
  980. style.hAlign = spreadNS.HorizontalAlign.left;
  981. }
  982. x = x + indent + img.width;
  983. w = w - indent - img.width;
  984. }
  985. // Drawing Text
  986. spreadNS.CellTypes.Text.prototype.paint.apply(this, [canvas, value, x, y, w, h, style, options]);
  987. };
  988. /**
  989. * 获取点击信息
  990. * @param {Number} x
  991. * @param {Number} y
  992. * @param {Object} cellStyle
  993. * @param {Object} cellRect
  994. * @param {Object} context
  995. * @returns {{x: *, y: *, row: *, col: *|boolean|*[]|number|{}|UE.dom.dtd.col, cellStyle: *, cellRect: *, sheet: *|StyleSheet, sheetArea: *}}
  996. */
  997. proto.getHitInfo = function (x, y, cellStyle, cellRect, context) {
  998. return {
  999. x: x,
  1000. y: y,
  1001. row: context.row,
  1002. col: context.col,
  1003. cellStyle: cellStyle,
  1004. cellRect: cellRect,
  1005. sheet: context.sheet,
  1006. sheetArea: context.sheetArea
  1007. };
  1008. };
  1009. /**
  1010. * 鼠标点击
  1011. * @param {Object} hitinfo - 见getHitInfo
  1012. */
  1013. proto.processMouseDown = function (hitinfo) {
  1014. const img = this.getImage(hitinfo.sheet, hitinfo.row, hitinfo.col);
  1015. if (img) {
  1016. const halfX = img.width / 2, halfY = img.height / 2;
  1017. const centerX = hitinfo.cellRect.x + indent + halfX;
  1018. const centerY = hitinfo.cellRect.y + hitinfo.cellRect.height / 2;
  1019. // 点击展开节点时,如果已加载子项,则展开,反之这加载子项,展开
  1020. if (Math.abs(hitinfo.x - centerX) < halfX && Math.abs(hitinfo.y - centerY) < halfY) {
  1021. const imageClick = hitinfo.sheet.zh_setting ? hitinfo.sheet.zh_setting.imageClick : null;
  1022. if (imageClick && Object.prototype.toString.apply(imageClick) === "[object Function]") {
  1023. const sortData = SpreadJsObj.getSortData(hitinfo.sheet);
  1024. const data = sortData ? sortData[hitinfo.row] : null;
  1025. imageClick(data);
  1026. }
  1027. }
  1028. }
  1029. };
  1030. return new ImageCellType();
  1031. },
  1032. /**
  1033. *
  1034. * 获取 带normal-hover-active按钮的cellType(需定义三张图片,须在document中定义好img,并写入col的normalImg, hoverImg, activeImg属性)
  1035. * 其中:normalImg必需,向下套用(不存在activeImg则使用hoverImg,不存在hoverImg则使用normalImg)
  1036. * 三个img均可像getImageCellType一样动态获取,参见getImageCellType注释
  1037. *
  1038. * @returns {ImageCellType}
  1039. */
  1040. getImageButtonCellType: function () {
  1041. let hover = 1, active = 2;
  1042. const indent = 10;
  1043. const ImageCellType = function (){};
  1044. ImageCellType.prototype = new spreadNS.CellTypes.Text();
  1045. const proto = ImageCellType.prototype;
  1046. proto.getImage = function (sheet, iRow, iCol) {
  1047. const col = sheet.zh_setting.cols[iCol];
  1048. let imgSource = col.normalImg;
  1049. const cell = sheet.getCell(iRow, iCol), tag = cell.tag();
  1050. if (tag === active) {
  1051. imgSource = col.activeImg ? col.activeImg : (col.hoverImg ? col.hoverImg : col.normalImg);
  1052. } else if (tag === hover) {
  1053. imgSource = col.hoverImg ? col.hoverImg : col.normalImg;
  1054. }
  1055. if (imgSource && Object.prototype.toString.apply(imgSource) === "[object Function]") {
  1056. const sortData = SpreadJsObj.getSortData(sheet);
  1057. const data = sortData ? sortData[iRow] : null;
  1058. return data ? imgSource(data) : null;
  1059. } else {
  1060. return $(imgSource)[0] ? $(imgSource)[0] : null;
  1061. }
  1062. };
  1063. proto.paint = function (canvas, value, x, y, w, h, style, options) {
  1064. const img = this.getImage(options.sheet, options.row, options.col);
  1065. if (img) {
  1066. if (style.backColor) {
  1067. canvas.save();
  1068. canvas.fillStyle = style.backColor;
  1069. canvas.fillRect(x, y, indent + img.width, h);
  1070. canvas.restore();
  1071. }
  1072. canvas.drawImage(img, x + 10, y + (h - img.height) / 2);
  1073. if (style.hAlign !== spreadNS.HorizontalAlign.left) {
  1074. style.hAlign = spreadNS.HorizontalAlign.left;
  1075. }
  1076. x = x + indent + img.width;
  1077. w = w - indent - img.width;
  1078. }
  1079. // Drawing Text
  1080. spreadNS.CellTypes.Text.prototype.paint.apply(this, [canvas, value, x, y, w, h, style, options]);
  1081. };
  1082. /**
  1083. * 获取点击信息
  1084. * @param {Number} x
  1085. * @param {Number} y
  1086. * @param {Object} cellStyle
  1087. * @param {Object} cellRect
  1088. * @param {Object} context
  1089. * @returns {{x: *, y: *, row: *, col: *|boolean|*[]|number|{}|UE.dom.dtd.col, cellStyle: *, cellRect: *, sheet: *|StyleSheet, sheetArea: *}}
  1090. */
  1091. proto.getHitInfo = function (x, y, cellStyle, cellRect, context) {
  1092. return {
  1093. x: x,
  1094. y: y,
  1095. row: context.row,
  1096. col: context.col,
  1097. cellStyle: cellStyle,
  1098. cellRect: cellRect,
  1099. sheet: context.sheet,
  1100. sheetArea: context.sheetArea
  1101. };
  1102. };
  1103. /**
  1104. * 鼠标点击
  1105. * @param {Object} hitinfo - 见getHitInfo
  1106. */
  1107. proto.processMouseEnter = function (hitinfo) {
  1108. const col = hitinfo.sheet.zh_setting.cols[hitinfo.col];
  1109. // Drawing Image
  1110. if (col.hoverImg) {
  1111. const cell = hitinfo.sheet.getCell(hitinfo.row, hitinfo.col);
  1112. cell.tag(hover);
  1113. hitinfo.sheet.repaint(hitinfo.cellRect);
  1114. }
  1115. };
  1116. proto.processMouseLeave = function (hitinfo) {
  1117. const col = hitinfo.sheet.zh_setting.cols[hitinfo.col];
  1118. // Drawing Image
  1119. if (col.hoverImg) {
  1120. const cell = hitinfo.sheet.getCell(hitinfo.row, hitinfo.col);
  1121. cell.tag(null);
  1122. hitinfo.sheet.repaint(hitinfo.cellRect);
  1123. }
  1124. };
  1125. proto.processMouseDown = function (hitinfo) {
  1126. const col = hitinfo.sheet.zh_setting.cols[hitinfo.col];
  1127. if (col.activeImg) {
  1128. const cell = hitinfo.sheet.getCell(hitinfo.row, hitinfo.col);
  1129. cell.tag(active);
  1130. hitinfo.sheet.repaint(hitinfo.cellRect);
  1131. }
  1132. };
  1133. proto.processMouseUp = function (hitinfo) {
  1134. const imageClick = hitinfo.sheet.zh_setting ? hitinfo.sheet.zh_setting.imageClick : null;
  1135. if (imageClick && Object.prototype.toString.apply(imageClick) === "[object Function]") {
  1136. const sortData = SpreadJsObj.getSortData(hitinfo.sheet);
  1137. const data = sortData ? sortData[hitinfo.row] : null;
  1138. imageClick(data);
  1139. const cell = hitinfo.sheet.getCell(hitinfo.row, hitinfo.col);
  1140. cell.tag(null);
  1141. hitinfo.sheet.repaint(hitinfo.cellRect);
  1142. }
  1143. };
  1144. /*
  1145. 注释部分以进入鼠标进入图片,点击图片为基准更新图片,鼠标快速移动时,可能失效
  1146. */
  1147. // proto.processMouseDown = function (hitinfo) {
  1148. // const img = this.getImage(hitinfo.sheet, hitinfo.row, hitinfo.col);
  1149. // const halfX = img.width / 2, halfY = img.height / 2;
  1150. // const centerX = hitinfo.cellRect.x + indent + halfX;
  1151. // const centerY = hitinfo.cellRect.y + hitinfo.cellRect.height / 2;
  1152. //
  1153. // if (Math.abs(hitinfo.x - centerX) < halfX && Math.abs(hitinfo.y - centerY) < halfY) {
  1154. // const cell = hitinfo.sheet.getCell(hitinfo.row, hitinfo.col);
  1155. // cell.tag(down);
  1156. // hitinfo.sheet.repaint(hitinfo.cellRect);
  1157. // }
  1158. // };
  1159. // proto.processMouseUp = function (hitinfo) {
  1160. // const img = this.getImage(hitinfo.sheet, hitinfo.row, hitinfo.col);
  1161. // const halfX = img.width / 2, halfY = img.height / 2;
  1162. // const centerX = hitinfo.cellRect.x + indent + halfX;
  1163. // const centerY = hitinfo.cellRect.y + hitinfo.cellRect.height / 2;
  1164. //
  1165. // // 点击展开节点时,如果已加载子项,则展开,反之这加载子项,展开
  1166. // if (Math.abs(hitinfo.x - centerX) < halfX && Math.abs(hitinfo.y - centerY) < halfY) {
  1167. // const imageClick = hitinfo.sheet.zh_setting ? hitinfo.sheet.zh_setting.imageClick : null;
  1168. // if (imageClick && Object.prototype.toString.apply(imageClick) === "[object Function]") {
  1169. // const sortData = SpreadJsObj.getSortData(hitinfo.sheet);
  1170. // const data = sortData ? sortData[hitinfo.row] : null;
  1171. // imageClick(data);
  1172. // const cell = hitinfo.sheet.getCell(hitinfo.row, hitinfo.col);
  1173. // cell.tag(null);
  1174. // hitinfo.sheet.repaint(hitinfo.cellRect);
  1175. // }
  1176. // }
  1177. // };
  1178. // proto.processMouseMove = function (hitinfo) {
  1179. // const img = this.getImage(hitinfo.sheet, hitinfo.row, hitinfo.col);
  1180. // const halfX = img.width / 2, halfY = img.height / 2;
  1181. // const centerX = hitinfo.cellRect.x + indent + halfX;
  1182. // const centerY = hitinfo.cellRect.y + hitinfo.cellRect.height / 2;
  1183. // const cell = hitinfo.sheet.getCell(hitinfo.row, hitinfo.col);
  1184. // if (Math.abs(hitinfo.x - centerX) < halfX && Math.abs(hitinfo.y - centerY) < halfY) {
  1185. // if (cell.tag() !== hover) {
  1186. // cell.tag(hover);
  1187. // hitinfo.sheet.repaint(hitinfo.cellRect);
  1188. // }
  1189. // } else {
  1190. // if (cell.tag() === hover) {
  1191. // cell.tag(null);
  1192. // hitinfo.sheet.repaint(hitinfo.cellRect);
  1193. // }
  1194. // }
  1195. // };
  1196. return new ImageCellType();
  1197. },
  1198. /**
  1199. * 获取 嵌入Html的cellType
  1200. * @returns {HTMLCellType}
  1201. */
  1202. getHtmlCellType: function () {
  1203. const HTMLCellType = function (){};
  1204. HTMLCellType.prototype = new spreadNS.CellTypes.Text;
  1205. const proto = ImageCellType.prototype;
  1206. proto.paint = function (ctx, value, x, y, w, h, style, context) {
  1207. let DOMURL = window.URL || window.webkitURL || window;
  1208. let cell = context.sheet.getCell(context.row, context.col);
  1209. let img = cell.tag();
  1210. if (img) {
  1211. try {
  1212. ctx.save();
  1213. ctx.rect(x, y, w, h);
  1214. ctx.clip();
  1215. ctx.drawImage(img, x + 2, y + 2)
  1216. ctx.restore();
  1217. cell.tag(null);
  1218. return;
  1219. }
  1220. catch (err) {
  1221. GC.Spread.Sheets.CustomCellType.prototype.paint.apply(this, [ctx, "#HTMLError", x, y, w, h, style, context])
  1222. cell.tag(null);
  1223. return;
  1224. }
  1225. }
  1226. let svgPattern = '<svg xmlns="http://www.w3.org/2000/svg" width="{0}" height="{1}">' +
  1227. '<foreignObject width="100%" height="100%"><div xmlns="http://www.w3.org/1999/xhtml" style="font:{2}">{3}</div></foreignObject></svg>';
  1228. let data = svgPattern.replace("{0}", w).replace("{1}", h).replace("{2}", style.font).replace("{3}", value);
  1229. let doc = document.implementation.createHTMLDocument("");
  1230. doc.write(data);
  1231. // Get well-formed markup
  1232. data = (new XMLSerializer()).serializeToString(doc.body.children[0]);
  1233. img = new Image();
  1234. //var svg = new Blob([data], {type: 'image/svg+xml;charset=utf-8'});
  1235. //var url = DOMURL.createObjectURL(svg);
  1236. //img.src = url;
  1237. img.src = 'data:image/svg+xml;base64,' + window.btoa(data);
  1238. cell.tag(img);
  1239. img.onload = function () {
  1240. context.sheet.repaint(new GC.Spread.Sheets.Rect(x, y, w, h));
  1241. }
  1242. };
  1243. return new HTMLCellType();
  1244. },
  1245. /**
  1246. * 获取 字符超长缩略的cellType
  1247. * @returns {EllipsisTextCellType}
  1248. */
  1249. getEllipsisTextCellType: function () {
  1250. const EllipsisTextCellType = function (){};
  1251. EllipsisTextCellType.prototype = new spreadNS.CellTypes.Text;
  1252. const proto = EllipsisTextCellType.prototype;
  1253. const getEllipsisText = function(c, str, maxWidth) {
  1254. var width = c.measureText(str).width;
  1255. var ellipsis = '…';
  1256. var ellipsisWidth = c.measureText(ellipsis).width;
  1257. if (width <= maxWidth || width <= ellipsisWidth) {
  1258. return str;
  1259. } else {
  1260. var len = str.length;
  1261. while (width >= maxWidth - ellipsisWidth && len-- > 0) {
  1262. str = str.substring(0, len);
  1263. width = c.measureText(str).width;
  1264. }
  1265. return str + ellipsis;
  1266. }
  1267. };
  1268. proto.paint = function (ctx, value, x, y, w, h, style, context) {
  1269. ctx.font = style.font;
  1270. value = getEllipsisText(ctx, value, w - 2);
  1271. spreadNS.CellTypes.Text.prototype.paint.apply(this, [ctx, value, x, y, w, h, style, context]);
  1272. };
  1273. return new EllipsisTextCellType();
  1274. },
  1275. }
  1276. };