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