Files
etcher/lib/browser/models/selection-state.js
Juan Cruz Viotti a874d4b808 Make all angular modules export the name of the module (#318)
This makes them very nicely require-able, for example:

    angular.module('MyModule', [
      require('my-dependency');
    ]);

From https://medium.com/@kentcdodds/how-to-distribute-your-angularjs-module-e04d4dd58ddc#.yqg2zo8im
Signed-off-by: Juan Cruz Viotti <jviottidc@gmail.com>
2016-04-13 14:22:51 -04:00

201 lines
4.1 KiB
JavaScript

/*
* 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';
/**
* @module Etcher.Models.SelectionState
*/
const _ = require('lodash');
const angular = require('angular');
const MODULE_NAME = 'Etcher.Models.SelectionState';
const SelectionStateModel = angular.module(MODULE_NAME, []);
SelectionStateModel.service('SelectionStateModel', function() {
let self = this;
/**
* @summary Selection state
* @type Object
* @private
*/
let selection = {};
/**
* @summary Set a drive
* @function
* @public
*
* @param {Object} drive - drive
*
* @example
* SelectionStateModel.setDrive({
* device: '/dev/disk2'
* });
*/
this.setDrive = function(drive) {
selection.drive = drive;
};
/**
* @summary Set a image
* @function
* @public
*
* @param {String} image - image
*
* @example
* SelectionStateModel.setImage('foo.img');
*/
this.setImage = function(image) {
selection.image = image;
};
/**
* @summary Get drive
* @function
* @public
*
* @returns {Object} drive
*
* @example
* const drive = SelectionStateModel.getDrive();
*/
this.getDrive = function() {
if (_.isEmpty(selection.drive)) {
return;
}
return selection.drive;
};
/**
* @summary Get image
* @function
* @public
*
* @returns {String} image
*
* @example
* const image = SelectionStateModel.getImage();
*/
this.getImage = function() {
return selection.image;
};
/**
* @summary Check if there is a selected drive
* @function
* @public
*
* @returns {Boolean} whether there is a selected drive
*
* @example
* if (SelectionStateModel.hasDrive()) {
* console.log('There is a drive!');
* }
*/
this.hasDrive = function() {
return Boolean(self.getDrive());
};
/**
* @summary Check if there is a selected image
* @function
* @public
*
* @returns {Boolean} whether there is a selected image
*
* @example
* if (SelectionStateModel.hasImage()) {
* console.log('There is an image!');
* }
*/
this.hasImage = function() {
return Boolean(self.getImage());
};
/**
* @summary Remove drive
* @function
* @public
*
* @example
* SelectionStateModel.removeDrive();
*/
this.removeDrive = _.partial(self.setDrive, undefined);
/**
* @summary Remove image
* @function
* @public
*
* @example
* SelectionStateModel.removeImage();
*/
this.removeImage = _.partial(self.setImage, undefined);
/**
* @summary Clear selections
* @function
* @public
*
* @param {Object} options - options
* @param {Boolean} [options.preserveImage] - preserve image
*
* @example
* SelectionStateModel.clear();
*
* @example
* SelectionStateModel.clear({ preserveImage: true });
*/
this.clear = function(options) {
if (options && options.preserveImage) {
selection = _.pick(selection, 'image');
} else {
selection = {};
}
};
/**
* @summary Check if a drive is the current drive
* @function
* @public
*
* @param {Object} drive - drive
* @returns {Boolean} whether the drive is the current drive
*
* @example
* if (SelectionStateModel.isCurrentDrive({
* device: '/dev/sdb',
* description: 'DataTraveler 2.0',
* size: '7.3G',
* mountpoint: '/media/UNTITLED',
* name: '/dev/sdb',
* system: false
* })) {
* console.log('This is the current drive!');
* }
*/
this.isCurrentDrive = function(drive) {
return angular.equals(self.getDrive(), drive);
};
});
module.exports = MODULE_NAME;