spreadjs_zh.js 84 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887
  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. // 授权码
  31. spreadNS.LicenseKey = "559432293813965#A0y3iTOzEDOzkjMyMDN9UTNiojIklkI1pjIEJCLi4TPB9mM5AFNTd4cvZ7SaJUVy3CWKtWYXx4VVhjMpp7dYNGdx2ia9sEVlZGOTh7NRlTUwkWR9wEV4gmbjBDZ4ElR8N7cGdHVvEWVBtCOwIGW0ZmeYVWVr3mI0IyUiwCMzETN8kzNzYTM0IicfJye&Qf35VfiEzRwEkI0IyQiwiIwEjL6ByUKBCZhVmcwNlI0IiTis7W0ICZyBlIsIyNyMzM5ADI5ADNwcTMwIjI0ICdyNkIsIibj9SbvNmL4N7bjRnch56ciojIz5GRiwiI8+Y9sWY9QmZ0Jyp96uL9v6L0wap9biY9qiq95q197Wr9g+89iojIh94Wiqi";
  32. // SpreadJs常用方法
  33. const SpreadJsObj = {
  34. initSpreadSettingEvents: function (setting, events) {
  35. const getEvent = function (eventName) {
  36. const names = eventName.split('.');
  37. let event = events;
  38. for (let name of names) {
  39. if (event[name]) {
  40. event = event[name];
  41. } else {
  42. return null;
  43. }
  44. }
  45. if (event && Object.prototype.toString.apply(event) !== "[object Function]") {
  46. return null;
  47. } else {
  48. return event;
  49. }
  50. };
  51. for (const col of setting.cols) {
  52. if (col.readOnly && Object.prototype.toString.apply(col.readOnly) === "[object String]") {
  53. col.readOnly = getEvent(col.readOnly);
  54. }
  55. if (col.getValue && Object.prototype.toString.apply(col.getValue) === "[object String]") {
  56. col.getValue = getEvent(col.getValue);
  57. }
  58. }
  59. },
  60. DataType: {
  61. Data: 'data',
  62. Tree: 'tree',
  63. },
  64. /**
  65. * 创建Spread(默认1张表,3行数据)
  66. * @param obj 用于创建spreadjs的Dom元素
  67. * @returns {GC.Spread.Sheets.Workbook}
  68. */
  69. createNewSpread: function (obj) {
  70. const spread = new spreadNS.Workbook(obj, {sheetCount: 1});
  71. spread.options.tabStripVisible = false;
  72. spread.options.scrollbarMaxAlign = true;
  73. spread.options.cutCopyIndicatorVisible = false;
  74. spread.options.allowCopyPasteExcelStyle = false;
  75. spread.options.allowUserDragDrop = false;
  76. spread.options.allowUserDragFill = false;
  77. spread.options.showDragFillSmartTag = false;
  78. spread.options.allowUserEditFormula = false;
  79. spread.options.allowExtendPasteRange = true;
  80. spread.options.copyPasteHeaderOptions = spreadNS.CopyPasteHeaderOptions.noHeaders;
  81. spread.getActiveSheet().options.clipBoardOptions = spreadNS.ClipboardPasteOptions.values;//设置粘贴时只粘贴值
  82. spread.getActiveSheet().setRowCount(3);
  83. return spread;
  84. },
  85. addCutEvents: function (spread, fun) {
  86. const cut = spreadNS.Commands.copy.execute;
  87. spreadNS.Commands.cut.execute = function (context, options, isUndo) {
  88. const sheet = context.getActiveSheet();
  89. const sels = sheet.getSelections();
  90. const sel = sels ? sels[0] : null;
  91. if (sel) {
  92. return fun(sheet, sel, function () {
  93. cut(context, options, isUndo);
  94. });
  95. }
  96. }
  97. },
  98. /**
  99. * 保护sheet(需设置保护后, 单元格的locked等属性方可生效)
  100. * @param {GC.Spread.Sheets.Worksheet} sheet
  101. */
  102. protectedSheet: function (sheet) {
  103. const option = {
  104. allowSelectLockedCells: true,
  105. allowSelectUnlockedCells: true,
  106. allowResizeRows: true,
  107. allowResizeColumns: true
  108. };
  109. const defaultStyle = sheet.getDefaultStyle();
  110. defaultStyle.locked = false;
  111. sheet.setDefaultStyle(defaultStyle);
  112. sheet.options.protectionOptions = option;
  113. sheet.options.isProtected = true;
  114. sheet.options.allowCellOverflow = false;
  115. },
  116. /**
  117. * sheet批量操作优化(sheet操作大批量数据时, 屏蔽数据刷新, 可优化大量时间)
  118. * @param {GC.Spread.Sheets.Worksheet} sheet
  119. * @param {function} operation
  120. */
  121. beginMassOperation: function (sheet) {
  122. sheet.suspendPaint();
  123. sheet.suspendEvent();
  124. },
  125. endMassOperation: function (sheet) {
  126. sheet.resumeEvent();
  127. sheet.resumePaint();
  128. },
  129. massOperationSheet: function (sheet, operation) {
  130. this.beginMassOperation(sheet);
  131. operation();
  132. this.endMassOperation(sheet);
  133. },
  134. /**
  135. * 获取Obj左顶点位置(部分功能需通过spreadjs左顶点位置计算)
  136. * @param obj
  137. * @returns {{x: number, y: number}}
  138. */
  139. getObjPos: function (obj) {
  140. if (!obj) return null;
  141. let target = obj;
  142. let pos = {x: obj.offsetLeft, y: obj.offsetTop};
  143. target = obj.offsetParent;
  144. while (target) {
  145. pos.x += target.offsetLeft;
  146. pos.y += target.offsetTop;
  147. target = target.offsetParent;
  148. }
  149. return pos;
  150. },
  151. /**
  152. * 以下四个方法来自Spread示例, 参见官网或文档
  153. */
  154. getHitTest: function (obj, e, sheet) {
  155. var offset = obj.offset(),
  156. x = e.pageX - offset.left,
  157. y = e.pageY - offset.top;
  158. return sheet.hitTest(x, y);
  159. },
  160. getTargetSelection: function (sheet, target) {
  161. if (target.hitTestType === spreadNS.SheetArea.colHeader) {
  162. return sheet.getRange(-1, target.col, sheet.getRowCount(), 1);
  163. } else if (target.hitTestType === spreadNS.SheetArea.rowHeader) {
  164. return sheet.getRange(target.row, -1, 1, sheet.getColumnCount());
  165. } else if (target.hitTestType === spreadNS.SheetArea.viewport) {
  166. return sheet.getRange(target.row, target.col, 1, 1);
  167. } else if (target.hitTestType === spreadNS.SheetArea.corner) {
  168. return sheet.getRange(-1, -1, sheet.getRowCount(), sheet.getColumnCount());
  169. };
  170. },
  171. getCellInSelections: function (selections, row, col) {
  172. const count = selections.length;
  173. let range;
  174. for (var i = 0; i < count; i++) {
  175. range = selections[i];
  176. if (range.contains(row, col)) {
  177. return range;
  178. }
  179. }
  180. return null;
  181. },
  182. checkTargetInSelection: function (selections, range) {
  183. var count = selections.length, sel;
  184. for (var i = 0; i < count; i++) {
  185. sel = selections[i];
  186. if (sel.containsRange(range)) {
  187. return true;
  188. }
  189. }
  190. return false;
  191. },
  192. /**
  193. * 获取 spread 在鼠标右键时, spread的选中区域
  194. * viewport, 选中鼠标点击单元格X, X不在原选中区域内, 则选中X
  195. * colHeader, 选中整列
  196. * rowHeader, 选中整行
  197. * corner, 全选
  198. * 该方法返回不符合需求时,可通过getHitTest/getTargetSelection/getCellInSelections/checkTargetInSelection来确定鼠标右键点击时,spread当前Sheet应选中的单元格
  199. * @param obj: 创建Spread的Dom元素(jquery-contextmenu.build方法的第一个变量可读取)
  200. * @param e: jquery-contextmenu.build方法的第二个变量
  201. * @param {GC.Spread.Sheets.Workbook} spread
  202. * @returns {*}
  203. */
  204. safeRightClickSelection: function (obj, e, spread) {
  205. const sheet = spread.getActiveSheet();
  206. const selections = sheet.getSelections(), target = this.getHitTest(obj, e, sheet), range = this.getTargetSelection(sheet, target);
  207. if (!this.checkTargetInSelection(selections, range)) {
  208. sheet.setSelection(range.row, range.col, range.rowCount, range.colCount);
  209. }
  210. return target;
  211. },
  212. /**
  213. * 禁用右键菜单
  214. * @param selector
  215. * @param spread
  216. */
  217. forbiddenSpreadContextMenu: function (selector, spread) {
  218. $.contextMenu({
  219. selector: selector,
  220. build: function ($trigger, e) {
  221. const target = SpreadJsObj.safeRightClickSelection($trigger, e, spread);
  222. return target.hitTestType === spreadNS.SheetArea.viewport || target.hitTestType === spreadNS.SheetArea.rowHeader;
  223. }
  224. });
  225. },
  226. /**
  227. * 获取写入sheet的数据序列
  228. * data:sheet.zh_data, tree: sheet.zh_tree.nodes
  229. * @param sheet
  230. * @returns {*}
  231. */
  232. getSortData: function (sheet) {
  233. if (sheet.zh_dataType) {
  234. if (sheet.zh_dataType === this.DataType.Data) {
  235. return sheet.zh_data;
  236. } else if (sheet.zh_dataType === this.DataType.Tree) {
  237. return sheet.zh_tree.nodes;
  238. } else {
  239. return null;
  240. }
  241. } else {
  242. return null;
  243. }
  244. },
  245. /**
  246. * sheet中 使用delete键,触发EndEdited事件
  247. * @param {GC.Spreads.Sheets.Workbook} spread
  248. * @param {function} fun
  249. */
  250. addDeleteBind: function (spread, fun) {
  251. spread.commandManager().register('deleteEvent', function () {
  252. fun(spread.getActiveSheet());
  253. });
  254. spread.commandManager().setShortcutKey(null, GC.Spread.Commands.Key.del, false, false, false, false);
  255. spread.commandManager().setShortcutKey('deleteEvent', GC.Spread.Commands.Key.del, false, false, false, false);
  256. },
  257. _initSheetDeafult: function (sheet) {
  258. if (sheet.zh_setting.headerFont) {
  259. const vStyle = new spreadNS.Style();
  260. vStyle.font = sheet.zh_setting.headerFont;
  261. vStyle.locked = false;
  262. sheet.setDefaultStyle(vStyle, spreadNS.SheetArea.colHeader);
  263. }
  264. if (sheet.zh_setting.font) {
  265. const vStyle = new spreadNS.Style();
  266. vStyle.font = sheet.zh_setting.font;
  267. vStyle.hAlign = 1;
  268. vStyle.locked = false;
  269. sheet.setDefaultStyle(vStyle, spreadNS.SheetArea.rowHeader);
  270. }
  271. if (sheet.zh_setting.font) {
  272. const vStyle = new spreadNS.Style();
  273. vStyle.font = sheet.zh_setting.font;
  274. vStyle.locked = false;
  275. sheet.setDefaultStyle(vStyle, spreadNS.SheetArea.viewport);
  276. }
  277. },
  278. /**
  279. * 根据sheet.zh_setting初始化sheet表头
  280. * @param {GC.Spread.Sheets.Worksheet} sheet
  281. */
  282. _initSheetHeader: function (sheet) {
  283. if (!sheet.zh_setting) { return; }
  284. sheet.setColumnCount(sheet.zh_setting.cols.length);
  285. sheet.setRowCount(sheet.zh_setting.headRows, spreadNS.SheetArea.colHeader);
  286. for (let iRow = 0; iRow < sheet.zh_setting.headRowHeight.length; iRow ++) {
  287. sheet.setRowHeight(iRow, sheet.zh_setting.headRowHeight[iRow], spreadNS.SheetArea.colHeader);
  288. }
  289. for (let iCol = 0; iCol < sheet.zh_setting.cols.length; iCol++) {
  290. const col = sheet.zh_setting.cols[iCol];
  291. const title = col.title.split('|');
  292. const colSpan = col.colSpan ? col.colSpan.split('|'): ['1'], rowSpan = col.rowSpan ? col.rowSpan.split('|'): ['1'];
  293. for (let i = 0; i < title.length; i++) {
  294. const cell = sheet.getCell(i, iCol, spreadNS.SheetArea.colHeader);
  295. cell.text(title[i]).wordWrap(true);
  296. if ((colSpan[i] !== '' && colSpan[i] !== '1') || (rowSpan[i] !== '' && rowSpan[i] !== '1')) {
  297. sheet.addSpan(i, iCol, parseInt(rowSpan[i]), parseInt(colSpan[i]), spreadNS.SheetArea.colHeader);
  298. }
  299. }
  300. sheet.setColumnWidth(iCol, col.width);
  301. if (col.visible !== undefined && col.visible !== null) {
  302. sheet.setColumnVisible(iCol, col.visible);
  303. }
  304. }
  305. sheet.rowOutlines.direction(spreadNS.Outlines.OutlineDirection.backward);
  306. sheet.showRowOutline(false);
  307. if (sheet.zh_setting.defaultRowHeight) {
  308. sheet.defaults.rowHeight = sheet.zh_setting.defaultRowHeight;
  309. }
  310. },
  311. /**
  312. * 初始化sheet, 设置sheet.zh_setting, 并初始化表头
  313. * @param {GC.Spread.Sheets.Worksheet} sheet
  314. * @param setting
  315. */
  316. initSheet: function (sheet, setting) {
  317. this.beginMassOperation(sheet);
  318. setting.pos = sheet.getParent().pos;
  319. sheet.zh_setting = setting;
  320. if (sheet.zh_setting.headColWidth) {
  321. for (const [i, w] of sheet.zh_setting.headColWidth.entries()) {
  322. sheet.setColumnWidth(i, w, spreadNS.SheetArea.rowHeader);
  323. }
  324. }
  325. this._initSheetDeafult(sheet);
  326. this._initSheetHeader(sheet);
  327. sheet.setRowCount(sheet.zh_setting.emptyRows);
  328. sheet.extendCellType = {};
  329. sheet.getRange(0, 0, sheet.getRowCount(), sheet.getColumnCount()).locked(setting.readOnly);
  330. this.endMassOperation(sheet);
  331. },
  332. reLoadSheetHeader: function (sheet) {
  333. if (sheet.zh_setting) {
  334. this.beginMassOperation(sheet);
  335. this._initSheetHeader(sheet);
  336. this.endMassOperation(sheet);
  337. }
  338. },
  339. _loadRowData: function (sheet, data, row) {
  340. // 单元格重新写入数据
  341. if (!data) { return }
  342. sheet.zh_setting.cols.forEach(function (col, j) {
  343. const cell = sheet.getCell(row, j);
  344. if (col.getValue && Object.prototype.toString.apply(col.getValue) === "[object Function]") {
  345. cell.value(col.getValue(data));
  346. } else if (col.field !== '' && data[col.field]) {
  347. cell.value(data[col.field]);
  348. }
  349. if (col.font) {
  350. cell.font(col.font);
  351. }
  352. if (col.foreColor) {
  353. if (Object.prototype.toString.apply(col.foreColor) === "[object Function]") {
  354. cell.foreColor(col.foreColor(data, sheet.getDefaultStyle().foreColor));
  355. } else {
  356. cell.foreColor(col.foreColor);
  357. }
  358. }
  359. if (col.readOnly && Object.prototype.toString.apply(col.readOnly) === "[object Function]") {
  360. cell.locked(col.readOnly(data) || sheet.zh_setting.readOnly || false).vAlign(1).hAlign(col.hAlign);
  361. } else {
  362. cell.locked(col.readOnly || sheet.zh_setting.readOnly || false).vAlign(1).hAlign(col.hAlign);
  363. }
  364. if (col.formatter) {
  365. cell.formatter(col.formatter);
  366. } else if (col.type === 'Number') {
  367. cell.formatter('0.######');
  368. }
  369. if (sheet.zh_setting.getColor && Object.prototype.toString.apply(sheet.zh_setting.getColor) === "[object Function]") {
  370. cell.backColor(sheet.zh_setting.getColor(sheet, data, col, sheet.getDefaultStyle().backColor));
  371. }
  372. });
  373. },
  374. _defineColCellType: function (sheet, col, colSetting) {
  375. sheet.AcitveComboRefresh = false;
  376. if(colSetting.cellType === 'ellipsis') {
  377. if (!sheet.extendCellType.ellipsis) {
  378. sheet.extendCellType.ellipsis = this.CellType.getEllipsisTextCellType();
  379. }
  380. sheet.getRange(-1, col, -1, 1).cellType(sheet.extendCellType.ellipsis);
  381. }
  382. if(colSetting.cellType === 'ellipsisAutoTip') {
  383. if (!sheet.extendCellType.ellipsisAutoTip) {
  384. sheet.extendCellType.ellipsisAutoTip = this.CellType.getEllipsisTextAutoTipCellType();
  385. }
  386. sheet.getRange(-1, col, -1, 1).cellType(sheet.extendCellType.ellipsisAutoTip);
  387. }
  388. if(colSetting.cellType === 'html') {
  389. if (!sheet.extendCellType.html) {
  390. sheet.extendCellType.html = this.CellType.getHtmlCellType();
  391. }
  392. sheet.getRange(-1, col, -1, 1).cellType(sheet.extendCellType.html);
  393. }
  394. if (colSetting.cellType === 'image') {
  395. if (!sheet.extendCellType.image) {
  396. sheet.extendCellType.image = this.CellType.getImageCellType();
  397. }
  398. sheet.getRange(-1, col, -1, 1).cellType(sheet.extendCellType.image);
  399. }
  400. if (colSetting.cellType === 'imageBtn') {
  401. if (!sheet.extendCellType.imageBtn) {
  402. sheet.extendCellType.imageBtn = this.CellType.getImageButtonCellType();
  403. }
  404. sheet.getRange(-1, col, -1, 1).cellType(sheet.extendCellType.imageBtn);
  405. }
  406. if (colSetting.cellType === 'activeImageBtn') {
  407. if (!sheet.extendCellType.activeImageBtn) {
  408. sheet.extendCellType.activeImageBtn = this.CellType.getActiveImageButtonCellType();
  409. }
  410. sheet.getRange(-1, col, -1, 1).cellType(sheet.extendCellType.activeImageBtn);
  411. }
  412. if (colSetting.cellType === 'tree') {
  413. if (!sheet.extendCellType.tree) {
  414. sheet.extendCellType.tree = this.CellType.getTreeNodeCellType();
  415. }
  416. sheet.getRange(-1, col, -1, 1).cellType(sheet.extendCellType.tree);
  417. }
  418. if (colSetting.cellType === 'tip') {
  419. if (!sheet.extendCellType.tip) {
  420. sheet.extendCellType.tip = this.CellType.getTipCellType();
  421. }
  422. sheet.getRange(-1, col, -1, 1).cellType(sheet.extendCellType.tip);
  423. }
  424. if (colSetting.cellType === 'autoTip') {
  425. if (!sheet.extendCellType.autoTip) {
  426. sheet.extendCellType.autoTip = this.CellType.getAutoTipCellType();
  427. }
  428. sheet.getRange(-1, col, -1, 1).cellType(sheet.extendCellType.autoTip);
  429. }
  430. if (colSetting.cellType === 'checkbox') {
  431. if (!sheet.extendCellType.checkbox) {
  432. sheet.extendCellType.checkbox = new spreadNS.CellTypes.CheckBox();
  433. }
  434. sheet.getRange(-1, col, -1, 1).cellType(sheet.extendCellType.checkbox);
  435. }
  436. if (colSetting.cellType === 'unit') {
  437. if (!sheet.extendCellType.unit) {
  438. sheet.extendCellType.unit = this.CellType.getUnitCellType();
  439. if (!sheet.AcitveComboRefresh) {
  440. sheet.bind(spreadNS.Events.LeaveCell, function (e, info) {
  441. const cellType = info.sheet.getCell(info.row, info.col).cellType();
  442. if (cellType === sheet.extendCellType.unit) {
  443. info.sheet.leaveCell = {row: info.row, col: info.col};
  444. } else {
  445. delete info.sheet.leaveCell;
  446. }
  447. });
  448. sheet.bind(spreadNS.Events.EnterCell, function (e, info) {
  449. if (info.sheet.leaveCell) {
  450. info.sheet.repaint(info.sheet.getCellRect(info.sheet.leaveCell.row, info.sheet.leaveCell.col));
  451. }
  452. });
  453. sheet.AcitveComboRefresh = true;
  454. }
  455. }
  456. sheet.getRange(-1, col, -1, 1).cellType(sheet.extendCellType.unit);
  457. }
  458. if (colSetting.cellType === 'customizeCombo') {
  459. const cellKey = colSetting.cellTypeKey ? 'customizeCombo-' + colSetting.cellTypeKey : 'customizeCombo';
  460. if (!sheet.extendCellType[cellKey]) {
  461. sheet.extendCellType[cellKey] = this.CellType.getCustomizeComboCellType(colSetting.comboItems);
  462. if (!sheet.AcitveComboRefresh) {
  463. sheet.bind(spreadNS.Events.LeaveCell, function (e, info) {
  464. const cellType = info.sheet.getCell(info.row, info.col).cellType();
  465. if (cellType === sheet.extendCellType[cellKey]) {
  466. info.sheet.leaveCell = {row: info.row, col: info.col};
  467. } else {
  468. delete info.sheet.leaveCell;
  469. }
  470. });
  471. sheet.bind(spreadNS.Events.EnterCell, function (e, info) {
  472. if (info.sheet.leaveCell) {
  473. info.sheet.repaint(info.sheet.getCellRect(info.sheet.leaveCell.row, info.sheet.leaveCell.col));
  474. }
  475. });
  476. sheet.AcitveComboRefresh = true;
  477. }
  478. }
  479. sheet.getRange(-1, col, -1, 1).cellType(sheet.extendCellType[cellKey]);
  480. }
  481. if (colSetting.formatter) {
  482. sheet.getRange(-1, col, -1, 1).formatter(colSetting.formatter);
  483. }
  484. },
  485. /**
  486. * 整个sheet重新加载数据
  487. * @param {GC.Spread.Sheets.Worksheet} sheet
  488. */
  489. reLoadSheetData: function (sheet) {
  490. const self = this;
  491. const sortData = sheet.zh_dataType === 'tree' ? sheet.zh_tree.nodes : sheet.zh_data;
  492. this.beginMassOperation(sheet);
  493. try {
  494. sheet.clear(0, 0, sheet.getRowCount(), sheet.getColumnCount(), spreadNS.SheetArea.viewport, spreadNS.StorageType.data);
  495. sheet.getRange(0, 0, sheet.getRowCount(), sheet.getColumnCount()).backColor(sheet.getDefaultStyle().backColor);
  496. // 设置总行数
  497. const totalRow = sortData.length + sheet.zh_setting.emptyRows;
  498. sheet.setRowCount(totalRow, spreadNS.SheetArea.viewport);
  499. // 控制空白行
  500. const emptyRows = sheet.getRange(sortData.length, -1, sheet.zh_setting.emptyRows, -1);
  501. emptyRows.locked(sheet.zh_dataType === 'tree');
  502. if (sortData) {
  503. // 单元格写入数据
  504. sortData.forEach(function (data, i) {
  505. self._loadRowData(sheet, data, i);
  506. sheet.setRowVisible(i, data.visible);
  507. });
  508. }
  509. // 设置列单元格格式
  510. sheet.zh_setting.cols.forEach(function (col, j) {
  511. //if (!col.cellType) { return; }
  512. self._defineColCellType(sheet, j, col);
  513. });
  514. this.endMassOperation(sheet);
  515. } catch (err) {
  516. this.endMassOperation(sheet);
  517. }
  518. },
  519. /**
  520. * 重新加载部分数据行
  521. * @param {GC.Spread.Sheets.Worksheet} sheet
  522. * @param {Number} row
  523. * @param {Number} count
  524. */
  525. reLoadRowData: function (sheet, row, count = 1) {
  526. if (row < 0) { return; }
  527. const self = this;
  528. const sortData = sheet.zh_dataType === 'tree' ? sheet.zh_tree.nodes : sheet.zh_data;
  529. this.beginMassOperation(sheet);
  530. try {
  531. // 清空原单元格数据
  532. sheet.clear(row, -1, count, -1, spreadNS.SheetArea.viewport, spreadNS.StorageType.data);
  533. sheet.getRange(row, -1, count, -1).backColor(sheet.getDefaultStyle().backColor);
  534. // 单元格重新写入数据
  535. for (let i = row; i < row + count; i++) {
  536. const data = sortData[i];
  537. if (!data) { continue; }
  538. this._loadRowData(sheet, data, i);
  539. sheet.setRowVisible(i, data.visible);
  540. }
  541. this.endMassOperation(sheet);
  542. } catch (err) {
  543. this.endMassOperation(sheet);
  544. }
  545. },
  546. /**
  547. * 重新加载部分行数据
  548. * @param {GC.Spread.Sheets.Worksheet} sheet
  549. * @param {Array} rows
  550. */
  551. reLoadRowsData: function (sheet, rows) {
  552. const self = this;
  553. const sortData = sheet.zh_dataType === 'tree' ? sheet.zh_tree.nodes : sheet.zh_data;
  554. this.beginMassOperation(sheet);
  555. try {
  556. for (const row of rows) {
  557. if (row < 0) { continue; }
  558. // 清空原单元格数据
  559. sheet.clear(row, -1, 1, -1, spreadNS.SheetArea.viewport, spreadNS.StorageType.data);
  560. sheet.getRange(row, -1, 1, -1).backColor(sheet.getDefaultStyle().backColor);
  561. const data = sortData[row];
  562. // 单元格重新写入数据
  563. this._loadRowData(sheet, data, row);
  564. sheet.setRowVisible(row, data.visible);
  565. };
  566. this.endMassOperation(sheet);
  567. } catch (err) {
  568. this.endMassOperation(sheet);
  569. }
  570. },
  571. /**
  572. * 重新加载部分列数据
  573. * @param {GC.Spread.Sheets.Worksheet} sheet
  574. * @param {Array} cols
  575. */
  576. reLoadColsData: function (sheet, cols) {
  577. const self = this;
  578. const sortData = sheet.zh_dataType === 'tree' ? sheet.zh_tree.nodes : sheet.zh_data;
  579. this.beginMassOperation(sheet);
  580. try {
  581. for (const iCol of cols) {
  582. // 清空原单元格数据
  583. sheet.clear(-1, iCol, -1, 1, spreadNS.SheetArea.viewport, spreadNS.StorageType.data);
  584. const col = sheet.zh_setting.cols[iCol];
  585. sortData.forEach(function (data, i) {
  586. // 设置值
  587. const cell = sheet.getCell(i, iCol);
  588. if (col.field !== '' && data[col.field]) {
  589. cell.value(data[col.field]).locked(col.readOnly || sheet.zh_setting.readOnly || false).vAlign(1).hAlign(col.hAlign);
  590. } else {
  591. cell.locked(col.readOnly || sheet.zh_setting.readOnly || false).vAlign(1).hAlign(col.hAlign);
  592. }
  593. // 设置单元格格式
  594. if (col.formatter) {
  595. cell.formatter(col.formatter);
  596. }
  597. });
  598. }
  599. this.endMassOperation(sheet);
  600. } catch (err) {
  601. this.endMassOperation(sheet);
  602. }
  603. },
  604. reLoadNodesData: function (sheet, nodes) {
  605. this.beginMassOperation(sheet);
  606. nodes = nodes instanceof Array ? nodes : [nodes];
  607. for (const node of nodes) {
  608. const sortData = sheet.zh_dataType === 'tree' ? sheet.zh_tree.nodes : sheet.zh_data;
  609. this._loadRowData(sheet, node, sortData.indexOf(node));
  610. }
  611. this.endMassOperation(sheet);
  612. },
  613. /**
  614. * 根据data加载sheet数据,合并了一般数据和树结构数据的加载
  615. * @param {GC.Spread.Sheets.Worksheet} sheet
  616. * @param {String} dataType - 1.'zh_data' 2.'zh_tree'
  617. * @param {Array|PathTree} data - 对dataType对应
  618. */
  619. loadSheetData: function (sheet, dataType, data){
  620. sheet.zh_dataType = dataType;
  621. if (dataType === 'tree') {
  622. sheet.zh_tree = data;
  623. } else {
  624. sheet.zh_data = data;
  625. }
  626. this.protectedSheet(sheet);
  627. this.reLoadSheetData(sheet);
  628. },
  629. /**
  630. * 获取复制数据HTML格式(过滤不可见单元格)
  631. * @param {GC.Spread.Sheets.Worksheet} sheet
  632. * @returns {string}
  633. */
  634. getFilterCopyHTML: function (sheet) {
  635. const sel = sheet.getSelections()[0];
  636. const html = [];
  637. html.push('<table>');
  638. for (let i = sel.row, iLen = sel.row + sel.rowCount; i < iLen; i++) {
  639. // 跳过隐藏行
  640. if (!sheet.getCell(i, -1).visible()) { continue; }
  641. const rowHtml = [];
  642. rowHtml.push('<tr>');
  643. for (let j = sel.col, jLen = sel.col + sel.colCount; j < jLen; j++) {
  644. const data = sheet.getText(i, j);
  645. rowHtml.push('<td>' + data + '</td>');
  646. }
  647. rowHtml.push('</tr>');
  648. html.push(rowHtml.join(''));
  649. }
  650. html.push('</table>');
  651. return html.join('');
  652. },
  653. /**
  654. * 获取复制数据Text格式(过滤不可见单元格)
  655. * @param {GC.Spread.Sheets.Worksheet} sheet
  656. * @returns {string}
  657. */
  658. getFilterCopyText: function (sheet) {
  659. const copyData = [];
  660. const sel = sheet.getSelections()[0];
  661. for(let i = sel.row, iLen = sel.row + sel.rowCount; i < iLen; i++) {
  662. // 跳过隐藏行
  663. if (!sheet.getCell(i, -1).visible()) { continue; }
  664. const rowText = [];
  665. for (let j = sel.col, jLen = sel.col + sel.colCount; j < jLen; j++) {
  666. const data = sheet.getText(i, j);
  667. rowText.push(data);
  668. }
  669. copyData.push(rowText.join('\t'));
  670. }
  671. return copyData.join('\n');
  672. },
  673. analysisPasteText: function (text) {
  674. const result = [];
  675. if (text === '') return result;
  676. const rows = text.split('\n');
  677. for (const r of rows) {
  678. const cols = r.split('\t');
  679. result.push(cols);
  680. }
  681. return result;
  682. },
  683. analysisPasteHtml: function (html) {
  684. const result = [];
  685. if (!html || html === '') return result;
  686. },
  687. /**
  688. * 树表结构,定位至指定的节点
  689. * @param {GC.Spread.Sheets.Worksheet} sheet - 需要定位的sheet
  690. * @param {Number} id - 定位节点的id
  691. */
  692. locateTreeNode: function (sheet, id, autoExpand = false) {
  693. const tree = sheet.zh_tree;
  694. if (!tree) { return }
  695. const node = tree.getItems(id);
  696. if (!node) { return }
  697. if (autoExpand && !node.visible) {
  698. const parents = tree.autoExpandNode(node);
  699. if (parents && parents.length > 0) {
  700. SpreadJsObj.reLoadNodesData(sheet, parents);
  701. SpreadJsObj.refreshTreeRowVisible(sheet);
  702. }
  703. }
  704. const index = tree.nodes.indexOf(node);
  705. const sels = sheet.getSelections();
  706. sheet.setSelection(index, sels[0].col, 1, 1);
  707. sheet.getParent().focus();
  708. sheet.showRow(index, spreadNS.VerticalPosition.center);
  709. },
  710. saveTopAndSelect: function (sheet, cacheKey) {
  711. const sel = sheet.getSelections()[0];
  712. const top = sheet.getViewportTopRow(1);
  713. setLocalCache(cacheKey, JSON.stringify({top: top, sel: sel}));
  714. },
  715. loadTopAndSelect: function (sheet, cacheKey) {
  716. let ts = getLocalCache(cacheKey);
  717. if (ts !== '') {
  718. ts = JSON.parse(ts);
  719. if (ts === undefined || ts === null) return;
  720. if (ts.sel) {
  721. sheet.setSelection(ts.sel.row, ts.sel.col, ts.sel.rowCount, ts.sel.colCount);
  722. }
  723. if (ts.top) {
  724. sheet.showRow(ts.top, spreadNS.VerticalPosition.top);
  725. }
  726. }
  727. },
  728. resetTopAndSelect: function (sheet) {
  729. sheet.setSelection(0, 0, 1, 1);
  730. sheet.showRow(0, spreadNS.VerticalPosition.top);
  731. },
  732. /**
  733. * 获取当前选行的数据对象
  734. * @param {GC.Spread.Sheets.Worksheet} sheet
  735. * @returns {Object}
  736. */
  737. getSelectObject: function (sheet) {
  738. if (!sheet) {
  739. return null;
  740. } else if (sheet.zh_dataType) {
  741. const sel = sheet.getSelections()[0];
  742. if (!sel) {
  743. return null;
  744. }
  745. if (sheet.zh_dataType === this.DataType.Tree) {
  746. return sheet.zh_tree.nodes[sel.row];
  747. } else if (sheet.zh_dataType === this.DataType.Data) {
  748. return sheet.zh_data[sel.row];
  749. } else {
  750. return null;
  751. }
  752. }
  753. },
  754. /**
  755. * 刷新列显示
  756. * @param sheet
  757. */
  758. refreshColumnVisible: function (sheet) {
  759. if(sheet.zh_setting) {
  760. sheet.zh_setting.cols.forEach(function (col, index) {
  761. if (col.visible !== undefined && col.visible !== null) {
  762. sheet.setColumnVisible(index, col.visible);
  763. }
  764. });
  765. }
  766. },
  767. /**
  768. * 刷新行显示
  769. * @param sheet
  770. */
  771. refreshTreeRowVisible: function (sheet) {
  772. this.beginMassOperation(sheet);
  773. const sortData = sheet.zh_dataType === this.DataType.Data ? sheet.zh_data : sheet.zh_tree.nodes;
  774. for (const iRow in sortData) {
  775. const node = sortData[iRow];
  776. if (node.visible !== undefined && node.visible !== null) {
  777. sheet.setRowVisible(iRow, node.visible);
  778. } else {
  779. sheet.setRowVisible(iRow, true);
  780. }
  781. }
  782. // if (sheet.zh_tree) {
  783. // for (const iRow in sheet.zh_tree.nodes) {
  784. // const node = sheet.zh_tree.nodes[iRow];
  785. // if (node.visible !== undefined && node.visible !== null) {
  786. // sheet.setRowVisible(iRow, node.visible);
  787. // } else {
  788. // sheet.setRowVisible(iRow, true);
  789. // }
  790. // }
  791. // }
  792. this.endMassOperation(sheet);
  793. },
  794. refreshColumnAlign: function (sheet) {
  795. if (sheet.zh_setting) {
  796. for (const iCol in sheet.zh_setting.cols) {
  797. const col = sheet.zh_setting.cols[iCol];
  798. sheet.getRange(-1, iCol, -1, 1).hAlign(col.hAlign);
  799. }
  800. }
  801. },
  802. /**
  803. * 刷新列是否只读
  804. * @param sheet
  805. * @param field
  806. * @param readonly
  807. */
  808. resetFieldReadOnly: function (sheet, field, readonly) {
  809. const fields = field instanceof Array ? field : [field];
  810. if (sheet.zh_setting) {
  811. sheet.zh_setting.cols.forEach(function (col, i) {
  812. if (fields.indexOf(col.field) !== -1) {
  813. col.readOnly = readonly;
  814. for (let iRow = 0; iRow < sheet.getRowCount(); iRow++) {
  815. sheet.getCell(iRow, i).locked(col.readOnly || sheet.zh_setting.readOnly || false);
  816. }
  817. //sheet.getRange(-1, i, -1, 1, GC.Spread.Sheets.SheetArea.viewport).locked(col.readOnly || sheet.zh_setting.readOnly || false);
  818. }
  819. });
  820. }
  821. },
  822. CellType: {
  823. /**
  824. * 获取树结构CellType
  825. * 通过SpreadJsObj.loadSheetData(sheet, 'tree', tree)加载树结构数据
  826. * 要求tree类型为PathTree, 节点必须含有{id, pid, level, order, is_leaf}数据
  827. * @returns {TreeNodeCellType}
  828. */
  829. getTreeNodeCellType: function () {
  830. const xOffset = 2 + 5;
  831. const indent = 20;
  832. const levelIndent = -5;
  833. const halfBoxLength = 5;
  834. const halfExpandLength = 3;
  835. /**
  836. * 画一条点线段
  837. * @param canvas - 画布
  838. * @param x1 - 线段起点 x
  839. * @param y1 - 线段起点 y
  840. * @param x2 - 线段终点 x
  841. * @param y2 - 线段终点 y
  842. * @param color - 线段颜色
  843. */
  844. const drawDotLine = function (canvas, x1, y1, x2, y2, color) {
  845. canvas.save();
  846. // 设置偏移量
  847. canvas.translate(0.5, 0.5);
  848. canvas.beginPath();
  849. canvas.strokeStyle = color;
  850. canvas.dottedLine(x1, y1, x2, y2);
  851. canvas.stroke();
  852. canvas.restore();
  853. };
  854. /**
  855. * 画一条线段
  856. * @param canvas - 画布
  857. * @param x1 - 线段起点 x
  858. * @param y1 - 线段起点 y
  859. * @param x2 - 线段终点 x
  860. * @param y2 - 线段终点 y
  861. * @param color - 线段颜色
  862. */
  863. const drawLine = function (canvas, x1, y1, x2, y2, color) {
  864. canvas.save();
  865. // 设置偏移量
  866. canvas.translate(0.5, 0.5);
  867. canvas.beginPath();
  868. canvas.moveTo(x1, y1);
  869. canvas.lineTo(x2, y2);
  870. canvas.strokeStyle = color;
  871. canvas.stroke();
  872. canvas.restore();
  873. };
  874. /**
  875. * 画一个方框
  876. * @param {Object} canvas - 画布
  877. * @param {Object} rect - 方框区域
  878. * @param {String} lineColor - 画线颜色
  879. * @param {String} fillColor - 填充颜色
  880. */
  881. const drawBox = function (canvas, rect, lineColor, fillColor) {
  882. canvas.save();
  883. // 设置偏移量
  884. canvas.translate(0.5, 0.5);
  885. canvas.strokeStyle = lineColor;
  886. canvas.beginPath();
  887. canvas.moveTo(rect.left, rect.top);
  888. canvas.lineTo(rect.left, rect.bottom);
  889. canvas.lineTo(rect.right, rect.bottom);
  890. canvas.lineTo(rect.right, rect.top);
  891. canvas.lineTo(rect.left, rect.top);
  892. canvas.stroke();
  893. canvas.fillStyle = fillColor;
  894. canvas.fill();
  895. canvas.restore();
  896. };
  897. /**
  898. * 画树结构-展开收起按钮
  899. * @param {Object} canvas - 画布
  900. * @param {Number} x - 单元格左顶点坐标 x
  901. * @param {Number} y - 单元格左顶点坐标 y
  902. * @param {Number} w - 单元格宽度
  903. * @param {Number} h - 单元格高度
  904. * @param {Number} centerX - 按钮中央坐标
  905. * @param {Number} centerY - 按钮中央坐标
  906. * @param {Boolean} expanded - 当前节点展开收起状态
  907. */
  908. const drawExpandBox = function (canvas, x, y, w, h, centerX, centerY, expanded) {
  909. let rect = {
  910. top: centerY - halfBoxLength,
  911. bottom: centerY + halfBoxLength,
  912. left: centerX - halfBoxLength,
  913. right: centerX + halfBoxLength
  914. };
  915. let h1, h2, offset = 1;
  916. if (rect.left < x + w) {
  917. // 方框超出单元格宽度时,超出部分不画。
  918. rect.right = Math.min(rect.right, x + w);
  919. drawBox(canvas, rect, '#808080', 'white');
  920. // 画中心十字
  921. // 画十字横线
  922. h1 = centerX - halfExpandLength;
  923. h2 = Math.min(centerX + halfExpandLength, x + w);
  924. if (h2 > h1) {
  925. drawLine(canvas, h1, centerY, h2, centerY, '#808080');
  926. }
  927. // 画十字竖线
  928. if (!expanded && (centerX < x + w)) {
  929. drawLine(canvas, centerX, centerY - halfExpandLength, centerX, centerY + halfExpandLength, '#808080');
  930. }
  931. }
  932. };
  933. let TreeNodeCellType = function (){};
  934. TreeNodeCellType.prototype = new spreadNS.CellTypes.Text();
  935. const proto = TreeNodeCellType.prototype;
  936. /**
  937. * 绘制方法
  938. * @param {Object} canvas - 画布
  939. * @param value - cell.value
  940. * @param {Number} x - 单元格左顶点坐标 x
  941. * @param {Number} y - 单元格左顶点坐标 y
  942. * @param {Number} w - 单元格宽度
  943. * @param {Number} h - 单元格高度
  944. * @param {Object} style - cell.style
  945. * @param {Object} options
  946. */
  947. proto.paint = function (canvas, value, x, y, w, h, style, options) {
  948. // 清理 画布--单元格范围 旧数据
  949. if (style.backColor) {
  950. canvas.save();
  951. canvas.fillStyle = style.backColor;
  952. canvas.fillRect(x, y, w, h);
  953. canvas.restore();
  954. } else {
  955. canvas.clearRect(x, y, w, h);
  956. }
  957. const tree = options.sheet.zh_tree;
  958. // 使用TreeCellType前,需定义sheet.tree
  959. if (tree) {
  960. const node = options.row < tree.nodes.length ? tree.nodes[options.row] : null;
  961. if (node) {
  962. const showTreeLine = true;
  963. const centerX = Math.floor(x) + (node.level - 1) * indent + (node.level) * levelIndent + indent / 2 + xOffset;
  964. const centerY = Math.floor((y + (y + h)) / 2);
  965. // Draw Sibling Line
  966. if (showTreeLine) {
  967. // Draw Horizontal Line
  968. if (centerX < x + w) {
  969. const x1 = centerX + indent / 2;
  970. //drawLine(canvas, centerX, centerY, Math.min(x1, x + w), centerY, 'gray');
  971. drawDotLine(canvas, centerX, centerY, Math.min(x1, x + w), centerY, '#b8b8b8');
  972. }
  973. // Draw Vertical Line
  974. if (centerX < x + w) {
  975. const y1 = tree.isLastSibling(node) ? centerY : y + h;
  976. const parent = tree.getParent(node);
  977. const y2 = y1 - centerY;
  978. if (node.order === 1 && !parent) {
  979. //drawLine(canvas, centerX, centerY, centerX, y1, 'gray');
  980. drawDotLine(canvas, centerX, centerY, centerX, y1, '#b8b8b8');
  981. } else {
  982. //drawLine(canvas, centerX, y, centerX, y1, 'gray');
  983. drawDotLine(canvas, centerX, y, centerX, y1, '#b8b8b8');
  984. }
  985. }
  986. }
  987. // Draw Expand Box
  988. if (!node.is_leaf) {
  989. drawExpandBox(canvas, x, y, w, h, centerX, centerY, node.expanded);
  990. }
  991. // Draw Parent Line
  992. if (showTreeLine) {
  993. let parent = tree.getParent(node), parentCenterX = centerX - indent - levelIndent;
  994. while (parent) {
  995. if (!tree.isLastSibling(parent)) {
  996. if (parentCenterX < x + w) {
  997. //drawLine(canvas, parentCenterX, y, parentCenterX, y + h, 'gray');
  998. drawDotLine(canvas, parentCenterX, y, parentCenterX, y + h, '#b8b8b8');
  999. }
  1000. }
  1001. parent = tree.getParent(parent);
  1002. parentCenterX -= (indent + levelIndent);
  1003. }
  1004. };
  1005. // 重定位x
  1006. const move = (node.level) * indent + (node.level) * levelIndent + xOffset;
  1007. x = x + move;
  1008. w = w - move;
  1009. }
  1010. }
  1011. // Drawing Text
  1012. spreadNS.CellTypes.Text.prototype.paint.apply(this, [canvas, value, x, y, w, h, style, options]);
  1013. };
  1014. /**
  1015. * 获取点击信息
  1016. * @param {Number} x
  1017. * @param {Number} y
  1018. * @param {Object} cellStyle
  1019. * @param {Object} cellRect
  1020. * @param {Object} context
  1021. * @returns {{x: *, y: *, row: *, col: *|boolean|*[]|number|{}|UE.dom.dtd.col, cellStyle: *, cellRect: *, sheet: *|StyleSheet, sheetArea: *}}
  1022. */
  1023. proto.getHitInfo = function (x, y, cellStyle, cellRect, context) {
  1024. return {
  1025. x: x,
  1026. y: y,
  1027. row: context.row,
  1028. col: context.col,
  1029. cellStyle: cellStyle,
  1030. cellRect: cellRect,
  1031. sheet: context.sheet,
  1032. sheetArea: context.sheetArea
  1033. };
  1034. };
  1035. /**
  1036. * 鼠标点击 树结构按钮 响应展开收起(未加载子节点时,先加载子节点)
  1037. * @param {Object} hitinfo - 见getHitInfo
  1038. */
  1039. proto.processMouseDown = function (hitinfo) {
  1040. const offset = -1;
  1041. const tree = hitinfo.sheet.zh_tree;
  1042. if (!tree) { return; }
  1043. const node = tree.nodes[hitinfo.row];
  1044. if (!node) { return; }
  1045. let centerX = hitinfo.cellRect.x + offset + (node.level - 1) * indent + (node.level) * levelIndent + indent / 2 + xOffset;
  1046. let centerY = (hitinfo.cellRect.y + offset + (hitinfo.cellRect.y + offset + hitinfo.cellRect.height)) / 2;
  1047. // 点击展开节点时,如果已加载子项,则展开,反之这加载子项,展开
  1048. if (Math.abs(hitinfo.x - centerX) < halfBoxLength && Math.abs(hitinfo.y - centerY) < halfBoxLength) {
  1049. const children = tree.getChildren(node);
  1050. if (!node.expanded && !node.is_leaf && children.length === 0 && tree.loadChildren) {
  1051. tree.loadChildren(node, function () {
  1052. node.expanded = true;
  1053. const children = tree.getChildren(node);
  1054. hitinfo.sheet.addRows(hitinfo.row + 1, children.length);
  1055. SpreadJsObj.reLoadRowData(hitinfo.sheet, hitinfo.row + 1, children.length);
  1056. });
  1057. } else {
  1058. tree.setExpanded(node, !node.expanded);
  1059. SpreadJsObj.massOperationSheet(hitinfo.sheet, function () {
  1060. const posterity = tree.getPosterity(node);
  1061. for (const child of posterity) {
  1062. hitinfo.sheet.setRowVisible(tree.nodes.indexOf(child), child.visible, hitinfo.sheetArea);
  1063. }
  1064. });
  1065. hitinfo.sheet.repaint();
  1066. }
  1067. }
  1068. };
  1069. return new TreeNodeCellType();
  1070. },
  1071. /**
  1072. * 获取 带悬浮提示的CellType
  1073. * @returns {TipCellType}
  1074. */
  1075. getTipCellType: function () {
  1076. const maxHintWidth = 200, indent = 15, borderIndent = 10;
  1077. const TipCellType = function () {};
  1078. // 继承 SpreadJs定义的 普通的TextCellType
  1079. TipCellType.prototype = new spreadNS.CellTypes.Text();
  1080. const proto = TipCellType.prototype;
  1081. proto.getTextDisplayWidth = function(hitinfo, str, font) {
  1082. const xs = hitinfo.sheet.getParent().xs;
  1083. const ctx = xs.childNodes[0].getContext("2d");
  1084. if (font && font !== '') {
  1085. ctx.font = font;
  1086. } else {
  1087. ctx.font = hitinfo.cellStyle.font;
  1088. }
  1089. return ctx.measureText(str).width;
  1090. };
  1091. proto.showTip = function (hitinfo, text) {
  1092. return text && text !== '';
  1093. };
  1094. /**
  1095. * 获取点击信息
  1096. * @param {Number} x
  1097. * @param {Number} y
  1098. * @param {Object} cellStyle
  1099. * @param {Object} cellRect
  1100. * @param {Object} context
  1101. * @returns {{x: *, y: *, row: *, col: *|boolean|*[]|number|{}|UE.dom.dtd.col, cellStyle: *, cellRect: *, sheet: *|StyleSheet, sheetArea: *}}
  1102. */
  1103. proto.getHitInfo = function (x, y, cellStyle, cellRect, context) {
  1104. return {
  1105. x: x,
  1106. y: y,
  1107. row: context.row,
  1108. col: context.col,
  1109. cellStyle: cellStyle,
  1110. cellRect: cellRect,
  1111. sheet: context.sheet,
  1112. sheetArea: context.sheetArea,
  1113. ctx: context.sheet.getParent().xs,
  1114. };
  1115. };
  1116. /**
  1117. * 鼠标进入单元格事件 - 显示悬浮提示
  1118. * @param {Object} hitinfo - 见getHitInfo返回值
  1119. */
  1120. proto.processMouseEnter = function (hitinfo) {
  1121. let text = hitinfo.sheet.getText(hitinfo.row, hitinfo.col);
  1122. const col = hitinfo.sheet.zh_setting.cols[hitinfo.col];
  1123. if (col.getTip && Object.prototype.toString.apply(col.getTip) === "[object Function]") {
  1124. const sortData = SpreadJsObj.getSortData(hitinfo.sheet);
  1125. text = col.getTip(sortData[hitinfo.row]);
  1126. }
  1127. const pos = SpreadJsObj.getObjPos(hitinfo.sheet.getParent().qo);
  1128. if (pos && this.showTip(hitinfo, text)) {
  1129. if (!this._toolTipElement) {
  1130. let div = $('#autoTip')[0];
  1131. if (!div) {
  1132. div = document.createElement("div");
  1133. $(div).css("position", "absolute")
  1134. .css("border", "1px #C0C0C0 solid")
  1135. .css("box-shadow", "1px 2px 5px rgba(0,0,0,0.4)")
  1136. .css("font", "9pt Arial")
  1137. .css("background", "white")
  1138. .css("padding", 5)
  1139. .css("z-index", 999)
  1140. .css("max-width", maxHintWidth)
  1141. .css("word-wrap", "break-word")
  1142. .attr("id", 'autoTip');
  1143. document.body.insertBefore(div, null);
  1144. }
  1145. const validWidth = $(window).width() - (pos.x + hitinfo.x + indent) - borderIndent;
  1146. const textWidth = this.getTextDisplayWidth(hitinfo, text, "9pt Arial");
  1147. if (validWidth >= maxHintWidth || textWidth <= validWidth) {
  1148. $(div).text(text).css("top", pos.y + hitinfo.y + indent).css("left", pos.x + hitinfo.x + indent);
  1149. } else if (textWidth > maxHintWidth) {
  1150. $(div).text(text).css("top", pos.y + hitinfo.y + indent).css("left", pos.x + hitinfo.x - indent - maxHintWidth);
  1151. } else {
  1152. $(div).text(text).css("top", pos.y + hitinfo.y + indent).css("left", pos.x + hitinfo.x - indent - textWidth);
  1153. }
  1154. this._toolTipElement = div;
  1155. $(div).show("fast");
  1156. }
  1157. }
  1158. };
  1159. /**
  1160. * 鼠标移出单元格事件 - 隐藏悬浮提示
  1161. * @param {Object} hitinfo - 见getHitInfo返回值
  1162. */
  1163. proto.processMouseLeave = function (hitinfo) {
  1164. if (this._toolTipElement) {
  1165. $(this._toolTipElement).hide();
  1166. this._toolTipElement = null;
  1167. }
  1168. };
  1169. return new TipCellType();
  1170. },
  1171. getAutoTipCellType: function () {
  1172. const AutoTipCellType = function () {};
  1173. // 继承 TipCellType
  1174. AutoTipCellType.prototype = SpreadJsObj.CellType.getTipCellType();
  1175. const proto = AutoTipCellType.prototype
  1176. proto.showTip = function (hitinfo, text) {
  1177. return text && text !== '' && this.getTextDisplayWidth(hitinfo, text) > hitinfo.cellRect.widthidth;
  1178. };
  1179. return new AutoTipCellType();
  1180. },
  1181. /**
  1182. * 获取 带图片的cellType(图片需在document中定义好img,并写入col的img属性)
  1183. *
  1184. * img:
  1185. * 1. 整列固定,则传入img的select
  1186. * e.g. {title: '附件', field: 'attachment', cellType: 'image', img = '#attachment-img'}
  1187. *
  1188. * 2. 各单元格自定义,则
  1189. * e.g. {title: '附件', field: 'attachment', cellType: 'image', img = getAttachmentImage}
  1190. * function getAttachmentImage (data) {
  1191. * $('#attachment-img').url = data.attachmentImageUrl;
  1192. * return $('#attachment-img')[0];
  1193. * }
  1194. *
  1195. * @returns {ImageCellType}
  1196. */
  1197. getImageCellType: function () {
  1198. const ImageCellType = function (){};
  1199. ImageCellType.prototype = new spreadNS.CellTypes.Text();
  1200. const proto = ImageCellType.prototype;
  1201. proto.getImage = function (sheet, iRow, iCol) {
  1202. const col = sheet.zh_setting.cols[iCol];
  1203. let imgSource = col.img;
  1204. if (imgSource && Object.prototype.toString.apply(imgSource) === "[object Function]") {
  1205. const sortData = SpreadJsObj.getSortData(sheet);
  1206. const data = sortData ? sortData[iRow] : null;
  1207. return data ? imgSource(data) : null;
  1208. } else {
  1209. return $(imgSource)[0] ? $(imgSource)[0] : null;
  1210. }
  1211. };
  1212. proto.paint = function (canvas, value, x, y, w, h, style, options) {
  1213. const col = options.sheet.zh_setting.cols[options.col];
  1214. const img = this.getImage(options.sheet, options.row, options.col);
  1215. const indent = col.indent ? col.indent : 10;
  1216. if (img) {
  1217. if (style.backColor) {
  1218. canvas.save();
  1219. canvas.fillStyle = style.backColor;
  1220. canvas.fillRect(x, y, indent + img.width, h);
  1221. canvas.restore();
  1222. }
  1223. canvas.drawImage(img, x + indent, y + (h - img.height) / 2);
  1224. if (style.hAlign !== spreadNS.HorizontalAlign.left) {
  1225. style.hAlign = spreadNS.HorizontalAlign.left;
  1226. }
  1227. x = x + indent + img.width;
  1228. w = w - indent - img.width;
  1229. }
  1230. // Drawing Text
  1231. spreadNS.CellTypes.Text.prototype.paint.apply(this, [canvas, value, x, y, w, h, style, options]);
  1232. };
  1233. /**
  1234. * 获取点击信息
  1235. * @param {Number} x
  1236. * @param {Number} y
  1237. * @param {Object} cellStyle
  1238. * @param {Object} cellRect
  1239. * @param {Object} context
  1240. * @returns {{x: *, y: *, row: *, col: *|boolean|*[]|number|{}|UE.dom.dtd.col, cellStyle: *, cellRect: *, sheet: *|StyleSheet, sheetArea: *}}
  1241. */
  1242. proto.getHitInfo = function (x, y, cellStyle, cellRect, context) {
  1243. return {
  1244. x: x,
  1245. y: y,
  1246. row: context.row,
  1247. col: context.col,
  1248. cellStyle: cellStyle,
  1249. cellRect: cellRect,
  1250. sheet: context.sheet,
  1251. sheetArea: context.sheetArea
  1252. };
  1253. };
  1254. /**
  1255. * 鼠标点击
  1256. * @param {Object} hitinfo - 见getHitInfo
  1257. */
  1258. proto.processMouseDown = function (hitinfo) {
  1259. const img = this.getImage(hitinfo.sheet, hitinfo.row, hitinfo.col);
  1260. if (img) {
  1261. const halfX = img.width / 2, halfY = img.height / 2;
  1262. const centerX = hitinfo.cellRect.x + indent + halfX;
  1263. const centerY = hitinfo.cellRect.y + hitinfo.cellRect.height / 2;
  1264. // 点击展开节点时,如果已加载子项,则展开,反之这加载子项,展开
  1265. if (Math.abs(hitinfo.x - centerX) < halfX && Math.abs(hitinfo.y - centerY) < halfY) {
  1266. const imageClick = hitinfo.sheet.zh_setting ? hitinfo.sheet.zh_setting.imageClick : null;
  1267. if (imageClick && Object.prototype.toString.apply(imageClick) === "[object Function]") {
  1268. const sortData = SpreadJsObj.getSortData(hitinfo.sheet);
  1269. const data = sortData ? sortData[hitinfo.row] : null;
  1270. imageClick(data);
  1271. }
  1272. }
  1273. }
  1274. };
  1275. return new ImageCellType();
  1276. },
  1277. /**
  1278. *
  1279. * 获取 带normal-hover-active按钮的cellType(需定义三张图片,须在document中定义好img,并写入col的normalImg, hoverImg, activeImg属性)
  1280. * 其中:normalImg必需,向下套用(不存在activeImg则使用hoverImg,不存在hoverImg则使用normalImg)
  1281. * 三个img均可像getImageCellType一样动态获取,参见getImageCellType注释
  1282. *
  1283. * @returns {ImageCellType}
  1284. */
  1285. getImageButtonCellType: function () {
  1286. let hover = 1, active = 2;
  1287. const ImageCellType = function (){};
  1288. ImageCellType.prototype = new spreadNS.CellTypes.Text();
  1289. const proto = ImageCellType.prototype;
  1290. proto.getImage = function (sheet, iRow, iCol) {
  1291. const col = sheet.zh_setting.cols[iCol];
  1292. let imgSource = col.normalImg;
  1293. const cell = sheet.getCell(iRow, iCol), tag = cell.tag();
  1294. if (tag === active) {
  1295. imgSource = col.activeImg ? col.activeImg : (col.hoverImg ? col.hoverImg : col.normalImg);
  1296. } else if (tag === hover) {
  1297. imgSource = col.hoverImg ? col.hoverImg : col.normalImg;
  1298. }
  1299. if (imgSource && Object.prototype.toString.apply(imgSource) === "[object Function]") {
  1300. const sortData = SpreadJsObj.getSortData(sheet);
  1301. const data = sortData ? sortData[iRow] : null;
  1302. return data ? imgSource(data) : null;
  1303. } else {
  1304. return $(imgSource)[0] ? $(imgSource)[0] : null;
  1305. }
  1306. };
  1307. proto.paint = function (canvas, value, x, y, w, h, style, options) {
  1308. const col = options.sheet.zh_setting.cols[options.col];
  1309. const sortData = SpreadJsObj.getSortData(options.sheet);
  1310. const data = sortData ? sortData[options.row] : null;
  1311. let showImage = true;
  1312. if (col.showImage && Object.prototype.toString.apply(col.showImage) === "[object Function]") {
  1313. showImage = col.showImage(data);
  1314. }
  1315. const img = showImage ? this.getImage(options.sheet, options.row, options.col) : null;
  1316. const indent = col.indent ? col.indent : 10;
  1317. if (style.hAlign === spreadNS.HorizontalAlign.right) {
  1318. if (img) {
  1319. if (style.backColor) {
  1320. canvas.save();
  1321. canvas.fillStyle = style.backColor;
  1322. canvas.fillRect(x + w - indent - img.width, y, img.width, h);
  1323. canvas.restore();
  1324. }
  1325. canvas.drawImage(img, x + w - indent - img.width, y + (h - img.height) / 2);
  1326. w = w - indent - img.width;
  1327. }
  1328. // Drawing Text
  1329. spreadNS.CellTypes.Text.prototype.paint.apply(this, [canvas, value, x, y, w, h, style, options]);
  1330. } else {
  1331. if (img) {
  1332. if (style.backColor) {
  1333. canvas.save();
  1334. canvas.fillStyle = style.backColor;
  1335. canvas.fillRect(x, y, indent + img.width, h);
  1336. canvas.restore();
  1337. }
  1338. canvas.drawImage(img, x + 10, y + (h - img.height) / 2);
  1339. if (style.hAlign !== spreadNS.HorizontalAlign.left) {
  1340. style.hAlign = spreadNS.HorizontalAlign.left;
  1341. }
  1342. x = x + indent + img.width;
  1343. w = w - indent - img.width;
  1344. }
  1345. // Drawing Text
  1346. spreadNS.CellTypes.Text.prototype.paint.apply(this, [canvas, value, x, y, w, h, style, options]);
  1347. }
  1348. };
  1349. /**
  1350. * 获取点击信息
  1351. * @param {Number} x
  1352. * @param {Number} y
  1353. * @param {Object} cellStyle
  1354. * @param {Object} cellRect
  1355. * @param {Object} context
  1356. * @returns {{x: *, y: *, row: *, col: *|boolean|*[]|number|{}|UE.dom.dtd.col, cellStyle: *, cellRect: *, sheet: *|StyleSheet, sheetArea: *}}
  1357. */
  1358. proto.getHitInfo = function (x, y, cellStyle, cellRect, context) {
  1359. return {
  1360. x: x,
  1361. y: y,
  1362. row: context.row,
  1363. col: context.col,
  1364. cellStyle: cellStyle,
  1365. cellRect: cellRect,
  1366. sheet: context.sheet,
  1367. sheetArea: context.sheetArea
  1368. };
  1369. };
  1370. proto.processMouseEnter = function (hitinfo) {
  1371. const col = hitinfo.sheet.zh_setting.cols[hitinfo.col];
  1372. // Drawing Image
  1373. if (col.hoverImg) {
  1374. const cell = hitinfo.sheet.getCell(hitinfo.row, hitinfo.col);
  1375. cell.tag(hover);
  1376. hitinfo.sheet.repaint(hitinfo.cellRect);
  1377. }
  1378. };
  1379. proto.processMouseLeave = function (hitinfo) {
  1380. const col = hitinfo.sheet.zh_setting.cols[hitinfo.col];
  1381. // Drawing Image
  1382. const cell = hitinfo.sheet.getCell(hitinfo.row, hitinfo.col);
  1383. cell.tag(null);
  1384. hitinfo.sheet.repaint(hitinfo.cellRect);
  1385. };
  1386. // proto.processMouseDown = function (hitinfo) {
  1387. // const col = hitinfo.sheet.zh_setting.cols[hitinfo.col];
  1388. // if (col.activeImg) {
  1389. // const cell = hitinfo.sheet.getCell(hitinfo.row, hitinfo.col);
  1390. // cell.tag(active);
  1391. // hitinfo.sheet.repaint(hitinfo.cellRect);
  1392. // }
  1393. // };
  1394. // proto.processMouseUp = function (hitinfo) {
  1395. // const col = hitinfo.sheet.zh_setting.cols[hitinfo.col];
  1396. // const sortData = SpreadJsObj.getSortData(hitinfo.sheet);
  1397. // const data = sortData ? sortData[hitinfo.row] : null;
  1398. // if (col.showImage && Object.prototype.toString.apply(col.showImage) === "[object Function]") {
  1399. // if (!col.showImage(data)) {
  1400. // return;
  1401. // }
  1402. // }
  1403. // const imageClick = hitinfo.sheet.zh_setting ? hitinfo.sheet.zh_setting.imageClick : null;
  1404. // if (imageClick && Object.prototype.toString.apply(imageClick) === "[object Function]") {
  1405. // imageClick(data);
  1406. // const cell = hitinfo.sheet.getCell(hitinfo.row, hitinfo.col);
  1407. // cell.tag(null);
  1408. // hitinfo.sheet.repaint(hitinfo.cellRect);
  1409. // }
  1410. // };
  1411. /*
  1412. 注释部分以进入鼠标进入图片,点击图片为基准更新图片,鼠标快速移动时,可能失效
  1413. */
  1414. proto.processMouseDown = function (hitinfo) {
  1415. const col = hitinfo.sheet.zh_setting.cols[hitinfo.col];
  1416. const img = this.getImage(hitinfo.sheet, hitinfo.row, hitinfo.col);
  1417. const halfX = img.width / 2, halfY = img.height / 2;
  1418. const indent = col.indent ? col.indent : 10;
  1419. const centerX = hitinfo.cellStyle.hAlign === spreadNS.HorizontalAlign.right
  1420. ? hitinfo.cellRect.x + hitinfo.cellRect.width - indent -halfX
  1421. : hitinfo.cellRect.x + indent + halfX;
  1422. const centerY = hitinfo.cellRect.y + hitinfo.cellRect.height / 2;
  1423. if (Math.abs(hitinfo.x - centerX) < halfX && Math.abs(hitinfo.y - centerY) < halfY) {
  1424. const cell = hitinfo.sheet.getCell(hitinfo.row, hitinfo.col);
  1425. cell.tag(active);
  1426. hitinfo.sheet.repaint(hitinfo.cellRect);
  1427. }
  1428. };
  1429. proto.processMouseUp = function (hitinfo) {
  1430. const col = hitinfo.sheet.zh_setting.cols[hitinfo.col];
  1431. const img = this.getImage(hitinfo.sheet, hitinfo.row, hitinfo.col);
  1432. const halfX = img.width / 2, halfY = img.height / 2;
  1433. const indent = col.indent ? col.indent : 10;
  1434. const centerX = hitinfo.cellStyle.hAlign === spreadNS.HorizontalAlign.right
  1435. ? hitinfo.cellRect.x + hitinfo.cellRect.width - indent -halfX
  1436. : hitinfo.cellRect.x + indent + halfX;
  1437. const centerY = hitinfo.cellRect.y + hitinfo.cellRect.height / 2;
  1438. if (Math.abs(hitinfo.x - centerX) < halfX && Math.abs(hitinfo.y - centerY) < halfY) {
  1439. const imageClick = hitinfo.sheet.zh_setting ? hitinfo.sheet.zh_setting.imageClick : null;
  1440. if (imageClick && Object.prototype.toString.apply(imageClick) === "[object Function]") {
  1441. const sortData = SpreadJsObj.getSortData(hitinfo.sheet);
  1442. const data = sortData ? sortData[hitinfo.row] : null;
  1443. imageClick(data);
  1444. const cell = hitinfo.sheet.getCell(hitinfo.row, hitinfo.col);
  1445. cell.tag(null);
  1446. hitinfo.sheet.repaint(hitinfo.cellRect);
  1447. }
  1448. }
  1449. };
  1450. proto.processMouseMove = function (hitinfo) {
  1451. const col = hitinfo.sheet.zh_setting.cols[hitinfo.col];
  1452. const img = this.getImage(hitinfo.sheet, hitinfo.row, hitinfo.col);
  1453. const halfX = img.width / 2, halfY = img.height / 2;
  1454. const indent = col.indent ? col.indent : 10;
  1455. const centerX = hitinfo.cellStyle.hAlign === spreadNS.HorizontalAlign.right
  1456. ? hitinfo.cellRect.x + hitinfo.cellRect.width - indent -halfX
  1457. : hitinfo.cellRect.x + indent + halfX;
  1458. const centerY = hitinfo.cellRect.y + hitinfo.cellRect.height / 2;
  1459. const cell = hitinfo.sheet.getCell(hitinfo.row, hitinfo.col);
  1460. if (Math.abs(hitinfo.x - centerX) < halfX && Math.abs(hitinfo.y - centerY) < halfY) {
  1461. if (cell.tag() !== hover) {
  1462. cell.tag(hover);
  1463. hitinfo.sheet.repaint(hitinfo.cellRect);
  1464. }
  1465. } else {
  1466. if (cell.tag() === hover) {
  1467. cell.tag(null);
  1468. hitinfo.sheet.repaint(hitinfo.cellRect);
  1469. }
  1470. }
  1471. };
  1472. return new ImageCellType();
  1473. },
  1474. getActiveImageButtonCellType: function () {
  1475. let show = 1, hover = 2, active = 2;
  1476. const ImageCellType = function (){};
  1477. ImageCellType.prototype = new spreadNS.CellTypes.Text();
  1478. const proto = ImageCellType.prototype;
  1479. proto.getImage = function (sheet, iRow, iCol) {
  1480. const col = sheet.zh_setting.cols[iCol];
  1481. let imgSource = null;
  1482. const cell = sheet.getCell(iRow, iCol), tag = cell.tag();
  1483. if (tag === show) {
  1484. imgSource = col.normalImg;
  1485. } else if (tag === active) {
  1486. imgSource = col.activeImg ? col.activeImg : (col.hoverImg ? col.hoverImg : col.normalImg);
  1487. } else if (tag === hover) {
  1488. imgSource = col.hoverImg ? col.hoverImg : col.normalImg;
  1489. }
  1490. if (imgSource && Object.prototype.toString.apply(imgSource) === "[object Function]") {
  1491. const sortData = SpreadJsObj.getSortData(sheet);
  1492. const data = sortData ? sortData[iRow] : null;
  1493. return data ? imgSource(data) : null;
  1494. } else {
  1495. return $(imgSource)[0] ? $(imgSource)[0] : null;
  1496. }
  1497. };
  1498. proto.paint = function (canvas, value, x, y, w, h, style, options) {
  1499. const col = options.sheet.zh_setting.cols[options.col];
  1500. const sortData = SpreadJsObj.getSortData(options.sheet);
  1501. const data = sortData ? sortData[options.row] : null;
  1502. let showImage = true;
  1503. if (col.showImage && Object.prototype.toString.apply(col.showImage) === "[object Function]") {
  1504. showImage = col.showImage(data);
  1505. }
  1506. const img = showImage ? this.getImage(options.sheet, options.row, options.col) : null;
  1507. const indent = col.indent ? col.indent : 10;
  1508. if (style.hAlign === spreadNS.HorizontalAlign.right) {
  1509. if (img) {
  1510. if (style.backColor) {
  1511. canvas.save();
  1512. canvas.fillStyle = style.backColor;
  1513. canvas.fillRect(x + w - indent - img.width, y, img.width, h);
  1514. canvas.restore();
  1515. }
  1516. canvas.drawImage(img, x + w - indent - img.width, y + (h - img.height) / 2);
  1517. w = w - indent - img.width;
  1518. }
  1519. // Drawing Text
  1520. spreadNS.CellTypes.Text.prototype.paint.apply(this, [canvas, value, x, y, w, h, style, options]);
  1521. } else {
  1522. if (img) {
  1523. if (style.backColor) {
  1524. canvas.save();
  1525. canvas.fillStyle = style.backColor;
  1526. canvas.fillRect(x, y, indent + img.width, h);
  1527. canvas.restore();
  1528. }
  1529. canvas.drawImage(img, x + 10, y + (h - img.height) / 2);
  1530. if (style.hAlign !== spreadNS.HorizontalAlign.left) {
  1531. style.hAlign = spreadNS.HorizontalAlign.left;
  1532. }
  1533. x = x + indent + img.width;
  1534. w = w - indent - img.width;
  1535. }
  1536. // Drawing Text
  1537. spreadNS.CellTypes.Text.prototype.paint.apply(this, [canvas, value, x, y, w, h, style, options]);
  1538. }
  1539. };
  1540. /**
  1541. * 获取点击信息
  1542. * @param {Number} x
  1543. * @param {Number} y
  1544. * @param {Object} cellStyle
  1545. * @param {Object} cellRect
  1546. * @param {Object} context
  1547. * @returns {{x: *, y: *, row: *, col: *|boolean|*[]|number|{}|UE.dom.dtd.col, cellStyle: *, cellRect: *, sheet: *|StyleSheet, sheetArea: *}}
  1548. */
  1549. proto.getHitInfo = function (x, y, cellStyle, cellRect, context) {
  1550. return {
  1551. x: x,
  1552. y: y,
  1553. row: context.row,
  1554. col: context.col,
  1555. cellStyle: cellStyle,
  1556. cellRect: cellRect,
  1557. sheet: context.sheet,
  1558. sheetArea: context.sheetArea
  1559. };
  1560. };
  1561. proto.processMouseEnter = function (hitinfo) {
  1562. const col = hitinfo.sheet.zh_setting.cols[hitinfo.col];
  1563. // Drawing Image
  1564. if (col.normalImg) {
  1565. const cell = hitinfo.sheet.getCell(hitinfo.row, hitinfo.col);
  1566. cell.tag(show);
  1567. hitinfo.sheet.repaint(hitinfo.cellRect);
  1568. }
  1569. };
  1570. proto.processMouseLeave = function (hitinfo) {
  1571. const cell = hitinfo.sheet.getCell(hitinfo.row, hitinfo.col);
  1572. cell.tag(null);
  1573. hitinfo.sheet.repaint(hitinfo.cellRect);
  1574. };
  1575. // proto.processMouseDown = function (hitinfo) {
  1576. // const col = hitinfo.sheet.zh_setting.cols[hitinfo.col];
  1577. // if (col.activeImg) {
  1578. // const cell = hitinfo.sheet.getCell(hitinfo.row, hitinfo.col);
  1579. // cell.tag(active);
  1580. // hitinfo.sheet.repaint(hitinfo.cellRect);
  1581. // }
  1582. // };
  1583. // proto.processMouseUp = function (hitinfo) {
  1584. // const col = hitinfo.sheet.zh_setting.cols[hitinfo.col];
  1585. // const sortData = SpreadJsObj.getSortData(hitinfo.sheet);
  1586. // const data = sortData ? sortData[hitinfo.row] : null;
  1587. // if (col.showImage && Object.prototype.toString.apply(col.showImage) === "[object Function]") {
  1588. // if (!col.showImage(data)) {
  1589. // return;
  1590. // }
  1591. // }
  1592. // const imageClick = hitinfo.sheet.zh_setting ? hitinfo.sheet.zh_setting.imageClick : null;
  1593. // if (imageClick && Object.prototype.toString.apply(imageClick) === "[object Function]") {
  1594. // imageClick(data);
  1595. // const cell = hitinfo.sheet.getCell(hitinfo.row, hitinfo.col);
  1596. // cell.tag(null);
  1597. // hitinfo.sheet.repaint(hitinfo.cellRect);
  1598. // }
  1599. // };
  1600. /*
  1601. 注释部分以进入鼠标进入图片,点击图片为基准更新图片,鼠标快速移动时,可能失效
  1602. */
  1603. proto.processMouseDown = function (hitinfo) {
  1604. const col = hitinfo.sheet.zh_setting.cols[hitinfo.col];
  1605. const img = this.getImage(hitinfo.sheet, hitinfo.row, hitinfo.col);
  1606. const halfX = img.width / 2, halfY = img.height / 2;
  1607. const indent = col.indent ? col.indent : 10;
  1608. const centerX = hitinfo.cellStyle.hAlign === spreadNS.HorizontalAlign.right
  1609. ? hitinfo.cellRect.x + hitinfo.cellRect.width - indent -halfX
  1610. : hitinfo.cellRect.x + indent + halfX;
  1611. const centerY = hitinfo.cellRect.y + hitinfo.cellRect.height / 2;
  1612. if (Math.abs(hitinfo.x - centerX) < halfX && Math.abs(hitinfo.y - centerY) < halfY) {
  1613. const cell = hitinfo.sheet.getCell(hitinfo.row, hitinfo.col);
  1614. cell.tag(active);
  1615. hitinfo.sheet.repaint(hitinfo.cellRect);
  1616. }
  1617. };
  1618. proto.processMouseUp = function (hitinfo) {
  1619. const col = hitinfo.sheet.zh_setting.cols[hitinfo.col];
  1620. const img = this.getImage(hitinfo.sheet, hitinfo.row, hitinfo.col);
  1621. const halfX = img.width / 2, halfY = img.height / 2;
  1622. const indent = col.indent ? col.indent : 10;
  1623. const centerX = hitinfo.cellStyle.hAlign === spreadNS.HorizontalAlign.right
  1624. ? hitinfo.cellRect.x + hitinfo.cellRect.width - indent -halfX
  1625. : hitinfo.cellRect.x + indent + halfX;
  1626. const centerY = hitinfo.cellRect.y + hitinfo.cellRect.height / 2;
  1627. if (Math.abs(hitinfo.x - centerX) < halfX && Math.abs(hitinfo.y - centerY) < halfY) {
  1628. const imageClick = hitinfo.sheet.zh_setting ? hitinfo.sheet.zh_setting.imageClick : null;
  1629. if (imageClick && Object.prototype.toString.apply(imageClick) === "[object Function]") {
  1630. const sortData = SpreadJsObj.getSortData(hitinfo.sheet);
  1631. const data = sortData ? sortData[hitinfo.row] : null;
  1632. imageClick(data);
  1633. const cell = hitinfo.sheet.getCell(hitinfo.row, hitinfo.col);
  1634. cell.tag(hover);
  1635. hitinfo.sheet.repaint(hitinfo.cellRect);
  1636. }
  1637. }
  1638. };
  1639. proto.processMouseMove = function (hitinfo) {
  1640. const col = hitinfo.sheet.zh_setting.cols[hitinfo.col];
  1641. const img = this.getImage(hitinfo.sheet, hitinfo.row, hitinfo.col);
  1642. const halfX = img.width / 2, halfY = img.height / 2;
  1643. const indent = col.indent ? col.indent : 10;
  1644. const centerX = hitinfo.cellStyle.hAlign === spreadNS.HorizontalAlign.right
  1645. ? hitinfo.cellRect.x + hitinfo.cellRect.width - indent -halfX
  1646. : hitinfo.cellRect.x + indent + halfX;
  1647. const centerY = hitinfo.cellRect.y + hitinfo.cellRect.height / 2;
  1648. const cell = hitinfo.sheet.getCell(hitinfo.row, hitinfo.col);
  1649. if (Math.abs(hitinfo.x - centerX) < halfX && Math.abs(hitinfo.y - centerY) < halfY) {
  1650. if (cell.tag() !== hover) {
  1651. cell.tag(hover);
  1652. hitinfo.sheet.repaint(hitinfo.cellRect);
  1653. }
  1654. } else {
  1655. if (cell.tag() === hover) {
  1656. cell.tag(show);
  1657. hitinfo.sheet.repaint(hitinfo.cellRect);
  1658. }
  1659. }
  1660. };
  1661. return new ImageCellType();
  1662. },
  1663. /**
  1664. * 获取 嵌入Html的cellType
  1665. * @returns {HTMLCellType}
  1666. */
  1667. getHtmlCellType: function () {
  1668. const HTMLCellType = function (){};
  1669. HTMLCellType.prototype = new spreadNS.CellTypes.Text;
  1670. const proto = ImageCellType.prototype;
  1671. proto.paint = function (ctx, value, x, y, w, h, style, context) {
  1672. let DOMURL = window.URL || window.webkitURL || window;
  1673. let cell = context.sheet.getCell(context.row, context.col);
  1674. let img = cell.tag();
  1675. if (img) {
  1676. try {
  1677. ctx.save();
  1678. ctx.rect(x, y, w, h);
  1679. ctx.clip();
  1680. ctx.drawImage(img, x + 2, y + 2)
  1681. ctx.restore();
  1682. cell.tag(null);
  1683. return;
  1684. }
  1685. catch (err) {
  1686. GC.Spread.Sheets.CustomCellType.prototype.paint.apply(this, [ctx, "#HTMLError", x, y, w, h, style, context])
  1687. cell.tag(null);
  1688. return;
  1689. }
  1690. }
  1691. let svgPattern = '<svg xmlns="http://www.w3.org/2000/svg" width="{0}" height="{1}">' +
  1692. '<foreignObject width="100%" height="100%"><div xmlns="http://www.w3.org/1999/xhtml" style="font:{2}">{3}</div></foreignObject></svg>';
  1693. let data = svgPattern.replace("{0}", w).replace("{1}", h).replace("{2}", style.font).replace("{3}", value);
  1694. let doc = document.implementation.createHTMLDocument("");
  1695. doc.write(data);
  1696. // Get well-formed markup
  1697. data = (new XMLSerializer()).serializeToString(doc.body.children[0]);
  1698. img = new Image();
  1699. //var svg = new Blob([data], {type: 'image/svg+xml;charset=utf-8'});
  1700. //var url = DOMURL.createObjectURL(svg);
  1701. //img.src = url;
  1702. img.src = 'data:image/svg+xml;base64,' + window.btoa(data);
  1703. cell.tag(img);
  1704. img.onload = function () {
  1705. context.sheet.repaint(new GC.Spread.Sheets.Rect(x, y, w, h));
  1706. }
  1707. };
  1708. return new HTMLCellType();
  1709. },
  1710. /**
  1711. * 获取 字符超长缩略的cellType
  1712. * @returns {EllipsisTextCellType}
  1713. */
  1714. getEllipsisTextCellType: function () {
  1715. const EllipsisTextCellType = function (){};
  1716. EllipsisTextCellType.prototype = new spreadNS.CellTypes.Text;
  1717. const proto = EllipsisTextCellType.prototype;
  1718. proto.getEllipsisText = function(c, str, maxWidth) {
  1719. var width = c.measureText(str).width;
  1720. var ellipsis = '…';
  1721. var ellipsisWidth = c.measureText(ellipsis).width;
  1722. if (width <= maxWidth || width <= ellipsisWidth) {
  1723. return str;
  1724. } else {
  1725. var len = str.length;
  1726. while (width >= maxWidth - ellipsisWidth && len-- > 0) {
  1727. str = str.substring(0, len);
  1728. width = c.measureText(str).width;
  1729. }
  1730. return str + ellipsis;
  1731. }
  1732. };
  1733. proto.paint = function (ctx, value, x, y, w, h, style, context) {
  1734. ctx.font = style.font;
  1735. value = this.getEllipsisText(ctx, value, w - 2);
  1736. spreadNS.CellTypes.Text.prototype.paint.apply(this, [ctx, value, x, y, w, h, style, context]);
  1737. };
  1738. return new EllipsisTextCellType();
  1739. },
  1740. getEllipsisTextAutoTipCellType: function () {
  1741. const CellType = function () {};
  1742. // 继承 TipCellType
  1743. CellType.prototype = SpreadJsObj.CellType.getAutoTipCellType();
  1744. const proto = CellType.prototype;
  1745. const ellipsisTextCellType = SpreadJsObj.CellType.getEllipsisTextCellType();
  1746. proto.getEllipsisText = ellipsisTextCellType.getEllipsisText;
  1747. proto.paint = ellipsisTextCellType.paint;
  1748. return new CellType();
  1749. },
  1750. /**
  1751. * 获取 动态显示ComboBox的cellType
  1752. * @returns {ActiveComboCellType}
  1753. */
  1754. getActiveComboCellType: function () {
  1755. const ActiveComboCellType = function () {};
  1756. ActiveComboCellType.prototype = new spreadNS.CellTypes.ComboBox();
  1757. const proto = ActiveComboCellType.prototype;
  1758. proto.paintValue = function (ctx, value, x, y, w, h, style, options) {
  1759. const sheet = options.sheet;
  1760. if (options.row === sheet.getActiveRowIndex() && options.col === sheet.getActiveColumnIndex()
  1761. && !sheet.getCell(options.row, options.col).locked()) {
  1762. spreadNS.CellTypes.ComboBox.prototype.paintValue.apply(this, arguments);
  1763. } else {
  1764. spreadNS.CellTypes.Base.prototype.paintValue.apply(this, arguments);
  1765. }
  1766. };
  1767. proto.getHitInfo = function (x, y, cellStyle, cellRect, options) {
  1768. const sheet = options.sheet;
  1769. if (options.row === sheet.getActiveRowIndex() && options.col === sheet.getActiveColumnIndex()
  1770. && !sheet.getCell(options.row, options.col).locked()) {
  1771. return spreadNS.CellTypes.ComboBox.prototype.getHitInfo.apply(this, [x, y, cellStyle, cellRect, options]);
  1772. } else {
  1773. return {
  1774. x: x,
  1775. y: y,
  1776. row: options.row,
  1777. col: options.col,
  1778. cellStyle: cellStyle,
  1779. cellRect: cellRect,
  1780. sheetArea: options.sheetArea
  1781. };
  1782. }
  1783. };
  1784. return new ActiveComboCellType();
  1785. },
  1786. /**
  1787. * 获取 单位的CellType
  1788. * @returns {GC.Spread.Sheets.CellTypes.ComboBox}
  1789. */
  1790. getUnitCellType: function () {
  1791. let combo = this.getActiveComboCellType();
  1792. combo.itemHeight(10).items(['m', 'km', 'm2', 'm3', 'dm3', 'kg', 't', 'm3·km',
  1793. '总额', '月' ,'项', '处' ,'个', '根', '棵', '块', '台', '系统', '每一试桩',
  1794. '桥长米', '公路公里', '株', '组', '座', '元', '工日', '套', '台班', '艘班', '亩',
  1795. 'm/处', 'm/道', 'm/座', 'm2/m', 'm3/m', 'm3/处', '根/米', 'm3/m2']);
  1796. return combo;
  1797. },
  1798. /**
  1799. * 获取 自定义的CellType
  1800. * @returns {GC.Spread.Sheets.CellTypes.ComboBox}
  1801. */
  1802. getCustomizeComboCellType: function (items) {
  1803. let combo = this.getActiveComboCellType();
  1804. combo.itemHeight(10).editorValueType(spreadNS.CellTypes.EditorValueType.value).items(items);
  1805. return combo;
  1806. },
  1807. },
  1808. Clipboard: (function () {
  1809. let cText = '', cHtml;
  1810. const setCopyData = function (text, html) {
  1811. cText = text;
  1812. cHtml = html;
  1813. };
  1814. const getPasteData = function () {
  1815. return {text: cText, html: cHtml};
  1816. };
  1817. const clearData = function () {
  1818. cText = '';
  1819. cHtml = undefined;
  1820. };
  1821. const setSheetFilterCopyData = function (sheet) {
  1822. cText = SpreadJsObj.getFilterCopyText(sheet);
  1823. cHtml = SpreadJsObj.getFilterCopyHTML(sheet);
  1824. };
  1825. const getAnalysisPasteText = function () {
  1826. return SpreadJsObj.analysisPasteText(cText);
  1827. };
  1828. const getAnalysisPasteHtml = function () {
  1829. return SpreadJsObj.analysisPasteHtml(cHtml);
  1830. };
  1831. return {
  1832. setCopyData, getPasteData, clearData,
  1833. setSheetFilterCopyData,
  1834. getAnalysisPasteText, getAnalysisPasteHtml,
  1835. }
  1836. })(),
  1837. };