|
| 1 | +(function($, _, document, window, localStorage) { |
| 2 | + GitHubShell = function(config) { |
| 3 | + config = config || {}; |
| 4 | + var shellConfig = {}; |
| 5 | + if(config.history) { |
| 6 | + shellConfig.history = config.history; |
| 7 | + } |
| 8 | + if(config.shell_view_id) { |
| 9 | + shellConfig.shell_view_id = config.shell_view_id; |
| 10 | + } else { |
| 11 | + config.shell_view_id |
| 12 | + } |
| 13 | + var _shell = new Shell(shellConfig); |
| 14 | + var _commandList = _.sortBy(['go', 'ls', 'cd', 'show', 'pwd', 'help','clear'], function(x) { |
| 15 | + return x; |
| 16 | + }); |
| 17 | + var _namespaces = { |
| 18 | + 0: "", |
| 19 | + 2: "User:", |
| 20 | + 10: "Template:", |
| 21 | + 101: "Special:" |
| 22 | + }; |
| 23 | + var _home = null; |
| 24 | + var _current = null; |
| 25 | + var _localState = { |
| 26 | + active: false |
| 27 | + }; |
| 28 | + // public methods |
| 29 | + var self = { |
| 30 | + activate: function() { |
| 31 | + _shell.activate(); |
| 32 | + }, |
| 33 | + deactivate: function() { |
| 34 | + _shell.deactivate(); |
| 35 | + }, |
| 36 | + onDeactivate: function(completionHandler) { |
| 37 | + _shell.onDeactivate(completionHandler); |
| 38 | + } |
| 39 | + }; |
| 40 | + |
| 41 | + // private methods |
| 42 | + function completionHandler(line, callback) { |
| 43 | + var partial = line.text.substr(0, line.cursor); |
| 44 | + var parts = split(partial); |
| 45 | + console.log(parts); |
| 46 | + if(parts.length == 0) { |
| 47 | + return callback({suggestions: _commandList}); |
| 48 | + } |
| 49 | + if(parts.length == 1) { |
| 50 | + var cmd = parts[0]; |
| 51 | + if(startsWith(cmd, ".") || startsWith(cmd, "/")) { |
| 52 | + return completePath(cmd, callback); |
| 53 | + } |
| 54 | + return callback(bestMatch(cmd, _commandList)); |
| 55 | + } |
| 56 | + return completePath(parts[1], callback); |
| 57 | + } |
| 58 | + |
| 59 | + function completePath(path, callback) { |
| 60 | + var uri = "/@api/deki/console/matches?path=" + encodeURIComponent(path); |
| 61 | + if(_current) { |
| 62 | + uri = uri + "¤t=" + _current.Id; |
| 63 | + } |
| 64 | + $.getJSON(uri, function(data) { |
| 65 | + console.log(data); |
| 66 | + callback(bestMatch(path, data.Matches)); |
| 67 | + }); |
| 68 | + } |
| 69 | + |
| 70 | + function cmdHandler(cmdtext, input_id, callback) { |
| 71 | + var parts = split(cmdtext); |
| 72 | + var cmd = parts[0]; |
| 73 | + var path = parts[1]; |
| 74 | + if(startsWith(cmd, ".") || startsWith(cmd, "/") || startsWith(cmd, "#")) { |
| 75 | + path = cmd; |
| 76 | + cmd = "show"; |
| 77 | + } |
| 78 | + switch(cmd) { |
| 79 | + case "clear": |
| 80 | + $(input_id).siblings().remove(); |
| 81 | + callback(); |
| 82 | + return; |
| 83 | + case "help": |
| 84 | + var content = $('<div><div><strong>Commands:</strong></div></div>'); |
| 85 | + _.each(_commandList, function(command) { |
| 86 | + content.append(_.template('<div> <%=command%></div>', {command: command})) |
| 87 | + }); |
| 88 | + $(input_id).after(content); |
| 89 | + callback(); |
| 90 | + return; |
| 91 | + case "go": |
| 92 | + getPage(path, function(page) { |
| 93 | + var path = getPath(page); |
| 94 | + console.log("navigating to " + path); |
| 95 | + window.location = path; |
| 96 | + }); |
| 97 | + return; |
| 98 | + case"ls": |
| 99 | + ls(input_id, path, callback); |
| 100 | + return; |
| 101 | + case"pwd": |
| 102 | + $(input_id).after(getPath(_current)); |
| 103 | + callback(); |
| 104 | + return; |
| 105 | + case "cd": |
| 106 | + getPage(path, function(page) { |
| 107 | + callback(setCurrent(page)); |
| 108 | + }); |
| 109 | + return; |
| 110 | + case "show": |
| 111 | + getPage(path, function(page) { |
| 112 | + show(input_id, page, callback); |
| 113 | + }); |
| 114 | + return; |
| 115 | + default: |
| 116 | + var content = _.template('<div><strong>Unrecognized command: </strong><%=cmd%></div>', {cmd: cmd}); |
| 117 | + $(input_id).after(content); |
| 118 | + callback(); |
| 119 | + return; |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + function setCurrent(page) { |
| 124 | + _current = page; |
| 125 | + return getPath(_current) + " $"; |
| 126 | + } |
| 127 | + |
| 128 | + function getPath(page) { |
| 129 | + return " /" + _namespaces[page.Namespace] + page.Path; |
| 130 | + } |
| 131 | + |
| 132 | + function getSegment(page) { |
| 133 | + var segments = page.Path.split('/'); |
| 134 | + return segments[segments.length - 1]; |
| 135 | + } |
| 136 | + |
| 137 | + function ls(input_id, path, callback) { |
| 138 | + getPage(path, function(page) { |
| 139 | + getChildren(page, function(pages) { |
| 140 | + var p = _.map(pages, function(page) { |
| 141 | + var lsInfo = { id: page.Id, segment: getSegment(page), name: page.DisplayName}; |
| 142 | + lsInfo.name = lsInfo.name || lsInfo.segment; |
| 143 | + return lsInfo; |
| 144 | + }); |
| 145 | + console.log(p); |
| 146 | + $(input_id).after(_.template("<% _.each(p, function(page) { %><div>[<%=page.id%>] <%=page.segment%> (<%=page.name%>)</div> <% }); %>", {p: p})); |
| 147 | + callback(); |
| 148 | + }); |
| 149 | + }); |
| 150 | + } |
| 151 | + |
| 152 | + function getChildren(page, callback) { |
| 153 | + $.getJSON("/@api/deki/console/node/" + page.Id + "/children", function(children) { |
| 154 | + callback(children); |
| 155 | + }); |
| 156 | + } |
| 157 | + |
| 158 | + function show(input_id, page, callback) { |
| 159 | + var content = $( |
| 160 | + '<div>' + |
| 161 | + '<div><strong>Id: </strong><span class="id"></span></div>' + |
| 162 | + '<div><strong>Path: </strong><span class="path"></span></div>' + |
| 163 | + '<div><strong>Displayname: </strong><span class="display"></span></div>' + |
| 164 | + '<div><strong>Parent Id: </strong><span class="parentid"></span></div>' + |
| 165 | + '<div><strong>Child Ids: </strong><span class="childids"></span></div>' + |
| 166 | + '</div>' |
| 167 | + ); |
| 168 | + $(content).find('.id').text(page.Id); |
| 169 | + $(content).find('.path').text(" /" + _namespaces[page.Namespace] + page.Path); |
| 170 | + $(content).find('.display').text(page.DisplayName); |
| 171 | + $(content).find('.parentid').text(page.ParentId); |
| 172 | + $(content).find('.childids').text("[" + page.ChildIds + "]"); |
| 173 | + $(input_id).after(content); |
| 174 | + callback(); |
| 175 | + } |
| 176 | + |
| 177 | + function split(str) { |
| 178 | + var parts = str.split(/\s+/); |
| 179 | + if(parts.length > 0 && !parts[parts.length - 1]) { |
| 180 | + parts.pop(); |
| 181 | + } |
| 182 | + return parts; |
| 183 | + } |
| 184 | + |
| 185 | + function startsWith(str1, str2) { |
| 186 | + return str1.slice(0, str2.length) == str2; |
| 187 | + } |
| 188 | + |
| 189 | + function bestMatch(partial, possible) { |
| 190 | + var completions = []; |
| 191 | + var common = ''; |
| 192 | + for(var i = 0; i < possible.length; i++) { |
| 193 | + var option = possible[i]; |
| 194 | + if(option.slice(0, partial.length) == partial) { |
| 195 | + completions.push(option); |
| 196 | + if(!common) { |
| 197 | + common = option; |
| 198 | + console.log("initial common:" + common); |
| 199 | + } else if(option.slice(0, common.length) != common) { |
| 200 | + console.log("find common stem for '" + common + "' and '" + option + "'"); |
| 201 | + var j = partial.length; |
| 202 | + while(j < common.length && j < option.length) { |
| 203 | + if(common[j] != option[j]) { |
| 204 | + common = common.substr(0, j); |
| 205 | + break; |
| 206 | + } |
| 207 | + j++; |
| 208 | + } |
| 209 | + } |
| 210 | + } |
| 211 | + } |
| 212 | + if(completions.length == 0) { |
| 213 | + return; |
| 214 | + } |
| 215 | + if(completions.length == 1) { |
| 216 | + completions = null; |
| 217 | + } |
| 218 | + return { |
| 219 | + result: common.substr(partial.length), |
| 220 | + suggestions: completions |
| 221 | + }; |
| 222 | + } |
| 223 | + |
| 224 | + |
| 225 | + _shell.onCompletion(completionHandler); |
| 226 | + _shell.onCmd(cmdHandler); |
| 227 | + |
| 228 | + return self; |
| 229 | + }; |
| 230 | + |
| 231 | +}) |
| 232 | + (jQuery, _, document, window, localStorage); |
0 commit comments