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