Actualización

This commit is contained in:
Xes
2025-04-10 12:36:07 +02:00
parent 1da7c3f3b9
commit 4aff98e77b
3147 changed files with 320647 additions and 0 deletions

View File

@@ -0,0 +1,31 @@
kampfer.provide('Class');
kampfer.Class = function() {};
kampfer.Class.initializing = false;
kampfer.Class.extend = function(props) {
var Class = function() {
if(!kampfer.Class.initializing && this.initializer) {
this.initializer.apply(this, arguments);
}
};
kampfer.Class.initializing = true;
// this === 构造函数。
//能否直接使用this.prototype考虑使用this.prototype。
var prototype = new this();
kampfer.Class.initializing = false;
prototype = kampfer.extend(prototype, props);
Class.prototype = prototype;
Class.prototype.constructor = Class;
Class.superClass = this.prototype;
Class.extend = kampfer.Class.extend;
return Class;
};

View File

@@ -0,0 +1,198 @@
kampfer.require('events.EventTarget');
kampfer.provide('class.Composition');
kampfer.class.Composition = kampfer.events.EventTarget.extend({
_id : null,
_parent : null,
//array
//_children必须与_childrenIndex同步
//懒加载
_children : null,
//object
//_childrenIndex必须与_children同步
//懒加载
_childrenIndex : null,
//递归调用子节点的指定方法
//实现composition模式的关键方法之一
walk : function(method) {
var args = Array.prototype.slice.call(arguments, 1);
this.forEachChild(function(child, i) {
if( kampfer.type(child[method]) === 'function' ) {
child[method].apply(child, args);
}
});
},
getId : function() {
return this._id ||
( this._id = kampfer.Composition.generateUniqueId() );
},
setId : function(id) {
if(this._parent && this._parent._childrenIndex) {
delete this._parent._childrenIndex[this._id];
this._parent._childrenIndex[id] = this;
}
this._id = id;
},
getParent : function() {
return this._parent;
},
setParent : function(parent) {
//新的parent不为空时必须是component实例
if( parent && !(parent instanceof kampfer.UIComponent) ) {
return;
}
//新的parent不能是对象自己
if(parent === this) {
return;
}
//对象已经是另一个对象的child那么必须先调用removeChild之后再调用setParent
//对象不可能同时是另外两个对象的child
if( parent && this._parent && this._id &&
this._parent.getChild(this._id) && parent !== this._parent ) {
return;
}
this._parent = parent;
this.setParentEventTarget(parent);
//对象不是新parent的child那么将child添加到parnet的子列表中
//因为addchild方法会检查_parent属性所以必须在设置完_parent属性后才能执行添加操作
//closure没有这一步, 它的setParent方法只保证child的parent属性正确,
//但不保证child一定在parnet的子节点列表中
if( parent && !parent.getChild(this._id) ) {
parent.addChild(this);
}
},
addChild : function(child, render) {
this.addChildAt(child, this.getChildCount(), render);
},
addChildAt : function(child, index, render) {
if( !(child instanceof kampfer.UIComponent) ) {
return;
}
if(index < 0 || index > this.getChildCount() ) {
return;
}
if(!this._children || !this._childrenIndex) {
this._children = [];
this._childrenIndex = {};
}
if( child.getParent() === this ) {
//删除_children中保存的child引用
//避免_children中保存多个child引用
for(var i = 0, c; (c = this._children[i]); i++) {
if(c === child) {
this._children.splice(i, 1);
}
}
}
this._childrenIndex[child.getId()] = child;
this._children.splice(index, 0, child);
//closure没有这一步, 它的addChildAt方法只保证child在parent的子节点列表中,
//不保证child的parent一定是this. 这里我尝试增加这种确定性.
if(child._parent !== this) {
child.setParent(this);
}
},
getChild : function(id) {
if(id && this._childrenIndex) {
return this._childrenIndex[id];
}
},
getChildAt : function(index) {
if(this._children) {
return this._children[index];
}
},
removeChild : function(child) {
if(child) {
var id;
if( kampfer.type(child) === 'string' ) {
id = child;
child = this.getChild(id);
} else {
id = child.getId();
}
for(var i = 0, c; (c = this._children[i]); i++) {
if(c === child) {
this._children.splice(i, 1);
}
}
delete this._childrenIndex[id];
child.setParent(null);
}
return child;
},
removeChildAt : function(index) {
this.remochild( this.getChildAt(index) );
},
forEachChild : function(callback, context) {
if(!this._children) {
return;
}
for(var i = 0, child; (child = this._children[i]); i++) {
if( calllback.call(context || child, child, i) === false ) {
return;
}
}
},
indexOfChild : function(child) {
this.forEachChild(function(c, i) {
if(c === child) {
return i;
}
});
},
getChildCount : function() {
if(this._children) {
return this._children.length;
}
},
dispose : function() {
kampfer.Composition.superClass.dispose.call(this);
delete this._parent;
delete this._children;
delete this._childrenIndex;
}
});
kampfer.Composition.generateUniqueId = function() {
var guid = "";
for(var i = 1; i <= 32; i++) {
var n = Math.floor(Math.random() * 16.0).toString(16);
guid += n;
if((i == 8) || (i == 12) || (i == 16) || (i == 20)) {
guid += "-";
}
}
return guid;
};

