1/**
2 * Outliner plugin JS library
3 *
4 * @author Michael Hamann <michael [at] content-space [dot] de>
5 */
6
7
8jQuery(function () {
9    var $outliner_dls = jQuery('dl.outliner');
10	var $outliner_dls_dt = jQuery('dl.outliner').find('dt');
11    var setState = function(node, state,actionState) {
12		if (state != 'open' && state != 'closed') { return; }
13		if (actionState != 'action' && actionState != 'noAction' && actionState != 'None') { console.error("actionState=",actionState) ;return; }
14
15		if (actionState == 'None'){
16			var nodeId = getOutlinerId(node);
17
18			actionState = localStorage.getItem(nodeId + '_action');
19		}
20		if (actionState == 'action' ) {
21			var otherState = (state == 'open') ? 'closed' : 'open';
22			jQuery(node).removeClass('outliner-' + otherState).addClass('outliner-' + state);
23			var nodeId = getOutlinerId(node);
24			if (nodeId) {
25				try {
26					localStorage.setItem(nodeId,state);
27					localStorage.setItem(nodeId + '_action',actionState);
28				} catch (e){
29					console.error("something went wrong when trying to access local storage : {",e,"}");
30				}
31			}
32		} else if (actionState == 'noAction'){
33			var nodeId = getOutlinerId(node);
34
35			if (nodeId) {
36				try {
37					localStorage.setItem(nodeId,'open');
38					localStorage.setItem(nodeId + '_action','action');
39				} catch (e){
40					console.error("something went wrong when trying to access local storage : {",e,"}");
41				}
42			}
43
44		}
45    };
46
47
48
49    var getOutlinerId = function(node) {
50        var match = node.className.match(/outl_\w+/);
51        if (match) {
52            return match[0];
53        } else {
54            return null;
55        }
56    };
57
58    $outliner_dls
59        .addClass('outliner-js')
60        .find('dt')
61            .click(function() {
62                if (jQuery(this.parentNode).hasClass('outliner-open')) {
63                    setState(this.parentNode, 'closed','None');
64                } else {
65                    setState(this.parentNode, 'open','None');
66                }
67            })
68            .mouseover(function() {
69                var thisPos = jQuery(this).position();
70                jQuery(this).siblings('dd').css({'left': thisPos.left + 40 + 'px', 'top': thisPos.top + 20 + 'px'});
71            });
72	$outliner_dls_dt
73		.find('a')
74			.click(function() {
75				var id = getOutlinerId(this.parentNode.parentNode);
76				localStorage.setItem(id,'open');
77				localStorage.setItem(id + '_action','noAction');
78				//setState(this.parentNode.parentNode, 'open','noAction');
79            });
80    $outliner_dls
81        .each(function() {
82            var id = getOutlinerId(this);
83            if (id) {
84					try {
85						setState(this, localStorage.getItem(id),localStorage.getItem(id + '_action'));
86					} catch (e){
87						console.error("Something went wrong when trying to access local storage : {",e,"}");
88						setState(this, "closed",'None');
89					}
90            }
91        })
92        .filter(':not(.outliner-open,.outliner-closed)').each(function() {
93            setState(this, 'closed','action');
94        });
95
96    $outliner_dls.find('li.active').parents('dl').each(function() {
97        setState(this, 'open', 'action');
98    });
99
100});
101