Skip to content

Commit 217df5d

Browse files
committed
Added Dialog
1 parent cc6e51e commit 217df5d

18 files changed

+739
-91
lines changed

src/Button.js

Lines changed: 174 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,174 @@
1+
/**
2+
* ESUI (Enterprise Simple UI)
3+
* Copyright 2013 Baidu Inc. All rights reserved.
4+
*
5+
* @file 按钮
6+
* @author dbear
7+
*/
8+
9+
define(
10+
function (require) {
11+
var lib = require('./lib');
12+
var helper = require('./controlHelper');
13+
var Control = require('./Control');
14+
var ui = require('./main');
15+
16+
/** button模板 */
17+
var tplButton = ''
18+
+ '<div id="${lfIconId}" class="${lfIconClass}"></div>'
19+
+ '<div id="${labelId}" class="${labelClass}">${btnLabel}</div>'
20+
+ '<div id="${rtIconId}" class="${rtIconClass}"></div>';
21+
22+
/**
23+
* 默认选项配置
24+
*
25+
* @const
26+
* @inner
27+
* @type {Object}
28+
*/
29+
var DEFAULT_OPTION = {
30+
content: '', // 按钮的显示文字
31+
disabled: false // 控件是否禁用
32+
};
33+
34+
/**
35+
* 按钮控件类
36+
*
37+
* @constructor
38+
* @param {Object} options 初始化参数
39+
*/
40+
function Button(options) {
41+
Control.apply(this, arguments);
42+
};
43+
44+
/**
45+
* 构建main的主内容
46+
*
47+
* @param {string} type foot | body
48+
* @inner
49+
*/
50+
function getMainHtml(control, type) {
51+
var me = control;
52+
var data = {
53+
lfIconId: helper.getId(me, 'lf-icon'),
54+
lfIconClass: helper.getClasses(me, 'lf-icon').join(' '),
55+
labelId: helper.getId(me, 'label'),
56+
labelClass: helper.getClasses(me, 'label').join(' '),
57+
btnLabel: me.content || '&nbsp;',
58+
rtIconId: helper.getId(me, 'rt-icon'),
59+
rtIconClass: helper.getClasses(me, 'rt-icon').join(' ')
60+
};
61+
var innerHtml = lib.format(tplButton, data);
62+
return innerHtml;
63+
64+
};
65+
66+
Button.prototype = {
67+
/**
68+
* 控件类型
69+
*
70+
* @type {string}
71+
*/
72+
type: 'Button',
73+
74+
/**
75+
* 初始化参数
76+
*
77+
* @param {Object=} options 构造函数传入的参数
78+
* @override
79+
* @protected
80+
*/
81+
initOptions: function(options) {
82+
options = lib.extend(options, DEFAULT_OPTION);
83+
if (options.main) {
84+
options.tagName = options.main.nodeName.toLowerCase();
85+
if (options.text == null) {
86+
options.text = lib.getText(options.main);
87+
}
88+
if (options.tagName != 'DIV') {
89+
var tempInnerHtml = options.main.innerHTML;
90+
options.main = document.createElement('div');
91+
options.main.innerHTML = tempInnerHtml;
92+
options.tagName = 'DIV';
93+
}
94+
var innerDiv = options.main.firstChild;
95+
if (!options.content
96+
&& innerDiv
97+
&& innerDiv.tagName != 'DIV'
98+
) {
99+
options.content = options.main.innerHTML;
100+
}
101+
}
102+
this.setProperties(options);
103+
},
104+
105+
/**
106+
* 创建控件主元素
107+
*
108+
* @return {HTMLElement}
109+
* @override
110+
*/
111+
createMain: function () {
112+
this.main = document.createElement('div');
113+
},
114+
115+
/**
116+
* 重新渲染视图
117+
* 仅当生命周期处于RENDER时,该方法才重新渲染
118+
*
119+
* @param {Array=} 变更过的属性的集合
120+
* @override
121+
*/
122+
repaint: function (changes) {
123+
var main = this.main;
124+
main.innerHTML = getMainHtml(this);
125+
126+
// 初始化状态事件
127+
main.onclick = lib.bind(this.clickHandler, this);
128+
helper.initMouseBehavior(this);
129+
130+
},
131+
132+
/**
133+
* 鼠标点击事件处理函数
134+
*/
135+
clickHandler: function () {
136+
this.fire('click');
137+
},
138+
139+
/**
140+
* 设置内容
141+
*
142+
* @param {string} content 要设置的内容.
143+
*/
144+
setContent: function (content) {
145+
this.content = content;
146+
if ( this.lifeCycle == Control.LifeCycle.RENDERED) {
147+
this.repaint({'content': content});
148+
}
149+
},
150+
151+
/**
152+
* 销毁释放控件
153+
*
154+
* @override
155+
*/
156+
dispose: function () {
157+
helper.beforeDispose(this);
158+
159+
var main = this.main;
160+
if (main) {
161+
main.onclick = null;
162+
}
163+
164+
helper.dispose(this);
165+
helper.afterDispose(this);
166+
}
167+
};
168+
169+
lib.inherits(Button, Control);
170+
ui.register(Button);
171+
172+
return Button;
173+
}
174+
);

src/Control.js

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,11 @@ define(
2323
this.states = {};
2424
this.events = {};
2525
this.domEvents = {};
26+
options = options || {};
2627

27-
this.initOptions(options);
28+
this.main = options.main ? options.main : this.createMain(options);
2829

29-
if (!this.main) {
30-
this.main = this.createMain();
31-
}
30+
this.initOptions(options);
3231

3332
// 自创建id
3433
if (!this.id) {
@@ -82,6 +81,12 @@ define(
8281
return document.createElement('div');
8382
},
8483

84+
/**
85+
* 初始化DOM结构,仅在第一次渲染时调用
86+
*/
87+
initStructure: function () {
88+
},
89+
8590
/**
8691
* 渲染控件
8792
*
@@ -96,6 +101,8 @@ define(
96101
this.main.id = helper.getId(this);
97102
}
98103

104+
this.initStructure();
105+
99106
helper.addClass(this, this.main);
100107
this.setDisabled(this.disabled);
101108
}
@@ -371,7 +378,13 @@ define(
371378
*/
372379
addChild: function (control, childName) {
373380
childName = childName || control.childName;
381+
382+
if (control.parent) {
383+
control.parent.removeChild(control);
384+
}
385+
374386
this.children.push(control);
387+
control.parent = this;
375388

376389
if (childName) {
377390
control.childName = childName;
@@ -404,6 +417,8 @@ define(
404417
if (childName) {
405418
this.childrenIndex[childName] = null;
406419
}
420+
421+
control.parent = null;
407422
},
408423

409424
/**

0 commit comments

Comments
 (0)