xref: /dokuwiki/lib/scripts/behaviour.js (revision f4aabe763b3ae83161819c991d4c7820523c3524)
1/**
2 * Automatic behaviours
3 *
4 * This class wraps various JavaScript functionalities that are triggered
5 * automatically whenever a certain object is in the DOM or a certain CSS
6 * class was found
7 */
8var dw_behaviour = {
9
10    init: function(){
11        dw_behaviour.focusMarker();
12        dw_behaviour.scrollToMarker();
13        dw_behaviour.removeHighlightOnClick();
14        dw_behaviour.quickSelect();
15        dw_behaviour.checkWindowsShares();
16        dw_behaviour.subscription();
17
18        dw_behaviour.revisionBoxHandler();
19        jQuery('#page__revisions input[type=checkbox]').click(
20            dw_behaviour.revisionBoxHandler
21        );
22    },
23
24    /**
25     * Looks for an element with the ID scroll__here at scrolls to it
26     */
27    scrollToMarker: function(){
28        var $obj = jQuery('#scroll__here');
29        if($obj.length) {
30            $obj[0].scrollIntoView();
31        }
32    },
33
34    /**
35     * Looks for an element with the ID focus__this at sets focus to it
36     */
37    focusMarker: function(){
38        jQuery('#focus__this').focus();
39    },
40
41    /**
42     * Remove all search highlighting when clicking on a highlighted term
43     *
44     * @FIXME would be nice to have it fade out
45     */
46    removeHighlightOnClick: function(){
47        jQuery('span.search_hit').click(
48            function(e){
49                jQuery(e.target).removeClass('search_hit');
50            }
51        );
52    },
53
54    /**
55     * Autosubmit quick select forms
56     *
57     * When a <select> tag has the class "quickselect", this script will
58     * automatically submit its parent form when the select value changes.
59     * It also hides the submit button of the form.
60     *
61     * @author Andreas Gohr <andi@splitbrain.org>
62     */
63    quickSelect: function(){
64        jQuery('select.quickselect')
65            .change(function(e){ e.target.form.submit(); })
66            .closest('form').find('input[type=submit]').not('.show').hide();
67    },
68
69    /**
70     * Display error for Windows Shares on browsers other than IE
71     *
72     * @author Michael Klier <chi@chimeric.de>
73     */
74    checkWindowsShares: function() {
75        if(!LANG.nosmblinks || typeof(document.all) !== 'undefined') {
76            // No warning requested or none necessary
77            return;
78        }
79
80        jQuery('a.windows').live('click', function(){
81            alert(LANG.nosmblinks.replace(/\\n/,"\n"));
82        });
83    },
84
85    /**
86     * Hide list subscription style if target is a page
87     *
88     * @author Adrian Lang <lang@cosmocode.de>
89     * @author Pierre Spring <pierre.spring@caillou.ch>
90     */
91    subscription: function(){
92        var $form, $list, $digest;
93
94        $form = jQuery('#subscribe__form');
95        if (0 === $form.length) return;
96
97        $list = $form.find("input[name='sub_style'][value='list']");
98        $digest = $form.find("input[name='sub_style'][value='digest']");
99
100        $form.find("input[name='sub_target']")
101            .click(
102                function () {
103                    var $this = jQuery(this), show_list;
104                    if (!$this.prop('checked')) {
105                        return;
106                    }
107
108                    show_list = $this.val().match(/:$/);
109                    $list.parent().dw_toggle(show_list);
110                    if (!show_list && $list.prop('checked')) {
111                        $digest.prop('checked', 'checked');
112                    }
113                }
114            )
115            .filter(':checked')
116            .click();
117    },
118
119    /**
120     * disable multiple revisions checkboxes if two are checked
121     *
122     * @author Andreas Gohr <andi@splitbrain.org>
123     */
124    revisionBoxHandler: function(){
125        var $checked = jQuery('#page__revisions input[type=checkbox]:checked');
126        var $all     = jQuery('#page__revisions input[type=checkbox]');
127
128        if($checked.length < 2){
129            $all.attr('disabled',false);
130            jQuery('#page__revisions input[type=submit]').attr('disabled',true);
131        }else{
132            $all.attr('disabled',true);
133            jQuery('#page__revisions input[type=submit]').attr('disabled',false);
134            for(var i=0; i<$checked.length; i++){
135                $checked[i].disabled = false;
136                if(i>1){
137                    $checked[i].checked = false;
138                }
139            }
140        }
141    }
142
143};
144
145/**
146 * Hides elements with a slide animation
147 *
148 * @param fn optional callback to run after hiding
149 * @author Adrian Lang <mail@adrianlang.de>
150 */
151jQuery.fn.dw_hide = function(fn) {
152    return this.slideUp('fast', fn);
153};
154
155/**
156 * Unhides elements with a slide animation
157 *
158 * @param fn optional callback to run after hiding
159 * @author Adrian Lang <mail@adrianlang.de>
160 */
161jQuery.fn.dw_show = function(fn) {
162    return this.slideDown('fast', fn);
163};
164
165/**
166 * Toggles visibility of an element using a slide element
167 *
168 * @param bool the current state of the element (optional)
169 */
170jQuery.fn.dw_toggle = function(bool, fn) {
171    return this.each(function() {
172        var $this = jQuery(this);
173        if (typeof bool === 'undefined') {
174            bool = $this.is(':hidden');
175        }
176        $this[bool ? "dw_show" : "dw_hide" ](fn);
177    });
178};
179
180jQuery(dw_behaviour.init);
181