summaryrefslogtreecommitdiff
path: root/html/assets/js/require/AttributePlugin.js
blob: d0568a20faef0f31f5c69a16b3a1a4831ced5f92 (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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
require('jQuery', function ($) {

    function debounce(func, wait, immediate) {
        var timeout;
        return function () {
            var context = this, args = arguments;
            var later = function () {
                timeout = null;
                if (!immediate) func.apply(context, args);
            };
            var callNow = immediate && !timeout;
            clearTimeout(timeout);
            timeout = setTimeout(later, wait);
            if (callNow) func.apply(context, args);
        };
    };

    function init($element, pluginName, pluginConfiguration, callback) {
        if (typeof (pluginName) === 'undefined') {
            return;
        }

        require([pluginName], function (plugin) {
            if (typeof (plugin) === 'undefined') {
                throw 'plugin ' + pluginName + ' is undefined';
            }
            plugin($element, pluginConfiguration);
        });
    }


    var rbrace = /^(?:\{[\w\W]*\}|\[[\w\W]*\])$/;
    function parseOptions(data, element) {
        if (data === "true") {
            return true;
        }

        if (data === "false") {
            return false;
        }

        if (data === "null") {
            return null;
        }

        // Only convert to a number if it doesn't change the string
        if (data === +data + "") {
            return +data;
        }

        if (rbrace.test(data)) {
            return tryParseJson(data, element);
        }

        return data;
    }
    function tryParseJson(json, element) {
        try {
            return JSON.parse(json)
        }
        catch (err) {
            console.warn(element, 'error during parsing of json', json, err);
            return json;
        }
    }

    function attach($container) {
        $container.find('*').addBack().each(function () {
            var domNode = this;
            var attributeNamePrefix = "data-plugin-";
            var plugins = [];
            var initialized = domNode.initializedPlugins = (domNode.initializedPlugins || {});
            var $element = $(this);
            for (var i = 0; i < domNode.attributes.length; i++) {
                var attribute = domNode.attributes[i];

                if (attribute.name.indexOf(attributeNamePrefix) != 0) {
                    continue;
                }
                if (initialized[attribute.name]) {
                    continue;
                }

                var plugin = { name: attribute.name.substring(attributeNamePrefix.length), options: parseOptions(attribute.value, domNode) };
                init($element, plugin.name, plugin.options);
                initialized[attribute.name] = true;
            }
        });
    }
    $.fn.parseAttributePlugins = function () {
        attach($(this));
        return $(this);
    };
    var attachFn = debounce(function () {
        attach($('body'));
    }, 100);

    $(function () {
        attachFn();
    });
    $('body').on('contentInjected', function (e) {
        attachFn();
    });
    $.defineAttributePlugin = function (pluginName, dependencies, plugin) {
        if (typeof (plugin) == 'undefined') {
            plugin = dependencies;
            dependencies = [];
        }
        define(pluginName, dependencies, function () {
            var dependencyValues = arguments;
            if (dependencies.length) {
                return function ($element, options) {
                    var args = [$element, options];
                    for (var i = 0; i < dependencyValues.length; i++) {
                        args.push(dependencyValues[i]);
                    }

                    plugin.apply(this, args);
                };
            }
            return function ($element, options) {

                plugin($element, options);
            };
        });
    };

    $.fn.executePlugin = function (pluginName, pluginConfiguration, callback) {
        return $(this).each(function () {
            var $element = $(this);
            init($element, pluginName, pluginConfiguration, function () {
                if (typeof (callback) !== 'undefined') {
                    callback.apply($element, []);
                }
            });
        });
    };
});