leftypol_vichan/js/inline.js

123 lines
2.8 KiB
JavaScript
Raw Normal View History

2014-09-24 02:52:06 +00:00
;(function() {
2014-10-05 22:51:22 +00:00
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)
})
2014-10-05 23:20:25 +00:00
},
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)
}
}
2014-10-05 22:51:22 +00:00
}
}
2014-09-24 19:49:18 +00:00
2014-09-24 04:25:34 +00:00
var inline = function(e) {
e.preventDefault()
2014-09-25 17:11:33 +00:00
var $root = $(this).closest('.post')
2014-10-03 20:32:41 +00:00
var targetNum = this.textContent.slice(2)
2014-09-24 04:25:34 +00:00
2014-10-03 20:32:41 +00:00
var $clone = $root.find('#inline_' + targetNum)
2014-09-24 04:25:34 +00:00
if ($clone.length)
return $clone.remove()
2014-10-03 20:40:05 +00:00
var srcOP = $root.closest('[id^=thread]').attr('id').match(/\d+/)[0]
2014-09-24 04:25:34 +00:00
2014-10-03 20:40:05 +00:00
var node, targetOP
if (this.className) {// backlink
node = $root.find('> .intro')
targetOP = srcOP
} else {
node = $(this)
targetOP = this.pathname.match(/(\d+).html/)[1]
}
2014-09-25 17:11:33 +00:00
2014-09-24 04:25:34 +00:00
var link = {
2014-09-25 17:11:33 +00:00
node: node,
2014-10-03 20:32:41 +00:00
targetNum: targetNum
2014-09-24 04:25:34 +00:00
}
2014-09-24 19:17:30 +00:00
2014-10-03 20:40:05 +00:00
var selector = targetNum === targetOP
? '#op_' + srcOP
2014-10-03 20:40:05 +00:00
: '#reply_' + targetNum
2014-10-03 20:32:41 +00:00
if (srcOP === targetOP) {
2014-10-04 15:16:01 +00:00
if (targetNum === targetOP)
link.node = link.node.next()// bypass `(OP)`
2014-10-03 20:28:52 +00:00
2014-09-24 19:17:30 +00:00
var $target = $(selector)
2014-10-03 20:28:52 +00:00
if ($target.length)
return add(link, $target)
2014-09-24 19:49:18 +00:00
}
2014-10-05 22:51:22 +00:00
App.get(this.pathname, function($page) {
var $target = $page.find(selector)
2014-09-24 19:49:18 +00:00
add(link, $target)
})
2014-09-24 04:25:34 +00:00
}
var add = function(link, $target) {
var $clone = $target.clone(true)
$clone.find('.inline').remove()
2014-09-24 04:25:34 +00:00
$clone.attr({
2014-10-03 16:53:11 +00:00
"class": 'inline post',
2014-10-03 20:32:41 +00:00
id: 'inline_' + link.targetNum,
2014-10-03 20:28:52 +00:00
style: null// XXX remove post hover styling
2014-09-24 04:25:34 +00:00
})
2014-10-03 20:05:45 +00:00
$clone.insertAfter(link.node)
2014-09-24 04:25:34 +00:00
}
2014-09-24 02:52:06 +00:00
2014-10-05 23:20:25 +00:00
App.options.add('inline', 'Inline quoted posts')
if (!App.options.get('inline'))
return
2014-09-24 02:52:06 +00:00
$('head').append(
'<style>' +
2014-10-04 13:48:29 +00:00
'.inline {' +
'border: 1px dashed black;' +
'white-space: normal;' +
'overflow: auto;' + // clearfix
'}' +
2014-09-24 02:52:06 +00:00
'</style>')
// don't attach to outbound links
$('.body a:not([rel]), .mentioned a')
2014-10-03 16:17:19 +00:00
.attr('onclick', null)// XXX disable highlightReply
2014-09-25 16:58:44 +00:00
.click(inline)
2014-09-24 04:25:34 +00:00
})()