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