Tyler Scott | 918210b | 2015-08-28 13:15:40 -0600 | [diff] [blame] | 1 | |
| 2 | (function(){ |
| 3 | "use strict"; |
| 4 | jQuery.fn.extend({ |
Tyler Scott | 918210b | 2015-08-28 13:15:40 -0600 | [diff] [blame] | 5 | |
Tyler Scott | cdfcde8 | 2015-09-14 16:13:29 -0600 | [diff] [blame] | 6 | treeExplorer: function(getChildren){ |
Tyler Scott | 918210b | 2015-08-28 13:15:40 -0600 | [diff] [blame] | 7 | 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 Scott | b59e6de | 2015-09-18 14:46:30 -0600 | [diff] [blame] | 30 | el.append(['<div class="nodeContent"><a href="#', path, current, '">', current, |
| 31 | '</a> <button class="treeSearch btn btn-success btn-xs">Search from here</button></div>'].join("")); |
Tyler Scott | 918210b | 2015-08-28 13:15:40 -0600 | [diff] [blame] | 32 | } else { |
| 33 | el.addClass('file'); |
| 34 | el.text(current); |
| 35 | } |
| 36 | c.append(el); |
| 37 | }); |
| 38 | } |
| 39 | |
Tyler Scott | b59e6de | 2015-09-18 14:46:30 -0600 | [diff] [blame] | 40 | tree.on('click', '.treeExplorerNode > .nodeContent > a', function(){ |
| 41 | var node = $(this).parent().parent(); |
Tyler Scott | 918210b | 2015-08-28 13:15:40 -0600 | [diff] [blame] | 42 | |
| 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 | })(); |