2017-10-30 12:38:01 +03:00
|
|
|
import Service, {inject as service} from '@ember/service';
|
2021-07-26 17:59:10 +03:00
|
|
|
import {
|
2021-07-29 13:35:03 +03:00
|
|
|
Color,
|
2021-07-26 17:59:10 +03:00
|
|
|
darkenToContrastThreshold,
|
|
|
|
lightenToContrastThreshold,
|
2021-07-26 19:00:11 +03:00
|
|
|
textColorForBackgroundColor
|
2021-07-29 13:35:03 +03:00
|
|
|
} from '@tryghost/color-utils';
|
|
|
|
import {action} from '@ember/object';
|
2019-05-20 16:57:21 +03:00
|
|
|
import {get} from '@ember/object';
|
2019-05-20 18:16:19 +03:00
|
|
|
import {isEmpty} from '@ember/utils';
|
2021-07-01 12:11:08 +03:00
|
|
|
import {tracked} from '@glimmer/tracking';
|
2017-08-14 15:30:00 +03:00
|
|
|
|
2019-05-20 17:33:12 +03:00
|
|
|
function collectMetadataClasses(transition, prop) {
|
2019-05-20 16:57:21 +03:00
|
|
|
let oldClasses = [];
|
|
|
|
let newClasses = [];
|
|
|
|
let {from, to} = transition;
|
|
|
|
|
|
|
|
while (from) {
|
2019-05-20 17:33:12 +03:00
|
|
|
oldClasses = oldClasses.concat(get(from, `metadata.${prop}`) || []);
|
2019-05-20 16:57:21 +03:00
|
|
|
from = from.parent;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (to) {
|
2019-05-20 17:33:12 +03:00
|
|
|
newClasses = newClasses.concat(get(to, `metadata.${prop}`) || []);
|
2019-05-20 16:57:21 +03:00
|
|
|
to = to.parent;
|
|
|
|
}
|
|
|
|
|
2019-05-20 17:33:12 +03:00
|
|
|
return {oldClasses, newClasses};
|
|
|
|
}
|
|
|
|
|
|
|
|
function updateBodyClasses(transition) {
|
2019-05-20 16:57:21 +03:00
|
|
|
let {body} = document;
|
2019-05-20 17:33:12 +03:00
|
|
|
let {oldClasses, newClasses} = collectMetadataClasses(transition, 'bodyClasses');
|
|
|
|
|
2019-05-20 16:57:21 +03:00
|
|
|
oldClasses.forEach((oldClass) => {
|
|
|
|
body.classList.remove(oldClass);
|
|
|
|
});
|
|
|
|
newClasses.forEach((newClass) => {
|
|
|
|
body.classList.add(newClass);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-07-01 12:11:08 +03:00
|
|
|
export default class UiService extends Service {
|
|
|
|
@service config;
|
|
|
|
@service dropdown;
|
2021-07-26 17:59:10 +03:00
|
|
|
@service feature;
|
2021-07-01 12:11:08 +03:00
|
|
|
@service mediaQueries;
|
|
|
|
@service router;
|
2021-07-26 17:59:10 +03:00
|
|
|
@service settings;
|
2017-08-14 15:30:00 +03:00
|
|
|
|
2021-10-11 17:24:46 +03:00
|
|
|
@tracked contextualNavMenu = null;
|
2021-07-01 12:11:08 +03:00
|
|
|
@tracked isFullScreen = false;
|
|
|
|
@tracked mainClass = '';
|
|
|
|
@tracked showMobileMenu = false;
|
2017-08-14 15:30:00 +03:00
|
|
|
|
2021-07-01 12:11:08 +03:00
|
|
|
get isMobile() {
|
|
|
|
return this.mediaQueries.isMobile;
|
|
|
|
}
|
|
|
|
|
|
|
|
get isSideNavHidden() {
|
|
|
|
return this.isFullScreen || this.isMobile;
|
|
|
|
}
|
|
|
|
|
|
|
|
get hasSideNav() {
|
|
|
|
return !this.isSideNavHidden;
|
|
|
|
}
|
2017-08-14 15:30:00 +03:00
|
|
|
|
2021-07-26 17:59:10 +03:00
|
|
|
get backgroundColor() {
|
|
|
|
// hardcoded background colors because
|
|
|
|
// grabbing color from .gh-main with getComputedStyle always returns #ffffff
|
|
|
|
return this.feature.nightShift ? '#151719' : '#ffffff';
|
|
|
|
}
|
|
|
|
|
|
|
|
get adjustedAccentColor() {
|
2021-07-28 19:14:24 +03:00
|
|
|
const accentColor = Color(this.settings.get('accentColor'));
|
|
|
|
const backgroundColor = Color(this.backgroundColor);
|
2021-07-26 17:59:10 +03:00
|
|
|
|
|
|
|
// WCAG contrast. 1 = lowest contrast, 21 = highest contrast
|
2021-07-28 19:14:24 +03:00
|
|
|
const accentContrast = accentColor.contrast(backgroundColor);
|
2021-07-26 17:59:10 +03:00
|
|
|
|
|
|
|
if (accentContrast > 2) {
|
2021-07-28 19:14:24 +03:00
|
|
|
return accentColor.hex();
|
2021-07-26 17:59:10 +03:00
|
|
|
}
|
|
|
|
|
2021-07-28 19:14:24 +03:00
|
|
|
let adjustedAccentColor = accentColor;
|
2021-07-26 17:59:10 +03:00
|
|
|
|
|
|
|
if (this.feature.nightShift) {
|
2021-07-28 19:14:24 +03:00
|
|
|
adjustedAccentColor = lightenToContrastThreshold(accentColor, backgroundColor, 2);
|
2021-07-26 17:59:10 +03:00
|
|
|
} else {
|
2021-07-28 19:14:24 +03:00
|
|
|
adjustedAccentColor = darkenToContrastThreshold(accentColor, backgroundColor, 2);
|
2021-07-26 17:59:10 +03:00
|
|
|
}
|
|
|
|
|
2021-07-28 19:14:24 +03:00
|
|
|
return adjustedAccentColor.hex();
|
2021-07-26 17:59:10 +03:00
|
|
|
}
|
|
|
|
|
2021-07-26 19:00:11 +03:00
|
|
|
get textColorForAdjustedAccentBackground() {
|
2021-07-29 17:41:36 +03:00
|
|
|
return textColorForBackgroundColor(this.adjustedAccentColor).hex();
|
2021-07-26 19:00:11 +03:00
|
|
|
}
|
|
|
|
|
2021-07-01 12:11:08 +03:00
|
|
|
constructor() {
|
|
|
|
super(...arguments);
|
2019-05-20 17:33:12 +03:00
|
|
|
|
2019-05-20 16:57:21 +03:00
|
|
|
this.router.on('routeDidChange', (transition) => {
|
|
|
|
updateBodyClasses(transition);
|
2019-05-20 17:33:12 +03:00
|
|
|
|
2019-05-20 18:16:19 +03:00
|
|
|
this.updateDocumentTitle();
|
|
|
|
|
2019-05-20 17:33:12 +03:00
|
|
|
let {newClasses: mainClasses} = collectMetadataClasses(transition, 'mainClasses');
|
2021-07-01 12:11:08 +03:00
|
|
|
this.mainClass = mainClasses.join(' ');
|
2019-05-20 16:57:21 +03:00
|
|
|
});
|
2021-07-01 12:11:08 +03:00
|
|
|
}
|
2019-05-20 16:57:21 +03:00
|
|
|
|
2021-07-01 12:11:08 +03:00
|
|
|
@action
|
2017-08-14 15:30:00 +03:00
|
|
|
closeMenus() {
|
2019-03-06 16:53:54 +03:00
|
|
|
this.dropdown.closeDropdowns();
|
2021-07-01 12:11:08 +03:00
|
|
|
this.showMobileMenu = false;
|
|
|
|
}
|
2017-08-14 15:30:00 +03:00
|
|
|
|
2021-07-01 12:11:08 +03:00
|
|
|
@action
|
2017-08-14 15:30:00 +03:00
|
|
|
closeMobileMenu() {
|
2021-07-01 12:11:08 +03:00
|
|
|
this.showMobileMenu = false;
|
|
|
|
}
|
2017-08-14 15:30:00 +03:00
|
|
|
|
2021-07-01 12:11:08 +03:00
|
|
|
@action
|
2017-08-14 15:30:00 +03:00
|
|
|
openMobileMenu() {
|
2021-07-01 12:11:08 +03:00
|
|
|
this.showMobileMenu = true;
|
|
|
|
}
|
2017-08-14 15:30:00 +03:00
|
|
|
|
2021-07-01 12:11:08 +03:00
|
|
|
@action
|
|
|
|
setMainClass(mainClass) {
|
|
|
|
this.mainClass = mainClass;
|
|
|
|
}
|
2017-08-14 15:30:00 +03:00
|
|
|
|
2021-07-01 12:11:08 +03:00
|
|
|
@action
|
2019-05-20 18:16:19 +03:00
|
|
|
updateDocumentTitle() {
|
|
|
|
let {currentRoute} = this.router;
|
|
|
|
let tokens = [];
|
|
|
|
|
|
|
|
while (currentRoute) {
|
|
|
|
let titleToken = get(currentRoute, 'metadata.titleToken');
|
|
|
|
|
|
|
|
if (typeof titleToken === 'function') {
|
|
|
|
titleToken = titleToken();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (titleToken) {
|
|
|
|
tokens.unshift(titleToken);
|
|
|
|
}
|
|
|
|
|
|
|
|
currentRoute = currentRoute.parent;
|
|
|
|
}
|
|
|
|
|
|
|
|
let blogTitle = this.config.get('blogTitle');
|
|
|
|
|
|
|
|
if (!isEmpty(tokens)) {
|
|
|
|
window.document.title = `${tokens.join(' - ')} - ${blogTitle}`;
|
|
|
|
} else {
|
|
|
|
window.document.title = blogTitle;
|
|
|
|
}
|
2017-08-14 15:30:00 +03:00
|
|
|
}
|
2021-08-31 16:21:00 +03:00
|
|
|
|
|
|
|
@action
|
|
|
|
initBodyDragHandlers() {
|
|
|
|
// when any drag event is occurring we add `data-user-is-dragging` to the
|
|
|
|
// body element so that we can have dropzones start listening to pointer
|
|
|
|
// events allowing us to have interactive elements "underneath" drop zones
|
|
|
|
this.bodyDragEnterHandler = (event) => {
|
|
|
|
if (!event.dataTransfer) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
document.body.dataset.userIsDragging = true;
|
|
|
|
window.clearTimeout(this.dragTimer);
|
|
|
|
};
|
|
|
|
|
|
|
|
this.bodyDragLeaveHandler = (event) => {
|
|
|
|
// only remove document-level "user is dragging" indicator when leaving the document
|
|
|
|
if (event.screenX !== 0 || event.screenY !== 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
window.clearTimeout(this.dragTimer);
|
|
|
|
this.dragTimer = window.setTimeout(() => {
|
|
|
|
delete document.body.dataset.userIsDragging;
|
|
|
|
}, 50);
|
|
|
|
};
|
|
|
|
|
|
|
|
this.cancelDrag = () => {
|
|
|
|
delete document.body.dataset.userIsDragging;
|
|
|
|
};
|
|
|
|
|
|
|
|
document.body.addEventListener('dragenter', this.bodyDragEnterHandler, {capture: true});
|
|
|
|
document.body.addEventListener('dragleave', this.bodyDragLeaveHandler, {capture: true});
|
|
|
|
document.body.addEventListener('dragend', this.cancelDrag, {capture: true});
|
|
|
|
document.body.addEventListener('drop', this.cancelDrag, {capture: true});
|
|
|
|
}
|
|
|
|
|
|
|
|
@action
|
|
|
|
cleanupBodyDragHandlers() {
|
|
|
|
document.body.removeEventListener('dragenter', this.bodyDragEnterHandler, {capture: true});
|
|
|
|
document.body.removeEventListener('dragleave', this.bodyDragLeaveHandler, {capture: true});
|
|
|
|
document.body.removeEventListener('dragend', this.cancelDrag, {capture: true});
|
|
|
|
document.body.removeEventListener('drop', this.cancelDrag, {capture: true});
|
|
|
|
}
|
2021-07-01 12:11:08 +03:00
|
|
|
}
|