summaryrefslogtreecommitdiff
path: root/js/inline.js
blob: ff897ca3148470dcba2a50731aedf9921183bd4c (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
;(function() {
  var cache = {}

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

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

    var $clone = $root.find('#inline_' + postNum)
    if ($clone.length)
      return $clone.remove()

    var postOP = this.pathname.match(/(\d+).html/)[1]
    var selector = postNum === postOP
      ? '.op .body'
      : '#reply_' + postNum + ' .body'

    var node = this.className
      // XXX post hover element is added to the quoting post
      ? $root.find('> .body, > .inline').first()
      : this.nextSibling

    var link = {
      node: node,
      postNum: postNum
    }

    var OP = $('input[name="thread"]').val()
    if (OP === postOP) {
      // XXX post hover adds fetched threads to the DOM
      selector = '#thread_' + OP + ' ' + selector
      var $target = $(selector)
      return add(link, $target)
    }

    var url = this.pathname
    var data = cache[url]
    if (data) {
      var $target = $(data).find(selector)
      return add(link, $target)
    }

    $.get(url, function(data) {
      cache[url] = data
      var $target = $(data).find(selector)
      add(link, $target)
    })
  }

  var add = function(link, $target) {
    var $clone = $target.clone(true)
    $clone.attr({
      "class": 'inline',
      id: 'inline_' + link.postNum
    })
    $clone.insertBefore(link.node)
  }

  $('head').append(
    '<style>' +
      '.inline {' +
      '  border: 1px dashed black;' +
      '  margin-left: 1em;' +
      '  padding: 1em;' +
      '}' +
    '</style>')

  $('.body a, .mentioned a')
    .attr('onclick', null)// XXX disable highlightReply
    .click(inline)
})()