Ghost/ghost/admin/app/components/gh-nav-menu.js
Kevin Ansfield 352c4af1d7 Refactored usage of .get('property') with es5 getters
no issue
- ran [es5-getter-ember-codemod](https://github.com/rondale-sc/es5-getter-ember-codemod)
- [es5 getters RFC](https://github.com/emberjs/rfcs/blob/master/text/0281-es5-getters.md)
- updates the majority of `object.get('property')` with `object.property` with exceptions:
  - `.get('nested.property')` - it's not possible to determine if this is relying on "safe" path chaining for when `nested` doesn't exist
  - `.get('config.x')` and `.get('settings.x')` - both our `config` and `settings` services are proxy objects which do not support es5 getters
- this PR is not exhaustive, there are still a number of places where `.get('service.foo')` and similar could be replaced but it gets us a long way there in a quick and automated fashion
2019-03-06 13:54:14 +00:00

82 lines
2.8 KiB
JavaScript

import Component from '@ember/component';
import calculatePosition from 'ember-basic-dropdown/utils/calculate-position';
import {computed} from '@ember/object';
import {htmlSafe} from '@ember/string';
import {inject as service} from '@ember/service';
export default Component.extend({
config: service(),
feature: service(),
ghostPaths: service(),
router: service('router'),
session: service(),
ui: service(),
tagName: 'nav',
classNames: ['gh-nav'],
iconStyle: '',
showMenuExtension: computed('config.clientExtensions.menu', 'session.user.isOwner', function () {
return this.get('config.clientExtensions.menu') && this.get('session.user.isOwner');
}),
showDropdownExtension: computed('config.clientExtensions.dropdown', 'session.user.isOwner', function () {
return this.get('config.clientExtensions.dropdown') && this.get('session.user.isOwner');
}),
showScriptExtension: computed('config.clientExtensions.script', 'session.user.isOwner', function () {
return this.get('config.clientExtensions.script') && this.get('session.user.isOwner');
}),
isIntegrationRoute: computed('router.currentRouteName', function () {
let re = /^settings\.integration/;
return re.test(this.router.currentRouteName);
}),
// the menu has a rendering issue (#8307) when the the world is reloaded
// during an import which we have worked around by not binding the icon
// style directly. However we still need to keep track of changing icons
// so that we can refresh when a new icon is uploaded
didReceiveAttrs() {
this._setIconStyle();
},
// equivalent to "left: auto; right: -20px"
userDropdownPosition(trigger, dropdown) {
let {horizontalPosition, verticalPosition, style} = calculatePosition(...arguments);
let {width: dropdownWidth} = dropdown.firstElementChild.getBoundingClientRect();
style.right += (dropdownWidth - 20);
style['z-index'] = 1100;
return {horizontalPosition, verticalPosition, style};
},
_setIconStyle() {
let icon = this.icon;
if (icon === this._icon) {
return;
}
this._icon = icon;
if (icon && icon.match(/^https?:\/\//i)) {
this.set('iconStyle', htmlSafe(`background-image: url(${icon})`));
return;
}
let subdirRegExp = new RegExp(`^${this.get('ghostPaths.subdir')}`);
let blogIcon = icon ? icon : 'favicon.ico';
let iconUrl;
blogIcon = blogIcon.replace(subdirRegExp, '');
iconUrl = this.get('ghostPaths.url').join(this.get('config.blogUrl'), blogIcon).replace(/\/$/, '');
iconUrl += `?t=${(new Date()).valueOf()}`;
this.set('iconStyle', htmlSafe(`background-image: url(${iconUrl})`));
}
});