View File

@@ -0,0 +1,94 @@
kampfer.require('class.UIComponent');
kampfer.require('dom');
kampfer.provide('Dialog');
kampfer.Dialog = kampfer.class.UIComponent.extend({
initializer : function() {},
createDom : function() {
kampfer.Dialog.superClass.createDom.call(this);
var element = this.getElement();
//header
this._headerElement = document.createElement('div');
//title
this._titleElement = document.createElement('h3');
//close
var closeButton = document.createElement('button');
//body
this._bodyElement = document.createElement('div');
//footer
this._footerElement = document.createElement('div');
this._headerElement.appendChild(closeButton);
this._headerElement.appendChild(this._titleElement);
element.appendChild(this._headerElement);
element.appendChild(this._bodyElement);
element.appendChild(this._footerElement);
kampfer.dom.addClass(this._element, 'modal');
kampfer.dom.addClass(this._headerElement, 'modal-header');
kampfer.dom.addClass(this._bodyElement, 'modal-body');
kampfer.dom.addClass(this._footerElement, 'modal-footer');
kampfer.dom.addClass(closeButton, 'close');
closeButton.innerHTML = 'x';
closeButton.setAttribute('data-action', 'close');
if(!this._buttons) {
this._footerElement.style.display = 'none';
} else {
for(var i = this._buttons.length - 1, buttonElment; (buttonElement = this._buttons[i]); i--) {
kampfer.dom.addClass(buttonElement, 'btn');
if(i === 0) {
kampfer.dom.addClass(buttonElement, 'btn-primary');
}
this._footerElement.appendChild(buttonElement);
}
}
},
setContent : function(html) {
this._bodyElement.innerHTML = html;
},
getContent : function(html) {
return this._bodyElement.innerHTML;
},
setTitle : function(title) {
this._titleElement.innerHTML = title;
},
//modal居中.但是bootstrap貌似直接定死了modal的宽度然后用样式居中.
reposition : function() {
var winWidth = Math.max(document.documentElement.offsetWidth, document.body.offsetWidth),
winHeight = Math.max(document.documentElement.offsetHeight, document.body.offsetHeight);
this._element.style.left = kampfer.dom.scrollLeft(window) + winWidth / 2 -
this._element.offsetWidth / 2 + 'px';
this._element.style.top = kampfer.dom.scrollTop(window) + winHeight / 2 -
this._element.offsetHeight / 2 + 'px';
},
show : function() {
if(!this._element) {
this.render();
}
this._element.style.display = '';
},
hide : function() {
this._element.style.display = 'none';
},
dispose : function() {
kampfer.Dialog.superClass.dispose.call(this);
delete this._titleElement;
delete this._headerElement;
delete this._bodyElement;
delete this._buttons;
delete this._footerElement;
}
});

