2018-01-11 17:16:42 +03:00
|
|
|
import $ from 'jquery';
|
2016-01-19 17:25:36 +03:00
|
|
|
import Ember from 'ember';
|
2017-08-22 10:53:26 +03:00
|
|
|
import EmberError from '@ember/error';
|
2017-05-29 21:50:03 +03:00
|
|
|
import RSVP from 'rsvp';
|
2017-10-30 12:38:01 +03:00
|
|
|
import Service, {inject as service} from '@ember/service';
|
2017-08-22 10:53:26 +03:00
|
|
|
import {computed} from '@ember/object';
|
|
|
|
import {set} from '@ember/object';
|
2016-01-19 17:25:36 +03:00
|
|
|
|
2018-01-11 17:16:42 +03:00
|
|
|
export function feature(name, options = {}) {
|
|
|
|
let {user, onChange} = options;
|
2017-03-03 19:14:33 +03:00
|
|
|
let watchedProps = user ? [`accessibility.${name}`] : [`config.${name}`, `labs.${name}`];
|
|
|
|
|
|
|
|
return computed.apply(Ember, watchedProps.concat({
|
2016-01-19 17:25:36 +03:00
|
|
|
get() {
|
2018-01-18 18:36:01 +03:00
|
|
|
let enabled = false;
|
|
|
|
|
2017-03-03 19:14:33 +03:00
|
|
|
if (user) {
|
2018-01-18 18:36:01 +03:00
|
|
|
enabled = this.get(`accessibility.${name}`);
|
|
|
|
} else if (this.get(`config.${name}`)) {
|
|
|
|
enabled = this.get(`config.${name}`);
|
|
|
|
} else {
|
|
|
|
enabled = this.get(`labs.${name}`) || false;
|
2017-03-03 19:14:33 +03:00
|
|
|
}
|
|
|
|
|
2018-01-18 18:36:01 +03:00
|
|
|
if (options.developer) {
|
|
|
|
enabled = enabled && this.get('config.enableDeveloperExperiments');
|
2016-05-05 17:03:09 +03:00
|
|
|
}
|
|
|
|
|
2018-01-18 18:36:01 +03:00
|
|
|
return enabled;
|
2016-01-19 17:25:36 +03:00
|
|
|
},
|
|
|
|
set(key, value) {
|
2018-01-18 18:36:01 +03:00
|
|
|
this.update(key, value, options);
|
2018-01-11 17:16:42 +03:00
|
|
|
|
|
|
|
if (onChange) {
|
|
|
|
// value must be passed here because the value isn't set until
|
|
|
|
// the setter function returns
|
|
|
|
this.get(onChange).bind(this)(value);
|
|
|
|
}
|
|
|
|
|
2016-05-05 17:03:09 +03:00
|
|
|
return value;
|
2016-01-19 17:25:36 +03:00
|
|
|
}
|
2017-03-03 19:14:33 +03:00
|
|
|
}));
|
2016-01-19 17:25:36 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
export default Service.extend({
|
2017-10-30 12:38:01 +03:00
|
|
|
store: service(),
|
|
|
|
config: service(),
|
|
|
|
session: service(),
|
|
|
|
settings: service(),
|
|
|
|
notifications: service(),
|
2018-01-11 17:16:42 +03:00
|
|
|
lazyLoader: service(),
|
2016-01-19 17:25:36 +03:00
|
|
|
|
2020-12-02 14:47:34 +03:00
|
|
|
emailAnalytics: feature('emailAnalytics'),
|
2018-01-11 17:16:42 +03:00
|
|
|
nightShift: feature('nightShift', {user: true, onChange: '_setAdminTheme'}),
|
2021-02-04 21:35:19 +03:00
|
|
|
launchComplete: feature('launchComplete', {user: true}),
|
2021-06-07 22:07:45 +03:00
|
|
|
matchHelper: feature('matchHelper'),
|
2021-06-09 13:44:36 +03:00
|
|
|
multipleProducts: feature('multipleProducts', {developer: true}),
|
2021-07-02 13:14:49 +03:00
|
|
|
emailCardSegments: feature('emailCardSegments', {developer: true}),
|
2016-01-19 17:25:36 +03:00
|
|
|
|
2017-03-03 19:14:33 +03:00
|
|
|
_user: null,
|
2016-02-23 18:00:17 +03:00
|
|
|
|
2017-03-17 20:16:21 +03:00
|
|
|
labs: computed('settings.labs', function () {
|
|
|
|
let labs = this.get('settings.labs');
|
2016-01-19 17:25:36 +03:00
|
|
|
|
|
|
|
try {
|
|
|
|
return JSON.parse(labs) || {};
|
|
|
|
} catch (e) {
|
|
|
|
return {};
|
|
|
|
}
|
2016-05-05 17:03:09 +03:00
|
|
|
}),
|
|
|
|
|
2017-03-03 19:14:33 +03:00
|
|
|
accessibility: computed('_user.accessibility', function () {
|
|
|
|
let accessibility = this.get('_user.accessibility');
|
|
|
|
|
|
|
|
try {
|
|
|
|
return JSON.parse(accessibility) || {};
|
|
|
|
} catch (e) {
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
}),
|
|
|
|
|
2016-05-05 17:03:09 +03:00
|
|
|
fetch() {
|
2017-03-03 19:14:33 +03:00
|
|
|
return RSVP.hash({
|
2019-03-06 16:53:54 +03:00
|
|
|
settings: this.settings.fetch(),
|
2017-03-03 19:14:33 +03:00
|
|
|
user: this.get('session.user')
|
2017-03-17 20:16:21 +03:00
|
|
|
}).then(({user}) => {
|
2017-03-03 19:14:33 +03:00
|
|
|
this.set('_user', user);
|
2018-01-11 17:16:42 +03:00
|
|
|
return this._setAdminTheme().then(() => true);
|
2016-05-05 17:03:09 +03:00
|
|
|
});
|
2016-01-19 17:25:36 +03:00
|
|
|
},
|
|
|
|
|
2018-01-18 18:36:01 +03:00
|
|
|
update(key, value, options = {}) {
|
|
|
|
let serviceProperty = options.user ? 'accessibility' : 'labs';
|
|
|
|
let model = this.get(options.user ? '_user' : 'settings');
|
2017-03-03 19:14:33 +03:00
|
|
|
let featureObject = this.get(serviceProperty);
|
|
|
|
|
|
|
|
// set the new key value for either the labs property or the accessibility property
|
|
|
|
set(featureObject, key, value);
|
2016-05-05 17:03:09 +03:00
|
|
|
|
2017-03-03 19:14:33 +03:00
|
|
|
// update the 'labs' or 'accessibility' key of the model
|
|
|
|
model.set(serviceProperty, JSON.stringify(featureObject));
|
2016-05-05 17:03:09 +03:00
|
|
|
|
2017-03-03 19:14:33 +03:00
|
|
|
return model.save().then(() => {
|
2016-05-05 17:03:09 +03:00
|
|
|
// return the labs key value that we get from the server
|
2017-03-03 19:14:33 +03:00
|
|
|
this.notifyPropertyChange(serviceProperty);
|
|
|
|
return this.get(`${serviceProperty}.${key}`);
|
2016-06-30 17:45:02 +03:00
|
|
|
}).catch((error) => {
|
2017-03-03 19:14:33 +03:00
|
|
|
model.rollbackAttributes();
|
|
|
|
this.notifyPropertyChange(serviceProperty);
|
2016-05-05 17:03:09 +03:00
|
|
|
|
|
|
|
// we'll always have an errors object unless we hit a
|
|
|
|
// validation error
|
2016-06-30 17:45:02 +03:00
|
|
|
if (!error) {
|
2018-01-18 18:36:01 +03:00
|
|
|
throw new EmberError(`Validation of the feature service ${options.user ? 'user' : 'settings'} model failed when updating ${serviceProperty}.`);
|
2016-05-05 17:03:09 +03:00
|
|
|
}
|
|
|
|
|
2019-03-06 16:53:54 +03:00
|
|
|
this.notifications.showAPIError(error);
|
2016-05-05 17:03:09 +03:00
|
|
|
|
2017-03-03 19:14:33 +03:00
|
|
|
return this.get(`${serviceProperty}.${key}`);
|
2016-01-19 17:25:36 +03:00
|
|
|
});
|
2018-01-11 17:16:42 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
_setAdminTheme(enabled) {
|
2018-03-09 12:34:43 +03:00
|
|
|
let nightShift = enabled;
|
|
|
|
|
|
|
|
if (typeof nightShift === 'undefined') {
|
2019-03-06 16:53:54 +03:00
|
|
|
nightShift = enabled || this.nightShift;
|
2018-03-09 12:34:43 +03:00
|
|
|
}
|
2018-01-11 17:16:42 +03:00
|
|
|
|
2019-03-06 16:53:54 +03:00
|
|
|
return this.lazyLoader.loadStyle('dark', 'assets/ghost-dark.css', true).then(() => {
|
2018-01-11 17:16:42 +03:00
|
|
|
$('link[title=dark]').prop('disabled', !nightShift);
|
2019-06-18 14:54:37 +03:00
|
|
|
}).catch(() => {
|
|
|
|
//TODO: Also disable toggle from settings and Labs hover
|
|
|
|
$('link[title=dark]').prop('disabled', true);
|
2018-01-11 17:16:42 +03:00
|
|
|
});
|
2016-01-19 17:25:36 +03:00
|
|
|
}
|
|
|
|
});
|