]>
Commit | Line | Data |
---|---|---|
28388e15 | 1 | /* |
53858785 | 2 | * jQuery tinydot 0.2.0 |
28388e15 | 3 | * |
53858785 AD |
4 | * Copyright (c) Alexander Danilov |
5 | * modos189.ru | |
28388e15 | 6 | * |
53858785 AD |
7 | * Plugin website: |
8 | * tinydot.modos189.ru | |
28388e15 | 9 | * |
53858785 AD |
10 | * Licensed under the MIT license. |
11 | * http://en.wikipedia.org/wiki/MIT_License | |
28388e15 AD |
12 | */ |
13 | ||
14 | (function( $, undef ) | |
15 | { | |
16 | if ( $.fn.tinydot ) | |
17 | { | |
18 | return; | |
19 | } | |
20 | ||
21 | $.fn.tinydot = function( o ) { | |
53858785 AD |
22 | |
23 | var $dot = this; | |
24 | $dot.child = getChildOrDie($dot); | |
25 | $dot.orgContent = $($dot.child).html(); | |
26 | ellipsis( $dot ); | |
27 | ||
28 | $dot.watch = function() | |
29 | { | |
30 | $(window).on('resize', function(){ | |
31 | if ( watchInt ) | |
32 | { | |
33 | clearInterval( watchInt ); | |
34 | } | |
35 | watchInt = setTimeout( | |
36 | function() | |
37 | { | |
38 | reinitialize($dot); | |
39 | }, 100 | |
40 | ); | |
28388e15 | 41 | }); |
53858785 AD |
42 | |
43 | return $dot; | |
44 | }; | |
45 | ||
46 | var opts = $.extend( true, {}, $.fn.tinydot.defaults, o ), | |
47 | watchInt = null; | |
48 | ||
49 | if ( opts.watch ) | |
50 | { | |
51 | $dot.watch(); | |
28388e15 AD |
52 | } |
53 | } | |
53858785 AD |
54 | |
55 | // public | |
56 | $.fn.tinydot.defaults = { | |
57 | 'watch' : false | |
58 | }; | |
59 | ||
60 | function getChildOrDie( $elem ) | |
61 | { | |
62 | var childrens = $elem.children(); | |
63 | if (childrens.length == 0) { | |
64 | // create children | |
65 | var data = $($elem).html(); | |
66 | $elem.html(''); | |
67 | $elem.append('<span />'); | |
68 | return $elem.children('span').html(data); | |
69 | } else { | |
70 | return childrens[0]; | |
71 | } | |
72 | } | |
73 | ||
74 | function reinitialize( $elem ) | |
75 | { | |
76 | $($elem.child).html($elem.orgContent); | |
77 | ellipsis( $elem ); | |
78 | } | |
79 | ||
80 | function ellipsis( $elem ) { | |
81 | var divh=$($elem).height(); | |
82 | while ($($elem.child).outerHeight()>divh) { | |
83 | $($elem.child).html(function (index, html) { | |
84 | return html.replace(/\W*\s(\S)*$/, '...'); | |
85 | }); | |
86 | } | |
87 | } | |
88 | ||
28388e15 AD |
89 | })( jQuery ); |
90 | ||
91 | jQuery(document).ready(function($) { | |
92 | //We only invoke jQuery.tinydot on elements that have dot-ellipsis class | |
93 | $(".dot-ellipsis").each(function(){ | |
53858785 AD |
94 | //Checking if update on window resize required |
95 | var watch_window=$(this).hasClass("dot-resize-update"); | |
96 | ||
97 | //Invoking jQuery.tinydot | |
98 | var x = new Object(); | |
99 | if (watch_window) | |
100 | x.watch='window'; | |
101 | $(this).tinydot(x); | |
28388e15 | 102 | }); |
28388e15 | 103 | }); |