aboutsummaryrefslogtreecommitdiff
path: root/resources/assets/javascripts/studip-jquery-tweaks.js
blob: 7527b2c4006f682aa2374d6f892bf517c5544255 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
/**
 * SVG class handling.
 *
 * This tweaks jQuery so that calls of addClass(), removeClass() and hasClass()
 * don't fail on svg elements.
 *
 * SVGs don't have a className attribute but rather a classList object
 * so the native jQuery methods will have no effect on SVG elements.
 */
(function ($) {
    'use strict';

    var originals = {
        addClass: $.fn.addClass,
        removeClass: $.fn.removeClass,
        hasClass: $.fn.hasClass
    };

    $.fn.addClass = function (value) {
        if (jQuery.isFunction(value)) {
            return originals.addClass.call(this, value);
        }

        this.filter('svg').each(function () {
            var classes = (value || '').trim().split(/\s+/) || [];

            this.classList.add.apply(this.classList, classes);
        });
        originals.addClass.call(this.not('svg'), value);

        return this;
    };

    $.fn.removeClass = function (value) {
        if (jQuery.isFunction(value)) {
            return originals.removeClass.call(this, value);
        }

        this.filter('svg').each(function () {
            var classes = (value || '').trim().split(/\s+/) || [];

            this.classList.remove.apply(this.classList, classes);
        });
        originals.removeClass.call(this.not('svg'), value);

        return this;
    };

    $.fn.hasClass = function (value) {
        var svgs = $(this).filter('svg'),
            i,
            l = svgs.length;
        if (l > 0) {
            for (i = 0; i < l; i += 1) {
                if (svgs.get(i).classList.contains(value)) {
                    return true;
                }
            }
            return false;
        }
        return originals.hasClass.call(this, value);
    };

}(jQuery));