@difizen/mana-core
Version:
1,546 lines (1,538 loc) • 50.1 kB
JavaScript
var _ContextKeyFalseExpr, _ContextKeyTrueExpr;
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); Object.defineProperty(subClass, "prototype", { writable: false }); if (superClass) _setPrototypeOf(subClass, superClass); }
function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; }
function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } else if (call !== void 0) { throw new TypeError("Derived constructors may only return object or undefined"); } return _assertThisInitialized(self); }
function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } }
function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf.bind() : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }
function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); }
function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); }
function _createForOfIteratorHelper(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (!it) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e) { throw _e; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = it.call(o); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e2) { didErr = true; err = _e2; }, f: function f() { try { if (!normalCompletion && it.return != null) it.return(); } finally { if (didErr) throw err; } } }; }
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
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(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, _toPropertyKey(descriptor.key), descriptor); } }
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : String(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); }
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { isFalsyOrWhitespace } from '@difizen/mana-common';
// import { createDecorator } from 'vs/platform/instantiation/common/instantiation';
import { userAgent, isMacintosh, isLinux, isWindows, isWeb } from '@difizen/mana-common';
import { Syringe } from '@difizen/mana-syringe';
var _userAgent = userAgent || '';
var STATIC_VALUES = new Map();
STATIC_VALUES.set('false', false);
STATIC_VALUES.set('true', true);
STATIC_VALUES.set('isMac', isMacintosh);
STATIC_VALUES.set('isLinux', isLinux);
STATIC_VALUES.set('isWindows', isWindows);
STATIC_VALUES.set('isWeb', isWeb);
STATIC_VALUES.set('isMacNative', isMacintosh && !isWeb);
STATIC_VALUES.set('isEdge', _userAgent.indexOf('Edg/') >= 0);
STATIC_VALUES.set('isFirefox', _userAgent.indexOf('Firefox') >= 0);
STATIC_VALUES.set('isChrome', _userAgent.indexOf('Chrome') >= 0);
STATIC_VALUES.set('isSafari', _userAgent.indexOf('Safari') >= 0);
STATIC_VALUES.set('isIPad', _userAgent.indexOf('iPad') >= 0);
var hasOwnProperty = Object.prototype.hasOwnProperty;
export var ContextKeyExprType = /*#__PURE__*/function (ContextKeyExprType) {
ContextKeyExprType[ContextKeyExprType["False"] = 0] = "False";
ContextKeyExprType[ContextKeyExprType["True"] = 1] = "True";
ContextKeyExprType[ContextKeyExprType["Defined"] = 2] = "Defined";
ContextKeyExprType[ContextKeyExprType["Not"] = 3] = "Not";
ContextKeyExprType[ContextKeyExprType["Equals"] = 4] = "Equals";
ContextKeyExprType[ContextKeyExprType["NotEquals"] = 5] = "NotEquals";
ContextKeyExprType[ContextKeyExprType["And"] = 6] = "And";
ContextKeyExprType[ContextKeyExprType["Regex"] = 7] = "Regex";
ContextKeyExprType[ContextKeyExprType["NotRegex"] = 8] = "NotRegex";
ContextKeyExprType[ContextKeyExprType["Or"] = 9] = "Or";
ContextKeyExprType[ContextKeyExprType["In"] = 10] = "In";
ContextKeyExprType[ContextKeyExprType["NotIn"] = 11] = "NotIn";
ContextKeyExprType[ContextKeyExprType["Greater"] = 12] = "Greater";
ContextKeyExprType[ContextKeyExprType["GreaterEquals"] = 13] = "GreaterEquals";
ContextKeyExprType[ContextKeyExprType["Smaller"] = 14] = "Smaller";
ContextKeyExprType[ContextKeyExprType["SmallerEquals"] = 15] = "SmallerEquals";
return ContextKeyExprType;
}({});
export var ContextKeyExpr = /*#__PURE__*/function () {
function ContextKeyExpr() {
_classCallCheck(this, ContextKeyExpr);
}
_createClass(ContextKeyExpr, null, [{
key: "false",
value: function _false() {
return ContextKeyFalseExpr.INSTANCE;
}
}, {
key: "true",
value: function _true() {
return ContextKeyTrueExpr.INSTANCE;
}
}, {
key: "has",
value: function has(key) {
return ContextKeyDefinedExpr.create(key);
}
}, {
key: "equals",
value: function equals(key, value) {
return ContextKeyEqualsExpr.create(key, value);
}
}, {
key: "notEquals",
value: function notEquals(key, value) {
return ContextKeyNotEqualsExpr.create(key, value);
}
}, {
key: "regex",
value: function regex(key, value) {
return ContextKeyRegexExpr.create(key, value);
}
}, {
key: "in",
value: function _in(key, value) {
return ContextKeyInExpr.create(key, value);
}
}, {
key: "not",
value: function not(key) {
return ContextKeyNotExpr.create(key);
}
}, {
key: "and",
value: function and() {
for (var _len = arguments.length, expr = new Array(_len), _key = 0; _key < _len; _key++) {
expr[_key] = arguments[_key];
}
return ContextKeyAndExpr.create(expr);
}
}, {
key: "or",
value: function or() {
for (var _len2 = arguments.length, expr = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
expr[_key2] = arguments[_key2];
}
return ContextKeyOrExpr.create(expr);
}
}, {
key: "greater",
value: function greater(key, value) {
return ContextKeyGreaterExpr.create(key, value);
}
}, {
key: "less",
value: function less(key, value) {
return ContextKeySmallerExpr.create(key, value);
}
}, {
key: "deserialize",
value: function deserialize(serialized) {
var strict = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
if (!serialized) {
return undefined;
}
return this._deserializeOrExpression(serialized, strict);
}
}, {
key: "_deserializeOrExpression",
value: function _deserializeOrExpression(serialized, strict) {
var _this = this;
var pieces = serialized.split('||');
return ContextKeyOrExpr.create(pieces.map(function (p) {
return _this._deserializeAndExpression(p, strict);
}));
}
}, {
key: "_deserializeAndExpression",
value: function _deserializeAndExpression(serialized, strict) {
var _this2 = this;
var pieces = serialized.split('&&');
return ContextKeyAndExpr.create(pieces.map(function (p) {
return _this2._deserializeOne(p, strict);
}));
}
}, {
key: "_deserializeOne",
value: function _deserializeOne(serializedOne, strict) {
serializedOne = serializedOne.trim();
if (serializedOne.indexOf('!=') >= 0) {
var pieces = serializedOne.split('!=');
return ContextKeyNotEqualsExpr.create(pieces[0].trim(), this._deserializeValue(pieces[1], strict));
}
if (serializedOne.indexOf('==') >= 0) {
var _pieces = serializedOne.split('==');
return ContextKeyEqualsExpr.create(_pieces[0].trim(), this._deserializeValue(_pieces[1], strict));
}
if (serializedOne.indexOf('=~') >= 0) {
var _pieces2 = serializedOne.split('=~');
return ContextKeyRegexExpr.create(_pieces2[0].trim(), this._deserializeRegexValue(_pieces2[1], strict));
}
if (serializedOne.indexOf(' in ') >= 0) {
var _pieces3 = serializedOne.split(' in ');
return ContextKeyInExpr.create(_pieces3[0].trim(), _pieces3[1].trim());
}
if (/^[^<=>]+>=[^<=>]+$/.test(serializedOne)) {
var _pieces4 = serializedOne.split('>=');
return ContextKeyGreaterEqualsExpr.create(_pieces4[0].trim(), _pieces4[1].trim());
}
if (/^[^<=>]+>[^<=>]+$/.test(serializedOne)) {
var _pieces5 = serializedOne.split('>');
return ContextKeyGreaterExpr.create(_pieces5[0].trim(), _pieces5[1].trim());
}
if (/^[^<=>]+<=[^<=>]+$/.test(serializedOne)) {
var _pieces6 = serializedOne.split('<=');
return ContextKeySmallerEqualsExpr.create(_pieces6[0].trim(), _pieces6[1].trim());
}
if (/^[^<=>]+<[^<=>]+$/.test(serializedOne)) {
var _pieces7 = serializedOne.split('<');
return ContextKeySmallerExpr.create(_pieces7[0].trim(), _pieces7[1].trim());
}
// eslint-disable-next-line no-useless-escape
if (/^\!\s*/.test(serializedOne)) {
return ContextKeyNotExpr.create(serializedOne.substr(1).trim());
}
return ContextKeyDefinedExpr.create(serializedOne);
}
}, {
key: "_deserializeValue",
value: function _deserializeValue(serializedValue, _strict) {
serializedValue = serializedValue.trim();
if (serializedValue === 'true') {
return true;
}
if (serializedValue === 'false') {
return false;
}
var m = /^'([^']*)'$/.exec(serializedValue);
if (m) {
return m[1].trim();
}
return serializedValue;
}
}, {
key: "_deserializeRegexValue",
value: function _deserializeRegexValue(serializedValue, strict) {
if (isFalsyOrWhitespace(serializedValue)) {
if (strict) {
throw new Error('missing regexp-value for =~-expression');
} else {
console.warn('missing regexp-value for =~-expression');
}
return null;
}
var start = serializedValue.indexOf('/');
var end = serializedValue.lastIndexOf('/');
if (start === end || start < 0 /* || to < 0 */) {
if (strict) {
throw new Error("bad regexp-value '".concat(serializedValue, "', missing /-enclosure"));
} else {
console.warn("bad regexp-value '".concat(serializedValue, "', missing /-enclosure"));
}
return null;
}
var value = serializedValue.slice(start + 1, end);
var caseIgnoreFlag = serializedValue[end + 1] === 'i' ? 'i' : '';
try {
return new RegExp(value, caseIgnoreFlag);
} catch (e) {
if (strict) {
throw new Error("bad regexp-value '".concat(serializedValue, "', parse error: ").concat(e));
} else {
console.warn("bad regexp-value '".concat(serializedValue, "', parse error: ").concat(e));
}
return null;
}
}
}]);
return ContextKeyExpr;
}();
function _cmp(a, b) {
return a.cmp(b);
}
export var ContextKeyFalseExpr = /*#__PURE__*/function () {
function ContextKeyFalseExpr() {
_classCallCheck(this, ContextKeyFalseExpr);
this.type = ContextKeyExprType.False;
} //
_createClass(ContextKeyFalseExpr, [{
key: "cmp",
value: function cmp(other) {
return this.type - other.type;
}
}, {
key: "equals",
value: function equals(other) {
return other.type === this.type;
}
}, {
key: "evaluate",
value: function evaluate(_context) {
return false;
}
}, {
key: "serialize",
value: function serialize() {
return 'false';
}
}, {
key: "keys",
value: function keys() {
return [];
}
}, {
key: "map",
value: function map(_mapFnc) {
return this;
}
}, {
key: "negate",
value: function negate() {
return ContextKeyTrueExpr.INSTANCE;
}
}]);
return ContextKeyFalseExpr;
}();
_ContextKeyFalseExpr = ContextKeyFalseExpr;
ContextKeyFalseExpr.INSTANCE = new _ContextKeyFalseExpr();
export var ContextKeyTrueExpr = /*#__PURE__*/function () {
function ContextKeyTrueExpr() {
_classCallCheck(this, ContextKeyTrueExpr);
this.type = ContextKeyExprType.True;
} //
_createClass(ContextKeyTrueExpr, [{
key: "cmp",
value: function cmp(other) {
return this.type - other.type;
}
}, {
key: "equals",
value: function equals(other) {
return other.type === this.type;
}
}, {
key: "evaluate",
value: function evaluate(_context) {
return true;
}
}, {
key: "serialize",
value: function serialize() {
return 'true';
}
}, {
key: "keys",
value: function keys() {
return [];
}
}, {
key: "map",
value: function map(_mapFnc) {
return this;
}
}, {
key: "negate",
value: function negate() {
return ContextKeyFalseExpr.INSTANCE;
}
}]);
return ContextKeyTrueExpr;
}();
_ContextKeyTrueExpr = ContextKeyTrueExpr;
ContextKeyTrueExpr.INSTANCE = new _ContextKeyTrueExpr();
export var ContextKeyDefinedExpr = /*#__PURE__*/function () {
function ContextKeyDefinedExpr(key) {
_classCallCheck(this, ContextKeyDefinedExpr);
this.type = ContextKeyExprType.Defined;
this.key = key;
}
_createClass(ContextKeyDefinedExpr, [{
key: "cmp",
value: function cmp(other) {
if (other.type !== this.type) {
return this.type - other.type;
}
return cmp1(this.key, other.key);
}
}, {
key: "equals",
value: function equals(other) {
if (other.type === this.type) {
return this.key === other.key;
}
return false;
}
}, {
key: "evaluate",
value: function evaluate(context) {
return !!context.getValue(this.key);
}
}, {
key: "serialize",
value: function serialize() {
return this.key;
}
}, {
key: "keys",
value: function keys() {
return [this.key];
}
}, {
key: "map",
value: function map(mapFnc) {
return mapFnc.mapDefined(this.key);
}
}, {
key: "negate",
value: function negate() {
return ContextKeyNotExpr.create(this.key);
}
}], [{
key: "create",
value: function create(key) {
var staticValue = STATIC_VALUES.get(key);
if (typeof staticValue === 'boolean') {
return staticValue ? ContextKeyTrueExpr.INSTANCE : ContextKeyFalseExpr.INSTANCE;
}
return new ContextKeyDefinedExpr(key);
}
}]);
return ContextKeyDefinedExpr;
}();
export var ContextKeyEqualsExpr = /*#__PURE__*/function () {
function ContextKeyEqualsExpr(key, value) {
_classCallCheck(this, ContextKeyEqualsExpr);
this.type = ContextKeyExprType.Equals;
this.key = key;
this.value = value;
}
_createClass(ContextKeyEqualsExpr, [{
key: "cmp",
value: function cmp(other) {
if (other.type !== this.type) {
return this.type - other.type;
}
return cmp2(this.key, this.value, other.key, other.value);
}
}, {
key: "equals",
value: function equals(other) {
if (other.type === this.type) {
return this.key === other.key && this.value === other.value;
}
return false;
}
}, {
key: "evaluate",
value: function evaluate(context) {
// Intentional ==
// eslint-disable-next-line eqeqeq
return context.getValue(this.key) == this.value;
}
}, {
key: "serialize",
value: function serialize() {
return "".concat(this.key, " == '").concat(this.value, "'");
}
}, {
key: "keys",
value: function keys() {
return [this.key];
}
}, {
key: "map",
value: function map(mapFnc) {
return mapFnc.mapEquals(this.key, this.value);
}
}, {
key: "negate",
value: function negate() {
return ContextKeyNotEqualsExpr.create(this.key, this.value);
}
}], [{
key: "create",
value: function create(key, value) {
if (typeof value === 'boolean') {
return value ? ContextKeyDefinedExpr.create(key) : ContextKeyNotExpr.create(key);
}
var staticValue = STATIC_VALUES.get(key);
if (typeof staticValue === 'boolean') {
var trueValue = staticValue ? 'true' : 'false';
return value === trueValue ? ContextKeyTrueExpr.INSTANCE : ContextKeyFalseExpr.INSTANCE;
}
return new ContextKeyEqualsExpr(key, value);
}
}]);
return ContextKeyEqualsExpr;
}();
export var ContextKeyInExpr = /*#__PURE__*/function () {
function ContextKeyInExpr(key, valueKey) {
_classCallCheck(this, ContextKeyInExpr);
this.type = ContextKeyExprType.In;
this.key = key;
this.valueKey = valueKey;
}
_createClass(ContextKeyInExpr, [{
key: "cmp",
value: function cmp(other) {
if (other.type !== this.type) {
return this.type - other.type;
}
return cmp2(this.key, this.valueKey, other.key, other.valueKey);
}
}, {
key: "equals",
value: function equals(other) {
if (other.type === this.type) {
return this.key === other.key && this.valueKey === other.valueKey;
}
return false;
}
}, {
key: "evaluate",
value: function evaluate(context) {
var source = context.getValue(this.valueKey);
var item = context.getValue(this.key);
if (Array.isArray(source)) {
return source.indexOf(item) >= 0;
}
if (typeof item === 'string' && _typeof(source) === 'object' && source !== null) {
return hasOwnProperty.call(source, item);
}
return false;
}
}, {
key: "serialize",
value: function serialize() {
return "".concat(this.key, " in '").concat(this.valueKey, "'");
}
}, {
key: "keys",
value: function keys() {
return [this.key, this.valueKey];
}
}, {
key: "map",
value: function map(mapFnc) {
return mapFnc.mapIn(this.key, this.valueKey);
}
}, {
key: "negate",
value: function negate() {
return ContextKeyNotInExpr.create(this);
}
}], [{
key: "create",
value: function create(key, valueKey) {
return new ContextKeyInExpr(key, valueKey);
}
}]);
return ContextKeyInExpr;
}();
export var ContextKeyNotInExpr = /*#__PURE__*/function () {
function ContextKeyNotInExpr(_actual) {
_classCallCheck(this, ContextKeyNotInExpr);
this.type = ContextKeyExprType.NotIn;
//
this._actual = _actual;
}
_createClass(ContextKeyNotInExpr, [{
key: "cmp",
value: function cmp(other) {
if (other.type !== this.type) {
return this.type - other.type;
}
return this._actual.cmp(other._actual);
}
}, {
key: "equals",
value: function equals(other) {
if (other.type === this.type) {
return this._actual.equals(other._actual);
}
return false;
}
}, {
key: "evaluate",
value: function evaluate(context) {
return !this._actual.evaluate(context);
}
}, {
key: "serialize",
value: function serialize() {
throw new Error('Method not implemented.');
}
}, {
key: "keys",
value: function keys() {
return this._actual.keys();
}
}, {
key: "map",
value: function map(mapFnc) {
return new ContextKeyNotInExpr(this._actual.map(mapFnc));
}
}, {
key: "negate",
value: function negate() {
return this._actual;
}
}], [{
key: "create",
value: function create(actual) {
return new ContextKeyNotInExpr(actual);
}
}]);
return ContextKeyNotInExpr;
}();
export var ContextKeyNotEqualsExpr = /*#__PURE__*/function () {
function ContextKeyNotEqualsExpr(key, value) {
_classCallCheck(this, ContextKeyNotEqualsExpr);
this.type = ContextKeyExprType.NotEquals;
this.key = key;
this.value = value;
}
_createClass(ContextKeyNotEqualsExpr, [{
key: "cmp",
value: function cmp(other) {
if (other.type !== this.type) {
return this.type - other.type;
}
return cmp2(this.key, this.value, other.key, other.value);
}
}, {
key: "equals",
value: function equals(other) {
if (other.type === this.type) {
return this.key === other.key && this.value === other.value;
}
return false;
}
}, {
key: "evaluate",
value: function evaluate(context) {
// Intentional !=
// eslint-disable-next-line eqeqeq
return context.getValue(this.key) != this.value;
}
}, {
key: "serialize",
value: function serialize() {
return "".concat(this.key, " != '").concat(this.value, "'");
}
}, {
key: "keys",
value: function keys() {
return [this.key];
}
}, {
key: "map",
value: function map(mapFnc) {
return mapFnc.mapNotEquals(this.key, this.value);
}
}, {
key: "negate",
value: function negate() {
return ContextKeyEqualsExpr.create(this.key, this.value);
}
}], [{
key: "create",
value: function create(key, value) {
if (typeof value === 'boolean') {
if (value) {
return ContextKeyNotExpr.create(key);
}
return ContextKeyDefinedExpr.create(key);
}
var staticValue = STATIC_VALUES.get(key);
if (typeof staticValue === 'boolean') {
var falseValue = staticValue ? 'true' : 'false';
return value === falseValue ? ContextKeyFalseExpr.INSTANCE : ContextKeyTrueExpr.INSTANCE;
}
return new ContextKeyNotEqualsExpr(key, value);
}
}]);
return ContextKeyNotEqualsExpr;
}();
export var ContextKeyNotExpr = /*#__PURE__*/function () {
function ContextKeyNotExpr(key) {
_classCallCheck(this, ContextKeyNotExpr);
this.type = ContextKeyExprType.Not;
this.key = key;
}
_createClass(ContextKeyNotExpr, [{
key: "cmp",
value: function cmp(other) {
if (other.type !== this.type) {
return this.type - other.type;
}
return cmp1(this.key, other.key);
}
}, {
key: "equals",
value: function equals(other) {
if (other.type === this.type) {
return this.key === other.key;
}
return false;
}
}, {
key: "evaluate",
value: function evaluate(context) {
return !context.getValue(this.key);
}
}, {
key: "serialize",
value: function serialize() {
return "!".concat(this.key);
}
}, {
key: "keys",
value: function keys() {
return [this.key];
}
}, {
key: "map",
value: function map(mapFnc) {
return mapFnc.mapNot(this.key);
}
}, {
key: "negate",
value: function negate() {
return ContextKeyDefinedExpr.create(this.key);
}
}], [{
key: "create",
value: function create(key) {
var staticValue = STATIC_VALUES.get(key);
if (typeof staticValue === 'boolean') {
return staticValue ? ContextKeyFalseExpr.INSTANCE : ContextKeyTrueExpr.INSTANCE;
}
return new ContextKeyNotExpr(key);
}
}]);
return ContextKeyNotExpr;
}();
export var ContextKeyGreaterExpr = /*#__PURE__*/function () {
function ContextKeyGreaterExpr(key, value) {
_classCallCheck(this, ContextKeyGreaterExpr);
this.type = ContextKeyExprType.Greater;
this.key = key;
this.value = value;
}
_createClass(ContextKeyGreaterExpr, [{
key: "cmp",
value: function cmp(other) {
if (other.type !== this.type) {
return this.type - other.type;
}
return cmp2(this.key, this.value, other.key, other.value);
}
}, {
key: "equals",
value: function equals(other) {
if (other.type === this.type) {
return this.key === other.key && this.value === other.value;
}
return false;
}
}, {
key: "evaluate",
value: function evaluate(context) {
return parseFloat(context.getValue(this.key)) > parseFloat(this.value);
}
}, {
key: "serialize",
value: function serialize() {
return "".concat(this.key, " > ").concat(this.value);
}
}, {
key: "keys",
value: function keys() {
return [this.key];
}
}, {
key: "map",
value: function map(mapFnc) {
return mapFnc.mapGreater(this.key, this.value);
}
}, {
key: "negate",
value: function negate() {
return ContextKeySmallerEqualsExpr.create(this.key, this.value);
}
}], [{
key: "create",
value: function create(key, value) {
return new ContextKeyGreaterExpr(key, value);
}
}]);
return ContextKeyGreaterExpr;
}();
export var ContextKeyGreaterEqualsExpr = /*#__PURE__*/function () {
function ContextKeyGreaterEqualsExpr(key, value) {
_classCallCheck(this, ContextKeyGreaterEqualsExpr);
this.type = ContextKeyExprType.GreaterEquals;
this.key = key;
this.value = value;
}
_createClass(ContextKeyGreaterEqualsExpr, [{
key: "cmp",
value: function cmp(other) {
if (other.type !== this.type) {
return this.type - other.type;
}
return cmp2(this.key, this.value, other.key, other.value);
}
}, {
key: "equals",
value: function equals(other) {
if (other.type === this.type) {
return this.key === other.key && this.value === other.value;
}
return false;
}
}, {
key: "evaluate",
value: function evaluate(context) {
return parseFloat(context.getValue(this.key)) >= parseFloat(this.value);
}
}, {
key: "serialize",
value: function serialize() {
return "".concat(this.key, " >= ").concat(this.value);
}
}, {
key: "keys",
value: function keys() {
return [this.key];
}
}, {
key: "map",
value: function map(mapFnc) {
return mapFnc.mapGreaterEquals(this.key, this.value);
}
}, {
key: "negate",
value: function negate() {
return ContextKeySmallerExpr.create(this.key, this.value);
}
}], [{
key: "create",
value: function create(key, value) {
return new ContextKeyGreaterEqualsExpr(key, value);
}
}]);
return ContextKeyGreaterEqualsExpr;
}();
export var ContextKeySmallerExpr = /*#__PURE__*/function () {
function ContextKeySmallerExpr(key, value) {
_classCallCheck(this, ContextKeySmallerExpr);
this.type = ContextKeyExprType.Smaller;
this.key = key;
this.value = value;
}
_createClass(ContextKeySmallerExpr, [{
key: "cmp",
value: function cmp(other) {
if (other.type !== this.type) {
return this.type - other.type;
}
return cmp2(this.key, this.value, other.key, other.value);
}
}, {
key: "equals",
value: function equals(other) {
if (other.type === this.type) {
return this.key === other.key && this.value === other.value;
}
return false;
}
}, {
key: "evaluate",
value: function evaluate(context) {
return parseFloat(context.getValue(this.key)) < parseFloat(this.value);
}
}, {
key: "serialize",
value: function serialize() {
return "".concat(this.key, " < ").concat(this.value);
}
}, {
key: "keys",
value: function keys() {
return [this.key];
}
}, {
key: "map",
value: function map(mapFnc) {
return mapFnc.mapSmaller(this.key, this.value);
}
}, {
key: "negate",
value: function negate() {
return ContextKeyGreaterEqualsExpr.create(this.key, this.value);
}
}], [{
key: "create",
value: function create(key, value) {
return new ContextKeySmallerExpr(key, value);
}
}]);
return ContextKeySmallerExpr;
}();
export var ContextKeySmallerEqualsExpr = /*#__PURE__*/function () {
function ContextKeySmallerEqualsExpr(key, value) {
_classCallCheck(this, ContextKeySmallerEqualsExpr);
this.type = ContextKeyExprType.SmallerEquals;
this.key = key;
this.value = value;
}
_createClass(ContextKeySmallerEqualsExpr, [{
key: "cmp",
value: function cmp(other) {
if (other.type !== this.type) {
return this.type - other.type;
}
return cmp2(this.key, this.value, other.key, other.value);
}
}, {
key: "equals",
value: function equals(other) {
if (other.type === this.type) {
return this.key === other.key && this.value === other.value;
}
return false;
}
}, {
key: "evaluate",
value: function evaluate(context) {
return parseFloat(context.getValue(this.key)) <= parseFloat(this.value);
}
}, {
key: "serialize",
value: function serialize() {
return "".concat(this.key, " <= ").concat(this.value);
}
}, {
key: "keys",
value: function keys() {
return [this.key];
}
}, {
key: "map",
value: function map(mapFnc) {
return mapFnc.mapSmallerEquals(this.key, this.value);
}
}, {
key: "negate",
value: function negate() {
return ContextKeyGreaterExpr.create(this.key, this.value);
}
}], [{
key: "create",
value: function create(key, value) {
return new ContextKeySmallerEqualsExpr(key, value);
}
}]);
return ContextKeySmallerEqualsExpr;
}();
export var ContextKeyRegexExpr = /*#__PURE__*/function () {
function ContextKeyRegexExpr(key, regexp) {
_classCallCheck(this, ContextKeyRegexExpr);
this.type = ContextKeyExprType.Regex;
//
this.key = key;
this.regexp = regexp;
}
_createClass(ContextKeyRegexExpr, [{
key: "cmp",
value: function cmp(other) {
if (other.type !== this.type) {
return this.type - other.type;
}
if (this.key < other.key) {
return -1;
}
if (this.key > other.key) {
return 1;
}
var thisSource = this.regexp ? this.regexp.source : '';
var otherSource = other.regexp ? other.regexp.source : '';
if (thisSource < otherSource) {
return -1;
}
if (thisSource > otherSource) {
return 1;
}
return 0;
}
}, {
key: "equals",
value: function equals(other) {
if (other.type === this.type) {
var thisSource = this.regexp ? this.regexp.source : '';
var otherSource = other.regexp ? other.regexp.source : '';
return this.key === other.key && thisSource === otherSource;
}
return false;
}
}, {
key: "evaluate",
value: function evaluate(context) {
var value = context.getValue(this.key);
return this.regexp ? this.regexp.test(value) : false;
}
}, {
key: "serialize",
value: function serialize() {
var value = this.regexp ? "/".concat(this.regexp.source, "/").concat(this.regexp.ignoreCase ? 'i' : '') : '/invalid/';
return "".concat(this.key, " =~ ").concat(value);
}
}, {
key: "keys",
value: function keys() {
return [this.key];
}
}, {
key: "map",
value: function map(mapFnc) {
return mapFnc.mapRegex(this.key, this.regexp);
}
}, {
key: "negate",
value: function negate() {
return ContextKeyNotRegexExpr.create(this);
}
}], [{
key: "create",
value: function create(key, regexp) {
return new ContextKeyRegexExpr(key, regexp);
}
}]);
return ContextKeyRegexExpr;
}();
export var ContextKeyNotRegexExpr = /*#__PURE__*/function () {
function ContextKeyNotRegexExpr(_actual) {
_classCallCheck(this, ContextKeyNotRegexExpr);
this.type = ContextKeyExprType.NotRegex;
this._actual = _actual;
}
_createClass(ContextKeyNotRegexExpr, [{
key: "cmp",
value: function cmp(other) {
if (other.type !== this.type) {
return this.type - other.type;
}
return this._actual.cmp(other._actual);
}
}, {
key: "equals",
value: function equals(other) {
if (other.type === this.type) {
return this._actual.equals(other._actual);
}
return false;
}
}, {
key: "evaluate",
value: function evaluate(context) {
return !this._actual.evaluate(context);
}
}, {
key: "serialize",
value: function serialize() {
throw new Error('Method not implemented.');
}
}, {
key: "keys",
value: function keys() {
return this._actual.keys();
}
}, {
key: "map",
value: function map(mapFnc) {
return new ContextKeyNotRegexExpr(this._actual.map(mapFnc));
}
}, {
key: "negate",
value: function negate() {
return this._actual;
}
}], [{
key: "create",
value: function create(actual) {
return new ContextKeyNotRegexExpr(actual);
}
}]);
return ContextKeyNotRegexExpr;
}();
export var ContextKeyAndExpr = /*#__PURE__*/function () {
function ContextKeyAndExpr(expr) {
_classCallCheck(this, ContextKeyAndExpr);
this.type = ContextKeyExprType.And;
this.expr = expr;
}
_createClass(ContextKeyAndExpr, [{
key: "cmp",
value: function cmp(other) {
if (other.type !== this.type) {
return this.type - other.type;
}
if (this.expr.length < other.expr.length) {
return -1;
}
if (this.expr.length > other.expr.length) {
return 1;
}
for (var i = 0, len = this.expr.length; i < len; i++) {
var r = _cmp(this.expr[i], other.expr[i]);
if (r !== 0) {
return r;
}
}
return 0;
}
}, {
key: "equals",
value: function equals(other) {
if (other.type === this.type) {
if (this.expr.length !== other.expr.length) {
return false;
}
for (var i = 0, len = this.expr.length; i < len; i++) {
if (!this.expr[i].equals(other.expr[i])) {
return false;
}
}
return true;
}
return false;
}
}, {
key: "evaluate",
value: function evaluate(context) {
for (var i = 0, len = this.expr.length; i < len; i++) {
if (!this.expr[i].evaluate(context)) {
return false;
}
}
return true;
}
}, {
key: "serialize",
value: function serialize() {
return this.expr.map(function (e) {
return e.serialize();
}).join(' && ');
}
}, {
key: "keys",
value: function keys() {
var result = [];
var _iterator = _createForOfIteratorHelper(this.expr),
_step;
try {
for (_iterator.s(); !(_step = _iterator.n()).done;) {
var expr = _step.value;
result.push.apply(result, _toConsumableArray(expr.keys()));
}
} catch (err) {
_iterator.e(err);
} finally {
_iterator.f();
}
return result;
}
}, {
key: "map",
value: function map(mapFnc) {
return new ContextKeyAndExpr(this.expr.map(function (expr) {
return expr.map(mapFnc);
}));
}
}, {
key: "negate",
value: function negate() {
var result = [];
var _iterator2 = _createForOfIteratorHelper(this.expr),
_step2;
try {
for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) {
var expr = _step2.value;
result.push(expr.negate());
}
} catch (err) {
_iterator2.e(err);
} finally {
_iterator2.f();
}
return ContextKeyOrExpr.create(result);
}
}], [{
key: "create",
value: function create(_expr) {
return ContextKeyAndExpr._normalizeArr(_expr);
}
}, {
key: "_normalizeArr",
value: function _normalizeArr(arr) {
var expr = [];
var hasTrue = false;
var _iterator3 = _createForOfIteratorHelper(arr),
_step3;
try {
for (_iterator3.s(); !(_step3 = _iterator3.n()).done;) {
var e = _step3.value;
if (!e) {
continue;
}
if (e.type === ContextKeyExprType.True) {
// anything && true ==> anything
hasTrue = true;
continue;
}
if (e.type === ContextKeyExprType.False) {
// anything && false ==> false
return ContextKeyFalseExpr.INSTANCE;
}
if (e.type === ContextKeyExprType.And) {
expr.push.apply(expr, _toConsumableArray(e.expr));
continue;
}
expr.push(e);
}
} catch (err) {
_iterator3.e(err);
} finally {
_iterator3.f();
}
if (expr.length === 0 && hasTrue) {
return ContextKeyTrueExpr.INSTANCE;
}
if (expr.length === 0) {
return undefined;
}
if (expr.length === 1) {
return expr[0];
}
expr.sort(_cmp);
// We must distribute any OR expression because we don't support parens
// OR extensions will be at the end (due to sorting rules)
var _loop = function _loop() {
var lastElement = expr[expr.length - 1];
if (lastElement.type !== ContextKeyExprType.Or) {
return 1; // break
}
// pop the last element
expr.pop();
// pop the second to last element
var secondToLastElement = expr.pop();
// distribute `lastElement` over `secondToLastElement`
var resultElement = ContextKeyOrExpr.create(lastElement.expr.map(function (el) {
return ContextKeyAndExpr.create([el, secondToLastElement]);
}));
if (resultElement) {
expr.push(resultElement);
expr.sort(_cmp);
}
};
while (expr.length > 1) {
if (_loop()) break;
}
if (expr.length === 1) {
return expr[0];
}
return new ContextKeyAndExpr(expr);
}
}]);
return ContextKeyAndExpr;
}();
export var ContextKeyOrExpr = /*#__PURE__*/function () {
function ContextKeyOrExpr(expr) {
_classCallCheck(this, ContextKeyOrExpr);
this.type = ContextKeyExprType.Or;
this.expr = expr;
}
_createClass(ContextKeyOrExpr, [{
key: "cmp",
value: function cmp(other) {
if (other.type !== this.type) {
return this.type - other.type;
}
if (this.expr.length < other.expr.length) {
return -1;
}
if (this.expr.length > other.expr.length) {
return 1;
}
for (var i = 0, len = this.expr.length; i < len; i++) {
var r = _cmp(this.expr[i], other.expr[i]);
if (r !== 0) {
return r;
}
}
return 0;
}
}, {
key: "equals",
value: function equals(other) {
if (other.type === this.type) {
if (this.expr.length !== other.expr.length) {
return false;
}
for (var i = 0, len = this.expr.length; i < len; i++) {
if (!this.expr[i].equals(other.expr[i])) {
return false;
}
}
return true;
}
return false;
}
}, {
key: "evaluate",
value: function evaluate(context) {
for (var i = 0, len = this.expr.length; i < len; i++) {
if (this.expr[i].evaluate(context)) {
return true;
}
}
return false;
}
}, {
key: "serialize",
value: function serialize() {
return this.expr.map(function (e) {
return e.serialize();
}).join(' || ');
}
}, {
key: "keys",
value: function keys() {
var result = [];
var _iterator4 = _createForOfIteratorHelper(this.expr),
_step4;
try {
for (_iterator4.s(); !(_step4 = _iterator4.n()).done;) {
var expr = _step4.value;
result.push.apply(result, _toConsumableArray(expr.keys()));
}
} catch (err) {
_iterator4.e(err);
} finally {
_iterator4.f();
}
return result;
}
}, {
key: "map",
value: function map(mapFnc) {
return new ContextKeyOrExpr(this.expr.map(function (expr) {
return expr.map(mapFnc);
}));
}
}, {
key: "negate",
value: function negate() {
var result = [];
var _iterator5 = _createForOfIteratorHelper(this.expr),
_step5;
try {
for (_iterator5.s(); !(_step5 = _iterator5.n()).done;) {
var expr = _step5.value;
result.push(expr.negate());
}
} catch (err) {
_iterator5.e(err);
} finally {
_iterator5.f();
}
var terminals = function terminals(node) {
if (node.type === ContextKeyExprType.Or) {
return node.expr;
}
return [node];
};
// We don't support parens, so here we distribute the AND over the OR terminals
// We always take the first 2 AND pairs and distribute them
while (result.length > 1) {
var LEFT = result.shift();
var RIGHT = result.shift();
var all = [];
var _iterator6 = _createForOfIteratorHelper(terminals(LEFT)),
_step6;
try {
for (_iterator6.s(); !(_step6 = _iterator6.n()).done;) {
var left = _step6.value;
var _iterator7 = _createForOfIteratorHelper(terminals(RIGHT)),
_step7;
try {
for (_iterator7.s(); !(_step7 = _iterator7.n()).done;) {
var right = _step7.value;
all.push(ContextKeyExpr.and(left, right));
}
} catch (err) {
_iterator7.e(err);
} finally {
_iterator7.f();
}
}
} catch (err) {
_iterator6.e(err);
} finally {
_iterator6.f();
}
result.unshift(ContextKeyExpr.or.apply(ContextKeyExpr, all));
}
return result[0];
}
}], [{
key: "create",
value: function create(_expr) {
var expr = ContextKeyOrExpr._normalizeArr(_expr);
if (expr.length === 0) {
return undefined;
}
if (expr.length === 1) {
return expr[0];
}
return new ContextKeyOrExpr(expr);
}
}, {
key: "_normalizeArr",
value: function _normalizeArr(arr) {
var expr = [];
var hasFalse = false;
if (arr) {
for (var i = 0, len = arr.length; i < len; i++) {
var e = arr[i];
if (!e) {
continue;
}
if (e.type === ContextKeyExprType.False) {
// anything || false ==> anything
hasFalse = true;
continue;
}
if (e.type === ContextKeyExprType.True) {
// anything || true ==> true
return [ContextKeyTrueExpr.INSTANCE];
}
if (e.type === ContextKeyExprType.Or) {
expr = expr.concat(e.expr);
continue;
}
expr.push(e);
}
if (expr.length === 0 && hasFalse) {
return [ContextKeyFalseExpr.INSTANCE];
}
expr.sort(_cmp);
}
return expr;
}
}]);
return ContextKeyOrExpr;
}();
export var RawContextKey = /*#__PURE__*/function (_ContextKeyDefinedExp) {
_inherits(RawContextKey, _ContextKeyDefinedExp);
var _super = _createSuper(RawContextKey);
function RawContextKey(key, defaultValue, metaOrHide) {
var _this3;
_classCallCheck(this, RawContextKey);
_this3 = _super.call(this, key);
_this3.key = key;
_this3._defaultValue = defaultValue;
// collect all context keys into a central place
if (_typeof(metaOrHide) === 'object') {
RawContextKey._info.push(_objectSpread(_objectSpread({}, metaOrHide), {}, {
key: key
}));
} else if (metaOrHide !== true) {
RawContextKey._info.push({
key: key,
description: metaOrHide,
type: defaultValue !== null && defaultValue !== undefined ? _typeof(defaultValue) : undefined
});
}
return _this3;
}
_createClass(RawContextKey, [{
key: "bindTo",
value: function bindTo(target) {
return target.createKey(this.key, this._defaultValue);
}
}, {
key: "getValue",
value: function getValue(target) {
return target.getContextKeyValue(this.key);
}
}, {
key: "toNegated",
value: function toNegated() {
return ContextKeyExpr.not(this.key);
}
}, {
key: "isEqualTo",
value: function isEqualTo(value) {
return ContextKeyExpr.equals(this.key, value);
}
}, {
key: "notEqualsTo",
value: function notEqualsTo(value) {
return ContextKeyExpr.notEquals(this.key, value);
}
}], [{
key: "all",
value: function all() {
return RawContextKey._info.values();
}
}]);
return RawContextKey;
}(ContextKeyDefinedExpr);
RawContextKey._info = [];
export var IContextKey;
(function (_IContextKey) {
var None = _IContextKey.None = Object.freeze({
set: function set() {
//
},
reset: function reset() {
//
},
get: function get() {
return undefined;
}
});
})(IContextKey || (IContextKey = {}));
// export const IContextKeyService = createDecorator<IContextKeyService>('contextKeyService');
export var IContextKeyService = Syringe.defineToken('IContextKeyService');
export var SET_CONTEXT_COMMAND_ID = 'setContext';
function cmp1(key1, key2) {
if (key1 < key2) {
return -1;
}
if (key1 > key2) {
return 1;
}
return 0;
}
function cmp2(key1, value1, key2, value2) {
if (key1 < key2) {
return -1;
}
if (key1 > key2) {
return 1;
}
if (value1 < value2) {
return -1;