blob: 6b678048d3caa53eb5d0afe50e4d6e120744a1f4 [file] [log] [blame]
Tyler Scott918210b2015-08-28 13:15:40 -06001
2(function(){
3 "use strict";
4 jQuery.fn.extend({
Tyler Scott918210b2015-08-28 13:15:40 -06005
Tyler Scottcdfcde82015-09-14 16:13:29 -06006 treeExplorer: function(getChildren){
Tyler Scott918210b2015-08-28 13:15:40 -06007 var cache = {}; //Cache previously requested paths.
8
9 var tree = $('<div class="treeExplorer"></div>');
10 this.append(tree);
11
12 var lookup = function(path, callback){
13 if (cache[path]){
14 callback(path, cache[path]);
15 } else {
16 getChildren(path, function(children){
17 cache[path] = children;
18 callback(path, children);
19 });
20 }
21 }
22
23 var append = function(path, children, node){
24 var c = $('<div class="nodeChildren"></div>');
25 node.append(c);
26 children.forEach(function(current){
27 var el = $('<div class="treeExplorerNode"></div>');
28 if (current.match(/\/$/)){
29 el.attr('id', path + current);
Tyler Scottb59e6de2015-09-18 14:46:30 -060030 el.append(['<div class="nodeContent"><a href="#', path, current, '">', current,
31 '</a>&nbsp;<button class="treeSearch btn btn-success btn-xs">Search from here</button></div>'].join(""));
Tyler Scott918210b2015-08-28 13:15:40 -060032 } else {
33 el.addClass('file');
34 el.text(current);
35 }
36 c.append(el);
37 });
38 }
39
Tyler Scottb59e6de2015-09-18 14:46:30 -060040 tree.on('click', '.treeExplorerNode > .nodeContent > a', function(){
41 var node = $(this).parent().parent();
Tyler Scott918210b2015-08-28 13:15:40 -060042
43 if (node.hasClass('open')){ //Are we open already?
44 node.removeClass('open');
45 return;
46 } else { //We need to open
47 if (node.find('.treeExplorerNode').length > 0){ //We already have children
48 node.addClass('open');
49 } else { //We need to get the children.
50 var path = node.attr('id');
51 lookup(path, function(path, children){
52 if (children.length === 0){
53 node.addClass('file');
54 var name = node.find('a').text().replace(/\/$/, "");
55 node.empty().text(name);
56 } else {
57 append(path, children, node);
58 node.addClass('open');
59 }
60 });
61 }
62 }
63
64 });
65
66 getChildren("/", function(children){
67 append("", children, tree);
68 });
69
70 return this;
71
72 }
73 })
74})();