Consolidate processing of node properties

Refactor and consolidate code to unify processing of metadata, extra, and
instance_info

Change-Id: I1726d0c7a8ecec83039d61be752940099bd2fa04
This commit is contained in:
Peter Piela 2016-10-02 18:30:37 -04:00
parent 1417ff4c1d
commit bb0948d00f
3 changed files with 84 additions and 212 deletions

View File

@ -51,7 +51,24 @@
ctrl.modalTitle = gettext("Node"); ctrl.modalTitle = gettext("Node");
ctrl.submitButtonTitle = gettext("Submit"); ctrl.submitButtonTitle = gettext("Submit");
ctrl.showInstanceInfo = false;
/* A property-collection is a set of properties that will be displayed
in the node view as a minimal browser ui that supports:
- adding new properties
- displaying the list of properties in the set
- changing the value of properties
*/
ctrl.propertyCollections = [
{id: "properties",
title: "Properties",
addPrompt: "Add Property",
placeholder: "Property Name"
},
{id: "extra",
title: "Extras",
addPrompt: "Add Extra",
placeholder: "Extra Property Name"
}];
// Node object suitable for Ironic api // Node object suitable for Ironic api
ctrl.node = { ctrl.node = {
@ -247,46 +264,26 @@
}; };
/** /**
* @desription Delete a node property * @description Check whether the specified property already exists
*
* @param {string} propertyName - Name of the property
* @return {void}
*/
ctrl.deleteProperty = function(propertyName) {
delete ctrl.node.properties[propertyName];
};
/**
* @description Check whether the specified node property already exists
* *
* @param {string} collectionId - Collection ID
* @param {string} propertyName - Name of the property * @param {string} propertyName - Name of the property
* @return {boolean} True if the property already exists, * @return {boolean} True if the property already exists,
* otherwise false * otherwise false
*/ */
ctrl.checkPropertyUnique = function(propertyName) { ctrl.collectionCheckPropertyUnique = function(collectionId, propertyName) {
return !(propertyName in ctrl.node.properties); return !(propertyName in ctrl.node[collectionId]);
}; };
/** /**
* @description Delete a node metadata property * @description Delete a node metadata property
* *
* @param {string} collectionId - Collection ID
* @param {string} propertyName - Name of the property * @param {string} propertyName - Name of the property
* @return {void} * @return {void}
*/ */
ctrl.deleteExtra = function(propertyName) { ctrl.collectionDeleteProperty = function(collectionId, propertyName) {
delete ctrl.node.extra[propertyName]; delete ctrl.node[collectionId][propertyName];
};
/**
* @description Check whether the specified node metadata property
* already exists
*
* @param {string} propertyName - Name of the metadata property
* @return {boolean} True if the property already exists,
* otherwise false
*/
ctrl.checkExtraUnique = function(propertyName) {
return !(propertyName in ctrl.node.extra);
}; };
/** /**

View File

@ -69,171 +69,64 @@
</select> </select>
</div> </div>
</div> </div>
<!--properties add-property--> <!--property collections-->
<form id="addPropertyForm" <div ng-repeat="collection in ctrl.propertyCollections">
name="addPropertyForm"> <form
<div class="form-group"> id="add-{$ collection.id $}-form"
<label for="properties" name="add-{$ collection.id $}-form">
class="control-label" <div class="form-group">
translate>Properties</label> <label for="add-{$ collection.id $}-input"
<div class="input-group input-group-sm"> class="control-label"
<span class="input-group-addon" translate>{$ collection.title $}</label>
style="width:25%;text-align:right" <div class="input-group input-group-sm">
translate> <span class="input-group-addon"
Add New Property:</span> style="width:25%;text-align:right"
<input class="form-control" translate>
id="properties" {$ collection.addPrompt $}:</span>
type="text" <input class="form-control"
ng-model="propertyName" id="add-{$ collection.id $}-input"
validate-unique="ctrl.checkPropertyUnique" type="text"
placeholder="{$ 'Property Name' | translate $}"/> ng-model="ctrl[collection.id]"
<span class="input-group-btn"> placeholder="{$ collection.placeholder | translate $}"/>
<button class="btn btn-primary" <span class="input-group-btn">
type="button" <button class="btn btn-primary"
ng-disabled="!propertyName || addPropertyForm.$invalid" type="button"
ng-click="ctrl.node.properties[propertyName] = null; ng-disabled="!ctrl[collection.id] ||
propertyName = null"> !ctrl.collectionCheckPropertyUnique(collection.id,
<span class="fa fa-plus"> </span> ctrl[collection.id]) ||
</button> add-{$ collection.id $}-form.$invalid"
</span> ng-click="ctrl.node[collection.id][ctrl[collection.id]] = null;
</div> ctrl[collection.id] = null">
</div> <span class="fa fa-plus"> </span>
</form> </button>
<!--properties property-list--> </span>
<form id="propertiesForm"
name="propertiesForm">
<div class="form-group">
<div class="input-group input-group-sm"
ng-repeat="(propertyName, propertyValue) in ctrl.node.properties">
<span class="input-group-addon"
style="width:25%;text-align:right">
{$ propertyName $}
</span>
<input class="form-control"
type="text"
name="{$ propertyName $}"
ng-model="ctrl.node.properties[propertyName]"
ng-required="true"/>
<div class="input-group-btn">
<a class="btn btn-default"
ng-click="ctrl.deleteProperty(propertyName)">
<span class="fa fa-minus"> </span>
</a>
</div> </div>
</div> </div>
</div> </form>
</form> <form id="{$ collection.id $}-form"
<!--extras add-property--> name="{$ collection.id $}-form">
<form id="addExtraForm" <div class="form-group">
name="addExtraForm"> <div class="input-group input-group-sm"
<div class="form-group"> ng-repeat="(propertyName, propertyValue) in ctrl.node[collection.id]">
<label for="extras" <span class="input-group-addon"
class="control-label" style="width:25%;text-align:right">
translate>Extras</label> {$ propertyName $}
<div class="input-group input-group-sm"> </span>
<span class="input-group-addon" <input class="form-control"
style="width:25%;text-align:right" type="text"
translate> name="{$ propertyName $}"
Add Extra:</span> ng-model="ctrl.node[collection.id][propertyName]"
<input class="form-control" ng-required="true"/>
id="extras" <div class="input-group-btn">
type="text" <a class="btn btn-default"
ng-model="extraName" ng-click="ctrl.collectionDeleteProperty(collection.id, propertyName)">
validate-unique="ctrl.checkExtraUnique" <span class="fa fa-minus"> </span>
placeholder="{$ 'Extra Property Name' | translate $}"/> </a>
<span class="input-group-btn"> </div>
<button class="btn btn-primary"
type="button"
ng-disabled="!extraName || addExtraForm.$invalid"
ng-click="ctrl.node.extra[extraName] = null;
extraName = null">
<span class="fa fa-plus"> </span>
</button>
</span>
</div>
</div>
</form>
<!--extras property-list-->
<form id="extraForm"
name="extraForm">
<div class="form-group">
<div class="input-group input-group-sm"
ng-repeat="(propertyName, propertyValue) in ctrl.node.extra">
<span class="input-group-addon"
style="width:25%;text-align:right">
{$ propertyName $}
</span>
<input class="form-control"
type="text"
name="{$ propertyName $}"
ng-model="ctrl.node.extra[propertyName]"
ng-required="true"/>
<div class="input-group-btn">
<a class="btn btn-default"
ng-click="ctrl.deleteExtra(propertyName)">
<span class="fa fa-minus"> </span>
</a>
</div> </div>
</div> </div>
</div> </form>
</form> </div>
<!--instance-info add-property-->
<form ng-if="ctrl.showInstanceInfo"
id="addInstancePropertyForm"
name="addInstancePropertyForm">
<div class="form-group">
<label for="instanceProperty"
class="control-label"
translate>Instance Info</label>
<div class="input-group input-group-sm">
<span class="input-group-addon"
style="width:25%;text-align:right"
translate>
Add New Instance Property:</span>
<input class="form-control"
id="instanceProperty"
type="text"
ng-model="instancePropertyName"
validate-unique="ctrl.checkInstancePropertyUnique"
placeholder="{$ 'Instance Property Name' | translate $}"/>
<span class="input-group-btn">
<button class="btn btn-primary"
type="button"
ng-disabled="!instancePropertyName ||
addInstancePropertyForm.$invalid"
ng-click="ctrl.node.instance_info[instancePropertyName] = null;
instancePropertyName = null">
<span class="fa fa-plus"> </span>
</button>
</span>
</div>
</div>
</form>
<!--instance-info property-list-->
<form ng-if="ctrl.showInstanceInfo"
id="instanceInfoForm"
name="instanceInfoForm">
<div class="form-group">
<div class="input-group input-group-sm"
ng-repeat="(propertyName, propertyValue) in ctrl.node.instance_info">
<span class="input-group-addon"
style="width:25%;text-align:right">
{$ propertyName $}
</span>
<input class="form-control"
type="text"
name="{$ propertyName $}"
ng-model="ctrl.node.instance_info[propertyName]"
ng-required="true"/>
<div class="input-group-btn">
<a class="btn btn-default"
ng-click="ctrl.deleteInstanceProperty(propertyName)">
<span class="fa fa-minus"> </span>
</a>
</div>
</div>
</div>
</form>
</div> </div>
<!--end node info tab--> <!--end node info tab-->

View File

@ -54,10 +54,14 @@
ctrl.submitButtonTitle = gettext("Update Node"); ctrl.submitButtonTitle = gettext("Update Node");
ctrl.node.instance_info = {}; ctrl.node.instance_info = {};
ctrl.showInstanceInfo = true;
ctrl.baseNode = null; ctrl.baseNode = null;
ctrl.propertyCollections.push({id: "instance_info",
title: "Instance Info",
addPrompt: "Add Instance Property",
placeholder: "Instance Property Name"});
init(node); init(node);
function init(node) { function init(node) {
@ -96,28 +100,6 @@
}); });
} }
/**
* @description Delete a node instance property
*
* @param {string} propertyName - Name of the property
* @return {void}
*/
ctrl.deleteInstanceProperty = function(propertyName) {
delete ctrl.node.instance_info[propertyName];
};
/**
* @description Check whether the specified node instance property
* already exists
*
* @param {string} propertyName - Name of the instance property
* @return {boolean} True if the property already exists,
* otherwise false
*/
ctrl.checkInstancePropertyUnique = function(propertyName) {
return !(propertyName in ctrl.node.instance_info);
};
/** /**
* @description Construct a patch that converts source node into * @description Construct a patch that converts source node into
* target node * target node