spreadjs_zh.js 57 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324
  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 ImageCellType = function (){};
  1043. ImageCellType.prototype = new spreadNS.CellTypes.Text();
  1044. const proto = ImageCellType.prototype;
  1045. proto.getImage = function (sheet, iRow, iCol) {
  1046. const col = sheet.zh_setting.cols[iCol];
  1047. let imgSource = col.normalImg;
  1048. const cell = sheet.getCell(iRow, iCol), tag = cell.tag();
  1049. if (tag === active) {
  1050. imgSource = col.activeImg ? col.activeImg : (col.hoverImg ? col.hoverImg : col.normalImg);
  1051. } else if (tag === hover) {
  1052. imgSource = col.hoverImg ? col.hoverImg : col.normalImg;
  1053. }
  1054. if (imgSource && Object.prototype.toString.apply(imgSource) === "[object Function]") {
  1055. const sortData = SpreadJsObj.getSortData(sheet);
  1056. const data = sortData ? sortData[iRow] : null;
  1057. return data ? imgSource(data) : null;
  1058. } else {
  1059. return $(imgSource)[0] ? $(imgSource)[0] : null;
  1060. }
  1061. };
  1062. proto.paint = function (canvas, value, x, y, w, h, style, options) {
  1063. const img = this.getImage(options.sheet, options.row, options.col);
  1064. const col = options.sheet.zh_setting.cols[options.col];
  1065. const indent = col.indent ? col.indent : 10;
  1066. if (style.hAlign === spreadNS.HorizontalAlign.right) {
  1067. if (img) {
  1068. if (style.backColor) {
  1069. canvas.save();
  1070. canvas.fillStyle = style.backColor;
  1071. canvas.fillRect(x + w - indent - img.width, y, img.width, h);
  1072. canvas.restore();
  1073. }
  1074. canvas.drawImage(img, x + w - indent - img.width, y + (h - img.height) / 2);
  1075. w = w - indent - img.width;
  1076. }
  1077. // Drawing Text
  1078. spreadNS.CellTypes.Text.prototype.paint.apply(this, [canvas, value, x, y, w, h, style, options]);
  1079. } else {
  1080. if (img) {
  1081. if (style.backColor) {
  1082. canvas.save();
  1083. canvas.fillStyle = style.backColor;
  1084. canvas.fillRect(x, y, indent + img.width, h);
  1085. canvas.restore();
  1086. }
  1087. canvas.drawImage(img, x + 10, y + (h - img.height) / 2);
  1088. if (style.hAlign !== spreadNS.HorizontalAlign.left) {
  1089. style.hAlign = spreadNS.HorizontalAlign.left;
  1090. }
  1091. x = x + indent + img.width;
  1092. w = w - indent - img.width;
  1093. }
  1094. // Drawing Text
  1095. spreadNS.CellTypes.Text.prototype.paint.apply(this, [canvas, value, x, y, w, h, style, options]);
  1096. }
  1097. };
  1098. /**
  1099. * 获取点击信息
  1100. * @param {Number} x
  1101. * @param {Number} y
  1102. * @param {Object} cellStyle
  1103. * @param {Object} cellRect
  1104. * @param {Object} context
  1105. * @returns {{x: *, y: *, row: *, col: *|boolean|*[]|number|{}|UE.dom.dtd.col, cellStyle: *, cellRect: *, sheet: *|StyleSheet, sheetArea: *}}
  1106. */
  1107. proto.getHitInfo = function (x, y, cellStyle, cellRect, context) {
  1108. return {
  1109. x: x,
  1110. y: y,
  1111. row: context.row,
  1112. col: context.col,
  1113. cellStyle: cellStyle,
  1114. cellRect: cellRect,
  1115. sheet: context.sheet,
  1116. sheetArea: context.sheetArea
  1117. };
  1118. };
  1119. /**
  1120. * 鼠标点击
  1121. * @param {Object} hitinfo - 见getHitInfo
  1122. */
  1123. proto.processMouseEnter = function (hitinfo) {
  1124. const col = hitinfo.sheet.zh_setting.cols[hitinfo.col];
  1125. // Drawing Image
  1126. if (col.hoverImg) {
  1127. const cell = hitinfo.sheet.getCell(hitinfo.row, hitinfo.col);
  1128. cell.tag(hover);
  1129. hitinfo.sheet.repaint(hitinfo.cellRect);
  1130. }
  1131. };
  1132. proto.processMouseLeave = function (hitinfo) {
  1133. const col = hitinfo.sheet.zh_setting.cols[hitinfo.col];
  1134. // Drawing Image
  1135. if (col.hoverImg) {
  1136. const cell = hitinfo.sheet.getCell(hitinfo.row, hitinfo.col);
  1137. cell.tag(null);
  1138. hitinfo.sheet.repaint(hitinfo.cellRect);
  1139. }
  1140. };
  1141. proto.processMouseDown = function (hitinfo) {
  1142. const col = hitinfo.sheet.zh_setting.cols[hitinfo.col];
  1143. if (col.activeImg) {
  1144. const cell = hitinfo.sheet.getCell(hitinfo.row, hitinfo.col);
  1145. cell.tag(active);
  1146. hitinfo.sheet.repaint(hitinfo.cellRect);
  1147. }
  1148. };
  1149. proto.processMouseUp = function (hitinfo) {
  1150. const imageClick = hitinfo.sheet.zh_setting ? hitinfo.sheet.zh_setting.imageClick : null;
  1151. if (imageClick && Object.prototype.toString.apply(imageClick) === "[object Function]") {
  1152. const sortData = SpreadJsObj.getSortData(hitinfo.sheet);
  1153. const data = sortData ? sortData[hitinfo.row] : null;
  1154. imageClick(data);
  1155. const cell = hitinfo.sheet.getCell(hitinfo.row, hitinfo.col);
  1156. cell.tag(null);
  1157. hitinfo.sheet.repaint(hitinfo.cellRect);
  1158. }
  1159. };
  1160. /*
  1161. 注释部分以进入鼠标进入图片,点击图片为基准更新图片,鼠标快速移动时,可能失效
  1162. */
  1163. // proto.processMouseDown = function (hitinfo) {
  1164. // const img = this.getImage(hitinfo.sheet, hitinfo.row, hitinfo.col);
  1165. // const halfX = img.width / 2, halfY = img.height / 2;
  1166. // const centerX = hitinfo.cellRect.x + indent + halfX;
  1167. // const centerY = hitinfo.cellRect.y + hitinfo.cellRect.height / 2;
  1168. //
  1169. // if (Math.abs(hitinfo.x - centerX) < halfX && Math.abs(hitinfo.y - centerY) < halfY) {
  1170. // const cell = hitinfo.sheet.getCell(hitinfo.row, hitinfo.col);
  1171. // cell.tag(down);
  1172. // hitinfo.sheet.repaint(hitinfo.cellRect);
  1173. // }
  1174. // };
  1175. // proto.processMouseUp = function (hitinfo) {
  1176. // const img = this.getImage(hitinfo.sheet, hitinfo.row, hitinfo.col);
  1177. // const halfX = img.width / 2, halfY = img.height / 2;
  1178. // const centerX = hitinfo.cellRect.x + indent + halfX;
  1179. // const centerY = hitinfo.cellRect.y + hitinfo.cellRect.height / 2;
  1180. //
  1181. // // 点击展开节点时,如果已加载子项,则展开,反之这加载子项,展开
  1182. // if (Math.abs(hitinfo.x - centerX) < halfX && Math.abs(hitinfo.y - centerY) < halfY) {
  1183. // const imageClick = hitinfo.sheet.zh_setting ? hitinfo.sheet.zh_setting.imageClick : null;
  1184. // if (imageClick && Object.prototype.toString.apply(imageClick) === "[object Function]") {
  1185. // const sortData = SpreadJsObj.getSortData(hitinfo.sheet);
  1186. // const data = sortData ? sortData[hitinfo.row] : null;
  1187. // imageClick(data);
  1188. // const cell = hitinfo.sheet.getCell(hitinfo.row, hitinfo.col);
  1189. // cell.tag(null);
  1190. // hitinfo.sheet.repaint(hitinfo.cellRect);
  1191. // }
  1192. // }
  1193. // };
  1194. // proto.processMouseMove = function (hitinfo) {
  1195. // const img = this.getImage(hitinfo.sheet, hitinfo.row, hitinfo.col);
  1196. // const halfX = img.width / 2, halfY = img.height / 2;
  1197. // const centerX = hitinfo.cellRect.x + indent + halfX;
  1198. // const centerY = hitinfo.cellRect.y + hitinfo.cellRect.height / 2;
  1199. // const cell = hitinfo.sheet.getCell(hitinfo.row, hitinfo.col);
  1200. // if (Math.abs(hitinfo.x - centerX) < halfX && Math.abs(hitinfo.y - centerY) < halfY) {
  1201. // if (cell.tag() !== hover) {
  1202. // cell.tag(hover);
  1203. // hitinfo.sheet.repaint(hitinfo.cellRect);
  1204. // }
  1205. // } else {
  1206. // if (cell.tag() === hover) {
  1207. // cell.tag(null);
  1208. // hitinfo.sheet.repaint(hitinfo.cellRect);
  1209. // }
  1210. // }
  1211. // };
  1212. return new ImageCellType();
  1213. },
  1214. /**
  1215. * 获取 嵌入Html的cellType
  1216. * @returns {HTMLCellType}
  1217. */
  1218. getHtmlCellType: function () {
  1219. const HTMLCellType = function (){};
  1220. HTMLCellType.prototype = new spreadNS.CellTypes.Text;
  1221. const proto = ImageCellType.prototype;
  1222. proto.paint = function (ctx, value, x, y, w, h, style, context) {
  1223. let DOMURL = window.URL || window.webkitURL || window;
  1224. let cell = context.sheet.getCell(context.row, context.col);
  1225. let img = cell.tag();
  1226. if (img) {
  1227. try {
  1228. ctx.save();
  1229. ctx.rect(x, y, w, h);
  1230. ctx.clip();
  1231. ctx.drawImage(img, x + 2, y + 2)
  1232. ctx.restore();
  1233. cell.tag(null);
  1234. return;
  1235. }
  1236. catch (err) {
  1237. GC.Spread.Sheets.CustomCellType.prototype.paint.apply(this, [ctx, "#HTMLError", x, y, w, h, style, context])
  1238. cell.tag(null);
  1239. return;
  1240. }
  1241. }
  1242. let svgPattern = '<svg xmlns="http://www.w3.org/2000/svg" width="{0}" height="{1}">' +
  1243. '<foreignObject width="100%" height="100%"><div xmlns="http://www.w3.org/1999/xhtml" style="font:{2}">{3}</div></foreignObject></svg>';
  1244. let data = svgPattern.replace("{0}", w).replace("{1}", h).replace("{2}", style.font).replace("{3}", value);
  1245. let doc = document.implementation.createHTMLDocument("");
  1246. doc.write(data);
  1247. // Get well-formed markup
  1248. data = (new XMLSerializer()).serializeToString(doc.body.children[0]);
  1249. img = new Image();
  1250. //var svg = new Blob([data], {type: 'image/svg+xml;charset=utf-8'});
  1251. //var url = DOMURL.createObjectURL(svg);
  1252. //img.src = url;
  1253. img.src = 'data:image/svg+xml;base64,' + window.btoa(data);
  1254. cell.tag(img);
  1255. img.onload = function () {
  1256. context.sheet.repaint(new GC.Spread.Sheets.Rect(x, y, w, h));
  1257. }
  1258. };
  1259. return new HTMLCellType();
  1260. },
  1261. /**
  1262. * 获取 字符超长缩略的cellType
  1263. * @returns {EllipsisTextCellType}
  1264. */
  1265. getEllipsisTextCellType: function () {
  1266. const EllipsisTextCellType = function (){};
  1267. EllipsisTextCellType.prototype = new spreadNS.CellTypes.Text;
  1268. const proto = EllipsisTextCellType.prototype;
  1269. const getEllipsisText = function(c, str, maxWidth) {
  1270. var width = c.measureText(str).width;
  1271. var ellipsis = '…';
  1272. var ellipsisWidth = c.measureText(ellipsis).width;
  1273. if (width <= maxWidth || width <= ellipsisWidth) {
  1274. return str;
  1275. } else {
  1276. var len = str.length;
  1277. while (width >= maxWidth - ellipsisWidth && len-- > 0) {
  1278. str = str.substring(0, len);
  1279. width = c.measureText(str).width;
  1280. }
  1281. return str + ellipsis;
  1282. }
  1283. };
  1284. proto.paint = function (ctx, value, x, y, w, h, style, context) {
  1285. ctx.font = style.font;
  1286. value = getEllipsisText(ctx, value, w - 2);
  1287. spreadNS.CellTypes.Text.prototype.paint.apply(this, [ctx, value, x, y, w, h, style, context]);
  1288. };
  1289. return new EllipsisTextCellType();
  1290. },
  1291. }
  1292. };