[Common UI] Initial commonUI bundles

Bring in work on general-purpose and over-arching
user interface bundles from the sandbox transition
branch. WTD-574.
This commit is contained in:
Victor Woeltjen
2014-11-23 15:41:20 -08:00
parent 0cd331e8a5
commit 1b0303e517
73 changed files with 6035 additions and 0 deletions

View File

@ -0,0 +1,56 @@
/*global define*/
/**
* Wrapper for both "context" and "composition" capabilities;
* ensures that any domain objects reachable in Edit mode
* are also wrapped as EditableDomainObjects
*/
define(
[],
function () {
'use strict';
return function EditableContextCapability(
contextCapability,
editableObject,
domainObject,
factory
) {
var capability = Object.create(contextCapability);
function isDomainObject(obj) {
return typeof obj.getId === 'function' &&
typeof obj.getModel === 'function' &&
typeof obj.getCapability === 'function';
}
function makeEditableObject(obj) {
return isDomainObject(obj) ?
factory.getEditableObject(obj) :
obj;
}
function makeEditable(obj) {
return Array.isArray(obj) ?
obj.map(makeEditableObject) :
makeEditableObject(obj);
}
// Replace all methods; return only editable domain objects.
Object.keys(contextCapability).forEach(function (k) {
capability[k] = function () {
var result = contextCapability[k].apply(
capability,
arguments
);
return result.then ? // promise-like
result.then(makeEditable) :
makeEditable(result);
};
});
return capability;
};
}
);

View File

@ -0,0 +1,30 @@
/*global define*/
/**
* Editable Persistence Capability. Overrides the persistence capability
* normally exhibited by a domain object to ensure that changes made
* during edit mode are not immediately stored to the database or other
* backing storage.
*/
define(
function () {
'use strict';
return function EditablePersistenceCapability(
persistenceCapability,
editableObject,
domainObject,
cache
) {
var persistence = Object.create(persistenceCapability);
// Simply trigger refresh of in-view objects; do not
// write anything to database.
persistence.persist = function () {
cache.markDirty(editableObject);
};
return persistence;
};
}
);

View File

@ -0,0 +1,55 @@
/*global define*/
/**
* Implements "save" and "cancel" as capabilities of
* the object. In editing mode, user is seeing/using
* a copy of the object (an EditableDomainObject)
* which is disconnected from persistence; the Save
* and Cancel actions can use this capability to
* propagate changes from edit mode to the underlying
* actual persistable object.
*/
define(
[],
function () {
'use strict';
return function EditorCapability(
persistenceCapability,
editableObject,
domainObject,
cache
) {
function doMutate() {
return domainObject.useCapability('mutation', function () {
return editableObject.getModel();
});
}
function doPersist() {
return persistenceCapability.persist();
}
function saveOthers() {
return cache.saveAll();
}
function markClean() {
return cache.markClean(editableObject);
}
return {
save: function () {
return Promise.resolve(doMutate())
.then(doPersist)
.then(markClean)
.then(saveOthers);
},
cancel: function () {
return Promise.resolve(undefined);
}
};
};
}
);