Skip to content

Commit

Permalink
Issue #287 : Move CheatsheetService to CheatsheetController + Dialogs…
Browse files Browse the repository at this point in the history
…Controller
  • Loading branch information
juliandescottes committed Oct 10, 2015
1 parent 628dd65 commit dffb43a
Show file tree
Hide file tree
Showing 12 changed files with 190 additions and 216 deletions.
18 changes: 0 additions & 18 deletions src/css/cheatsheet.css
Original file line number Diff line number Diff line change
Expand Up @@ -23,25 +23,7 @@
opacity: 1;
}

#cheatsheet-wrapper {
position: absolute;
z-index: 10000;

top: 50px;
right: 50px;
bottom: 50px;
left: 50px;

box-sizing: border-box;
-moz-box-sizing : border-box;

color: white;
}

.cheatsheet-container {
width: 100%;
height: 100%;

box-sizing: border-box;
-moz-box-sizing : border-box;

Expand Down
9 changes: 9 additions & 0 deletions src/css/dialogs.css
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,22 @@
position : relative;
}

.dialog-content {
position: absolute;
top: 45px;
bottom: 0;
width: 100%;
box-sizing: border-box;
}

.dialog-head {
width: 100%;
background: gold;
margin: 0;
padding: 10px;
color: black;
font-size: 1.8em;
height: 45px;
box-sizing: border-box;
-moz-box-sizing: border-box;
}
Expand Down
4 changes: 3 additions & 1 deletion src/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -90,13 +90,15 @@
<iframe src="templates/dialogs/create-palette.html" onload="iframeloader.onLoad(event)" data-iframe-loader="store"></iframe>
<iframe src="templates/dialogs/import-image.html" onload="iframeloader.onLoad(event)" data-iframe-loader="store"></iframe>
<iframe src="templates/dialogs/browse-local.html" onload="iframeloader.onLoad(event)" data-iframe-loader="store"></iframe>
<iframe src="templates/dialogs/cheatsheet.html" onload="iframeloader.onLoad(event)" data-iframe-loader="store"></iframe>
</div>
</div>

<iframe src="templates/cheatsheet.html" onload="iframeloader.onLoad(event)" data-iframe-loader="display"></iframe>
<iframe src="templates/misc-templates.html" onload="iframeloader.onLoad(event)" data-iframe-loader="display"></iframe>
<iframe src="templates/popup-preview.html" onload="iframeloader.onLoad(event)" data-iframe-loader="display"></iframe>

<span class="cheatsheet-link" rel="tooltip" data-placement="right" title="Keyboard shortcuts">&nbsp;</span>

<script type="text/javascript" src="piskel-boot.js"></script>
<!--body-main-end-->
<!-- the comment above indicates the end of the markup reused by the editor integrated in piskelapp.com -->
Expand Down
3 changes: 0 additions & 3 deletions src/js/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,9 +127,6 @@
this.imageUploadService = new pskl.service.ImageUploadService();
this.imageUploadService.init();

this.cheatsheetService = new pskl.service.keyboard.CheatsheetService();
this.cheatsheetService.init();

this.savedStatusService = new pskl.service.SavedStatusService(this.piskelController);
this.savedStatusService.init();

Expand Down
4 changes: 3 additions & 1 deletion src/js/controller/PalettesListController.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,9 @@
};

ns.PalettesListController.prototype.onCreatePaletteClick_ = function (evt) {
$.publish(Events.DIALOG_DISPLAY, 'create-palette');
$.publish(Events.DIALOG_DISPLAY, {
dialogId : 'create-palette'
});
};

