]>
git.immae.eu Git - github/wallabag/wallabag.git/blob - src/Wallabag/CoreBundle/Resources/public/themes/material/js/jquery.dotdotdot.js
2 * jQuery dotdotdot 1.8.3
4 * Copyright (c) Fred Heusschen
8 * dotdotdot.frebsite.nl
10 * Licensed under the MIT license.
11 * http://en.wikipedia.org/wiki/MIT_License
21 $. fn
. dotdotdot = function ( o
)
23 if ( this . length
== 0 )
25 $. fn
. dotdotdot
. debug ( 'No element found for "' + this . selector
+ '".' );
28 if ( this . length
> 1 )
33 $( this ). dotdotdot ( o
);
40 var orgContent
= $ dot
. contents ();
42 if ( $ dot
. data ( 'dotdotdot' ) )
44 $ dot
. trigger ( 'destroy.dot' );
47 $ dot
. data ( 'dotdotdot-style' , $ dot
. attr ( 'style' ) || '' );
48 $ dot
. css ( 'word-wrap' , 'break-word' );
49 if ($ dot
. css ( 'white-space' ) === 'nowrap' )
51 $ dot
. css ( 'white-space' , 'normal' );
54 $ dot
. bind_events = function ()
60 $ dot
. removeClass ( "is-truncated" );
64 switch ( typeof opts
. height
)
67 opts
. maxHeight
= opts
. height
;
71 opts
. maxHeight
= opts
. height
. call ( $ dot
[ 0 ] );
75 opts
. maxHeight
= getTrueInnerHeight ( $ dot
);
79 opts
. maxHeight
+= opts
. tolerance
;
81 if ( typeof c
!= 'undefined' )
83 if ( typeof c
== 'string' || ( 'nodeType' in c
&& c
. nodeType
=== 1 ) )
85 c
= $( '<div />' ). append ( c
). contents ();
93 $ inr
= $ dot
. wrapInner ( '<div class="dotdotdot" />' ). children ();
97 . append ( orgContent
. clone ( true ) )
99 . replaceWith ( ' <br /> ' )
112 if ( conf
. afterElement
)
114 after
= conf
. afterElement
. clone ( true );
116 conf
. afterElement
. detach ();
119 if ( test ( $ inr
, opts
) )
121 if ( opts
. wrap
== 'children' )
123 trunc
= children ( $ inr
, opts
, after
);
127 trunc
= ellipsis ( $ inr
, $ dot
, $ inr
, opts
, after
);
130 $ inr
. replaceWith ( $ inr
. contents () );
133 if ( $. isFunction ( opts
. callback
) )
135 opts
. callback
. call ( $ dot
[ 0 ], trunc
, orgContent
);
138 conf
. isTruncated
= trunc
;
149 if ( typeof fn
== 'function' )
151 fn
. call ( $ dot
[ 0 ], conf
. isTruncated
);
153 return conf
. isTruncated
;
157 'originalContent.dot' ,
163 if ( typeof fn
== 'function' )
165 fn
. call ( $ dot
[ 0 ], orgContent
);
182 . append ( orgContent
)
183 . attr ( 'style' , $ dot
. data ( 'dotdotdot-style' ) || '' )
184 . removeClass ( 'is-truncated' )
185 . data ( 'dotdotdot' , false );
191 $ dot
. unbind_events = function ()
197 $ dot
. watch = function ()
200 if ( opts
. watch
== 'window' )
202 var $ window
= $( window
),
203 _wWidth
= $ window
. width (),
204 _wHeight
= $ window
. height ();
207 'resize.dot' + conf
. dotId
,
210 if ( _wWidth
!= $ window
. width () || _wHeight
!= $ window
. height () || ! opts
. windowResizeFix
)
212 _wWidth
= $ window
. width ();
213 _wHeight
= $ window
. height ();
217 clearInterval ( watchInt
);
219 watchInt
= setTimeout (
222 $ dot
. trigger ( 'update.dot' );
231 watchOrg
= getSizes ( $ dot
);
232 watchInt
= setInterval (
235 if ( $ dot
. is ( ':visible' ) )
237 var watchNew
= getSizes ( $ dot
);
238 if ( watchOrg
. width
!= watchNew
. width
||
239 watchOrg
. height
!= watchNew
. height
)
241 $ dot
. trigger ( 'update.dot' );
250 $ dot
. unwatch = function ()
252 $( window
). unbind ( 'resize.dot' + conf
. dotId
);
255 clearInterval ( watchInt
);
260 var opts
= $. extend ( true , {}, $. fn
. dotdotdot
. defaults
, o
),
267 if ( !( opts
. lastCharacter
. remove
instanceof Array
) )
269 opts
. lastCharacter
. remove
= $. fn
. dotdotdot
. defaultArrays
. lastCharacter
. remove
;
271 if ( !( opts
. lastCharacter
. noEllipsis
instanceof Array
) )
273 opts
. lastCharacter
. noEllipsis
= $. fn
. dotdotdot
. defaultArrays
. lastCharacter
. noEllipsis
;
277 conf
. afterElement
= getElement ( opts
. after
, $ dot
);
278 conf
. isTruncated
= false ;
279 conf
. dotId
= dotId
++;
282 $ dot
. data ( 'dotdotdot' , true )
284 . trigger ( 'update.dot' );
296 $. fn
. dotdotdot
. defaults
= {
299 'fallbackToLetter' : true ,
300 'lastCharacter' : {},
306 'windowResizeFix' : true
308 $. fn
. dotdotdot
. defaultArrays
= {
310 'remove' : [ ' ' , ' \u3000 ' , ',' , ';' , '.' , '!' , '?' ],
314 $. fn
. dotdotdot
. debug = function ( msg
) {};
320 function children ( $ elem
, o
, after
)
322 var $ elements
= $ elem
. children (),
327 for ( var a
= 0 , l
= $ elements
. length
; a
< l
; a
++ )
329 var $ e
= $ elements
. eq ( a
);
333 $ elem
. append ( after
);
335 if ( test ( $ elem
, o
) )
351 function ellipsis ( $ elem
, $ d
, $ i
, o
, after
)
353 var isTruncated
= false ;
355 // Don't put the ellipsis directly inside these elements
356 var notx
= 'a, table, thead, tbody, tfoot, tr, col, colgroup, object, embed, param, ol, ul, dl, blockquote, select, optgroup, option, textarea, script, style' ;
358 // Don't remove these elements even if they are after the ellipsis
359 var noty
= 'script, .dotdotdot-keep' ;
371 if ( typeof e
== 'undefined' )
375 else if ( $ e
. is ( noty
) )
379 else if ( isTruncated
)
386 if ( after
&& !$ e
. is ( o
. after
) && !$ e
. find ( o
. after
). length
)
388 $ elem
[ $ elem
. is ( notx
) ? 'after' : 'append' ]( after
);
392 if ( e
. nodeType
== 3 ) // node is TEXT
394 isTruncated
= ellipsisElement ( $ e
, $ d
, $ i
, o
, after
);
398 isTruncated
= ellipsis ( $ e
, $ d
, $ i
, o
, after
);
412 $ d
. addClass ( "is-truncated" );
415 function ellipsisElement ( $ e
, $ d
, $ i
, o
, after
)
424 var txt
= getTextContent ( e
),
425 space
= ( txt
. indexOf ( ' ' ) !== - 1 ) ? ' ' : ' \u3000 ' ,
426 separator
= ( o
. wrap
== 'letter' ) ? '' : space
,
427 textArr
= txt
. split ( separator
),
431 endPos
= textArr
. length
- 1 ;
435 if ( o
. fallbackToLetter
&& startPos
== 0 && endPos
== 0 )
438 textArr
= txt
. split ( separator
);
439 endPos
= textArr
. length
- 1 ;
442 while ( startPos
<= endPos
&& !( startPos
== 0 && endPos
== 0 ) )
444 var m
= Math
. floor ( ( startPos
+ endPos
) / 2 );
451 setTextContent ( e
, textArr
. slice ( 0 , midPos
+ 1 ). join ( separator
) + o
. ellipsis
);
456 $( this ). toggle (). toggle ();
460 if ( ! test ( $ i
, o
) )
469 // Fallback to letter
470 if ( o
. fallbackToLetter
&& startPos
== 0 && endPos
== 0 )
473 textArr
= textArr
[ 0 ]. split ( separator
);
477 endPos
= textArr
. length
- 1 ;
482 if ( position
!= - 1 && !( textArr
. length
== 1 && textArr
[ 0 ]. length
== 0 ) )
484 txt
= addEllipsis ( textArr
. slice ( 0 , position
+ 1 ). join ( separator
), o
);
485 setTextContent ( e
, txt
);
489 var $ w
= $ e
. parent ();
492 var afterLength
= ( after
&& after
. closest ($ w
). length
) ? after
. length : 0 ;
494 if ( $ w
. contents (). length
> afterLength
)
496 e
= findLastTextNode ( $ w
. contents (). eq ( - 1 - afterLength
), $ d
);
500 e
= findLastTextNode ( $ w
, $ d
, true );
508 txt
= addEllipsis ( getTextContent ( e
), o
);
509 setTextContent ( e
, txt
);
510 if ( afterLength
&& after
)
512 var $ parent
= after
. parent ();
514 $( e
). parent (). append ( after
);
516 if ( !$. trim ( $ parent
. html () ) )
526 function test ( $ i
, o
)
528 return $ i
. innerHeight () > o
. maxHeight
;
530 function addEllipsis ( txt
, o
)
532 while ( $. inArray ( txt
. slice ( - 1 ), o
. lastCharacter
. remove
) > - 1 )
534 txt
= txt
. slice ( 0 , - 1 );
536 if ( $. inArray ( txt
. slice ( - 1 ), o
. lastCharacter
. noEllipsis
) < 0 )
542 function getSizes ( $ d
)
545 'width' : $ d
. innerWidth (),
546 'height' : $ d
. innerHeight ()
549 function setTextContent ( e
, content
)
553 e
. innerText
= content
;
555 else if ( e
. nodeValue
)
557 e
. nodeValue
= content
;
559 else if ( e
. textContent
)
561 e
. textContent
= content
;
565 function getTextContent ( e
)
571 else if ( e
. nodeValue
)
575 else if ( e
. textContent
)
577 return e
. textContent
;
584 function getPrevNode ( n
)
588 n
= n
. previousSibling
;
590 while ( n
&& n
. nodeType
!== 1 && n
. nodeType
!== 3 );
594 function findLastTextNode ( $ el
, $ top
, excludeCurrent
)
596 var e
= $ el
&& $ el
[ 0 ], p
;
599 if ( ! excludeCurrent
)
601 if ( e
. nodeType
=== 3 )
605 if ( $. trim ( $ el
. text () ) )
607 return findLastTextNode ( $ el
. contents (). last (), $ top
);
610 p
= getPrevNode ( e
);
614 if ( $ el
. is ( $ top
) || !$ el
. length
)
618 p
= getPrevNode ( $ el
[ 0 ] );
622 return findLastTextNode ( $( p
), $ top
);
627 function getElement ( e
, $ i
)
633 if ( typeof e
=== 'string' )
644 function getTrueInnerHeight ( $ el
)
646 var h
= $ el
. innerHeight (),
647 a
= [ 'paddingTop' , 'paddingBottom' ];
649 for ( var z
= 0 , l
= a
. length
; z
< l
; z
++ )
651 var m
= parseInt ( $ el
. css ( a
[ z
] ), 10 );
662 // override jQuery.html
663 var _orgHtml
= $. fn
. html
;
664 $. fn
. html = function ( str
)
666 if ( str
!= undef
&& !$. isFunction ( str
) && this . data ( 'dotdotdot' ) )
668 return this . trigger ( 'update' , [ str
] );
670 return _orgHtml
. apply ( this , arguments
);
674 // override jQuery.text
675 var _orgText
= $. fn
. text
;
676 $. fn
. text = function ( str
)
678 if ( str
!= undef
&& !$. isFunction ( str
) && this . data ( 'dotdotdot' ) )
680 str
= $( '<div />' ). text ( str
). html ();
681 return this . trigger ( 'update' , [ str
] );
683 return _orgText
. apply ( this , arguments
);
691 ## Automatic parsing for CSS classes
692 Contributed by [Ramil Valitov](https://github.com/rvalitov)
695 You can add one or several CSS classes to HTML elements to automatically invoke "jQuery.dotdotdot functionality" and some extra features. It allows to use jQuery.dotdotdot only by adding appropriate CSS classes without JS programming.
697 ### Available classes and their description
698 * dot-ellipsis - automatically invoke jQuery.dotdotdot to this element. This class must be included if you plan to use other classes below.
699 * dot-resize-update - automatically update if window resize event occurs. It's equivalent to option `watch:'window'`.
700 * dot-timer-update - automatically update if window resize event occurs. It's equivalent to option `watch:true`.
701 * dot-load-update - automatically update after the window has beem completely rendered. Can be useful if your content is generated dynamically using using JS and, hence, jQuery.dotdotdot can't correctly detect the height of the element before it's rendered completely.
702 * dot-height-XXX - available height of content area in pixels, where XXX is a number, e.g. can be `dot-height-35` if you want to set maximum height for 35 pixels. It's equivalent to option `height:'XXX'`.
705 *Adding jQuery.dotdotdot to element*
707 <div class="dot-ellipsis">
708 <p>Lorem Ipsum is simply dummy text.</p>
711 *Adding jQuery.dotdotdot to element with update on window resize*
713 <div class="dot-ellipsis dot-resize-update">
714 <p>Lorem Ipsum is simply dummy text.</p>
717 *Adding jQuery.dotdotdot to element with predefined height of 50px*
719 <div class="dot-ellipsis dot-height-50">
720 <p>Lorem Ipsum is simply dummy text.</p>
725 jQuery ( document
). ready ( function ($) {
726 //We only invoke jQuery.dotdotdot on elements that have dot-ellipsis class
727 $( ".dot-ellipsis" ). each ( function (){
728 //Checking if update on window resize required
729 var watch_window
=$( this ). hasClass ( "dot-resize-update" );
731 //Checking if update on timer required
732 var watch_timer
=$( this ). hasClass ( "dot-timer-update" );
734 //Checking if height set
736 var classList
= $( this ). attr ( 'class' ). split ( /\s+/ );
737 $. each ( classList
, function ( index
, item
) {
738 var matchResult
= item
. match ( /^dot-height-(\d+)$/ );
739 if ( matchResult
!== null )
740 height
= Number ( matchResult
[ 1 ]);
743 //Invoking jQuery.dotdotdot
744 var x
= new Object ();
751 $( this ). dotdotdot ( x
);
756 //Updating elements (if any) on window.load event
757 jQuery ( window
). on ( 'load' , function (){
758 jQuery ( ".dot-ellipsis.dot-load-update" ). trigger ( "update.dot" );