Completed
Pull Request — develop (#233)
by Wachter
45:40 queued 30:46
created

main.js ➔ ... ➔ .initialize   B

Complexity

Conditions 1
Paths 6

Size

Total Lines 100

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 100
rs 8.2857
c 0
b 0
f 0
cc 1
nc 6
nop 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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
/**
11
 * Handles articles for teaser-selection.
12
 *
13
 * @class ListArticleTeaser
14
 * @constructor
15
 */
16
define(['underscore', 'config', 'services/suluarticle/filter-helper'], function(_, Config, filterHelper) {
17
18
    'use strict';
19
20
    var defaults = {
21
            options: {
22
                locale: null,
23
                url: '',
24
                resultKey: null,
25
                searchFields: [],
26
                instanceName: 'teaser-selection',
27
                selectCallback: function(item) {
0 ignored issues
show
Unused Code introduced by
The parameter item is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
28
                }
29
            },
30
            templates: {
31
                skeleton: [
32
                    '<div class="teaser-selection-tabs"></div>',
33
                    '<div class="grid">',
34
                    '   <div class="grid-row search-row">',
35
                    '       <div class="grid-col-12 teaser-selection-search"/>',
36
                    '   </div>',
37
                    '   <div class="grid-row">',
38
                    '       <div class="grid-col-12 teaser-selection-list"/>',
39
                    '   </div>',
40
                    '</div>'
41
                ].join(''),
42
                draftIcon: '<span class="draft-icon" title="<%= title %>"/>',
43
            },
44
            translations: {
45
                filterAll: 'sulu_article.list.filter.all',
46
                filterByTimescale: 'sulu_article.list.filter.by-timescale',
47
                from: 'sulu_article.authored-selection-overlay.from',
48
                to: 'sulu_article.authored-selection-overlay.to',
49
            }
50
        },
51
52
        config = Config.get('sulu_article'),
53
54
        getTabsData = function() {
55
            if (1 === config.typeNames.length) {
56
                return [];
57
            }
58
59
            var tabsData = [];
60
61
            if (config.displayTabAll === true) {
62
                tabsData.push(
63
                    {
64
                        id: 'all',
65
                        name: 'public.all',
66
                    }
67
                );
68
            }
69
70
            // add tab item for each type
71
            _.each(config.typeNames, function(type) {
72
                tabsData.push(
73
                    {
74
                        id: type,
75
                        name: config.types[type].title,
76
                    }
77
                );
78
            }.bind(this));
0 ignored issues
show
unused-code introduced by
The call to bind does not seem necessary since the function does not use this. Consider calling it directly.
Loading history...
79
80
            return tabsData;
81
        };
82
83
    return {
84
        defaults: defaults,
85
86
        initialize: function() {
87
            this.$el.parent().removeClass('content-spacing');
88
            this.$el.parent().addClass('article-teaser-selection');
89
90
            var $container = $(this.templates.skeleton());
91
            this.$el.append($container);
92
93
            var toolbar = this.retrieveListToolbarTemplate();
94
            this.sandbox.sulu.initListToolbarAndList.call(this,
95
                'article',
96
                '/admin/api/articles/fields',
97
                {
98
                    el: '.teaser-selection-search',
99
                    instanceName: this.options.instanceName,
100
                    template: toolbar
101
                },
102
                {
103
                    el: '.teaser-selection-list',
104
                    instanceName: this.options.instanceName,
105
                    url: this.getUrl(),
106
                    preselected: _.map(this.options.data, function(item) {
107
                        return item.id;
108
                    }),
109
                    resultKey: this.options.resultKey,
110
                    clickCallback: function(item) {
111
                        this.sandbox.emit('husky.datagrid.teaser-selection.toggle.item', item);
112
                    }.bind(this),
113
                    searchInstanceName: this.options.instanceName,
114
                    searchFields: this.options.searchFields,
115
                    paginationOptions: {
116
                        dropdown: {
117
                            limit: 20
118
                        }
119
                    },
120
                    viewOptions: {
121
                        table: {
122
                            actionIconColumn: 'title',
123
                            badges: [
124
                                {
125
                                    column: 'title',
126
                                    callback: function(item, badge) {
127
                                        if (!!item.localizationState &&
128
                                            item.localizationState.state === 'ghost' &&
129
                                            item.localizationState.locale !== this.options.locale
130
                                        ) {
131
                                            badge.title = item.localizationState.locale;
132
133
                                            return badge;
134
                                        }
135
136
                                        return false;
137
                                    }.bind(this)
138
                                },
139
                                {
140
                                    column: 'title',
141
                                    callback: function(item, badge) {
142
                                        var icons = '',
143
                                            tooltip = this.translations.unpublished;
144
145
                                        if (!!item.published && !item.publishedState) {
146
                                            tooltip = this.translations.publishedWithDraft;
147
                                            icons += this.templates.publishedIcon({title: tooltip});
148
                                        }
149
                                        if (!item.publishedState) {
150
                                            icons += this.templates.draftIcon({title: tooltip});
151
                                        }
152
153
                                        badge.title = icons;
154
                                        badge.cssClass = 'badge-none';
155
156
                                        return badge;
157
                                    }.bind(this)
158
                                }
159
                            ]
160
                        }
161
                    },
162
                }
163
            );
164
165
            this.sandbox.start([
166
                {
167
                    name: 'tabs@husky',
168
                    options: {
169
                        el: '.teaser-selection-tabs',
170
                        data: getTabsData(),
171
                        callback: this.changeType.bind(this)
172
                    }
173
                },
174
                {
175
                    name: 'articles/list/authored-selection/form@suluarticle',
176
                    options: {
177
                        el: '.slide.authored-slide .overlay-content',
178
                        data: this.options.data,
179
                        selectCallback: this.closeAuthoredSelection.bind(this)
180
                    }
181
                }
182
            ]);
183
184
            this.bindCustomEvents();
185
        },
186
187
        bindCustomEvents: function() {
188
            this.sandbox.on('husky.datagrid.' + this.options.instanceName + '.item.select', function(id) {
189
                this.options.selectCallback({type: this.options.type, id: id});
190
            }.bind(this));
191
            this.sandbox.on('husky.datagrid.' + this.options.instanceName + '.item.deselect', function(id) {
192
                this.options.deselectCallback({type: this.options.type, id: id});
193
            }.bind(this));
194
        },
195
196
        getUrl: function() {
197
            return this.options.url.replace('{locale}', this.options.locale);
198
        },
199
200
        changeType: function(item) {
201
            var type = item.id;
202
            if (item.id === 'all') {
203
                type = null;
204
            }
205
206
            this.sandbox.emit('husky.datagrid.' + this.options.instanceName + '.url.update', {type: type});
207
        },
208
209
        /**
210
         * Generates list toolbar buttons.
211
         */
212
        retrieveListToolbarTemplate: function() {
213
            return this.sandbox.sulu.buttons.get({
214
                authoredDate: {
215
                    options: {
216
                        icon: 'calendar',
217
                        group: 2,
218
                        title: this.translations.filterAll,
219
                        showTitle: true,
220
                        dropdownOptions: {
221
                            idAttribute: 'id',
222
                            markSelected: false
223
                        },
224
                        dropdownItems: [
225
                            {
226
                                title: this.translations.filterAll,
227
                                callback: this.closeAuthoredSelection.bind(this)
228
                            },
229
                            {
230
                                id: 'timescale',
231
                                title: this.translations.filterByTimescale,
232
                                callback: this.openAuthoredSelection.bind(this)
233
                            }
234
                        ]
235
                    }
236
                }
237
            });
238
        },
239
240
        openAuthoredSelection: function() {
241
            this.$el.parent().addClass('limited');
242
            this.sandbox.emit('husky.overlay.' + this.options.instanceName + '.slide-to', 1);
243
244
            this.sandbox.once('sulu_content.teaser-selection.' + this.options.instanceName + '.ok-button.clicked', function() {
245
                this.sandbox.emit('sulu_article.authored-selection.form.get');
246
            }.bind(this));
247
        },
248
249
        closeAuthoredSelection: function(data) {
250
            this.$el.parent().removeClass('limited');
251
252
            this.sandbox.emit('husky.datagrid.' + this.options.instanceName + '.url.update', {
253
                authoredFrom: data ? data.from : null,
254
                authoredTo: data ? data.to : null,
255
            });
256
257
            this.sandbox.emit(
258
                'husky.toolbar.' + this.options.instanceName + '.button.set',
259
                'authoredDate',
260
                {title: filterHelper.getAuthoredTitle(data)}
261
            );
262
263
            this.sandbox.emit('husky.overlay.' + this.options.instanceName + '.slide-to', 0);
264
        }
265
    };
266
});
267