Fixjs——显示容器基类DisplayObjectContainer

<sdt id="89512093" sdtlocked="t" contentlocked="t" sdtgroup="t"></sdt>

Fixjs介绍

Fixjs是我打算在javascript技术领域开始积累的一个框架项目,这套框架主要为开发复杂组件提供底层的框架支持。

框架的类与接口我会尽量参考flash框架的实现。同时,我也会开放Fixjs的源代码,欢迎同仁一起学习、交流。

DisplayObjectContainer

DisplayObjectContainer是显示容器基类,它提供了显示子项的添加、移除、层次控制等接口。以下是代码实现:

fixjs.display.DisplayObjectContainer = fixjs.display.InteractiveObject.extend({

init: function (ele) {

fixjs.display.DisplayObjectContainer.base.init.call(this, ele);

this.mouseChildren = true;

this.numChildren = 0;

this.tabChildren = true;

this._children = [];

},

disposing:function () {

this.numChildren = 0;

fixjs.DisposeUtil.dispose(this._children);

this._children = null;

fixjs.display.DisplayObjectContainer.base.disposing.call(this);

},

setMouseChildren: function(value) {

var array = this._children;

for (var i = 0; i < array.length; i++) {

var child = array[i];

child.mouseEnabled = value;

if (child instanceof fixjs.display.DisplayObjectContainer) {

child.setMouseChildren(value);

}

}

},

setTabChildren: function(value) {

var array = this._children;

for (var i = 0; i < array.length; i++) {

var child = array[i];

child.tabEnabled = value;

if (child instanceof fixjs.display.DisplayObjectContainer) {

child.setTabChildren(value);

}

}

},

addChild: function(child) {

if (!(child instanceof fixjs.display.DisplayObject))

throw new Error("[ArgumentError]child必须是fixjs.display.DisplayObject类型!");

if (child == this)

throw new Error("[ArgumentError]child不能添加到自身!");

if (child.parent!= null)

throw new Error("[ArgumentError]child.parent不为空!");

if (this.contains(child))

throw new Error("[ArgumentError]已经包含child");

this.ele.appendChild(child.ele);

child.parent = this;

this.numChildren++;

this._children.push(child);

var e = new fixjs.events.Event(fixjs.events.Event.ADDED);

this.dispatchEvent(e);

},

addChildAt: function(child, index) {

if (!(child instanceof fixjs.display.DisplayObject))

throw new Error("[ArgumentError]child必须是fixjs.display.DisplayObject类型!");

if (child == this)

throw new Error("[ArgumentError]child不能添加到自身!");

if (child.parent!= null)

throw new Error("[ArgumentError]child.parent不为空!");

if (this.contains(child))

throw new Error("[ArgumentError]已经包含child");

if (index < 0 || index>= this.numChildren)

throw new Error("[RangeError]索引超出范围!");

var prev = this._children[index];

this.ele.insertBefore(child.ele, prev.ele);

child.parent = this;

this.numChildren++;

this._children.splice(index, 0, child);

var e = new fixjs.events.Event(fixjs.events.Event.ADDED);

this.dispatchEvent(e);

},

contains:function (child) {

var array = this._children;

return array.indexOf(child)>= 0;

},

getChildAt: function(index) {

if (index < 0 || index>= this.numChildren)

throw new Error("[RangeError]索引超出范围!");

return this._children[index];

},

getChildByName: function(name) {

var array = this._children;

for (var i = 0; i < array.length; i++) {

var child = array[i];

if (child.name== name)

return child;

}

},

getChildIndex: function(child) {

var i = this._children.indexOf(child);

if (i< 0)

throw newError("[ArgumentError]child不是该对象的子项!");

return i;

},

removeChild: function(child) {

var i = this._children.indexOf(child);

if (i< 0)

throw newError("[ArgumentError]child不是该对象的子项!");

return this.removeChildAt(i);

},

removeChildAt: function(index) {

if (index < 0 || index>= this.numChildren)

throw new Error("[RangeError]索引超出范围!");

var child = this._children[index];

this.ele.removeChild(child.ele);

this.numChildren--;

this._children.splice(index, 1);

var e = new fixjs.events.Event(fixjs.events.Event.REMOVED);

this.dispatchEvent(e);

return child;

},

removeAllChildren: function() {

var e;

if (this.numChildren)

e = new fixjs.events.Event(fixjs.events.Event.REMOVED);

while (this._children.length) {

var child = this._children.pop();

this.ele.removeChild(child.ele);

child.dispose();

}

this.numChildren = 0;

if (e)

this.dispatchEvent(e);

},

setChildIndex: function(child, index) {

if (index < 0 || index>= this.numChildren)

throw new Error("[RangeError]索引超出范围!");

var i = this._children.indexOf(child);

if (i< 0)

throw new Error("[ArgumentError]child不是该对象的子项!");

if (i== index)

return;

this._children.splice(i, 1);

this.ele.removeChild(child.ele);

if (index < this._children.length) {

var prev = this._children[index];

this.ele.insertBefore(child.ele, prev.ele);

}

else {

this.ele.appendChild(child.ele);

}

this._children.splice(index, 0, child);

},

swapChildren: function(child1, child2) {

var i1 = this._children.indexOf(child1);

if (i1 < 0)

throw new Error("[ArgumentError]child1不是该对象的子项!");

var i2 = this._children.indexOf(child2);

if (i2 < 0)

throw new Error("[ArgumentError]child2不是该对象的子项!");

var t1 = child1.ele.nextSibling;

var t2 = child2.ele.nextSibling;

if (t1)

this.ele.insertBefore(child2.ele, t1);

else

this.ele.appendChild(child2.ele);

if (t2)

this.ele.insertBefore(child1.ele, t2);

else

this.ele.appendChild(child1.ele);

},

swapChildrenAt: function(index1, index2) {

if (index1 < 0 || index1>= this.numChildren)

throw new Error("[RangeError]索引index1超出范围!");

if (index2 < 0 || index2>= this.numChildren)

throw new Error("[RangeError]索引index2超出范围!");

var child1 = this._children[index1];

var child2 = this._children[index2];

this.swapChildren(child1, child2);

}

});

相关文章

Fixjs专栏

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值