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