xref: /dokuwiki/lib/scripts/behaviour.js (revision 4ee1558545059fa73700709a9ef4c0ab22ce8f92)
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]').each(function(){
77                if (!jQuery(this).hasClass('not_hide')) jQuery(this).hide();
78            });
79    },
80
81    /**
82     * Display error for Windows Shares on browsers other than IE
83     *
84     * @author Michael Klier <chi@chimeric.de>
85     */
86    checkWindowsShares: function() {
87        if(!LANG.nosmblinks || document.all !== null) {
88            // No warning requested or none necessary
89            return;
90        }
91
92        jQuery('a.windows').live('click', function(){
93            alert(LANG.nosmblinks);
94        });
95    },
96
97    /**
98     * Adds the toggle switch to the TOC
99     */
100    initTocToggle: function() {
101        var $header = jQuery('#toc__header');
102        if(!$header.length) return;
103        var $toc    = jQuery('#toc__inside');
104
105        var $clicky = jQuery(document.createElement('span'))
106                        .attr('id','toc__toggle')
107                        .css('cursor','pointer')
108                        .click(function(){
109                            $toc.slideToggle();
110                            setClicky();
111                        });
112        $header.prepend($clicky);
113
114        var setClicky = function(){
115            if($toc.css('display') == 'none'){
116                $clicky.html('<span>+</span>');
117                $clicky[0].className = 'toc_open';
118            }else{
119                $clicky.html('<span>&minus;</span>');
120                $clicky[0].className = 'toc_close';
121            }
122        };
123
124        setClicky();
125    }
126
127};
128
129jQuery.fn.dw_hide = function(fn) {
130    return this.slideUp('fast', fn);
131};
132
133jQuery.fn.dw_show = function() {
134    return this.slideDown('fast');
135};
136
137jQuery.fn.dw_toggle = function(bool) {
138    return this.each(function() {
139        var $this = jQuery(this);
140        if (typeof bool === 'undefined') {
141            bool = $this.is(':hidden');
142        }
143        $this[bool ? "dw_show" : "dw_hide" ]();
144    });
145};
146
147jQuery(dw_behaviour.init);
148