1
+ /* W3Data ver 1.3 by W3Schools.com */
2
+ var w3DataObject = { } ;
3
+ function w3DisplayData ( id , data ) {
4
+ var htmlObj , htmlTemplate , html , arr = [ ] , a , l , rowClone , x , j , i , ii , cc , repeat , repeatObj , repeatX = "" ;
5
+ htmlObj = document . getElementById ( id ) ;
6
+ htmlTemplate = w3InitTemplate ( id , htmlObj ) ;
7
+ html = htmlTemplate . cloneNode ( true ) ;
8
+ arr = w3GetElementsByAttribute ( html , "w3-repeat" ) ;
9
+ l = arr . length ;
10
+ for ( j = ( l - 1 ) ; j >= 0 ; j -= 1 ) {
11
+ cc = arr [ j ] . getAttribute ( "w3-repeat" ) . split ( " " ) ;
12
+ if ( cc . length == 1 ) {
13
+ repeat = cc [ 0 ] ;
14
+ } else {
15
+ repeatX = cc [ 0 ] ;
16
+ repeat = cc [ 2 ] ;
17
+ }
18
+ arr [ j ] . removeAttribute ( "w3-repeat" ) ;
19
+ repeatObj = data [ repeat ] ;
20
+ if ( repeatObj && typeof repeatObj == "object" && repeatObj . length != "undefined" ) {
21
+ i = 0 ;
22
+ for ( x in repeatObj ) {
23
+ i += 1 ;
24
+ rowClone = arr [ j ] ;
25
+ rowClone = w3NeedleInHaystack ( rowClone , "element" , repeatX , repeatObj [ x ] ) ;
26
+ a = rowClone . attributes ;
27
+ for ( ii = 0 ; ii < a . length ; ii += 1 ) {
28
+ a [ ii ] . value = w3NeedleInHaystack ( a [ ii ] , "attribute" , repeatX , repeatObj [ x ] ) . value ;
29
+ }
30
+ ( i === repeatObj . length ) ? arr [ j ] . parentNode . replaceChild ( rowClone , arr [ j ] ) : arr [ j ] . parentNode . insertBefore ( rowClone , arr [ j ] ) ;
31
+ }
32
+ } else {
33
+ console . log ( "w3-repeat must be an array. " + repeat + " is not an array." ) ;
34
+ continue ;
35
+ }
36
+ }
37
+ html = w3NeedleInHaystack ( html , "element" ) ;
38
+ htmlObj . parentNode . replaceChild ( html , htmlObj ) ;
39
+ function w3InitTemplate ( id , obj ) {
40
+ var template ;
41
+ template = obj . cloneNode ( true ) ;
42
+ if ( w3DataObject . hasOwnProperty ( id ) ) { return w3DataObject [ id ] ; }
43
+ w3DataObject [ id ] = template ;
44
+ return template ;
45
+ }
46
+ function w3GetElementsByAttribute ( x , att ) {
47
+ var arr = [ ] , arrCount = - 1 , i , l , y = x . getElementsByTagName ( "*" ) , z = att . toUpperCase ( ) ;
48
+ l = y . length ;
49
+ for ( i = - 1 ; i < l ; i += 1 ) {
50
+ if ( i == - 1 ) { y [ i ] = x ; }
51
+ if ( y [ i ] . getAttribute ( z ) !== null ) { arrCount += 1 ; arr [ arrCount ] = y [ i ] ; }
52
+ }
53
+ return arr ;
54
+ }
55
+ function w3NeedleInHaystack ( elmnt , typ , repeatX , x ) {
56
+ var value , rowClone , pos1 , haystack , pos2 , needle = [ ] , needleToReplace , i , cc , r ;
57
+ rowClone = elmnt . cloneNode ( true ) ;
58
+ pos1 = 0 ;
59
+ while ( pos1 > - 1 ) {
60
+ haystack = ( typ == "attribute" ) ? rowClone . value : rowClone . innerHTML ;
61
+ pos1 = haystack . indexOf ( "{{" , pos1 ) ;
62
+ if ( pos1 === - 1 ) { break ; }
63
+ pos2 = haystack . indexOf ( "}}" , pos1 + 1 ) ;
64
+ needleToReplace = haystack . substring ( pos1 + 2 , pos2 ) ;
65
+ needle = needleToReplace . split ( "||" ) ;
66
+ value = undefined ;
67
+ for ( i = 0 ; i < needle . length ; i += 1 ) {
68
+ needle [ i ] = needle [ i ] . replace ( / ^ \s + | \s + $ / gm, '' ) ; //trim
69
+ //value = ((x && x[needle[i]]) || (data && data[needle[i]]));
70
+ if ( x ) { value = x [ needle [ i ] ] ; }
71
+ if ( value == undefined && data ) { value = data [ needle [ i ] ] ; }
72
+ if ( value == undefined ) {
73
+ cc = needle [ i ] . split ( "." ) ;
74
+ if ( cc [ 0 ] == repeatX ) { value = x [ cc [ 1 ] ] ; }
75
+ }
76
+ if ( value == undefined ) {
77
+ if ( needle [ i ] == repeatX ) { value = x ; }
78
+ }
79
+ if ( value == undefined ) {
80
+ if ( needle [ i ] . substr ( 0 , 1 ) == '"' ) {
81
+ value = needle [ i ] . replace ( / " / g, "" ) ;
82
+ } else if ( needle [ i ] . substr ( 0 , 1 ) == "'" ) {
83
+ value = needle [ i ] . replace ( / ' / g, "" ) ;
84
+ }
85
+ }
86
+ if ( value != undefined ) { break ; }
87
+ }
88
+ if ( value != undefined ) {
89
+ r = "{{" + needleToReplace + "}}" ;
90
+ if ( typ == "attribute" ) {
91
+ rowClone . value = rowClone . value . replace ( r , value ) ;
92
+ } else {
93
+ w3ReplaceHTML ( rowClone , r , value ) ;
94
+ }
95
+ }
96
+ pos1 = pos1 + 1 ;
97
+ }
98
+ return rowClone ;
99
+ }
100
+ function w3ReplaceHTML ( a , r , result ) {
101
+ var b , l , i , a , x , y , j , ll , nam ;
102
+ if ( a . hasAttributes ( ) ) {
103
+ b = a . attributes ;
104
+ l = b . length ;
105
+ for ( i = 0 ; i < l ; i += 1 ) {
106
+ if ( b [ i ] . value . indexOf ( r ) > - 1 ) { b [ i ] . value = b [ i ] . value . replace ( r , result ) ; }
107
+ }
108
+ }
109
+ x = a . getElementsByTagName ( "*" ) ;
110
+ l = x . length ;
111
+ a . innerHTML = a . innerHTML . replace ( r , result ) ;
112
+ }
113
+ }
114
+ function w3IncludeHTML ( ) {
115
+ var z , i , elmnt , a , file , xhttp ;
116
+ z = document . getElementsByTagName ( "*" ) ;
117
+ for ( i = 0 ; i < z . length ; i ++ ) {
118
+ if ( z [ i ] . getAttribute ( "w3-include-html" ) ) {
119
+ elmnt = z [ i ] ;
120
+ break ;
121
+ }
122
+ }
123
+ if ( elmnt ) {
124
+ a = z [ i ] . cloneNode ( false ) ;
125
+ file = elmnt . getAttribute ( "w3-include-html" ) ;
126
+ xhttp = new XMLHttpRequest ( ) ;
127
+ xhttp . onreadystatechange = function ( ) {
128
+ if ( this . readyState == 4 && this . status == 200 ) {
129
+ a . removeAttribute ( "w3-include-html" ) ;
130
+ a . innerHTML = this . responseText ;
131
+ elmnt . parentNode . replaceChild ( a , elmnt ) ;
132
+ w3IncludeHTML ( ) ;
133
+ }
134
+ }
135
+ xhttp . open ( "GET" , file , true ) ;
136
+ xhttp . send ( ) ;
137
+ }
138
+ }
139
+ function w3Http ( target , readyfunc , xml , method ) {
140
+ var httpObj ;
141
+ if ( ! method ) { method = "GET" ; }
142
+ if ( window . XMLHttpRequest ) {
143
+ httpObj = new XMLHttpRequest ( ) ;
144
+ } else if ( window . ActiveXObject ) {
145
+ httpObj = new ActiveXObject ( "Microsoft.XMLHTTP" ) ;
146
+ }
147
+ if ( httpObj ) {
148
+ if ( readyfunc ) { httpObj . onreadystatechange = readyfunc ; }
149
+ httpObj . open ( method , target , true ) ;
150
+ httpObj . send ( xml ) ;
151
+ }
152
+ }
0 commit comments