View File

@@ -0,0 +1,43 @@
kampfer.require('events');
kampfer.require('Class');
kampfer.provide('events.EventTarget');
/*
* 所有需要实现自定义事件的类都必须继承EventTarget类。
*/
kampfer.events.EventTarget = kampfer.Class.extend({
_parentNode : null,
addListener : function(type, listener, context) {
k.events.addListener(this, type, listener, context);
},
removeListener : function(type, listener) {
k.events.removeListener(this, type, listener);
},
dispatch : function(type) {
if(type) {
var args = Array.prototype.slice.apply(arguments);
args.unshift(this);
k.events.dispatch.apply(null, args);
}
},
getParentEventTarget : function() {
return this._parentNode;
},
setParentEventTarget : function(obj) {
this._parentNode = obj;
},
dispose : function() {
this._parentNode = null;
k.events.removeListener(this);
}
});

View File

@@ -0,0 +1,150 @@
kampfer.require('class.Composition');
kampfer.require('events');
kampfer.provide('class.UIComponent');
kampfer.class.UIComponent = kampfer.class.Composition.extend({
_element : null,
_inDocument : false,
/**
* @type {object}
*/
events : null,
isInDocument : function() {
return this._inDocument;
},
setElement : function() {
this._element = element;
},
getElement : function() {
return this._element;
},
createDom : function() {
this._element = document.createElement('div');
return this._element;
},
//component有两种初始化的方式:
//1.动态生成
//2.传入已有dom,component解析
//decorate方法就是针对第二种方式处理解析和预处理逻辑
decorate : function(element) {},
enterDocument : function() {
this._inDocument = true;
var that = this;
if(this.events) {
for(var attr in this.events) {
kampfer.events.addListener(this._element, attr, this._transition, this);
}
}
this.walk('enterDocument');
},
_transition : function(event) {
var element = event.target,
handlers = this.events[event.type],
action = element.getAttribute('data-action');
while( !action && (element = element.parentNode) ) {
if(element.getAttribute) {
action = element.getAttribute('data-action');
}
}
if( !action || !handlers || !(action in handlers) ) {
return;
}
event.target = element;
if(typeof handlers[action] === 'string') {
handlers = handlers[action].split(' ');
for(var i = 0, handle; (handle = handlers[i]); i++) {
if( this[handle] && this[handle](event) === false ) {
return false;
}
}
} else if(typeof handlers[action] === 'function') {
handlers[action].call(this, event);
}
},
exitDocument : function() {
this._inDocument = true;
kampfer.events.removeListener(this._element);
this.walk('exitDocument');
},
render : function(parentElement, beforeNode) {
if(this._inDocument) {
return;
}
if(!this._element) {
this.createDom();
}
if(parentElement) {
parentElement.insertBefore(this._element, beforeNode || null);
} else {
document.body.appendChild(this._element);
}
//父component存在,但是它不在document中,那么子component不进入document
if( !this._parent || this._parent.isInDocument() ) {
this.enterDocument();
}
},
addChild : function(child, render) {
this.addChildAt(child, this.getChildCount(), render);
},
addChildAt : function(child, index, render) {
kampfer.UIComponent.superClass.addChildAt.call(this, child, index);
if( child._inDocument && this._inDocument && child.getParent() === this ) {
var parentElement = this.getElement();
parentElement.insertBefore( child.getElement(),
(parentElement.childNodes[index] || null) );
} else if(render) {
if (!this._element) {
this.createDom();
}
var sibling = this.getChildAt(index + 1);
child.render_(this.getElement(), sibling ? sibling._element : null);
}
},
removeChild : function(child, unrender) {
kampfer.UIComponent.superClass.removeChild.call(this, child);
if(unrender) {
child.exitDocument();
if( child._element ) {
child._element.parentNode.removeChild(child._element);
}
}
},
removeChildAt : function(index, unrender) {
this.remochild( this.getChildAt(index), unrender );
},
dispose : function() {
kampfer.UIComponent.superClass.dispose.call(this);
this.exitDocument();
delete this._element;
}
});