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