Last active
February 16, 2018 12:47
-
-
Save rubenRP/2da70b482be9d543dca36ee3043454f5 to your computer and use it in GitHub Desktop.
Magento 2. Medium post snippet
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
define([ | |
'jquery', | |
'uiComponent', | |
'Magento_Customer/js/customer-data', | |
'underscore', | |
'jquery/jquery-storageapi' | |
], function ($, Component, customerData, _) { | |
'use strict'; | |
return Component.extend({ | |
defaults: { | |
cookieMessages: [], | |
messages: [], | |
isHidden: false, | |
selector: '.page.messages .messages', | |
listens: { | |
isHidden: 'onHiddenChange' | |
} | |
}, | |
/** | |
* Extends Component object by storage observable messages. | |
*/ | |
initialize: function () { | |
this._super(); | |
this.cookieMessages = $.cookieStorage.get('mage-messages'); | |
this.messages = customerData.get('messages').extend({ | |
disposableCustomerData: 'messages' | |
}); | |
// Force to clean obsolete messages | |
if (!_.isEmpty(this.messages().messages)) { | |
customerData.set('messages', {}); | |
} | |
$.cookieStorage.set('mage-messages', ''); | |
}, | |
initObservable: function () { | |
this._super() | |
.observe('isHidden'); | |
return this; | |
}, | |
isVisible: function () { | |
return this.isHidden(!_.isEmpty(this.messages().messages) || !_.isEmpty(this.cookieMessages)); | |
}, | |
removeAll: function () { | |
$(self.selector).hide(); | |
}, | |
onHiddenChange: function (isHidden) { | |
var self = this; | |
// Hide message block if needed | |
if (isHidden) { | |
setTimeout(function () { | |
$(self.selector).hide(); | |
}, 5000); | |
} | |
} | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment