@robotlegsjs/createjs
Version:
CreateJS View Integration with RobotlegsJS
174 lines • 8.03 kB
JavaScript
"use strict";
// ------------------------------------------------------------------------------
// Copyright (c) 2017-present, RobotlegsJS. All Rights Reserved.
//
// NOTICE: You are permitted to use, modify, and distribute this file
// in accordance with the terms of the license agreement accompanying it.
// ------------------------------------------------------------------------------
Object.defineProperty(exports, "__esModule", { value: true });
exports.ContainerRegistry = void 0;
var tslib_1 = require("tslib");
var core_1 = require("@robotlegsjs/core");
var ContainerBinding_1 = require("./ContainerBinding");
var ContainerBindingEvent_1 = require("./ContainerBindingEvent");
var ContainerRegistryEvent_1 = require("./ContainerRegistryEvent");
// [Event(name="containerAdd", type="robotlegs.bender.extensions.viewManager.impl.ContainerRegistryEvent")]
// [Event(name="containerRemove", type="robotlegs.bender.extensions.viewManager.impl.ContainerRegistryEvent")]
// [Event(name="rootContainerAdd", type="robotlegs.bender.extensions.viewManager.impl.ContainerRegistryEvent")]
// [Event(name="rootContainerRemove", type="robotlegs.bender.extensions.viewManager.impl.ContainerRegistryEvent")]
/**
* @private
*/
var ContainerRegistry = /** @class */ (function (_super) {
tslib_1.__extends(ContainerRegistry, _super);
function ContainerRegistry() {
/*============================================================================*/
/* Public Properties */
/*============================================================================*/
var _this = _super !== null && _super.apply(this, arguments) || this;
_this._bindings = [];
_this._rootBindings = [];
/*============================================================================*/
/* Private Properties */
/*============================================================================*/
_this._bindingByContainer = new Map();
return _this;
}
Object.defineProperty(ContainerRegistry.prototype, "bindings", {
/**
* @private
*/
get: function () {
return this._bindings;
},
enumerable: false,
configurable: true
});
Object.defineProperty(ContainerRegistry.prototype, "rootBindings", {
/**
* @private
*/
get: function () {
return this._rootBindings;
},
enumerable: false,
configurable: true
});
/*============================================================================*/
/* Public Functions */
/*============================================================================*/
/**
* @private
*/
ContainerRegistry.prototype.addContainer = function (container) {
var binding = this._bindingByContainer.get(container);
if (!binding) {
binding = this._createBinding(container);
this._bindingByContainer.set(container, binding);
}
return binding;
};
/**
* @private
*/
ContainerRegistry.prototype.removeContainer = function (container) {
var binding = this._bindingByContainer.get(container);
if (binding) {
this._removeBinding(binding);
}
return binding;
};
/**
* Finds the closest parent binding for a given display object
*
* @private
*/
ContainerRegistry.prototype.findParentBinding = function (target) {
var parent = target.parent;
while (parent) {
var binding = this._bindingByContainer.get(parent);
if (binding) {
return binding;
}
parent = parent.parent;
}
return null;
};
/**
* @private
*/
ContainerRegistry.prototype.getBinding = function (container) {
return this._bindingByContainer.get(container);
};
/*============================================================================*/
/* Private Functions */
/*============================================================================*/
ContainerRegistry.prototype._createBinding = function (container) {
var _this = this;
var binding = new ContainerBinding_1.ContainerBinding(container);
this._bindings.push(binding);
// Add a listener so that we can remove this binding when it has no handlers
binding.addEventListener(ContainerBindingEvent_1.ContainerBindingEvent.BINDING_EMPTY, this._onBindingEmpty.bind(this));
// If the new binding doesn't have a parent it is a Root
binding.parent = this.findParentBinding(container);
if (binding.parent == null) {
this._addRootBinding(binding);
}
// Reparent any bindings which are contained within the new binding AND
// A. Don't have a parent, OR
// B. Have a parent that is not contained within the new binding
this._bindingByContainer.forEach(function (childBinding) {
if (container.contains(childBinding.container)) {
if (!childBinding.parent) {
_this._removeRootBinding(childBinding);
childBinding.parent = binding;
}
else if (!container.contains(childBinding.parent.container)) {
childBinding.parent = binding;
}
}
});
this.dispatchEvent(new ContainerRegistryEvent_1.ContainerRegistryEvent(ContainerRegistryEvent_1.ContainerRegistryEvent.CONTAINER_ADD, binding.container));
return binding;
};
ContainerRegistry.prototype._removeBinding = function (binding) {
var _this = this;
// Remove the binding itself
this._bindingByContainer.delete(binding.container);
var index = this._bindings.indexOf(binding);
this._bindings.splice(index, 1);
// Drop the empty binding listener
binding.removeEventListener(ContainerBindingEvent_1.ContainerBindingEvent.BINDING_EMPTY, this._onBindingEmpty);
if (!binding.parent) {
// This binding didn't have a parent, so it was a Root
this._removeRootBinding(binding);
}
// Re-parent the bindings
this._bindingByContainer.forEach(function (childBinding) {
if (childBinding.parent === binding) {
childBinding.parent = binding.parent;
if (!childBinding.parent) {
// This binding used to have a parent,
// but no longer does, so it is now a Root
_this._addRootBinding(childBinding);
}
}
});
this.dispatchEvent(new ContainerRegistryEvent_1.ContainerRegistryEvent(ContainerRegistryEvent_1.ContainerRegistryEvent.CONTAINER_REMOVE, binding.container));
};
ContainerRegistry.prototype._addRootBinding = function (binding) {
this._rootBindings.push(binding);
this.dispatchEvent(new ContainerRegistryEvent_1.ContainerRegistryEvent(ContainerRegistryEvent_1.ContainerRegistryEvent.ROOT_CONTAINER_ADD, binding.container));
};
ContainerRegistry.prototype._removeRootBinding = function (binding) {
var index = this._rootBindings.indexOf(binding);
this._rootBindings.splice(index, 1);
this.dispatchEvent(new ContainerRegistryEvent_1.ContainerRegistryEvent(ContainerRegistryEvent_1.ContainerRegistryEvent.ROOT_CONTAINER_REMOVE, binding.container));
};
ContainerRegistry.prototype._onBindingEmpty = function (event) {
this._removeBinding(event.target);
};
return ContainerRegistry;
}(core_1.EventDispatcher));
exports.ContainerRegistry = ContainerRegistry;
//# sourceMappingURL=ContainerRegistry.js.map