Created
June 1, 2015 06:51
-
-
Save zajca/3a015e80cdb46ed7b44d to your computer and use it in GitHub Desktop.
Bootstrap alers ES6 singleton class
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
import $ from 'jquery'; | |
import domready from 'domready'; | |
import is from 'is'; | |
class Alert{ | |
constructor(alertsContainer) { | |
if(!Alert.instance){ | |
Alert.instance = this; | |
}else{ | |
return Alert.instance; | |
} | |
this.container = alertsContainer; | |
return Alert.instance; | |
} | |
pushNewAlert(type,msg){ | |
let html = Alert.toAlertString(type,msg); | |
$(this.container).append(html); | |
} | |
static toAlertString(type,msg){ | |
return ` | |
<div class="alert alert-${type} alert-dismissible fade in" role="alert"> | |
<button type="button" class="close" data-dismiss="alert"> | |
<span data-aria-hidden="true">×</span> | |
<span class="sr-only">${Translator.trans('front.close')}</span> | |
</button> | |
${msg} | |
</div>`; | |
} | |
} | |
Alert.instance = null; | |
Alert.TYPE = { | |
WARN: 'warning', | |
ERR: 'danger', | |
INFO: 'info', | |
OK: 'success' | |
}; | |
domready(function () { | |
let alertsContainer = document.getElementById("flash-messages"); | |
if (!is.nil(alertsContainer) && is.defined(alertsContainer)) { | |
new Alert(alertsContainer); | |
} | |
}); | |
export default Alert; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment