xref: /dokuwiki/lib/scripts/behaviour.js (revision 5b812846ce6a2e72e388adbb0dcaadbc0137b173)
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
115jQuery.fn.dw_hide = function(fn) {
116    return this.slideUp('fast', fn);
117};
118
119jQuery.fn.dw_show = function() {
120    return this.slideDown('fast');
121};
122
123jQuery.fn.dw_toggle = function(bool) {
124    return this.each(function() {
125        var $this = jQuery(this);
126        if (typeof bool === 'undefined') {
127            bool = $this.is(':hidden');
128        }
129        $this[bool ? "dw_show" : "dw_hide" ]();
130    });
131};
132
133jQuery(dw_behaviour.init);
134