xref: /dokuwiki/lib/scripts/behaviour.js (revision 32cb905a0339607a7acfef6488bec0015bae6b8b)
1/*jslint sloppy: true */
2/*global jQuery, LANG, document, alert */
3
4/**
5 * Automatic behaviours
6 *
7 * This class wraps various JavaScript functionalities that are triggered
8 * automatically whenever a certain object is in the DOM or a certain CSS
9 * class was found
10 */
11var dw_behaviour = {
12
13    init: function(){
14        dw_behaviour.focusMarker();
15        dw_behaviour.scrollToMarker();
16        dw_behaviour.closeMsgOnClick();
17        dw_behaviour.removeHighlightOnClick();
18        dw_behaviour.quickSelect();
19        dw_behaviour.checkWindowsShares();
20        dw_behaviour.initTocToggle();
21    },
22
23    /**
24     * Looks for an element with the ID scroll__here at scrolls to it
25     */
26    scrollToMarker: function(){
27        var $obj = jQuery('#scroll__here');
28        if($obj.length) {
29            $obj[0].scrollIntoView();
30        }
31    },
32
33    /**
34     * Looks for an element with the ID focus__this at sets focus to it
35     */
36    focusMarker: function(){
37        jQuery('#focus__this').focus();
38    },
39
40    /**
41     * Close messages shown by the msg() PHP function by click
42     */
43    closeMsgOnClick: function(){
44        jQuery('div.success, div.info, div.error, div.notify').click(
45            function(e){
46                jQuery(e.target).fadeOut('fast');
47            }
48        );
49    },
50
51    /**
52     * Remove all search highlighting when clicking on a highlighted term
53     *
54     * @FIXME would be nice to have it fade out
55     */
56    removeHighlightOnClick: function(){
57        jQuery('span.search_hit').click(
58            function(e){
59                jQuery(e.target).removeClass('search_hit');
60            }
61        );
62    },
63
64    /**
65     * Autosubmit quick select forms
66     *
67     * When a <select> tag has the class "quickselect", this script will
68     * automatically submit its parent form when the select value changes.
69     * It also hides the submit button of the form.
70     *
71     * @author Andreas Gohr <andi@splitbrain.org>
72     */
73    quickSelect: function(){
74        jQuery('select.quickselect')
75            .change(function(e){ e.target.form.submit(); })
76            .parents('form').find('input[type=submit]').hide();
77    },
78
79    /**
80     * Display error for Windows Shares on browsers other than IE
81     *
82     * @author Michael Klier <chi@chimeric.de>
83     */
84    checkWindowsShares: function() {
85        if(!LANG.nosmblinks || document.all !== null) {
86            // No warning requested or none necessary
87            return;
88        }
89
90        jQuery('a.windows').live('click', function(){
91            alert(LANG.nosmblinks);
92        });
93    },
94
95    /**
96     * Adds the toggle switch to the TOC
97     */
98    initTocToggle: function() {
99        var $header = jQuery('#toc__header');
100        if(!$header.length) return;
101        var $toc    = jQuery('#toc__inside');
102
103        var $clicky = jQuery(document.createElement('span'))
104                        .attr('id','toc__toggle')
105                        .css('cursor','pointer')
106                        .click(function(){
107                            $toc.slideToggle();
108                            setClicky();
109                        });
110        $header.prepend($clicky);
111
112        var setClicky = function(){
113            if($toc.css('display') == 'none'){
114                $clicky.html('<span>+</span>');
115                $clicky[0].className = 'toc_open';
116            }else{
117                $clicky.html('<span>&minus;</span>');
118                $clicky[0].className = 'toc_close';
119            }
120        };
121
122        setClicky();
123    }
124
125};
126
127jQuery(dw_behaviour.init);
128