xref: /dokuwiki/lib/scripts/behaviour.js (revision 21606750a4ae0a46937a589ca323c250f26a2e58)
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    return this.slideUp('fast', fn);
9};
10
11/**
12 * Unhides elements with a slide animation
13 *
14 * @param fn optional callback to run after hiding
15 * @author Adrian Lang <mail@adrianlang.de>
16 */
17jQuery.fn.dw_show = function(fn) {
18    return this.slideDown('fast', fn);
19};
20
21/**
22 * Toggles visibility of an element using a slide element
23 *
24 * @param bool the current state of the element (optional)
25 */
26jQuery.fn.dw_toggle = function(bool, fn) {
27    return this.each(function() {
28        var $this = jQuery(this);
29        if (typeof bool === 'undefined') {
30            bool = $this.is(':hidden');
31        }
32        $this[bool ? "dw_show" : "dw_hide" ](fn);
33    });
34};
35
36/**
37 * Automatic behaviours
38 *
39 * This class wraps various JavaScript functionalities that are triggered
40 * automatically whenever a certain object is in the DOM or a certain CSS
41 * class was found
42 */
43var dw_behaviour = {
44
45    init: function(){
46        dw_behaviour.focusMarker();
47        dw_behaviour.scrollToMarker();
48        dw_behaviour.removeHighlightOnClick();
49        dw_behaviour.quickSelect();
50        dw_behaviour.checkWindowsShares();
51        dw_behaviour.subscription();
52
53        dw_behaviour.revisionBoxHandler();
54        jQuery(document).on('click','#page__revisions input[type=checkbox]',
55            dw_behaviour.revisionBoxHandler
56        );
57    },
58
59    /**
60     * Looks for an element with the ID scroll__here at scrolls to it
61     */
62    scrollToMarker: function(){
63        var $obj = jQuery('#scroll__here');
64        if($obj.length) {
65            $obj[0].scrollIntoView();
66        }
67    },
68
69    /**
70     * Looks for an element with the ID focus__this at sets focus to it
71     */
72    focusMarker: function(){
73        jQuery('#focus__this').focus();
74    },
75
76    /**
77     * Remove all search highlighting when clicking on a highlighted term
78     *
79     * @FIXME would be nice to have it fade out
80     */
81    removeHighlightOnClick: function(){
82        jQuery('span.search_hit').click(
83            function(e){
84                jQuery(e.target).removeClass('search_hit');
85            }
86        );
87    },
88
89    /**
90     * Autosubmit quick select forms
91     *
92     * When a <select> tag has the class "quickselect", this script will
93     * automatically submit its parent form when the select value changes.
94     * It also hides the submit button of the form.
95     *
96     * @author Andreas Gohr <andi@splitbrain.org>
97     */
98    quickSelect: function(){
99        jQuery('select.quickselect')
100            .change(function(e){ e.target.form.submit(); })
101            .closest('form').find('input[type=submit]').not('.show').hide();
102    },
103
104    /**
105     * Display error for Windows Shares on browsers other than IE
106     *
107     * @author Michael Klier <chi@chimeric.de>
108     */
109    checkWindowsShares: function() {
110        if(!LANG.nosmblinks || typeof(document.all) !== 'undefined') {
111            // No warning requested or none necessary
112            return;
113        }
114
115        jQuery('a.windows').on('click', function(){
116            alert(LANG.nosmblinks.replace(/\\n/,"\n"));
117        });
118    },
119
120    /**
121     * Hide list subscription style if target is a page
122     *
123     * @author Adrian Lang <lang@cosmocode.de>
124     * @author Pierre Spring <pierre.spring@caillou.ch>
125     */
126    subscription: function(){
127        var $form, $list, $digest;
128
129        $form = jQuery('#subscribe__form');
130        if (0 === $form.length) return;
131
132        $list = $form.find("input[name='sub_style'][value='list']");
133        $digest = $form.find("input[name='sub_style'][value='digest']");
134
135        $form.find("input[name='sub_target']")
136            .click(
137                function () {
138                    var $this = jQuery(this), show_list;
139                    if (!$this.prop('checked')) {
140                        return;
141                    }
142
143                    show_list = $this.val().match(/:$/);
144                    $list.parent().dw_toggle(show_list);
145                    if (!show_list && $list.prop('checked')) {
146                        $digest.prop('checked', 'checked');
147                    }
148                }
149            )
150            .filter(':checked')
151            .click();
152    },
153
154    /**
155     * disable multiple revisions checkboxes if two are checked
156     *
157     * @author Andreas Gohr <andi@splitbrain.org>
158     */
159    revisionBoxHandler: function(){
160        var $checked = jQuery('#page__revisions input[type=checkbox]:checked');
161        var $all     = jQuery('#page__revisions input[type=checkbox]');
162
163        if($checked.length < 2){
164            $all.attr('disabled',false);
165            jQuery('#page__revisions input[type=submit]').attr('disabled',true);
166        }else{
167            $all.attr('disabled',true);
168            jQuery('#page__revisions input[type=submit]').attr('disabled',false);
169            for(var i=0; i<$checked.length; i++){
170                $checked[i].disabled = false;
171                if(i>1){
172                    $checked[i].checked = false;
173                }
174            }
175        }
176    }
177
178};
179
180jQuery(dw_behaviour.init);
181