jellyfin-web/dashboard-ui/bower_components/emby-webcomponents/usersettings/usersettingsbuilder.js

162 lines
4.2 KiB
JavaScript
Raw Normal View History

2016-09-09 12:03:03 -07:00
define(['appSettings', 'events', 'browser'], function (appsettings, events, browser) {
2016-03-01 21:46:10 -07:00
2016-09-05 23:45:22 -07:00
return function () {
2016-03-01 21:46:10 -07:00
var self = this;
2016-09-05 23:45:22 -07:00
var currentUserId;
var currentApiClient;
var displayPrefs;
2016-03-01 21:46:10 -07:00
2016-09-05 23:45:22 -07:00
self.setUserInfo = function (userId, apiClient) {
2016-03-01 21:46:10 -07:00
2016-09-05 23:45:22 -07:00
currentUserId = userId;
currentApiClient = apiClient;
2016-03-01 21:46:10 -07:00
2016-09-05 23:45:22 -07:00
if (!userId) {
displayPrefs = null;
return Promise.resolve();
}
2016-09-05 23:45:22 -07:00
return apiClient.getDisplayPreferences('usersettings', userId, 'emby').then(function (result) {
result.CustomPrefs = result.CustomPrefs || {};
displayPrefs = result;
});
};
var saveTimeout;
function onSaveTimeout() {
saveTimeout = null;
2016-09-05 23:45:22 -07:00
currentApiClient.updateDisplayPreferences('usersettings', displayPrefs, currentUserId, 'emby');
}
function saveServerPreferences() {
if (saveTimeout) {
clearTimeout(saveTimeout);
}
saveTimeout = setTimeout(onSaveTimeout, 50);
}
2016-03-01 21:46:10 -07:00
2016-09-05 23:45:22 -07:00
self.set = function (name, value, enableOnServer) {
2016-03-01 21:46:10 -07:00
2016-09-05 23:45:22 -07:00
var userId = currentUserId;
2016-03-01 21:46:10 -07:00
if (!userId) {
throw new Error('userId cannot be null');
}
var currentValue = self.get(name);
appsettings.set(name, value, userId);
2016-09-05 23:45:22 -07:00
if (enableOnServer !== false && displayPrefs) {
displayPrefs.CustomPrefs[name] = value == null ? value : value.toString();
saveServerPreferences();
}
2016-03-01 21:46:10 -07:00
if (currentValue != value) {
events.trigger(self, 'change', [name]);
}
};
2016-09-05 23:45:22 -07:00
self.get = function (name, enableOnServer) {
var userId = currentUserId;
2016-03-01 21:46:10 -07:00
if (!userId) {
throw new Error('userId cannot be null');
}
2016-09-05 23:45:22 -07:00
if (enableOnServer !== false) {
if (displayPrefs) {
return displayPrefs.CustomPrefs[name];
}
}
2016-03-01 21:46:10 -07:00
return appsettings.get(name, userId);
};
self.enableCinemaMode = function (val) {
if (val != null) {
self.set('enableCinemaMode', val.toString());
}
2016-09-05 23:45:22 -07:00
val = self.get('enableCinemaMode', false);
2016-03-01 21:46:10 -07:00
if (val) {
return val != 'false';
}
return true;
};
2016-09-09 12:03:03 -07:00
self.enableThemeSongs = function (val) {
if (val != null) {
self.set('enableThemeSongs', val.toString());
}
val = self.get('enableThemeSongs', false);
if (val) {
return val != 'false';
}
return true;
};
self.enableThemeVideos = function (val) {
if (val != null) {
self.set('enableThemeVideos', val.toString());
}
val = self.get('enableThemeVideos', false);
if (val) {
return val != 'false';
}
return !browser.slow;
};
2016-03-01 21:46:10 -07:00
self.language = function (val) {
if (val != null) {
self.set('language', val.toString());
}
return self.get('language');
};
2016-03-06 21:56:45 -07:00
self.skipBackLength = function (val) {
2016-03-04 20:39:49 -07:00
if (val != null) {
2016-03-06 21:56:45 -07:00
self.set('skipBackLength', val.toString());
2016-03-04 20:39:49 -07:00
}
2016-03-06 21:56:45 -07:00
return parseInt(self.get('skipBackLength') || '15000');
};
self.skipForwardLength = function (val) {
if (val != null) {
self.set('skipForwardLength', val.toString());
}
return parseInt(self.get('skipForwardLength') || '15000');
2016-03-04 20:39:49 -07:00
};
2016-03-01 21:46:10 -07:00
self.serverConfig = function (config) {
2016-09-05 23:45:22 -07:00
var apiClient = currentApiClient;
2016-07-13 11:23:01 -07:00
2016-03-01 21:46:10 -07:00
if (config) {
2016-09-05 23:45:22 -07:00
return apiClient.updateUserConfiguration(currentUserId, config);
2016-03-01 21:46:10 -07:00
} else {
2016-09-05 23:45:22 -07:00
return apiClient.getUser(currentUserId).then(function (user) {
2016-03-01 21:46:10 -07:00
return user.Configuration;
});
}
};
};
2016-03-01 21:46:10 -07:00
});