-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
219 lines (179 loc) · 5.14 KB
/
script.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
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
/*
* Tip
* ---
*
* Version 1.1, 2017-12-15
*
* (c) Copyright 2017 Hazem AbuMostafa.
* This project is subject to the Apache License, Version 2.0
* <http://apache.org/licenses/LICENSE-2.0.html>.
*/
var Tip = function(options){
var self = this;
options = options || {};
self.elem = self.getElement(options.id || 'tip');
self.delay = options.delay || parseInt(self.elem.dataset.duration) || 3500;
self.onClickMessage = options.onClickMessage || '';
self.hover = false;
self.visible = false; //The tip is currently visible to the user, or animating to disappear.
self.prev = null; //Holding the original tip string while clicked.
self.mode = null;
self.hideTimeoutId = null;
self.killTimeoutId = null;
//Listeners:
self.elem.onmouseover = function(e){
if(self.isHiddenOrKilled())
return;
self.hover = true;
window.clearTimeout(self.hideTimeoutId);
self.hideTimeoutId = null;
};
self.elem.onmouseout = function(e){
if(self.isHiddenOrKilled())
return;
self.hover = false;
self.hideTimeoutId = window.setTimeout(self.hide.bind(self), self.delay/2);
if(self.isClicked()){
self.elem.innerHTML = self.prev;
self.elem.setAttribute('class','in-move');
self.adjustPos();
self.prev = null;
}
};
//Only add on-click listener when there's actually something to do:
if(self.onClickMessage){
self.elem.onclick = function(e){
if(!self.isClicked()){
self.prev = self.elem.innerHTML;
self.elem.innerHTML = self.onClickMessage;
self.elem.setAttribute('class','in-move');
self.adjustPos();
}
};
}
window.addEventListener('resize', function(e){
//Stopping any in-move animations/transitions:
self.elem.classList.remove('in-move');
self.adjustPos();
});
};
//Functions:
Tip.prototype.getElement = function(id){
var exist = document.getElementById(id),
theElem;
if(exist)
theElem = exist;
else{
theElem = document.createElement('div');
theElem.id = id;
document.body.appendChild(theElem);
}
theElem.setAttribute('class', 'killed');
return theElem;
};
Tip.prototype.adjustPos = function(){
var leftValue = (window.innerWidth/2) - (this.elem.offsetWidth/2);
this.elem.style.left = leftValue.toString() + 'px';
};
Tip.prototype.hide = function(){
this.elem.setAttribute('class','hidden');
this.elem.style.opacity = '0';
var duration = this.getCurrentAnimationDuration();
this.killTimeoutId = window.setTimeout(function(){
this.visible = false;
this.mode = null;
this.elem.setAttribute('class', 'killed');
this.elem.innerHTML = '';
}.bind(this), duration);
};
/*
* The main method. Shows a new tip to the user, and overrides
* the previous tip if any.
*/
Tip.prototype.echo = function(text, mode, animateInAgain){
if(this.isHiddenOrKilled())
this.elem.setAttribute('class','shown');
else{
this.elem.setAttribute('class','in-move');
if(animateInAgain !== false && text == this.elem.innerHTML){
this.elem.setAttribute('class','in-again');
var duration = this.getCurrentAnimationDuration();
window.setTimeout(function(){
this.elem.classList.remove('in-again');
}.bind(this), duration);
}
}
window.clearTimeout(this.hideTimeoutId);
this.hideTimeoutId = null;
window.clearTimeout(this.killTimeoutId);
this.killTimeoutId = null;
this.elem.innerHTML = text.toString();
this.elem.style.opacity = '1';
if(!this.hover)
this.hideTimeoutId = window.setTimeout(this.hide.bind(this), this.delay);
this.prev = null;
this.mode = mode;
this.visible = true;
this.adjustPos();
};
/*
* Force the tip to hide even if the timer isn't over yet.
*/
Tip.prototype.forceHide = function(){
if(this.hideTimeoutId){
window.clearTimeout(this.hideTimeoutId);
this.hideTimeoutId = null;
window.clearTimeout(this.killTimeoutId);
this.killTimeoutId = null;
this.hide();
}
};
/*
* Updates the tip text without renewing the timer.
* Works best with `Tip.isVisible()`, for regularly updating
* very-time-sensitive data while the tip is visible to the user.
*/
Tip.prototype.updateText = function(text){
if(this.visible && !this.isClicked())
this.elem.innerHTML = text.toString();
else if(this.visible)
this.prev = text;
this.adjustPos();
};
Tip.prototype.getText = function(){
return this.elem.innerHTML;
};
Tip.prototype.getMode = function(){
return this.mode;
};
Tip.prototype.isVisible = function(){
return this.visible;
};
/*
* Returns the animation duration for the current class list
* of the tip element. If there are multiple animations, it
* returns the duration for the first one.
*/
Tip.prototype.getCurrentAnimationDuration = function(){
var dur = window.getComputedStyle(this.elem).getPropertyValue('animation-duration');
return parseFloat(dur) * 1000; //'0.2s' (string) => 2000 (number).
};
Tip.prototype.isClicked = function(){
return this.prev != null;
};
Tip.prototype.isHiddenOrKilled = function(){
return this.elem.classList.contains('hidden') ||
this.elem.classList.contains('killed');
};
/*
* Module export
*/
if(typeof module !== 'undefined' && typeof module.exports !== 'undefined'){
module.exports = Tip;
} else if(typeof define === 'function' && define.amd){
define(function(){
return Tip;
});
} else if(window){
window.Tip = Tip;
}