refactor(GUI): remove angular dependency from selection-state (#1509)

We remove the dependency on Angular from SelectionStateModel and rename
it to selectionState.
This commit is contained in:
Benedict Aas 2017-06-12 13:40:52 +01:00 committed by Juan Cruz Viotti
parent cff445b64b
commit ef739fb222
16 changed files with 1235 additions and 1284 deletions

View File

@ -41,6 +41,7 @@ const windowProgress = require('./os/window-progress');
const analytics = require('./modules/analytics'); const analytics = require('./modules/analytics');
const updateNotifier = require('./components/update-notifier'); const updateNotifier = require('./components/update-notifier');
const availableDrives = require('./models/available-drives'); const availableDrives = require('./models/available-drives');
const selectionState = require('./models/selection-state');
const Store = require('./models/store'); const Store = require('./models/store');
@ -53,9 +54,6 @@ const app = angular.module('Etcher', [
require('./modules/error'), require('./modules/error'),
require('./modules/drive-scanner'), require('./modules/drive-scanner'),
// Models
require('./models/selection-state'),
// Components // Components
require('./components/svg-icon/svg-icon'), require('./components/svg-icon/svg-icon'),
require('./components/warning-modal/warning-modal'), require('./components/warning-modal/warning-modal'),
@ -89,7 +87,7 @@ app.run(() => {
].join('\n')); ].join('\n'));
}); });
app.run((ErrorService, SelectionStateModel) => { app.run((ErrorService) => {
analytics.logEvent('Application start'); analytics.logEvent('Application start');
const currentVersion = packageJSON.version; const currentVersion = packageJSON.version;
@ -139,7 +137,7 @@ app.run((ErrorService, SelectionStateModel) => {
// the user about the new version if he didn't start the flash // the user about the new version if he didn't start the flash
// process (e.g: selected an image), otherwise such interruption // process (e.g: selected an image), otherwise such interruption
// might be annoying. // might be annoying.
if (SelectionStateModel.hasImage()) { if (selectionState.hasImage()) {
analytics.logEvent('Update notification skipped', { analytics.logEvent('Update notification skipped', {
reason: 'Image selected' reason: 'Image selected'
}); });
@ -278,7 +276,7 @@ app.config(($provide) => {
}); });
}); });
app.controller('HeaderController', function(SelectionStateModel, OSOpenExternalService) { app.controller('HeaderController', function(OSOpenExternalService) {
/** /**
* @summary Open help page * @summary Open help page
@ -294,7 +292,7 @@ app.controller('HeaderController', function(SelectionStateModel, OSOpenExternalS
*/ */
this.openHelpPage = () => { this.openHelpPage = () => {
const DEFAULT_SUPPORT_URL = 'https://github.com/resin-io/etcher/blob/master/SUPPORT.md'; const DEFAULT_SUPPORT_URL = 'https://github.com/resin-io/etcher/blob/master/SUPPORT.md';
const supportUrl = SelectionStateModel.getImageSupportUrl() || DEFAULT_SUPPORT_URL; const supportUrl = selectionState.getImageSupportUrl() || DEFAULT_SUPPORT_URL;
OSOpenExternalService.open(supportUrl); OSOpenExternalService.open(supportUrl);
}; };

View File

@ -22,11 +22,11 @@ const messages = require('../../../../shared/messages');
const constraints = require('../../../../shared/drive-constraints'); const constraints = require('../../../../shared/drive-constraints');
const analytics = require('../../../modules/analytics'); const analytics = require('../../../modules/analytics');
const availableDrives = require('../../../models/available-drives'); const availableDrives = require('../../../models/available-drives');
const selectionState = require('../../../models/selection-state');
module.exports = function( module.exports = function(
$q, $q,
$uibModalInstance, $uibModalInstance,
SelectionStateModel,
WarningModalService WarningModalService
) { ) {
@ -35,7 +35,7 @@ module.exports = function(
* @type {Object} * @type {Object}
* @public * @public
*/ */
this.state = SelectionStateModel; this.state = selectionState;
/** /**
* @summary Static methods to check a drive's properties * @summary Static methods to check a drive's properties
@ -72,11 +72,11 @@ module.exports = function(
* }); * });
*/ */
const shouldChangeDriveSelectionState = (drive) => { const shouldChangeDriveSelectionState = (drive) => {
if (!constraints.isDriveValid(drive, SelectionStateModel.getImage())) { if (!constraints.isDriveValid(drive, selectionState.getImage())) {
return $q.resolve(false); return $q.resolve(false);
} }
if (constraints.isDriveSizeRecommended(drive, SelectionStateModel.getImage())) { if (constraints.isDriveSizeRecommended(drive, selectionState.getImage())) {
return $q.resolve(true); return $q.resolve(true);
} }
@ -84,7 +84,7 @@ module.exports = function(
confirmationLabel: 'Yes, continue', confirmationLabel: 'Yes, continue',
description: [ description: [
messages.warning.unrecommendedDriveSize({ messages.warning.unrecommendedDriveSize({
image: SelectionStateModel.getImage(), image: selectionState.getImage(),
drive drive
}), }),
'Are you sure you want to continue?' 'Are you sure you want to continue?'
@ -111,12 +111,12 @@ module.exports = function(
analytics.logEvent('Toggle drive', { analytics.logEvent('Toggle drive', {
drive, drive,
previouslySelected: SelectionStateModel.isCurrentDrive(drive.device) previouslySelected: selectionState.isCurrentDrive(drive.device)
}); });
return shouldChangeDriveSelectionState(drive).then((canChangeDriveSelectionState) => { return shouldChangeDriveSelectionState(drive).then((canChangeDriveSelectionState) => {
if (canChangeDriveSelectionState) { if (canChangeDriveSelectionState) {
SelectionStateModel.toggleSetDrive(drive.device); selectionState.toggleSetDrive(drive.device);
} }
}); });
@ -131,7 +131,7 @@ module.exports = function(
* DriveSelectorController.closeModal(); * DriveSelectorController.closeModal();
*/ */
this.closeModal = () => { this.closeModal = () => {
const selectedDrive = SelectionStateModel.getDrive(); const selectedDrive = selectionState.getDrive();
// Sanity check to cover the case where a drive is selected, // Sanity check to cover the case where a drive is selected,
// the drive is then unplugged from the computer and the modal // the drive is then unplugged from the computer and the modal
@ -162,7 +162,7 @@ module.exports = function(
this.selectDriveAndClose = (drive) => { this.selectDriveAndClose = (drive) => {
return shouldChangeDriveSelectionState(drive).then((canChangeDriveSelectionState) => { return shouldChangeDriveSelectionState(drive).then((canChangeDriveSelectionState) => {
if (canChangeDriveSelectionState) { if (canChangeDriveSelectionState) {
SelectionStateModel.setDrive(drive.device); selectionState.setDrive(drive.device);
analytics.logEvent('Drive selected (double click)'); analytics.logEvent('Drive selected (double click)');

View File

@ -25,7 +25,6 @@ const MODULE_NAME = 'Etcher.Components.DriveSelector';
const DriveSelector = angular.module(MODULE_NAME, [ const DriveSelector = angular.module(MODULE_NAME, [
require('../modal/modal'), require('../modal/modal'),
require('../warning-modal/warning-modal'), require('../warning-modal/warning-modal'),
require('../../models/selection-state'),
require('../../utils/byte-size/byte-size') require('../../utils/byte-size/byte-size')
]); ]);

View File

@ -23,8 +23,7 @@
const angular = require('angular'); const angular = require('angular');
const MODULE_NAME = 'Etcher.Components.FlashErrorModal'; const MODULE_NAME = 'Etcher.Components.FlashErrorModal';
const FlashErrorModal = angular.module(MODULE_NAME, [ const FlashErrorModal = angular.module(MODULE_NAME, [
require('../warning-modal/warning-modal'), require('../warning-modal/warning-modal')
require('../../models/selection-state')
]); ]);
FlashErrorModal.service('FlashErrorModalService', require('./services/flash-error-modal')); FlashErrorModal.service('FlashErrorModalService', require('./services/flash-error-modal'));

View File

@ -17,9 +17,10 @@
'use strict'; 'use strict';
const flashState = require('../../../models/flash-state'); const flashState = require('../../../models/flash-state');
const selectionState = require('../../../models/selection-state');
const analytics = require('../../../modules/analytics'); const analytics = require('../../../modules/analytics');
module.exports = function(WarningModalService, SelectionStateModel) { module.exports = function(WarningModalService) {
/** /**
* @summary Open the flash error modal * @summary Open the flash error modal
@ -42,7 +43,7 @@ module.exports = function(WarningModalService, SelectionStateModel) {
if (confirmed) { if (confirmed) {
analytics.logEvent('Restart after failure'); analytics.logEvent('Restart after failure');
} else { } else {
SelectionStateModel.clear(); selectionState.clear();
} }
}); });
}; };

View File

@ -16,338 +16,325 @@
'use strict'; 'use strict';
/**
* @module Etcher.Models.SelectionState
*/
const _ = require('lodash'); const _ = require('lodash');
const angular = require('angular');
const Store = require('./store'); const Store = require('./store');
const availableDrives = require('./available-drives'); const availableDrives = require('./available-drives');
const MODULE_NAME = 'Etcher.Models.SelectionState';
const SelectionStateModel = angular.module(MODULE_NAME, []);
SelectionStateModel.service('SelectionStateModel', function() { /**
* @summary Set a drive
* @function
* @public
*
* @param {String} drive - drive device
*
* @example
* selectionState.setDrive('/dev/disk2');
*/
exports.setDrive = (drive) => {
Store.dispatch({
type: Store.Actions.SELECT_DRIVE,
data: drive
});
};
/** /**
* @summary Set a drive * @summary Toggle set drive
* @function * @function
* @public * @public
* *
* @param {String} drive - drive device * @param {String} drive - drive device
* *
* @example * @example
* SelectionStateModel.setDrive('/dev/disk2'); * selectionState.toggleSetDrive('/dev/disk2');
*/ */
this.setDrive = (drive) => { exports.toggleSetDrive = (drive) => {
Store.dispatch({ if (exports.isCurrentDrive(drive)) {
type: Store.Actions.SELECT_DRIVE, exports.removeDrive();
data: drive } else {
}); exports.setDrive(drive);
}; }
};
/** /**
* @summary Toggle set drive * @summary Set a image
* @function * @function
* @public * @public
* *
* @param {String} drive - drive device * @param {Object} image - image
* *
* @example * @example
* SelectionStateModel.toggleSetDrive('/dev/disk2'); * selectionState.setImage({
*/ * path: 'foo.img'
this.toggleSetDrive = (drive) => { * });
if (this.isCurrentDrive(drive)) { */
this.removeDrive(); exports.setImage = (image) => {
} else { Store.dispatch({
this.setDrive(drive); type: Store.Actions.SELECT_IMAGE,
} data: image
}; });
};
/** /**
* @summary Set a image * @summary Get drive
* @function * @function
* @public * @public
* *
* @param {Object} image - image * @returns {Object} drive
* *
* @example * @example
* SelectionStateModel.setImage({ * const drive = selectionState.getDrive();
* path: 'foo.img' */
* }); exports.getDrive = () => {
*/ return _.find(availableDrives.getDrives(), {
this.setImage = (image) => { device: Store.getState().getIn([ 'selection', 'drive' ])
Store.dispatch({ });
type: Store.Actions.SELECT_IMAGE, };
data: image
});
};
/** /**
* @summary Get drive * @summary Get the selected image
* @function * @function
* @public * @public
* *
* @returns {Object} drive * @returns {Object} image
* *
* @example * @example
* const drive = SelectionStateModel.getDrive(); * const image = selectionState.getImage();
*/ */
this.getDrive = () => { exports.getImage = () => {
return _.find(availableDrives.getDrives(), { return _.get(Store.getState().toJS(), [ 'selection', 'image' ]);
device: Store.getState().getIn([ 'selection', 'drive' ]) };
});
};
/** /**
* @summary Get the selected image * @summary Get image path
* @function * @function
* @public * @public
* *
* @returns {Object} image * @returns {String} image path
* *
* @example * @example
* const image = SelectionStateModel.getImage(); * const imagePath = selectionState.getImagePath();
*/ */
this.getImage = () => { exports.getImagePath = () => {
return _.get(Store.getState().toJS(), [ 'selection', 'image' ]); return _.get(Store.getState().toJS(), [
}; 'selection',
'image',
'path'
]);
};
/** /**
* @summary Get image path * @summary Get image size
* @function * @function
* @public * @public
* *
* @returns {String} image path * @returns {Number} image size
* *
* @example * @example
* const imagePath = SelectionStateModel.getImagePath(); * const imageSize = selectionState.getImageSize();
*/ */
this.getImagePath = () => { exports.getImageSize = () => {
return _.get(Store.getState().toJS(), [ return _.get(Store.getState().toJS(), [
'selection', 'selection',
'image', 'image',
'path' 'size',
]); 'final',
}; 'value'
]);
};
/** /**
* @summary Get image size * @summary Get image url
* @function * @function
* @public * @public
* *
* @returns {Number} image size * @returns {String} image url
* *
* @example * @example
* const imageSize = SelectionStateModel.getImageSize(); * const imageUrl = selectionState.getImageUrl();
*/ */
this.getImageSize = () => { exports.getImageUrl = () => {
return _.get(Store.getState().toJS(), [ return _.get(Store.getState().toJS(), [
'selection', 'selection',
'image', 'image',
'size', 'url'
'final', ]);
'value' };
]);
};
/** /**
* @summary Get image url * @summary Get image name
* @function * @function
* @public * @public
* *
* @returns {String} image url * @returns {String} image name
* *
* @example * @example
* const imageUrl = SelectionStateModel.getImageUrl(); * const imageName = selectionState.getImageName();
*/ */
this.getImageUrl = () => { exports.getImageName = () => {
return _.get(Store.getState().toJS(), [ return _.get(Store.getState().toJS(), [
'selection', 'selection',
'image', 'image',
'url' 'name'
]); ]);
}; };
/** /**
* @summary Get image name * @summary Get image logo
* @function * @function
* @public * @public
* *
* @returns {String} image name * @returns {String} image logo
* *
* @example * @example
* const imageName = SelectionStateModel.getImageName(); * const imageLogo = selectionState.getImageLogo();
*/ */
this.getImageName = () => { exports.getImageLogo = () => {
return _.get(Store.getState().toJS(), [ return _.get(Store.getState().toJS(), [
'selection', 'selection',
'image', 'image',
'name' 'logo'
]); ]);
}; };
/** /**
* @summary Get image logo * @summary Get image support url
* @function * @function
* @public * @public
* *
* @returns {String} image logo * @returns {String} image support url
* *
* @example * @example
* const imageLogo = SelectionStateModel.getImageLogo(); * const imageSupportUrl = selectionState.getImageSupportUrl();
*/ */
this.getImageLogo = () => { exports.getImageSupportUrl = () => {
return _.get(Store.getState().toJS(), [ return _.get(Store.getState().toJS(), [
'selection', 'selection',
'image', 'image',
'logo' 'supportUrl'
]); ]);
}; };
/** /**
* @summary Get image support url * @summary Get image recommended drive size
* @function * @function
* @public * @public
* *
* @returns {String} image support url * @returns {String} image recommended drive size
* *
* @example * @example
* const imageSupportUrl = SelectionStateModel.getImageSupportUrl(); * const imageRecommendedDriveSize = selectionState.getImageRecommendedDriveSize();
*/ */
this.getImageSupportUrl = () => { exports.getImageRecommendedDriveSize = () => {
return _.get(Store.getState().toJS(), [ return _.get(Store.getState().toJS(), [
'selection', 'selection',
'image', 'image',
'supportUrl' 'recommendedDriveSize'
]); ]);
}; };
/** /**
* @summary Get image recommended drive size * @summary Check if there is a selected drive
* @function * @function
* @public * @public
* *
* @returns {String} image recommended drive size * @returns {Boolean} whether there is a selected drive
* *
* @example * @example
* const imageRecommendedDriveSize = SelectionStateModel.getImageRecommendedDriveSize(); * if (selectionState.hasDrive()) {
*/ * console.log('There is a drive!');
this.getImageRecommendedDriveSize = () => { * }
return _.get(Store.getState().toJS(), [ */
'selection', exports.hasDrive = () => {
'image', return Boolean(exports.getDrive());
'recommendedDriveSize' };
]);
};
/** /**
* @summary Check if there is a selected drive * @summary Check if there is a selected image
* @function * @function
* @public * @public
* *
* @returns {Boolean} whether there is a selected drive * @returns {Boolean} whether there is a selected image
* *
* @example * @example
* if (SelectionStateModel.hasDrive()) { * if (selectionState.hasImage()) {
* console.log('There is a drive!'); * console.log('There is an image!');
* } * }
*/ */
this.hasDrive = () => { exports.hasImage = () => {
return Boolean(this.getDrive()); return Boolean(exports.getImage());
}; };
/** /**
* @summary Check if there is a selected image * @summary Remove drive
* @function * @function
* @public * @public
* *
* @returns {Boolean} whether there is a selected image * @example
* * selectionState.removeDrive();
* @example */
* if (SelectionStateModel.hasImage()) { exports.removeDrive = () => {
* console.log('There is an image!'); Store.dispatch({
* } type: Store.Actions.REMOVE_DRIVE
*/ });
this.hasImage = () => { };
return Boolean(this.getImage());
};
/** /**
* @summary Remove drive * @summary Remove image
* @function * @function
* @public * @public
* *
* @example * @example
* SelectionStateModel.removeDrive(); * selectionState.removeImage();
*/ */
this.removeDrive = () => { exports.removeImage = () => {
Store.dispatch({ Store.dispatch({
type: Store.Actions.REMOVE_DRIVE type: Store.Actions.REMOVE_IMAGE
}); });
}; };
/** /**
* @summary Remove image * @summary Clear selections
* @function * @function
* @public * @public
* *
* @example * @param {Object} options - options
* SelectionStateModel.removeImage(); * @param {Boolean} [options.preserveImage] - preserve image
*/ *
this.removeImage = () => { * @example
* selectionState.clear();
*
* @example
* selectionState.clear({ preserveImage: true });
*/
exports.clear = (options = {}) => {
if (!options.preserveImage) {
Store.dispatch({ Store.dispatch({
type: Store.Actions.REMOVE_IMAGE type: Store.Actions.REMOVE_IMAGE
}); });
}; }
/** Store.dispatch({
* @summary Clear selections type: Store.Actions.REMOVE_DRIVE
* @function });
* @public };
*
* @param {Object} options - options
* @param {Boolean} [options.preserveImage] - preserve image
*
* @example
* SelectionStateModel.clear();
*
* @example
* SelectionStateModel.clear({ preserveImage: true });
*/
this.clear = (options = {}) => {
if (!options.preserveImage) {
Store.dispatch({
type: Store.Actions.REMOVE_IMAGE
});
}
Store.dispatch({ /**
type: Store.Actions.REMOVE_DRIVE * @summary Check if a drive is the current drive
}); * @function
}; * @public
*
* @param {String} drive - drive device
* @returns {Boolean} whether the drive is the current drive
*
* @example
* if (selectionState.isCurrentDrive('/dev/sdb')) {
* console.log('This is the current drive!');
* }
*/
exports.isCurrentDrive = (drive) => {
if (!drive) {
return false;
}
/** return drive === _.get(exports.getDrive(), [ 'device' ]);
* @summary Check if a drive is the current drive };
* @function
* @public
*
* @param {String} drive - drive device
* @returns {Boolean} whether the drive is the current drive
*
* @example
* if (SelectionStateModel.isCurrentDrive('/dev/sdb')) {
* console.log('This is the current drive!');
* }
*/
this.isCurrentDrive = (drive) => {
if (!drive) {
return false;
}
return drive === _.get(this.getDrive(), [ 'device' ]);
};
});
module.exports = MODULE_NAME;

View File

@ -18,9 +18,10 @@
const settings = require('../../../models/settings'); const settings = require('../../../models/settings');
const flashState = require('../../../models/flash-state'); const flashState = require('../../../models/flash-state');
const selectionState = require('../../../models/selection-state');
const analytics = require('../../../modules/analytics'); const analytics = require('../../../modules/analytics');
module.exports = function($state, SelectionStateModel) { module.exports = function($state) {
/** /**
* @summary Settings model * @summary Settings model
@ -48,7 +49,7 @@ module.exports = function($state, SelectionStateModel) {
* FinishController.restart({ preserveImage: true }); * FinishController.restart({ preserveImage: true });
*/ */
this.restart = (options) => { this.restart = (options) => {
SelectionStateModel.clear(options); selectionState.clear(options);
analytics.logEvent('Restart', options); analytics.logEvent('Restart', options);
$state.go('main'); $state.go('main');
}; };

View File

@ -29,8 +29,7 @@
const angular = require('angular'); const angular = require('angular');
const MODULE_NAME = 'Etcher.Pages.Finish'; const MODULE_NAME = 'Etcher.Pages.Finish';
const FinishPage = angular.module(MODULE_NAME, [ const FinishPage = angular.module(MODULE_NAME, [
require('angular-ui-router'), require('angular-ui-router')
require('../../models/selection-state')
]); ]);
FinishPage.controller('FinishController', require('./controllers/finish')); FinishPage.controller('FinishController', require('./controllers/finish'));

View File

@ -17,9 +17,10 @@
'use strict'; 'use strict';
const settings = require('../../../models/settings'); const settings = require('../../../models/settings');
const selectionState = require('../../../models/selection-state');
const analytics = require('../../../modules/analytics'); const analytics = require('../../../modules/analytics');
module.exports = function(SelectionStateModel, ErrorService, DriveSelectorService) { module.exports = function(ErrorService, DriveSelectorService) {
/** /**
* @summary Open drive selector * @summary Open drive selector
@ -35,7 +36,7 @@ module.exports = function(SelectionStateModel, ErrorService, DriveSelectorServic
return; return;
} }
SelectionStateModel.setDrive(drive.device); selectionState.setDrive(drive.device);
analytics.logEvent('Select drive', { analytics.logEvent('Select drive', {
device: drive.device, device: drive.device,

View File

@ -24,9 +24,9 @@ const errors = require('../../../../shared/errors');
const imageStream = require('../../../../image-stream'); const imageStream = require('../../../../image-stream');
const supportedFormats = require('../../../../shared/supported-formats'); const supportedFormats = require('../../../../shared/supported-formats');
const analytics = require('../../../modules/analytics'); const analytics = require('../../../modules/analytics');
const selectionState = require('../../../models/selection-state');
module.exports = function( module.exports = function(
SelectionStateModel,
ErrorService, ErrorService,
OSDialogService, OSDialogService,
WarningModalService WarningModalService
@ -100,7 +100,7 @@ module.exports = function(
return this.reselectImage(); return this.reselectImage();
} }
SelectionStateModel.setImage(image); selectionState.setImage(image);
// An easy way so we can quickly identify if we're making use of // An easy way so we can quickly identify if we're making use of
// certain features without printing pages of text to DevTools. // certain features without printing pages of text to DevTools.
@ -161,7 +161,7 @@ module.exports = function(
*/ */
this.reselectImage = () => { this.reselectImage = () => {
analytics.logEvent('Reselect image', { analytics.logEvent('Reselect image', {
previousImage: SelectionStateModel.getImage() previousImage: selectionState.getImage()
}); });
this.openImageSelector(); this.openImageSelector();
@ -178,11 +178,11 @@ module.exports = function(
* const imageBasename = ImageSelectionController.getImageBasename(); * const imageBasename = ImageSelectionController.getImageBasename();
*/ */
this.getImageBasename = () => { this.getImageBasename = () => {
if (!SelectionStateModel.hasImage()) { if (!selectionState.hasImage()) {
return ''; return '';
} }
return path.basename(SelectionStateModel.getImagePath()); return path.basename(selectionState.getImagePath());
}; };
}; };

View File

@ -20,16 +20,16 @@ const settings = require('../../../models/settings');
const flashState = require('../../../models/flash-state'); const flashState = require('../../../models/flash-state');
const analytics = require('../../../modules/analytics'); const analytics = require('../../../modules/analytics');
const availableDrives = require('../../../models/available-drives'); const availableDrives = require('../../../models/available-drives');
const selectionState = require('../../../models/selection-state');
module.exports = function( module.exports = function(
SelectionStateModel,
TooltipModalService, TooltipModalService,
ErrorService, ErrorService,
OSOpenExternalService OSOpenExternalService
) { ) {
// Expose several modules to the template for convenience // Expose several modules to the template for convenience
this.selection = SelectionStateModel; this.selection = selectionState;
this.drives = availableDrives; this.drives = availableDrives;
this.state = flashState; this.state = flashState;
this.settings = settings; this.settings = settings;
@ -48,7 +48,7 @@ module.exports = function(
* } * }
*/ */
this.shouldDriveStepBeDisabled = () => { this.shouldDriveStepBeDisabled = () => {
return !SelectionStateModel.hasImage(); return !selectionState.hasImage();
}; };
/** /**
@ -64,7 +64,7 @@ module.exports = function(
* } * }
*/ */
this.shouldFlashStepBeDisabled = () => { this.shouldFlashStepBeDisabled = () => {
return !SelectionStateModel.hasDrive() || this.shouldDriveStepBeDisabled(); return !selectionState.hasDrive() || this.shouldDriveStepBeDisabled();
}; };
/** /**
@ -79,12 +79,12 @@ module.exports = function(
*/ */
this.showSelectedImageDetails = () => { this.showSelectedImageDetails = () => {
analytics.logEvent('Show selected image tooltip', { analytics.logEvent('Show selected image tooltip', {
imagePath: SelectionStateModel.getImagePath() imagePath: selectionState.getImagePath()
}); });
return TooltipModalService.show({ return TooltipModalService.show({
title: 'Image File Name', title: 'Image File Name',
message: SelectionStateModel.getImagePath() message: selectionState.getImagePath()
}).catch(ErrorService.reportException); }).catch(ErrorService.reportException);
}; };

View File

@ -46,7 +46,6 @@ const MainPage = angular.module(MODULE_NAME, [
require('../../modules/drive-scanner'), require('../../modules/drive-scanner'),
require('../../modules/image-writer'), require('../../modules/image-writer'),
require('../../modules/error'), require('../../modules/error'),
require('../../models/selection-state'),
require('../../utils/byte-size/byte-size') require('../../utils/byte-size/byte-size')
]); ]);

View File

@ -17,7 +17,6 @@ describe('Browser: DriveSelector', function() {
let $rootScope; let $rootScope;
let $q; let $q;
let $uibModalInstance; let $uibModalInstance;
let SelectionStateModel;
let WarningModalService; let WarningModalService;
let controller; let controller;
@ -26,14 +25,12 @@ describe('Browser: DriveSelector', function() {
_$controller_, _$controller_,
_$rootScope_, _$rootScope_,
_$q_, _$q_,
_SelectionStateModel_,
_WarningModalService_ _WarningModalService_
) { ) {
$controller = _$controller_; $controller = _$controller_;
$rootScope = _$rootScope_; $rootScope = _$rootScope_;
$q = _$q_; $q = _$q_;
$uibModalInstance = {}; $uibModalInstance = {};
SelectionStateModel = _SelectionStateModel_;
WarningModalService = _WarningModalService_; WarningModalService = _WarningModalService_;
})); }));
@ -42,7 +39,6 @@ describe('Browser: DriveSelector', function() {
$scope: $rootScope.$new(), $scope: $rootScope.$new(),
$q, $q,
$uibModalInstance, $uibModalInstance,
SelectionStateModel,
WarningModalService WarningModalService
}); });
}); });

View File

@ -2,24 +2,13 @@
const m = require('mochainon'); const m = require('mochainon');
const path = require('path'); const path = require('path');
const angular = require('angular');
const availableDrives = require('../../../lib/gui/models/available-drives'); const availableDrives = require('../../../lib/gui/models/available-drives');
require('angular-mocks'); const selectionState = require('../../../lib/gui/models/selection-state');
describe('Browser: availableDrives', function() { describe('Browser: availableDrives', function() {
beforeEach(angular.mock.module(
require('../../../lib/gui/models/selection-state')
));
describe('availableDrives', function() { describe('availableDrives', function() {
let SelectionStateModel;
beforeEach(angular.mock.inject(function(_SelectionStateModel_) {
SelectionStateModel = _SelectionStateModel_;
}));
it('should have no drives by default', function() { it('should have no drives by default', function() {
m.chai.expect(availableDrives.getDrives()).to.deep.equal([]); m.chai.expect(availableDrives.getDrives()).to.deep.equal([]);
}); });
@ -80,12 +69,12 @@ describe('Browser: availableDrives', function() {
describe('given no selected image and no selected drive', function() { describe('given no selected image and no selected drive', function() {
beforeEach(function() { beforeEach(function() {
SelectionStateModel.removeDrive(); selectionState.removeDrive();
SelectionStateModel.removeImage(); selectionState.removeImage();
}); });
it('should auto-select a single valid available drive', function() { it('should auto-select a single valid available drive', function() {
m.chai.expect(SelectionStateModel.hasDrive()).to.be.false; m.chai.expect(selectionState.hasDrive()).to.be.false;
availableDrives.setDrives([ availableDrives.setDrives([
{ {
@ -98,8 +87,8 @@ describe('Browser: availableDrives', function() {
} }
]); ]);
m.chai.expect(SelectionStateModel.hasDrive()).to.be.true; m.chai.expect(selectionState.hasDrive()).to.be.true;
m.chai.expect(SelectionStateModel.getDrive().device).to.equal('/dev/sdb'); m.chai.expect(selectionState.getDrive().device).to.equal('/dev/sdb');
}); });
}); });
@ -113,8 +102,8 @@ describe('Browser: availableDrives', function() {
this.imagePath = '/mnt/bar/foo.img'; this.imagePath = '/mnt/bar/foo.img';
} }
SelectionStateModel.removeDrive(); selectionState.removeDrive();
SelectionStateModel.setImage({ selectionState.setImage({
path: this.imagePath, path: this.imagePath,
extension: 'img', extension: 'img',
size: { size: {
@ -129,11 +118,11 @@ describe('Browser: availableDrives', function() {
}); });
afterEach(function() { afterEach(function() {
SelectionStateModel.removeImage(); selectionState.removeImage();
}); });
it('should not auto-select when there are multiple valid available drives', function() { it('should not auto-select when there are multiple valid available drives', function() {
m.chai.expect(SelectionStateModel.hasDrive()).to.be.false; m.chai.expect(selectionState.hasDrive()).to.be.false;
availableDrives.setDrives([ availableDrives.setDrives([
{ {
@ -154,11 +143,11 @@ describe('Browser: availableDrives', function() {
} }
]); ]);
m.chai.expect(SelectionStateModel.hasDrive()).to.be.false; m.chai.expect(selectionState.hasDrive()).to.be.false;
}); });
it('should auto-select a single valid available drive', function() { it('should auto-select a single valid available drive', function() {
m.chai.expect(SelectionStateModel.hasDrive()).to.be.false; m.chai.expect(selectionState.hasDrive()).to.be.false;
availableDrives.setDrives([ availableDrives.setDrives([
{ {
@ -171,7 +160,7 @@ describe('Browser: availableDrives', function() {
} }
]); ]);
m.chai.expect(SelectionStateModel.getDrive()).to.deep.equal({ m.chai.expect(selectionState.getDrive()).to.deep.equal({
device: '/dev/sdb', device: '/dev/sdb',
name: 'Foo', name: 'Foo',
size: 2000000000, size: 2000000000,
@ -182,7 +171,7 @@ describe('Browser: availableDrives', function() {
}); });
it('should not auto-select a single too small drive', function() { it('should not auto-select a single too small drive', function() {
m.chai.expect(SelectionStateModel.hasDrive()).to.be.false; m.chai.expect(selectionState.hasDrive()).to.be.false;
availableDrives.setDrives([ availableDrives.setDrives([
{ {
@ -195,11 +184,11 @@ describe('Browser: availableDrives', function() {
} }
]); ]);
m.chai.expect(SelectionStateModel.hasDrive()).to.be.false; m.chai.expect(selectionState.hasDrive()).to.be.false;
}); });
it('should not auto-select a single drive that doesn\'t meet the recommended size', function() { it('should not auto-select a single drive that doesn\'t meet the recommended size', function() {
m.chai.expect(SelectionStateModel.hasDrive()).to.be.false; m.chai.expect(selectionState.hasDrive()).to.be.false;
availableDrives.setDrives([ availableDrives.setDrives([
{ {
@ -212,11 +201,11 @@ describe('Browser: availableDrives', function() {
} }
]); ]);
m.chai.expect(SelectionStateModel.hasDrive()).to.be.false; m.chai.expect(selectionState.hasDrive()).to.be.false;
}); });
it('should not auto-select a single protected drive', function() { it('should not auto-select a single protected drive', function() {
m.chai.expect(SelectionStateModel.hasDrive()).to.be.false; m.chai.expect(selectionState.hasDrive()).to.be.false;
availableDrives.setDrives([ availableDrives.setDrives([
{ {
@ -229,11 +218,11 @@ describe('Browser: availableDrives', function() {
} }
]); ]);
m.chai.expect(SelectionStateModel.hasDrive()).to.be.false; m.chai.expect(selectionState.hasDrive()).to.be.false;
}); });
it('should not auto-select a source drive', function() { it('should not auto-select a source drive', function() {
m.chai.expect(SelectionStateModel.hasDrive()).to.be.false; m.chai.expect(selectionState.hasDrive()).to.be.false;
availableDrives.setDrives([ availableDrives.setDrives([
{ {
@ -250,11 +239,11 @@ describe('Browser: availableDrives', function() {
} }
]); ]);
m.chai.expect(SelectionStateModel.hasDrive()).to.be.false; m.chai.expect(selectionState.hasDrive()).to.be.false;
}); });
it('should not auto-select a single system drive', function() { it('should not auto-select a single system drive', function() {
m.chai.expect(SelectionStateModel.hasDrive()).to.be.false; m.chai.expect(selectionState.hasDrive()).to.be.false;
availableDrives.setDrives([ availableDrives.setDrives([
{ {
@ -267,7 +256,7 @@ describe('Browser: availableDrives', function() {
} }
]); ]);
m.chai.expect(SelectionStateModel.hasDrive()).to.be.false; m.chai.expect(selectionState.hasDrive()).to.be.false;
}); });
}); });
@ -315,15 +304,15 @@ describe('Browser: availableDrives', function() {
} }
]); ]);
SelectionStateModel.setDrive('/dev/sdc'); selectionState.setDrive('/dev/sdc');
}); });
afterEach(function() { afterEach(function() {
SelectionStateModel.removeDrive(); selectionState.removeDrive();
}); });
it('should be deleted if its not contained in the available drives anymore', function() { it('should be deleted if its not contained in the available drives anymore', function() {
m.chai.expect(SelectionStateModel.hasDrive()).to.be.true; m.chai.expect(selectionState.hasDrive()).to.be.true;
// We have to provide at least two drives, otherwise, // We have to provide at least two drives, otherwise,
// if we only provide one, the single drive will be // if we only provide one, the single drive will be
@ -347,7 +336,7 @@ describe('Browser: availableDrives', function() {
} }
]); ]);
m.chai.expect(SelectionStateModel.hasDrive()).to.be.false; m.chai.expect(selectionState.hasDrive()).to.be.false;
}); });
}); });

File diff suppressed because it is too large Load Diff

View File

@ -8,6 +8,7 @@ const angular = require('angular');
const settings = require('../../../lib/gui/models/settings'); const settings = require('../../../lib/gui/models/settings');
const flashState = require('../../../lib/gui/models/flash-state'); const flashState = require('../../../lib/gui/models/flash-state');
const availableDrives = require('../../../lib/gui/models/available-drives'); const availableDrives = require('../../../lib/gui/models/available-drives');
const selectionState = require('../../../lib/gui/models/selection-state');
require('angular-mocks'); require('angular-mocks');
describe('Browser: MainPage', function() { describe('Browser: MainPage', function() {
@ -19,11 +20,9 @@ describe('Browser: MainPage', function() {
describe('MainController', function() { describe('MainController', function() {
let $controller; let $controller;
let SelectionStateModel;
beforeEach(angular.mock.inject(function(_$controller_, _SelectionStateModel_) { beforeEach(angular.mock.inject(function(_$controller_) {
$controller = _$controller_; $controller = _$controller_;
SelectionStateModel = _SelectionStateModel_;
})); }));
describe('.shouldDriveStepBeDisabled()', function() { describe('.shouldDriveStepBeDisabled()', function() {
@ -33,7 +32,7 @@ describe('Browser: MainPage', function() {
$scope: {} $scope: {}
}); });
SelectionStateModel.clear(); selectionState.clear();
m.chai.expect(controller.shouldDriveStepBeDisabled()).to.be.true; m.chai.expect(controller.shouldDriveStepBeDisabled()).to.be.true;
}); });
@ -43,7 +42,7 @@ describe('Browser: MainPage', function() {
$scope: {} $scope: {}
}); });
SelectionStateModel.setImage({ selectionState.setImage({
path: 'rpi.img', path: 'rpi.img',
extension: 'img', extension: 'img',
size: { size: {
@ -67,7 +66,7 @@ describe('Browser: MainPage', function() {
$scope: {} $scope: {}
}); });
SelectionStateModel.clear(); selectionState.clear();
m.chai.expect(controller.shouldFlashStepBeDisabled()).to.be.true; m.chai.expect(controller.shouldFlashStepBeDisabled()).to.be.true;
}); });
@ -77,8 +76,8 @@ describe('Browser: MainPage', function() {
$scope: {} $scope: {}
}); });
SelectionStateModel.clear(); selectionState.clear();
SelectionStateModel.setImage({ selectionState.setImage({
path: 'rpi.img', path: 'rpi.img',
extension: 'img', extension: 'img',
size: { size: {
@ -108,8 +107,8 @@ describe('Browser: MainPage', function() {
} }
]); ]);
SelectionStateModel.clear(); selectionState.clear();
SelectionStateModel.setDrive('/dev/disk2'); selectionState.setDrive('/dev/disk2');
m.chai.expect(controller.shouldFlashStepBeDisabled()).to.be.true; m.chai.expect(controller.shouldFlashStepBeDisabled()).to.be.true;
}); });
@ -129,10 +128,10 @@ describe('Browser: MainPage', function() {
} }
]); ]);
SelectionStateModel.clear(); selectionState.clear();
SelectionStateModel.setDrive('/dev/disk2'); selectionState.setDrive('/dev/disk2');
SelectionStateModel.setImage({ selectionState.setImage({
path: 'rpi.img', path: 'rpi.img',
extension: 'img', extension: 'img',
size: { size: {
@ -154,11 +153,9 @@ describe('Browser: MainPage', function() {
describe('ImageSelectionController', function() { describe('ImageSelectionController', function() {
let $controller; let $controller;
let SelectionStateModel;
beforeEach(angular.mock.inject(function(_$controller_, _SelectionStateModel_) { beforeEach(angular.mock.inject(function(_$controller_) {
$controller = _$controller_; $controller = _$controller_;
SelectionStateModel = _SelectionStateModel_;
})); }));
it('should contain all available extensions in mainSupportedExtensions and extraSupportedExtensions', function() { it('should contain all available extensions in mainSupportedExtensions and extraSupportedExtensions', function() {
@ -178,7 +175,7 @@ describe('Browser: MainPage', function() {
$scope: {} $scope: {}
}); });
SelectionStateModel.setImage({ selectionState.setImage({
path: path.join(__dirname, 'foo', 'bar.img'), path: path.join(__dirname, 'foo', 'bar.img'),
extension: 'img', extension: 'img',
size: { size: {
@ -191,7 +188,7 @@ describe('Browser: MainPage', function() {
}); });
m.chai.expect(controller.getImageBasename()).to.equal('bar.img'); m.chai.expect(controller.getImageBasename()).to.equal('bar.img');
SelectionStateModel.removeImage(); selectionState.removeImage();
}); });
it('should return an empty string if no selected image', function() { it('should return an empty string if no selected image', function() {
@ -199,7 +196,7 @@ describe('Browser: MainPage', function() {
$scope: {} $scope: {}
}); });
SelectionStateModel.removeImage(); selectionState.removeImage();
m.chai.expect(controller.getImageBasename()).to.equal(''); m.chai.expect(controller.getImageBasename()).to.equal('');
}); });