summaryrefslogtreecommitdiff
path: root/js/inline.js
blob: 63b7a8df11bad87acb8ee922c9e285545b8e390e (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
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
144
145
$(document).ready(function() {
  var App = {
    cache: {},
    get: function(url, cb) {
      var $page = App.cache[url]
      if ($page)
        return cb($page)

      $.get(url, function(data) {
        var $page = $(data)
        App.cache[url] = $page
        cb($page)
      })
    },
    options: {
      add: function(key, description, tab) {
        tab || (tab = 'general')

        var checked = App.options.get(key)
        var $el = $(
          '<div>' +
            '<label>' +
              '<input type="checkbox">' +
              description +
            '</label>' +
          '</div>')

        $el
          .find('input')
          .prop('checked', checked)
          .on('change', App.options.check(key))

        window.Options.extend_tab(tab, $el)
      },
      get: function(key) {
        if (localStorage[key])
          return JSON.parse(localStorage[key])
      },
      check: function(key) {
        return function(e) {
          var val = this.checked
          localStorage[key] = JSON.stringify(val)
        }
      }
    }
  }

  var inline = function(e) {
    e.preventDefault()

    var $root = $(this).closest('.post')
    var targetNum = this.textContent.slice(2)

    var srcOP = $root.closest('[id^=thread]').attr('id').match(/\d+/)[0]

    var node, targetOP
    var isBacklink = !!this.className
    if (isBacklink) {
      node = $root.find('> .intro')
      targetOP = srcOP
    } else {
      node = $(this)

      var to_search = inMod ? this.search : this.pathname;
      targetOP = to_search.match(/(\d+).html/)[1]
    }

    var link = {
      id: 'inline_' + targetNum,
      isBacklink: isBacklink,
      node: node
    }

    var selector = targetNum === targetOP
      ? '#op_' + srcOP
      : '#reply_' + targetNum

    var $clone = $root.find('#inline_' + targetNum)
    if ($clone.length) {
      $clone.remove()
      $(selector)
        .show()
        .next()
        .show()
      return
    }

    if (srcOP === targetOP) {
      if (targetNum === targetOP)
        link.node = link.node.next()// bypass `(OP)`

      var $target = $(selector)
      if ($target.length)
        return add(link, $target)
    }

    var $loading = $('<div class="inline post">loading...</div>')
      .attr('id', link.id)
      .insertAfter(link.node)

    App.get(this.pathname, function($page) {
      $loading.remove()
      var $target = $page.find(selector)
      add(link, $target)
    })
  }

  var add = function(link, $target) {
    var $clone = $target.clone(true)

    if (link.isBacklink && App.options.get('hidePost'))
      $target
        .hide()
        .next()
        .hide()

    $clone.find('.inline').remove()
    $clone.attr({
      "class": 'inline post',
      id: link.id,
      style: null// XXX remove post hover styling
    })
    $clone.insertAfter(link.node)
  }

  App.options.add('useInlining', 'Enable inlining')
  App.options.add('hidePost', 'Hide inlined backlinked posts')

  $('head').append(
    '<style>' +
      '.inline {' +
        'border: 1px dashed black;' +
        'white-space: normal;' +
        'overflow: auto;' + // clearfix
      '}' +
    '</style>')

  // don't attach to outbound links

  if (App.options.get('useInlining')) {
    $('.body a:not([rel]), .mentioned a')
      .attr('onclick', null)// XXX disable highlightReply
      .click(inline)
  }
});