1
0
mirror of https://github.com/vichan-devel/vichan.git synced 2024-11-24 23:50:11 +01:00
vichan/js/inline.js
James Campos bbc4169eaa fix
2014-10-04 08:16:01 -07:00

85 lines
1.9 KiB
JavaScript

;(function() {
var cache = {}
var inline = function(e) {
e.preventDefault()
var $root = $(this).closest('.post')
var targetNum = this.textContent.slice(2)
var $clone = $root.find('#inline_' + targetNum)
if ($clone.length)
return $clone.remove()
var srcOP = $root.closest('[id^=thread]').attr('id').match(/\d+/)[0]
var node, targetOP
if (this.className) {// backlink
node = $root.find('> .intro')
targetOP = srcOP
} else {
node = $(this)
targetOP = this.pathname.match(/(\d+).html/)[1]
}
var link = {
node: node,
targetNum: targetNum
}
var selector = targetNum === targetOP
? '.op .body'
: '#reply_' + targetNum
if (srcOP === targetOP) {
// XXX post hover adds fetched threads to the DOM
selector = '#thread_' + srcOP + ' ' + selector
// bypass `(OP)`
if (targetNum === targetOP)
link.node = link.node.next()
var $target = $(selector)
if ($target.length)
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.find('.inline').remove()
$clone.attr({
"class": 'inline post',
id: 'inline_' + link.targetNum,
style: null// XXX remove post hover styling
})
$clone.insertAfter(link.node)
}
$('head').append(
'<style>' +
'.inline {' +
'border: 1px dashed black;' +
'white-space: normal;' +
'overflow: auto;' + // clearfix
'}' +
'</style>')
$('.body a, .mentioned a')
.attr('onclick', null)// XXX disable highlightReply
.click(inline)
})()