xref: /dokuwiki/lib/scripts/behaviour.js (revision 20e3e8ebefec296327d39fb572b06d60c151c867)
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.removeHighlightOnClick();
17        dw_behaviour.quickSelect();
18        dw_behaviour.checkWindowsShares();
19        dw_behaviour.initTocToggle();
20    },
21
22    /**
23     * Looks for an element with the ID scroll__here at scrolls to it
24     */
25    scrollToMarker: function(){
26        var $obj = jQuery('#scroll__here');
27        if($obj.length) {
28            $obj[0].scrollIntoView();
29        }
30    },
31
32    /**
33     * Looks for an element with the ID focus__this at sets focus to it
34     */
35    focusMarker: function(){
36        jQuery('#focus__this').focus();
37    },
38
39    /**
40     * Remove all search highlighting when clicking on a highlighted term
41     *
42     * @FIXME would be nice to have it fade out
43     */
44    removeHighlightOnClick: function(){
45        jQuery('span.search_hit').click(
46            function(e){
47                jQuery(e.target).removeClass('search_hit');
48            }
49        );
50    },
51
52    /**
53     * Autosubmit quick select forms
54     *
55     * When a <select> tag has the class "quickselect", this script will
56     * automatically submit its parent form when the select value changes.
57     * It also hides the submit button of the form.
58     *
59     * @author Andreas Gohr <andi@splitbrain.org>
60     */
61    quickSelect: function(){
62        jQuery('select.quickselect')
63            .change(function(e){ e.target.form.submit(); })
64            .parents('form').find('input[type=submit]').hide();
65    },
66
67    /**
68     * Display error for Windows Shares on browsers other than IE
69     *
70     * @author Michael Klier <chi@chimeric.de>
71     */
72    checkWindowsShares: function() {
73        if(!LANG.nosmblinks || document.all !== null) {
74            // No warning requested or none necessary
75            return;
76        }
77
78        jQuery('a.windows').live('click', function(){
79            alert(LANG.nosmblinks);
80        });
81    },
82
83    /**
84     * Adds the toggle switch to the TOC
85     */
86    initTocToggle: function() {
87        var $header = jQuery('#toc__header');
88        if(!$header.length) return;
89        var $toc    = jQuery('#toc__inside');
90
91        var $clicky = jQuery(document.createElement('span'))
92                        .attr('id','toc__toggle')
93                        .css('cursor','pointer')
94                        .click(function(){
95                            $toc.slideToggle(200);
96                            setClicky();
97                        });
98        $header.prepend($clicky);
99
100        var setClicky = function(){
101            if($toc.css('display') == 'none'){
102                $clicky.html('<span>+</span>');
103                $clicky[0].className = 'toc_open';
104            }else{
105                $clicky.html('<span>&minus;</span>');
106                $clicky[0].className = 'toc_close';
107            }
108        };
109
110        setClicky();
111    }
112
113};
114
115/**
116 * Hides elements with a slide animation
117 *
118 * @param fn optional callback to run after hiding
119 * @author Adrian Lang <mail@adrianlang.de>
120 */
121jQuery.fn.dw_hide = function(fn) {
122    return this.slideUp('fast', fn);
123};
124
125/**
126 * Unhides elements with a slide animation
127 *
128 * @param fn optional callback to run after hiding
129 * @author Adrian Lang <mail@adrianlang.de>
130 */
131jQuery.fn.dw_show = function(fn) {
132    return this.slideDown('fast', fn);
133};
134
135/**
136 * Toggles visibility of an element using a slide element
137 *
138 * @param bool the current state of the element (optional)
139 */
140jQuery.fn.dw_toggle = function(bool) {
141    return this.each(function() {
142        var $this = jQuery(this);
143        if (typeof bool === 'undefined') {
144            bool = $this.is(':hidden');
145        }
146        $this[bool ? "dw_show" : "dw_hide" ]();
147    });
148};
149
150jQuery(dw_behaviour.init);
151