summaryrefslogtreecommitdiff
path: root/public/assets/site.js
blob: c414e2dd5df8e5a583ebf81a2038df319c760f69 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
var isIphone = !! (navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPod/i))
var isIpad = !! (navigator.userAgent.match(/iPad/i))
var isAndroid = !! (navigator.userAgent.match(/Android/i))
var isMobile = isIphone || isIpad || isAndroid
var isDesktop = ! isMobile

document.body.classList.add(isDesktop ? 'desktop' : 'mobile')

$(function(){
  $.getJSON('/db.json', function(data){
    site = data
    var entries = site.entries = {}
    site.entry.forEach(function(entry){
      entries[entry.id] = entry
      entry.tags = entry.tags.split('#').map(function(tag){
        return tag.trim()
      })
    })
    ready()
  });
  (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
  (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
  m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
  })(window,document,'script','https://www.google-analytics.com/analytics.js','ga');

  ga('create', 'UA-35821710-3', 'auto');
  ga('send', 'pageview');
})

function ready() {
  $('.readmore').click( function(){
    var id = $(this).data().id
    var entry = site.entries[id]
    show_entry(entry)
  })
  $('.close').click( function(){
    close_modals()
  })
  $('.page, #entry').click( function(){
    close_modals()
  })
  $('.inner').click( function(e){
    e.stopPropagation()
  })
  $('.tag').click( function(){
    var tag = $(this).html()
    filter_by_tag(tag)
  })
  new Flickity( "#testimonials", {
    cellSelector: '.testimonial',
    wrapAround: true,
    prevNextButtons: false,
    pageDots: false,
    setGallerySize: false,
    draggable: true,
    autoPlay: 5000,
  })
  
  postGallery = new Flickity( "#posts", {
    cellSelector: '.cell',
    wrapAround: true,
    prevNextButtons: false,
    pageDots: false,
    setGallerySize: false,
    draggable: true,
    autoPlay: 0,
  })
  $("#left").click(() => { postGallery.previous() })
  $("#right").click(() => { postGallery.next() })
}

function show_entry(entry){
  console.log('showing', entry)
  $('#entry h3').html(entry.title)
  $('#entry .media').empty()
  entry.media.forEach( (media) => {
    console.log(media)
    var dom;
    switch (media.type) {
      case 'youtube':
        dom = $('<iframe>').attr({
          src: 'https://www.youtube.com/embed/' + media.token + '?modestbranding=1&rel=0',
          width: media.width,
          height: media.height,
          frameborder: 0,
          allowfullscreen: 'allowfullscreen',
        })
        break
      case 'image':
        dom = new Image
        dom.src = media.url
        break
    }
    dom && $('#entry .media').append(dom)
  })
  $('#entry .content').html(newlineToBr(entry.body))
  $('#entry .comments').html( '<div class="fb-comments" data-href="https://howtomakeafolkestone.life/entry/' + entry.id +'" data-width="360"></div>' )
  FB && FB.XFBML.parse( $('#entry .comments').get(0) )
  $('body').addClass('entry')
}
function hide_entry(){
  setTimeout(clear_entry, 500)
}
function clear_entry(){
}
function close_modals(){
  $('body').removeClass('entry').removeClass('about')
}

function newlineToBr(s){ return s.replace(/\n/g,'<br>') }