woocommerce-paypal-payments/modules.local/ppcp-button/resources/js/modules/ErrorHandler.js

39 lines
942 B
JavaScript
Raw Normal View History

2020-04-02 08:38:00 +03:00
class ErrorHandler {
2020-07-28 08:05:18 +03:00
constructor(genericErrorText)
2020-04-06 11:16:18 +03:00
{
2020-07-28 08:05:18 +03:00
this.genericErrorText = genericErrorText;
2020-04-02 08:38:00 +03:00
this.wrapper = document.querySelector('.woocommerce-notices-wrapper');
}
2020-07-28 08:05:18 +03:00
genericError() {
this.clear();
this.message(this.genericErrorText)
}
2020-04-06 11:16:18 +03:00
message(text)
{
2020-04-02 08:38:00 +03:00
this.wrapper.classList.add('woocommerce-error');
this.wrapper.innerText = this.sanitize(text);
2020-07-16 13:17:46 +03:00
jQuery.scroll_to_notices(jQuery('.woocommerce-notices-wrapper'))
2020-04-02 08:38:00 +03:00
}
2020-04-06 11:16:18 +03:00
sanitize(text)
{
2020-04-02 08:38:00 +03:00
const textarea = document.createElement('textarea');
textarea.innerHTML = text;
return textarea.value.replace('Error: ', '');
2020-04-02 08:38:00 +03:00
}
2020-04-06 11:16:18 +03:00
clear()
{
2020-04-02 08:38:00 +03:00
if (! this.wrapper.classList.contains('woocommerce-error')) {
return;
}
this.wrapper.classList.remove('woocommerce-error');
this.wrapper.innerText = '';
}
}
export default ErrorHandler;