1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 | var Emitter = require('emitter')
|
7 | , o = require('jquery');
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 | module.exports = Tip;
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 | function tip(el, options) {
|
30 | options = options || {};
|
31 | var delay = options.delay || 0;
|
32 |
|
33 | o(el).each(function(i, el){
|
34 | el = o(el);
|
35 | var val = options.value || el.attr('title');
|
36 | var tip = new Tip(val);
|
37 | el.attr('title', '');
|
38 | tip.cancelHideOnHover(delay);
|
39 | tip.attach(el, delay);
|
40 | });
|
41 | }
|
42 |
|
43 |
|
44 |
|
45 |
|
46 |
|
47 |
|
48 |
|
49 |
|
50 | function Tip(content, options) {
|
51 | if (!(this instanceof Tip)) return tip(content, options);
|
52 | Emitter.call(this);
|
53 | this.classname = '';
|
54 | this._content = content;
|
55 | this.el = o(require('./template'));
|
56 | this.inner = this.el.find('.tip-inner');
|
57 | this.position('north');
|
58 | if (Tip.effect) this.effect(Tip.effect);
|
59 | }
|
60 |
|
61 |
|
62 |
|
63 |
|
64 |
|
65 | Tip.prototype.__proto__ = Emitter.prototype;
|
66 |
|
67 |
|
68 |
|
69 |
|
70 |
|
71 |
|
72 |
|
73 |
|
74 |
|
75 |
|
76 | Tip.prototype.attach = function(el, delay){
|
77 | el.hover(
|
78 | this.show.bind(this, el),
|
79 | this.hide.bind(this, delay || 0));
|
80 | return this;
|
81 | };
|
82 |
|
83 |
|
84 |
|
85 |
|
86 |
|
87 |
|
88 |
|
89 |
|
90 |
|
91 | Tip.prototype.cancelHideOnHover = function(delay){
|
92 | this.el.hover(
|
93 | this.cancelHide.bind(this),
|
94 | this.hide.bind(this, delay || 0));
|
95 | return this;
|
96 | };
|
97 |
|
98 |
|
99 |
|
100 |
|
101 |
|
102 |
|
103 |
|
104 |
|
105 |
|
106 | Tip.prototype.effect = function(type){
|
107 | this._effect = type;
|
108 | this.el.addClass(type);
|
109 | return this;
|
110 | };
|
111 |
|
112 |
|
113 |
|
114 |
|
115 |
|
116 |
|
117 |
|
118 |
|
119 |
|
120 |
|
121 |
|
122 |
|
123 |
|
124 |
|
125 |
|
126 |
|
127 |
|
128 |
|
129 | Tip.prototype.position = function(type){
|
130 | this._position = type;
|
131 | return this;
|
132 | };
|
133 |
|
134 |
|
135 |
|
136 |
|
137 |
|
138 |
|
139 |
|
140 |
|
141 |
|
142 |
|
143 |
|
144 | Tip.prototype.show = function(el){
|
145 | if (!el) throw new Error('.show() element required');
|
146 | this.target = o(el);
|
147 | this.inner.empty().append(this._content);
|
148 | this.el.appendTo('body');
|
149 | this.el.addClass('tip-' + this._position);
|
150 | this.reposition();
|
151 | this.el.removeClass('tip-hide');
|
152 | this.emit('show', this.target);
|
153 | this._reposition = this.reposition.bind(this);
|
154 | o(window).bind('resize', this._reposition);
|
155 | o(window).bind('scroll', this._reposition);
|
156 | return this;
|
157 | };
|
158 |
|
159 |
|
160 |
|
161 |
|
162 |
|
163 |
|
164 |
|
165 | Tip.prototype.reposition = function(){
|
166 | var pos = this._position;
|
167 | var off = this.offset(pos);
|
168 | var newpos = this.suggested(pos, off);
|
169 | if (newpos) off = this.offset(pos = newpos);
|
170 | this.replaceClass(pos);
|
171 | this.el.css(off);
|
172 | };
|
173 |
|
174 |
|
175 |
|
176 |
|
177 |
|
178 |
|
179 |
|
180 |
|
181 |
|
182 |
|
183 |
|
184 | Tip.prototype.suggested = function(pos, off){
|
185 | var el = this.el;
|
186 |
|
187 | var ew = el.outerWidth();
|
188 | var eh = el.outerHeight();
|
189 |
|
190 | var win = o(window);
|
191 | var top = win.scrollTop();
|
192 | var left = win.scrollLeft();
|
193 | var w = win.width();
|
194 | var h = win.height();
|
195 |
|
196 |
|
197 | if (off.top < top) return 'south';
|
198 |
|
199 |
|
200 | if (off.top + eh > top + h) return 'north';
|
201 |
|
202 |
|
203 | if (off.left + ew > left + w) return 'west';
|
204 |
|
205 |
|
206 | if (off.left < left) return 'east';
|
207 | };
|
208 |
|
209 |
|
210 |
|
211 |
|
212 |
|
213 |
|
214 |
|
215 |
|
216 | Tip.prototype.replaceClass = function(name){
|
217 | name = name.split(' ').join('-');
|
218 | this.el.attr('class', this.classname + ' tip tip-' + name + ' ' + this._effect);
|
219 | };
|
220 |
|
221 |
|
222 |
|
223 |
|
224 |
|
225 |
|
226 |
|
227 |
|
228 |
|
229 |
|
230 | Tip.prototype.offset = function(pos){
|
231 | var el = this.el;
|
232 | var target = this.target;
|
233 |
|
234 | var ew = el.outerWidth();
|
235 | var eh = el.outerHeight();
|
236 |
|
237 | var to = target.offset();
|
238 | var tw = target.outerWidth();
|
239 | var th = target.outerHeight();
|
240 |
|
241 | switch (pos) {
|
242 | case 'north':
|
243 | return {
|
244 | top: to.top - eh,
|
245 | left: to.left + tw / 2 - ew / 2
|
246 | }
|
247 | case 'north west':
|
248 | return {
|
249 | top: to.top,
|
250 | left: to.left - ew
|
251 | }
|
252 | case 'north east':
|
253 | return {
|
254 | top: to.top,
|
255 | left: to.left + tw
|
256 | }
|
257 | case 'south':
|
258 | return {
|
259 | top: to.top + th,
|
260 | left: to.left + tw / 2 - ew / 2
|
261 | }
|
262 | case 'south west':
|
263 | return {
|
264 | top: to.top + th - eh * .85,
|
265 | left: to.left - ew
|
266 | }
|
267 | case 'south east':
|
268 | return {
|
269 | top: to.top + th - eh * .85,
|
270 | left: to.left + tw
|
271 | }
|
272 | case 'east':
|
273 | return {
|
274 | top: to.top + th / 2 - eh / 2,
|
275 | left: to.left + tw
|
276 | }
|
277 | case 'west':
|
278 | return {
|
279 | top: to.top + th / 2 - eh / 2,
|
280 | left: to.left - ew
|
281 | }
|
282 | default:
|
283 | throw new Error('invalid position "' + pos + '"');
|
284 | }
|
285 | };
|
286 |
|
287 |
|
288 |
|
289 |
|
290 |
|
291 |
|
292 |
|
293 | Tip.prototype.cancelHide = function(){
|
294 | clearTimeout(this._hide);
|
295 | };
|
296 |
|
297 |
|
298 |
|
299 |
|
300 |
|
301 |
|
302 |
|
303 |
|
304 |
|
305 |
|
306 |
|
307 | Tip.prototype.hide = function(ms){
|
308 | var self = this;
|
309 |
|
310 |
|
311 | if (ms) {
|
312 | this._hide = setTimeout(this.hide.bind(this), ms);
|
313 | return this;
|
314 | }
|
315 |
|
316 |
|
317 | this.el.addClass('tip-hide');
|
318 | if (this._effect) {
|
319 | setTimeout(this.remove.bind(this), 300);
|
320 | } else {
|
321 | self.remove();
|
322 | }
|
323 |
|
324 | return this;
|
325 | };
|
326 |
|
327 |
|
328 |
|
329 |
|
330 |
|
331 |
|
332 |
|
333 |
|
334 | Tip.prototype.remove = function(){
|
335 | o(window).unbind('resize', this._reposition);
|
336 | o(window).unbind('scroll', this._reposition);
|
337 | this.emit('hide');
|
338 | this.el.detach();
|
339 | return this;
|
340 | };
|