[Code Style] Use prototypes in representation bundle

WTD-1482
This commit is contained in:
Victor Woeltjen 2015-08-14 16:10:41 -07:00
parent 07a2065c11
commit 365134b085
7 changed files with 216 additions and 192 deletions

View File

@ -238,6 +238,25 @@ define(
}; };
} }
/**
* A representer participates in the process of instantiating a
* representation of a domain object.
*
* @interface Representer
* @augments {Destroyable}
*/
/**
* Set the current representation in use, and the domain
* object being represented.
*
* @method Representer#represent
* @param {RepresentationDefinition} representation the
* definition of the representation in use
* @param {DomainObject} domainObject the domain object
* being represented
*/
return MCTRepresentation; return MCTRepresentation;
} }
); );

View File

@ -47,11 +47,23 @@ define(
* @param $rootScope Angular's root scope * @param $rootScope Angular's root scope
* @param actionContexr the context in which the action * @param actionContexr the context in which the action
* should be performed * should be performed
* @implements {Action}
*/ */
function ContextMenuAction($compile, $document, $window, $rootScope, actionContext) { function ContextMenuAction($compile, $document, $window, $rootScope, actionContext) {
this.$compile = $compile;
this.actionContext = actionContext;
this.getDocument = function () { return $document; };
this.getWindow = function () { return $window; };
this.getRootScope = function () { return $rootScope; };
}
function perform() { ContextMenuAction.prototype.perform = function () {
var winDim = [$window.innerWidth, $window.innerHeight], var $compile = this.$compile,
$document = this.getDocument(),
$window = this.getWindow(),
$rootScope = this.getRootScope(),
actionContext = this.actionContext,
winDim = [$window.innerWidth, $window.innerHeight],
eventCoors = [actionContext.event.pageX, actionContext.event.pageY], eventCoors = [actionContext.event.pageX, actionContext.event.pageY],
menuDim = GestureConstants.MCT_MENU_DIMENSIONS, menuDim = GestureConstants.MCT_MENU_DIMENSIONS,
body = $document.find('body'), body = $document.find('body'),
@ -105,12 +117,7 @@ define(
// Don't launch browser's context menu // Don't launch browser's context menu
actionContext.event.preventDefault(); actionContext.event.preventDefault();
}
return {
perform: perform
}; };
}
return ContextMenuAction; return ContextMenuAction;
} }

View File

@ -36,32 +36,30 @@ define(
* @memberof platform/representation * @memberof platform/representation
* @constructor * @constructor
* @param element the jqLite-wrapped element which should exhibit * @param element the jqLite-wrapped element which should exhibit
* the context mennu * the context menu
* @param {DomainObject} domainObject the object on which actions * @param {DomainObject} domainObject the object on which actions
* in the context menu will be performed * in the context menu will be performed
* @implements {Gesture}
*/ */
function ContextMenuGesture(element, domainObject) { function ContextMenuGesture(element, domainObject) {
var actionContext, function showMenu(event) {
stop; domainObject.getCapability('action').perform({
key: 'menu',
domainObject: domainObject,
event: event
});
}
// When context menu event occurs, show object actions instead // When context menu event occurs, show object actions instead
element.on('contextmenu', function (event) { element.on('contextmenu', showMenu);
actionContext = {key: 'menu', domainObject: domainObject, event: event};
stop = domainObject.getCapability('action').perform(actionContext);
});
return { this.showMenuCallback = showMenu;
/** this.element = element;
* Detach any event handlers associated with this gesture.
* @method
* @memberof ContextMenuGesture
* @memberof platform/representation.ContextMenuGesture#
*/
destroy: function () {
element.off('contextmenu', stop);
} }
ContextMenuGesture.prototype.destroy = function () {
this.element.off('contextmenu', this.showMenu);
}; };
}
return ContextMenuGesture; return ContextMenuGesture;
} }

View File

