-
Notifications
You must be signed in to change notification settings - Fork 0
/
alert.js
190 lines (164 loc) · 4.93 KB
/
alert.js
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
var hrxAlert = {
SUCCESS: 'success',
WARNING: 'warning',
DANGER: 'danger',
container:'alert-container',
cshow:'alert-show',
cright:'alert-right',
cleft:'alert-left',
ctop:'alert-top',
cdown:'alert-down',
csuccess:'alert-success',
cwarning:'alert-warning',
cdanger:'alert-danger'
};
var Animation = {
SLIDEIN: 'slidein'
};
var Position = {
RIGHT: 'right',
LEFT: 'left',
TOPLEFT: 'top-left',
TOPRIGHT: 'top-right',
BOTTOMRIGHT: 'bottom-right',
BOTTOMLEFT: 'bottom-left'
};
$.alert = (options) => {
var defOpt = {
message: '',
type: hrxAlert.SUCCESS,
animation: Animation.SLIDEIN,
position: Position.RIGHT,
destroy: 2
};
if ($('.alert-container').length == 0)
$("body").append('<div class="alert-container"></div>');
var settings = $.extend({}, defOpt, options);
var alertContainer = $('.alert-container')[0];
var alertMessageWrap = $('<div class="alert-message-wrap"></div>');
var alertMessage = $('<p class="alert-message"></p>');
$(alertMessage).text(settings.message);
$(alertMessage).appendTo(alertMessageWrap);
$(alertMessageWrap).appendTo(alertContainer);
if (settings.position.includes('bottom'))
$(alertContainer).append(alertMessageWrap);
else
$(alertContainer).prepend(alertMessageWrap);
addType(alertMessageWrap, settings.type);
addPosition(alertContainer, settings.position);
setTimeout(function () {
showAlert(alertMessageWrap, settings.animation);
}, 100);
destroyAlert(alertMessageWrap,Number(settings.destroy));
};
$.modal = (options) => {
var defOpt = {
title : 'Modal title',
message:'Here goes message',
animation:'zoomin'
};
if ($('.hrx-modal-container').length == 0)
$("body").append(`<div class="hrx-modal-container">
<div class="hrx-modal hrx-zoom-out">
<span class="hrx-modal-close"> x </span>
<div class="hrx-modal-title">Modal Title</div>
<div class="hrx-modal-message">Modal message</div>
<div class="hrx-modal-footer"></div>
</div>
<div class="hrx-bg"></div>
</div>`);
var modalContainer = $('.hrx-modal-container')[0];
var settings = $.extend({}, defOpt, options);
$(modalContainer).find('.hrx-modal-title').text(settings.title);
$(modalContainer).find('.hrx-modal-message').text(settings.message);
var closeBtn = $(modalContainer).find('.hrx-modal-close');
$(document).on('click', '.hrx-modal-close', function() {
RemoveModalAnimation(modalContainer);
setTimeout(function(){$(modalContainer).remove();},300);
});
setTimeout(function(){
ApplyModalAnimation(modalContainer);},100);
};
function ApplyModalAnimation(modalContainer){
$(modalContainer).find('.hrx-modal').removeClass('hrx-zoom-out').addClass('hrx-zoom-in');
}
function RemoveModalAnimation(modalContainer){
$(modalContainer).find('.hrx-modal').removeClass('hrx-zoom-in').addClass('hrx-zoom-out');
}
//Utility functions
function showAlert(ele, anim) {
addAnimation(ele, anim);
addClass(ele, hrxAlert.cshow);
}
function addPosition(ele, pos) {
switch (pos) {
case "top-right":
addMClass(ele, 'alert-right,alert-top');
break;
case "bottom-right":
addMClass(ele, 'alert-right,alert-bottom');
break;
case "top-left":
addMClass(ele, 'alert-left,alert-top');
break;
case "bottom-left":
addMClass(ele, 'alert-left,alert-bottom');
break;
}
}
function addType(ele, type) {
switch (type) {
case hrxAlert.SUCCESS:
addClass(ele, hrxAlert.csuccess);
break;
case hrxAlert.DANGER:
addClass(ele, hrxAlert.cdanger);
break;
case hrxAlert.WARNING:
addClass(ele, hrxAlert.cwarning);
break;
}
}
function addAnimation(ele, anim) {
switch (anim) {
case "slidein":
addClass(ele, 'alert-slide-in');
break;
case "fadein":
addClass(ele, 'alert-fade-in');
break;
}
}
function destroyAlert(ele, time) {
setTimeout(function () {
hideAlert(ele);
removeAnimation(ele);
setTimeout(function(){
$(ele).remove();
},100);
}, time * 1000);
}
function hideAlert(ele){
removeClass(ele,hrxAlert.cshow);
}
function removeAnimation(ele){
removeMClass(ele,'alert-slide-in,alert-fade-in');
}
function addClass(ele, cls) {
$(ele).addClass(cls);
}
function removeClass(ele, cls) {
$(ele).removeClass(cls);
}
function addMClass(ele, cls) {
var c = cls.split(',')
c.forEach(element => {
addClass(ele,element);
});
}
function removeMClass(ele, cls) {
var c = cls.split(',')
c.forEach(element => {
removeClass(ele,element);
});
}