BigW Consortium Gitlab

project_find_file.js 5.94 KB
Newer Older
Fatih Acet committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
(function() {
  var bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; };

  this.ProjectFindFile = (function() {
    var highlighter;

    function ProjectFindFile(element1, options) {
      this.element = element1;
      this.options = options;
      this.goToBlob = bind(this.goToBlob, this);
      this.goToTree = bind(this.goToTree, this);
      this.selectRowDown = bind(this.selectRowDown, this);
      this.selectRowUp = bind(this.selectRowUp, this);
      this.filePaths = {};
      this.inputElement = this.element.find(".file-finder-input");
16
      // init event
Fatih Acet committed
17
      this.initEvent();
18
      // focus text input box
Fatih Acet committed
19
      this.inputElement.focus();
20
      // load file list
Fatih Acet committed
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
      this.load(this.options.url);
    }

    ProjectFindFile.prototype.initEvent = function() {
      this.inputElement.off("keyup");
      this.inputElement.on("keyup", (function(_this) {
        return function(event) {
          var oldValue, ref, target, value;
          target = $(event.target);
          value = target.val();
          oldValue = (ref = target.data("oldValue")) != null ? ref : "";
          if (value !== oldValue) {
            target.data("oldValue", value);
            _this.findFile();
            return _this.element.find("tr.tree-item").eq(0).addClass("selected").focus();
          }
        };
      })(this));
      return this.element.find(".tree-content-holder .tree-table").on("click", function(event) {
        var path;
        if (event.target.nodeName !== "A") {
          path = this.element.find(".tree-item-file-name a", this).attr("href");
          if (path) {
            return location.href = path;
          }
        }
      });
48
    // init event
Fatih Acet committed
49 50 51 52 53 54 55
    };

    ProjectFindFile.prototype.findFile = function() {
      var result, searchText;
      searchText = this.inputElement.val();
      result = searchText.length > 0 ? fuzzaldrinPlus.filter(this.filePaths, searchText) : this.filePaths;
      return this.renderList(result, searchText);
56
    // find file
Fatih Acet committed
57 58
    };

59
    // files pathes load
Fatih Acet committed
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
    ProjectFindFile.prototype.load = function(url) {
      return $.ajax({
        url: url,
        method: "get",
        dataType: "json",
        success: (function(_this) {
          return function(data) {
            _this.element.find(".loading").hide();
            _this.filePaths = data;
            _this.findFile();
            return _this.element.find(".files-slider tr.tree-item").eq(0).addClass("selected").focus();
          };
        })(this)
      });
    };

76
    // render result
Fatih Acet committed
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
    ProjectFindFile.prototype.renderList = function(filePaths, searchText) {
      var blobItemUrl, filePath, html, i, j, len, matches, results;
      this.element.find(".tree-table > tbody").empty();
      results = [];
      for (i = j = 0, len = filePaths.length; j < len; i = ++j) {
        filePath = filePaths[i];
        if (i === 20) {
          break;
        }
        if (searchText) {
          matches = fuzzaldrinPlus.match(filePath, searchText);
        }
        blobItemUrl = this.options.blobUrlTemplate + "/" + filePath;
        html = this.makeHtml(filePath, matches, blobItemUrl);
        results.push(this.element.find(".tree-table > tbody").append(html));
      }
      return results;
    };

96
    // highlight text(awefwbwgtc -> <b>a</b>wefw<b>b</b>wgt<b>c</b> )
Fatih Acet committed
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
    highlighter = function(element, text, matches) {
      var highlightText, j, lastIndex, len, matchIndex, matchedChars, unmatched;
      lastIndex = 0;
      highlightText = "";
      matchedChars = [];
      for (j = 0, len = matches.length; j < len; j++) {
        matchIndex = matches[j];
        unmatched = text.substring(lastIndex, matchIndex);
        if (unmatched) {
          if (matchedChars.length) {
            element.append(matchedChars.join("").bold());
          }
          matchedChars = [];
          element.append(document.createTextNode(unmatched));
        }
        matchedChars.push(text[matchIndex]);
        lastIndex = matchIndex + 1;
      }
      if (matchedChars.length) {
        element.append(matchedChars.join("").bold());
      }
      return element.append(document.createTextNode(text.substring(lastIndex)));
    };

121
    // make tbody row html
Fatih Acet committed
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
    ProjectFindFile.prototype.makeHtml = function(filePath, matches, blobItemUrl) {
      var $tr;
      $tr = $("<tr class='tree-item'><td class='tree-item-file-name'><i class='fa fa-file-text-o fa-fw'></i><span class='str-truncated'><a></a></span></td></tr>");
      if (matches) {
        $tr.find("a").replaceWith(highlighter($tr.find("a"), filePath, matches).attr("href", blobItemUrl));
      } else {
        $tr.find("a").attr("href", blobItemUrl).text(filePath);
      }
      return $tr;
    };

    ProjectFindFile.prototype.selectRow = function(type) {
      var next, rows, selectedRow;
      rows = this.element.find(".files-slider tr.tree-item");
      selectedRow = this.element.find(".files-slider tr.tree-item.selected");
      if (rows && rows.length > 0) {
        if (selectedRow && selectedRow.length > 0) {
          if (type === "UP") {
            next = selectedRow.prev();
          } else if (type === "DOWN") {
            next = selectedRow.next();
          }
          if (next.length > 0) {
            selectedRow.removeClass("selected");
            selectedRow = next;
          }
        } else {
          selectedRow = rows.eq(0);
        }
        return selectedRow.addClass("selected").focus();
      }
    };

    ProjectFindFile.prototype.selectRowUp = function() {
      return this.selectRow("UP");
    };

    ProjectFindFile.prototype.selectRowDown = function() {
      return this.selectRow("DOWN");
    };

    ProjectFindFile.prototype.goToTree = function() {
      return location.href = this.options.treeUrl;
    };

    ProjectFindFile.prototype.goToBlob = function() {
      var path;
      path = this.element.find(".tree-item.selected .tree-item-file-name a").attr("href");
      if (path) {
        return location.href = path;
      }
    };

    return ProjectFindFile;

  })();

}).call(this);