diff --git a/lib/gui/pages/main/controllers/image-selection.js b/lib/gui/pages/main/controllers/image-selection.js index a04418fa..553de563 100644 --- a/lib/gui/pages/main/controllers/image-selection.js +++ b/lib/gui/pages/main/controllers/image-selection.js @@ -18,6 +18,7 @@ const _ = require('lodash'); const Bluebird = require('bluebird'); +const path = require('path'); const messages = require('../../../../shared/messages'); const errors = require('../../../../shared/errors'); @@ -146,4 +147,22 @@ module.exports = function( this.openImageSelector(); }; + /** + * @summary Get the basename of the selected image + * @function + * @public + * + * @returns {String} basename of the selected image + * + * @example + * const imageBasename = ImageSelectionController.getImageBasename(); + */ + this.getImageBasename = () => { + if (!SelectionStateModel.hasImage()) { + return ''; + } + + return path.basename(SelectionStateModel.getImagePath()); + }; + }; diff --git a/lib/gui/pages/main/main.js b/lib/gui/pages/main/main.js index dd3991b6..7132ac39 100644 --- a/lib/gui/pages/main/main.js +++ b/lib/gui/pages/main/main.js @@ -55,7 +55,6 @@ const MainPage = angular.module(MODULE_NAME, [ require('../../models/supported-formats'), require('../../models/drives'), - require('../../utils/path/path'), require('../../utils/byte-size/byte-size') ]); diff --git a/lib/gui/pages/main/templates/main.tpl.html b/lib/gui/pages/main/templates/main.tpl.html index 9da16fad..abba9f69 100644 --- a/lib/gui/pages/main/templates/main.tpl.html +++ b/lib/gui/pages/main/templates/main.tpl.html @@ -23,8 +23,8 @@ + ng-bind="main.selection.getImageName() || image.getImageBasename() | middleEllipses:20" + uib-tooltip="{{ image.getImageBasename() }}"> {{ main.selection.getImageSize() | gigabyte | number:1 }} GB diff --git a/lib/gui/utils/path/filters/basename.js b/lib/gui/utils/path/filters/basename.js deleted file mode 100644 index 823c15e2..00000000 --- a/lib/gui/utils/path/filters/basename.js +++ /dev/null @@ -1,42 +0,0 @@ -/* - * Copyright 2016 resin.io - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -'use strict'; - -const path = require('path'); - -module.exports = () => { - - /** - * @summary Get the basename of a path - * @function - * @public - * - * @param {String} input - input path - * @returns {String} path basename - * - * @example - * {{ '/foo/bar/baz' | basename }} - */ - return (input) => { - if (!input) { - return ''; - } - - return path.basename(input); - }; - -}; diff --git a/lib/gui/utils/path/path.js b/lib/gui/utils/path/path.js deleted file mode 100644 index aa55d9fe..00000000 --- a/lib/gui/utils/path/path.js +++ /dev/null @@ -1,36 +0,0 @@ -/* - * Copyright 2016 resin.io - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -'use strict'; - -/** - * The purpose of this module is to provide utilities - * to work with file paths. - * - * @module Etcher.Utils.Path - */ - -const angular = require('angular'); -const MODULE_NAME = 'Etcher.Utils.Path'; -const Path = angular.module(MODULE_NAME, []); - -/* eslint-disable lodash/prefer-lodash-method */ - -Path.filter('basename', require('./filters/basename')); - -/* eslint-enable lodash/prefer-lodash-method */ - -module.exports = MODULE_NAME; diff --git a/tests/gui/pages/main.spec.js b/tests/gui/pages/main.spec.js index 0f35a689..88a6ded8 100644 --- a/tests/gui/pages/main.spec.js +++ b/tests/gui/pages/main.spec.js @@ -2,6 +2,7 @@ const m = require('mochainon'); const _ = require('lodash'); +const path = require('path'); const angular = require('angular'); require('angular-mocks'); @@ -131,10 +132,12 @@ describe('Browser: MainPage', function() { let $controller; let SupportedFormatsModel; + let SelectionStateModel; - beforeEach(angular.mock.inject(function(_$controller_, _SupportedFormatsModel_) { + beforeEach(angular.mock.inject(function(_$controller_, _SupportedFormatsModel_, _SelectionStateModel_) { $controller = _$controller_; SupportedFormatsModel = _SupportedFormatsModel_; + SelectionStateModel = _SelectionStateModel_; })); it('should contain all available extensions in mainSupportedExtensions and extraSupportedExtensions', function() { @@ -147,6 +150,33 @@ describe('Browser: MainPage', function() { m.chai.expect(_.sortBy(extensions)).to.deep.equal(_.sortBy(SupportedFormatsModel.getAllExtensions())); }); + describe('.getImageBasename()', function() { + + it('should return the basename of the selected image', function() { + const controller = $controller('ImageSelectionController', { + $scope: {} + }); + + SelectionStateModel.setImage({ + path: path.join(__dirname, 'foo', 'bar.img'), + size: 999999999 + }); + + m.chai.expect(controller.getImageBasename()).to.equal('bar.img'); + SelectionStateModel.removeImage(); + }); + + it('should return an empty string if no selected image', function() { + const controller = $controller('ImageSelectionController', { + $scope: {} + }); + + SelectionStateModel.removeImage(); + m.chai.expect(controller.getImageBasename()).to.equal(''); + }); + + }); + }); describe('FlashController', function() { diff --git a/tests/gui/utils/path.spec.js b/tests/gui/utils/path.spec.js deleted file mode 100644 index 1b003442..00000000 --- a/tests/gui/utils/path.spec.js +++ /dev/null @@ -1,40 +0,0 @@ -'use strict'; - -const m = require('mochainon'); -const angular = require('angular'); -const os = require('os'); -require('angular-mocks'); - -describe('Browser: Path', function() { - - beforeEach(angular.mock.module( - require('../../../lib/gui/utils/path/path') - )); - - describe('BasenameFilter', function() { - - let basenameFilter; - - beforeEach(angular.mock.inject(function(_basenameFilter_) { - basenameFilter = _basenameFilter_; - })); - - it('should return an empty string if no input', function() { - m.chai.expect(basenameFilter()).to.equal(''); - }); - - it('should return the basename', function() { - const isWindows = os.platform() === 'win32'; - let basename; - - if (isWindows) { - basename = basenameFilter('C:\\Users\\jviotti\\foo.img'); - } else { - basename = basenameFilter('/Users/jviotti/foo.img'); - } - - m.chai.expect(basename).to.equal('foo.img'); - }); - - }); -});