Merge remote-tracking branch 'origin/open987' into open-master

This commit is contained in:
bwyu
2015-03-16 10:28:55 -07:00
14 changed files with 721 additions and 2 deletions

View File

@@ -28,7 +28,7 @@ define(
editableObject,
domainObject,
cache,
true // Not idempotent
true // Idempotent
);
};
}

View File

@@ -0,0 +1,36 @@
/*global define*/
define(
['./EditableLookupCapability'],
function (EditableLookupCapability) {
'use strict';
/**
* Wrapper for the "relationship" capability;
* ensures that any domain objects reachable in Edit mode
* are also wrapped as EditableDomainObjects.
*
* Meant specifically for use by EditableDomainObject and the
* associated cache; the constructor signature is particular
* to a pattern used there and may contain unused arguments.
*/
return function EditableRelationshipCapability(
relationshipCapability,
editableObject,
domainObject,
cache
) {
// This is a "lookup" style capability (it looks up other
// domain objects), but we do not want to return the same
// specific value every time (composition may change)
return new EditableLookupCapability(
relationshipCapability,
editableObject,
domainObject,
cache,
false // Not idempotent
);
};
}
);

View File

@@ -14,6 +14,7 @@ define(
'../capabilities/EditablePersistenceCapability',
'../capabilities/EditableContextCapability',
'../capabilities/EditableCompositionCapability',
'../capabilities/EditableRelationshipCapability',
'../capabilities/EditorCapability',
'./EditableDomainObjectCache'
],
@@ -21,6 +22,7 @@ define(
EditablePersistenceCapability,
EditableContextCapability,
EditableCompositionCapability,
EditableRelationshipCapability,
EditorCapability,
EditableDomainObjectCache
) {
@@ -30,6 +32,7 @@ define(
persistence: EditablePersistenceCapability,
context: EditableContextCapability,
composition: EditableCompositionCapability,
relationship: EditableRelationshipCapability,
editor: EditorCapability
};
@@ -64,7 +67,10 @@ define(
// Override certain capabilities
editableObject.getCapability = function (name) {
var delegateArguments = getDelegateArguments(name, arguments),
capability = domainObject.getCapability.apply(this, delegateArguments),
capability = domainObject.getCapability.apply(
this,
delegateArguments
),
factory = capabilityFactories[name];
return (factory && capability) ?

View File

@@ -0,0 +1,54 @@
/*global define,describe,it,expect,beforeEach,jasmine*/
define(
["../../src/capabilities/EditableRelationshipCapability"],
function (EditableRelationshipCapability) {
"use strict";
describe("An editable relationship capability", function () {
var mockContext,
mockEditableObject,
mockDomainObject,
mockTestObject,
someValue,
mockFactory,
capability;
beforeEach(function () {
// EditableContextCapability should watch ALL
// methods for domain objects, so give it an
// arbitrary interface to wrap.
mockContext =
jasmine.createSpyObj("context", [ "getDomainObject" ]);
mockTestObject = jasmine.createSpyObj(
"domainObject",
[ "getId", "getModel", "getCapability" ]
);
mockFactory =
jasmine.createSpyObj("factory", ["getEditableObject"]);
someValue = { x: 42 };
mockContext.getDomainObject.andReturn(mockTestObject);
mockFactory.getEditableObject.andReturn(someValue);
capability = new EditableRelationshipCapability(
mockContext,
mockEditableObject,
mockDomainObject,
mockFactory
);
});
// Most behavior is tested for EditableLookupCapability,
// so just verify that this isse
it("presumes non-idempotence of its wrapped capability", function () {
expect(capability.getDomainObject())
.toEqual(capability.getDomainObject());
expect(mockContext.getDomainObject.calls.length).toEqual(2);
});
});
}
);

View File

@@ -9,6 +9,7 @@
"capabilities/EditableContextCapability",
"capabilities/EditableLookupCapability",
"capabilities/EditablePersistenceCapability",
"capabilities/EditableRelationshipCapability",
"capabilities/EditorCapability",
"controllers/EditActionController",
"controllers/EditController",

View File

@@ -88,6 +88,11 @@
{
"key": "SplitPaneController",
"implementation": "controllers/SplitPaneController.js"
},
{
"key": "SelectorController",
"implementation": "controllers/SelectorController.js",
"depends": [ "objectService", "$scope" ]
}
],
"directives": [
@@ -185,6 +190,12 @@
"uses": [ "view" ]
}
],
"controls": [
{
"key": "selector",
"templateUrl": "templates/controls/selector.html"
}
],
"licenses": [
{
"name": "Modernizr",

View File

@@ -0,0 +1,62 @@
<div class='form-control complex channel-selector cols cols-32'
ng-controller="SelectorController as selector">
<div class='col col-15'>
<div class='line field-hints'>Available</div>
<!--div id='_form_filter' class='line filter'>
<input type='text' class='control filter' name='filter-available' />
<a class='icon ui-symbol t-available-trigger'
href=''
title="Filter is case sensitive">M</a>
</div>
<div class="line">
Showing {{shown}} of {{count}} available options.
</div -->
<div class='line treeview checkbox-list' name='available'>
<mct-representation key="'tree'"
mct-object="selector.root()"
ng-model="selector.treeModel">
</mct-representation>
</div>
</div>
<div class='col col-2'>
<div class='btn-holder valign-mid btns-add-remove'>
<a class='btn major'
ng-click="selector.select(selector.treeModel.selectedObject)">
<span class='ui-symbol'>&gt;</span>
</a>
<a class='btn major'
ng-click="selector.deselect(selector.listModel.selectedObject)">
<span class='ui-symbol'>&lt;</span>
</a>
</div>
</div>
<div class='col col-15'>
<div class='line field-hints'>Selected</div>
<!-- div id='_form_filter' class='line filter'>
<input type='text' class='control filter' name='filter-selected' />
<a class='icon ui-symbol t-selected-trigger'
href=''
title="Filter is case sensitive">
M
</a>
</div>
<div class="line">
Showing {{shown}} of {{count}} available options.
</div -->
<div class='line treeview checkbox-list' name='selected'>
<ul class="tree">
<li ng-repeat="selectedObject in selector.selected()">
<mct-representation key="'label'"
mct-object="selectedObject"
ng-click="selector.listModel.selectedObject = selectedObject"
ng-class="{ test: selector.listModel.selectedObject === selectedObject }">
</mct-representation>
</li>
</ul>
</div>
</div>
</div>

View File

@@ -0,0 +1,135 @@
/*global define*/
define(
[],
function () {
"use strict";
var ROOT_ID = "ROOT";
/**
* Controller for the domain object selector control.
* @constructor
* @param {ObjectService} objectService service from which to
* read domain objects
* @param $scope Angular scope for this controller
*/
function SelectorController(objectService, $scope) {
var treeModel = {},
listModel = {},
selectedObjects = [],
rootObject,
previousSelected;
// For watch; look at the user's selection in the tree
function getTreeSelection() {
return treeModel.selectedObject;
}
// Get the value of the field being edited
function getField() {
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 &&
selectedObject.getCapability('type');
// Delegate type-checking to the capability...
if (!type || !type.instanceOf($scope.structure.type)) {
treeModel.selectedObject = previousSelected;
}
// Track current selection to restore it if an invalid
// selection is made later.
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.getObjects(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.getObjects([ROOT_ID]).then(storeRoot);
return {
/**
* 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;
}));
// Clear the current list selection
delete listModel.selectedObject;
}
},
/**
* Get the currently-selected domain objects.
* @returns {DomainObject[]} the current selection
*/
selected: function () {
return selectedObjects;
},
// Expose tree/list model for use in template directly
treeModel: treeModel,
listModel: listModel
};
}
return SelectorController;
}
);

View File

@@ -0,0 +1,163 @@
/*global define,Promise,describe,it,expect,beforeEach,waitsFor,jasmine*/
define(
["../../src/controllers/SelectorController"],
function (SelectorController) {
"use strict";
describe("The controller for the 'selector' control", function () {
var mockObjectService,
mockScope,
mockDomainObject,
mockType,
mockDomainObjects,
controller;
function promiseOf(v) {
return (v || {}).then ? v : {
then: function (callback) {
return promiseOf(callback(v));
}
};
}
function makeMockObject(id) {
var mockObject = jasmine.createSpyObj(
'object-' + id,
[ 'getId' ]
);
mockObject.getId.andReturn(id);
return mockObject;
}
beforeEach(function () {
mockObjectService = jasmine.createSpyObj(
'objectService',
['getObjects']
);
mockScope = jasmine.createSpyObj(
'$scope',
['$watch', '$watchCollection']
);
mockDomainObject = jasmine.createSpyObj(
'domainObject',
[ 'getCapability', 'hasCapability' ]
);
mockType = jasmine.createSpyObj(
'type',
[ 'instanceOf' ]
);
mockDomainObjects = {};
[ "ROOT", "abc", "def", "xyz" ].forEach(function (id) {
mockDomainObjects[id] = makeMockObject(id);
});
mockDomainObject.getCapability.andReturn(mockType);
mockObjectService.getObjects.andReturn(promiseOf(mockDomainObjects));
mockScope.field = "testField";
mockScope.ngModel = {};
controller = new SelectorController(
mockObjectService,
mockScope
);
});
it("loads the root object", function () {
expect(mockObjectService.getObjects)
.toHaveBeenCalledWith(["ROOT"]);
});
it("watches for changes in selection in left-hand tree", function () {
var testObject = { a: 123, b: 456 };
// This test is sensitive to ordering of watch calls
expect(mockScope.$watch.calls.length).toEqual(1);
// Make sure we're watching the correct object
controller.treeModel.selectedObject = testObject;
expect(mockScope.$watch.calls[0].args[0]()).toBe(testObject);
});
it("watches for changes in controlled property", function () {
var testValue = [ "a", "b", 1, 2 ];
// This test is sensitive to ordering of watch calls
expect(mockScope.$watchCollection.calls.length).toEqual(1);
// Make sure we're watching the correct object
mockScope.ngModel = { testField: testValue };
expect(mockScope.$watchCollection.calls[0].args[0]()).toBe(testValue);
});
it("rejects selection of incorrect types", function () {
mockScope.structure = { type: "someType" };
mockType.instanceOf.andReturn(false);
controller.treeModel.selectedObject = mockDomainObject;
// Fire the watch
mockScope.$watch.calls[0].args[1](mockDomainObject);
// Should have cleared the selection
expect(controller.treeModel.selectedObject).toBeUndefined();
// Verify interaction (that instanceOf got a useful argument)
expect(mockType.instanceOf).toHaveBeenCalledWith("someType");
});
it("permits selection of matching types", function () {
mockScope.structure = { type: "someType" };
mockType.instanceOf.andReturn(true);
controller.treeModel.selectedObject = mockDomainObject;
// Fire the watch
mockScope.$watch.calls[0].args[1](mockDomainObject);
// Should have preserved the selection
expect(controller.treeModel.selectedObject).toEqual(mockDomainObject);
// Verify interaction (that instanceOf got a useful argument)
expect(mockType.instanceOf).toHaveBeenCalledWith("someType");
});
it("loads objects when the underlying list changes", function () {
var testIds = [ "abc", "def", "xyz" ];
// This test is sensitive to ordering of watch calls
expect(mockScope.$watchCollection.calls.length).toEqual(1);
// Make sure we're watching the correct object
mockScope.ngModel = { testField: testIds };
// Fire the watch
mockScope.$watchCollection.calls[0].args[1](testIds);
// Should have loaded the corresponding objects
expect(mockObjectService.getObjects).toHaveBeenCalledWith(testIds);
});
it("exposes the root object to populate the left-hand tree", function () {
expect(controller.root()).toEqual(mockDomainObjects.ROOT);
});
it("adds objects to the underlying model", function () {
expect(mockScope.ngModel.testField).toBeUndefined();
controller.select(mockDomainObjects.def);
expect(mockScope.ngModel.testField).toEqual(["def"]);
controller.select(mockDomainObjects.abc);
expect(mockScope.ngModel.testField).toEqual(["def", "abc"]);
});
it("removes objects to the underlying model", function () {
controller.select(mockDomainObjects.def);
controller.select(mockDomainObjects.abc);
expect(mockScope.ngModel.testField).toEqual(["def", "abc"]);
controller.deselect(mockDomainObjects.def);
expect(mockScope.ngModel.testField).toEqual(["abc"]);
});
it("provides a list of currently-selected objects", function () {
// Verify precondition
expect(controller.selected()).toEqual([]);
// Select some objects
controller.select(mockDomainObjects.def);
controller.select(mockDomainObjects.abc);
// Fire the watch for the id changes...
mockScope.$watchCollection.calls[0].args[1](
mockScope.$watchCollection.calls[0].args[0]()
);
// Should have loaded and exposed those objects
expect(controller.selected()).toEqual(
[mockDomainObjects.def, mockDomainObjects.abc]
);
});
});
}
);

View File

@@ -4,6 +4,7 @@
"controllers/ClickAwayController",
"controllers/ContextMenuController",
"controllers/GetterSetterController",
"controllers/SelectorController",
"controllers/SplitPaneController",
"controllers/ToggleController",
"controllers/TreeNodeController",