11// https://github.com/awerlang/angular-responsive-tables
22( function ( ) {
33 "use strict" ;
4+ function getHeaders ( element ) {
5+ return element . querySelectorAll ( "tr > th" ) ;
6+ }
7+ function updateTitle ( td , th ) {
8+ var title = th && th . textContent ;
9+ if ( title && ! td . getAttributeNode ( "data-title" ) ) {
10+ td . setAttribute ( "data-title" , title ) ;
11+ }
12+ }
13+ function colspan ( td ) {
14+ var colspan = td . getAttributeNode ( "colspan" ) ;
15+ return colspan ? parseInt ( colspan . value ) : 1 ;
16+ }
417 function wtResponsiveTable ( ) {
518 return {
619 restrict : "A" ,
20+ controller : function ( $element ) {
21+ return {
22+ getHeader : function ( td ) {
23+ var headers = getHeaders ( $element [ 0 ] ) ;
24+ if ( headers . length ) {
25+ var row = td . parentElement ;
26+ var headerIndex = 0 ;
27+ var found = Array . prototype . some . call ( row . querySelectorAll ( "td" ) , function ( value , index ) {
28+ if ( value === td ) {
29+ return true ;
30+ }
31+ headerIndex += colspan ( value ) ;
32+ } ) ;
33+ return found ? headers . item ( headerIndex ) : null ;
34+ }
35+ }
36+ } ;
37+ } ,
738 compile : function ( element , attrs ) {
8- attrs . $addClass ( "responsive" ) ;
9- var headers = element [ 0 ] . querySelectorAll ( "tr > th" ) ;
10- if ( headers . length ) {
11- var rows = element [ 0 ] . querySelectorAll ( "tbody > tr" ) ;
12- Array . prototype . forEach . call ( rows , function ( row ) {
13- var headerIndex = 0 ;
14- Array . prototype . forEach . call ( row . querySelectorAll ( "td" ) , function ( value , index ) {
15- var th = value . parentElement . querySelector ( "th" ) || headers . item ( headerIndex ) ;
16- var title = th . textContent ;
17- if ( title && ! value . getAttributeNode ( "data-title" ) ) {
18- value . setAttribute ( "data-title" , title ) ;
19- }
20- var colspan = value . getAttributeNode ( "colspan" ) ;
21- headerIndex += colspan ? parseInt ( colspan . value ) : 1 ;
39+ function apply ( ) {
40+ var headers = getHeaders ( element [ 0 ] ) ;
41+ if ( headers . length ) {
42+ var rows = element [ 0 ] . querySelectorAll ( "tbody > tr" ) ;
43+ Array . prototype . forEach . call ( rows , function ( row ) {
44+ var headerIndex = 0 ;
45+ Array . prototype . forEach . call ( row . querySelectorAll ( "td" ) , function ( value , index ) {
46+ if ( ! value . getAttributeNode ( "responsive-dynamic" ) ) {
47+ var th = value . parentElement . querySelector ( "th" ) || headers . item ( headerIndex ) ;
48+ updateTitle ( value , th ) ;
49+ }
50+ headerIndex += colspan ( value ) ;
51+ } ) ;
2252 } ) ;
23- } ) ;
53+ }
54+ }
55+ attrs . $addClass ( "responsive" ) ;
56+ if ( Array . prototype . some . call ( element . find ( "th" ) , function ( it ) {
57+ return it . getAttributeNode ( "ng-repeat" ) || it . getAttributeNode ( "data-ng-repeat" ) ;
58+ } ) ) {
59+ setTimeout ( function ( ) {
60+ apply ( ) ;
61+ } , 0 ) ;
62+ } else {
63+ apply ( ) ;
2464 }
2565 }
2666 } ;
2767 }
68+ function wtResponsiveDynamic ( ) {
69+ return {
70+ restrict : "A" ,
71+ require : "^^wtResponsiveTable" ,
72+ link : function ( scope , element , attrs , tableCtrl ) {
73+ var td = element [ 0 ] ;
74+ var th = tableCtrl . getHeader ( td ) ;
75+ updateTitle ( td , th ) ;
76+ }
77+ } ;
78+ }
2879 "use strict" ;
29- angular . module ( "wt.responsive" , [ ] ) . directive ( "wtResponsiveTable" , [ wtResponsiveTable ] ) ;
80+ angular . module ( "wt.responsive" , [ ] ) . directive ( "wtResponsiveTable" , [ wtResponsiveTable ] ) . directive ( "responsiveDynamic" , [ wtResponsiveDynamic ] ) ;
3081} ) ( ) ;
0 commit comments