diff --git a/platform/features/layout/src/elements/LineHandle.js b/platform/features/layout/src/elements/LineHandle.js new file mode 100644 index 0000000000..03474df1c1 --- /dev/null +++ b/platform/features/layout/src/elements/LineHandle.js @@ -0,0 +1,61 @@ +/*global define*/ +define( + [], + function () { + 'use strict'; + + /** + * Handle for changing x/y position of a line's end point. + * This is used to support drag handles for line elements + * in a fixed position view. Field names for opposite ends + * are provided to avoid zero-length lines. + * @constructor + * @param element the line element + * @param {string} xProperty field which stores x position + * @param {string} yProperty field which stores x position + * @param {string} xOther field which stores x of other end + * @param {string} yOther field which stores y of other end + */ + function LineHandle(element, xProperty, yProperty, xOther, yOther) { + return { + /** + * Get/set the x position of the lower-right corner + * of the handle-controlled element, changing size + * as necessary. + */ + x: function (value) { + if (arguments.length > 0) { + // Ensure we stay in view + value = Math.max(value, 0); + // Make sure end points will still be different + if (element[yOther] !== element[yProperty] || + element[xOther] !== value) { + element[xProperty] = value; + } + } + return element[xProperty]; + }, + /** + * Get/set the y position of the lower-right corner + * of the handle-controlled element, changing size + * as necessary. + */ + y: function (value) { + if (arguments.length > 0) { + // Ensure we stay in view + value = Math.max(value, 0); + // Make sure end points will still be different + if (element[xOther] !== element[xProperty] || + element[yOther] !== value) { + element[yProperty] = value; + } + } + return element[yProperty]; + } + }; + } + + return LineHandle; + + } +); \ No newline at end of file diff --git a/platform/features/layout/src/elements/LineProxy.js b/platform/features/layout/src/elements/LineProxy.js index 9247840b54..dc4729ef6e 100644 --- a/platform/features/layout/src/elements/LineProxy.js +++ b/platform/features/layout/src/elements/LineProxy.js @@ -1,8 +1,8 @@ /*global define*/ define( - ['./ElementProxy'], - function (ElementProxy) { + ['./ElementProxy', './LineHandle'], + function (ElementProxy, LineHandle) { 'use strict'; /** @@ -15,7 +15,11 @@ define( * @param {Array} elements the full array of elements */ function LineProxy(element, index, elements) { - var proxy = new ElementProxy(element, index, elements); + var proxy = new ElementProxy(element, index, elements), + handles = [ + new LineHandle(element, 'x', 'y', 'x2', 'y2'), + new LineHandle(element, 'x2', 'y2', 'x', 'y') + ]; /** * Get the top-left x coordinate, in grid space, of @@ -105,6 +109,15 @@ define( return element.y2 - proxy.y(); }; + /** + * Get element handles for changing the position of end + * points of this line. + * @returns {LineHandle[]} line handles for both end points + */ + proxy.handles = function () { + return handles; + }; + return proxy; }