summaryrefslogtreecommitdiff
path: root/js/treeview.js
blob: c9bd55f8ae452b92597184f2aad545a9d6b207ea (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
/*
 * treeview.js
 * https://github.com/vichan-devel/vichan/blob/master/js/treeview.js
 *
 * Released under the MIT license
 * Copyright (c) 2014 Marcin Łabanowski <marcin@6irc.net>
 *
 * Usage:
 *   $config['additional_javascript'][] = 'js/jquery.min.js';
 *   $config['additional_javascript'][] = 'js/treeview.js';
 *
 */

if (active_page == 'thread' || active_page == 'ukko' || active_page == 'index')
$(function() {
	if (window.Options && Options.get_tab('general')) {
		var selector = '#treeview-global>input';
		Options.extend_tab("general", "<label id='treeview-global'><input type='checkbox' /> "+_('Use tree view by default')+"</label>");
	}
	$(selector).on('change', function () {
		if (localStorage.treeview === 'true') {
			localStorage.treeview = 'false';
		} else {
			localStorage.treeview = 'true';
		}
	});
	if (localStorage.treeview === 'true') {
		$(selector).attr('checked', 'checked');
	}
});

if (active_page == 'thread')
$(function() {
	var treeview = function() {
		$('.post.reply').each(function(){
			var references = [];
			$(this).find('.body a').each(function(){
				if ($(this).html().match('^&gt;&gt;[0-9]+$')) {
					references.push(parseInt($(this).html().replace('&gt;&gt;', '')));
				}
			});
			var maxref = references.reduce(function(a,b) { return a > b ? a : b; }, 0);
	
			var parent_post = $("#reply_"+maxref);
			if (parent_post.length == 0) return;
	
			var margin = parseInt(parent_post.css("margin-left"))+32;
	
			var post = $(this);
			var br = post.next();

			post.detach().css("margin-left", margin).insertAfter(parent_post.next());
			br.detach().insertAfter(post);
		});
	}
	if (localStorage.treeview === 'true') {
		treeview();
	}
	
        $('hr:first').before('<div id="treeview" style="text-align:right"><a class="unimportant" href="javascript:void(0)"></a></div>');
        $('div#treeview a')
                .text(_('Tree view'))
                .click(function(e) { treeview(); e.preventDefault(); });
});