awv3
Version:
⚡ AWV3 embedded CAD
279 lines (239 loc) • 9.21 kB
JavaScript
import _extends from "@babel/runtime/helpers/extends";
import _objectWithoutProperties from "@babel/runtime/helpers/objectWithoutProperties";
import _taggedTemplateLiteralLoose from "@babel/runtime/helpers/taggedTemplateLiteralLoose";
import _inheritsLoose from "@babel/runtime/helpers/inheritsLoose";
import _assertThisInitialized from "@babel/runtime/helpers/assertThisInitialized";
var _class, _temp;
var _templateObject = /*#__PURE__*/ _taggedTemplateLiteralLoose(["plugin must be initialized with a session"], ["plugin must be initialized with a session"]);
import * as THREE from 'three';
import React from 'react';
import Lifecycle from './lifecycle.js';
import { errUndefined } from '../core/helpers';
import { actions } from './store/plugins';
import { actions as elementActions, base as elementsBase } from './store/elements';
import { actions as globalActions } from './store/globals';
import { actions as connectionActions, base as connectionBase } from './store/connections';
import { base as pluginBase } from './store/plugins';
import { createObserver } from './helpers';
import Element from './element';
import { Group, Button, Spacer, Divider } from './elements';
import ElementRenderer, { Provider } from './reconciler';
var Plugin = (_temp = _class =
/*#__PURE__*/
function (_Lifecycle) {
_inheritsLoose(Plugin, _Lifecycle);
//Default context menu items:
function Plugin(session, _ref) {
var _this;
if (session === void 0) {
session = errUndefined(_templateObject);
}
var feature = _ref.feature,
connection = _ref.connection,
pool = _ref.pool,
render = _ref.render,
props = _objectWithoutProperties(_ref, ["feature", "connection", "pool", "render"]);
_this = _Lifecycle.call(this, session, actions, function (state) {
return state.plugins[_this.id];
}, _extends({
type: '',
name: '',
title: '',
resources: {},
icon: undefined,
enabled: false,
active: false,
collapsed: true,
closeable: true,
portal: false,
parent: undefined,
elements: [],
feature: feature ? feature.id : false,
connection: connection ? connection.id : undefined,
managed: true
}, props)) || this; // JSX render shortcut
if (render) _this.render = function () {
return render;
}; // If a parent exists (which mean this is a linked plugin, invoked by a parent plugin)
if (_this.parent) {
var parentPlugin = pluginBase.references[_this.parent]; // And if the parent is a feature, it gets to store its own pool under the parent, while
// it is allowed to pose as a feature plugin itself
if (parentPlugin.feature) {
feature = parentPlugin;
connection = parentPlugin.connection;
_this.pool = new THREE.Group();
_this.pool.updateParentMaterials = false;
_this.pool.measurable = false;
_this.pool.name = "plugin.linked_pool." + _this.type + (_this.name ? '+' + _this.name : '');
feature.pool.add(_this.pool);
}
}
if (feature) {
Object.defineProperty(_assertThisInitialized(_this), 'feature', {
get: function get() {
return feature.id;
}
});
Object.defineProperty(_assertThisInitialized(_this), 'connection', {
get: function get() {
return connection;
}
});
if (!_this.parent) _this.pool = pool;
} else {
Object.defineProperty(_assertThisInitialized(_this), 'connection', {
get: function get() {
return connectionBase.references[_this.session.globals.activeConnection];
}
});
_this.pool = new THREE.Group();
_this.pool.updateParentMaterials = false;
_this.pool.measurable = false;
_this.pool.name = "plugin.pool." + _this.type + (_this.name ? '+' + _this.name : '');
_this.session.pool.temporary.add(_this.pool);
} // this.tree refers to the connections tree
Object.defineProperty(_assertThisInitialized(_this), 'tree', {
get: function get() {
return _this.connection.tree;
}
});
Object.defineProperty(_assertThisInitialized(_this), 'root', {
get: function get() {
return _this.tree[_this.connection.activeRoot || _this.tree.root];
}
});
Object.defineProperty(_assertThisInitialized(_this), 'view', {
get: function get() {
return _this.pool.view;
}
}); // Internal observer for enabled state, gets cleared after destroy
// The enabled flag has to be kept locally as the destroyed event can be dispatched
// without calling destroy() directly, leaving the linked state empty
_this.__enabled = _this.enabled;
_this.__enabledUnsubscribe = _this.observe( // selector
function (state) {
return state.enabled;
}, // onChanged
function (enabled, prev) {
_this.__enabled = enabled;
if (enabled !== prev) {
if (enabled) {
_this.onEnabled();
if (_this.render) ElementRenderer.render(React.createElement(Provider, {
plugin: _assertThisInitialized(_this)
}, _this.render()), _assertThisInitialized(_this));
} else {
_this.onDisabled();
if (_this.render) ElementRenderer.unmountComponentAtNode(_assertThisInitialized(_this));
}
}
!enabled && _this.removeSubscriptions();
}, // options
{
manager: undefined
});
return _this;
}
var _proto = Plugin.prototype;
_proto.findElement = function findElement(condition, elements) {
if (elements === void 0) {
elements = this.elements;
}
for (var _iterator = elements, _isArray = Array.isArray(_iterator), _i = 0, _iterator = _isArray ? _iterator : _iterator[Symbol.iterator]();;) {
var _ref2;
if (_isArray) {
if (_i >= _iterator.length) break;
_ref2 = _iterator[_i++];
} else {
_i = _iterator.next();
if (_i.done) break;
_ref2 = _i.value;
}
var _key = _ref2;
var el = this.session.elements[_key];
var test = el.plugin === this.id && condition(el);
if (test) {
return el.id;
} else if (el.children.length) {
return this.findElement(condition, el.children);
}
}
};
_proto.findElementClass = function findElementClass(condition) {
return elementsBase.references[this.findElement(condition)];
};
_proto.addElement = function addElement() {
var _this2 = this;
for (var _len = arguments.length, elements = new Array(_len), _key2 = 0; _key2 < _len; _key2++) {
elements[_key2] = arguments[_key2];
}
elements.forEach(function (element) {
return _this2.store.dispatch(actions.addElement(_this2.id, element instanceof Element ? element.id : element));
});
};
_proto.removeElement = function removeElement(element) {
this.store.dispatch(actions.removeElement(this.id, element instanceof Element ? element.id : element));
};
_proto.destroyElements = function destroyElements() {
var _this3 = this;
this.store.dispatch(elementActions.unregister(Object.keys(this.session.elements).filter(function (id) {
return _this3.session.elements[id].plugin === _this3.id;
})));
};
_proto.resetElements = function resetElements() {
this.store.dispatch(elementActions.merge(this.dependencies.reduce(function (prev, obj) {
var _extends2;
return _extends({}, prev, (_extends2 = {}, _extends2[obj.id] = obj.props, _extends2));
}, {})));
};
_proto.onEnabled = function onEnabled() {};
_proto.onDisabled = function onDisabled() {};
_proto.__onDestroyed = function __onDestroyed() {
this.store.dispatch(globalActions.unlinkPlugins(this.id));
if (this.connection) this.store.dispatch(connectionActions.unlinkPlugins(this.connection.id, this.id));
this.__enabledUnsubscribe();
this.__enabled && this.onDisabled();
if (this.render) ElementRenderer.unmountComponentAtNode(this);
this.pool && this.pool.destroy();
};
return Plugin;
}(Lifecycle()), Object.defineProperty(_class, "persistent", {
configurable: true,
enumerable: true,
writable: true,
value: false
}), Object.defineProperty(_class, "measurable", {
configurable: true,
enumerable: true,
writable: true,
value: false
}), Object.defineProperty(_class, "connection", {
configurable: true,
enumerable: true,
writable: true,
value: void 0
}), Object.defineProperty(_class, "store", {
configurable: true,
enumerable: true,
writable: true,
value: void 0
}), Object.defineProperty(_class, "contextMenuItems", {
configurable: true,
enumerable: true,
writable: true,
value: {
delete: {
name: "Delete",
callback: function callback(feature, connectionId, session) {
g_activeConnection.execute("_C.CADApplication.DeleteFeature(" + feature.id + ",1);");
}
},
edit: {
name: "Edit",
callback: function callback(feature, activeConnectionId, session) {
session.store.dispatch(connectionActions.setActiveFeature(activeConnectionId, feature.id));
}
}
}
}), _temp);
export { Plugin as default };