Completed
Pull Request — develop (#147)
by Wachter
14:07
created

property-configuration.js ➔ ... ➔ .generate   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 32

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 32
rs 8.8571
c 0
b 0
f 0
cc 2
nc 2
nop 1
1
/*
2
 * This file is part of the Sulu CMS.
3
 *
4
 * (c) MASSIVE ART WebServices GmbH
5
 *
6
 * This source file is subject to the MIT license that is bundled
7
 * with this source code in the file LICENSE.
8
 */
9
10
define(['jquery', 'underscore'], function($, _) {
11
12
    'use strict';
13
14
    var denormalizeTag = function(property, tag, tags) {
15
        if (!tags[tag.name]) {
16
            tags[tag.name] = {
17
                properties: {},
18
                highestProperty: property,
19
                highestPriority: tag.priority,
20
                lowestProperty: property,
21
                lowestPriority: tag.priority
22
            };
23
            tags[tag.name].properties[tag.priority] = [property];
24
25
            return;
26
        }
27
28
        if (!tags[tag.name].properties[tag.priority]) {
29
            tags[tag.name].properties[tag.priority] = [property];
30
        } else {
31
            tags[tag.name].properties[tag.priority].push(property);
32
        }
33
34
        // replace highest if priority is higher
35
        if (tags[tag.name].highestPriority < tag.priority) {
36
            tags[tag.name].highestProperty = property;
37
            tags[tag.name].highestPriority = tag.priority;
38
        }
39
40
        // replace lowest if priority is lower
41
        if (tags[tag.name].lowestPriority > tag.priority) {
42
            tags[tag.name].lowestProperty = property;
43
            tags[tag.name].lowestPriority = tag.priority;
44
        }
45
    };
46
47
    return {
48
        generate: function($form) {
49
            var $items = $form.find('*[data-property]');
50
51
            if ($items.length === 0) {
52
                return {};
53
            }
54
55
            var propertyConfiguration = {
56
                tags: {}
57
            };
58
59
            $items.each(function() {
60
                var $this = $(this),
61
                    property = $this.data('property');
62
63
                property.$el = $this;
64
65
                // remove property from dom
66
                $this.data('property', null);
67
                $this.removeAttr('data-property');
68
69
                propertyConfiguration[property.name] = property;
70
71
                _.each(property.tags, function(tag) {
72
                    denormalizeTag(property, tag, propertyConfiguration.tags);
73
                });
74
75
                return propertyConfiguration;
76
            });
77
78
            return propertyConfiguration;
79
        }
80
    };
81
});
82