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