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

field-array.controller.js « field-array « angularjs « CoreHome « plugins - github.com/matomo-org/matomo.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 40f3f3ca74d5c5c5ba69c9d0d735047dd3db601f (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
/*!
 * Piwik - free/libre analytics platform
 *
 * @link http://piwik.org
 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
 */
(function () {
    angular.module('piwikApp').controller('FieldArrayController', FieldArrayController);

    FieldArrayController.$inject = ['$scope'];

    function FieldArrayController($scope){

        function getTemplate(field) {
            var control = field.uiControl;
            if (control === 'password' || control === 'url' || control === 'search' || control === 'email') {
                control = 'text'; // we use same template for text and password both
            }

            var file = 'field-' + control;
            var fieldsSupportingArrays = ['textarea', 'checkbox', 'text'];
            if (field.type === 'array' && fieldsSupportingArrays.indexOf(control) !== -1) {
                file += '-array';
            }

            return 'plugins/CorePluginsAdmin/angularjs/form-field/' + file + '.html?cb=' + piwik.cacheBuster;
        }

        if ($scope.field && !$scope.field.templateFile) {
            $scope.field.templateFile = getTemplate($scope.field);
        }

        var self = this;
        $scope.$watch('formValue', function () {
            if (!$scope.formValue || !$scope.formValue.length) {
                self.addEntry();
            } else {
                self.onEntryChange();
            }
        }, true);

        this.onEntryChange = function () {
            var hasAny = true;
            angular.forEach($scope.formValue, function (entry) {
                if (!entry) {
                    hasAny = false;
                }
            });
            if (hasAny) {
                this.addEntry();
            }
        };

        this.addEntry = function () {
            if (angular.isArray($scope.formValue)) {
                $scope.formValue.push('');
            }
        };

        this.removeEntry = function (index) {
            if (index > -1) {
                $scope.formValue.splice(index, 1);
            }
        };

        if (!$scope.formValue || !$scope.formValue.length) {
            this.addEntry();
        }
    }

})();