@ -35,6 +35,7 @@ define(
* *
* @memberof platform/representation * @memberof platform/representation
* @constructor * @constructor
* @implements {Gesture}
* @param $log Angular's logging service * @param $log Angular's logging service
* @param element the jqLite-wrapped element which should become * @param element the jqLite-wrapped element which should become
* draggable * draggable
@ -104,22 +105,19 @@ define(
element.on('dragstart', startDrag); element.on('dragstart', startDrag);
element.on('dragend', endDrag); element.on('dragend', endDrag);
return { this.element = element;
/** this.startDragCallback = startDrag;
* Detach any event handlers associated with this gesture. this.endDragCallback = endDrag;
* @memberof DragGesture
* @method
* @memberof platform/representation.DragGesture#
*/
destroy: function () {
// Detach listener
element.removeAttr('draggable');
element.off('dragstart', startDrag);
}
};
} }
DragGesture.prototype.destroy = function () {
// Detach listener
this.element.removeAttr('draggable');
this.element.off('dragstart', this.startDragCallback);
this.element.off('dragend', this.endDragCallback);
};
return DragGesture; return DragGesture;
} }
); );

View File

@ -32,7 +32,7 @@ define(
/** /**
* A DropGesture adds and maintains event handlers upon an element * A DropGesture adds and maintains event handlers upon an element
* such that it may act as a drop target for drag-drop composition. * such that it may act as a drop target for drag-drop composition.
*
* @memberof platform/representation * @memberof platform/representation
* @constructor * @constructor
* @param $q Angular's $q, for promise handling * @param $q Angular's $q, for promise handling
@ -40,7 +40,6 @@ define(
* @param {DomainObject} domainObject the domain object whose * @param {DomainObject} domainObject the domain object whose
* composition should be modified as a result of the drop. * composition should be modified as a result of the drop.
*/ */
function DropGesture(dndService, $q, element, domainObject) { function DropGesture(dndService, $q, element, domainObject) {
var actionCapability = domainObject.getCapability('action'), var actionCapability = domainObject.getCapability('action'),
action; // Action for the drop, when it occurs action; // Action for the drop, when it occurs
@ -122,18 +121,15 @@ define(
element.on('drop', drop); element.on('drop', drop);
} }
return { this.element = element;
/** this.dragOverCallback = dragOver;
* Detach any event handlers associated with this gesture. this.dropCallback = drop;
* @memberof platform/representation.DropGesture#
*/
destroy: function () {
element.off('dragover', dragOver);
element.off('drop', drop);
} }
};
} DropGesture.prototype.destroy = function () {
this.element.off('dragover', this.dragOverCallback);
this.element.off('drop', this.dropCallback);
};
return DropGesture; return DropGesture;

View File

