ComunicWeb/assets/js/components/darkTheme.js

66 lines
1.3 KiB
JavaScript
Raw Permalink Normal View History

2019-01-10 14:44:39 +00:00
/**
* Dark theme component
*
* @author Pierre HUBERT
*/
ComunicWeb.components.darkTheme = {
/**
2019-01-10 16:07:42 +00:00
* Specify whether dark theme has to be enabled or not
2019-01-10 14:44:39 +00:00
*/
2019-01-10 16:07:42 +00:00
_local_storage_name: "dark_theme_mode",
2019-01-10 14:44:39 +00:00
/**
* CSS element that contains dark theme CSS rules
*/
_cssElem: null,
/**
* Check out whether dark theme is enabled or not
*
* @return {boolean} TRUE if enabled / FALSE else
*/
isEnabled: function(){
2019-01-10 16:07:42 +00:00
return localStorage.getItem(this._local_storage_name) == "true";
2019-01-10 14:44:39 +00:00
},
/**
* Specify whether dark theme should be enabled or not
*
* @param {boolean} enable TRUE to enable / FALSE else
*/
setEnabled: function(enable){
2019-01-10 16:07:42 +00:00
localStorage.setItem(this._local_storage_name, enable ? "true" : "false");
this.refresh();
},
/**
* Refresh dark theme state
*/
refresh: function(){
2019-01-10 14:44:39 +00:00
//Check if the theme has to be disabled
2019-01-10 16:07:42 +00:00
if(!this.isEnabled()){
2019-01-10 14:44:39 +00:00
if(this._cssElem != null)
this._cssElem.disabled = true;
return;
}
//Check if CSS element is already loaded
else if(this._cssElem != null)
this._cssElem.disabled = false;
//We need to load dark theme
else {
this._cssElem = createElem2({
type: "link",
href: ComunicWeb.__config.assetsURL + "css/dark_theme.css"
});
this._cssElem.setAttribute("rel", "stylesheet");
document.head.appendChild(this._cssElem);
}
}
}