All files / web/bundles/pimui/js/product/form comments.js

74.36% Statements 29/39
66.67% Branches 4/6
77.78% Functions 14/18
74.36% Lines 29/39

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 210 211 212 213 214 215 216 217 218 219 220                  265x                                                 265x                                     323x   323x             322x         322x             10x       10x 10x   10x                 10x     10x                 10x                               44x 44x 44x 44x                                             2x           2x 2x                       1x                         1x           1x 1x                       1x   1x                       1x 1x 1x                
'use strict';
/**
 * Comment panel extension
 *
 * @author    Julien Sanchez <julien@akeneo.com>
 * @author    Filips Alpe <filips@akeneo.com>
 * @copyright 2015 Akeneo SAS (http://www.akeneo.com)
 * @license   http://opensource.org/licenses/osl-3.0.php  Open Software License (OSL 3.0)
 */
define(
    [
        'jquery',
        'underscore',
        'oro/translator',
        'backbone',
        'pim/form',
        'pim/user-context',
        'pim/template/product/comments',
        'routing',
        'oro/messenger',
        'pim/dialog'
    ],
    function (
        $,
        _,
        __,
        Backbone,
        BaseForm,
        UserContext,
        template,
        Routing,
        messenger,
        Dialog
    ) {
        return BaseForm.extend({
            template: _.template(template),
 
            className: 'panel-pane',
 
            comments: [],
 
            events: {
                'keyup .comment-create textarea, .reply-to-comment textarea': 'toggleButtons',
                'click .comment-create .send-comment': 'saveComment',
                'click .remove-comment': 'removeComment',
                'click .comment-thread .send-comment': 'saveReply',
                'click .comment-thread .cancel-comment, .comment-create .cancel-comment': 'cancelComment'
            },
 
            /**
             * {@inheritdoc}
             */
            initialize: function () {
                this.comment = new Backbone.Model();
 
                BaseForm.prototype.initialize.apply(this, arguments);
            },
 
            /**
             * {@inheritdoc}
             */
            configure: function () {
                this.trigger('tab:register', {
                    code: this.code,
                    label: __('pim_enrich.entity.product.module.comment.title')
                });
 
                return BaseForm.prototype.configure.apply(this, arguments);
            },
 
            /**
             * {@inheritdoc}
             */
            render: function () {
                Iif (!this.configured || this.code !== this.getParent().getCurrentTab()) {
                    return this;
                }
 
                this.loadData().done(function (data) {
                    this.comments = data;
 
                    this.$el.html(
                        this.template({
                            __: __,
                            comments: this.comments,
                            username: UserContext.get('username'),
                            removedAuthor: '[' + __('pim_enrich.entity.product.module.comment.removed_author') + ']',
                            emptyLabel: __('pim_enrich.entity.product.module.comment.empty')
                        })
                    );
                    this.delegateEvents();
                }.bind(this));
 
                return this;
            },
 
            /**
             * Load the comments from database
             *
             * @return {Promise}
             */
            loadData: function () {
                return $.get(
                    Routing.generate(
                        'pim_enrich_product_comments_rest_get',
                        {
                            id: this.getFormData().meta.id
                        }
                    )
                );
            },
 
            /**
             * Display or hide the save/cancel buttons attached to the text area
             *
             * @param {Event} event
             */
            toggleButtons: function (event) {
                var $element = $(event.currentTarget).parents('.comment-thread, .comment-create');
                Eif ($element.find('textarea').val()) {
                    $element.addClass('active');
                    $element.find('.AknButtonList').removeClass('AknButtonList--hide');
                } else {
                    $element.removeClass('active');
                    $element.find('.AknButtonList').addClass('AknButtonList--hide');
                }
            },
 
            /**
             * Cancels the current written comment
             *
             * @param {Event} event
             */
            cancelComment: function (event) {
                var $element = $(event.currentTarget).parents('.comment-thread, .comment-create');
                $element.find('textarea').val('');
                $element.removeClass('active');
                $element.find('.AknButtonList').addClass('AknButtonList--hide');
            },
 
            /**
             * Saves the current comment to database
             */
            saveComment: function () {
                $.ajax({
                    type: 'POST',
                    url: Routing.generate('pim_enrich_product_comments_rest_post', { id: this.getFormData().meta.id }),
                    contentType: 'application/json',
                    data: JSON.stringify({ 'body': this.$('.comment-create textarea').val() })
                }).done(function () {
                    this.render();
                    messenger.notify('success', __('flash.comment.create.success'));
                }.bind(this)).fail(function () {
                    messenger.notify('error', __('flash.comment.create.error'));
                });
            },
 
            /**
             * Shows a confirm dialog before removing the current comment
             *
             * @param {Event} event
             */
            removeComment: function (event) {
                Dialog.confirmDelete(
                    __('pim_enrich.entity.product.module.comment.delete_confirm'),
                    __('pim_common.confirm_deletion'),
                    this.doRemove.bind(this, event),
                );
            },
 
            /**
             * Removes the comment from database
             *
             * @param {Event} event
             */
            doRemove: function (event) {
                $.ajax({
                    url: Routing.generate('pim_comment_comment_delete', { id: event.currentTarget.dataset.commentId }),
                    type: 'POST',
                    headers: { accept: 'application/json' },
                    data: { _method: 'DELETE' }
                }).done(function () {
                    this.render();
                    messenger.notify('success', __('flash.comment.delete.success'));
                }.bind(this)).fail(function () {
                    messenger.notify('error', __('flash.comment.delete.error'));
                });
            },
 
            /**
             * Save the current reply of a comment
             *
             * @param {Event} event
             */
            saveReply: function (event) {
                var $thread = $(event.currentTarget).parents('.comment-thread');
 
                $.ajax({
                    type: 'POST',
                    url: Routing.generate(
                        'pim_enrich_product_comment_reply_rest_post',
                        {
                            id: this.getFormData().meta.id,
                            commentId: $thread.data('comment-id')
                        }
                    ),
                    contentType: 'application/json',
                    data: JSON.stringify({ 'body': $thread.find('textarea').val()})
                }).done(function () {
                    $thread.find('textarea').val('');
                    this.render();
                    messenger.notify('success', __('flash.comment.reply.success'));
                }.bind(this)).fail(function () {
                    messenger.notify('error', __('flash.comment.reply.error'));
                });
            }
        });
    }
);