xref: /dokuwiki/lib/scripts/behaviour.js (revision bcdf03d0e0542c0ef7715b05a753a1cd32bd525a)
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> 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     * @author Andreas Gohr <andi@splitbrain.org>
122     */
123    quickSelect: function(){
124        jQuery('select.quickselect')
125            .on('change', function(e){ e.target.form.submit(); })
126            .closest('form').find(':button').not('.show').hide();
127    },
128
129    /**
130     * Display error for Windows Shares on browsers other than IE
131     *
132     * @author Michael Klier <chi@chimeric.de>
133     */
134    checkWindowsShares: function() {
135        if(!LANG.nosmblinks || navigator.userAgent.match(/(Trident|MSIE|Edge)/)) {
136            // No warning requested or none necessary
137            return;
138        }
139
140        jQuery('a.windows').on('click', function(){
141            alert(LANG.nosmblinks.replace(/\\n/,"\n"));
142        });
143    },
144
145    /**
146     * Hide list subscription style if target is a page
147     *
148     * @author Adrian Lang <lang@cosmocode.de>
149     * @author Pierre Spring <pierre.spring@caillou.ch>
150     */
151    subscription: function(){
152        var $form, $list, $digest;
153
154        $form = jQuery('#subscribe__form');
155        if (0 === $form.length) return;
156
157        $list = $form.find("input[name='sub_style'][value='list']");
158        $digest = $form.find("input[name='sub_style'][value='digest']");
159
160        $form.find("input[name='sub_target']")
161            .on('click',
162                function () {
163                    var $this = jQuery(this), show_list;
164                    if (!$this.prop('checked')) {
165                        return;
166                    }
167
168                    show_list = $this.val().match(/:$/);
169                    $list.parent().dw_toggle(show_list);
170                    if (!show_list && $list.prop('checked')) {
171                        $digest.prop('checked', 'checked');
172                    }
173                }
174            )
175            .filter(':checked')
176            .trigger('click');
177    },
178
179    /**
180     * disable multiple revisions checkboxes if two are checked
181     *
182     * @author Andreas Gohr <andi@splitbrain.org>
183     * @author Anika Henke <anika@selfthinker.org>
184     */
185    revisionBoxHandler: function() {
186        var $revisions = jQuery('#page__revisions');
187        var $all       = jQuery('input[type=checkbox]', $revisions);
188        var $checked   = $all.filter(':checked');
189        var $button    = jQuery('button', $revisions);
190
191        if($checked.length < 2) {
192            $all.prop('disabled', false);
193            $button.prop('disabled', true);
194        } else {
195            $all.prop('disabled', true);
196            $button.prop('disabled', false);
197            $checked.each(function(i) {
198                jQuery(this).prop('disabled', false);
199                if(i>1) {
200                    jQuery(this).prop('checked', false);
201                }
202            });
203        }
204    }
205};
206
207jQuery(dw_behaviour.init);
208