aboutsummaryrefslogtreecommitdiffstats
path: root/syte-pelican/static/js/components/blog-posts.js
blob: 27f8ab53775d7c72c32d0bc6ef992f21bc46f218 (plain) (blame)
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143

function fetchBlogPosts(post, tag) {
  var blog_fetch_url = '/blog.json';

  if (post)
      blog_fetch_url = '/post/' + post;
  else if (tag)
      blog_fetch_url = '/tags/' + tag;

  $.getJSON(blog_fetch_url, function(blog_posts) {
      require(["text!templates/blog-post-text.html",
              "text!templates/blog-post-photo.html",
              "text!templates/blog-post-link.html",
              "text!templates/blog-post-video.html",
              "text!templates/blog-post-audio.html",
              "text!templates/blog-post-quote.html"],

         function(text_post_template, photo_post_template, 
                  link_post_template, video_post_template, audio_post_template,
                  quote_post_template) {

            var text_template = Handlebars.compile(text_post_template);
            var photo_template = Handlebars.compile(photo_post_template);
            var link_template = Handlebars.compile(link_post_template);
            var video_template = Handlebars.compile(video_post_template);
            var audio_template = Handlebars.compile(audio_post_template);
            var quote_template = Handlebars.compile(quote_post_template);

            $('.loading').remove();
            $.each(blog_posts.response.posts, function(i, p) {
                p.formated_date = moment(p.date).format('MMMM DD, YYYY')

                if (p.type == 'text')
                    $('#blog-posts').append(text_template(p));
                else if (p.type == 'photo')
                    $('#blog-posts').append(photo_template(p));
                else if (p.type == 'link')
                    $('#blog-posts').append(link_template(p));
                else if (p.type == 'video')
                    $('#blog-posts').append(video_template(p));
                else if (p.type == 'audio')
                    $('#blog-posts').append(audio_template(p));
                else if (p.type == 'quote')
                    $('#blog-posts').append(quote_template(p));

            });

            setupLinks();
            adjustBlogHeaders();
            prettyPrint();
            setTimeout(setupBlogHeaderScroll, 1000);
            adjustSelection('home-link');
         });
  });
}

function adjustBlogHeaders() {
  if(isMobileView)
    return;

  $('.blog-section article hgroup').each(function(i, e) {
    $(e).find('h3 a').css({
       'margin-top': '-' + ($(e).height() + 100) + 'px' 
    }).addClass('adjusted');
  });
}

function setupBlogHeaderScroll() {

  if(isMobileView)
    return;

  var previousTarget,
      activeTarget,
      $window = $(window),
      offsets = [],
      targets = [],
      $posts = $('.blog-section article hgroup h3 a').each(function() {
        if (this.hash) {
          targets.push(this.hash);
          offsets.push($(this.hash).offset().top);
        }
      });

  function processScroll(e) {
    var scrollTop = $window.scrollTop(),
        i = offsets.length;

    for (i; i--;) {
      if (activeTarget != targets[i] && scrollTop > offsets[i] && (!offsets[i + 1] || scrollTop < offsets[i + 1])) {

          var hgroup = $(activeTarget).find("hgroup");
          var margintop = '';
          if (hgroup.length) {
            margintop = '-' + ($(hgroup[0]).height() + 100) + 'px';
          }

          //set current target to be absolute
          $("h3 a[href=" + activeTarget + "]").removeClass("active").css({
            position: "absolute",
            top: "auto",
            'margin-top': margintop
          });

          //set new target to be fixed
          activeTarget = targets[i];
          $("h3 a[href=" + activeTarget + "]").attr('style', '').addClass("active");
      }

      if (activeTarget && activeTarget != targets[i] && scrollTop + 50 >= offsets[i] && (!offsets[i + 1] || scrollTop + 50 <= offsets[i + 1])) {

          // if it's close to the new target scroll the current target up
          $("h3 a[href=" + activeTarget + "]")
              .removeClass("active")
              .css({
                  position: "absolute",
                  top: ($(activeTarget).outerHeight(true) + $(activeTarget).offset().top - 50) + "px",
                  bottom: "auto"
              });
      }

      if (activeTarget == targets[i] && scrollTop > offsets[i] - 50  && (!offsets[i + 1] || scrollTop <= offsets[i + 1] - 50)) {
          // if the current target is not fixed make it fixed.
          if (!$("h3 a[href=" + activeTarget + "]").hasClass("active")) {
              $("h3 a[href=" + activeTarget + "]").attr('style', '').addClass("active");
          }
      }
    }
  }

  $posts.click(function(e) {
    if (!this.hash)
      return;
    $('html, body').stop().animate({
        scrollTop: $(this.hash).offset().top
    }, 500, 'linear');

    processScroll();
    e.preventDefault();
  });

  $window.scroll(processScroll).trigger("scroll");
}