1
1
// Generating content based on the template
2
- var template = " <article>\n\
3
- <img src='data/img/placeholder.png' data-src='data/img/SLUG.jpg' alt='NAME'>\n\
4
- <h3>#POS. NAME</h3>\n\
5
- <ul>\n\
6
- <li><span>Author:</span> <strong>AUTHOR</strong></li>\n\
7
- <li><span>Twitter:</span> <a href='https://twitter.com/TWITTER'>@TWITTER</a></li>\n\
8
- <li><span>Website:</span> <a href='http://WEBSITE/'>WEBSITE</a></li>\n\
9
- <li><span>GitHub:</span> <a href='https://GITHUB'>GITHUB</a></li>\n\
10
- <li><span>More:</span> <a href='http://js13kgames.com/entries/SLUG'>js13kgames.com/entries/SLUG</a></li>\n\
11
- </ul>\n\
12
- </article>" ;
13
- var content = '' ;
14
- for ( var i = 0 ; i < games . length ; i ++ ) {
15
- var entry = template . replace ( / P O S / g, ( i + 1 ) )
16
- . replace ( / S L U G / g, games [ i ] . slug )
17
- . replace ( / N A M E / g, games [ i ] . name )
18
- . replace ( / A U T H O R / g, games [ i ] . author )
19
- . replace ( / T W I T T E R / g, games [ i ] . twitter )
20
- . replace ( / W E B S I T E / g, games [ i ] . website )
21
- . replace ( / G I T H U B / g, games [ i ] . github ) ;
22
- entry = entry . replace ( '<a href=\'http:///\'></a>' , '-' ) ;
23
- content += entry ;
24
- } ;
2
+ const template = ` <article>
3
+ <img src='data/img/placeholder.png' data-src='data/img/SLUG.jpg' alt='NAME'>
4
+ <h3>#POS. NAME</h3>
5
+ <ul>
6
+ <li><span>Author:</span> <strong>AUTHOR</strong></li>
7
+ <li><span>Twitter:</span> <a href='https://twitter.com/TWITTER'>@TWITTER</a></li>
8
+ <li><span>Website:</span> <a href='http://WEBSITE/'>WEBSITE</a></li>
9
+ <li><span>GitHub:</span> <a href='https://GITHUB'>GITHUB</a></li>
10
+ <li><span>More:</span> <a href='http://js13kgames.com/entries/SLUG'>js13kgames.com/entries/SLUG</a></li>
11
+ </ul>
12
+ </article>` ;
13
+ let content = '' ;
14
+ for ( let i = 0 ; i < games . length ; i ++ ) {
15
+ let entry = template . replace ( / P O S / g, ( i + 1 ) )
16
+ . replace ( / S L U G / g, games [ i ] . slug )
17
+ . replace ( / N A M E / g, games [ i ] . name )
18
+ . replace ( / A U T H O R / g, games [ i ] . author )
19
+ . replace ( / T W I T T E R / g, games [ i ] . twitter )
20
+ . replace ( / W E B S I T E / g, games [ i ] . website )
21
+ . replace ( / G I T H U B / g, games [ i ] . github ) ;
22
+ entry = entry . replace ( '<a href=\'http:///\'></a>' , '-' ) ;
23
+ content += entry ;
24
+ }
25
25
document . getElementById ( 'content' ) . innerHTML = content ;
26
26
27
27
// Registering Service Worker
28
- if ( 'serviceWorker' in navigator ) {
29
- navigator . serviceWorker . register ( '/pwa-examples/js13kpwa/sw.js' ) ;
30
- } ;
28
+ if ( 'serviceWorker' in navigator ) {
29
+ navigator . serviceWorker . register ( '/pwa-examples/js13kpwa/sw.js' ) ;
30
+ }
31
31
32
32
// Requesting permission for Notifications after clicking on the button
33
- var button = document . getElementById ( " notifications" ) ;
34
- button . addEventListener ( 'click' , function ( e ) {
35
- Notification . requestPermission ( ) . then ( function ( result ) {
36
- if ( result === 'granted' ) {
37
- randomNotification ( ) ;
38
- }
39
- } ) ;
33
+ const button = document . getElementById ( ' notifications' ) ;
34
+ button . addEventListener ( 'click' , ( ) => {
35
+ Notification . requestPermission ( ) . then ( ( result ) => {
36
+ if ( result === 'granted' ) {
37
+ randomNotification ( ) ;
38
+ }
39
+ } ) ;
40
40
} ) ;
41
41
42
42
// Setting up random Notification
43
43
function randomNotification ( ) {
44
- var randomItem = Math . floor ( Math . random ( ) * games . length ) ;
45
- var notifTitle = games [ randomItem ] . name ;
46
- var notifBody = ' Created by ' + games [ randomItem ] . author + '.' ;
47
- var notifImg = ' data/img/' + games [ randomItem ] . slug + ' .jpg' ;
48
- var options = {
49
- body : notifBody ,
50
- icon : notifImg
51
- }
52
- var notif = new Notification ( notifTitle , options ) ;
53
- setTimeout ( randomNotification , 30000 ) ;
54
- } ;
44
+ const randomItem = Math . floor ( Math . random ( ) * games . length ) ;
45
+ const notifTitle = games [ randomItem ] . name ;
46
+ const notifBody = ` Created by ${ games [ randomItem ] . author } .` ;
47
+ const notifImg = ` data/img/${ games [ randomItem ] . slug } .jpg` ;
48
+ const options = {
49
+ body : notifBody ,
50
+ icon : notifImg ,
51
+ } ;
52
+ new Notification ( notifTitle , options ) ;
53
+ setTimeout ( randomNotification , 30000 ) ;
54
+ }
55
55
56
56
// Progressive loading images
57
- var imagesToLoad = document . querySelectorAll ( 'img[data-src]' ) ;
58
- var loadImages = function ( image ) {
59
- image . setAttribute ( 'src' , image . getAttribute ( 'data-src' ) ) ;
60
- image . onload = function ( ) {
61
- image . removeAttribute ( 'data-src' ) ;
62
- } ;
57
+ const imagesToLoad = document . querySelectorAll ( 'img[data-src]' ) ;
58
+ const loadImages = ( image ) => {
59
+ image . setAttribute ( 'src' , image . getAttribute ( 'data-src' ) ) ;
60
+ image . onload = ( ) => {
61
+ image . removeAttribute ( 'data-src' ) ;
62
+ } ;
63
63
} ;
64
- if ( 'IntersectionObserver' in window ) {
65
- var observer = new IntersectionObserver ( function ( items , observer ) {
66
- items . forEach ( function ( item ) {
67
- if ( item . isIntersecting ) {
68
- loadImages ( item . target ) ;
69
- observer . unobserve ( item . target ) ;
70
- }
71
- } ) ;
72
- } ) ;
73
- imagesToLoad . forEach ( function ( img ) {
74
- observer . observe ( img ) ;
75
- } ) ;
64
+ if ( 'IntersectionObserver' in window ) {
65
+ const observer = new IntersectionObserver ( ( items ) => {
66
+ items . forEach ( ( item ) => {
67
+ if ( item . isIntersecting ) {
68
+ loadImages ( item . target ) ;
69
+ observer . unobserve ( item . target ) ;
70
+ }
71
+ } ) ;
72
+ } ) ;
73
+ imagesToLoad . forEach ( ( img ) => {
74
+ observer . observe ( img ) ;
75
+ } ) ;
76
+ } else {
77
+ imagesToLoad . forEach ( ( img ) => {
78
+ loadImages ( img ) ;
79
+ } ) ;
76
80
}
77
- else {
78
- imagesToLoad . forEach ( function ( img ) {
79
- loadImages ( img ) ;
80
- } ) ;
81
- }
0 commit comments