2019-11-18 04:06:43 -05:00
|
|
|
/* eslint-disable func-names, consistent-return, no-return-assign */
|
2017-11-01 04:58:41 -04:00
|
|
|
|
|
|
|
import fuzzaldrinPlus from 'fuzzaldrin-plus';
|
2021-02-14 13:09:20 -05:00
|
|
|
import $ from 'jquery';
|
2021-06-20 23:10:27 -04:00
|
|
|
import createFlash from '~/flash';
|
2020-09-23 02:09:45 -04:00
|
|
|
import { sanitize } from '~/lib/dompurify';
|
2018-01-31 17:27:03 -05:00
|
|
|
import axios from '~/lib/utils/axios_utils';
|
2020-10-12 05:08:38 -04:00
|
|
|
import { spriteIcon } from '~/lib/utils/common_utils';
|
2021-02-14 13:09:20 -05:00
|
|
|
import { joinPaths, escapeFileUrl } from '~/lib/utils/url_utility';
|
2018-01-31 17:27:03 -05:00
|
|
|
import { __ } from '~/locale';
|
2016-12-14 00:26:26 -05:00
|
|
|
|
2017-12-13 04:26:44 -05:00
|
|
|
// highlight text(awefwbwgtc -> <b>a</b>wefw<b>b</b>wgt<b>c</b> )
|
2020-12-23 07:10:26 -05:00
|
|
|
const highlighter = function (element, text, matches) {
|
2019-11-18 04:06:43 -05:00
|
|
|
let j = 0;
|
|
|
|
let len = 0;
|
|
|
|
let lastIndex = 0;
|
|
|
|
let matchedChars = [];
|
|
|
|
let matchIndex = matches[j];
|
|
|
|
let unmatched = text.substring(lastIndex, matchIndex);
|
2017-12-13 04:26:44 -05:00
|
|
|
for (j = 0, len = matches.length; j < len; j += 1) {
|
|
|
|
matchIndex = matches[j];
|
|
|
|
unmatched = text.substring(lastIndex, matchIndex);
|
|
|
|
if (unmatched) {
|
|
|
|
if (matchedChars.length) {
|
2018-10-10 03:13:34 -04:00
|
|
|
element.append(matchedChars.join('').bold());
|
2017-12-13 04:26:44 -05:00
|
|
|
}
|
|
|
|
matchedChars = [];
|
|
|
|
element.append(document.createTextNode(unmatched));
|
2016-07-24 16:45:11 -04:00
|
|
|
}
|
2017-12-13 04:26:44 -05:00
|
|
|
matchedChars.push(text[matchIndex]);
|
|
|
|
lastIndex = matchIndex + 1;
|
|
|
|
}
|
|
|
|
if (matchedChars.length) {
|
2018-10-10 03:13:34 -04:00
|
|
|
element.append(matchedChars.join('').bold());
|
2017-12-13 04:26:44 -05:00
|
|
|
}
|
|
|
|
return element.append(document.createTextNode(text.substring(lastIndex)));
|
|
|
|
};
|
|
|
|
|
|
|
|
export default class ProjectFindFile {
|
|
|
|
constructor(element1, options) {
|
|
|
|
this.element = element1;
|
|
|
|
this.options = options;
|
|
|
|
this.goToBlob = this.goToBlob.bind(this);
|
|
|
|
this.goToTree = this.goToTree.bind(this);
|
|
|
|
this.selectRowDown = this.selectRowDown.bind(this);
|
|
|
|
this.selectRowUp = this.selectRowUp.bind(this);
|
|
|
|
this.filePaths = {};
|
2018-10-10 03:13:34 -04:00
|
|
|
this.inputElement = this.element.find('.file-finder-input');
|
2017-12-13 04:26:44 -05:00
|
|
|
// init event
|
|
|
|
this.initEvent();
|
|
|
|
// focus text input box
|
|
|
|
this.inputElement.focus();
|
|
|
|
// load file list
|
|
|
|
this.load(this.options.url);
|
|
|
|
}
|
|
|
|
|
|
|
|
initEvent() {
|
2020-12-08 16:10:06 -05:00
|
|
|
// eslint-disable-next-line @gitlab/no-global-event-off
|
2018-10-10 03:13:34 -04:00
|
|
|
this.inputElement.off('keyup');
|
2020-12-23 19:10:25 -05:00
|
|
|
this.inputElement.on('keyup', (event) => {
|
2019-12-23 04:07:42 -05:00
|
|
|
const target = $(event.target);
|
|
|
|
const value = target.val();
|
|
|
|
const ref = target.data('oldValue');
|
|
|
|
const oldValue = ref != null ? ref : '';
|
|
|
|
if (value !== oldValue) {
|
|
|
|
target.data('oldValue', value);
|
|
|
|
this.findFile();
|
2020-12-23 07:10:26 -05:00
|
|
|
return this.element.find('tr.tree-item').eq(0).addClass('selected').focus();
|
2019-12-23 04:07:42 -05:00
|
|
|
}
|
|
|
|
});
|
2017-12-13 04:26:44 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
findFile() {
|
2019-11-18 04:06:43 -05:00
|
|
|
const searchText = sanitize(this.inputElement.val());
|
|
|
|
const result =
|
2018-10-10 03:13:34 -04:00
|
|
|
searchText.length > 0 ? fuzzaldrinPlus.filter(this.filePaths, searchText) : this.filePaths;
|
2017-12-13 04:26:44 -05:00
|
|
|
return this.renderList(result, searchText);
|
2018-10-10 03:13:34 -04:00
|
|
|
// find file
|
2017-12-13 04:26:44 -05:00
|
|
|
}
|
2016-07-24 16:45:11 -04:00
|
|
|
|
2019-09-18 17:06:34 -04:00
|
|
|
// files paths load
|
2017-12-13 04:26:44 -05:00
|
|
|
load(url) {
|
2018-10-10 03:13:34 -04:00
|
|
|
axios
|
|
|
|
.get(url)
|
2018-01-31 17:27:03 -05:00
|
|
|
.then(({ data }) => {
|
|
|
|
this.element.find('.loading').hide();
|
|
|
|
this.filePaths = data;
|
|
|
|
this.findFile();
|
2020-12-23 07:10:26 -05:00
|
|
|
this.element.find('.files-slider tr.tree-item').eq(0).addClass('selected').focus();
|
2018-01-31 17:27:03 -05:00
|
|
|
})
|
2021-06-20 23:10:27 -04:00
|
|
|
.catch(() =>
|
|
|
|
createFlash({
|
|
|
|
message: __('An error occurred while loading filenames'),
|
|
|
|
}),
|
|
|
|
);
|
2017-12-13 04:26:44 -05:00
|
|
|
}
|
2016-07-24 16:45:11 -04:00
|
|
|
|
2018-02-21 10:17:03 -05:00
|
|
|
// render result
|
2017-12-13 04:26:44 -05:00
|
|
|
renderList(filePaths, searchText) {
|
2019-11-18 04:06:43 -05:00
|
|
|
let i = 0;
|
|
|
|
let len = 0;
|
|
|
|
let matches = [];
|
|
|
|
const results = [];
|
2018-10-10 03:13:34 -04:00
|
|
|
this.element.find('.tree-table > tbody').empty();
|
2018-06-21 10:50:34 -04:00
|
|
|
for (i = 0, len = filePaths.length; i < len; i += 1) {
|
2019-11-18 04:06:43 -05:00
|
|
|
const filePath = filePaths[i];
|
2017-12-13 04:26:44 -05:00
|
|
|
if (i === 20) {
|
|
|
|
break;
|
2016-07-24 16:45:11 -04:00
|
|
|
}
|
2017-12-13 04:26:44 -05:00
|
|
|
if (searchText) {
|
|
|
|
matches = fuzzaldrinPlus.match(filePath, searchText);
|
2016-07-24 16:45:11 -04:00
|
|
|
}
|
2019-12-17 04:07:48 -05:00
|
|
|
const blobItemUrl = joinPaths(this.options.blobUrlTemplate, escapeFileUrl(filePath));
|
2019-11-18 04:06:43 -05:00
|
|
|
const html = ProjectFindFile.makeHtml(filePath, matches, blobItemUrl);
|
2018-10-10 03:13:34 -04:00
|
|
|
results.push(this.element.find('.tree-table > tbody').append(html));
|
2017-12-13 04:26:44 -05:00
|
|
|
}
|
2019-09-23 14:06:14 -04:00
|
|
|
|
|
|
|
this.element.find('.empty-state').toggleClass('hidden', Boolean(results.length));
|
|
|
|
|
2017-12-13 04:26:44 -05:00
|
|
|
return results;
|
|
|
|
}
|
|
|
|
|
|
|
|
// make tbody row html
|
|
|
|
static makeHtml(filePath, matches, blobItemUrl) {
|
2019-11-18 04:06:43 -05:00
|
|
|
const $tr = $(
|
2020-10-12 05:08:38 -04:00
|
|
|
`<tr class='tree-item'><td class='tree-item-file-name link-container'><a>${spriteIcon(
|
|
|
|
'doc-text',
|
|
|
|
's16 vertical-align-middle gl-mr-1',
|
|
|
|
)}<span class='str-truncated'></span></a></td></tr>`,
|
2018-10-10 03:13:34 -04:00
|
|
|
);
|
2017-12-13 04:26:44 -05:00
|
|
|
if (matches) {
|
2018-10-10 03:13:34 -04:00
|
|
|
$tr
|
|
|
|
.find('a')
|
|
|
|
.replaceWith(highlighter($tr.find('a'), filePath, matches).attr('href', blobItemUrl));
|
2017-12-13 04:26:44 -05:00
|
|
|
} else {
|
2018-10-10 03:13:34 -04:00
|
|
|
$tr.find('a').attr('href', blobItemUrl);
|
|
|
|
$tr.find('.str-truncated').text(filePath);
|
2017-12-13 04:26:44 -05:00
|
|
|
}
|
|
|
|
return $tr;
|
|
|
|
}
|
|
|
|
|
|
|
|
selectRow(type) {
|
2019-11-18 04:06:43 -05:00
|
|
|
const rows = this.element.find('.files-slider tr.tree-item');
|
|
|
|
let selectedRow = this.element.find('.files-slider tr.tree-item.selected');
|
|
|
|
let next = selectedRow.prev();
|
2017-12-13 04:26:44 -05:00
|
|
|
if (rows && rows.length > 0) {
|
|
|
|
if (selectedRow && selectedRow.length > 0) {
|
2018-10-10 03:13:34 -04:00
|
|
|
if (type === 'UP') {
|
2017-12-13 04:26:44 -05:00
|
|
|
next = selectedRow.prev();
|
2018-10-10 03:13:34 -04:00
|
|
|
} else if (type === 'DOWN') {
|
2017-12-13 04:26:44 -05:00
|
|
|
next = selectedRow.next();
|
|
|
|
}
|
|
|
|
if (next.length > 0) {
|
2018-10-10 03:13:34 -04:00
|
|
|
selectedRow.removeClass('selected');
|
2017-12-13 04:26:44 -05:00
|
|
|
selectedRow = next;
|
2016-07-24 16:45:11 -04:00
|
|
|
}
|
2017-12-13 04:26:44 -05:00
|
|
|
} else {
|
|
|
|
selectedRow = rows.eq(0);
|
2016-07-24 16:45:11 -04:00
|
|
|
}
|
2018-10-10 03:13:34 -04:00
|
|
|
return selectedRow.addClass('selected').focus();
|
2017-12-13 04:26:44 -05:00
|
|
|
}
|
|
|
|
}
|
2016-07-24 16:45:11 -04:00
|
|
|
|
2017-12-13 04:26:44 -05:00
|
|
|
selectRowUp() {
|
2018-10-10 03:13:34 -04:00
|
|
|
return this.selectRow('UP');
|
2017-12-13 04:26:44 -05:00
|
|
|
}
|
2016-07-24 16:45:11 -04:00
|
|
|
|
2017-12-13 04:26:44 -05:00
|
|
|
selectRowDown() {
|
2018-10-10 03:13:34 -04:00
|
|
|
return this.selectRow('DOWN');
|
2017-12-13 04:26:44 -05:00
|
|
|
}
|
2016-07-24 16:45:11 -04:00
|
|
|
|
2017-12-13 04:26:44 -05:00
|
|
|
goToTree() {
|
2018-10-10 03:13:34 -04:00
|
|
|
return (window.location.href = this.options.treeUrl);
|
2017-12-13 04:26:44 -05:00
|
|
|
}
|
2016-10-17 11:55:34 -04:00
|
|
|
|
2017-12-13 04:26:44 -05:00
|
|
|
goToBlob() {
|
2019-11-18 04:06:43 -05:00
|
|
|
const $link = this.element.find('.tree-item.selected .tree-item-file-name a');
|
2016-10-17 11:55:34 -04:00
|
|
|
|
2017-12-13 04:26:44 -05:00
|
|
|
if ($link.length) {
|
|
|
|
$link.get(0).click();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|