@ -29,6 +29,29 @@ define(
function () { function () {
"use strict"; "use strict";
/**
* Handles the attachment of gestures (responses to DOM events,
* generally) to DOM elements which represent domain objects.
*
* @interface GestureService
*/
/**
* Attach a set of gestures (indicated by key) to a
* DOM element which represents a specific domain object.
* @method GestureService#attachGestures
* @param element the jqLite-wrapped DOM element which the
* user will interact with
* @param {DomainObject} domainObject the domain object which
* is represented by that element
* @param {string[]} gestureKeys an array of keys identifying
* which gestures should apply; these will be matched
* against the keys defined in the gestures' extension
* definitions
* @return {Destroyable} an object with a `destroy`
* method which can (and should) be used when
* gestures should no longer be applied to an element.
*/
/** /**
* The GestureProvider exposes defined gestures. Gestures are used * The GestureProvider exposes defined gestures. Gestures are used
* do describe and handle general-purpose interactions with the DOM * do describe and handle general-purpose interactions with the DOM
@ -41,6 +64,7 @@ define(
* where they are used. * where they are used.
* *
* @memberof platform/representation * @memberof platform/representation
* @implements {GestureService}
* @constructor * @constructor
* @param {Gesture[]} gestures an array of all gestures which are * @param {Gesture[]} gestures an array of all gestures which are
* available as extensions * available as extensions
@ -48,6 +72,14 @@ define(
function GestureProvider(gestures) { function GestureProvider(gestures) {
var gestureMap = {}; var gestureMap = {};
// Assemble all gestures into a map, for easy look up
gestures.forEach(function (gesture) {
gestureMap[gesture.key] = gesture;
});
this.gestureMap = gestureMap;
}
function releaseGesture(gesture) { function releaseGesture(gesture) {
// Invoke the gesture's "destroy" method (if there is one) // Invoke the gesture's "destroy" method (if there is one)
// to release any held resources and detach event handlers. // to release any held resources and detach event handlers.
@ -56,11 +88,12 @@ define(
} }
} }
function attachGestures(element, domainObject, gestureKeys) { GestureProvider.prototype.attachGestures = function attachGestures(element, domainObject, gestureKeys) {
// Look up the desired gestures, filter for applicability, // Look up the desired gestures, filter for applicability,
// and instantiate them. Maintain a reference to allow them // and instantiate them. Maintain a reference to allow them
// to be destroyed as a group later. // to be destroyed as a group later.
var attachedGestures = gestureKeys.map(function (key) { var gestureMap = this.gestureMap,
attachedGestures = gestureKeys.map(function (key) {
return gestureMap[key]; return gestureMap[key];
}).filter(function (Gesture) { }).filter(function (Gesture) {
return Gesture !== undefined && (Gesture.appliesTo ? return Gesture !== undefined && (Gesture.appliesTo ?
@ -76,36 +109,26 @@ define(
attachedGestures.forEach(releaseGesture); attachedGestures.forEach(releaseGesture);
} }
}; };
}
// Assemble all gestures into a map, for easy look up
gestures.forEach(function (gesture) {
gestureMap[gesture.key] = gesture;
});
return {
/**
* Attach a set of gestures (indicated by key) to a
* DOM element which represents a specific domain object.
* @method
* @memberof GestureProvider
* @param element the jqLite-wrapped DOM element which the
* user will interact with
* @param {DomainObject} domainObject the domain object which
* is represented by that element
* @param {string[]} gestureKeys an array of keys identifying
* which gestures should apply; these will be matched
* against the keys defined in the gestures' extension
* definitions
* @return {{ destroy: function }} an object with a `destroy`
* method which can (and should) be used when a
* gesture should no longer be applied to an element.
* @memberof platform/representation.GestureProvider#
*/
attachGestures: attachGestures
}; };
}
/**
* A destroyable object may have resources allocated which require
* explicit release.
*
* @interface Destroyable
*/
/**
* Release any resources associated with this object.
*
* @method Destroyable#destroy
*/
/**
* A gesture describes manners in which certain representations of
* domain objects may respond to DOM events upon those representations.
* @interface Gesture
* @augments Destroyable
*/
return GestureProvider; return GestureProvider;
} }

View File

@ -38,49 +38,32 @@ define(
* @param {Scope} scope the Angular scope for this representation * @param {Scope} scope the Angular scope for this representation
* @param element the JQLite-wrapped mct-representation element * @param element the JQLite-wrapped mct-representation element
* @constructor * @constructor
* @implements {Representer}
* @memberof platform/representation * @memberof platform/representation
*/ */
function GestureRepresenter(gestureService, scope, element) { function GestureRepresenter(gestureService, scope, element) {
var gestureHandle; this.gestureService = gestureService;
this.element = element;
function destroy() {
// Release any resources associated with these gestures
if (gestureHandle) {
gestureHandle.destroy();
}
} }
function represent(representation, domainObject) { GestureRepresenter.prototype.represent = function represent(representation, domainObject) {
// Clear out any existing gestures // Clear out any existing gestures
destroy(); this.destroy();
// Attach gestures - by way of the service. // Attach gestures - by way of the service.
gestureHandle = gestureService.attachGestures( this.gestureHandle = this.gestureService.attachGestures(
element, this.element,
domainObject, domainObject,
(representation || {}).gestures || [] (representation || {}).gestures || []
); );
}
return {
/**
* Set the current representation in use, and the domain
* object being represented.
*
* @param {RepresentationDefinition} representation the
* definition of the representation in use
* @param {DomainObject} domainObject the domain object
* being represented
* @memberof platform/representation.GestureRepresenter#
*/
represent: represent,
/**
* Release any resources associated with this representer.
* @memberof platform/representation.GestureRepresenter#
*/
destroy: destroy
}; };
GestureRepresenter.prototype.destroy = function () {
// Release any resources associated with these gestures
if (this.gestureHandle) {
this.gestureHandle.destroy();
} }
};
return GestureRepresenter; return GestureRepresenter;
} }