Test Setup Failed
Push — master ( cd1dab...2a689d )
by
unknown
03:51
created

ProductShoppingListsView   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
define(function(require) {
2
    'use strict';
3
4
    var ProductShoppingListsView;
5
    var BaseView = require('oroui/js/app/views/base/view');
6
    var ElementsHelper = require('orofrontend/js/app/elements-helper');
7
    var _ = require('underscore');
8
    var $ = require('jquery');
9
    var mediator = require('oroui/js/mediator');
10
    var ShoppingListCollectionService = require('oroshoppinglist/js/shoppinglist-collection-service');
11
12
    ProductShoppingListsView = BaseView.extend(_.extend({}, ElementsHelper, {
13
        options: {
14
            template: ''
15
        },
16
17
        modelAttr: {
18
            shopping_lists: []
19
        },
20
21
        modelEvents: {
22
            shopping_lists: ['change', 'render']
23
        },
24
25
        shoppingListCollection: null,
26
27
        /**
28
         * @inheritDoc
29
         */
30
        constructor: function ProductShoppingListsView() {
31
            ProductShoppingListsView.__super__.constructor.apply(this, arguments);
32
        },
33
34
        /**
35
         * @inheritDoc
36
         */
37
        initialize: function(options) {
38
            ProductShoppingListsView.__super__.initialize.apply(this, arguments);
39
40
            this.options = _.defaults(options || {}, this.options);
41
            this.options.template = _.template(this.options.template);
42
43
            this.initModel(options);
44
            if (!this.model) {
45
                return;
46
            }
47
            this.initializeElements(options);
48
49
            ShoppingListCollectionService.shoppingListCollection.done((function(collection) {
50
                this.shoppingListCollection = collection;
51
                this.listenTo(collection, 'change', this.render);
52
                this.render();
53
            }).bind(this));
54
        },
55
56
        initModel: function(options) {
57
            var modelAttr = _.each(options.modelAttr, function(value, attribute) {
58
                options.modelAttr[attribute] = value === 'undefined' ? undefined : value;
59
            }) || {};
60
            this.modelAttr = $.extend(true, {}, this.modelAttr, modelAttr);
61
62
            if (options.productModel) {
63
                this.model = options.productModel;
64
            }
65
66
            _.each(this.modelAttr, function(value, attribute) {
67
                if (!this.model.has(attribute) || modelAttr[attribute] !== undefined) {
68
                    this.model.set(attribute, value);
69
                }
70
            }, this);
71
72
            if (this.model.get('shopping_lists') === undefined) {
73
                this.model.set('shopping_lists', []);
74
            }
75
        },
76
77
        dispose: function() {
78
            this.disposeElements();
79
            delete this.shoppingListCollection;
80
            ProductShoppingListsView.__super__.dispose.apply(this, arguments);
81
        },
82
83
        delegateEvents: function() {
84
            ProductShoppingListsView.__super__.delegateEvents.apply(this, arguments);
85
            this.delegateElementsEvents();
86
        },
87
88
        undelegateEvents: function() {
89
            this.undelegateElementsEvents();
90
            return ProductShoppingListsView.__super__.undelegateEvents.apply(this, arguments);
91
        },
92
93
        render: function() {
94
            this.clearElementsCache();
95
            this.updateShoppingLists();
96
            this.initLayout({
97
                options: {
98
                    productModel: this.model
99
                }
100
            });
101
            mediator.trigger('layout:adjustHeight');
102
        },
103
104
        updateShoppingLists: function() {
105
            var modelShoppingLists = this.model.get('shopping_lists');
106
            var $el = $(this.options.template({
107
                currentShoppingList: this.findCurrentShoppingList(modelShoppingLists),
108
                shoppingListsCount: modelShoppingLists && modelShoppingLists.length || 0
109
            }));
110
111
            this.$el.html($el);
112
            this.delegateEvents();
113
        },
114
115
        findCurrentShoppingList: function(modelShoppingLists) {
116
            var current = _.find(modelShoppingLists, function(list) {
117
                var model = this.shoppingListCollection.get(list.id);
118
                return model && model.get('is_current');
119
            }, this) || null;
120
            if (!current) {
121
                return null;
122
            }
123
            return _.extend(
124
                {},
125
                {line_items: current.line_items},
126
                this.shoppingListCollection.get(current.id).toJSON()
127
            );
128
        }
129
    }));
130
131
    return ProductShoppingListsView;
132
});
133