From 66e0d2fcfac948ec95d49ffcfd3165b71d6a0da8 Mon Sep 17 00:00:00 2001 From: Victor Woeltjen Date: Mon, 9 Mar 2015 17:16:31 -0700 Subject: [PATCH] [Forms] Complete initial selector Complete initial implementation of the domain object selector for WTD-987. --- .../res/templates/controls/selector.html | 4 +- .../src/controllers/SelectorController.js | 74 ++++++++++++++++++- 2 files changed, 74 insertions(+), 4 deletions(-) diff --git a/platform/commonUI/general/res/templates/controls/selector.html b/platform/commonUI/general/res/templates/controls/selector.html index b8ec1a2849..7411f2ac10 100644 --- a/platform/commonUI/general/res/templates/controls/selector.html +++ b/platform/commonUI/general/res/templates/controls/selector.html @@ -27,7 +27,7 @@ > + ng-click="selector.deselect(selector.listModel.selectedObject)"> < @@ -48,7 +48,7 @@
diff --git a/platform/commonUI/general/src/controllers/SelectorController.js b/platform/commonUI/general/src/controllers/SelectorController.js index cec420f682..1ec4fcac46 100644 --- a/platform/commonUI/general/src/controllers/SelectorController.js +++ b/platform/commonUI/general/src/controllers/SelectorController.js @@ -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 }; }