Path: blob/master/webroot/rsrc/js/application/diff/DiffTreeView.js
12242 views
/**1* @provides phabricator-diff-tree-view2* @requires javelin-dom3* @javelin4*/56JX.install('DiffTreeView', {78construct: function() {9this._keys = [];10this._tree = this._newTreeNode(null, [], 0);11this._nodes = {};12this._paths = [];13},1415members: {16_node: null,17_keys: null,18_tree: null,19_nodes: null,20_dirty: false,21_paths: null,22_selectedPath: null,23_focusedPath: null,2425getNode: function() {26if (!this._node) {27var attrs = {28className: 'diff-tree-view'29};3031this._node = JX.$N('ul', attrs);32}3334if (this._dirty) {35this.redraw();36}3738return this._node;39},4041addPath: function(path) {42this._paths.push(path);4344var tree = this._getTree(this._tree, path.getPath(), 0);45tree.pathObject = path;4647this._dirty = true;4849return this;50},5152getPaths: function() {53return this._paths;54},5556setSelectedPath: function(path) {57if (this._selectedPath) {58this._selectedPath.setIsSelected(false);59this._selectedPath = null;60}6162if (path) {63path.setIsSelected(true);64}6566this._selectedPath = path;6768return this;69},7071setFocusedPath: function(path) {72if (this._focusedPath) {73this._focusedPath.setIsFocused(false);74this._focusedPath = null;75}7677if (path) {78path.setIsFocused(true);79}8081this._focusedPath = path;8283return this;84},8586redraw: function() {87if (!this._dirty) {88return;89}90this._dirty = false;9192var ii;9394// For nodes which don't have a path object yet, build one.95var tree;96var path;97var trees = [];98for (ii = 0; ii < this._keys.length; ii++) {99var key = this._keys[ii];100tree = this._nodes[key];101path = tree.pathObject;102103if (!path) {104path = new JX.DiffPathView()105.setPath(tree.parts);106107path.getIcon()108.setIcon('fa-folder-open-o')109.setColor('grey');110111tree.pathObject = path;112}113114trees.push(tree);115}116117for (ii = 0; ii < trees.length; ii++) {118tree = trees[ii];119tree.displayRoot = null;120tree.displayPath = null;121tree.displayHide = false;122}123124var child;125for (ii = 0; ii < trees.length; ii++) {126tree = trees[ii];127128if (tree.childCount !== 1) {129continue;130}131132for (var k in tree.children) {133if (tree.children.hasOwnProperty(k)) {134child = tree.children[k];135break;136}137}138139if (child.pathObject.getChangeset()) {140continue;141}142143child.displayRoot = tree.displayRoot || tree;144}145146for (ii = 0; ii < trees.length; ii++) {147tree = trees[ii];148149if (!tree.displayRoot) {150continue;151}152153if (!tree.displayRoot.displayPath) {154tree.displayRoot.displayPath = [155tree.displayRoot.parts[tree.displayRoot.parts.length - 1]156];157}158159tree.displayRoot.displayPath.push(tree.parts[tree.parts.length - 1]);160tree.displayHide = true;161}162163for (ii = 0; ii < trees.length; ii++) {164tree = trees[ii];165path = tree.pathObject;166167path.setHidden(!!tree.displayHide);168169if (tree.displayPath) {170path.setDisplayPath(tree.displayPath.join('/'));171} else {172path.setDisplayPath(null);173}174}175176for (ii = 0; ii < trees.length; ii++) {177tree = trees[ii];178179if (!tree.parent) {180tree.depth = 0;181} else {182// If this node was collapsed into the parent node, don't increase183// the tree depth.184if (tree.displayHide) {185tree.depth = tree.parent.depth;186} else {187tree.depth = tree.parent.depth + 1;188}189}190191path = tree.pathObject;192193if (tree.childCount > 0) {194path.setIsDirectory(true);195}196197path.setDepth((tree.depth - 1));198}199200var nodes = [];201for (ii = 0; ii < trees.length; ii++) {202tree = trees[ii];203nodes.push(tree.pathObject.getNode());204}205206JX.DOM.setContent(this.getNode(), nodes);207},208209_getTree: function(root, path, ii) {210if (ii >= path.length) {211return root;212}213214var part = path[ii];215216if (!root.children.hasOwnProperty(part)) {217root.children[part] = this._newTreeNode(root, path, ii);218root.childCount++;219}220221return this._getTree(root.children[part], path, ii + 1);222},223224_newTreeNode: function(parent, path, ii) {225var key;226var parts;227if (path.length) {228parts = path.slice(0, ii + 1);229key = parts.join('/');230this._keys.push(key);231} else {232parts = [];233key = null;234}235236var node = {237parent: parent,238nodeKey: key,239parts: parts,240children: {},241pathObject: null,242childCount: 0,243depth: 0244};245246if (key !== null) {247this._nodes[key] = node;248}249250return node;251}252253}254255});256257258