xref: /dokuwiki/lib/scripts/behaviour.js (revision a802fe3c551a51dc15cb56b04863c663783b6b4c)
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        var $revert = jQuery('#dokuwiki__pagetools li.revert a');
90        if($revert.length) {
91            $revert.on('click', function() {
92                return confirm(LANG.restore_confirm);
93            });
94        }
95    },
96
97    /**
98     * Looks for an element with the ID focus__this at sets focus to it
99     */
100    focusMarker: function(){
101        jQuery('#focus__this').trigger('focus');
102    },
103
104    /**
105     * Remove all search highlighting when clicking on a highlighted term
106     */
107    removeHighlightOnClick: function(){
108        jQuery('span.search_hit').on('click',
109            function(e){
110                jQuery(e.target).removeClass('search_hit', 1000);
111            }
112        );
113    },
114
115    /**
116     * Autosubmit quick select forms
117     *
118     * When a <select> tag has the class "quickselect", this script will
119     * automatically submit its parent form when the select value changes.
120     * It also hides the submit button of the form.
121     *
122     * @author Andreas Gohr <andi@splitbrain.org>
123     */
124    quickSelect: function(){
125        jQuery('select.quickselect')
126            .on('change', function(e){ e.target.form.submit(); })
127            .closest('form').find(':button').not('.show').hide();
128    },
129
130    /**
131     * Display error for Windows Shares on browsers other than IE
132     *
133     * @author Michael Klier <chi@chimeric.de>
134     */
135    checkWindowsShares: function() {
136        if(!LANG.nosmblinks || navigator.userAgent.match(/(Trident|MSIE|Edge)/)) {
137            // No warning requested or none necessary
138            return;
139        }
140
141        jQuery('a.windows').on('click', function(){
142            alert(LANG.nosmblinks.replace(/\\n/,"\n"));
143        });
144    },
145
146    /**
147     * Hide list subscription style if target is a page
148     *
149     * @author Adrian Lang <lang@cosmocode.de>
150     * @author Pierre Spring <pierre.spring@caillou.ch>
151     */
152    subscription: function(){
153        var $form, $list, $digest;
154
155        $form = jQuery('#subscribe__form');
156        if (0 === $form.length) return;
157
158        $list = $form.find("input[name='sub_style'][value='list']");
159        $digest = $form.find("input[name='sub_style'][value='digest']");
160
161        $form.find("input[name='sub_target']")
162            .on('click',
163                function () {
164                    var $this = jQuery(this), show_list;
165                    if (!$this.prop('checked')) {
166                        return;
167                    }
168
169                    show_list = $this.val().match(/:$/);
170                    $list.parent().dw_toggle(show_list);
171                    if (!show_list && $list.prop('checked')) {
172                        $digest.prop('checked', 'checked');
173                    }
174                }
175            )
176            .filter(':checked')
177            .trigger('click');
178    },
179
180    /**
181     * disable multiple revisions checkboxes if two are checked
182     *
183     * @author Andreas Gohr <andi@splitbrain.org>
184     * @author Anika Henke <anika@selfthinker.org>
185     */
186    revisionBoxHandler: function() {
187        var $revisions = jQuery('#page__revisions');
188        var $all       = jQuery('input[type=checkbox]', $revisions);
189        var $checked   = $all.filter(':checked');
190        var $button    = jQuery('button', $revisions);
191
192        if($checked.length < 2) {
193            $all.prop('disabled', false);
194            $button.prop('disabled', true);
195        } else {
196            $all.prop('disabled', true);
197            $button.prop('disabled', false);
198            $checked.each(function(i) {
199                jQuery(this).prop('disabled', false);
200                if(i>1) {
201                    jQuery(this).prop('checked', false);
202                }
203            });
204        }
205    }
206};
207
208jQuery(dw_behaviour.init);
209