@robotlegsjs/createjs
Version:
CreateJS View Integration with RobotlegsJS
106 lines • 4.31 kB
JavaScript
;
// ------------------------------------------------------------------------------
// 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.applyCreateJSPatch = void 0;
/**
* Patch createjs to:
* - emit "added"/"removed" events on stage
*/
function isConnectedToStage(stage, displayObject) {
return displayObject.stage === stage;
}
function emitAddedEvent(stage, target) {
var event = new createjs.Event("added", true, false);
event.data = target;
stage.dispatchEvent(event);
if (target instanceof createjs.Container) {
target.children.forEach(function (child) { return emitAddedEvent(stage, child); });
}
}
function emitRemovedEvent(stage, target) {
var event = new createjs.Event("removed", true, false);
event.data = target;
stage.dispatchEvent(event);
if (target instanceof createjs.Container) {
target.children.forEach(function (child) { return emitRemovedEvent(stage, child); });
}
}
function applyCreateJSPatch(stage) {
var addChild = createjs.Container.prototype.addChild;
var addChildAt = createjs.Container.prototype.addChildAt;
var removeChild = createjs.Container.prototype.removeChild;
var removeChildAt = createjs.Container.prototype.removeChildAt;
var removeAllChildren = createjs.Container.prototype.removeAllChildren;
createjs.Container.prototype.addChild = function patchedAddChild(child) {
var additionalChildren = [];
for (var _i = 1; _i < arguments.length; _i++) {
additionalChildren[_i - 1] = arguments[_i];
}
for (var i = 0, len = arguments.length; i < len; i++) {
var object = arguments[i];
addChild.call(this, object);
if (isConnectedToStage(stage, object)) {
emitAddedEvent(stage, object);
}
}
return arguments[arguments.length - 1];
};
createjs.Container.prototype.addChildAt = function patchedAddChildAt() {
var childOrIndex = [];
for (var _i = 0; _i < arguments.length; _i++) {
childOrIndex[_i] = arguments[_i];
}
for (var i = 0, len = arguments.length - 1; i < len; i++) {
var object = arguments[i];
addChildAt.call(this, object, arguments[len] + i);
if (isConnectedToStage(stage, object)) {
emitAddedEvent(stage, object);
}
}
return arguments[arguments.length - 2];
};
createjs.Container.prototype.removeChild = function patchedRemoveChild() {
var child = [];
for (var _i = 0; _i < arguments.length; _i++) {
child[_i] = arguments[_i];
}
var removed = true;
for (var i = 0, len = child.length; i < len; i++) {
if (isConnectedToStage(stage, child[i])) {
emitRemovedEvent(stage, child[i]);
}
removed = removed && removeChild.call(this, child[i]);
}
return removed;
};
createjs.Container.prototype.removeChildAt = function patchedRemoveChildAt() {
var index = [];
for (var _i = 0; _i < arguments.length; _i++) {
index[_i] = arguments[_i];
}
var removed = true;
for (var i = 0, len = arguments.length; i < len; i++) {
var child = this.getChildAt(arguments[i]);
if (isConnectedToStage(stage, child)) {
emitRemovedEvent(stage, child);
}
removed = removed && removeChildAt.call(this, arguments[i]);
}
return removed;
};
createjs.Container.prototype.removeAllChildren = function patchedRemoveAllChildren() {
if (isConnectedToStage(stage, this) && this.children.length) {
this.children.forEach(function (child) {
emitRemovedEvent(stage, child);
});
}
removeAllChildren.call(this);
};
}
exports.applyCreateJSPatch = applyCreateJSPatch;
//# sourceMappingURL=createjs-patch.js.map