xref: /dokuwiki/lib/scripts/behaviour.js (revision c949174a2e8c324e3e463a9d10e9e6dc07b0ba9e)
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 */
11
12var dw_behaviour = {
13
14    init: function(){
15        dw_behaviour.focusMarker();
16        dw_behaviour.scrollToMarker();
17        dw_behaviour.closeMsgOnClick();
18        dw_behaviour.removeHighlightOnClick();
19        dw_behaviour.quickSelect();
20        dw_behaviour.checkWindowsShares();
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     * Close messages shown by the msg() PHP function by click
42     */
43    closeMsgOnClick: function(){
44        jQuery('div.success, div.info, div.error, div.notify').click(
45            function(e){
46                jQuery(e.target).fadeOut('fast');
47            }
48        );
49    },
50
51    /**
52     * Remove all search highlighting when clicking on a highlighted term
53     *
54     * @FIXME would be nice to have it fade out
55     */
56    removeHighlightOnClick: function(){
57        jQuery('span.search_hit').click(
58            function(e){
59                jQuery(e.target).removeClass('search_hit');
60            }
61        );
62    },
63
64    /**
65     * Autosubmit quick select forms
66     *
67     * When a <select> tag has the class "quickselect", this script will
68     * automatically submit its parent form when the select value changes.
69     * It also hides the submit button of the form.
70     *
71     * @author Andreas Gohr <andi@splitbrain.org>
72     */
73    quickSelect: function(){
74        jQuery('select.quickselect')
75            .change(function(e){ e.target.form.submit(); })
76            .parents('form').find('input[type=submit]').hide();
77    },
78
79    /**
80     * Display error for Windows Shares on browsers other than IE
81     *
82     * @author Michael Klier <chi@chimeric.de>
83     */
84    checkWindowsShares: function() {
85        if(!LANG.nosmblinks || document.all !== null) {
86            // No warning requested or none necessary
87            return;
88        }
89
90        jQuery('a.windows').live('click', function(){
91            alert(LANG.nosmblinks);
92        });
93    }
94
95};
96
97jQuery(dw_behaviour.init);
98