snappymail/dev/View/Popup/Languages.js

114 lines
2.3 KiB
JavaScript
Raw Normal View History

2014-09-05 06:49:03 +08:00
(function () {
2014-08-25 23:49:01 +08:00
'use strict';
2014-08-21 23:08:34 +08:00
var
2014-08-25 23:49:01 +08:00
_ = require('_'),
ko = require('ko'),
2014-08-25 15:10:51 +08:00
2014-09-05 06:49:03 +08:00
Utils = require('Common/Utils'),
2014-08-21 23:08:34 +08:00
kn = require('Knoin/Knoin'),
AbstractView = require('Knoin/AbstractView')
2014-08-21 23:08:34 +08:00
;
/**
* @constructor
* @extends AbstractView
2014-08-21 23:08:34 +08:00
*/
function LanguagesPopupView()
2014-08-21 23:08:34 +08:00
{
AbstractView.call(this, 'Popups', 'PopupsLanguages');
2014-08-21 23:08:34 +08:00
this.LanguageStore = require('Stores/Language');
2014-08-21 23:08:34 +08:00
this.exp = ko.observable(false);
this.languages = ko.computed(function () {
return _.map(this.LanguageStore.languages(), function (sLanguage) {
2014-08-21 23:08:34 +08:00
return {
'key': sLanguage,
'selected': ko.observable(false),
'fullName': Utils.convertLangName(sLanguage)
};
});
}, this);
2014-08-21 23:08:34 +08:00
2015-03-16 21:54:56 +08:00
this.languagesTop = ko.computed(function () {
var
aTop = this.LanguageStore.languagesTop(),
aLangs = this.languages()
;
return 0 < aTop.length ? _.compact(_.map(aTop, function (sLang) {
return _.find(aLangs, function (aItem) {
return aItem && sLang === aItem.key;
});
})) : [];
}, this);
this.languagesBottom = ko.computed(function () {
var
aTop = this.languagesTop(),
aLangs = this.languages()
;
if (0 < aTop.length)
{
return _.filter(aLangs, function (aItem) {
return -1 === Utils.inArray(aItem, aTop);
});
}
return aLangs;
}, this);
this.LanguageStore.language.subscribe(function () {
2014-08-21 23:08:34 +08:00
this.resetMainLanguage();
}, this);
kn.constructorEnd(this);
}
kn.extendAsViewModel(['View/Popup/Languages', 'PopupsLanguagesViewModel'], LanguagesPopupView);
_.extend(LanguagesPopupView.prototype, AbstractView.prototype);
2014-08-21 23:08:34 +08:00
LanguagesPopupView.prototype.languageEnName = function (sLanguage)
2014-08-21 23:08:34 +08:00
{
var sResult = Utils.convertLangName(sLanguage, true);
return 'English' === sResult ? '' : sResult;
2014-08-21 23:08:34 +08:00
};
LanguagesPopupView.prototype.resetMainLanguage = function ()
2014-08-21 23:08:34 +08:00
{
var sCurrent = this.LanguageStore.language();
2014-08-21 23:08:34 +08:00
_.each(this.languages(), function (oItem) {
oItem['selected'](oItem['key'] === sCurrent);
});
2014-08-21 23:08:34 +08:00
};
LanguagesPopupView.prototype.onShow = function ()
2014-08-21 23:08:34 +08:00
{
this.exp(true);
this.resetMainLanguage();
2014-08-21 23:08:34 +08:00
};
LanguagesPopupView.prototype.onHide = function ()
2014-08-21 23:08:34 +08:00
{
this.exp(false);
};
LanguagesPopupView.prototype.changeLanguage = function (sLang)
2014-08-21 23:08:34 +08:00
{
this.LanguageStore.language(sLang);
2014-08-21 23:08:34 +08:00
this.cancelCommand();
};
module.exports = LanguagesPopupView;
2014-08-21 23:08:34 +08:00
2014-09-05 06:49:03 +08:00
}());