forked from ecomfe/moye
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDialog.tpl
301 lines (236 loc) · 7.45 KB
/
Dialog.tpl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
{% target: Dialog(master=base) %}
{% content: style%}
<link rel="stylesheet" href="../src/css/Dialog.less">
<style>
.content {
margin: 20px 0;
}
</style>
{% content: content%}
{%filter: markdown%}
# Dialog 窗口
## 普通窗口
```html
<button id="newDialog">普通青年(呃,普通窗口)</button>
```
```js
var dialog = new Dialog({
title: '普通青年有一个普通标题',
content: ''
+ '<h3>普通青年背古诗...</h3>'
+ '<br>'
+ '君不见孤雁关外发,酸嘶度扬越。<br> '
+ '空城客子心肠断,幽闺思妇气欲绝。<br>'
+ '凝霜夜下拂罗衣,浮云中断开明月。<br>'
+ '夜夜遥遥徒相思,年年望望情不歇。<br>'
+ '寄我匣中青铜镜,倩人为君除白发。<br>'
+ '行路难,行路难,<br>'
+ '夜闻南城汉使度,使我流泪忆长安!<br>'
}).render();
$('#dialog').on('click', $.proxy(dialog.show, dialog));
```
<div class="content">
<button id="dialog">普通青年(呃,普通窗口)</button>
</div>
<script>
require(['jquery', 'ui/Dialog', 'ui/Button'], function ($, Dialog) {
var dialog = new Dialog({
content: ''
+ '<h3>普通青年背古诗...</h3>'
+ '<br>'
+ '君不见孤雁关外发,酸嘶度扬越。<br> '
+ '空城客子心肠断,幽闺思妇气欲绝。<br>'
+ '凝霜夜下拂罗衣,浮云中断开明月。<br>'
+ '夜夜遥遥徒相思,年年望望情不歇。<br>'
+ '寄我匣中青铜镜,倩人为君除白发。<br>'
+ '行路难,行路难,<br>'
+ '夜闻南城汉使度,使我流泪忆长安!<br>',
title: '普通青年有一个普通标题'
}).render();
$('#dialog').on('click', $.proxy(dialog.show, dialog));
});
</script>
## 会自毁的窗口
```js
$('#auto-dispose').on('click', function () {
new Dialog({
content: '会自毁的窗口' + (++i),
title: '会自毁的窗口',
hideDispose: true
}).render().show();
});
```
当窗口被隐藏时自动销毁
<div class="content">
<button id="auto-dispose">会自毁的窗口</button>
</div>
<script>
require(['jquery', 'ui/Dialog', 'ui/Button'], function ($, Dialog) {
var i = 0;
$('#auto-dispose').on('click', function () {
new Dialog({
content: '会自毁的窗口' + (++i),
title: '会自毁的窗口',
hideDispose: true
}).render().show();
});
});
</script>
```js
var dialog = new Dialog({
content: '设定`buttons`参数来添加脚注中的按钮',
title: '带按钮的窗口',
buttons: [{
text: '嘿嘿, 我是一个按钮',
part: 'big-button'
}]
}).render().on('buttonclick', function (e) {
// e.part => big-button
this.hide();
});
$('#footer-buttons').on('click', $.proxy(dialog.show, dialog));
```
## 带按钮的窗口
首先, 我们提供`buttons`参数. 通过这个参数, 可以很方便地添加脚注中的按钮
<div class="content">
<button id="footer-buttons">带按钮的窗口</button>
</div>
<script>
require(['jquery', 'ui/Dialog', 'ui/Button'], function ($, Dialog) {
var dialog = new Dialog({
content: '设定`buttons`参数来添加脚注中的按钮',
title: '带按钮的窗口',
buttons: [{
text: '好',
part: 'big',
skin: 'mini'
}]
})
.render()
.on('big', function (e) {
this.hide();
});
$('#footer-buttons').on('click', $.proxy(dialog.show, dialog));
});
</script>
```js
var dialog = new Dialog({
content: '带按钮的窗口',
title: '带按钮的窗口',
footer: '<button>ok</button>'
}).render().on('click', function (e) {
if ($(e.target).is('button')) {
this.hide();
}
});
$('#footer').on('click', $.proxy(dialog.show, dialog));
```
还可以通过设定`footer`的值, 来构建自定义脚注, 自行添加按钮
<div class="content">
<button id="footer">带按钮的窗口</button>
</div>
<script>
require(['jquery', 'ui/Dialog', 'ui/Button'], function ($, Dialog) {
var dialog = new Dialog({
content: '带按钮的窗口',
title: '带按钮的窗口',
footer: '<button>ok</button>'
}).render().on('click', function (e) {
if ($(e.target).is('button')) {
this.hide();
}
});
$('#footer').on('click', $.proxy(dialog.show, dialog));
});
</script>
> 注意: `footer`与`buttons`的优先级. 如果, `footer`与`buttons`同时存在, 那么`footer`会覆盖`buttons`.
是不是觉得自己拼`按钮`很麻烦呢? 来, 我们内置一些常用的带按钮的窗口~
## 内置的窗口们
```js
var i = 0;
$('#alert-dialog').on('click', function () {
var me = this;
Dialog.alert({
title: 'WARNING',
content: '狼来啦, 大王快跑呀~!',
buttons: [{
text: '傻呀, 当然跑啊'
}]
}).then(function () {
me.innerHTML = '狼来啦~' + (++i) + '次';
});
});
```
### 警告窗口 `Dialog.alert()`
<div class="content">
<button id="alert-dialog">狼来啦~</button>
</div>
<script>
require(['jquery', 'ui/Dialog', 'ui/Button'], function ($, Dialog) {
var i = 0;
$('#alert-dialog').on('click', function () {
var me = this;
Dialog.alert({
title: 'WARNING',
content: '狼来啦, 大王快跑呀~!',
buttons: [{
text: '傻呀, 当然跑啊'
}]
}).then(function () {
me.innerHTML = '狼来啦~' + (++i) + '次';
});
});
});
</script>
### 确认窗口 `Dialog.confirm()`
```js
$('#confirm-dialog').on('click', function () {
var me = this;
Dialog.confirm({
title: ' ', // 小trick, 有标题, 但是没有字...
content: '大王, 狼来了, 约吗? 啊不, 跑吗~!?',
buttons: [{
text: '约约约'
}, {
text: '约你妹'
}]
}).then(function () {
me.innerHTML = '狼来啦~约了' + (++i) + '次, 不约' + j + '次';
}, function () {
me.innerHTML = '狼来啦~约了' + i + '次, 不约' + (++j) + '次';
});
});
```
<div class="content">
<button id="confirm-dialog">狼来啦~</button>
</div>
<script>
require(['jquery', 'ui/Dialog', 'ui/Button'], function ($, Dialog) {
var i = 0;
var j = 0;
$('#confirm-dialog').on('click', function () {
var me = this;
Dialog.confirm({
title: ' ', // 小trick, 有标题, 但是没有字...
content: '大王, 狼来了, 约吗? 啊不, 跑吗~!?',
buttons: [{
text: '约约约'
}, {
text: '约你妹'
}]
}).then(function () {
me.innerHTML = '狼来啦~约了' + (++i) + '次, 不约' + j + '次';
}, function () {
me.innerHTML = '狼来啦~约了' + i + '次, 不约' + (++j) + '次';
});
});
});
</script>
### 内置的窗口们的特性
1. `Dialog.alert()`和`Dialog.confirm()`会返回`promise`. 即这两个操作是异步的, 会返回预期结果. 用户只能在我们限定的选项中做出一个选择. 当`promise`被`resolved`或`rejected`, 会带有按钮的标识`part`.
2. 都是没有关闭按钮的. 原因是关闭按钮在这里是多余的. 关闭按钮在`alert`中表达的`确认`, 与`确认`按钮一致; 在`confirm`中表达的意图是`取消`, 与`取消`按钮一致. 因此, 关闭按钮是重复的, 不展现这个按钮. 这也与`window.alert()`和`window.confirm()`的交互相一致.
3. 都是会自毁的. 这与window.alert()和window.confirm()是一致的.
4. `Dialog.alert()`产生的窗口带有样式类`.skin-alert-dialog`, 也可以添加其他的`skin`来丰富样式效果.
5. `Dialog.confirm()`产生的窗口带有样式类`.skin-confirm-dialog`, 也可以添加其他的`skin`来丰富样式效果.
6. 点击`mask`不会关闭窗口, 原因与2一致.
{%/filter%}