Skip to content

Commit

Permalink
remove component folder, refactor events and url
Browse files Browse the repository at this point in the history
  • Loading branch information
Shou committed May 31, 2017
1 parent 717a0f0 commit cf11dc3
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 41 deletions.
21 changes: 17 additions & 4 deletions lib/gui/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ const app = angular.module('Etcher', [
// Components
require('./components/svg-icon/svg-icon'),
require('./components/warning-modal/warning-modal'),
require('./components/safe-webview/safe-webview'),
require('./components/safe-webview'),

// Pages
require('./pages/main/main'),
Expand Down Expand Up @@ -300,13 +300,14 @@ app.controller('HeaderController', function(SelectionStateModel, OSOpenExternalS

});

app.controller('StateController', function($rootScope) {

$rootScope.$on('$stateChangeSuccess', (event, toState, toParams, fromState) => {
app.controller('StateController', function($rootScope, $scope) {
const unregisterStateChange = $rootScope.$on('$stateChangeSuccess', (event, toState, toParams, fromState) => {
this.previousName = fromState.name;
this.currentName = toState.name;
});

$scope.$on('$destroy', unregisterStateChange);

/**
* @summary Get the previous state name
* @function
Expand All @@ -321,6 +322,18 @@ app.controller('StateController', function($rootScope) {
*/
this.previousName = null;

/**
* @summary Get the current state name
* @function
* @public
*
* @returns {String} current state name
*
* @example
* if (StateController.currentName === 'main') {
* console.log('We are on the main screen!');
* }
*/
this.currentName = null;

});
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ const angular = require('angular');
const react = require('react');
const propTypes = require('prop-types');
const react2angular = require('react2angular').react2angular;
const analytics = require('../../modules/analytics');
const packageJSON = require('../../../../package.json');
const analytics = require('../modules/analytics');
const packageJSON = require('../../../package.json');

const MODULE_NAME = 'Etcher.Components.SafeWebview';
const angularSafeWebview = angular.module(MODULE_NAME, []);
Expand Down Expand Up @@ -56,10 +56,24 @@ class SafeWebview extends react.PureComponent {
shouldShow: true
};

const url = new URL(props.src);

// We set the version GET parameter here.
url.searchParams.set(VERSION_PARAM, packageJSON.version);

this.entryHref = url.href;

// Events steal 'this'
this.didFailLoad = _.bind(this.didFailLoad, this);
this.didGetResponseDetails = _.bind(this.didGetResponseDetails, this);

this.eventTuples = [
[ 'did-fail-load', this.didFailLoad ],
[ 'did-get-response-details', this.didGetResponseDetails ],
[ 'new-window', this.constructor.newWindow ],
[ 'console-message', this.constructor.consoleMessage ]
];

// Make a persistent electron session called 'success-banner'
electron.remote.session.fromPartition('persist:success-banner', {

Expand Down Expand Up @@ -88,17 +102,27 @@ class SafeWebview extends react.PureComponent {
componentDidMount() {

// Events React is unaware of have to be handled manually
this.refs.webview.addEventListener('did-fail-load', this.didFailLoad);
this.refs.webview.addEventListener('did-get-response-details', this.didGetResponseDetails);
this.refs.webview.addEventListener('new-window', this.constructor.newWindow);
this.refs.webview.addEventListener('console-message', this.constructor.consoleMessage);
_.map(this.eventTuples, (tuple) => {
this.refs.webview.addEventListener(...tuple);
});

// Use the 'success-banner' session
this.refs.webview.partition = 'persist:success-banner';

// It's important that this comes after the partition setting, otherwise it will
// use another session and we can't change it without destroying the element again
this.refs.webview.src = this.makeURL();
this.refs.webview.src = this.entryHref;
}

/**
* @summary Remove the Webview events
*/
componentWillUnmount() {

// Events that React is unaware of have to be handled manually
_.map(this.eventTuples, (tuple) => {
this.refs.webview.removeEventListener(...tuple);
});
}

/**
Expand All @@ -111,11 +135,11 @@ class SafeWebview extends react.PureComponent {
// Reload the page if it hasn't changed, otherwise reset the source URL,
// because reload interferes with 'src' setting, resetting the 'src' attribute
// to what it was was just prior.
if (this.refs.webview.src === this.makeURL()) {
if (this.refs.webview.src === this.entryHref) {
this.refs.webview.reload();

} else {
this.refs.webview.src = this.makeURL();
this.refs.webview.src = this.entryHref;
}

this.setState({
Expand All @@ -124,18 +148,6 @@ class SafeWebview extends react.PureComponent {
}
}

/**
* @summary Remove the Webview events
*/
componentWillUnmount() {

// Events that React is unaware of have to be handled manually
this.refs.webview.removeEventListener('did-fail-load', this.didFailLoad);
this.refs.webview.removeEventListener('did-get-response-details', this.didGetResponseDetails);
this.refs.webview.removeEventListener('new-window', this.constructor.newWindow);
this.refs.webview.removeEventListener('console-message', this.constructor.consoleMessage);
}

/**
* @summary Set the element state to hidden
*/
Expand Down Expand Up @@ -188,22 +200,6 @@ class SafeWebview extends react.PureComponent {
}
}

/**
* @summary Parse the 'src' prop into an URL and append metadata as GET parameters
* @function
* @public
*
* @returns {URL} url object
*/
makeURL() {
const url = new URL(this.props.src);

// We set the version GET parameter here.
url.searchParams.set(VERSION_PARAM, packageJSON.version);

return url.href;
}

}

SafeWebview.propTypes = {
Expand Down

0 comments on commit cf11dc3

Please sign in to comment.