Skip to content
This repository was archived by the owner on Jun 6, 2023. It is now read-only.

Commit

Permalink
Use predefined values select for bodypart options and request fields …
Browse files Browse the repository at this point in the history
…// Issue #13
  • Loading branch information
dmytrokyrychuk committed Sep 21, 2015
1 parent 7ed48ec commit 25beca8
Show file tree
Hide file tree
Showing 10 changed files with 138 additions and 82 deletions.
1 change: 1 addition & 0 deletions app/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@
<script src="scripts/factories/id-to-name-mapper.js"></script>
<script src="scripts/factories/extract-number-from-name.js"></script>
<script src="scripts/factories/get-board-pins.js"></script>
<script src="scripts/factories/get-option-value.js"></script>
<script src="scripts/directives/showformerrors.js"></script>
<script src="scripts/controllers/main.js"></script>
<script src="scripts/controllers/dashboard.js"></script>
Expand Down
19 changes: 10 additions & 9 deletions app/scripts/controllers/device-bodypart-run.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
.module('siteApp')
.controller('RunDeviceBodyPartController', RunDeviceBodyPartController);

function RunDeviceBodyPartController($scope, $modalInstance, $filter,
function RunDeviceBodyPartController($scope, $modalInstance, getOptionValue,
dataService, pluginInfo, deviceInfo, bodyPartInfo, settings) {

var vm = this;
Expand All @@ -42,22 +42,23 @@

// Populate default request field values
angular.forEach(vm.plugin.request_fields, function(field) { // jshint ignore:line
if (!angular.isUndefined(field.default)) {
if ($filter('optionTypeToInputType')(field.type) === 'number') {
vm.requestFields[field.name] = parseInt(field.default);
} else {
vm.requestFields[field.name] = field.default;
}
}
vm.requestFields[field.name] = getOptionValue(field);
});

////////////

function runBodyPart() {
vm.runDisabled = true;

var cleanRequestFields = {};
angular.forEach(vm.plugin.request_fields, function(field) { // jshint ignore:line
cleanRequestFields[field.name] = field._values ?
vm.requestFields[field.name].value : vm.requestFields[field.name];
});

dataService.runBodyPart(
'localhost:' + vm.settings.services.api.options.rest.port,
vm.device.id, vm.bodyPart.name, vm.requestFields)
vm.device.id, vm.bodyPart.name, cleanRequestFields)
.then(function(data) {
vm.responseContent = data;
vm.runDisabled = false;
Expand Down
41 changes: 22 additions & 19 deletions app/scripts/controllers/device-bodypart.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,39 +23,29 @@
.controller('DeviceBodyPartController', DeviceBodyPartController);

function DeviceBodyPartController($scope, $modalInstance, $window,
extractNumberFromName, getBoardPins, initialState, pluginsList, deviceInfo,
boardInfo, editMode, wizardMode) {
extractNumberFromName, getBoardPins, getOptionValue, initialState,
pluginsList, deviceInfo, boardInfo, editMode, wizardMode) {

var vm = this;

vm.device = deviceInfo;
vm.plugins = pluginsList;
vm.item = initialState;
vm.itemOptions = {};
vm.boardPins = getBoardPins(boardInfo);
vm.selectPlugin = {};
vm.wizardMode = wizardMode;

angular.forEach(vm.plugins, function(item) {
if (item.id === initialState.pluginId) {
vm.selectPlugin.selected = item;
}
});

$scope.$watch('vm.selectPlugin.selected', function(newValue, oldValue) {
if (!angular.isObject(newValue) || newValue === oldValue) {
if (!angular.isObject(newValue)) {
return;
}

vm.item.peripheral = {};
vm.item.options = {};

if (angular.isObject(vm.selectPlugin.selected.options)) {
angular.forEach(vm.selectPlugin.selected.options, function(item) {
if (!angular.isUndefined(item.default)) {
vm.item.options[item.name] = item.default;
}
});
}
// Assigning default option values
angular.forEach(vm.selectPlugin.selected.options, function(option) {
vm.itemOptions[option.name] = getOptionValue(
option, vm.item.options[option.name]);
});

if (vm.wizardMode || !editMode || !vm.item.name) {
vm.item.name = newValue.id;
Expand All @@ -74,6 +64,12 @@

});

angular.forEach(vm.plugins, function(item) {
if (item.id === initialState.pluginId) {
vm.selectPlugin.selected = item;
}
});

// handlers
vm.save = save;
vm.cancel = cancel;
Expand All @@ -89,6 +85,13 @@
}
}
}

// Copy option values to item object
angular.forEach(vm.selectPlugin.selected.options, function(spec) {
vm.item.options[spec.name] = spec._values ?
vm.itemOptions[spec.name].value : vm.itemOptions[spec.name];
});

$modalInstance.close({
'name': vm.item.name,
'pluginId': vm.selectPlugin.selected.id,
Expand Down
23 changes: 4 additions & 19 deletions app/scripts/controllers/device-bus.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@
.module('siteApp')
.controller('DeviceBusController', DeviceBusController);

function DeviceBusController($log, $scope, $filter, $modalInstance,
dataService, notifyUser, extractNumberFromName, getBoardPins, initialState,
deviceInfo, editMode, serialPortsList, transportsList, wizardMode,
boardInfo) {
function DeviceBusController($log, $scope, $modalInstance, dataService,
notifyUser, extractNumberFromName, getBoardPins, getOptionValue,
initialState, deviceInfo, editMode, serialPortsList, transportsList,
wizardMode, boardInfo) {

var vm = this;

Expand Down Expand Up @@ -146,21 +146,6 @@
}
}

function getOptionValue(optionSpec, currentValue) {
var optionValue = currentValue || optionSpec.default;
if ('number' === $filter('optionTypeToInputType')(optionSpec.type)) {
optionValue = parseInt(optionValue);
}
if (optionSpec._values) {
angular.forEach(optionSpec._values, function(valueObj) {
if (valueObj.value === optionValue) {
optionValue = valueObj;
}
});
}
return optionValue;
}

function hubMayBeAdded() {
return !vm.editMode && vm.transport.id === 'serial';
}
Expand Down
5 changes: 4 additions & 1 deletion app/scripts/controllers/device-edit-bodyparts.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,10 @@
////////////

function bodyPartModal(index) {
var state = {};
var state = {
'peripheral': {},
'options': {},
};
if (index === undefined || index === null) {
index = -1;
} else {
Expand Down
3 changes: 2 additions & 1 deletion app/scripts/controllers/device-info.js
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,8 @@
}

function runBodyPartDisabled(device) {
return !device.enabled || 2 === device.status;
return false; // _values support debug
// return !device.enabled || 2 === device.status;
}

function runBodyPartTooltipText(device) {
Expand Down
42 changes: 42 additions & 0 deletions app/scripts/factories/get-option-value.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/**
Copyright (C) 2015 OLogN Technologies AG
This source file is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License version 2
as published by the Free Software Foundation.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/

(function() {
'use strict';

angular
.module('siteApp')
.factory('getOptionValue', getOptionValue);

function getOptionValue($filter) {
return function(optionSpec, currentValue) {
var optionValue = currentValue || optionSpec.default;
if ('number' === $filter('optionTypeToInputType')(optionSpec.type)) {
optionValue = parseInt(optionValue);
}
if (optionSpec._values) {
angular.forEach(optionSpec._values, function(valueObj) {
if (valueObj.value === optionValue) {
optionValue = valueObj;
}
});
}
return optionValue;
};
}

})();
2 changes: 1 addition & 1 deletion app/scripts/filters/option-input-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
}

function _extractSize(string) {
return parseInt(/\[(\d+)\]/.exec(string)[1]);
return string ? parseInt(/\[(\d+)\]/.exec(string)[1]) : null;
}

function _isUnsigned(string) {
Expand Down
39 changes: 25 additions & 14 deletions app/views/device-bodypart-run.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,34 @@ <h4 ng-show="vm.plugin.request_fields.length">Request fields:</h4>
<form name="bodyPartFieldsForm" novalidate role="form">
<div class="form-group" ng-repeat="field in vm.plugin.request_fields" ng-switch on="field.type | optionTypeToInputType" show-form-errors>
<label class="control-label"><code>{{ field.name }}</code><span ng-show="field.title"> - {{ field.title }}</span></label>
<input ng-switch-when="text"
type="text"
name="{{ field.name }}"
class="form-control"
required
ng-model="vm.requestFields[field.name]">
<input ng-switch-when="number"
type="number"
name="{{ field.name }}"
class="form-control"
required
min="{{ field.min }}"
max="{{ field.max }}"
ng-model="vm.requestFields[field.name]">
<div ng-if="!field._values" ng-switch on="field.type | optionTypeToInputType">
<input ng-switch-when="text"
type="text"
name="{{ field.name }}"
class="form-control"
required
ng-model="vm.requestFields[field.name]">
<input ng-switch-when="number"
type="number"
name="{{ field.name }}"
class="form-control"
required
min="{{ field.min }}"
max="{{ field.max }}"
ng-model="vm.requestFields[field.name]">
</div>
<div ng-if="field._values">
<select name="{{ field.name }}"
class="form-control"
ng-model="vm.requestFields[field.name]"
ng-options="value as value.title for value in field._values">
</select>
</div>
</div>
</form>

<pre>{{ vm.requestFields | json }}</pre>

<div class="bodyPartResponse" ng-show="vm.responseContent">
<h4>Response:</h4>
<pre>{{ vm.responseContent | json }}</pre>
Expand Down
45 changes: 27 additions & 18 deletions app/views/device-bodypart.html
Original file line number Diff line number Diff line change
Expand Up @@ -40,25 +40,34 @@ <h3 class="modal-title">Body Part</h3>
</select>
</div>

<div class="form-group" ng-repeat="option in vm.selectPlugin.selected.options" ng-switch on="option.type | optionTypeToInputType" show-form-errors>
<div class="form-group" ng-repeat="option in vm.selectPlugin.selected.options" show-form-errors>
<label class="control-label">{{ option.title }}</label>
<input ng-switch-when="text"
type="text"
maxlength="{{ option.type | maxCharLength }}"
name="{{ option.name }}"
class="form-control"
required
ng-model="vm.item.options[option.name]">
<input ng-switch-when="number"
type="number"
min="{{ option | minNumberOptionValue }}"
max="{{ option | maxNumberOptionValue }}"
name="{{ option.name }}"
class="form-control"
required
ng-model="vm.item.options[option.name]"
tooltip="Min: {{ option | minNumberOptionValue }}. Max: {{ option | maxNumberOptionValue }}"
tooltip-placement="right">
<div ng-if="!option._values" ng-switch on="option.type | optionTypeToInputType">
<input ng-switch-when="text"
type="text"
maxlength="{{ option.type | maxCharLength }}"
name="{{ option.name }}"
class="form-control"
required
ng-model="vm.itemOptions[option.name]">
<input ng-switch-when="number"
type="number"
min="{{ option | minNumberOptionValue }}"
max="{{ option | maxNumberOptionValue }}"
name="{{ option.name }}"
class="form-control"
required
ng-model="vm.itemOptions[option.name]"
tooltip="Min: {{ option | minNumberOptionValue }}. Max: {{ option | maxNumberOptionValue }}"
tooltip-placement="right">
</div>
<div ng-if="option._values">
<select name="{{ option.name }}"
class="form-control"
ng-model="vm.itemOptions[option.name]"
ng-options="value as value.title for value in option._values">
</select>
</div>
</div>
</form>

Expand Down

0 comments on commit 25beca8

Please sign in to comment.