/*globals define, WebGMEGlobal*/ /*jshint browser: true*/ define([ 'js/Constants', 'js/Utils/GMEConcepts', 'js/NodePropertyNames' ], function ( CONSTANTS, GMEConcepts, nodePropertyNames ) { 'use strict'; var LineGraphControl; LineGraphControl = function (options) { this._logger = options.logger.fork('Control'); this._client = options.client; this._embedded = options.embedded; // Initialize core collections and variables this._widget = options.widget; this._currentNodeId = null; this._currentNodeParentId = undefined; this._logger.debug('ctor finished'); }; /* * * * * * * * Visualizer content update callbacks * * * * * * * */ // One major concept here is with managing the territory. The territory // defines the parts of the project that the visualizer is interested in // (this allows the browser to then only load those relevant parts). LineGraphControl.prototype.selectedObjectChanged = function (nodeId) { var desc = this._getObjectDescriptor(nodeId), self = this; self._logger.debug('activeObject nodeId \'' + nodeId + '\''); // Remove current territory patterns if (self._currentNodeId) { self._client.removeUI(self._territoryId); } self._currentNodeId = nodeId; self._currentNodeParentId = undefined; if (typeof self._currentNodeId === 'string') { // Put new node's info into territory rules self._selfPatterns = {}; self._selfPatterns[nodeId] = {children: 0}; // Territory "rule" self._widget.setTitle(desc.name.toUpperCase()); self._currentNodeParentId = desc.parentId; self._territoryId = self._client.addUI(self, function (events) { self._eventCallback(events); }); // Update the territory self._client.updateTerritory(self._territoryId, self._selfPatterns); self._selfPatterns[nodeId] = {children: 1}; self._client.updateTerritory(self._territoryId, self._selfPatterns); } }; // This next function retrieves the relevant node information for the widget LineGraphControl.prototype._getObjectDescriptor = function (nodeId) { var node = this._client.getNode(nodeId), desc; if (node) { desc = { id: node.getId(), name: node.getAttribute(nodePropertyNames.Attributes.name) }; // Check if it is a line if (desc.id !== this._currentNodeId) { var points = (node.getAttribute('points') || '').split(';') .map(pair => { var nums = pair.split(','), x = +nums[0], y = +nums[1]; return { x: x, y:y }; }); desc.type = 'line'; desc.points = points; } } return desc; }; /* * * * * * * * Node Event Handling * * * * * * * */ LineGraphControl.prototype._eventCallback = function (events) { var i = events ? events.length : 0, event; this._logger.debug('_eventCallback \'' + i + '\' items'); while (i--) { event = events[i]; switch (event.etype) { case CONSTANTS.TERRITORY_EVENT_LOAD: this._onLoad(event.eid); break; case CONSTANTS.TERRITORY_EVENT_UPDATE: this._onUpdate(event.eid); break; case CONSTANTS.TERRITORY_EVENT_UNLOAD: this._onUnload(event.eid); break; default: break; } } this._logger.debug('_eventCallback \'' + events.length + '\' items - DONE'); }; LineGraphControl.prototype._onLoad = function (gmeId) { var description = this._getObjectDescriptor(gmeId); this._widget.addNode(description); }; LineGraphControl.prototype._onUpdate = function (gmeId) { var description = this._getObjectDescriptor(gmeId); this._widget.updateNode(description); }; LineGraphControl.prototype._onUnload = function (gmeId) { this._widget.removeNode(gmeId); }; LineGraphControl.prototype._stateActiveObjectChanged = function (model, activeObjectId) { if (this._currentNodeId === activeObjectId) { // The same node selected as before - do not trigger } else { this.selectedObjectChanged(activeObjectId); } }; /* * * * * * * * Visualizer life cycle callbacks * * * * * * * */ LineGraphControl.prototype.destroy = function () { this._detachClientEventListeners(); }; LineGraphControl.prototype._attachClientEventListeners = function () { this._detachClientEventListeners(); WebGMEGlobal.State.on('change:' + CONSTANTS.STATE_ACTIVE_OBJECT, this._stateActiveObjectChanged, this); }; LineGraphControl.prototype._detachClientEventListeners = function () { WebGMEGlobal.State.off('change:' + CONSTANTS.STATE_ACTIVE_OBJECT, this._stateActiveObjectChanged); }; LineGraphControl.prototype.onActivate = function () { this._attachClientEventListeners(); if (typeof this._currentNodeId === 'string') { WebGMEGlobal.State.registerSuppressVisualizerFromNode(true); WebGMEGlobal.State.registerActiveObject(this._currentNodeId); WebGMEGlobal.State.registerSuppressVisualizerFromNode(false); } }; LineGraphControl.prototype.onDeactivate = function () { this._detachClientEventListeners(); }; return LineGraphControl; });