| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768 |
- import inherits from 'inherits-browser';
- import CreateShapeHandler from './CreateShapeHandler';
- /**
- * A handler that attaches a label to a given target shape.
- *
- * @param {Canvas} canvas
- */
- export default function CreateLabelHandler(canvas) {
- CreateShapeHandler.call(this, canvas);
- }
- inherits(CreateLabelHandler, CreateShapeHandler);
- CreateLabelHandler.$inject = [ 'canvas' ];
- // api //////////////////////
- var originalExecute = CreateShapeHandler.prototype.execute;
- /**
- * Appends a label to a target shape.
- *
- * @method CreateLabelHandler#execute
- *
- * @param {Object} context
- * @param {ElementDescriptor} context.target the element the label is attached to
- * @param {ElementDescriptor} context.parent the parent object
- * @param {Point} context.position position of the new element
- */
- CreateLabelHandler.prototype.execute = function(context) {
- var label = context.shape;
- ensureValidDimensions(label);
- label.labelTarget = context.labelTarget;
- return originalExecute.call(this, context);
- };
- var originalRevert = CreateShapeHandler.prototype.revert;
- /**
- * Undo append by removing the shape
- */
- CreateLabelHandler.prototype.revert = function(context) {
- context.shape.labelTarget = null;
- return originalRevert.call(this, context);
- };
- // helpers //////////////////////
- function ensureValidDimensions(label) {
- // make sure a label has valid { width, height } dimensions
- [ 'width', 'height' ].forEach(function(prop) {
- if (typeof label[prop] === 'undefined') {
- label[prop] = 0;
- }
- });
- }
|