Code refactoring (5) (es5 -> es2015)

This commit is contained in:
RainLoop Team 2016-07-16 00:29:42 +03:00
parent c148e19ea3
commit cec53b111f
68 changed files with 2210 additions and 2472 deletions

View file

@ -1,177 +1,159 @@
var
_ = require('_'),
$ = require('$'),
ko = require('ko'),
import _ from '_';
import $ from '$';
import ko from 'ko';
Jua = require('Jua'),
import Jua from 'Jua';
Enums = require('Common/Enums'),
Utils = require('Common/Utils'),
Links = require('Common/Links'),
Translator = require('Common/Translator'),
import {SaveSettingsStep, UploadErrorCode, Capa, Magics} from 'Common/Enums';
import {changeTheme, convertThemeName} from 'Common/Utils';
import {userBackground, themePreviewLink, uploadBackground} from 'Common/Links';
import {i18n} from 'Common/Translator';
ThemeStore = require('Stores/Theme'),
import {capa} from 'Storage/Settings';
Settings = require('Storage/Settings'),
import ThemeStore from 'Stores/Theme';
Remote = require('Remote/User/Ajax');
import Remote from 'Remote/User/Ajax';
/**
* @constructor
*/
function ThemesUserSettings()
class ThemesUserSettings
{
this.theme = ThemeStore.theme;
this.themes = ThemeStore.themes;
this.themesObjects = ko.observableArray([]);
constructor() {
this.theme = ThemeStore.theme;
this.themes = ThemeStore.themes;
this.themesObjects = ko.observableArray([]);
this.background = {};
this.background.name = ThemeStore.themeBackgroundName;
this.background.hash = ThemeStore.themeBackgroundHash;
this.background.uploaderButton = ko.observable(null);
this.background.loading = ko.observable(false);
this.background.error = ko.observable('');
this.background = {};
this.background.name = ThemeStore.themeBackgroundName;
this.background.hash = ThemeStore.themeBackgroundHash;
this.background.uploaderButton = ko.observable(null);
this.background.loading = ko.observable(false);
this.background.error = ko.observable('');
this.capaUserBackground = ko.observable(Settings.capa(Enums.Capa.UserBackground));
this.capaUserBackground = ko.observable(capa(Capa.UserBackground));
this.themeTrigger = ko.observable(Enums.SaveSettingsStep.Idle).extend({'throttle': 100});
this.themeTrigger = ko.observable(SaveSettingsStep.Idle).extend({throttle: Magics.Time100ms});
this.iTimer = 0;
this.oThemeAjaxRequest = null;
this.iTimer = 0;
this.oThemeAjaxRequest = null;
this.theme.subscribe(function(sValue) {
_.each(this.themesObjects(), function(oTheme) {
oTheme.selected(sValue === oTheme.name);
});
Utils.changeTheme(sValue, this.themeTrigger);
Remote.saveSettings(null, {
'Theme': sValue
});
}, this);
this.background.hash.subscribe(function(sValue) {
var $oBg = $('#rl-bg');
if (!sValue)
{
if ($oBg.data('backstretch'))
{
$oBg.backstretch('destroy').attr('style', '');
}
}
else
{
$('#rl-bg').attr('style', 'background-image: none !important;').backstretch(
Links.userBackground(sValue), {
'fade': 1000, 'centeredX': true, 'centeredY': true
}).removeAttr('style');
}
}, this);
}
ThemesUserSettings.prototype.onBuild = function()
{
var sCurrentTheme = this.theme();
this.themesObjects(_.map(this.themes(), function(sTheme) {
return {
'name': sTheme,
'nameDisplay': Utils.convertThemeName(sTheme),
'selected': ko.observable(sTheme === sCurrentTheme),
'themePreviewSrc': Links.themePreviewLink(sTheme)
};
}));
this.initUploader();
};
ThemesUserSettings.prototype.onShow = function()
{
this.background.error('');
};
ThemesUserSettings.prototype.clearBackground = function()
{
if (this.capaUserBackground())
{
var self = this;
Remote.clearUserBackground(function() {
self.background.name('');
self.background.hash('');
});
}
};
ThemesUserSettings.prototype.initUploader = function()
{
if (this.background.uploaderButton() && this.capaUserBackground())
{
var
oJua = new Jua({
'action': Links.uploadBackground(),
'name': 'uploader',
'queueSize': 1,
'multipleSizeLimit': 1,
'disableDragAndDrop': true,
'disableMultiple': true,
'clickElement': this.background.uploaderButton()
this.theme.subscribe((value) => {
_.each(this.themesObjects(), (theme) => {
theme.selected(value === theme.name);
});
oJua
.on('onStart', _.bind(function() {
changeTheme(value, this.themeTrigger);
this.background.loading(true);
this.background.error('');
Remote.saveSettings(null, {
'Theme': value
});
});
return true;
}, this))
.on('onComplete', _.bind(function(sId, bResult, oData) {
this.background.loading(false);
if (bResult && sId && oData && oData.Result && oData.Result.Name && oData.Result.Hash)
this.background.hash.subscribe((value) => {
const $bg = $('#rl-bg');
if (!value)
{
if ($bg.data('backstretch'))
{
this.background.name(oData.Result.Name);
this.background.hash(oData.Result.Hash);
$bg.backstretch('destroy').attr('style', '');
}
else
{
this.background.name('');
this.background.hash('');
var sError = '';
if (oData.ErrorCode)
{
switch (oData.ErrorCode)
{
case Enums.UploadErrorCode.FileIsTooBig:
sError = Translator.i18n('SETTINGS_THEMES/ERROR_FILE_IS_TOO_BIG');
break;
case Enums.UploadErrorCode.FileType:
sError = Translator.i18n('SETTINGS_THEMES/ERROR_FILE_TYPE_ERROR');
break;
// no default
}
}
if (!sError && oData.ErrorMessage)
{
sError = oData.ErrorMessage;
}
this.background.error(sError || Translator.i18n('SETTINGS_THEMES/ERROR_UNKNOWN'));
}
return true;
}, this));
}
else
{
$bg.attr('style', 'background-image: none !important;').backstretch(userBackground(value), {
fade: 1000, centeredX: true, centeredY: true
}).removeAttr('style');
}
});
}
};
onBuild() {
const currentTheme = this.theme();
this.themesObjects(_.map(this.themes(), (theme) => ({
name: theme,
nameDisplay: convertThemeName(theme),
selected: ko.observable(theme === currentTheme),
themePreviewSrc: themePreviewLink(theme)
})));
this.initUploader();
}
onShow() {
this.background.error('');
}
clearBackground() {
if (this.capaUserBackground())
{
Remote.clearUserBackground(() => {
this.background.name('');
this.background.hash('');
});
}
}
initUploader() {
if (this.background.uploaderButton() && this.capaUserBackground())
{
const
oJua = new Jua({
'action': uploadBackground(),
'name': 'uploader',
'queueSize': 1,
'multipleSizeLimit': 1,
'disableDragAndDrop': true,
'disableMultiple': true,
'clickElement': this.background.uploaderButton()
});
oJua
.on('onStart', () => {
this.background.loading(true);
this.background.error('');
return true;
})
.on('onComplete', (id, result, data) => {
this.background.loading(false);
if (result && id && data && data.Result && data.Result.Name && data.Result.Hash)
{
this.background.name(data.Result.Name);
this.background.hash(data.Result.Hash);
}
else
{
this.background.name('');
this.background.hash('');
let errorMsg = '';
if (data.ErrorCode)
{
switch (data.ErrorCode)
{
case UploadErrorCode.FileIsTooBig:
errorMsg = i18n('SETTINGS_THEMES/ERROR_FILE_IS_TOO_BIG');
break;
case UploadErrorCode.FileType:
errorMsg = i18n('SETTINGS_THEMES/ERROR_FILE_TYPE_ERROR');
break;
// no default
}
}
if (!errorMsg && data.ErrorMessage)
{
errorMsg = data.ErrorMessage;
}
this.background.error(errorMsg || i18n('SETTINGS_THEMES/ERROR_UNKNOWN'));
}
return true;
});
}
}
}
export {ThemesUserSettings, ThemesUserSettings as default};