Test Setup Failed
Push — master ( ae6bbc...554531 )
by
unknown
09:25 queued 04:41
created

view.js ➔ ... ➔ WidgetContainerView   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
nop 0
1
define(function(require) {
2
    'use strict';
3
4
    var WidgetContainerView;
5
    var _ = require('underscore');
6
    var $ = require('jquery');
7
    var Backbone = require('backbone');
8
    var constants = require('../constants');
9
    var widgetMinTemplate = require('tpl!./templates/widget-min-template.html');
10
    var widgetMaxTemplate = require('tpl!./templates/widget-max-template.html');
11
    var widgetIconTemplate = require('tpl!./templates/icon-template.html');
12
    var BaseView = require('oroui/js/app/views/base/view');
13
14
    WidgetContainerView = BaseView.extend({
15
        templateMin: widgetMinTemplate,
16
        templateMax: widgetMaxTemplate,
17
        templateIcon: widgetIconTemplate,
18
19
        events: {
20
            'click .sidebar-widget-header-toggle': 'onClickToggle',
21
            'click .sidebar-widget-refresh': 'onClickRefresh',
22
            'click .sidebar-widget-settings': 'onClickSettings',
23
            'click .sidebar-widget-remove': 'onClickRemove',
24
            'click .sidebar-widget-close': 'onClickClose'
25
        },
26
27
        listen: {
28
            'change model': 'render',
29
            'start-loading model': 'onLoadingStart',
30
            'end-loading model': 'onLoadingEnd',
31
            'layout:reposition mediator': 'adjustMaxHeight'
32
        },
33
34
        /**
35
         * @inheritDoc
36
         */
37
        constructor: function WidgetContainerView() {
38
            WidgetContainerView.__super__.constructor.apply(this, arguments);
39
        },
40
41
        render: function() {
42
            this.$el.attr('data-cid', this.model.cid);
43
44
            if (this.model.get('cssClass')) {
45
                this.$el.attr('class', this.model.get('cssClass'));
46
            }
47
48
            if (this.model.get('state') === constants.WIDGET_MAXIMIZED_HOVER) {
49
                this.$el.addClass('sidebar-widget-popup');
50
            } else {
51
                this.$el.addClass('sidebar-widget-embedded');
52
            }
53
54
            this.$el.toggleClass('sidebar-highlight', this.model.get('highlighted'));
55
56
            this.model.loadModule().then(_.bind(this._deferredRender, this));
57
58
            return this;
59
        },
60
61
        getTemplateFunction: function() {
62
            var template = this.templateMax;
63
            if (this.model.get('state') === constants.WIDGET_MINIMIZED) {
64
                template = this.templateMin;
65
            }
66
            return template;
67
        },
68
69
        getTemplateData: function() {
70
            var data = WidgetContainerView.__super__.getTemplateData.call(this);
71
            if (this.model.module.titleTemplate) {
72
                data.title = this.model.module.titleTemplate(data);
73
            }
74
            data.icon = this.templateIcon(data);
75
            return data;
76
        },
77
78
        /**
79
         * Renders the widget content once widget module is loaded
80
         *
81
         * @protected
82
         */
83
        _deferredRender: function() {
84
            WidgetContainerView.__super__.render.call(this);
85
86
            if (this.model.get('state') !== constants.WIDGET_MINIMIZED) {
87
                if (this.contentView) {
88
                    this.contentView.dispose();
89
                }
90
                this.contentView = new this.model.module.ContentView({
91
                    autoRender: true,
92
                    model: this.model,
93
                    el: this.$('.sidebar-widget-content')
94
                });
95
            }
96
        },
97
98
        setOffset: function(cord) {
99
            var view = this;
100
            view.$el.offset(cord);
101
        },
102
103
        adjustMaxHeight: function() {
104
            var rect;
105
            var contentMargin;
106
            var $content;
107
            var windowHeight;
108
            if (this.contentView) {
109
                $content = this.contentView.$el;
110
                windowHeight = $('html').height();
111
                if (this.model.get('state') === constants.WIDGET_MAXIMIZED_HOVER) {
112
                    rect = $content[0].getBoundingClientRect();
113
                    contentMargin = $content.outerHeight(true) - rect.height;
114
                    $content.css('max-height', windowHeight - rect.top - contentMargin + 'px');
115
                } else {
116
                    $content.css('max-height', 'none');
117
                }
118
            }
119
        },
120
121
        onClickToggle: function(e) {
122
            e.stopPropagation();
123
            e.preventDefault();
124
125
            this.model.toggleState();
126
            this.model.save();
127
        },
128
129
        onClickRefresh: function(e) {
130
            e.stopPropagation();
131
            e.preventDefault();
132
133
            Backbone.trigger('refreshWidget', this.model.cid);
134
        },
135
136
        onClickSettings: function(e) {
137
            e.stopPropagation();
138
            e.preventDefault();
139
140
            Backbone.trigger('setupWidget', this.model.cid);
141
        },
142
143
        onClickRemove: function(e) {
144
            e.stopPropagation();
145
            e.preventDefault();
146
            Backbone.trigger('removeWidget', this.model.cid);
147
        },
148
149
        onClickClose: function(e) {
150
            e.stopPropagation();
151
            e.preventDefault();
152
            Backbone.trigger('closeWidget', this.model.cid);
153
        },
154
155
        onLoadingStart: function() {
156
            this.$('.sidebar-widget-header-icon').addClass('loading');
157
        },
158
159
        onLoadingEnd: function() {
160
            this.$('.sidebar-widget-header-icon').removeClass('loading');
161
            this.adjustMaxHeight();
162
        }
163
    });
164
165
    return WidgetContainerView;
166
});
167