Completed
Pull Request — develop (#236)
by Wachter
21:30 queued 06:18
created

main.js ➔ ... ➔ .openContactSelection   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
cc 1
nc 1
nop 0
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/list-helper'], function(_, Config, listHelper) {
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
            },
43
            translations: {
44
                filterAll: 'sulu_article.list.filter.all',
45
                filterByTimescale: 'sulu_article.list.filter.by-timescale',
46
                published: 'public.published',
47
                unpublished: 'public.unpublished',
48
                filterMe: 'sulu_article.list.filter.me',
49
                filterByAuthor: 'sulu_article.list.filter.by-author',
50
                filterByCategory: 'sulu_article.list.filter.by-category',
51
                filterByTag: 'sulu_article.list.filter.by-tag',
52
                filterByPage: 'sulu_article.list.filter.by-page',
53
            }
54
        },
55
56
        config = Config.get('sulu_article'),
57
58
        getTabsData = function() {
59
            if (1 === config.typeNames.length) {
60
                return [];
61
            }
62
63
            var tabsData = [];
64
65
            if (config.displayTabAll === true) {
66
                tabsData.push(
67
                    {
68
                        id: 'all',
69
                        name: 'public.all',
70
                    }
71
                );
72
            }
73
74
            // add tab item for each type
75
            _.each(config.typeNames, function(type) {
76
                tabsData.push(
77
                    {
78
                        id: type,
79
                        name: config.types[type].title,
80
                    }
81
                );
82
            }.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...
83
84
            return tabsData;
85
        };
86
87
    return {
88
89
        defaults: defaults,
90
91
        filter: {},
92
93
        initialize: function() {
94
            this.$el.parent().removeClass('content-spacing');
95
            this.$el.parent().addClass('article-teaser-selection');
96
97
            var $container = $(this.templates.skeleton());
98
            this.$el.append($container);
99
100
            var toolbar = this.retrieveListToolbarTemplate();
101
            this.sandbox.sulu.initListToolbarAndList.call(this,
102
                'article',
103
                '/admin/api/articles/fields',
104
                {
105
                    el: '.teaser-selection-search',
106
                    instanceName: this.options.instanceName,
107
                    template: toolbar
108
                },
109
                {
110
                    el: '.teaser-selection-list',
111
                    instanceName: this.options.instanceName,
112
                    url: this.getUrl(),
113
                    preselected: _.map(this.options.data, function(item) {
114
                        return item.id;
115
                    }),
116
                    resultKey: this.options.resultKey,
117
                    clickCallback: function(item) {
118
                        this.sandbox.emit('husky.datagrid.teaser-selection.toggle.item', item);
119
                    }.bind(this),
120
                    searchInstanceName: this.options.instanceName,
121
                    searchFields: this.options.searchFields,
122
                    paginationOptions: {
123
                        dropdown: {
124
                            limit: 20
125
                        }
126
                    },
127
                    viewOptions: {
128
                        table: {
129
                            actionIconColumn: 'title',
130
                            badges: [
131
                                {
132
                                    column: 'title',
133
                                    callback: function(item, badge) {
134
                                        return listHelper.generateLocalizationBadge(item, badge, this.options.locale);
135
                                    }.bind(this)
136
                                },
137
                                {
138
                                    column: 'title',
139
                                    callback: listHelper.generateWorkflowBadge
140
                                }
141
                            ]
142
                        }
143
                    },
144
                }
145
            );
146
147
            this.sandbox.start([
148
                {
149
                    name: 'tabs@husky',
150
                    options: {
151
                        el: '.teaser-selection-tabs',
152
                        data: getTabsData(),
153
                        callback: this.changeType.bind(this)
154
                    }
155
                },
156
                {
157
                    name: 'articles/list/authored-selection/form@suluarticle',
158
                    options: {
159
                        el: '.slide.authored-slide .overlay-content',
160
                        data: this.options.data,
161
                        selectCallback: this.closeAuthoredSelection.bind(this)
162
                    }
163
                },
164
                {
165
                    name: 'articles/list/contact-selection/form@suluarticle',
166
                    options: {
167
                        el: '.slide.contact-slide .overlay-content',
168
                        data: this.options.data,
169
                        selectCallback: this.closeContactSelection.bind(this)
170
                    }
171
                },
172
                {
173
                    name: 'articles/list/category-selection/form@suluarticle',
174
                    options: {
175
                        el: '.slide.category-slide .overlay-content',
176
                        locale: this.options.locale,
177
                        data: this.options.data,
178
                        selectCallback: this.closeCategorySelection.bind(this)
179
                    }
180
                },
181
                {
182
                    name: 'articles/list/tag-selection/form@suluarticle',
183
                    options: {
184
                        el: '.slide.tag-slide .overlay-content',
185
                        data: this.options.data,
186
                        selectCallback: this.closeTagSelection.bind(this)
187
                    }
188
                }
189
            ]);
190
191
            this.bindCustomEvents();
192
        },
193
194
        bindCustomEvents: function() {
195
            this.sandbox.on('husky.datagrid.' + this.options.instanceName + '.item.select', function(id) {
196
                this.options.selectCallback({type: this.options.type, id: id});
197
            }.bind(this));
198
            this.sandbox.on('husky.datagrid.' + this.options.instanceName + '.item.deselect', function(id) {
199
                this.options.deselectCallback({type: this.options.type, id: id});
200
            }.bind(this));
201
        },
202
203
        getUrl: function() {
204
            return this.options.url.replace('{locale}', this.options.locale);
205
        },
206
207
        changeType: function(item) {
208
            var type = item.id;
209
            if (item.id === 'all') {
210
                type = null;
211
            }
212
213
            this.sandbox.emit('husky.datagrid.' + this.options.instanceName + '.url.update', {type: type});
214
        },
215
216
        /**
217
         * Generates list toolbar buttons.
218
         */
219
        retrieveListToolbarTemplate: function() {
220
            return this.sandbox.sulu.buttons.get({
221
                authoredDate: {
222
                    options: {
223
                        icon: 'calendar',
224
                        group: 2,
225
                        title: this.translations.filterAll,
226
                        showTitle: true,
227
                        dropdownOptions: {
228
                            idAttribute: 'id',
229
                            markSelected: false
230
                        },
231
                        dropdownItems: [
232
                            {
233
                                title: this.translations.filterAll,
234
                                callback: this.closeAuthoredSelection.bind(this)
235
                            },
236
                            {
237
                                id: 'timescale',
238
                                title: this.translations.filterByTimescale,
239
                                callback: this.openAuthoredSelection.bind(this)
240
                            }
241
                        ]
242
                    }
243
                },
244
                workflowStage: {
245
                    options: {
246
                        icon: 'circle-o',
247
                        group: 2,
248
                        title: listHelper.getPublishedTitle(),
249
                        showTitle: true,
250
                        dropdownOptions: {
251
                            idAttribute: 'id',
252
                            markSelected: true,
253
                            changeButton: true
254
                        },
255
                        dropdownItems: [
256
                            {
257
                                title: this.translations.filterAll,
258
                                marked: true,
259
                                callback: function() {
260
                                    this.setWorkflowStage(null);
261
                                }.bind(this)
262
                            },
263
                            {
264
                                id: 'published',
265
                                title: this.translations.published,
266
                                callback: function() {
267
                                    this.setWorkflowStage('published');
268
                                }.bind(this)
269
                            },
270
                            {
271
                                id: 'test',
272
                                title: this.translations.unpublished,
273
                                callback: function() {
274
                                    this.setWorkflowStage('test');
275
                                }.bind(this)
276
                            }
277
                        ]
278
                    }
279
                },
280
                filter: {
281
                    options: {
282
                        icon: 'filter',
283
                        group: 2,
284
                        title: listHelper.getFilterTitle(),
285
                        showTitle: true,
286
                        dropdownOptions: {
287
                            idAttribute: 'id',
288
                            markSelected: true,
289
                            changeButton: false
290
                        },
291
                        dropdownItems: [
292
                            {
293
                                id: 'all',
294
                                title: this.translations.filterAll,
295
                                marked: true,
296
                                callback: this.removeFilter.bind(this)
297
                            },
298
                            {
299
                                id: 'me',
300
                                title: this.translations.filterMe,
301
                                callback: function() {
302
                                    this.closeContactSelection({contactItem: this.sandbox.sulu.user.contact}, 'me');
303
                                }.bind(this)
304
                            },
305
                            {
306
                                id: 'filterByAuthor',
307
                                title: this.translations.filterByAuthor + ' ...',
308
                                callback: this.openContactSelection.bind(this)
309
                            },
310
                            {
311
                                divider: true
312
                            },
313
                            {
314
                                id: 'filterByCategory',
315
                                title: this.translations.filterByCategory + ' ...',
316
                                callback: this.openCategorySelection.bind(this)
317
                            },
318
                            {
319
                                id: 'filterByTag',
320
                                title: this.translations.filterByTag + ' ...',
321
                                callback: this.openTagSelection.bind(this)
322
                            }
323
                        ]
324
                    }
325
                }
326
            });
327
        },
328
329
        setButtonTitle: function(button, title) {
330
            this.sandbox.emit('husky.toolbar.' + this.options.instanceName + '.button.set', button, {title: title});
331
        },
332
333
        openAuthoredSelection: function() {
334
            this.$el.parent().addClass('limited');
335
            this.sandbox.emit('husky.overlay.' + this.options.instanceName + '.slide-to', 1);
336
337
            this.sandbox.once('sulu_content.teaser-selection.' + this.options.instanceName + '.ok-button.clicked', function() {
338
                this.sandbox.emit('sulu_article.authored-selection.form.get');
339
            }.bind(this));
340
        },
341
342
        closeAuthoredSelection: function(data) {
343
            this.$el.parent().removeClass('limited');
344
345
            this.sandbox.emit('husky.datagrid.' + this.options.instanceName + '.url.update', {
346
                authoredFrom: data ? data.from : null,
347
                authoredTo: data ? data.to : null,
348
            });
349
350
            this.setButtonTitle('authoredDate', listHelper.getAuthoredTitle(data));
351
352
            this.sandbox.emit('husky.overlay.' + this.options.instanceName + '.slide-to', 0);
353
        },
354
355
        openContactSelection: function() {
356
            this.sandbox.emit('husky.overlay.' + this.options.instanceName + '.slide-to', 2);
357
358
            this.sandbox.once('sulu_content.teaser-selection.' + this.options.instanceName + '.ok-button.clicked', function() {
359
                this.sandbox.emit('sulu_article.contact-selection.form.get');
360
            }.bind(this));
361
        },
362
363
        closeContactSelection: function(data, filterKey) {
364
            var filter = {filterKey: filterKey || 'filterByAuthor', contact: data.contactItem};
365
366
            this.setButtonTitle('filter', listHelper.getFilterTitle(filter));
367
            this.sandbox.emit('husky.datagrid.' + this.options.instanceName + '.url.update',this.getUrlParameter(filter));
368
369
            this.sandbox.emit('husky.overlay.' + this.options.instanceName + '.slide-to', 0);
370
        },
371
372
        openCategorySelection: function() {
373
            this.$el.parent().addClass('limited');
374
            this.sandbox.emit('husky.overlay.' + this.options.instanceName + '.slide-to', 3);
375
376
            this.sandbox.once('sulu_content.teaser-selection.' + this.options.instanceName + '.ok-button.clicked', function() {
377
                this.sandbox.emit('sulu_article.category-selection.form.get');
378
            }.bind(this));
379
        },
380
381
        closeCategorySelection: function(data) {
382
            this.$el.parent().removeClass('limited');
383
384
            var filter = {filterKey: 'filterByCategory', category: data.categoryItem};
385
386
            this.setButtonTitle('filter', listHelper.getFilterTitle(filter));
387
            this.sandbox.emit('husky.datagrid.' + this.options.instanceName + '.url.update',this.getUrlParameter(filter));
388
389
            this.sandbox.emit('husky.overlay.' + this.options.instanceName + '.slide-to', 0);
390
        },
391
392
        openTagSelection: function() {
393
            this.sandbox.emit('husky.overlay.' + this.options.instanceName + '.slide-to', 4);
394
395
            this.sandbox.once('sulu_content.teaser-selection.' + this.options.instanceName + '.ok-button.clicked', function() {
396
                this.sandbox.emit('sulu_article.tag-selection.form.get');
397
            }.bind(this));
398
        },
399
400
        closeTagSelection: function(data) {
401
            var filter = {filterKey: 'filterByTag', tag: data.tagItem};
402
403
            this.setButtonTitle('filter', listHelper.getFilterTitle(filter));
404
            this.sandbox.emit('husky.datagrid.' + this.options.instanceName + '.url.update',this.getUrlParameter(filter));
405
406
            this.sandbox.emit('husky.overlay.' + this.options.instanceName + '.slide-to', 0);
407
        },
408
409
        removeFilter: function() {
410
            this.setButtonTitle('filter', listHelper.getFilterTitle());
411
412
            this.sandbox.emit('husky.datagrid.' + this.options.instanceName + '.url.update',this.getUrlParameter({}));
413
        },
414
415
        setWorkflowStage: function(workflowStage) {
416
            this.sandbox.emit('husky.datagrid.' + this.options.instanceName + '.url.update', {
417
                workflowStage: workflowStage,
418
            });
419
        },
420
421
        getUrlParameter: function(filter) {
422
            return {
423
                contactId: filter.contact ? filter.contact.id : null,
424
                categoryId: filter.category ? filter.category.id : null,
425
                tagId: filter.tag ? filter.tag.id : null,
426
            };
427
        }
428
    };
429
});
430