140 lines
4.4 KiB
JavaScript
Raw Normal View History

2016-03-11 11:12:43 -08:00
/*****************************************************************************
* Open MCT, Copyright (c) 2014-2017, United States Government
2016-03-11 11:12:43 -08:00
* as represented by the Administrator of the National Aeronautics and Space
* Administration. All rights reserved.
*
* Open MCT is licensed under the Apache License, Version 2.0 (the
2016-03-11 11:12:43 -08:00
* "License"); you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0.
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
* Open MCT includes source code licensed under additional open source
2016-03-11 11:12:43 -08:00
* licenses. See the Open Source Licenses file (LICENSES.md) included with
* this source code distribution or the Licensing information page available
* at runtime from the About dialog for additional information.
*****************************************************************************/
define([
2016-03-11 14:40:04 -08:00
'zepto',
2016-03-11 18:00:58 -08:00
'./TreeNodeView',
'text!../../res/templates/tree/wait-node.html'
], function ($, TreeNodeView, spinnerTemplate) {
2016-03-11 11:12:43 -08:00
2016-03-11 17:43:18 -08:00
function TreeView(gestureService, selectFn) {
this.ul = $('<ul class="tree"></ul>');
2016-03-11 11:12:43 -08:00
this.nodeViews = [];
2016-03-11 17:00:55 -08:00
this.callbacks = [];
2016-03-11 17:32:57 -08:00
this.selectFn = selectFn || this.value.bind(this);
2016-03-11 17:43:18 -08:00
this.gestureService = gestureService;
2016-03-11 18:00:58 -08:00
this.pending = false;
2016-03-11 11:12:43 -08:00
}
2016-03-11 17:32:57 -08:00
TreeView.prototype.newTreeView = function () {
2016-03-11 17:43:18 -08:00
return new TreeView(this.gestureService, this.selectFn);
};
2016-03-11 11:12:43 -08:00
TreeView.prototype.setSize = function (sz) {
var nodeView;
while (this.nodeViews.length < sz) {
2016-03-11 17:32:57 -08:00
nodeView = new TreeNodeView(
2016-03-11 17:43:18 -08:00
this.gestureService,
2016-03-11 17:32:57 -08:00
this.newTreeView.bind(this),
this.selectFn
);
2016-03-11 11:12:43 -08:00
this.nodeViews.push(nodeView);
this.ul.append($(nodeView.elements()));
}
while (this.nodeViews.length > sz) {
nodeView = this.nodeViews.pop();
$(nodeView.elements()).remove();
}
};
TreeView.prototype.loadComposition = function () {
var self = this,
domainObject = this.activeObject;
2016-03-11 11:12:43 -08:00
function addNode(domainObj, index) {
self.nodeViews[index].model(domainObj);
2016-03-11 11:12:43 -08:00
}
function addNodes(domainObjects) {
2016-03-11 18:00:58 -08:00
if (self.pending) {
self.pending = false;
self.nodeViews = [];
self.ul.empty();
}
2016-03-11 11:12:43 -08:00
if (domainObject === self.activeObject) {
self.setSize(domainObjects.length);
domainObjects.forEach(addNode);
2016-03-11 17:00:55 -08:00
self.updateNodeViewSelection();
2016-03-11 11:12:43 -08:00
}
}
domainObject.useCapability('composition')
.then(addNodes);
};
TreeView.prototype.model = function (domainObject) {
if (this.unlisten) {
this.unlisten();
}
this.activeObject = domainObject;
this.ul.empty();
if (domainObject && domainObject.hasCapability('composition')) {
2016-03-11 18:00:58 -08:00
this.pending = true;
this.ul.append($(spinnerTemplate));
2016-03-11 11:12:43 -08:00
this.unlisten = domainObject.getCapability('mutation')
.listen(this.loadComposition.bind(this));
this.loadComposition(domainObject);
} else {
this.setSize(0);
}
};
2016-03-11 17:00:55 -08:00
TreeView.prototype.updateNodeViewSelection = function () {
this.nodeViews.forEach(function (nodeView) {
nodeView.value(this.selectedObject);
2016-03-11 17:26:00 -08:00
}.bind(this));
2016-03-11 17:00:55 -08:00
};
TreeView.prototype.value = function (domainObject, event) {
2016-03-11 17:00:55 -08:00
this.selectedObject = domainObject;
this.updateNodeViewSelection();
this.callbacks.forEach(function (callback) {
callback(domainObject, event);
2016-03-11 17:00:55 -08:00
});
};
2016-03-11 17:09:17 -08:00
TreeView.prototype.observe = function (callback) {
this.callbacks.push(callback);
return function () {
this.callbacks = this.callbacks.filter(function (c) {
return c !== callback;
});
}.bind(this);
};
2016-03-11 11:12:43 -08:00
/**
*
* @returns {HTMLElement[]}
*/
TreeView.prototype.elements = function () {
return this.ul;
};
return TreeView;
2016-03-11 17:00:55 -08:00
});