xref: /dokuwiki/lib/scripts/behaviour.js (revision 4063422b2ff7e6a9d56d7335cfdd2a8c87f49132)
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            if($obj.offset().top != 0) {
68                jQuery('html, body').animate({
69                    scrollTop: $obj.offset().top - 100
70                }, 500);
71            } else {
72                // hidden object have no offset but can still be scrolled into view
73                $obj[0].scrollIntoView();
74            }
75        }
76    },
77
78    /**
79     * Looks for an element with the ID focus__this at sets focus to it
80     */
81    focusMarker: function(){
82        jQuery('#focus__this').focus();
83    },
84
85    /**
86     * Remove all search highlighting when clicking on a highlighted term
87     */
88    removeHighlightOnClick: function(){
89        jQuery('span.search_hit').click(
90            function(e){
91                jQuery(e.target).removeClass('search_hit', 1000);
92            }
93        );
94    },
95
96    /**
97     * Autosubmit quick select forms
98     *
99     * When a <select> tag has the class "quickselect", this script will
100     * automatically submit its parent form when the select value changes.
101     * It also hides the submit button of the form.
102     *
103     * @author Andreas Gohr <andi@splitbrain.org>
104     */
105    quickSelect: function(){
106        jQuery('select.quickselect')
107            .change(function(e){ e.target.form.submit(); })
108            .closest('form').find('input[type=submit]').not('.show').hide();
109    },
110
111    /**
112     * Display error for Windows Shares on browsers other than IE
113     *
114     * @author Michael Klier <chi@chimeric.de>
115     */
116    checkWindowsShares: function() {
117        if(!LANG.nosmblinks || navigator.userAgent.match(/(Trident|MSIE)/)) {
118            // No warning requested or none necessary
119            return;
120        }
121
122        jQuery('a.windows').on('click', function(){
123            alert(LANG.nosmblinks.replace(/\\n/,"\n"));
124        });
125    },
126
127    /**
128     * Hide list subscription style if target is a page
129     *
130     * @author Adrian Lang <lang@cosmocode.de>
131     * @author Pierre Spring <pierre.spring@caillou.ch>
132     */
133    subscription: function(){
134        var $form, $list, $digest;
135
136        $form = jQuery('#subscribe__form');
137        if (0 === $form.length) return;
138
139        $list = $form.find("input[name='sub_style'][value='list']");
140        $digest = $form.find("input[name='sub_style'][value='digest']");
141
142        $form.find("input[name='sub_target']")
143            .click(
144                function () {
145                    var $this = jQuery(this), show_list;
146                    if (!$this.prop('checked')) {
147                        return;
148                    }
149
150                    show_list = $this.val().match(/:$/);
151                    $list.parent().dw_toggle(show_list);
152                    if (!show_list && $list.prop('checked')) {
153                        $digest.prop('checked', 'checked');
154                    }
155                }
156            )
157            .filter(':checked')
158            .click();
159    },
160
161    /**
162     * disable multiple revisions checkboxes if two are checked
163     *
164     * @author Andreas Gohr <andi@splitbrain.org>
165     */
166    revisionBoxHandler: function(){
167        var $checked = jQuery('#page__revisions input[type=checkbox]:checked');
168        var $all     = jQuery('#page__revisions input[type=checkbox]');
169
170        if($checked.length < 2){
171            $all.attr('disabled',false);
172            jQuery('#page__revisions input[type=submit]').attr('disabled',true);
173        }else{
174            $all.attr('disabled',true);
175            jQuery('#page__revisions input[type=submit]').attr('disabled',false);
176            for(var i=0; i<$checked.length; i++){
177                $checked[i].disabled = false;
178                if(i>1){
179                    $checked[i].checked = false;
180                }
181            }
182        }
183    }
184
185};
186
187jQuery(dw_behaviour.init);
188