piskel/js/controller/settings/SettingsController.js

100 lines
3.0 KiB
JavaScript
Raw Normal View History

(function () {
2014-03-30 05:12:56 +04:00
var ns = $.namespace('pskl.controller.settings');
var settings = {
'user' : {
template : 'templates/settings/application.html',
controller : ns.ApplicationSettingsController
},
2014-03-30 05:12:56 +04:00
'resize' : {
template : 'templates/settings/resize.html',
controller : ns.ResizeController
},
'gif' : {
template : 'templates/settings/export-gif.html',
controller : ns.GifExportController
},
2014-03-30 05:12:56 +04:00
'png' : {
template : 'templates/settings/export-png.html',
controller : ns.PngExportController
},
'import' : {
template : 'templates/settings/import.html',
controller : ns.ImportController
2014-03-30 05:12:56 +04:00
},
'localstorage' : {
template : 'templates/settings/localstorage.html',
controller : ns.LocalStorageController
},
'save' : {
template : 'templates/settings/save.html',
controller : ns.SaveController
}
};
var SEL_SETTING_CLS = 'has-expanded-drawer';
var EXP_DRAWER_CLS = 'expanded';
ns.SettingsController = function (piskelController) {
this.piskelController = piskelController;
2014-03-30 05:12:56 +04:00
this.drawerContainer = document.getElementById('drawer-container');
this.settingsContainer = $('[data-pskl-controller=settings]');
2014-03-30 05:12:56 +04:00
this.isExpanded = false;
this.currentSetting = null;
};
/**
* @public
*/
ns.SettingsController.prototype.init = function() {
2014-03-30 05:12:56 +04:00
$('[data-setting]').click(this.onSettingIconClick.bind(this));
$('body').click(this.onBodyClick.bind(this));
$.subscribe(Events.CLOSE_SETTINGS_DRAWER, this.closeDrawer.bind(this));
};
2014-03-30 05:12:56 +04:00
ns.SettingsController.prototype.onSettingIconClick = function (evt) {
var el = evt.originalEvent.currentTarget;
var setting = el.getAttribute('data-setting');
if (this.currentSetting != setting) {
this.loadSetting(setting);
} else {
this.closeDrawer();
}
evt.originalEvent.stopPropagation();
evt.originalEvent.preventDefault();
};
2014-03-30 05:12:56 +04:00
ns.SettingsController.prototype.onBodyClick = function (evt) {
var target = evt.target;
var isInDrawerContainer = pskl.utils.Dom.isParent(target, this.drawerContainer);
var isInSettingsIcon = target.getAttribute('data-setting');
var isInSettingsContainer = isInDrawerContainer || isInSettingsIcon;
if (this.isExpanded && !isInSettingsContainer) {
this.closeDrawer();
}
};
ns.SettingsController.prototype.loadSetting = function (setting) {
this.drawerContainer.innerHTML = pskl.utils.Template.get(settings[setting].template);
(new settings[setting].controller(this.piskelController)).init();
this.settingsContainer.addClass(EXP_DRAWER_CLS);
$('.' + SEL_SETTING_CLS).removeClass(SEL_SETTING_CLS);
$('[data-setting='+setting+']').addClass(SEL_SETTING_CLS);
2014-03-30 05:12:56 +04:00
this.isExpanded = true;
this.currentSetting = setting;
};
ns.SettingsController.prototype.closeDrawer = function () {
this.settingsContainer.removeClass(EXP_DRAWER_CLS);
$('.' + SEL_SETTING_CLS).removeClass(SEL_SETTING_CLS);
2014-03-30 05:12:56 +04:00
this.isExpanded = false;
this.currentSetting = null;
};
})();