summaryrefslogtreecommitdiff
path: root/lib/scripts/behaviour.js
blob: db9dbacc2bb5b01556a4a84851bab4148311cd6d (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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/*jslint sloppy: true */
/*global jQuery, LANG, document, alert */

/**
 * Automatic behaviours
 *
 * This class wraps various JavaScript functionalities that are triggered
 * automatically whenever a certain object is in the DOM or a certain CSS
 * class was found
 */
var dw_behaviour = {

    init: function(){
        dw_behaviour.focusMarker();
        dw_behaviour.scrollToMarker();
        dw_behaviour.closeMsgOnClick();
        dw_behaviour.removeHighlightOnClick();
        dw_behaviour.quickSelect();
        dw_behaviour.checkWindowsShares();
        dw_behaviour.initTocToggle();
    },

    /**
     * Looks for an element with the ID scroll__here at scrolls to it
     */
    scrollToMarker: function(){
        var $obj = jQuery('#scroll__here');
        if($obj.length) {
            $obj[0].scrollIntoView();
        }
    },

    /**
     * Looks for an element with the ID focus__this at sets focus to it
     */
    focusMarker: function(){
        jQuery('#focus__this').focus();
    },

    /**
     * Close messages shown by the msg() PHP function by click
     */
    closeMsgOnClick: function(){
        jQuery('div.success, div.info, div.error, div.notify').click(
            function(e){
                jQuery(e.target).fadeOut('fast');
            }
        );
    },

    /**
     * Remove all search highlighting when clicking on a highlighted term
     *
     * @FIXME would be nice to have it fade out
     */
    removeHighlightOnClick: function(){
        jQuery('span.search_hit').click(
            function(e){
                jQuery(e.target).removeClass('search_hit');
            }
        );
    },

    /**
     * Autosubmit quick select forms
     *
     * When a <select> tag has the class "quickselect", this script will
     * automatically submit its parent form when the select value changes.
     * It also hides the submit button of the form.
     *
     * @author Andreas Gohr <andi@splitbrain.org>
     */
    quickSelect: function(){
        jQuery('select.quickselect')
            .change(function(e){ e.target.form.submit(); })
            .parents('form').find('input[type=submit]').hide();
    },

    /**
     * Display error for Windows Shares on browsers other than IE
     *
     * @author Michael Klier <chi@chimeric.de>
     */
    checkWindowsShares: function() {
        if(!LANG.nosmblinks || document.all !== null) {
            // No warning requested or none necessary
            return;
        }

        jQuery('a.windows').live('click', function(){
            alert(LANG.nosmblinks);
        });
    },

    /**
     * Adds the toggle switch to the TOC
     */
    initTocToggle: function() {
        var $header = jQuery('#toc__header');
        if(!$header.length) return;
        var $toc    = jQuery('#toc__inside');

        var $clicky = jQuery(document.createElement('span'))
                        .attr('id','toc__toggle')
                        .css('cursor','pointer')
                        .click(function(){
                            $toc.slideToggle();
                            setClicky();
                        });
        $header.prepend($clicky);

        var setClicky = function(){
            if($toc.css('display') == 'none'){
                $clicky.html('<span>+</span>');
                $clicky[0].className = 'toc_open';
            }else{
                $clicky.html('<span>&minus;</span>');
                $clicky[0].className = 'toc_close';
            }
        };

        setClicky();
    }

};

jQuery(dw_behaviour.init);