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