[Forms] Complete initial selector

Complete initial implementation of the domain object
selector for WTD-987.
This commit is contained in:
Victor Woeltjen 2015-03-09 17:16:31 -07:00
parent 4146a2ad01
commit 66e0d2fcfa
2 changed files with 74 additions and 4 deletions

View File

@ -27,7 +27,7 @@
<span class='ui-symbol'>&gt;</span>
</a>
<a class='btn major' href=''
ng-click="selector.deselect">
ng-click="selector.deselect(selector.listModel.selectedObject)">
<span class='ui-symbol'>&lt;</span>
</a>
</div>
@ -48,7 +48,7 @@
<div class='line treeview checkbox-list' name='selected'>
<mct-representation ng-repeat="selectedObject in selector.selected()"
key="'breadcrumb'"
key="'label'"
mct-object="selectedObject"
ng-click="selector.listModel.selectedObject = selectedObject"
ng-class="{ test: selector.listModel.selectedObject === selectedObject }">

View File

@ -5,6 +5,8 @@ define(
function () {
"use strict";
var ROOT_ID = "ROOT";
/**
* Controller for the domain object selector control.
* @constructor
@ -14,6 +16,9 @@ define(
*/
function SelectorController(objectService, $scope) {
var treeModel = {},
listModel = {},
selectedObjects = [],
rootObject,
previousSelected;
// For watch; look at the user's selection in the tree
@ -26,6 +31,16 @@ define(
return $scope.ngModel[$scope.field];
}
// Get the value of the field being edited
function setField(value) {
$scope.ngModel[$scope.field] = value;
}
// Store root object for subsequent exposure to template
function storeRoot(objects) {
rootObject = objects[ROOT_ID];
}
// Check that a selection is of the valid type
function validateTreeSelection(selectedObject) {
var type = selectedObject &&
@ -41,12 +56,67 @@ define(
previousSelected = treeModel.selectedObject;
}
// Update the right-hand list of currently-selected objects
function updateList(ids) {
function updateSelectedObjects(objects) {
// Look up from the
function getObject(id) { return objects[id]; }
selectedObjects = ids.filter(getObject).map(getObject);
}
// Look up objects by id, then populate right-hand list
objectService.get(ids).then(updateSelectedObjects);
}
// Reject attempts to select objects of the wrong type
$scope.$watch(getTreeSelection, validateTreeSelection);
// Make sure right-hand list matches underlying model
$scope.$watchCollection(getField, updateList);
// Look up root object, then store it
objectService.get(ROOT_ID).then(storeRoot);
return {
// Expose tree model for use in template directly
treeModel: treeModel
/**
* Get the root object to show in the left-hand tree.
* @returns {DomainObject} the root object
*/
root: function () {
return rootObject;
},
/**
* Add a domain object to the list of selected objects.
* @param {DomainObject} the domain object to select
*/
select: function (domainObject) {
var id = domainObject && domainObject.getId(),
list = getField() || [];
// Only select if we have a valid id,
// and it isn't already selected
if (id && list.indexOf(id) === -1) {
setField(list.concat([id]));
}
},
/**
* Remove a domain object from the list of selected objects.
* @param {DomainObject} the domain object to select
*/
deselect: function (domainObject) {
var id = domainObject && domainObject.getId(),
list = getField() || [];
// Only change if this was a valid id,
// for an object which was already selected
if (id && list.indexOf(id) !== -1) {
// Filter it out of the current field
setField(list.filter(function (otherId) {
return otherId !== id;
}));
}
},
// Expose tree/list model for use in template directly
treeModel: treeModel,
listModel: listModel
};
}