react-qml
Version:
334 lines (257 loc) • 11.4 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.isAnchorLineProp = isAnchorLineProp;
exports.isAnchorItemProp = isAnchorItemProp;
exports.isAnchorProp = isAnchorProp;
exports.default = exports.ParentAnchor = exports.Anchor = exports.AnchorLineRef = void 0;
function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
function _get(target, property, receiver) { if (typeof Reflect !== "undefined" && Reflect.get) { _get = Reflect.get; } else { _get = function _get(target, property, receiver) { var base = _superPropBase(target, property); if (!base) return; var desc = Object.getOwnPropertyDescriptor(base, property); if (desc.get) { return desc.get.call(receiver); } return desc.value; }; } return _get(target, property, receiver || target); }
function _superPropBase(object, property) { while (!Object.prototype.hasOwnProperty.call(object, property)) { object = _getPrototypeOf(object); if (object === null) break; } return object; }
function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); }
function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
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 } }); if (superClass) _setPrototypeOf(subClass, superClass); }
function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
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, descriptor.key, descriptor); } }
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
var ANCHOR_LINES = ['left', 'top', 'right', 'bottom', 'horizontalCenter', 'verticalCenter', 'baseline'];
var ANCHOR_ITEM_REF = ['fill', 'centerIn'];
function isAnchorLineProp(key) {
return ANCHOR_LINES.indexOf(key) > -1;
}
function isAnchorItemProp(key) {
return ANCHOR_ITEM_REF.indexOf(key) > -1;
}
function isAnchorProp(key) {
return isAnchorLineProp(key) || isAnchorItemProp(key);
}
var AbstractAnchorRef =
/*#__PURE__*/
function () {
function AbstractAnchorRef() {
var qmlElement = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : null;
_classCallCheck(this, AbstractAnchorRef);
_defineProperty(this, "qmlElement", void 0);
_defineProperty(this, "subscriptions", []);
this.qmlElement = qmlElement;
}
_createClass(AbstractAnchorRef, [{
key: "processSubscriptions",
value: function processSubscriptions() {
var _this = this;
if (!this.isReady()) {
throw new Error('Anchor not ready');
}
this.subscriptions.forEach(function (_ref) {
var source = _ref.source,
sourceProp = _ref.sourceProp;
source.anchors[sourceProp] = _this.value();
});
}
}, {
key: "setQmlElement",
value: function setQmlElement(qmlElement) {
if (this.qmlElement != null) {
throw new Error('Anchor ref cannot be used twice');
}
this.qmlElement = qmlElement;
this.processSubscriptions();
}
}, {
key: "isReady",
value: function isReady() {
return this.qmlElement != null;
}
}, {
key: "addSubscription",
value: function addSubscription(source, sourceProp) {
this.subscriptions.push({
source: source,
sourceProp: sourceProp
});
if (this.qmlElement) {
source.anchors[sourceProp] = this.value();
}
}
}, {
key: "removeSubscription",
value: function removeSubscription(source, sourceProp) {
for (var index = 0; index < this.subscriptions.length; index++) {
var subscription = this.subscriptions[index];
if (subscription.source === source && subscription.sourceProp === sourceProp) {
// found
this.subscriptions.splice(index, 1);
// early return
return;
}
}
}
}]);
return AbstractAnchorRef;
}();
var AnchorLineRef =
/*#__PURE__*/
function (_AbstractAnchorRef) {
_inherits(AnchorLineRef, _AbstractAnchorRef);
function AnchorLineRef(line) {
var _this2;
var qmlElement = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : null;
_classCallCheck(this, AnchorLineRef);
_this2 = _possibleConstructorReturn(this, _getPrototypeOf(AnchorLineRef).call(this, qmlElement));
_defineProperty(_assertThisInitialized(_this2), "line", void 0);
_this2.line = line;
return _this2;
}
_createClass(AnchorLineRef, [{
key: "value",
value: function value() {
if (this.qmlElement) {
return this.qmlElement[this.line];
}
return undefined;
}
}, {
key: "type",
value: function type() {
return 'anchor_line';
}
}]);
return AnchorLineRef;
}(AbstractAnchorRef);
exports.AnchorLineRef = AnchorLineRef;
var Anchor =
/*#__PURE__*/
function (_AbstractAnchorRef2) {
_inherits(Anchor, _AbstractAnchorRef2);
function Anchor() {
var _this3;
_classCallCheck(this, Anchor);
_this3 = _possibleConstructorReturn(this, _getPrototypeOf(Anchor).call(this));
_defineProperty(_assertThisInitialized(_this3), "id", void 0);
_defineProperty(_assertThisInitialized(_this3), "left", void 0);
_defineProperty(_assertThisInitialized(_this3), "horizontalCenter", void 0);
_defineProperty(_assertThisInitialized(_this3), "right", void 0);
_defineProperty(_assertThisInitialized(_this3), "top", void 0);
_defineProperty(_assertThisInitialized(_this3), "verticalCenter", void 0);
_defineProperty(_assertThisInitialized(_this3), "baseline", void 0);
_defineProperty(_assertThisInitialized(_this3), "bottom", void 0);
_this3.id = Symbol(); // anchor lines
_this3.left = new AnchorLineRef('left');
_this3.horizontalCenter = new AnchorLineRef('horizontalCenter');
_this3.right = new AnchorLineRef('right');
_this3.top = new AnchorLineRef('top');
_this3.verticalCenter = new AnchorLineRef('verticalCenter');
_this3.baseline = new AnchorLineRef('baseline');
_this3.bottom = new AnchorLineRef('bottom');
return _this3;
}
_createClass(Anchor, [{
key: "setQmlElement",
value: function setQmlElement(qmlElement) {
_get(_getPrototypeOf(Anchor.prototype), "setQmlElement", this).call(this, qmlElement);
this.left.setQmlElement(qmlElement);
this.horizontalCenter.setQmlElement(qmlElement);
this.right.setQmlElement(qmlElement);
this.top.setQmlElement(qmlElement);
this.verticalCenter.setQmlElement(qmlElement);
this.baseline.setQmlElement(qmlElement);
this.bottom.setQmlElement(qmlElement);
}
}, {
key: "value",
value: function value() {
if (this.qmlElement) {
return this.qmlElement;
}
return undefined;
}
}, {
key: "type",
value: function type() {
return 'anchor';
}
}], [{
key: "createRef",
value: function createRef() {
return new Anchor();
}
}]);
return Anchor;
}(AbstractAnchorRef);
exports.Anchor = Anchor;
var ParentAnchor =
/*#__PURE__*/
function () {
function ParentAnchor() {
_classCallCheck(this, ParentAnchor);
}
_createClass(ParentAnchor, null, [{
key: "addSubscription",
value: function addSubscription(childElement, childPropName, parentAnchorRef) {
var parentPropName = undefined; // parse parentAnchorRef first
if (parentAnchorRef && parentAnchorRef.length > 0 && parentAnchorRef !== 'parent') {
var parts = parentAnchorRef.split('.');
if (parts.length === 2 && parts[0] === 'parent') {
if (!isAnchorProp(parts[1])) {
console.warn("Invalid anchor reference: ".concat(parts[1]));
return;
}
parentPropName = parts[1];
}
}
var onTargetChanged = function onTargetChanged() {
ParentAnchor.updateAnchors(childElement, childPropName, parentPropName);
};
ParentAnchor.parentAnchorSubscriptions.push({
source: childElement,
sourceProp: childPropName,
targetProp: parentPropName,
onTargetChanged: onTargetChanged
});
if (childElement.parent) {
ParentAnchor.updateAnchors(childElement, childPropName, parentPropName);
}
childElement.parentChanged.connect(onTargetChanged);
}
}, {
key: "removeSubscription",
value: function removeSubscription(childElement, childPropName) {
for (var index = 0; index < ParentAnchor.parentAnchorSubscriptions.length; index++) {
var subscription = ParentAnchor.parentAnchorSubscriptions[index];
if (subscription.source === childElement && subscription.sourceProp === childPropName) {
// found
if (subscription.onTargetChanged && childElement.parentChanged) {
childElement.parentChanged.disconnect(subscription.onTargetChanged);
}
ParentAnchor.parentAnchorSubscriptions.splice(index, 1);
// early return
return;
}
}
}
}]);
return ParentAnchor;
}();
exports.ParentAnchor = ParentAnchor;
_defineProperty(ParentAnchor, "parentAnchorSubscriptions", []);
_defineProperty(ParentAnchor, "updateAnchors", function (childElement, childPropName, parentPropName) {
if (!childElement.parent) {
// console.log('ChildElement has no parent', childElement);
return;
}
if (childPropName === 'fill' || childPropName === 'centerIn') {
childElement.anchors[childPropName] = childElement.parent;
} else {
if (!parentPropName) {
console.warn('Undefined anchor reference');
return;
}
childElement.anchors[childPropName] = childElement.parent[parentPropName];
}
});
var _default = Anchor;
exports.default = _default;