modal.js
3.59 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
/**
* Modal 组件: 仿BOOTSTRAP:MODAL
* @author xuan.chen@yoho.cn
*/
'use strict';
/* config */
var NAME = 'yModal';
var DATA_KEY = 'yoho.modal';
var Selector = {
DATA_TOGGLE: '[data-toggle="ymodal"]',
DATA_DISMISS: '[data-dismiss="ymodal"]'
};
var ClassName = {
BACKDROP: 'ymodal-backdrop'
};
var Modal = function(elem, config) {
this.$element = $(elem);
this.$body = $(document.body);
this._backdrop = null;
this._config = $.extend(
{},
Modal.DEFAULT,
toString.call(config) === '[object Object]' && config
);
this._isShown = false;
};
/* ------------------------static attribute ------------------*/
Modal.DEFAULT = {
backdrop: true,
show: true
};
/* ------------------------public method ------------------*/
Modal.prototype.show = function(relatedTarget) {
if (this._isShown) {
return this;
}
this.$body.toggleClass('ymodal-open', true);
this.$element.trigger('show.yoho.modal', relatedTarget);
this.$element.on('click.dismiss.yoho.modal', Selector.DATA_DISMISS, $.proxy(this.hide, this));
this._isShown = true;
this.$element.show().focus();
this.backdrop();
this.$element.trigger('shown.yoho.modal', relatedTarget);
};
Modal.prototype.hide = function(e) {
if (e) {
e.preventDefault();
e.stopPropagation();
}
this._isShown = false;
this.$element
.off('click.dismiss.yoho.modal')
.hide();
this.$body.removeClass('ymodal-open');
this.backdrop();
this.$element.trigger('hiden.yoho.modal');
};
Modal.prototype.toggle = function(relatedTarget) {
this._isShown ? this.hide() : this.show(relatedTarget);
return this;
};
Modal.prototype.backdrop = function(callback) {
var self = this;
if (this._isShown && this._config.backdrop) {
this._backdrop = document.createElement('div');
$(this._backdrop)
.addClass(ClassName.BACKDROP)
.appendTo(document.body);
this.$element.on('click.dismiss.yoho.modal', function(event) {
if (event.target !== event.currentTarget) {
return;
}
self.hide();
});
} else if (!this._isShown && this._backdrop) {
$(this._backdrop).remove();
}
};
/* ------------------------private method ------------------*/
/* ------------------------static method ------------------*/
Modal._jqueryBridge = function(config, relatedTarget) {
return this.each(function() {
var $this = $(this);
var data = $this.data(DATA_KEY);
var configs = $this.data(DATA_KEY) ? 'toggle' : $.extend(
{},
Modal.DEFAULT,
$this.data(),
toString.call(config) === '[object Object]' && config
);
if (!data) {
$this.data(DATA_KEY, data = new Modal($this, config));
}
if (typeof config === 'string') {
data[config](relatedTarget);
} else if (configs.show) {
data.show(relatedTarget);
}
});
};
/* ----------------------- DATA-API ----------------------*/
$(document).on('click.yoho.modal.data-api', Selector.DATA_TOGGLE, function(event) {
var $this = $(this);
var selector = this.getAttribute('data-target');
var $target = $(selector);
var config = $target.data(DATA_KEY) ? 'toggle' : $.extend($target.data(), $this.data());
if ($target[0].tagName.toUpperCase() === 'A') {
event.preventDefault();
}
Modal._jqueryBridge.call($target, config, this);
});
$.fn[NAME] = Modal._jqueryBridge;
$.fn[NAME].constructor = Modal;
module.exports = Modal;