ns.PalettesListController.prototype.onEditPaletteClick_ = function (evt) {
Expand Down
93 changes: 93 additions & 0 deletions src/js/controller/dialogs/CheatsheetController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
(function () {
var ns = $.namespace('pskl.controller.dialogs');

ns.CheatsheetController = function () {
this.shortcuts = pskl.service.keyboard.Shortcuts;
};

pskl.utils.inherit(ns.CheatsheetController, ns.AbstractDialogController);

ns.CheatsheetController.prototype.init = function () {
this.cheatsheetEl = document.getElementById('cheatsheetContainer');
if (!this.cheatsheetEl) {
throw 'cheatsheetEl DOM element could not be retrieved';
}
console.log('>>>>>> CheatsheetController INIT');
this.initMarkup_();
};

ns.CheatsheetController.prototype.initMarkup_ = function () {
this.initMarkupForTools_();
this.initMarkupForMisc_();
this.initMarkupForColors_();
this.initMarkupForSelection_();
};

ns.CheatsheetController.prototype.initMarkupForTools_ = function () {
var descriptors = this.createShortcutDescriptors_(this.shortcuts.TOOL, this.getToolShortcutClassname_);
this.initMarkupForDescriptors_(descriptors, '.cheatsheet-tool-shortcuts');
};

ns.CheatsheetController.prototype.getToolShortcutClassname_ = function (shortcut) {
return 'tool-icon ' + shortcut.getId();
};

ns.CheatsheetController.prototype.initMarkupForMisc_ = function () {
var descriptors = this.createShortcutDescriptors_(this.shortcuts.MISC);
this.initMarkupForDescriptors_(descriptors, '.cheatsheet-misc-shortcuts');
};

ns.CheatsheetController.prototype.initMarkupForColors_ = function () {
var descriptors = this.createShortcutDescriptors_(this.shortcuts.COLOR);
this.initMarkupForDescriptors_(descriptors, '.cheatsheet-colors-shortcuts');
};

ns.CheatsheetController.prototype.initMarkupForSelection_ = function () {
var descriptors = this.createShortcutDescriptors_(this.shortcuts.SELECTION);
this.initMarkupForDescriptors_(descriptors, '.cheatsheet-selection-shortcuts');
};

ns.CheatsheetController.prototype.createShortcutDescriptors_ = function (shortcutMap, classnameProvider) {
return Object.keys(shortcutMap).map(function (shortcutKey) {
var shortcut = shortcutMap[shortcutKey];
var classname = typeof classnameProvider == 'function' ? classnameProvider(shortcut) : '';
return this.toDescriptor_(shortcut.getKey(), shortcut.getDescription(), classname);
}.bind(this));
};

ns.CheatsheetController.prototype.toDescriptor_ = function (key, description, icon) {
if (pskl.utils.UserAgent.isMac) {
key = key.replace('ctrl', 'cmd');
}
key = key.replace('up', '&#65514;');
key = key.replace('down', '&#65516;');
key = key.replace(/>/g, '&gt;');
key = key.replace(/</g, '&lt;');
key = key.replace(/^(.*[^ ])\+([^ ].*)$/g, '$1 + $2');

return {
'key' : key,
'description' : description,
'icon' : icon
};
};

ns.CheatsheetController.prototype.initMarkupForDescriptors_ = function (descriptors, containerSelector) {
var container = document.querySelector(containerSelector);
descriptors.forEach(function (descriptor) {
var shortcut = this.getDomFromDescriptor_(descriptor);
container.appendChild(shortcut);
}.bind(this));
};

ns.CheatsheetController.prototype.getDomFromDescriptor_ = function (descriptor) {
var shortcutTemplate = pskl.utils.Template.get('cheatsheet-shortcut-template');
var markup = pskl.utils.Template.replace(shortcutTemplate, {
shortcutIcon : descriptor.icon,
shortcutDescription : descriptor.description,
shortcutKey : descriptor.key
});

return pskl.utils.Template.createFromHTML(markup);
};
})();
115 changes: 71 additions & 44 deletions src/js/controller/dialogs/DialogsController.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
var ns = $.namespace('pskl.controller.dialogs');

var dialogs = {
'cheatsheet' : {
template : 'templates/dialogs/cheatsheet.html',
controller : ns.CheatsheetController
},
'create-palette' : {
template : 'templates/dialogs/create-palette.html',
controller : ns.CreatePaletteController
Expand All @@ -25,78 +29,101 @@
ns.DialogsController.prototype.init = function () {
this.dialogContainer_ = document.getElementById('dialog-container');
this.dialogWrapper_ = document.getElementById('dialog-container-wrapper');

$.subscribe(Events.DIALOG_DISPLAY, this.onDialogDisplayEvent_.bind(this));
$.subscribe(Events.DIALOG_HIDE, this.onDialogHideEvent_.bind(this));
$.subscribe(Events.DIALOG_HIDE, this.hideDialog.bind(this));

// TODO : JD : should be moved to a main controller
var createPaletteShortcut = pskl.service.keyboard.Shortcuts.COLOR.CREATE_PALETTE;
pskl.app.shortcutService.registerShortcut(createPaletteShortcut, this.onCreatePaletteShortcut_.bind(this));

var cheatsheetShortcut = pskl.service.keyboard.Shortcuts.MISC.CHEATSHEET;
pskl.app.shortcutService.registerShortcut(cheatsheetShortcut, this.onCheatsheetShortcut_.bind(this));
pskl.utils.Event.addEventListener('.cheatsheet-link', 'click', this.onCheatsheetShortcut_, this);

// adding the .animated class here instead of in the markup to avoid an animation during app startup
this.dialogWrapper_.classList.add('animated');
};

ns.DialogsController.prototype.onCreatePaletteShortcut_ = function () {
this.onDialogDisplayEvent_(null, 'create-palette');
this.toggleDialog_('create-palette');
};

ns.DialogsController.prototype.onDialogDisplayEvent_ = function (evt, args) {
var dialogId, initArgs;
if (typeof args === 'string') {
dialogId = args;
} else {
dialogId = args.dialogId;
initArgs = args.initArgs;
}
if (!this.isDisplayed()) {
var config = dialogs[dialogId];
if (config) {
this.dialogContainer_.classList.add(dialogId);
this.dialogContainer_.innerHTML = pskl.utils.Template.get(config.template);

var controller = new config.controller(this.piskelController);
controller.init(initArgs);

this.showDialogWrapper_();
this.currentDialog_ = {
id : dialogId,
controller : controller
};
} else {
console.error('Could not find dialog configuration for dialogId : ' + dialogId);
}
ns.DialogsController.prototype.onCheatsheetShortcut_ = function () {
this.toggleDialog_('cheatsheet');
};

/**
* If no dialog is currently displayed, the dialog with the provided id will be displayed.
* If a dialog is displayed and has the same id as the provided id, hide it.
* Otherwise, no-op.
*/
ns.DialogsController.prototype.toggleDialog_ = function (dialogId) {
if (!this.isDisplayingDialog_()) {
this.showDialog(dialogId);
} else if (this.getCurrentDialogId_() === dialogId) {
this.hideDialog();
}
};

ns.DialogsController.prototype.onDialogHideEvent_ = function () {
this.hideDialog();
ns.DialogsController.prototype.onDialogDisplayEvent_ = function (evt, args) {
this.showDialog(args.dialogId, args.initArgs);
};

ns.DialogsController.prototype.showDialogWrapper_ = function () {
ns.DialogsController.prototype.showDialog = function (dialogId, initArgs) {
if (this.isDisplayingDialog_()) {
return;
}

var config = dialogs[dialogId];
if (!config) {
console.error('Could not find dialog configuration for dialogId : ' + dialogId);
return;
}

this.dialogContainer_.classList.add(dialogId);

this.dialogContainer_.innerHTML = pskl.utils.Template.get(config.template);
var controller = new config.controller(this.piskelController);
controller.init(initArgs);

this.currentDialog_ = {
id : dialogId,
controller : controller
};

pskl.app.shortcutService.registerShortcut(this.closePopupShortcut, this.hideDialog.bind(this));
this.dialogWrapper_.classList.add('show');
};

ns.DialogsController.prototype.hideDialog = function () {
var currentDialog = this.currentDialog_;
if (currentDialog) {
currentDialog.controller.destroy();
var dialogId = this.currentDialog_.id;
window.setTimeout(function () {
this.dialogContainer_.classList.remove(dialogId);
}.bind(this), 800);
if (this.isHiding_ || !this.isDisplayingDialog_()) {
return;
}

this.hideDialogWrapper_();
this.currentDialog_ = null;
};

ns.DialogsController.prototype.hideDialogWrapper_ = function () {
pskl.app.shortcutService.unregisterShortcut(this.closePopupShortcut);
this.dialogWrapper_.classList.remove('show');
window.setTimeout(this.cleanupDialogContainer_.bind(this), 500);
this.isHiding_ = true;
};

ns.DialogsController.prototype.cleanupDialogContainer_ = function () {
this.dialogContainer_.classList.remove(this.currentDialog_.id);
this.currentDialog_.controller.destroy();
this.currentDialog_ = null;

this.dialogContainer_.innerHTML = '';
this.isHiding_ = false;
};

ns.DialogsController.prototype.isDisplayed = function () {
ns.DialogsController.prototype.isDisplayingDialog_ = function () {
return this.currentDialog_ !== null;
};

ns.DialogsController.prototype.getCurrentDialogId_ = function () {
if (this.currentDialog_) {
return this.currentDialog_.id;
}
return null;
};

})();
4 changes: 3 additions & 1 deletion src/js/controller/settings/ImportController.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,9 @@
};

ns.ImportController.prototype.onBrowseLocalClick_ = function (evt) {
$.publish(Events.DIALOG_DISPLAY, 'browse-local');
$.publish(Events.DIALOG_DISPLAY, {
dialogId : 'browse-local'
});
this.closeDrawer_();
};

Expand Down
Loading

0 comments on commit dffb43a

Please sign in to comment.