1
1
'use strict' ;
2
2
3
+ function getHeaders ( element ) {
4
+ return element . querySelectorAll ( 'tr > th' ) ;
5
+ }
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
+
14
+ function colspan ( td ) {
15
+ var colspan = td . getAttributeNode ( 'colspan' ) ;
16
+ return colspan ? parseInt ( colspan . value ) : 1 ;
17
+ }
18
+
3
19
function wtResponsiveTable ( ) {
4
20
return {
5
21
restrict : 'A' ,
22
+ controller : function ( $element ) {
23
+ return {
24
+ getHeader : function ( td ) {
25
+ var headers = getHeaders ( $element [ 0 ] ) ;
26
+ if ( headers . length ) {
27
+ var row = td . parentElement ;
28
+ var headerIndex = 0 ;
29
+ var found = Array . prototype . some . call ( row . querySelectorAll ( 'td' ) , function ( value , index ) {
30
+ if ( value === td ) {
31
+ return true ;
32
+ }
33
+
34
+ headerIndex += colspan ( value ) ;
35
+ } ) ;
36
+
37
+ return found ? headers . item ( headerIndex ) : null ;
38
+ }
39
+ } ,
40
+ }
41
+ } ,
6
42
compile : function ( element , attrs ) {
7
43
attrs . $addClass ( 'responsive' ) ;
8
- var headers = element [ 0 ] . querySelectorAll ( 'tr > th' ) ;
44
+ var headers = getHeaders ( element [ 0 ] ) ;
9
45
if ( headers . length ) {
10
46
var rows = element [ 0 ] . querySelectorAll ( 'tbody > tr' ) ;
11
47
Array . prototype . forEach . call ( rows , function ( row ) {
12
48
var headerIndex = 0 ;
13
49
Array . prototype . forEach . call ( row . querySelectorAll ( 'td' ) , function ( value , index ) {
14
- var th = value . parentElement . querySelector ( 'th' ) || headers . item ( headerIndex ) ;
15
- var title = th && th . textContent ;
16
- if ( title && ! value . getAttributeNode ( 'data-title' ) ) {
17
- value . setAttribute ( 'data-title' , title ) ;
50
+ if ( ! value . getAttributeNode ( 'responsive-dynamic' ) ) {
51
+ var th = value . parentElement . querySelector ( 'th' ) || headers . item ( headerIndex ) ;
52
+ updateTitle ( value , th ) ;
18
53
}
19
-
20
- var colspan = value . getAttributeNode ( 'colspan' ) ;
21
- headerIndex += colspan ? parseInt ( colspan . value ) : 1 ;
54
+
55
+ headerIndex += colspan ( value ) ;
22
56
} ) ;
23
57
} ) ;
24
58
}
25
59
}
26
60
} ;
61
+ }
62
+
63
+ function wtResponsiveDynamic ( ) {
64
+ return {
65
+ restrict : 'A' ,
66
+ require : '^^wtResponsiveTable' ,
67
+ link : function ( scope , element , attrs , tableCtrl ) {
68
+ var td = element [ 0 ] ;
69
+ var th = tableCtrl . getHeader ( td ) ;
70
+ updateTitle ( td , th ) ;
71
+ }
72
+ } ;
27
73
}
0 commit comments