Welcome to mirror list, hosted at ThFree Co, Russian Federation.

field-condition.js « directives « common « angularjs « CoreHome « plugins - github.com/matomo-org/matomo.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 5c4c33ff8e3ae935a80b0eb7ab2158debf164042 (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
/*!
 * Matomo - free/libre analytics platform
 *
 * @link https://matomo.org
 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
 */

/**
 * Usage:
 * <div piwik-form-condition>
 */
(function () {
    angular.module('piwikApp.directive').directive('fieldCondition', piwikFieldCondition);

    piwikFieldCondition.$inject = ['piwik', '$timeout'];

    /**
     * Unused.
     * @deprecated
     */
    function piwikFieldCondition(piwik, $timeout){

        function evaluate(scope, condition, element)
        {
            if (scope.$eval(condition, scope.allValues)) {
                element.show();
            } else {
                element.hide();
            }
        }

        function getValueFromElement(element)
        {
            if (element.attr('type') === 'checkbox') {
                return element.is(':checked');
            } else if (element.attr('type') === 'radio') {
                return $('.form-group [name=' + element.attr('name') + ']:checked').val();
            } else if (element.prop('tagName').toLowerCase() === 'select') {
                var name = element.val();
                if (name.indexOf('string:') === 0) {
                    return name.slice('string:'.length);
                }

                return name;
            }

            return element.val();
        }

        function evaluateConditionalExpression(scope, condition, element)
        {
            var fieldParts = condition.replace('!', '');
            fieldParts = fieldParts.split(' ');
            var fieldNames = [];
            fieldParts.forEach(function (name) {
                name = $.trim(name);
                if (name && name.length > 3) {
                    fieldNames.push(name);
                }
            });

            scope.allValues = {};
            angular.forEach(fieldNames, function (name) {
                var actualField = $('.form-group [name=' + name + ']').first();
                if (actualField.length) {
                    scope.allValues[name] = getValueFromElement(actualField);
                    actualField.on('change', function () {
                        scope.allValues[name] = getValueFromElement($(this));
                        evaluate(scope, condition, element);
                    });
                }
            });

            evaluate(scope, condition, element);
        }


        return {
            priority: 10, // makes sure to render after other directives, otherwise the content might be overwritten again see https://github.com/piwik/piwik/pull/8467
            restrict: 'A',
            link: function(scope, element, attrs) {

                var condition = attrs.fieldCondition;
                if (condition) {
                    $timeout(function (){
                        evaluateConditionalExpression(scope, condition, element);
                    });
                }
            },
        };
    }
})();