-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtip.js
More file actions
278 lines (241 loc) · 7.07 KB
/
tip.js
File metadata and controls
278 lines (241 loc) · 7.07 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
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
define(['view',
'render',
'class'],
function(View, render, clazz) {
// TODO: Ensure that the metrics functions used in this view are available in
// minimal, non-jQuery DOM utilities (ex: Zepto, Bonzo, Anchor, etc.)
function Tip(el, options) {
options = options || {};
Tip.super_.call(this, el, options);
this.arrowEl = this.el.find(options.arrowSelector || '.arrow');
this.className = options.className || 'tip';
this._bodySel = options.bodySelector || options.contentSelector || '.body';
this._fixed = options.fixed || false;
this._padj = { top: 0, left: 0 };
this._autoRemove = options.autoRemove !== undefined ? options.autoRemove : true;
this.position(options.position || 'north');
}
clazz.inherits(Tip, View);
Tip.prototype.body =
Tip.prototype.content = function(el) {
this.el.find(this._bodySel).empty().append(el);
return this;
};
Tip.prototype.position = function(type) {
this._position = type;
return this;
};
Tip.prototype.attach = function(el, delay) {
el = render.$(el); delay = delay || 0;
this._attachedTo = el;
this._onmouseenterel = mouseenterel.bind(this);
this._onmouseleaveel = mouseleaveel.bind(this);
this._onmouseentertip = mouseentertip.bind(this);
this._onmouseleavetip = mouseleavetip.bind(this);
// show tip on hover
el.on('mouseenter', this._onmouseenterel);
el.on('mouseleave', this._onmouseleaveel);
// cancel hide on hover
this.el.on('mouseenter', this._onmouseentertip);
this.el.on('mouseleave', this._onmouseleavetip);
function mouseenterel(e) { this.show(el); }
function mouseleaveel(e) { this.hide(delay); }
function mouseentertip(e) { this.cancelHide(); }
function mouseleavetip(e) { this.hide(delay); }
}
Tip.prototype.unattach = function() {
this._attachedTo.off('mouseenter', this._onmouseenterel);
this._attachedTo.off('mouseleave', this._onmouseleaveel);
this.el.off('mouseenter', this._onmouseentertip);
this.el.off('mouseleave', this._onmouseleavetip);
delete this._attachedTo;
}
Tip.prototype.show = function(el) {
if (!el) throw new Error('Tip.show() element required');
this.target = render.$(el);
this.emit('show', this.target);
this.el.appendTo(document.body);
this.el.addClass('tip-' + this._position);
this.reposition();
this.el.removeClass('hide');
this._reposition = this.reposition.bind(this);
render.$(window).on('resize', this._reposition);
render.$(window).on('scroll', this._reposition);
return this;
}
Tip.prototype.hide = function(ms) {
// duration
if (ms) {
this._hide = setTimeout(this.hide.bind(this), ms);
return this;
}
// hide
this.emit('hide');
render.$(window).off('scroll', this._reposition);
render.$(window).off('resize', this._reposition);
this.el.addClass('hide');
if (this._autoRemove) {
var self = this;
setTimeout(function() {
self.remove();
self.dispose();
}, 10);
}
return this;
}
Tip.prototype.cancelHide = function() {
if (!this._hide) return;
clearTimeout(this._hide);
delete this._hide;
};
Tip.prototype.remove = function() {
if (this._attachedTo) {
this.el.detach();
return this;
}
return Tip.super_.prototype.remove.call(this);
};
/**
* Reposition the tip if necessary.
*
* @api private
*/
Tip.prototype.reposition = function() {
var pos = this._position;
var off = this.offset(pos);
var newpos = this._fixed ? pos : this.suggested(pos, off);
if (newpos) off = this.offset(pos = newpos);
var adj = this.adjust(pos, off);
this.replaceClass(pos);
this.el.css({ top: off.top + adj.top, left: off.left + adj.left });
if (adj.top || adj.left) {
var ael = this.arrowEl;
var ao = ael.offset();
var padj = this._padj;
ael.offset({ top: ao.top + padj.top - adj.top, left: ao.left + padj.left - adj.left });
this._padj = adj;
}
};
/**
* Compute the offset for `.el`, relative to `.target`,
* based on the given `pos`.
*
* @param {String} pos
* @return {Object}
* @api private
*/
Tip.prototype.offset = function(pos) {
var el = this.el;
var target = this.target;
var ew = el.outerWidth();
var eh = el.outerHeight();
var to = target.offset();
var tw = target.outerWidth();
var th = target.outerHeight();
switch (pos) {
case 'north':
return {
top: to.top - eh,
left: to.left + tw / 2 - ew / 2
}
case 'north west':
return {
top: to.top,
left: to.left - ew
}
case 'north east':
return {
top: to.top,
left: to.left + tw
}
case 'south':
return {
top: to.top + th,
left: to.left + tw / 2 - ew / 2
}
case 'south west':
return {
top: to.top + th - eh * .85,
left: to.left - ew
}
case 'south east':
return {
top: to.top + th - eh * .85,
left: to.left + tw
}
case 'east':
return {
top: to.top + th / 2 - eh / 2,
left: to.left + tw
}
case 'west':
return {
top: to.top + th / 2 - eh / 2,
left: to.left - ew
}
default:
throw new Error('invalid position "' + pos + '"');
}
};
Tip.prototype.adjust = function(pos, off) {
var el = this.el;
var ew = el.outerWidth();
var eh = el.outerHeight();
var win = render.$(window);
var top = win.scrollTop();
var left = win.scrollLeft();
var w = win.width();
var h = win.height();
// TODO: Implement support for adjusting other positions.
switch (pos) {
case 'south':
return {
top: 0,
left: off.left + ew > w ? 0 - (off.left + ew - w) - 12 : 0
}
default:
return {
top: 0,
left: 0
}
}
}
/**
* Compute the "suggested" position favouring `pos`.
* Returns undefined if no suggestion is made.
*
* @param {String} pos
* @param {Object} offset
* @return {String}
* @api private
*/
Tip.prototype.suggested = function(pos, off) {
var el = this.el;
var ew = el.outerWidth();
var eh = el.outerHeight();
var win = render.$(window);
var top = win.scrollTop();
var left = win.scrollLeft();
var w = win.width();
var h = win.height();
// too high
if (off.top < top) return 'south';
// too low
if (off.top + eh > top + h) return 'north';
// too far to the right
if (off.left + ew > left + w) return 'west';
// too far to the left
if (off.left < left) return 'east';
};
/**
* Replace position class `name`.
*
* @param {String} name
* @api private
*/
Tip.prototype.replaceClass = function(pos) {
pos = pos.split(' ').join('-');
this.el.attr('class', this.className + ' ' + pos + ' ' + (this._effect || ''));
};
return Tip;
});