All files / web/bundles/pimnotification/js notification-list.js

70.69% Statements 41/58
37.5% Branches 3/8
68.18% Functions 15/22
70.69% Lines 41/58

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 2101160x                         1160x                           1160x           1160x                                                                                                 19x   19x       19x                                 19x       19x             19x       19x   12x           1160x                   3669x   3669x   3669x                         16x       16x   16x   16x   16x 16x   16x 16x 16x         11468x   11468x 19x         19x       19x       1160x 3669x 3669x 3669x   3669x 7338x 7338x 7338x   3669x 16x     3669x        
define(
    [
        'backbone',
        'jquery',
        'underscore',
        'oro/translator',
        'routing',
        'pim/router',
        'pim/template/notification/notification-list'
    ],
    function (Backbone, $, _, __, Routing, router, template) {
        'use strict';
 
        var Notification = Backbone.Model.extend({
            defaults: {
                viewed:            false,
                url:               null,
                message:           '',
                id:                null,
                type:              'success',
                createdAt:         null,
                actionType:        null,
                showReportButton:  true,
                comment:           null
            }
        });
 
        var NotificationList = Backbone.Collection.extend({
            model:     Notification,
            loading:   false,
            hasMore:   true
        });
 
        var NotificationView = Backbone.View.extend({
            tagName: 'li',
            className: 'AknNotification',
            model: Notification,
            template: _.template(template),
            events: {
                'click .icon-trash':     'remove',
                'click .icon-eye-close': 'markAsRead',
                'click i':               'preventOpen',
                'click a.new':           'markAsRead',
                'click a':               'open'
            },
 
            remove: function () {
                this.model.destroy({
                    url: Routing.generate('pim_notification_notification_remove', { id: this.model.get('id') }),
                    wait: false,
                    _method: 'DELETE'
                });
 
                this.$el.fadeOut(function () {
                    this.remove();
                });
            },
 
            open: function (e) {
                this.preventOpen(e);
                if (this.model.get('url')) {
                    router.redirect(this.model.get('url'));
                }
                this.$el.closest('.dropdown').removeClass('open');
            },
 
            preventOpen: function (e) {
                e.preventDefault();
                e.stopPropagation();
            },
 
            markAsRead: function () {
                this.model.trigger('mark_as_read', this.model.id);
                this.model.set('viewed', true);
                $.ajax({
                    type: 'POST',
                    url: Routing.generate('pim_notification_notification_mark_viewed', {id: this.model.id}),
                    async: true
                });
            },
 
            initialize: function () {
                this.listenTo(this.model, 'change', this.render);
 
                this.render();
            },
 
            render: function () {
                this.$el.html(
                    this.template({
                        viewed: this.model.get('viewed'),
                        message: this.model.get('message'),
                        url: this.model.get('url'),
                        icon: this.getIcon(this.model.get('type')),
                        type: this.model.get('type'),
                        createdAt: this.model.get('createdAt'),
                        actionType: this.camelize(this.model.get('actionType')),
                        actionTypeMessage: 'foo',
                        buttonLabel: this.model.get('buttonLabel'),
                        actionTypeMessage: __('pim_notification.types.' + this.model.get('actionType')),
                        showReportButton: this.model.get('showReportButton'),
                        comment: this.model.get('comment')
                    }
                ));
 
                return this;
            },
 
            getIcon: function (type) {
                var icons = {
                    'success': 'ok',
                    'warning': 'warning-sign',
                    'error':   'remove',
                    'add':     'plus'
                };
 
                return _.result(icons, type, 'remove');
            },
 
            camelize: function (str) {
                return str.toLowerCase()
                    .replace(/_(.)/g, function ($firstLetter) {
                        return $firstLetter.toUpperCase();
                    })
                    .replace(/_/g, '');
            }
        });
 
        var NotificationListView = Backbone.View.extend({
            tagName: 'ol',
 
            collection: NotificationList,
 
            events: {
                'scroll': 'onScroll'
            },
 
            initialize: function () {
                _.bindAll(this, 'render');
 
                this.collection.on('add reset', this.render);
 
                this.render();
            },
 
            onScroll: function () {
                var self = this;
                this.$el.on('scroll', function () {
                    if ($(this).scrollTop() + $(this).innerHeight() >= this.scrollHeight) {
                        self.loadNotifications();
                    }
                });
            },
 
            loadNotifications: function () {
                Iif (this.collection.loading || !this.collection.hasMore) {
                    return;
                }
 
                this.collection.loading = true;
 
                this.collection.trigger('loading:start');
 
                $.getJSON(Routing.generate('pim_notification_notification_list') + '?skip=' + this.collection.length)
                    .then(_.bind(function (data) {
                        this.collection.add(data.notifications);
                        this.collection.hasMore = data.notifications.length >= 10;
 
                        this.collection.trigger('load:unreadCount', data.unreadCount);
                        this.collection.loading = false;
                        this.collection.trigger('loading:finish');
                    }, this));
            },
 
            render: function () {
                this.$el.empty();
 
                _.each(this.collection.models, function (model) {
                    this.renderNotification(model);
                }, this);
            },
 
            renderNotification: function (item) {
                var itemView = new NotificationView({
                    model: item
                });
 
                this.$el.append(itemView.$el);
            }
        });
 
        return function (opts) {
            var notificationList = new NotificationList();
            var options = _.extend({}, { el: null, collection: notificationList }, opts);
            var notificationListView = new NotificationListView(options);
 
            notificationList.setElement = function (element) {
                notificationListView.$el.prependTo(element);
                notificationListView.delegateEvents();
                notificationListView.render();
            };
            notificationList.loadNotifications = function () {
                return notificationListView.loadNotifications();
            };
 
            return notificationList;
        };
    }
);