2015-01-16 02:29:31 +00:00
|
|
|
/*global define*/
|
|
|
|
|
|
|
|
define(
|
2015-03-04 23:08:11 +00:00
|
|
|
['./FixedProxy', './elements/ElementProxies', './FixedDragHandle'],
|
|
|
|
function (FixedProxy, ElementProxies, FixedDragHandle) {
|
2015-01-16 02:29:31 +00:00
|
|
|
"use strict";
|
|
|
|
|
2015-01-16 02:36:48 +00:00
|
|
|
var DEFAULT_DIMENSIONS = [ 2, 1 ],
|
2015-01-16 02:48:11 +00:00
|
|
|
DEFAULT_GRID_SIZE = [64, 16],
|
|
|
|
DEFAULT_GRID_EXTENT = [4, 4];
|
2015-01-16 02:29:31 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The FixedController is responsible for supporting the
|
|
|
|
* Fixed Position view. It arranges frames according to saved
|
|
|
|
* configuration and provides methods for updating these based on
|
|
|
|
* mouse movement.
|
|
|
|
* @constructor
|
|
|
|
* @param {Scope} $scope the controller's Angular scope
|
|
|
|
*/
|
2015-02-20 20:21:28 +00:00
|
|
|
function FixedController($scope, $q, dialogService, telemetrySubscriber, telemetryFormatter) {
|
2015-01-16 02:29:31 +00:00
|
|
|
var gridSize = DEFAULT_GRID_SIZE,
|
2015-01-16 02:48:11 +00:00
|
|
|
gridExtent = DEFAULT_GRID_EXTENT,
|
2015-02-19 04:29:54 +00:00
|
|
|
dragging,
|
2015-01-16 03:16:15 +00:00
|
|
|
subscription,
|
2015-01-16 02:48:11 +00:00
|
|
|
cellStyles = [],
|
2015-02-18 21:50:25 +00:00
|
|
|
elementProxies = [],
|
2015-02-24 17:25:14 +00:00
|
|
|
names = {}, // Cache names by ID
|
|
|
|
values = {}, // Cache values by ID
|
2015-02-20 00:24:45 +00:00
|
|
|
elementProxiesById = {},
|
2015-02-24 18:33:36 +00:00
|
|
|
handles = [],
|
2015-02-24 19:29:52 +00:00
|
|
|
moveHandle,
|
2015-02-18 17:03:50 +00:00
|
|
|
selection;
|
2015-01-16 02:29:31 +00:00
|
|
|
|
2015-01-16 02:48:11 +00:00
|
|
|
// Refresh cell styles (e.g. because grid extent changed)
|
|
|
|
function refreshCellStyles() {
|
|
|
|
var x, y;
|
|
|
|
|
2015-02-19 18:22:12 +00:00
|
|
|
// Clear previous styles
|
2015-01-16 02:48:11 +00:00
|
|
|
cellStyles = [];
|
|
|
|
|
2015-02-19 18:22:12 +00:00
|
|
|
// Update grid size from model
|
|
|
|
gridSize = ($scope.model || {}).layoutGrid || gridSize;
|
|
|
|
|
2015-01-16 02:48:11 +00:00
|
|
|
for (x = 0; x < gridExtent[0]; x += 1) {
|
|
|
|
for (y = 0; y < gridExtent[1]; y += 1) {
|
|
|
|
// Position blocks; subtract out border size from w/h
|
|
|
|
cellStyles.push({
|
|
|
|
left: x * gridSize[0] + 'px',
|
|
|
|
top: y * gridSize[1] + 'px',
|
2015-01-16 02:53:59 +00:00
|
|
|
width: gridSize[0] - 1 + 'px',
|
|
|
|
height: gridSize[1] - 1 + 'px'
|
2015-01-16 02:48:11 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-18 21:50:25 +00:00
|
|
|
// Convert from element x/y/width/height to an
|
|
|
|
// apropriate ng-style argument, to position elements.
|
2015-02-20 23:02:16 +00:00
|
|
|
function convertPosition(elementProxy) {
|
2015-01-16 02:29:31 +00:00
|
|
|
// Multiply position/dimensions by grid size
|
|
|
|
return {
|
2015-02-20 23:02:16 +00:00
|
|
|
left: (gridSize[0] * elementProxy.x()) + 'px',
|
|
|
|
top: (gridSize[1] * elementProxy.y()) + 'px',
|
|
|
|
width: (gridSize[0] * elementProxy.width()) + 'px',
|
|
|
|
height: (gridSize[1] * elementProxy.height()) + 'px'
|
2015-01-16 02:29:31 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2015-02-24 19:29:52 +00:00
|
|
|
// Update the style for a selected element
|
|
|
|
function updateSelectionStyle() {
|
|
|
|
var element = selection && selection.get();
|
|
|
|
if (element) {
|
|
|
|
element.style = convertPosition(element);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-24 18:33:36 +00:00
|
|
|
// Generate a specific drag handle
|
|
|
|
function generateDragHandle(elementHandle) {
|
2015-02-24 19:29:52 +00:00
|
|
|
return new FixedDragHandle(
|
|
|
|
elementHandle,
|
|
|
|
gridSize,
|
|
|
|
updateSelectionStyle,
|
|
|
|
$scope.commit
|
|
|
|
);
|
2015-02-24 18:33:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Generate drag handles for an element
|
|
|
|
function generateDragHandles(element) {
|
|
|
|
return element.handles().map(generateDragHandle);
|
|
|
|
}
|
|
|
|
|
2015-02-24 19:22:52 +00:00
|
|
|
// Select an element
|
|
|
|
function select(element) {
|
|
|
|
if (selection) {
|
|
|
|
// Update selection...
|
|
|
|
selection.select(element);
|
2015-02-24 19:29:52 +00:00
|
|
|
// ...as well as move, resize handles
|
|
|
|
moveHandle = generateDragHandle(element);
|
2015-02-24 19:22:52 +00:00
|
|
|
handles = generateDragHandles(element);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-17 16:32:35 +00:00
|
|
|
// Update the displayed value for this object
|
2015-01-16 03:16:15 +00:00
|
|
|
function updateValue(telemetryObject) {
|
|
|
|
var id = telemetryObject && telemetryObject.getId();
|
|
|
|
if (id) {
|
2015-02-20 00:24:45 +00:00
|
|
|
(elementProxiesById[id] || []).forEach(function (element) {
|
2015-02-24 17:25:14 +00:00
|
|
|
names[id] = telemetryObject.getModel().name;
|
|
|
|
values[id] = telemetryFormatter.formatRangeValue(
|
2015-02-18 21:50:25 +00:00
|
|
|
subscription.getRangeValue(telemetryObject)
|
|
|
|
);
|
2015-02-24 17:25:14 +00:00
|
|
|
element.name = names[id];
|
|
|
|
element.value = values[id];
|
2015-02-18 21:50:25 +00:00
|
|
|
});
|
2015-01-16 03:16:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update telemetry values based on new data available
|
|
|
|
function updateValues() {
|
|
|
|
if (subscription) {
|
|
|
|
subscription.getTelemetryObjects().forEach(updateValue);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-18 21:50:25 +00:00
|
|
|
// Decorate an element for display
|
2015-02-19 03:56:54 +00:00
|
|
|
function makeProxyElement(element, index, elements) {
|
2015-02-19 03:41:27 +00:00
|
|
|
var ElementProxy = ElementProxies[element.type],
|
2015-02-19 03:56:54 +00:00
|
|
|
e = ElementProxy && new ElementProxy(element, index, elements);
|
2015-02-19 03:41:27 +00:00
|
|
|
|
|
|
|
if (e) {
|
|
|
|
// Provide a displayable position (convert from grid to px)
|
2015-02-20 23:02:16 +00:00
|
|
|
e.style = convertPosition(e);
|
2015-02-19 03:41:27 +00:00
|
|
|
// Template names are same as type names, presently
|
|
|
|
e.template = element.type;
|
|
|
|
}
|
|
|
|
|
|
|
|
return e;
|
2015-02-18 21:50:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Decorate elements in the current configuration
|
|
|
|
function refreshElements() {
|
2015-02-19 04:12:55 +00:00
|
|
|
// Cache selection; we are instantiating new proxies
|
|
|
|
// so we may want to restore this.
|
|
|
|
var selected = selection && selection.get(),
|
|
|
|
elements = (($scope.configuration || {}).elements || []),
|
|
|
|
index = -1; // Start with a 'not-found' value
|
|
|
|
|
|
|
|
// Find the selection in the new array
|
|
|
|
if (selected !== undefined) {
|
|
|
|
index = elements.indexOf(selected.element);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create the new proxies...
|
|
|
|
elementProxies = elements.map(makeProxyElement);
|
|
|
|
|
|
|
|
// Clear old selection, and restore if appropriate
|
|
|
|
if (selection) {
|
|
|
|
selection.deselect();
|
|
|
|
if (index > -1) {
|
2015-02-24 19:22:52 +00:00
|
|
|
select(elementProxies[index]);
|
2015-02-19 04:12:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-20 00:24:45 +00:00
|
|
|
// Finally, rebuild lists of elements by id to
|
|
|
|
// facilitate faster update when new telemetry comes in.
|
|
|
|
elementProxiesById = {};
|
|
|
|
elementProxies.forEach(function (elementProxy) {
|
|
|
|
var id = elementProxy.id;
|
|
|
|
if (elementProxy.element.type === 'fixed.telemetry') {
|
2015-02-24 17:25:14 +00:00
|
|
|
// Provide it a cached name/value to avoid flashing
|
|
|
|
elementProxy.name = names[id];
|
|
|
|
elementProxy.value = values[id];
|
2015-02-20 00:24:45 +00:00
|
|
|
elementProxiesById[id] = elementProxiesById[id] || [];
|
|
|
|
elementProxiesById[id].push(elementProxy);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2015-02-18 21:50:25 +00:00
|
|
|
// TODO: Ensure elements for all domain objects?
|
|
|
|
}
|
|
|
|
|
2015-02-17 16:35:32 +00:00
|
|
|
// Free up subscription to telemetry
|
|
|
|
function releaseSubscription() {
|
|
|
|
if (subscription) {
|
|
|
|
subscription.unsubscribe();
|
|
|
|
subscription = undefined;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-16 03:16:15 +00:00
|
|
|
// Subscribe to telemetry updates for this domain object
|
|
|
|
function subscribe(domainObject) {
|
|
|
|
// Release existing subscription (if any)
|
|
|
|
if (subscription) {
|
|
|
|
subscription.unsubscribe();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make a new subscription
|
|
|
|
subscription = domainObject &&
|
|
|
|
telemetrySubscriber.subscribe(domainObject, updateValues);
|
|
|
|
}
|
|
|
|
|
2015-02-17 18:34:13 +00:00
|
|
|
// Handle changes in the object's composition
|
|
|
|
function updateComposition(ids) {
|
|
|
|
// Populate panel positions
|
2015-02-18 21:50:25 +00:00
|
|
|
// TODO: Ensure defaults here
|
2015-02-17 18:34:13 +00:00
|
|
|
// Resubscribe - objects in view have changed
|
|
|
|
subscribe($scope.domainObject);
|
|
|
|
}
|
|
|
|
|
2015-02-20 23:47:07 +00:00
|
|
|
// Add an element to this view
|
|
|
|
function addElement(element) {
|
2015-02-17 18:50:02 +00:00
|
|
|
// Ensure that configuration field is populated
|
|
|
|
$scope.configuration = $scope.configuration || {};
|
2015-02-17 18:27:35 +00:00
|
|
|
// Make sure there is a "elements" field in the
|
|
|
|
// view configuration.
|
|
|
|
$scope.configuration.elements =
|
2015-02-18 21:50:25 +00:00
|
|
|
$scope.configuration.elements || [];
|
2015-02-17 18:27:35 +00:00
|
|
|
// Store the position of this element.
|
2015-02-20 23:47:07 +00:00
|
|
|
$scope.configuration.elements.push(element);
|
|
|
|
// Refresh displayed elements
|
|
|
|
refreshElements();
|
|
|
|
// Select the newly-added element
|
2015-02-24 19:22:52 +00:00
|
|
|
select(elementProxies[elementProxies.length - 1]);
|
2015-02-20 23:47:07 +00:00
|
|
|
// Mark change as persistable
|
|
|
|
if ($scope.commit) {
|
|
|
|
$scope.commit("Dropped an element.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Position a panel after a drop event
|
|
|
|
function handleDrop(e, id, position) {
|
|
|
|
// Store the position of this element.
|
|
|
|
addElement({
|
2015-02-18 21:50:25 +00:00
|
|
|
type: "fixed.telemetry",
|
|
|
|
x: Math.floor(position.x / gridSize[0]),
|
|
|
|
y: Math.floor(position.y / gridSize[1]),
|
|
|
|
id: id,
|
2015-02-24 02:05:37 +00:00
|
|
|
stroke: "transparent",
|
2015-03-04 23:08:11 +00:00
|
|
|
color: "#cccccc",
|
2015-02-24 03:25:33 +00:00
|
|
|
titled: true,
|
2015-02-18 21:50:25 +00:00
|
|
|
width: DEFAULT_DIMENSIONS[0],
|
|
|
|
height: DEFAULT_DIMENSIONS[1]
|
|
|
|
});
|
2015-02-17 18:27:35 +00:00
|
|
|
}
|
|
|
|
|
2015-03-04 02:52:13 +00:00
|
|
|
// Track current selection state
|
2015-03-04 02:35:28 +00:00
|
|
|
selection = $scope.selection;
|
2015-02-18 17:03:50 +00:00
|
|
|
|
2015-03-04 02:52:13 +00:00
|
|
|
// Expose the view's selection proxy
|
|
|
|
if (selection) {
|
|
|
|
selection.proxy(new FixedProxy(addElement, $q, dialogService));
|
|
|
|
}
|
|
|
|
|
2015-02-18 21:50:25 +00:00
|
|
|
// Refresh list of elements whenever model changes
|
|
|
|
$scope.$watch("model.modified", refreshElements);
|
|
|
|
|
2015-01-16 02:29:31 +00:00
|
|
|
// Position panes when the model field changes
|
2015-02-17 18:34:13 +00:00
|
|
|
$scope.$watch("model.composition", updateComposition);
|
2015-01-16 02:29:31 +00:00
|
|
|
|
2015-01-16 03:16:15 +00:00
|
|
|
// Subscribe to telemetry when an object is available
|
|
|
|
$scope.$watch("domainObject", subscribe);
|
|
|
|
|
2015-02-17 16:35:32 +00:00
|
|
|
// Free up subscription on destroy
|
|
|
|
$scope.$on("$destroy", releaseSubscription);
|
|
|
|
|
2015-02-17 18:27:35 +00:00
|
|
|
// Position panes where they are dropped
|
|
|
|
$scope.$on("mctDrop", handleDrop);
|
|
|
|
|
2015-02-17 16:32:35 +00:00
|
|
|
// Initialize styles (position etc.) for cells
|
2015-01-16 02:48:11 +00:00
|
|
|
refreshCellStyles();
|
|
|
|
|
2015-01-16 02:29:31 +00:00
|
|
|
return {
|
2015-01-16 02:48:11 +00:00
|
|
|
/**
|
|
|
|
* Get styles for all background cells, as will populate the
|
|
|
|
* ng-style tag.
|
|
|
|
* @memberof FixedController#
|
|
|
|
* @returns {Array} cell styles
|
|
|
|
*/
|
|
|
|
getCellStyles: function () {
|
|
|
|
return cellStyles;
|
|
|
|
},
|
2015-02-20 23:02:16 +00:00
|
|
|
/**
|
|
|
|
* Get the size of the grid, in pixels. The returned array
|
|
|
|
* is in the form `[x, y]`.
|
|
|
|
* @returns {number[]} the grid size
|
|
|
|
*/
|
|
|
|
getGridSize: function () {
|
|
|
|
return gridSize;
|
|
|
|
},
|
2015-01-16 02:53:59 +00:00
|
|
|
/**
|
|
|
|
* Set the size of the viewable fixed position area.
|
|
|
|
* @memberof FixedController#
|
|
|
|
* @param bounds the width/height, as reported by mct-resize
|
|
|
|
*/
|
|
|
|
setBounds: function (bounds) {
|
|
|
|
var w = Math.ceil(bounds.width / gridSize[0]),
|
|
|
|
h = Math.ceil(bounds.height / gridSize[1]);
|
|
|
|
if (w !== gridExtent[0] || h !== gridExtent[1]) {
|
|
|
|
gridExtent = [w, h];
|
|
|
|
refreshCellStyles();
|
|
|
|
}
|
|
|
|
},
|
2015-02-18 23:11:52 +00:00
|
|
|
/**
|
2015-02-20 00:24:45 +00:00
|
|
|
* Get an array of elements in this panel; these are
|
|
|
|
* decorated proxies for both selection and display.
|
2015-02-18 23:11:52 +00:00
|
|
|
* @returns {Array} elements in this panel
|
|
|
|
*/
|
2015-02-20 00:24:45 +00:00
|
|
|
getElements: function () {
|
2015-02-18 23:11:52 +00:00
|
|
|
return elementProxies;
|
|
|
|
},
|
2015-02-19 03:41:27 +00:00
|
|
|
/**
|
2015-02-24 18:33:36 +00:00
|
|
|
* Check if the element is currently selected, or (if no
|
|
|
|
* argument is supplied) get the currently selected element.
|
2015-02-19 03:41:27 +00:00
|
|
|
* @returns {boolean} true if selected
|
|
|
|
*/
|
|
|
|
selected: function (element) {
|
2015-02-24 18:33:36 +00:00
|
|
|
return selection && ((arguments.length > 0) ?
|
|
|
|
selection.selected(element) : selection.get());
|
2015-02-19 03:41:27 +00:00
|
|
|
},
|
2015-02-18 23:11:52 +00:00
|
|
|
/**
|
|
|
|
* Set the active user selection in this view.
|
|
|
|
* @param element the element to select
|
|
|
|
*/
|
2015-02-24 19:22:52 +00:00
|
|
|
select: select,
|
2015-02-18 23:11:52 +00:00
|
|
|
/**
|
|
|
|
* Clear the current user selection.
|
|
|
|
*/
|
|
|
|
clearSelection: function () {
|
2015-02-19 03:51:42 +00:00
|
|
|
if (selection) {
|
|
|
|
selection.deselect();
|
2015-02-24 18:33:36 +00:00
|
|
|
handles = [];
|
2015-02-24 19:29:52 +00:00
|
|
|
moveHandle = undefined;
|
2015-02-19 03:51:42 +00:00
|
|
|
}
|
2015-02-18 23:11:52 +00:00
|
|
|
},
|
2015-01-16 02:29:31 +00:00
|
|
|
/**
|
2015-02-24 18:33:36 +00:00
|
|
|
* Get drag handles.
|
|
|
|
* @returns {Array} drag handles for the current selection
|
2015-01-16 02:29:31 +00:00
|
|
|
*/
|
2015-02-24 18:33:36 +00:00
|
|
|
handles: function () {
|
|
|
|
return handles;
|
2015-01-16 02:29:31 +00:00
|
|
|
},
|
|
|
|
/**
|
2015-02-24 19:29:52 +00:00
|
|
|
* Get the handle to handle dragging to reposition an element.
|
|
|
|
* @returns {FixedDragHandle} the drag handle
|
2015-01-16 02:29:31 +00:00
|
|
|
*/
|
2015-02-24 19:29:52 +00:00
|
|
|
moveHandle: function () {
|
|
|
|
return moveHandle;
|
2015-01-16 02:29:31 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return FixedController;
|
|
|
|
}
|
|
|
|
);
|