Test Setup Failed
Push — master ( d60e73...a71a5d )
by
unknown
04:37 queued 10s
created

AbstractWidgetView.extend._delegateWidgetEvents   C

Complexity

Conditions 7
Paths 7

Size

Total Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
nc 7
dl 0
loc 28
rs 6.7272
c 0
b 0
f 0
nop 1
1
define(['underscore', 'backbone', 'oroui/js/widget/abstract-widget'
2
], function(_, Backbone, AbstractWidgetView) {
3
    'use strict';
4
5
    var $ = Backbone.$;
6
    var BlockWidgetView;
7
8
    /**
9
     * @export  oro/block-widget
10
     * @class   oro.BlockWidgetView
11
     * @extends oroui.widget.AbstractWidgetView
12
     */
13
    BlockWidgetView = AbstractWidgetView.extend({
14
        options: _.extend({}, AbstractWidgetView.prototype.options, {
15
            type: 'block',
16
            cssClass: '',
17
            title: null,
18
            titleBlock: '.title',
19
            titleContainer: '.widget-title',
20
            actionsContainer: '.widget-actions-container',
21
            contentContainer: '.row-fluid',
22
            contentClasses: [],
23
            templateParams: {},
24
            template: _.template('<div class="box-type1" data-layout="separate">' +
25
                '<div class="title"<% if (_.isNull(title)) { %>style="display: none;"<% } %>>' +
26
                    '<div class="pull-right widget-actions-container"></div>' +
27
                    '<span class="widget-title"><%- title %></span>' +
28
                '</div>' +
29
                '<div class="row-fluid <%= contentClasses.join(\' \') %>"></div>' +
30
            '</div>')
31
        }),
32
33
        /**
34
         * @inheritDoc
35
         */
36
        constructor: function BlockWidgetView() {
37
            BlockWidgetView.__super__.constructor.apply(this, arguments);
38
        },
39
40
        initialize: function(options) {
41
            options = options || {};
42
            this.options = _.defaults(options, this.options);
43
44
            if (!_.isFunction(this.options.template)) {
45
                this.options.template = _.template(this.options.template);
46
            }
47
            var params = _.extend({
48
                title: this.options.title,
49
                contentClasses: this.options.contentClasses
50
            }, this.options.templateParams);
51
            this.widget = $(this.options.template(params));
52
            this.widget.addClass(this.options.cssClass);
53
            this.widgetContentContainer = this.widget.find(this.options.contentContainer);
54
            this.initializeWidget(options);
55
            this.delegateEvents();
56
        },
57
58
        setTitle: function(title) {
59
            if (_.isNull(this.options.title)) {
60
                this._getTitleContainer().closest(this.options.titleBlock).show();
61
            }
62
            this.options.title = title;
63
            this._getTitleContainer().text(this.options.title).attr('title', this.options.title);
64
        },
65
66
        getActionsElement: function() {
67
            if (this.actionsContainer === undefined) {
68
                this.actionsContainer = this.widget.find(this.options.actionsContainer);
69
            }
70
            return this.actionsContainer;
71
        },
72
73
        _getTitleContainer: function() {
74
            if (this.titleContainer === undefined) {
75
                this.titleContainer = this.widget.find(this.options.titleContainer);
76
            }
77
            return this.titleContainer;
78
        },
79
80
        /**
81
         * Remove widget
82
         */
83
        remove: function() {
84
            AbstractWidgetView.prototype.remove.call(this);
85
            this.widget.remove();
86
        },
87
88
        show: function() {
89
            if (!this.$el.data('wid')) {
90
                if (this.$el.parent().length) {
91
                    this._showStatic();
92
                } else {
93
                    this._showRemote();
94
                }
95
            }
96
            this.loadingElement = this.widgetContentContainer.parent();
97
            AbstractWidgetView.prototype.show.apply(this);
98
        },
99
100
        _showStatic: function() {
101
            var anchorId = '_widget_anchor-' + this.getWid();
102
            var anchorDiv = $('<div id="' + anchorId + '"/>');
103
            anchorDiv.insertAfter(this.$el);
104
            this.widgetContentContainer.append(this.$el);
105
            $('#' + anchorId).replaceWith($(this.widget));
106
        },
107
108
        _showRemote: function() {
109
            this.widgetContentContainer.empty();
110
            this.widgetContentContainer.append(this.$el);
111
        },
112
113
        delegateEvents: function(events) {
114
            AbstractWidgetView.prototype.delegateEvents.apply(this, arguments);
115
            if (this.widget) {
116
                this._delegateWidgetEvents(events);
117
            }
118
        },
119
120
        _delegateWidgetEvents: function(events) {
121
            var delegateEventSplitter = /^(\S+)\s*(.*)$/;
122
            if (!(events || (events = _.result(this, 'widgetEvents')))) {
123
                return;
124
            }
125
            this._undelegateWidgetEvents();
126
            for (var key in events) {
127
                if (events.hasOwnProperty(key)) {
128
                    var method = events[key];
129
                    if (!_.isFunction(method)) {
130
                        method = this[events[key]];
131
                    }
132
                    if (!method) {
133
                        throw new Error('Method "' + events[key] + '" does not exist');
134
                    }
135
                    var match = key.match(delegateEventSplitter);
136
                    var eventName = match[1];
137
                    var selector = match[2];
138
                    method = _.bind(method, this);
139
                    eventName += '.delegateWidgetEvents' + this.cid;
140
                    if (selector === '') {
141
                        this.widget.on(eventName, method);
142
                    } else {
143
                        this.widget.on(eventName, selector, method);
144
                    }
145
                }
146
            }
147
        },
148
149
        _undelegateWidgetEvents: function() {
150
            this.widget.off('.delegateWidgetEvents' + this.cid);
151
        }
152
    });
153
154
    return BlockWidgetView;
155
});
156