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-10-30 12:38:01 +03:00
|
|
|
import Service, {inject as service} from '@ember/service';
|
2022-01-21 22:25:47 +03:00
|
|
|
import {computed, 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
|
|
|
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-09-09 14:38:05 +03:00
|
|
|
multipleProducts: feature('multipleProducts'),
|
2021-12-02 13:34:35 +03:00
|
|
|
oauthLogin: feature('oauthLogin'),
|
|
|
|
membersActivity: feature('membersActivity'),
|
|
|
|
urlCache: feature('urlCache'),
|
2021-12-02 13:38:16 +03:00
|
|
|
beforeAfterCard: feature('beforeAfterCard'),
|
2021-12-08 12:13:54 +03:00
|
|
|
tweetGridCard: feature('tweetGridCard'),
|
2022-01-17 15:24:40 +03:00
|
|
|
membersActivityFeed: feature('membersActivityFeed'),
|
2022-01-31 14:31:58 +03:00
|
|
|
improvedOnboarding: feature('improvedOnboarding'),
|
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() {
|
2021-07-08 16:37:31 +03:00
|
|
|
return this.settings.fetch().then(() => {
|
|
|
|
this.set('_user', this.session.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
|
|
|
|
2021-10-12 17:33:03 +03:00
|
|
|
if (options.requires && value === true) {
|
|
|
|
options.requires.forEach((flag) => {
|
|
|
|
set(featureObject, flag, true);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
});
|