dialog.js
4.22 KB
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
/**
* 弹框公共组件
* @author: xuqi<qi.xu@yoho.cn>
* @date: 2016/2/24
*/
var $ = require('yoho-jquery'),
Handlebars = require('yoho-handlebars');
var defaultOptions = {
mask: true,
closeIcon: true,
refreshOnClose: false
};
var tpl =
'<div class="yoho-dialog {{className}} hide">' +
'{{#if closeIcon}}' +
'<span class="close">' +
'<i class="iconfont"></i>' +
'</span>' +
'{{/if}}' +
'<div class="content">' +
'{{{content}}}' +
'</div>' +
'<div class="btns">' +
'{{# btns}}' +
'<span {{#if id}}id="dialog-{{id}}"{{/if}} class="btn{{#each btnClass}} {{.}}{{/each}}">' +
'{{name}}' +
'</span>' +
'{{/ btns}}' +
'</div>' +
'</div>';
var tplFn = Handlebars.compile(tpl);
// 背景蒙版
function createMask() {
if ($('.body-mask').length === 0) {
$('body').append('<div class="body-mask hide"></div>');
}
return $('.body-mask').css({
height: $(document).height(),
width: $(document).width()
});
}
function createDialog(data) {
$('body').append(tplFn(data));
return $('.yoho-dialog');
}
function cerateSubContent(text) {
var $yohoDialog = $('.yoho-dialog'),
subContent = '<p class="sub-content">' + text + '</p>';
if ($yohoDialog && $yohoDialog.length > 0) {
$yohoDialog.find('.content').after(subContent);
}
}
function Dialog(options) {
var opt = $.extend({}, defaultOptions, options);
var that = this,
i;
// 实现继承时,只返回实例,不生成html
if (opt.inherit) {
return this;
}
if (opt.mask) {
that.$mask = createMask();
}
that.$el = createDialog(opt);
if (opt.subContent) {
cerateSubContent(opt.subContent);
}
if (opt.subContents) {
for (i = opt.subContents.length - 1; i >= 0; i--) {
cerateSubContent(opt.subContents[i]);
}
}
// 绑定x关闭事件
that.$el.find('.close').click(function() {
that.close();
if (options.refreshOnClose) {
window.location.reload();
}
});
// 绑定btn关闭事件
that.$el.find('.btn-close').click(function() {
that.close();
});
function bindBtnEvt(index) {
that.$el.find('#dialog-' + opt.btns[index].id).on('click', function() {
opt.btns[index].cb && opt.btns[index].cb();
});
}
// 绑定按钮事件
if (opt.btns) {
for (i = 0; i < opt.btns.length; i++) {
bindBtnEvt(i);
}
}
}
Dialog.prototype.close = function() {
this.$mask && this.$mask.addClass('hide');
this.$el.remove();
};
Dialog.prototype.show = function() {
this.$mask && this.$mask.removeClass('hide');
this.$el.removeClass('hide').css({
'margin-top': -this.$el.height() / 2,
'margin-left': -this.$el.width() / 2
});
};
exports.Dialog = Dialog;
// Alert
function Alert(content) {
var that = this;
var option = {
content: content,
className: 'alert-dialog',
btns: [
{
id: 'alert-sure',
btnClass: ['alert-sure'],
name: '确定',
cb: function() {
that.close();
}
}
]
};
Dialog.call(this, option);
}
Alert.prototype = new Dialog({
inherit: true
});
Alert.prototype.constructor = Alert;
exports.Alert = Alert;
// Confirm
function Confirm(opt) {
var that = this;
var option = {
content: opt.content,
className: 'confirm-dialog',
btns: [
{
id: 'confirm-sure',
btnClass: ['confirm-sure'],
name: '确定',
cb: opt.cb
},
{
id: 'confirm-cancel',
btnClass: ['confirm-cancel'],
name: '取消',
cb: function() {
that.close();
}
}
]
};
Dialog.call(this, option);
}
Confirm.prototype = new Dialog({
inherit: true
});
Confirm.prototype.constructor = Confirm;
exports.Confirm = Confirm;