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 | e8dac70 | 2015-10-13 14:33:25 -0600 | [diff] [blame] | 6 | 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 Scott | 918210b | 2015-08-28 13:15:40 -0600 | [diff] [blame] | 18 | 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 Scott | bb42ed2 | 2015-10-21 17:02:56 -0600 | [diff] [blame] | 41 | el.append(['<div class="nodeContent"><a class="treeExplorerExpander"></a><a class="treeSearch" href="#', path, current, '">', current, |
| 42 | '</a></div>'].join("")); |
Tyler Scott | 918210b | 2015-08-28 13:15:40 -0600 | [diff] [blame] | 43 | } else { |
| 44 | el.addClass('file'); |
| 45 | el.text(current); |
| 46 | } |
| 47 | c.append(el); |
| 48 | }); |
| 49 | } |
| 50 | |
Tyler Scott | e8dac70 | 2015-10-13 14:33:25 -0600 | [diff] [blame] | 51 | var scope = this; |
| 52 | |
Tyler Scott | bb42ed2 | 2015-10-21 17:02:56 -0600 | [diff] [blame] | 53 | tree.on('click', '.treeExplorerExpander', function(e){ |
Tyler Scott | e8dac70 | 2015-10-13 14:33:25 -0600 | [diff] [blame] | 54 | if (!scope.settings.autoScroll){ |
| 55 | e.preventDefault(); |
| 56 | } |
| 57 | |
Tyler Scott | b59e6de | 2015-09-18 14:46:30 -0600 | [diff] [blame] | 58 | var node = $(this).parent().parent(); |
Tyler Scott | 918210b | 2015-08-28 13:15:40 -0600 | [diff] [blame] | 59 | |
| 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 | })(); |