|
281 | 281 | // a pathnode or null;
|
282 | 282 | _self.pathhandler.getNode = function(path, callback) {
|
283 | 283 | _console.log("looking for node at: " + path);
|
| 284 | + |
| 285 | + // If the given path is empty, just return the current pathnode. |
284 | 286 | if(!path) {
|
285 | 287 | return callback(_self.pathhandler.current);
|
286 | 288 | }
|
| 289 | + var parts = getPathParts(path); |
| 290 | + |
| 291 | + // If the first part of path parts isn't empty, the path is a relative path, which can be turned into an |
| 292 | + // *absolutish* path by pre-pending the parts of the current pathnode. |
| 293 | + if(parts[0] !== '') { |
| 294 | + parts = getPathParts(_self.pathhandler.current.path).concat(parts); |
| 295 | + } |
287 | 296 |
|
288 |
| - // `buildAbsolutePath` is called recursively to turn `path` into an absolute path that can be resolved against the |
289 |
| - // github API with `getDir`. |
290 |
| - buildAbsolutePath(path, _self.pathhandler.current, function(absPath) { |
291 |
| - _console.log("path to fetch: " + absPath); |
292 |
| - return getDir(_self.repo.full_name, _self.branch, absPath, callback); |
| 297 | + // At this point the path is *absolutish*, i.e. looks absolute, but all `.` and `..` mentions need to removed and |
| 298 | + // resolved before it is truly absolute. |
| 299 | + var resolved = []; |
| 300 | + _.each(parts, function(x) { |
| 301 | + if(x === '.') { |
| 302 | + return; |
| 303 | + } |
| 304 | + if(x === '..') { |
| 305 | + resolved.pop(); |
| 306 | + } else { |
| 307 | + resolved.push(x); |
| 308 | + } |
293 | 309 | });
|
| 310 | + var absolute = resolved.join('/'); |
| 311 | + _console.log("path to fetch: " + absolute); |
| 312 | + return getDir(_self.repo.full_name, _self.branch, absolute, callback); |
294 | 313 | };
|
295 | 314 |
|
296 | 315 | //<section id='getChildNodes'/>
|
|
348 | 367 | withCredentials: true
|
349 | 368 | }
|
350 | 369 | };
|
351 |
| - $.ajax(request).done(function(response,status,xhr) { |
| 370 | + $.ajax(request).done(function(response, status, xhr) { |
352 | 371 |
|
353 | 372 | // Every response from the API includes rate limiting headers, as well as an indicator injected by the API proxy
|
354 | 373 | // whether the request was done with authentication. Both are used to display request rate information and a
|
|
526 | 545 | });
|
527 | 546 | }
|
528 | 547 |
|
529 |
| - //<section id='buildAbsolutePath'/> |
| 548 | + //<section id='getPathParts'/> |
530 | 549 |
|
531 |
| - // buildAbsolutePath |
532 |
| - // ----------------- |
| 550 | + // getPathParts |
| 551 | + // ------------ |
533 | 552 |
|
534 |
| - // This function resolves a path to an absolute path given a current node. |
535 |
| - function buildAbsolutePath(path, current, callback) { |
536 |
| - _console.log("resolving path: "+path); |
| 553 | + // This function splits a path on `/` and removes any empty trailing element. |
| 554 | + function getPathParts(path) { |
537 | 555 | var parts = path.split("/");
|
538 |
| - |
539 |
| - // If the first part of the path is `..`, `current` is used to determine the parent path and construct an absolute |
540 |
| - // path from the combination of the parent path and the remainder of `path`. Since this compoint path may still |
541 |
| - // contain `.` or `..`, path operators that the github API does not understand, the resulting value is fed back |
542 |
| - // into `buildAbsolutePath`. |
543 |
| - if(parts[0] === '..' ) { |
544 |
| - var parentParts = _.filter(current.path.split("/"), function(x) { |
545 |
| - return x; |
546 |
| - }); |
547 |
| - path = "/" + parentParts.slice(0, parentParts.length - 1).join('/') + "/" + parts.slice(1).join("/"); |
548 |
| - return buildAbsolutePath(path, _self.root, callback); |
549 |
| - } |
550 |
| - |
551 |
| - // If the first parht of the path is either a `.` or not empty (i.e. the path had started with a `/`, the path must |
552 |
| - // be relative and an absolute path can be constructed by combining the path and `current`. Once again, the value |
553 |
| - // isfed back into `buildAbsolutePath` for final resolution. |
554 |
| - if(parts[0] === '.' || parts[0] !== '') { |
555 |
| - path = current.path+"/"+path; |
556 |
| - return buildAbsolutePath(path, _self.root, callback); |
| 556 | + if(parts[parts.length - 1] === '') { |
| 557 | + return parts.slice(0, parts.length - 1); |
557 | 558 | }
|
558 |
| - |
559 |
| - // At this point the path looks absolute, but all `.` and `..` mentions need to removed and resolved before a truly |
560 |
| - // absolute path can be returned. |
561 |
| - var resolved = []; |
562 |
| - _.each(parts, function(x) { |
563 |
| - if(x === '.') { |
564 |
| - return; |
565 |
| - } |
566 |
| - if(x === '..') { |
567 |
| - resolved.pop(); |
568 |
| - } else { |
569 |
| - resolved.push(x); |
570 |
| - } |
571 |
| - }); |
572 |
| - return callback(resolved.join('/')); |
| 559 | + return parts; |
573 | 560 | }
|
574 | 561 |
|
575 | 562 | //<section id='makeNodes'/>
|
576 | 563 |
|
577 | 564 | // makeNodes
|
578 | 565 | // ---------
|
579 | 566 |
|
580 |
| - // This method builds child pathnodes from the directory information returned by getDir. |
| 567 | + // This function builds child pathnodes from the directory information returned by getDir. |
581 | 568 | function makeNodes(children) {
|
582 | 569 | return _.map(children, function(node) {
|
583 | 570 | return {
|
|
651 | 638 | );
|
652 | 639 | });
|
653 | 640 | })(root, $, _);
|
654 |
| -})(this, $, _); |
| 641 | +}) |
| 642 | + (this, $, _); |
0 commit comments