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