piano/js/app.js

96 lines
2.3 KiB
JavaScript
Raw Normal View History

2014-11-09 18:12:03 +00:00
/*jshint browser:true */
2014-11-08 17:52:36 +00:00
/*globals MIDI, React, App, $ */
/**
* app.js
*
* Bootstrap app:
* - connect to MIDI device;
* - config vendors;
* - instantiate main component;
* - integrate with shell.
*/
(function () {
'use strict';
var remote, ipc, shell;
// shell integration
if (window.atom) {
remote = window.atom.require('remote');
shell = window.atom.require('shell');
ipc = window.atom.require('ipc');
}
// configs
window.vex.defaultOptions.className = 'dialog-theme';
window.vex.dialog.buttons.YES.text = 'OK';
window.vex.dialog.buttons.NO.text = 'Annuler';
// bootstrap
function bootstrap() {
var main = React.renderComponent(
App.components.Main(),
$('#content')[0]
);
// we're ready to receive shell options
// and to show the window
if (ipc) {
ipc.send('ready');
ipc.on('options', function (options) {
main.setProps({
url: options.file
});
});
ipc.on('visible', function (state) {
if (state) {
main.resume();
} else {
main.halt();
}
});
window.onkeydown = function (e) {
if (e.keyCode === 123 && remote) {
remote.getCurrentWindow().toggleDevTools();
}
};
}
// handle links in default navigator, not in app
if (shell) {
$(document).on('click', 'a', function (e) {
shell.openExternal($(this).attr('href'));
e.preventDefault();
});
}
}
// on error, offer choice to retry or ignore
function error(err) {
var modal = App.components.Error({
title: "Erreur d'initialisation",
error: err,
canRetry: true,
onRetry: function () {
window.location.reload();
},
onClose: function () {
bootstrap();
}
});
ipc.send('ready');
modal.open();
}
// establish connection to MIDI device
App.MIDI.connect()
.then(bootstrap)
.catch(error);
}());