@protobi/exceljs
Version:
Excel Workbook Manager - Temporary fork with pivot table enhancements and bug fixes pending upstream merge
393 lines (391 loc) • 13.5 kB
JavaScript
"use strict";
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
function _classCallCheck(a, n) { if (!(a instanceof n)) throw new TypeError("Cannot call a class as a function"); }
function _defineProperties(e, r) { for (var t = 0; t < r.length; t++) { var o = r[t]; o.enumerable = o.enumerable || !1, o.configurable = !0, "value" in o && (o.writable = !0), Object.defineProperty(e, _toPropertyKey(o.key), o); } }
function _createClass(e, r, t) { return r && _defineProperties(e.prototype, r), t && _defineProperties(e, t), Object.defineProperty(e, "prototype", { writable: !1 }), e; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; }
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
function _inherits(t, e) { if ("function" != typeof e && null !== e) throw new TypeError("Super expression must either be null or a function"); t.prototype = Object.create(e && e.prototype, { constructor: { value: t, writable: !0, configurable: !0 } }), Object.defineProperty(t, "prototype", { writable: !1 }), e && _setPrototypeOf(t, e); }
function _setPrototypeOf(t, e) { return _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function (t, e) { return t.__proto__ = e, t; }, _setPrototypeOf(t, e); }
function _createSuper(t) { var r = _isNativeReflectConstruct(); return function () { var e, o = _getPrototypeOf(t); if (r) { var s = _getPrototypeOf(this).constructor; e = Reflect.construct(o, arguments, s); } else e = o.apply(this, arguments); return _possibleConstructorReturn(this, e); }; }
function _possibleConstructorReturn(t, e) { if (e && ("object" == _typeof(e) || "function" == typeof e)) return e; if (void 0 !== e) throw new TypeError("Derived constructors may only return object or undefined"); return _assertThisInitialized(t); }
function _assertThisInitialized(e) { if (void 0 === e) throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); return e; }
function _isNativeReflectConstruct() { try { var t = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); } catch (t) {} return (_isNativeReflectConstruct = function _isNativeReflectConstruct() { return !!t; })(); }
function _getPrototypeOf(t) { return _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf.bind() : function (t) { return t.__proto__ || Object.getPrototypeOf(t); }, _getPrototypeOf(t); }
/* eslint-disable max-classes-per-file */
var BaseXform = require('../base-xform');
var ColorXform = require('./color-xform');
var StopXform = /*#__PURE__*/function (_BaseXform) {
_inherits(StopXform, _BaseXform);
var _super = _createSuper(StopXform);
function StopXform() {
var _this;
_classCallCheck(this, StopXform);
_this = _super.call(this);
_this.map = {
color: new ColorXform()
};
return _this;
}
_createClass(StopXform, [{
key: "tag",
get: function get() {
return 'stop';
}
}, {
key: "render",
value: function render(xmlStream, model) {
xmlStream.openNode('stop');
xmlStream.addAttribute('position', model.position);
this.map.color.render(xmlStream, model.color);
xmlStream.closeNode();
}
}, {
key: "parseOpen",
value: function parseOpen(node) {
if (this.parser) {
this.parser.parseOpen(node);
return true;
}
switch (node.name) {
case 'stop':
this.model = {
position: parseFloat(node.attributes.position)
};
return true;
case 'color':
this.parser = this.map.color;
this.parser.parseOpen(node);
return true;
default:
return false;
}
}
}, {
key: "parseText",
value: function parseText() {}
}, {
key: "parseClose",
value: function parseClose(name) {
if (this.parser) {
if (!this.parser.parseClose(name)) {
this.model.color = this.parser.model;
this.parser = undefined;
}
return true;
}
return false;
}
}]);
return StopXform;
}(BaseXform);
var PatternFillXform = /*#__PURE__*/function (_BaseXform2) {
_inherits(PatternFillXform, _BaseXform2);
var _super2 = _createSuper(PatternFillXform);
function PatternFillXform() {
var _this2;
_classCallCheck(this, PatternFillXform);
_this2 = _super2.call(this);
_this2.map = {
fgColor: new ColorXform('fgColor'),
bgColor: new ColorXform('bgColor')
};
return _this2;
}
_createClass(PatternFillXform, [{
key: "name",
get: function get() {
return 'pattern';
}
}, {
key: "tag",
get: function get() {
return 'patternFill';
}
}, {
key: "render",
value: function render(xmlStream, model) {
xmlStream.openNode('patternFill');
xmlStream.addAttribute('patternType', model.pattern);
if (model.fgColor) {
this.map.fgColor.render(xmlStream, model.fgColor);
}
if (model.bgColor) {
this.map.bgColor.render(xmlStream, model.bgColor);
}
xmlStream.closeNode();
}
}, {
key: "parseOpen",
value: function parseOpen(node) {
if (this.parser) {
this.parser.parseOpen(node);
return true;
}
switch (node.name) {
case 'patternFill':
this.model = {
type: 'pattern',
pattern: node.attributes.patternType
};
return true;
default:
this.parser = this.map[node.name];
if (this.parser) {
this.parser.parseOpen(node);
return true;
}
return false;
}
}
}, {
key: "parseText",
value: function parseText(text) {
if (this.parser) {
this.parser.parseText(text);
}
}
}, {
key: "parseClose",
value: function parseClose(name) {
if (this.parser) {
if (!this.parser.parseClose(name)) {
if (this.parser.model) {
this.model[name] = this.parser.model;
}
this.parser = undefined;
}
return true;
}
return false;
}
}]);
return PatternFillXform;
}(BaseXform);
var GradientFillXform = /*#__PURE__*/function (_BaseXform3) {
_inherits(GradientFillXform, _BaseXform3);
var _super3 = _createSuper(GradientFillXform);
function GradientFillXform() {
var _this3;
_classCallCheck(this, GradientFillXform);
_this3 = _super3.call(this);
_this3.map = {
stop: new StopXform()
};
// if (model) {
// this.gradient = model.gradient;
// if (model.center) {
// this.center = model.center;
// }
// if (model.degree !== undefined) {
// this.degree = model.degree;
// }
// this.stops = model.stops.map(function(stop) { return new StopXform(stop); });
// } else {
// this.stops = [];
// }
return _this3;
}
_createClass(GradientFillXform, [{
key: "name",
get: function get() {
return 'gradient';
}
}, {
key: "tag",
get: function get() {
return 'gradientFill';
}
}, {
key: "render",
value: function render(xmlStream, model) {
xmlStream.openNode('gradientFill');
switch (model.gradient) {
case 'angle':
xmlStream.addAttribute('degree', model.degree);
break;
case 'path':
xmlStream.addAttribute('type', 'path');
if (model.center.left) {
xmlStream.addAttribute('left', model.center.left);
if (model.center.right === undefined) {
xmlStream.addAttribute('right', model.center.left);
}
}
if (model.center.right) {
xmlStream.addAttribute('right', model.center.right);
}
if (model.center.top) {
xmlStream.addAttribute('top', model.center.top);
if (model.center.bottom === undefined) {
xmlStream.addAttribute('bottom', model.center.top);
}
}
if (model.center.bottom) {
xmlStream.addAttribute('bottom', model.center.bottom);
}
break;
default:
break;
}
var stopXform = this.map.stop;
model.stops.forEach(function (stopModel) {
stopXform.render(xmlStream, stopModel);
});
xmlStream.closeNode();
}
}, {
key: "parseOpen",
value: function parseOpen(node) {
if (this.parser) {
this.parser.parseOpen(node);
return true;
}
switch (node.name) {
case 'gradientFill':
{
var model = this.model = {
stops: []
};
if (node.attributes.degree) {
model.gradient = 'angle';
model.degree = parseInt(node.attributes.degree, 10);
} else if (node.attributes.type === 'path') {
model.gradient = 'path';
model.center = {
left: node.attributes.left ? parseFloat(node.attributes.left) : 0,
top: node.attributes.top ? parseFloat(node.attributes.top) : 0
};
if (node.attributes.right !== node.attributes.left) {
model.center.right = node.attributes.right ? parseFloat(node.attributes.right) : 0;
}
if (node.attributes.bottom !== node.attributes.top) {
model.center.bottom = node.attributes.bottom ? parseFloat(node.attributes.bottom) : 0;
}
}
return true;
}
case 'stop':
this.parser = this.map.stop;
this.parser.parseOpen(node);
return true;
default:
return false;
}
}
}, {
key: "parseText",
value: function parseText(text) {
if (this.parser) {
this.parser.parseText(text);
}
}
}, {
key: "parseClose",
value: function parseClose(name) {
if (this.parser) {
if (!this.parser.parseClose(name)) {
this.model.stops.push(this.parser.model);
this.parser = undefined;
}
return true;
}
return false;
}
}]);
return GradientFillXform;
}(BaseXform); // Fill encapsulates translation from fill model to/from xlsx
var FillXform = /*#__PURE__*/function (_BaseXform4) {
_inherits(FillXform, _BaseXform4);
var _super4 = _createSuper(FillXform);
function FillXform() {
var _this4;
_classCallCheck(this, FillXform);
_this4 = _super4.call(this);
_this4.map = {
patternFill: new PatternFillXform(),
gradientFill: new GradientFillXform()
};
return _this4;
}
_createClass(FillXform, [{
key: "tag",
get: function get() {
return 'fill';
}
}, {
key: "render",
value: function render(xmlStream, model) {
xmlStream.addRollback();
xmlStream.openNode('fill');
switch (model.type) {
case 'pattern':
this.map.patternFill.render(xmlStream, model);
break;
case 'gradient':
this.map.gradientFill.render(xmlStream, model);
break;
default:
xmlStream.rollback();
return;
}
xmlStream.closeNode();
xmlStream.commit();
}
}, {
key: "parseOpen",
value: function parseOpen(node) {
if (this.parser) {
this.parser.parseOpen(node);
return true;
}
switch (node.name) {
case 'fill':
this.model = {};
return true;
default:
this.parser = this.map[node.name];
if (this.parser) {
this.parser.parseOpen(node);
return true;
}
return false;
}
}
}, {
key: "parseText",
value: function parseText(text) {
if (this.parser) {
this.parser.parseText(text);
}
}
}, {
key: "parseClose",
value: function parseClose(name) {
if (this.parser) {
if (!this.parser.parseClose(name)) {
this.model = this.parser.model;
this.model.type = this.parser.name;
this.parser = undefined;
}
return true;
}
return false;
}
}, {
key: "validStyle",
value: function validStyle(value) {
return FillXform.validPatternValues[value];
}
}]);
return FillXform;
}(BaseXform);
FillXform.validPatternValues = ['none', 'solid', 'darkVertical', 'darkGray', 'mediumGray', 'lightGray', 'gray125', 'gray0625', 'darkHorizontal', 'darkVertical', 'darkDown', 'darkUp', 'darkGrid', 'darkTrellis', 'lightHorizontal', 'lightVertical', 'lightDown', 'lightUp', 'lightGrid', 'lightTrellis', 'lightGrid'].reduce(function (p, v) {
p[v] = true;
return p;
}, {});
FillXform.StopXform = StopXform;
FillXform.PatternFillXform = PatternFillXform;
FillXform.GradientFillXform = GradientFillXform;
module.exports = FillXform;
//# sourceMappingURL=fill-xform.js.map