4

I want to remove the success notification message with animation included, trying with below code with no success:

define([
'jquery',
'uiComponent',
'underscore',
'Magento_Customer/js/customer-data',
'jquery/jquery-storageapi'
], function ($, Component, _, customerData) {
'use strict';

return Component.extend({

    defaults: {
        cookieMessages: [],
        messages: [],
        selector: '.page.messages .message'
    },
    /** @inheritdoc */
    initialize: function () {
        this._super();

        this.cookieMessages = $.cookieStorage.get('mage-messages');
        this.messages = customerData.get('messages').extend({
            disposableCustomerData: 'messages'
        });

        if (!_.isEmpty(this.messages().messages)) {
            customerData.set('messages', {});
        }

        $.cookieStorage.set('mage-messages', '');
        this.RemoveMessageAfterTime;
    },

    initObservable: function () {
        this._super();

        return this;
    },

    RemoveMessage: function () {

        var el = $(this.selector);

        el.toggleClass('bounceInRight bounceOutRight');
    },

    RemoveMessageAfterTime: function() {
        var el = $(this.selector);

        setTimeout(function() {
            $('.page.messages .message').toggleClass('bounceInRight bounceOutRight');
        }, 6000);
    }

});

});

Messages.phtml

<div data-bind="scope: 'messages'">
<div data-bind="click: RemoveMessage">
    <div role="alert" data-bind="foreach: { data: cookieMessages, as: 'message' }" class="messages">
        <div data-bind="attr: {
            class: 'bounceInRight animated message-' + message.type + ' ' + message.type + ' message',
            'data-ui-id': 'message-' + message.type
        }">
            <div class="text" data-bind="html: message.text"></div>
            <div class="close" style="display: none;" data-bind="visible: true"><i class="fa fa-times" aria-hidden="true"></i></div>
        </div>
    </div>
    <div role="alert" data-bind="foreach: { data: messages().messages, as: 'message' }" class="messages">
        <div data-bind="attr: {
            class: 'bounceInRight animated message-' + message.type + ' ' + message.type + ' message',
            'data-ui-id': 'message-' + message.type
        }">
            <div class="text" data-bind="html: message.text"></div>
            <div class="close" style="display: none;" data-bind="visible: true"><i class="fa fa-times" aria-hidden="true"></i></div>
        </div>
    </div>
</div>
</div>
<script type="text/x-magento-init">
  {
    "*": {
        "Magento_Ui/js/core/app": {
            "components": {
                    "messages": {
                        "component": "Magento_Theme/js/view/messages"
                    }
                }
            }
        }
  }
</script>
0

Solved it, compatible with ko.js for magento 2.2.6:

Reading this helped: https://www.interactiv4.com/blog-es/ocultar-automaticamente-los-mensajes-magento-2-codehacks/

Here is the code:

Messages.phtml

<div data-bind="scope: 'messages'">
<div data-bind="visible: isVisible(), click: RemoveMessage">
    <div role="alert" data-bind="foreach: { data: cookieMessages, as: 'message' }" class="messages">
        <div data-bind="attr: {
            class: 'bounceInRight animated message-' + message.type + ' ' + message.type + ' message',
            'data-ui-id': 'message-' + message.type
        }">
            <div class="text" data-bind="html: message.text"></div>
            <div class="close" style="display: none;" data-bind="visible: true"><i class="fa fa-times" aria-hidden="true"></i></div>
        </div>
    </div>
    <div role="alert" data-bind="foreach: { data: messages().messages, as: 'message' }" class="messages">
        <div data-bind="attr: {
            class: 'bounceInRight animated message-' + message.type + ' ' + message.type + ' message',
            'data-ui-id': 'message-' + message.type
        }">
            <div class="text" data-bind="html: message.text"></div>
            <div class="close" style="display: none;" data-bind="visible: true"><i class="fa fa-times" aria-hidden="true"></i></div>
        </div>
    </div>
</div>
</div> 
<script type="text/x-magento-init">
  {
    "*": {
        "Magento_Ui/js/core/app": {
            "components": {
                    "messages": {
                        "component": "Magento_Theme/js/view/messages"
                    }
                }
            }
        }
  }
</script>

Messages.js

define([
'jquery',
'uiComponent',
'underscore',
'Magento_Customer/js/customer-data',
'jquery/jquery-storageapi'
], function ($, Component, _, customerData,ko) {
'use strict';

return Component.extend({
    defaults: {
        cookieMessages: [],
        messages: [],
        selector: '.page.messages .message',
        listens: {
            isHidden: 'onHiddenChange'
        }
    },
    /** @inheritdoc */
    initialize: function () {
        this._super();

        this.cookieMessages = $.cookieStorage.get('mage-messages');
        this.messages = customerData.get('messages').extend({
            disposableCustomerData: 'messages'
        });

        if (!_.isEmpty(this.messages().messages)) {
            customerData.set('messages', {});
        } 

        $.cookieStorage.set('mage-messages', '');
    },

    initObservable: function () {
        this._super()
            .observe('isHidden');

        return this;
    },

    RemoveMessage: function () {

        var el = $(this.selector);

        el.toggleClass('bounceInRight bounceOutRight');
        setTimeout(function () {
            el.hide()
        }, 2000);
    },

    isVisible: function () {
        return this.isHidden(!_.isEmpty(this.messages().messages) || !_.isEmpty(this.cookieMessages));
    },

    onHiddenChange: function (isHidden) {
        var self = this;

        // Hide message block if needed
        console.log(isHidden);
        if (isHidden) {
            setTimeout(function () {
                self.RemoveMessage();
            }, 5000);
        }
        // reseting variable
        this.isHidden(false);
    }

});

});

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.