blob: 5037629fd436a8913af52d4d188786f55703c9c3 [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);
30 el.append(['<a href="#' , path , current , '">' , current , '</a>'].join(""));
31 } else {
32 el.addClass('file');
33 el.text(current);
34 }
35 c.append(el);
36 });
37 }
38
39 tree.on('click', '.treeExplorerNode > a', function(){
40 var node = $(this).parent();
41
42 if (node.hasClass('open')){ //Are we open already?
43 node.removeClass('open');
44 return;
45 } else { //We need to open
46 if (node.find('.treeExplorerNode').length > 0){ //We already have children
47 node.addClass('open');
48 } else { //We need to get the children.
49 var path = node.attr('id');
50 lookup(path, function(path, children){
51 if (children.length === 0){
52 node.addClass('file');
53 var name = node.find('a').text().replace(/\/$/, "");
54 node.empty().text(name);
55 } else {
56 append(path, children, node);
57 node.addClass('open');
58 }
59 });
60 }
61 }
62
63 });
64
65 getChildren("/", function(children){
66 append("", children, tree);
67 });
68
69 return this;
70
71 }
72 })
73})();