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