web-apps/apps/common/main/lib/controller/Themes.js

331 lines
11 KiB
JavaScript
Raw Normal View History

2021-02-06 10:03:46 +00:00
/**
* Created by Maxim.Kadushkin on 2/5/2021.
*/
define([
'core'
], function () {
'use strict';
2021-04-18 14:32:39 +00:00
Common.UI.Themes = new (function(locale) {
!locale && (locale = {});
var themes_map = {
2021-04-18 14:32:39 +00:00
'theme-light': {
text: locale.txtThemeLight || 'Light',
2021-06-25 08:53:43 +00:00
type: 'light',
source: 'static',
2021-04-18 14:32:39 +00:00
},
'theme-classic-light': {
text: locale.txtThemeClassicLight || 'Classic Light',
2021-06-25 08:53:43 +00:00
type: 'light',
source: 'static',
2021-04-18 14:32:39 +00:00
},
'theme-dark': {
text: locale.txtThemeDark || 'Dark',
2021-06-25 08:53:43 +00:00
type: 'dark',
source: 'static',
2021-04-18 14:32:39 +00:00
},
}
2021-06-25 08:53:43 +00:00
if ( !!window.currentLoaderTheme ) {
themes_map[currentLoaderTheme.id] = {};
window.currentLoaderTheme = undefined;
}
2021-04-22 16:49:37 +00:00
var id_default_light_theme = 'theme-classic-light',
id_default_dark_theme = 'theme-dark';
var name_colors = [
2021-06-25 08:53:43 +00:00
"toolbar-header-document",
"toolbar-header-spreadsheet",
"toolbar-header-presentation",
"background-normal",
"background-toolbar",
"background-toolbar-additional",
"background-primary-dialog-button",
"background-tab-underline",
"background-notification-popover",
"background-notification-badge",
"background-scrim",
"background-loader",
"highlight-button-hover",
"highlight-button-pressed",
"highlight-button-pressed-hover",
"highlight-primary-dialog-button-hover",
"highlight-header-button-hover",
"highlight-header-button-pressed",
"highlight-toolbar-tab-underline",
"highlight-text-select",
"border-toolbar",
"border-divider",
"border-regular-control",
"border-toolbar-button-hover",
"border-preview-hover",
"border-preview-select",
"border-control-focus",
"border-color-shading",
"border-error",
"text-normal",
"text-normal-pressed",
"text-secondary",
"text-tertiary",
"text-link",
2021-04-24 20:20:55 +00:00
"text-link-hover",
"text-link-active",
"text-link-visited",
"text-inverse",
"text-toolbar-header",
"text-contrast-background",
"icon-normal",
"icon-normal-pressed",
"icon-inverse",
"icon-toolbar-header",
"icon-notification-badge",
"icon-contrast-popover",
"icon-success",
"canvas-background",
"canvas-content-background",
"canvas-page-border",
"canvas-ruler-background",
"canvas-ruler-margins-background",
"canvas-ruler-mark",
"canvas-ruler-handle-border",
"canvas-ruler-handle-border-disabled",
"canvas-high-contrast",
"canvas-high-contrast-disabled",
"canvas-cell-border",
"canvas-cell-title-border",
"canvas-cell-title-border-hover",
"canvas-cell-title-border-selected",
"canvas-cell-title-hover",
"canvas-cell-title-selected",
"canvas-dark-cell-title",
"canvas-dark-cell-title-hover",
"canvas-dark-cell-title-selected",
"canvas-dark-cell-title-border",
"canvas-dark-cell-title-border-hover",
"canvas-dark-cell-title-border-selected",
"canvas-scroll-thumb",
"canvas-scroll-thumb-hover",
"canvas-scroll-thumb-pressed",
"canvas-scroll-thumb-border",
"canvas-scroll-thumb-border-hover",
"canvas-scroll-thumb-border-pressed",
"canvas-scroll-arrow",
"canvas-scroll-arrow-hover",
"canvas-scroll-arrow-pressed",
"canvas-scroll-thumb-target",
"canvas-scroll-thumb-target-hover",
"canvas-scroll-thumb-target-pressed"
];
var get_current_theme_colors = function (colors) {
var out_object = {};
if ( !(Common.Utils.isIE10 || Common.Utils.isIE11) ) {
var style = getComputedStyle(document.body);
colors.forEach(function (item, index) {
out_object[item] = style.getPropertyValue('--' + item).trim()
})
}
return out_object;
}
var create_colors_css = function (id, colors) {
if ( !!colors && !!id ) {
var _css_array = [':root .', id, '{'];
for (var c in colors) {
_css_array.push('--', c, ':', colors[c], ';');
}
_css_array.push('}');
return _css_array.join('');
}
}
var write_theme_css = function (css) {
if ( !!css ) {
var style = document.createElement('style');
style.type = 'text/css';
style.innerHTML = css;
document.getElementsByTagName('head')[0].appendChild(style);
}
}
var parse_themes_object = function (obj) {
if ( !!obj.themes && obj.themes instanceof Array ) {
obj.themes.forEach(function (item) {
if ( !!item.id ) {
themes_map[item.id] = {text: item.name, type: item.type};
write_theme_css(create_colors_css(item.id, item.colors));
} else
if ( typeof item == 'string' ) {
get_themes_config(item)
}
});
} else
if ( obj.id ) {
themes_map[obj.id] = {text: obj.name, type: obj.type};
write_theme_css( create_colors_css(obj.id, obj.colors) );
}
}
var get_themes_config = function (url) {
2021-06-25 08:53:43 +00:00
Common.Utils.loadConfig(url,
function ( obj ) {
if ( obj != 'error' ) {
parse_themes_object(obj);
}
}
2021-06-25 08:53:43 +00:00
);
// fetch(url, {
// method: 'get',
// headers: {
// 'Accept': 'application/json',
// },
// }).then(function(response) {
// if (!response.ok) {
// throw new Error('server error');
// }
// return response.json();
// }).then(function(response) {
// if ( response.then ) {
// // return response.json();
// } else {
// parse_themes_object(response);
//
// /* to break promises chain */
// throw new Error('loaded');
// }
// }).catch(function(e) {
// if ( e.message == 'loaded' ) {
// } else console.log('fetch error: ' + e);
// });
}
var on_document_ready = function (el) {
2021-06-25 08:53:43 +00:00
get_themes_config('../../common/main/resources/themes/themes.json');
}
2021-06-25 08:03:05 +00:00
var get_ui_theme_name = function (objtheme) {
if ( typeof(objtheme) == 'string' &&
objtheme.startsWith("{") && objtheme.endsWith("}") )
{
objtheme = JSON.parse(objtheme);
}
if ( objtheme && typeof(objtheme) == 'object' )
return objtheme.id;
return objtheme;
}
2021-02-06 10:03:46 +00:00
return {
init: function (api) {
var me = this;
2021-03-13 21:20:01 +00:00
2021-02-06 10:03:46 +00:00
$(window).on('storage', function (e) {
2021-06-25 08:03:05 +00:00
if ( e.key == 'ui-theme' || e.key == 'ui-theme-id' ) {
2021-02-06 10:03:46 +00:00
me.setTheme(e.originalEvent.newValue);
}
})
this.api = api;
2021-06-25 08:03:05 +00:00
var theme_name = get_ui_theme_name(Common.localStorage.getItem('ui-theme'));
2021-04-26 10:36:18 +00:00
if ( !themes_map[theme_name] )
2021-04-26 07:53:52 +00:00
theme_name = id_default_light_theme;
if ( !$('body').hasClass(theme_name) ) {
$('body').addClass(theme_name);
}
2021-02-06 10:03:46 +00:00
var obj = get_current_theme_colors(name_colors);
2021-04-23 12:39:46 +00:00
obj.type = themes_map[theme_name].type;
2021-04-06 17:06:14 +00:00
obj.name = theme_name;
api.asc_setSkin(obj);
Common.NotificationCenter.on('document:ready', on_document_ready.bind(this));
2021-02-06 10:03:46 +00:00
},
available: function () {
2021-05-15 10:47:32 +00:00
return !Common.Utils.isIE && !this.locked;
},
setAvailable: function (value) {
this.locked = value;
},
2021-04-18 14:32:39 +00:00
map: function () {
return themes_map
},
get: function (id) {
return themes_map[id]
},
currentThemeId: function () {
2021-06-25 08:03:05 +00:00
return get_ui_theme_name(Common.localStorage.getItem('ui-theme')) || id_default_light_theme;
2021-04-18 14:32:39 +00:00
},
2021-04-22 16:49:37 +00:00
defaultThemeId: function (type) {
return type == 'dark' ? id_default_dark_theme : id_default_light_theme;
2021-04-18 14:32:39 +00:00
},
2021-04-22 16:49:37 +00:00
defaultTheme: function (type) {
return themes_map[this.defaultThemeId(type)]
2021-02-06 10:03:46 +00:00
},
isDarkTheme: function () {
2021-04-22 16:49:37 +00:00
return themes_map[this.currentThemeId()].type == 'dark';
},
2021-06-25 08:03:05 +00:00
setTheme: function (obj, force) {
var id = get_ui_theme_name(obj);
2021-05-12 14:20:01 +00:00
if ( (this.currentThemeId() != id || force) && !!themes_map[id] ) {
2021-04-22 21:25:31 +00:00
var classname = document.body.className.replace(/theme-\w+\s?/, '');
2021-02-17 15:58:24 +00:00
document.body.className = classname;
2021-02-06 10:03:46 +00:00
2021-04-18 14:32:39 +00:00
$('body').addClass(id);
var obj = get_current_theme_colors(name_colors);
2021-04-18 14:32:39 +00:00
obj.type = themes_map[id].type;
obj.name = id;
this.api.asc_setSkin(obj);
2021-02-17 21:27:58 +00:00
2021-06-25 08:03:05 +00:00
if ( !(Common.Utils.isIE10 || Common.Utils.isIE11) ) {
var theme_obj = {
id: id,
type: obj.type,
};
2021-06-25 08:53:43 +00:00
if ( themes_map[id].source != 'static' ) {
theme_obj.colors = obj;
}
2021-06-25 08:03:05 +00:00
Common.localStorage.setItem('ui-theme', JSON.stringify(theme_obj));
}
Common.localStorage.setItem('ui-theme-id', id);
2021-04-18 14:32:39 +00:00
Common.NotificationCenter.trigger('uitheme:changed', id);
2021-02-06 10:03:46 +00:00
}
},
toggleTheme: function () {
2021-04-22 16:49:37 +00:00
this.setTheme( this.isDarkTheme() ? id_default_light_theme : id_default_dark_theme );
2021-02-06 10:03:46 +00:00
}
}
2021-04-18 14:32:39 +00:00
})(Common.UI.Themes);
2021-04-06 17:06:14 +00:00
});