|
@@ -30,6 +30,34 @@ proto.dottedLine = function (x1, y1, x2, y2, interval = 4) {
|
|
|
const spreadNS = GC.Spread.Sheets;
|
|
|
// SpreadJs常用方法
|
|
|
const SpreadJsObj = {
|
|
|
+ initSpreadSettingEvents: function (setting, events) {
|
|
|
+ const getEvent = function (eventName) {
|
|
|
+ const names = eventName.split('.');
|
|
|
+ let event = events;
|
|
|
+ for (let name of names) {
|
|
|
+ if (event[name]) {
|
|
|
+ event = event[name];
|
|
|
+ } else {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (event && Object.prototype.toString.apply(event) !== "[object Function]") {
|
|
|
+ return null;
|
|
|
+ } else {
|
|
|
+ return event;
|
|
|
+ }
|
|
|
+ };
|
|
|
+
|
|
|
+ for (const col of setting.cols) {
|
|
|
+ if (col.readOnly && Object.prototype.toString.apply(col.readOnly) === "[object String]") {
|
|
|
+ col.readOnly = getEvent(col.readOnly);
|
|
|
+ }
|
|
|
+ if (col.getValue && Object.prototype.toString.apply(col.getValue) === "[object String]") {
|
|
|
+ col.getValue = getEvent(col.getValue);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ },
|
|
|
+
|
|
|
DataType: {
|
|
|
Data: 'data',
|
|
|
Tree: 'tree',
|
|
@@ -164,6 +192,25 @@ const SpreadJsObj = {
|
|
|
return target;
|
|
|
},
|
|
|
/**
|
|
|
+ * 获取写入sheet的数据序列
|
|
|
+ * data:sheet.zh_data, tree: sheet.zh_tree.nodes
|
|
|
+ * @param sheet
|
|
|
+ * @returns {*}
|
|
|
+ */
|
|
|
+ getSortData: function (sheet) {
|
|
|
+ if (sheet.zh_dataType) {
|
|
|
+ if (sheet.zh_dataType === this.DataType.Data) {
|
|
|
+ return sheet.zh_data;
|
|
|
+ } else if (sheet.zh_dataType === this.DataType.Tree) {
|
|
|
+ return sheet.zh_tree.nodes;
|
|
|
+ } else {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ },
|
|
|
+ /**
|
|
|
* sheet中 使用delete键,触发EndEdited事件
|
|
|
* @param {GC.Spreads.Sheets.Workbook} spread
|
|
|
* @param {function} fun
|
|
@@ -241,9 +288,17 @@ const SpreadJsObj = {
|
|
|
if (!data) { return }
|
|
|
sheet.zh_setting.cols.forEach(function (col, j) {
|
|
|
const cell = sheet.getCell(row, j);
|
|
|
- if (col.field !== '' && data[col.field]) {
|
|
|
+ if (col.getValue && Object.prototype.toString.apply(col.getValue) === "[object Function]") {
|
|
|
+ cell.value(col.getValue(data));
|
|
|
+ } else if (col.field !== '' && data[col.field]) {
|
|
|
cell.value(data[col.field]);
|
|
|
}
|
|
|
+ if (col.font) {
|
|
|
+ cell.font(col.font);
|
|
|
+ }
|
|
|
+ if (col.foreColor) {
|
|
|
+ cell.foreColor(col.foreColor);
|
|
|
+ }
|
|
|
if (col.readOnly && Object.prototype.toString.apply(col.readOnly) === "[object Function]") {
|
|
|
cell.locked(col.readOnly(data) || sheet.zh_setting.readOnly || false).vAlign(1).hAlign(col.hAlign);
|
|
|
} else {
|
|
@@ -252,8 +307,58 @@ const SpreadJsObj = {
|
|
|
if (col.formatter) {
|
|
|
cell.formatter(col.formatter);
|
|
|
}
|
|
|
+ if (sheet.zh_setting.getColor && Object.prototype.toString.apply(sheet.zh_setting.getColor) === "[object Function]") {
|
|
|
+ cell.backColor(sheet.zh_setting.getColor(data, col, sheet.getDefaultStyle().backColor));
|
|
|
+ }
|
|
|
});
|
|
|
},
|
|
|
+ _defineColCellType: function (sheet, col, colSetting) {
|
|
|
+ if(colSetting.cellType === 'ellipsis') {
|
|
|
+ if (!sheet.extendCellType.ellipsis) {
|
|
|
+ sheet.extendCellType.ellipsis = this.CellType.getEllipsisTextCellType();
|
|
|
+ }
|
|
|
+ sheet.getRange(-1, col, -1, 1).cellType(sheet.extendCellType.ellipsis);
|
|
|
+ }
|
|
|
+ if(colSetting.cellType === 'html') {
|
|
|
+ if (!sheet.extendCellType.html) {
|
|
|
+ sheet.extendCellType.html = this.CellType.getHtmlCellType();
|
|
|
+ }
|
|
|
+ sheet.getRange(-1, col, -1, 1).cellType(sheet.extendCellType.html);
|
|
|
+ }
|
|
|
+ if (colSetting.cellType === 'image') {
|
|
|
+ if (!sheet.extendCellType.image) {
|
|
|
+ sheet.extendCellType.image = this.CellType.getImageCellType();
|
|
|
+ }
|
|
|
+ sheet.getRange(-1, col, -1, 1).cellType(sheet.extendCellType.image);
|
|
|
+ }
|
|
|
+ if (colSetting.cellType === 'imageBtn') {
|
|
|
+ if (!sheet.extendCellType.image) {
|
|
|
+ sheet.extendCellType.imageBtn = this.CellType.getImageButtonCellType();
|
|
|
+ }
|
|
|
+ sheet.getRange(-1, col, -1, 1).cellType(sheet.extendCellType.imageBtn);
|
|
|
+ }
|
|
|
+ if (colSetting.cellType === 'tree') {
|
|
|
+ if (!sheet.extendCellType.tree) {
|
|
|
+ sheet.extendCellType.tree = this.CellType.getTreeNodeCellType();
|
|
|
+ }
|
|
|
+ sheet.getRange(-1, col, -1, 1).cellType(sheet.extendCellType.tree);
|
|
|
+ }
|
|
|
+ if (colSetting.cellType === 'tip') {
|
|
|
+ if (!sheet.extendCellType.tip) {
|
|
|
+ sheet.extendCellType.tip = this.CellType.getTipCellType();
|
|
|
+ }
|
|
|
+ sheet.getRange(-1, col, -1, 1).cellType(sheet.extendCellType.tip);
|
|
|
+ }
|
|
|
+ if (colSetting.cellType === 'checkbox') {
|
|
|
+ if (!sheet.extendCellType.checkbox) {
|
|
|
+ sheet.extendCellType.checkbox = new spreadNS.CellTypes.CheckBox();
|
|
|
+ }
|
|
|
+ sheet.getRange(-1, col, -1, 1).cellType(sheet.extendCellType.checkbox);
|
|
|
+ }
|
|
|
+ if (colSetting.formatter) {
|
|
|
+ sheet.getRange(-1, col, -1, 1).formatter(colSetting.formatter);
|
|
|
+ }
|
|
|
+ },
|
|
|
/**
|
|
|
* 整个sheet重新加载数据
|
|
|
* @param {GC.Spread.Sheets.Worksheet} sheet
|
|
@@ -278,26 +383,7 @@ const SpreadJsObj = {
|
|
|
// 设置列单元格格式
|
|
|
sheet.zh_setting.cols.forEach(function (col, j) {
|
|
|
//if (!col.cellType) { return; }
|
|
|
-
|
|
|
- if (col.cellType === 'tree') {
|
|
|
- if (!sheet.extendCellType.tree) {
|
|
|
- sheet.extendCellType.tree = self.CellType.getTreeNodeCellType();
|
|
|
- }
|
|
|
- sheet.getRange(-1, j, -1, 1).cellType(sheet.extendCellType.tree);
|
|
|
- } else if (col.cellType === 'tip') {
|
|
|
- if (!sheet.extendCellType.tip) {
|
|
|
- sheet.extendCellType.tip = self.CellType.getTipCellType();
|
|
|
- }
|
|
|
- sheet.getRange(-1, j, -1, 1).cellType(sheet.extendCellType.tip);
|
|
|
- } else if (col.cellType === 'checkbox') {
|
|
|
- if (!sheet.extendCellType.checkbox) {
|
|
|
- sheet.extendCellType.checkbox = new spreadNS.CellTypes.CheckBox();
|
|
|
- }
|
|
|
- sheet.getRange(-1, j, -1, 1).cellType(sheet.extendCellType.checkbox);
|
|
|
- }
|
|
|
- if (col.formatter) {
|
|
|
- sheet.getRange(-1, j, -1, 1).formatter(col.formatter);
|
|
|
- }
|
|
|
+ self._defineColCellType(sheet, j, col);
|
|
|
});
|
|
|
this.endMassOperation(sheet);
|
|
|
} catch (err) {
|
|
@@ -803,7 +889,7 @@ const SpreadJsObj = {
|
|
|
return new TreeNodeCellType();
|
|
|
},
|
|
|
/**
|
|
|
- * 获取带悬浮提示CellType
|
|
|
+ * 获取 带悬浮提示的CellType
|
|
|
* @returns {TipCellType}
|
|
|
*/
|
|
|
getTipCellType: function () {
|
|
@@ -873,6 +959,349 @@ const SpreadJsObj = {
|
|
|
};
|
|
|
|
|
|
return new TipCellType();
|
|
|
- }
|
|
|
+ },
|
|
|
+ /**
|
|
|
+ * 获取 带图片的cellType(图片需在document中定义好img,并写入col的img属性)
|
|
|
+ *
|
|
|
+ * img:
|
|
|
+ * 1. 整列固定,则传入img的select
|
|
|
+ * e.g. {title: '附件', field: 'attachment', cellType: 'image', img = '#attachment-img'}
|
|
|
+ *
|
|
|
+ * 2. 各单元格自定义,则
|
|
|
+ * e.g. {title: '附件', field: 'attachment', cellType: 'image', img = getAttachmentImage}
|
|
|
+ * function getAttachmentImage (data) {
|
|
|
+ * $('#attachment-img').url = data.attachmentImageUrl;
|
|
|
+ * return $('#attachment-img')[0];
|
|
|
+ * }
|
|
|
+ *
|
|
|
+ * @returns {ImageCellType}
|
|
|
+ */
|
|
|
+ getImageCellType: function () {
|
|
|
+ const indent = 10;
|
|
|
+ const ImageCellType = function (){};
|
|
|
+ ImageCellType.prototype = new spreadNS.CellTypes.Text();
|
|
|
+ const proto = ImageCellType.prototype;
|
|
|
+ proto.getImage = function (sheet, iRow, iCol) {
|
|
|
+ const col = sheet.zh_setting.cols[iCol];
|
|
|
+ let imgSource = col.img;
|
|
|
+ if (imgSource && Object.prototype.toString.apply(imgSource) === "[object Function]") {
|
|
|
+ const sortData = SpreadJsObj.getSortData(sheet);
|
|
|
+ const data = sortData ? sortData[iRow] : null;
|
|
|
+ return data ? imgSource(data) : null;
|
|
|
+ } else {
|
|
|
+ return $(imgSource)[0] ? $(imgSource)[0] : null;
|
|
|
+ }
|
|
|
+
|
|
|
+ };
|
|
|
+ proto.paint = function (canvas, value, x, y, w, h, style, options) {
|
|
|
+ const img = this.getImage(options.sheet, options.row, options.col);
|
|
|
+ if (img) {
|
|
|
+ if (style.backColor) {
|
|
|
+ canvas.save();
|
|
|
+ canvas.fillStyle = style.backColor;
|
|
|
+ canvas.fillRect(x, y, indent + img.width, h);
|
|
|
+ canvas.restore();
|
|
|
+ }
|
|
|
+ canvas.drawImage(img, x + 10, y + (h - img.height) / 2);
|
|
|
+ if (style.hAlign !== spreadNS.HorizontalAlign.left) {
|
|
|
+ style.hAlign = spreadNS.HorizontalAlign.left;
|
|
|
+ }
|
|
|
+ x = x + indent + img.width;
|
|
|
+ w = w - indent - img.width;
|
|
|
+ }
|
|
|
+ // Drawing Text
|
|
|
+ spreadNS.CellTypes.Text.prototype.paint.apply(this, [canvas, value, x, y, w, h, style, options]);
|
|
|
+ };
|
|
|
+ /**
|
|
|
+ * 获取点击信息
|
|
|
+ * @param {Number} x
|
|
|
+ * @param {Number} y
|
|
|
+ * @param {Object} cellStyle
|
|
|
+ * @param {Object} cellRect
|
|
|
+ * @param {Object} context
|
|
|
+ * @returns {{x: *, y: *, row: *, col: *|boolean|*[]|number|{}|UE.dom.dtd.col, cellStyle: *, cellRect: *, sheet: *|StyleSheet, sheetArea: *}}
|
|
|
+ */
|
|
|
+ proto.getHitInfo = function (x, y, cellStyle, cellRect, context) {
|
|
|
+ return {
|
|
|
+ x: x,
|
|
|
+ y: y,
|
|
|
+ row: context.row,
|
|
|
+ col: context.col,
|
|
|
+ cellStyle: cellStyle,
|
|
|
+ cellRect: cellRect,
|
|
|
+ sheet: context.sheet,
|
|
|
+ sheetArea: context.sheetArea
|
|
|
+ };
|
|
|
+ };
|
|
|
+ /**
|
|
|
+ * 鼠标点击
|
|
|
+ * @param {Object} hitinfo - 见getHitInfo
|
|
|
+ */
|
|
|
+ proto.processMouseDown = function (hitinfo) {
|
|
|
+ const img = this.getImage(hitinfo.sheet, hitinfo.row, hitinfo.col);
|
|
|
+ if (img) {
|
|
|
+ const halfX = img.width / 2, halfY = img.height / 2;
|
|
|
+ const centerX = hitinfo.cellRect.x + indent + halfX;
|
|
|
+ const centerY = hitinfo.cellRect.y + hitinfo.cellRect.height / 2;
|
|
|
+
|
|
|
+ // 点击展开节点时,如果已加载子项,则展开,反之这加载子项,展开
|
|
|
+ if (Math.abs(hitinfo.x - centerX) < halfX && Math.abs(hitinfo.y - centerY) < halfY) {
|
|
|
+ const imageClick = hitinfo.sheet.zh_setting ? hitinfo.sheet.zh_setting.imageClick : null;
|
|
|
+ if (imageClick && Object.prototype.toString.apply(imageClick) === "[object Function]") {
|
|
|
+ const sortData = SpreadJsObj.getSortData(hitinfo.sheet);
|
|
|
+ const data = sortData ? sortData[hitinfo.row] : null;
|
|
|
+ imageClick(data);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ };
|
|
|
+ return new ImageCellType();
|
|
|
+ },
|
|
|
+ /**
|
|
|
+ *
|
|
|
+ * 获取 带normal-hover-active按钮的cellType(需定义三张图片,须在document中定义好img,并写入col的normalImg, hoverImg, activeImg属性)
|
|
|
+ * 其中:normalImg必需,向下套用(不存在activeImg则使用hoverImg,不存在hoverImg则使用normalImg)
|
|
|
+ * 三个img均可像getImageCellType一样动态获取,参见getImageCellType注释
|
|
|
+ *
|
|
|
+ * @returns {ImageCellType}
|
|
|
+ */
|
|
|
+ getImageButtonCellType: function () {
|
|
|
+ let hover = 1, active = 2;
|
|
|
+ const indent = 10;
|
|
|
+ const ImageCellType = function (){};
|
|
|
+ ImageCellType.prototype = new spreadNS.CellTypes.Text();
|
|
|
+ const proto = ImageCellType.prototype;
|
|
|
+ proto.getImage = function (sheet, iRow, iCol) {
|
|
|
+ const col = sheet.zh_setting.cols[iCol];
|
|
|
+ let imgSource = col.normalImg;
|
|
|
+ const cell = sheet.getCell(iRow, iCol), tag = cell.tag();
|
|
|
+ if (tag === active) {
|
|
|
+ imgSource = col.activeImg ? col.activeImg : (col.hoverImg ? col.hoverImg : col.normalImg);
|
|
|
+ } else if (tag === hover) {
|
|
|
+ imgSource = col.hoverImg ? col.hoverImg : col.normalImg;
|
|
|
+ }
|
|
|
+ if (imgSource && Object.prototype.toString.apply(imgSource) === "[object Function]") {
|
|
|
+ const sortData = SpreadJsObj.getSortData(sheet);
|
|
|
+ const data = sortData ? sortData[iRow] : null;
|
|
|
+ return data ? imgSource(data) : null;
|
|
|
+ } else {
|
|
|
+ return $(imgSource)[0] ? $(imgSource)[0] : null;
|
|
|
+ }
|
|
|
+
|
|
|
+ };
|
|
|
+ proto.paint = function (canvas, value, x, y, w, h, style, options) {
|
|
|
+ const img = this.getImage(options.sheet, options.row, options.col);
|
|
|
+ if (img) {
|
|
|
+ if (style.backColor) {
|
|
|
+ canvas.save();
|
|
|
+ canvas.fillStyle = style.backColor;
|
|
|
+ canvas.fillRect(x, y, indent + img.width, h);
|
|
|
+ canvas.restore();
|
|
|
+ }
|
|
|
+ canvas.drawImage(img, x + 10, y + (h - img.height) / 2);
|
|
|
+ if (style.hAlign !== spreadNS.HorizontalAlign.left) {
|
|
|
+ style.hAlign = spreadNS.HorizontalAlign.left;
|
|
|
+ }
|
|
|
+ x = x + indent + img.width;
|
|
|
+ w = w - indent - img.width;
|
|
|
+ }
|
|
|
+ // Drawing Text
|
|
|
+ spreadNS.CellTypes.Text.prototype.paint.apply(this, [canvas, value, x, y, w, h, style, options]);
|
|
|
+ };
|
|
|
+ /**
|
|
|
+ * 获取点击信息
|
|
|
+ * @param {Number} x
|
|
|
+ * @param {Number} y
|
|
|
+ * @param {Object} cellStyle
|
|
|
+ * @param {Object} cellRect
|
|
|
+ * @param {Object} context
|
|
|
+ * @returns {{x: *, y: *, row: *, col: *|boolean|*[]|number|{}|UE.dom.dtd.col, cellStyle: *, cellRect: *, sheet: *|StyleSheet, sheetArea: *}}
|
|
|
+ */
|
|
|
+ proto.getHitInfo = function (x, y, cellStyle, cellRect, context) {
|
|
|
+ return {
|
|
|
+ x: x,
|
|
|
+ y: y,
|
|
|
+ row: context.row,
|
|
|
+ col: context.col,
|
|
|
+ cellStyle: cellStyle,
|
|
|
+ cellRect: cellRect,
|
|
|
+ sheet: context.sheet,
|
|
|
+ sheetArea: context.sheetArea
|
|
|
+ };
|
|
|
+ };
|
|
|
+ /**
|
|
|
+ * 鼠标点击
|
|
|
+ * @param {Object} hitinfo - 见getHitInfo
|
|
|
+ */
|
|
|
+ proto.processMouseEnter = function (hitinfo) {
|
|
|
+ const col = hitinfo.sheet.zh_setting.cols[hitinfo.col];
|
|
|
+ // Drawing Image
|
|
|
+ if (col.hoverImg) {
|
|
|
+ const cell = hitinfo.sheet.getCell(hitinfo.row, hitinfo.col);
|
|
|
+ cell.tag(hover);
|
|
|
+ hitinfo.sheet.repaint(hitinfo.cellRect);
|
|
|
+ }
|
|
|
+ };
|
|
|
+ proto.processMouseLeave = function (hitinfo) {
|
|
|
+ const col = hitinfo.sheet.zh_setting.cols[hitinfo.col];
|
|
|
+ // Drawing Image
|
|
|
+ if (col.hoverImg) {
|
|
|
+ const cell = hitinfo.sheet.getCell(hitinfo.row, hitinfo.col);
|
|
|
+ cell.tag(null);
|
|
|
+ hitinfo.sheet.repaint(hitinfo.cellRect);
|
|
|
+ }
|
|
|
+ };
|
|
|
+ proto.processMouseDown = function (hitinfo) {
|
|
|
+ const col = hitinfo.sheet.zh_setting.cols[hitinfo.col];
|
|
|
+ if (col.activeImg) {
|
|
|
+ const cell = hitinfo.sheet.getCell(hitinfo.row, hitinfo.col);
|
|
|
+ cell.tag(active);
|
|
|
+ hitinfo.sheet.repaint(hitinfo.cellRect);
|
|
|
+ }
|
|
|
+ };
|
|
|
+ proto.processMouseUp = function (hitinfo) {
|
|
|
+ const imageClick = hitinfo.sheet.zh_setting ? hitinfo.sheet.zh_setting.imageClick : null;
|
|
|
+ if (imageClick && Object.prototype.toString.apply(imageClick) === "[object Function]") {
|
|
|
+ const sortData = SpreadJsObj.getSortData(hitinfo.sheet);
|
|
|
+ const data = sortData ? sortData[hitinfo.row] : null;
|
|
|
+ imageClick(data);
|
|
|
+ const cell = hitinfo.sheet.getCell(hitinfo.row, hitinfo.col);
|
|
|
+ cell.tag(null);
|
|
|
+ hitinfo.sheet.repaint(hitinfo.cellRect);
|
|
|
+ }
|
|
|
+ };
|
|
|
+ /*
|
|
|
+ 注释部分以进入鼠标进入图片,点击图片为基准更新图片,鼠标快速移动时,可能失效
|
|
|
+ */
|
|
|
+ // proto.processMouseDown = function (hitinfo) {
|
|
|
+ // const img = this.getImage(hitinfo.sheet, hitinfo.row, hitinfo.col);
|
|
|
+ // const halfX = img.width / 2, halfY = img.height / 2;
|
|
|
+ // const centerX = hitinfo.cellRect.x + indent + halfX;
|
|
|
+ // const centerY = hitinfo.cellRect.y + hitinfo.cellRect.height / 2;
|
|
|
+ //
|
|
|
+ // if (Math.abs(hitinfo.x - centerX) < halfX && Math.abs(hitinfo.y - centerY) < halfY) {
|
|
|
+ // const cell = hitinfo.sheet.getCell(hitinfo.row, hitinfo.col);
|
|
|
+ // cell.tag(down);
|
|
|
+ // hitinfo.sheet.repaint(hitinfo.cellRect);
|
|
|
+ // }
|
|
|
+ // };
|
|
|
+ // proto.processMouseUp = function (hitinfo) {
|
|
|
+ // const img = this.getImage(hitinfo.sheet, hitinfo.row, hitinfo.col);
|
|
|
+ // const halfX = img.width / 2, halfY = img.height / 2;
|
|
|
+ // const centerX = hitinfo.cellRect.x + indent + halfX;
|
|
|
+ // const centerY = hitinfo.cellRect.y + hitinfo.cellRect.height / 2;
|
|
|
+ //
|
|
|
+ // // 点击展开节点时,如果已加载子项,则展开,反之这加载子项,展开
|
|
|
+ // if (Math.abs(hitinfo.x - centerX) < halfX && Math.abs(hitinfo.y - centerY) < halfY) {
|
|
|
+ // const imageClick = hitinfo.sheet.zh_setting ? hitinfo.sheet.zh_setting.imageClick : null;
|
|
|
+ // if (imageClick && Object.prototype.toString.apply(imageClick) === "[object Function]") {
|
|
|
+ // const sortData = SpreadJsObj.getSortData(hitinfo.sheet);
|
|
|
+ // const data = sortData ? sortData[hitinfo.row] : null;
|
|
|
+ // imageClick(data);
|
|
|
+ // const cell = hitinfo.sheet.getCell(hitinfo.row, hitinfo.col);
|
|
|
+ // cell.tag(null);
|
|
|
+ // hitinfo.sheet.repaint(hitinfo.cellRect);
|
|
|
+ // }
|
|
|
+ // }
|
|
|
+ // };
|
|
|
+ // proto.processMouseMove = function (hitinfo) {
|
|
|
+ // const img = this.getImage(hitinfo.sheet, hitinfo.row, hitinfo.col);
|
|
|
+ // const halfX = img.width / 2, halfY = img.height / 2;
|
|
|
+ // const centerX = hitinfo.cellRect.x + indent + halfX;
|
|
|
+ // const centerY = hitinfo.cellRect.y + hitinfo.cellRect.height / 2;
|
|
|
+ // const cell = hitinfo.sheet.getCell(hitinfo.row, hitinfo.col);
|
|
|
+ // if (Math.abs(hitinfo.x - centerX) < halfX && Math.abs(hitinfo.y - centerY) < halfY) {
|
|
|
+ // if (cell.tag() !== hover) {
|
|
|
+ // cell.tag(hover);
|
|
|
+ // hitinfo.sheet.repaint(hitinfo.cellRect);
|
|
|
+ // }
|
|
|
+ // } else {
|
|
|
+ // if (cell.tag() === hover) {
|
|
|
+ // cell.tag(null);
|
|
|
+ // hitinfo.sheet.repaint(hitinfo.cellRect);
|
|
|
+ // }
|
|
|
+ // }
|
|
|
+ // };
|
|
|
+ return new ImageCellType();
|
|
|
+ },
|
|
|
+ /**
|
|
|
+ * 获取 嵌入Html的cellType
|
|
|
+ * @returns {HTMLCellType}
|
|
|
+ */
|
|
|
+ getHtmlCellType: function () {
|
|
|
+ const HTMLCellType = function (){};
|
|
|
+ HTMLCellType.prototype = new spreadNS.CellTypes.Text;
|
|
|
+ const proto = ImageCellType.prototype;
|
|
|
+ proto.paint = function (ctx, value, x, y, w, h, style, context) {
|
|
|
+ let DOMURL = window.URL || window.webkitURL || window;
|
|
|
+ let cell = context.sheet.getCell(context.row, context.col);
|
|
|
+ let img = cell.tag();
|
|
|
+ if (img) {
|
|
|
+ try {
|
|
|
+ ctx.save();
|
|
|
+ ctx.rect(x, y, w, h);
|
|
|
+ ctx.clip();
|
|
|
+ ctx.drawImage(img, x + 2, y + 2)
|
|
|
+ ctx.restore();
|
|
|
+ cell.tag(null);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ catch (err) {
|
|
|
+ GC.Spread.Sheets.CustomCellType.prototype.paint.apply(this, [ctx, "#HTMLError", x, y, w, h, style, context])
|
|
|
+ cell.tag(null);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ let svgPattern = '<svg xmlns="http://www.w3.org/2000/svg" width="{0}" height="{1}">' +
|
|
|
+ '<foreignObject width="100%" height="100%"><div xmlns="http://www.w3.org/1999/xhtml" style="font:{2}">{3}</div></foreignObject></svg>';
|
|
|
+
|
|
|
+ let data = svgPattern.replace("{0}", w).replace("{1}", h).replace("{2}", style.font).replace("{3}", value);
|
|
|
+ let doc = document.implementation.createHTMLDocument("");
|
|
|
+ doc.write(data);
|
|
|
+ // Get well-formed markup
|
|
|
+ data = (new XMLSerializer()).serializeToString(doc.body.children[0]);
|
|
|
+
|
|
|
+ img = new Image();
|
|
|
+ //var svg = new Blob([data], {type: 'image/svg+xml;charset=utf-8'});
|
|
|
+ //var url = DOMURL.createObjectURL(svg);
|
|
|
+ //img.src = url;
|
|
|
+ img.src = 'data:image/svg+xml;base64,' + window.btoa(data);
|
|
|
+ cell.tag(img);
|
|
|
+ img.onload = function () {
|
|
|
+ context.sheet.repaint(new GC.Spread.Sheets.Rect(x, y, w, h));
|
|
|
+ }
|
|
|
+ };
|
|
|
+ return new HTMLCellType();
|
|
|
+ },
|
|
|
+ /**
|
|
|
+ * 获取 字符超长缩略的cellType
|
|
|
+ * @returns {EllipsisTextCellType}
|
|
|
+ */
|
|
|
+ getEllipsisTextCellType: function () {
|
|
|
+ const EllipsisTextCellType = function (){};
|
|
|
+ EllipsisTextCellType.prototype = new spreadNS.CellTypes.Text;
|
|
|
+ const proto = EllipsisTextCellType.prototype;
|
|
|
+ const getEllipsisText = function(c, str, maxWidth) {
|
|
|
+ var width = c.measureText(str).width;
|
|
|
+ var ellipsis = '…';
|
|
|
+ var ellipsisWidth = c.measureText(ellipsis).width;
|
|
|
+ if (width <= maxWidth || width <= ellipsisWidth) {
|
|
|
+ return str;
|
|
|
+ } else {
|
|
|
+ var len = str.length;
|
|
|
+ while (width >= maxWidth - ellipsisWidth && len-- > 0) {
|
|
|
+ str = str.substring(0, len);
|
|
|
+ width = c.measureText(str).width;
|
|
|
+ }
|
|
|
+ return str + ellipsis;
|
|
|
+ }
|
|
|
+ };
|
|
|
+ proto.paint = function (ctx, value, x, y, w, h, style, context) {
|
|
|
+ ctx.font = style.font;
|
|
|
+ value = getEllipsisText(ctx, value, w - 2);
|
|
|
+ spreadNS.CellTypes.Text.prototype.paint.apply(this, [ctx, value, x, y, w, h, style, context]);
|
|
|
+ };
|
|
|
+ return new EllipsisTextCellType();
|
|
|
+ },
|
|
|
}
|
|
|
};
|