blob: 58a6a69ef3c28e6d2d4046513bc94cf0eecc0963 [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 Scotte8dac702015-10-13 14:33:25 -06006 treeExplorer: function(getChildren, settings){
7
8 this.settings = {
9 autoScroll : false
10 }
11
12 for (var value in settings) {
13 if (this.settings[value] !== undefined){
14 this.settings[value] = settings[value];
15 }
16 }
17
Tyler Scott918210b2015-08-28 13:15:40 -060018 var cache = {}; //Cache previously requested paths.
19
20 var tree = $('<div class="treeExplorer"></div>');
21 this.append(tree);
22
23 var lookup = function(path, callback){
24 if (cache[path]){
25 callback(path, cache[path]);
26 } else {
27 getChildren(path, function(children){
28 cache[path] = children;
29 callback(path, children);
30 });
31 }
32 }
33
34 var append = function(path, children, node){
35 var c = $('<div class="nodeChildren"></div>');
36 node.append(c);
37 children.forEach(function(current){
38 var el = $('<div class="treeExplorerNode"></div>');
39 if (current.match(/\/$/)){
40 el.attr('id', path + current);
Tyler Scottbb42ed22015-10-21 17:02:56 -060041 el.append(['<div class="nodeContent"><a class="treeExplorerExpander"></a><a class="treeSearch" href="#', path, current, '">', current,
42 '</a></div>'].join(""));
Tyler Scott918210b2015-08-28 13:15:40 -060043 } else {
44 el.addClass('file');
45 el.text(current);
46 }
47 c.append(el);
48 });
49 }
50
Tyler Scotte8dac702015-10-13 14:33:25 -060051 var scope = this;
52
Tyler Scottbb42ed22015-10-21 17:02:56 -060053 tree.on('click', '.treeExplorerExpander', function(e){
Tyler Scotte8dac702015-10-13 14:33:25 -060054 if (!scope.settings.autoScroll){
55 e.preventDefault();
56 }
57
Tyler Scottb59e6de2015-09-18 14:46:30 -060058 var node = $(this).parent().parent();
Tyler Scott918210b2015-08-28 13:15:40 -060059
60 if (node.hasClass('open')){ //Are we open already?
61 node.removeClass('open');
62 return;
63 } else { //We need to open
64 if (node.find('.treeExplorerNode').length > 0){ //We already have children
65 node.addClass('open');
66 } else { //We need to get the children.
67 var path = node.attr('id');
68 lookup(path, function(path, children){
69 if (children.length === 0){
70 node.addClass('file');
71 var name = node.find('a').text().replace(/\/$/, "");
72 node.empty().text(name);
73 } else {
74 append(path, children, node);
75 node.addClass('open');
76 }
77 });
78 }
79 }
80
81 });
82
83 getChildren("/", function(children){
84 append("", children, tree);
85 });
86
87 return this;
88
89 }
90 })
91})();