Commit 8b090caf authored by Mike Greiling's avatar Mike Greiling

Prettify environments feature_highlight and filtered_search modules

parent 4d0db16f
<script> <script>
import tablePagination from '../../vue_shared/components/table_pagination.vue'; import tablePagination from '../../vue_shared/components/table_pagination.vue';
import environmentTable from '../components/environments_table.vue'; import environmentTable from '../components/environments_table.vue';
export default { export default {
components: { components: {
environmentTable, environmentTable,
tablePagination, tablePagination,
...@@ -34,7 +34,7 @@ ...@@ -34,7 +34,7 @@
this.$emit('onChangePage', page); this.$emit('onChangePage', page);
}, },
}, },
}; };
</script> </script>
<template> <template>
......
<script> <script>
export default { export default {
name: 'EnvironmentsEmptyState', name: 'EnvironmentsEmptyState',
props: { props: {
newPath: { newPath: {
...@@ -15,7 +15,7 @@ ...@@ -15,7 +15,7 @@
required: true, required: true,
}, },
}, },
}; };
</script> </script>
<template> <template>
<div class="blank-state-row"> <div class="blank-state-row">
......
...@@ -38,7 +38,9 @@ export default { ...@@ -38,7 +38,9 @@ export default {
computed: { computed: {
title() { title() {
return this.isLastDeployment ? s__('Environments|Re-deploy to environment') : s__('Environments|Rollback environment'); return this.isLastDeployment
? s__('Environments|Re-deploy to environment')
: s__('Environments|Rollback environment');
}, },
}, },
......
...@@ -5,7 +5,8 @@ import Translate from '../../vue_shared/translate'; ...@@ -5,7 +5,8 @@ import Translate from '../../vue_shared/translate';
Vue.use(Translate); Vue.use(Translate);
export default () => new Vue({ export default () =>
new Vue({
el: '#environments-folder-list-view', el: '#environments-folder-list-view',
components: { components: {
environmentsFolderApp, environmentsFolderApp,
...@@ -32,4 +33,4 @@ export default () => new Vue({ ...@@ -32,4 +33,4 @@ export default () => new Vue({
}, },
}); });
}, },
}); });
<script> <script>
import environmentsMixin from '../mixins/environments_mixin'; import environmentsMixin from '../mixins/environments_mixin';
import CIPaginationMixin from '../../vue_shared/mixins/ci_pagination_api_mixin'; import CIPaginationMixin from '../../vue_shared/mixins/ci_pagination_api_mixin';
import StopEnvironmentModal from '../components/stop_environment_modal.vue'; import StopEnvironmentModal from '../components/stop_environment_modal.vue';
export default { export default {
components: { components: {
StopEnvironmentModal, StopEnvironmentModal,
}, },
mixins: [ mixins: [environmentsMixin, CIPaginationMixin],
environmentsMixin,
CIPaginationMixin,
],
props: { props: {
endpoint: { endpoint: {
...@@ -40,7 +37,7 @@ ...@@ -40,7 +37,7 @@
this.saveData(resp); this.saveData(resp);
}, },
}, },
}; };
</script> </script>
<template> <template>
<div :class="cssContainerClass"> <div :class="cssContainerClass">
......
...@@ -5,7 +5,8 @@ import Translate from '../vue_shared/translate'; ...@@ -5,7 +5,8 @@ import Translate from '../vue_shared/translate';
Vue.use(Translate); Vue.use(Translate);
export default () => new Vue({ export default () =>
new Vue({
el: '#environments-list-view', el: '#environments-list-view',
components: { components: {
environmentsComponent, environmentsComponent,
...@@ -36,4 +37,4 @@ export default () => new Vue({ ...@@ -36,4 +37,4 @@ export default () => new Vue({
}, },
}); });
}, },
}); });
...@@ -4,9 +4,7 @@ ...@@ -4,9 +4,7 @@
import _ from 'underscore'; import _ from 'underscore';
import Visibility from 'visibilityjs'; import Visibility from 'visibilityjs';
import Poll from '../../lib/utils/poll'; import Poll from '../../lib/utils/poll';
import { import { getParameterByName } from '../../lib/utils/common_utils';
getParameterByName,
} from '../../lib/utils/common_utils';
import { s__ } from '../../locale'; import { s__ } from '../../locale';
import Flash from '../../flash'; import Flash from '../../flash';
import eventHub from '../event_hub'; import eventHub from '../event_hub';
...@@ -19,7 +17,6 @@ import tabs from '../../vue_shared/components/navigation_tabs.vue'; ...@@ -19,7 +17,6 @@ import tabs from '../../vue_shared/components/navigation_tabs.vue';
import container from '../components/container.vue'; import container from '../components/container.vue';
export default { export default {
components: { components: {
environmentTable, environmentTable,
container, container,
...@@ -65,7 +62,8 @@ export default { ...@@ -65,7 +62,8 @@ export default {
updateContent(parameters) { updateContent(parameters) {
this.updateInternalState(parameters); this.updateInternalState(parameters);
// fetch new data // fetch new data
return this.service.fetchEnvironments(this.requestData) return this.service
.fetchEnvironments(this.requestData)
.then(response => this.successCallback(response)) .then(response => this.successCallback(response))
.then(() => { .then(() => {
// restart polling // restart polling
...@@ -88,7 +86,8 @@ export default { ...@@ -88,7 +86,8 @@ export default {
if (!this.isMakingRequest) { if (!this.isMakingRequest) {
this.isLoading = true; this.isLoading = true;
this.service.postAction(endpoint) this.service
.postAction(endpoint)
.then(() => this.fetchEnvironments()) .then(() => this.fetchEnvironments())
.catch(() => { .catch(() => {
this.isLoading = false; this.isLoading = false;
...@@ -100,7 +99,8 @@ export default { ...@@ -100,7 +99,8 @@ export default {
fetchEnvironments() { fetchEnvironments() {
this.isLoading = true; this.isLoading = true;
return this.service.fetchEnvironments(this.requestData) return this.service
.fetchEnvironments(this.requestData)
.then(this.successCallback) .then(this.successCallback)
.catch(this.errorCallback); .catch(this.errorCallback);
}, },
...@@ -111,7 +111,9 @@ export default { ...@@ -111,7 +111,9 @@ export default {
stopEnvironment(environment) { stopEnvironment(environment) {
const endpoint = environment.stop_path; const endpoint = environment.stop_path;
const errorMessage = s__('Environments|An error occurred while stopping the environment, please try again'); const errorMessage = s__(
'Environments|An error occurred while stopping the environment, please try again',
);
this.postAction({ endpoint, errorMessage }); this.postAction({ endpoint, errorMessage });
}, },
}, },
...@@ -149,7 +151,7 @@ export default { ...@@ -149,7 +151,7 @@ export default {
data: this.requestData, data: this.requestData,
successCallback: this.successCallback, successCallback: this.successCallback,
errorCallback: this.errorCallback, errorCallback: this.errorCallback,
notificationCallback: (isMakingRequest) => { notificationCallback: isMakingRequest => {
this.isMakingRequest = isMakingRequest; this.isMakingRequest = isMakingRequest;
}, },
}); });
......
import $ from 'jquery'; import $ from 'jquery';
import { import { getSelector, inserted } from './feature_highlight_helper';
getSelector, import { togglePopover, mouseenter, debouncedMouseleave } from '../shared/popover';
inserted,
} from './feature_highlight_helper';
import {
togglePopover,
mouseenter,
debouncedMouseleave,
} from '../shared/popover';
export function setupFeatureHighlightPopover(id, debounceTimeout = 300) { export function setupFeatureHighlightPopover(id, debounceTimeout = 300) {
const $selector = $(getSelector(id)); const $selector = $(getSelector(id));
...@@ -41,8 +34,9 @@ export function setupFeatureHighlightPopover(id, debounceTimeout = 300) { ...@@ -41,8 +34,9 @@ export function setupFeatureHighlightPopover(id, debounceTimeout = 300) {
export function findHighestPriorityFeature() { export function findHighestPriorityFeature() {
let priorityFeature; let priorityFeature;
const sortedFeatureEls = [].slice.call(document.querySelectorAll('.js-feature-highlight')).sort((a, b) => const sortedFeatureEls = [].slice
(a.dataset.highlightPriority || 0) < (b.dataset.highlightPriority || 0)); .call(document.querySelectorAll('.js-feature-highlight'))
.sort((a, b) => (a.dataset.highlightPriority || 0) < (b.dataset.highlightPriority || 0));
const [priorityFeatureEl] = sortedFeatureEls; const [priorityFeatureEl] = sortedFeatureEls;
if (priorityFeatureEl) { if (priorityFeatureEl) {
......
...@@ -8,10 +8,17 @@ import { togglePopover } from '../shared/popover'; ...@@ -8,10 +8,17 @@ import { togglePopover } from '../shared/popover';
export const getSelector = highlightId => `.js-feature-highlight[data-highlight=${highlightId}]`; export const getSelector = highlightId => `.js-feature-highlight[data-highlight=${highlightId}]`;
export function dismiss(highlightId) { export function dismiss(highlightId) {
axios.post(this.attr('data-dismiss-endpoint'), { axios
.post(this.attr('data-dismiss-endpoint'), {
feature_name: highlightId, feature_name: highlightId,
}) })
.catch(() => Flash(__('An error occurred while dismissing the feature highlight. Refresh the page and try dismissing again.'))); .catch(() =>
Flash(
__(
'An error occurred while dismissing the feature highlight. Refresh the page and try dismissing again.',
),
),
);
togglePopover.call(this, false); togglePopover.call(this, false);
this.hide(); this.hide();
...@@ -23,8 +30,7 @@ export function inserted() { ...@@ -23,8 +30,7 @@ export function inserted() {
const $popover = $(this); const $popover = $(this);
const dismissWrapper = dismiss.bind($popover, highlightId); const dismissWrapper = dismiss.bind($popover, highlightId);
$(`#${popoverId} .dismiss-feature-highlight`) $(`#${popoverId} .dismiss-feature-highlight`).on('click', dismissWrapper);
.on('click', dismissWrapper);
const lazyImg = $(`#${popoverId} .feature-highlight-illustration`)[0]; const lazyImg = $(`#${popoverId} .feature-highlight-illustration`)[0];
if (lazyImg) { if (lazyImg) {
......
import FilteredSearchTokenKeys from './filtered_search_token_keys'; import FilteredSearchTokenKeys from './filtered_search_token_keys';
const tokenKeys = [{ const tokenKeys = [
{
key: 'status', key: 'status',
type: 'string', type: 'string',
param: 'status', param: 'status',
symbol: '', symbol: '',
icon: 'messages', icon: 'messages',
tag: 'status', tag: 'status',
}, { },
{
key: 'type', key: 'type',
type: 'string', type: 'string',
param: 'type', param: 'type',
symbol: '', symbol: '',
icon: 'cube', icon: 'cube',
tag: 'type', tag: 'type',
}]; },
];
const AdminRunnersFilteredSearchTokenKeys = new FilteredSearchTokenKeys(tokenKeys); const AdminRunnersFilteredSearchTokenKeys = new FilteredSearchTokenKeys(tokenKeys);
......
...@@ -21,9 +21,11 @@ export default { ...@@ -21,9 +21,11 @@ export default {
}, },
computed: { computed: {
processedItems() { processedItems() {
return this.items.map((item) => { return this.items.map(item => {
const { tokens, searchToken } const { tokens, searchToken } = FilteredSearchTokenizer.processTokens(
= FilteredSearchTokenizer.processTokens(item, this.allowedKeys); item,
this.allowedKeys,
);
const resultantTokens = tokens.map(token => ({ const resultantTokens = tokens.map(token => ({
prefix: `${token.key}:`, prefix: `${token.key}:`,
......
...@@ -24,8 +24,12 @@ export default class DropdownEmoji extends FilteredSearchDropdown { ...@@ -24,8 +24,12 @@ export default class DropdownEmoji extends FilteredSearchDropdown {
}; };
import(/* webpackChunkName: 'emoji' */ '~/emoji') import(/* webpackChunkName: 'emoji' */ '~/emoji')
.then(({ glEmojiTag }) => { this.glEmojiTag = glEmojiTag; }) .then(({ glEmojiTag }) => {
.catch(() => { /* ignore error and leave emoji name in the search bar */ }); this.glEmojiTag = glEmojiTag;
})
.catch(() => {
/* ignore error and leave emoji name in the search bar */
});
this.unbindEvents(); this.unbindEvents();
this.bindEvents(); this.bindEvents();
...@@ -48,7 +52,7 @@ export default class DropdownEmoji extends FilteredSearchDropdown { ...@@ -48,7 +52,7 @@ export default class DropdownEmoji extends FilteredSearchDropdown {
} }
itemClicked(e) { itemClicked(e) {
super.itemClicked(e, (selected) => { super.itemClicked(e, selected => {
const name = selected.querySelector('.js-data-value').innerText.trim(); const name = selected.querySelector('.js-data-value').innerText.trim();
return DropdownUtils.getEscapedText(name); return DropdownUtils.getEscapedText(name);
}); });
...@@ -64,7 +68,7 @@ export default class DropdownEmoji extends FilteredSearchDropdown { ...@@ -64,7 +68,7 @@ export default class DropdownEmoji extends FilteredSearchDropdown {
// Replace empty gl-emoji tag to real content // Replace empty gl-emoji tag to real content
const dropdownItems = [...this.dropdown.querySelectorAll('.filter-dropdown-item')]; const dropdownItems = [...this.dropdown.querySelectorAll('.filter-dropdown-item')];
dropdownItems.forEach((dropdownItem) => { dropdownItems.forEach(dropdownItem => {
const name = dropdownItem.querySelector('.js-data-value').innerText; const name = dropdownItem.querySelector('.js-data-value').innerText;
const emojiTag = this.glEmojiTag(name); const emojiTag = this.glEmojiTag(name);
const emojiElement = dropdownItem.querySelector('gl-emoji'); const emojiElement = dropdownItem.querySelector('gl-emoji');
...@@ -73,7 +77,6 @@ export default class DropdownEmoji extends FilteredSearchDropdown { ...@@ -73,7 +77,6 @@ export default class DropdownEmoji extends FilteredSearchDropdown {
} }
init() { init() {
this.droplab this.droplab.addHook(this.input, this.dropdown, [Ajax, Filter], this.config).init();
.addHook(this.input, this.dropdown, [Ajax, Filter], this.config).init();
} }
} }
...@@ -41,8 +41,10 @@ export default class DropdownHint extends FilteredSearchDropdown { ...@@ -41,8 +41,10 @@ export default class DropdownHint extends FilteredSearchDropdown {
previousInputValues.forEach((value, index) => { previousInputValues.forEach((value, index) => {
searchTerms.push(value); searchTerms.push(value);
if (index === previousInputValues.length - 1 if (
&& token.indexOf(value.toLowerCase()) !== -1) { index === previousInputValues.length - 1 &&
token.indexOf(value.toLowerCase()) !== -1
) {
searchTerms.pop(); searchTerms.pop();
} }
}); });
...@@ -64,8 +66,7 @@ export default class DropdownHint extends FilteredSearchDropdown { ...@@ -64,8 +66,7 @@ export default class DropdownHint extends FilteredSearchDropdown {
} }
renderContent() { renderContent() {
const dropdownData = this.tokenKeys.get() const dropdownData = this.tokenKeys.get().map(tokenKey => ({
.map(tokenKey => ({
icon: `${gon.sprite_icons}#${tokenKey.icon}`, icon: `${gon.sprite_icons}#${tokenKey.icon}`,
hint: tokenKey.key, hint: tokenKey.key,
tag: `:${tokenKey.tag}`, tag: `:${tokenKey.tag}`,
......
...@@ -29,20 +29,18 @@ export default class DropdownNonUser extends FilteredSearchDropdown { ...@@ -29,20 +29,18 @@ export default class DropdownNonUser extends FilteredSearchDropdown {
} }
itemClicked(e) { itemClicked(e) {
super.itemClicked(e, (selected) => { super.itemClicked(e, selected => {
const title = selected.querySelector('.js-data-value').innerText.trim(); const title = selected.querySelector('.js-data-value').innerText.trim();
return `${this.symbol}${DropdownUtils.getEscapedText(title)}`; return `${this.symbol}${DropdownUtils.getEscapedText(title)}`;
}); });
} }
renderContent(forceShowList = false) { renderContent(forceShowList = false) {
this.droplab this.droplab.changeHookList(this.hookId, this.dropdown, [Ajax, Filter], this.config);
.changeHookList(this.hookId, this.dropdown, [Ajax, Filter], this.config);
super.renderContent(forceShowList); super.renderContent(forceShowList);
} }
init() { init() {
this.droplab this.droplab.addHook(this.input, this.dropdown, [Ajax, Filter], this.config).init();
.addHook(this.input, this.dropdown, [Ajax, Filter], this.config).init();
} }
} }
...@@ -41,7 +41,7 @@ export default class DropdownUtils { ...@@ -41,7 +41,7 @@ export default class DropdownUtils {
// Removes the first character if it is a quotation so that we can search // Removes the first character if it is a quotation so that we can search
// with multiple words // with multiple words
if ((value[0] === '"' || value[0] === '\'') && title.indexOf(' ') !== -1) { if ((value[0] === '"' || value[0] === "'") && title.indexOf(' ') !== -1) {
value = value.slice(1); value = value.slice(1);
} }
...@@ -82,11 +82,13 @@ export default class DropdownUtils { ...@@ -82,11 +82,13 @@ export default class DropdownUtils {
// Reduce the colors to 4 // Reduce the colors to 4
colors.length = Math.min(colors.length, 4); colors.length = Math.min(colors.length, 4);
const color = colors.map((c, i) => { const color = colors
.map((c, i) => {
const percentFirst = Math.floor(spacing * i); const percentFirst = Math.floor(spacing * i);
const percentSecond = Math.floor(spacing * (i + 1)); const percentSecond = Math.floor(spacing * (i + 1));
return `${c} ${percentFirst}%, ${c} ${percentSecond}%`; return `${c} ${percentFirst}%, ${c} ${percentSecond}%`;
}).join(', '); })
.join(', ');
return `linear-gradient(${color})`; return `linear-gradient(${color})`;
} }
...@@ -97,8 +99,7 @@ export default class DropdownUtils { ...@@ -97,8 +99,7 @@ export default class DropdownUtils {
data.forEach(DropdownUtils.mergeDuplicateLabels.bind(null, dataMap)); data.forEach(DropdownUtils.mergeDuplicateLabels.bind(null, dataMap));
Object.keys(dataMap) Object.keys(dataMap).forEach(key => {
.forEach((key) => {
const label = dataMap[key]; const label = dataMap[key];
if (label.multipleColors) { if (label.multipleColors) {
...@@ -118,8 +119,7 @@ export default class DropdownUtils { ...@@ -118,8 +119,7 @@ export default class DropdownUtils {
const { input, allowedKeys } = config; const { input, allowedKeys } = config;
const updatedItem = item; const updatedItem = item;
const searchInput = DropdownUtils.getSearchQuery(input); const searchInput = DropdownUtils.getSearchQuery(input);
const { lastToken, tokens } = const { lastToken, tokens } = FilteredSearchTokenizer.processTokens(searchInput, allowedKeys);
FilteredSearchTokenizer.processTokens(searchInput, allowedKeys);
const lastKey = lastToken.key || lastToken || ''; const lastKey = lastToken.key || lastToken || '';
const allowMultiple = item.type === 'array'; const allowMultiple = item.type === 'array';
const itemInExistingTokens = tokens.some(t => t.key === item.hint); const itemInExistingTokens = tokens.some(t => t.key === item.hint);
...@@ -154,7 +154,10 @@ export default class DropdownUtils { ...@@ -154,7 +154,10 @@ export default class DropdownUtils {
static getVisualTokenValues(visualToken) { static getVisualTokenValues(visualToken) {
const tokenName = visualToken && visualToken.querySelector('.name').textContent.trim(); const tokenName = visualToken && visualToken.querySelector('.name').textContent.trim();
let tokenValue = visualToken && visualToken.querySelector('.value') && visualToken.querySelector('.value').textContent.trim(); let tokenValue =
visualToken &&
visualToken.querySelector('.value') &&
visualToken.querySelector('.value').textContent.trim();
if (tokenName === 'label' && tokenValue) { if (tokenName === 'label' && tokenValue) {
// remove leading symbol and wrapping quotes // remove leading symbol and wrapping quotes
tokenValue = tokenValue.replace(/^~("|')?(.*)/, '$2').replace(/("|')$/, ''); tokenValue = tokenValue.replace(/^~("|')?(.*)/, '$2').replace(/("|')$/, '');
...@@ -174,7 +177,7 @@ export default class DropdownUtils { ...@@ -174,7 +177,7 @@ export default class DropdownUtils {
tokens.splice(inputIndex + 1); tokens.splice(inputIndex + 1);
} }
tokens.forEach((token) => { tokens.forEach(token => {
if (token.classList.contains('js-visual-token')) { if (token.classList.contains('js-visual-token')) {
const name = token.querySelector('.name'); const name = token.querySelector('.name');
const value = token.querySelector('.value'); const value = token.querySelector('.value');
...@@ -194,8 +197,9 @@ export default class DropdownUtils { ...@@ -194,8 +197,9 @@ export default class DropdownUtils {
values.push(name.innerText); values.push(name.innerText);
} }
} else if (token.classList.contains('input-token')) { } else if (token.classList.contains('input-token')) {
const { isLastVisualTokenValid } = const {
FilteredSearchVisualTokens.getLastVisualTokenBeforeInput(); isLastVisualTokenValid,
} = FilteredSearchVisualTokens.getLastVisualTokenBeforeInput();
const input = FilteredSearchContainer.container.querySelector('.filtered-search'); const input = FilteredSearchContainer.container.querySelector('.filtered-search');
const inputValue = input && input.value; const inputValue = input && input.value;
...@@ -209,9 +213,7 @@ export default class DropdownUtils { ...@@ -209,9 +213,7 @@ export default class DropdownUtils {
} }
}); });
return values return values.map(value => value.trim()).join(' ');
.map(value => value.trim())
.join(' ');
} }
static getSearchInput(filteredSearchInput) { static getSearchInput(filteredSearchInput) {
...@@ -227,7 +229,9 @@ export default class DropdownUtils { ...@@ -227,7 +229,9 @@ export default class DropdownUtils {
// Replace all spaces inside quote marks with underscores // Replace all spaces inside quote marks with underscores
// (will continue to match entire string until an end quote is found if any) // (will continue to match entire string until an end quote is found if any)
// This helps with matching the beginning & end of a token:key // This helps with matching the beginning & end of a token:key
inputValue = inputValue.replace(/(('[^']*'{0,1})|("[^"]*"{0,1})|:\s+)/g, str => str.replace(/\s/g, '_')); inputValue = inputValue.replace(/(('[^']*'{0,1})|("[^"]*"{0,1})|:\s+)/g, str =>
str.replace(/\s/g, '_'),
);
// Get the right position for the word selected // Get the right position for the word selected
// Regex matches first space // Regex matches first space
......
...@@ -87,10 +87,12 @@ export default class FilteredSearchDropdown { ...@@ -87,10 +87,12 @@ export default class FilteredSearchDropdown {
dispatchInputEvent() { dispatchInputEvent() {
// Propogate input change to FilteredSearchDropdownManager // Propogate input change to FilteredSearchDropdownManager
// so that it can determine which dropdowns to open // so that it can determine which dropdowns to open
this.input.dispatchEvent(new CustomEvent('input', { this.input.dispatchEvent(
new CustomEvent('input', {
bubbles: true, bubbles: true,
cancelable: true, cancelable: true,
})); }),
);
} }
dispatchFormSubmitEvent() { dispatchFormSubmitEvent() {
...@@ -114,7 +116,7 @@ export default class FilteredSearchDropdown { ...@@ -114,7 +116,7 @@ export default class FilteredSearchDropdown {
if (!data) return; if (!data) return;
const results = data.map((o) => { const results = data.map(o => {
const updated = o; const updated = o;
updated.droplab_hidden = false; updated.droplab_hidden = false;
return updated; return updated;
......
...@@ -42,7 +42,8 @@ export default class FilteredSearchTokenKeys { ...@@ -42,7 +42,8 @@ export default class FilteredSearchTokenKeys {
} }
searchByKeyParam(keyParam) { searchByKeyParam(keyParam) {
return this.tokenKeysWithAlternative.find((tokenKey) => { return (
this.tokenKeysWithAlternative.find(tokenKey => {
let tokenKeyParam = tokenKey.key; let tokenKeyParam = tokenKey.key;
// Replace hyphen with underscore to compare keyParam with tokenKeyParam // Replace hyphen with underscore to compare keyParam with tokenKeyParam
...@@ -54,7 +55,8 @@ export default class FilteredSearchTokenKeys { ...@@ -54,7 +55,8 @@ export default class FilteredSearchTokenKeys {
} }
return keyParam === tokenKeyParam; return keyParam === tokenKeyParam;
}) || null; }) || null
);
} }
searchByConditionUrl(url) { searchByConditionUrl(url) {
...@@ -62,8 +64,10 @@ export default class FilteredSearchTokenKeys { ...@@ -62,8 +64,10 @@ export default class FilteredSearchTokenKeys {
} }
searchByConditionKeyValue(key, value) { searchByConditionKeyValue(key, value) {
return this.conditions return (
.find(condition => condition.tokenKey === key && condition.value === value) || null; this.conditions.find(condition => condition.tokenKey === key && condition.value === value) ||
null
);
} }
addExtraTokensForMergeRequests() { addExtraTokensForMergeRequests() {
......
...@@ -4,11 +4,16 @@ export default class FilteredSearchTokenizer { ...@@ -4,11 +4,16 @@ export default class FilteredSearchTokenizer {
static processTokens(input, allowedKeys) { static processTokens(input, allowedKeys) {
// Regex extracts `(token):(symbol)(value)` // Regex extracts `(token):(symbol)(value)`
// Values that start with a double quote must end in a double quote (same for single) // Values that start with a double quote must end in a double quote (same for single)
const tokenRegex = new RegExp(`(${allowedKeys.join('|')}):([~%@]?)(?:('[^']*'{0,1})|("[^"]*"{0,1})|(\\S+))`, 'g'); const tokenRegex = new RegExp(
`(${allowedKeys.join('|')}):([~%@]?)(?:('[^']*'{0,1})|("[^"]*"{0,1})|(\\S+))`,
'g',
);
const tokens = []; const tokens = [];
const tokenIndexes = []; // stores key+value for simple search const tokenIndexes = []; // stores key+value for simple search
let lastToken = null; let lastToken = null;
const searchToken = input.replace(tokenRegex, (match, key, symbol, v1, v2, v3) => { const searchToken =
input
.replace(tokenRegex, (match, key, symbol, v1, v2, v3) => {
let tokenValue = v1 || v2 || v3; let tokenValue = v1 || v2 || v3;
let tokenSymbol = symbol; let tokenSymbol = symbol;
let tokenIndex = ''; let tokenIndex = '';
...@@ -32,13 +37,15 @@ export default class FilteredSearchTokenizer { ...@@ -32,13 +37,15 @@ export default class FilteredSearchTokenizer {
} }
return ''; return '';
}).replace(/\s{2,}/g, ' ').trim() || ''; })
.replace(/\s{2,}/g, ' ')
.trim() || '';
if (tokens.length > 0) { if (tokens.length > 0) {
const last = tokens[tokens.length - 1]; const last = tokens[tokens.length - 1];
const lastString = `${last.key}:${last.symbol}${last.value}`; const lastString = `${last.key}:${last.symbol}${last.value}`;
lastToken = input.lastIndexOf(lastString) === lastToken =
input.length - lastString.length ? last : searchToken; input.lastIndexOf(lastString) === input.length - lastString.length ? last : searchToken;
} else { } else {
lastToken = searchToken; lastToken = searchToken;
} }
......
...@@ -13,7 +13,10 @@ export default class FilteredSearchVisualTokens { ...@@ -13,7 +13,10 @@ export default class FilteredSearchVisualTokens {
return { return {
lastVisualToken, lastVisualToken,
isLastVisualTokenValid: lastVisualToken === null || lastVisualToken.className.indexOf('filtered-search-term') !== -1 || (lastVisualToken && lastVisualToken.querySelector('.value') !== null), isLastVisualTokenValid:
lastVisualToken === null ||
lastVisualToken.className.indexOf('filtered-search-term') !== -1 ||
(lastVisualToken && lastVisualToken.querySelector('.value') !== null),
}; };
} }
...@@ -33,7 +36,9 @@ export default class FilteredSearchVisualTokens { ...@@ -33,7 +36,9 @@ export default class FilteredSearchVisualTokens {
} }
static unselectTokens() { static unselectTokens() {
const otherTokens = FilteredSearchContainer.container.querySelectorAll('.js-visual-token .selectable.selected'); const otherTokens = FilteredSearchContainer.container.querySelectorAll(
'.js-visual-token .selectable.selected',
);
[].forEach.call(otherTokens, t => t.classList.remove('selected')); [].forEach.call(otherTokens, t => t.classList.remove('selected'));
} }
...@@ -56,11 +61,7 @@ export default class FilteredSearchVisualTokens { ...@@ -56,11 +61,7 @@ export default class FilteredSearchVisualTokens {
} }
static createVisualTokenElementHTML(options = {}) { static createVisualTokenElementHTML(options = {}) {
const { const { canEdit = true, uppercaseTokenName = false, capitalizeTokenValue = false } = options;
canEdit = true,
uppercaseTokenName = false,
capitalizeTokenValue = false,
} = options;
return ` return `
<div class="${canEdit ? 'selectable' : 'hidden'}" role="button"> <div class="${canEdit ? 'selectable' : 'hidden'}" role="button">
...@@ -115,15 +116,20 @@ export default class FilteredSearchVisualTokens { ...@@ -115,15 +116,20 @@ export default class FilteredSearchVisualTokens {
return AjaxCache.retrieve(labelsEndpoint) return AjaxCache.retrieve(labelsEndpoint)
.then(FilteredSearchVisualTokens.preprocessLabel.bind(null, labelsEndpoint)) .then(FilteredSearchVisualTokens.preprocessLabel.bind(null, labelsEndpoint))
.then((labels) => { .then(labels => {
const matchingLabel = (labels || []).find(label => `~${DropdownUtils.getEscapedText(label.title)}` === tokenValue); const matchingLabel = (labels || []).find(
label => `~${DropdownUtils.getEscapedText(label.title)}` === tokenValue,
);
if (!matchingLabel) { if (!matchingLabel) {
return; return;
} }
FilteredSearchVisualTokens FilteredSearchVisualTokens.setTokenStyle(
.setTokenStyle(tokenValueContainer, matchingLabel.color, matchingLabel.text_color); tokenValueContainer,
matchingLabel.color,
matchingLabel.text_color,
);
}) })
.catch(() => new Flash('An error occurred while fetching label colors.')); .catch(() => new Flash('An error occurred while fetching label colors.'));
} }
...@@ -134,8 +140,9 @@ export default class FilteredSearchVisualTokens { ...@@ -134,8 +140,9 @@ export default class FilteredSearchVisualTokens {
} }
const username = tokenValue.replace(/^@/, ''); const username = tokenValue.replace(/^@/, '');
return UsersCache.retrieve(username) return (
.then((user) => { UsersCache.retrieve(username)
.then(user => {
if (!user) { if (!user) {
return; return;
} }
...@@ -149,15 +156,17 @@ export default class FilteredSearchVisualTokens { ...@@ -149,15 +156,17 @@ export default class FilteredSearchVisualTokens {
/* eslint-enable no-param-reassign */ /* eslint-enable no-param-reassign */
}) })
// ignore error and leave username in the search bar // ignore error and leave username in the search bar
.catch(() => { }); .catch(() => {})
);
} }
static updateEmojiTokenAppearance(tokenValueContainer, tokenValueElement, tokenValue) { static updateEmojiTokenAppearance(tokenValueContainer, tokenValueElement, tokenValue) {
const container = tokenValueContainer; const container = tokenValueContainer;
const element = tokenValueElement; const element = tokenValueElement;
return import(/* webpackChunkName: 'emoji' */ '../emoji') return (
.then((Emoji) => { import(/* webpackChunkName: 'emoji' */ '../emoji')
.then(Emoji => {
if (!Emoji.isEmojiNameValid(tokenValue)) { if (!Emoji.isEmojiNameValid(tokenValue)) {
return; return;
} }
...@@ -166,7 +175,8 @@ export default class FilteredSearchVisualTokens { ...@@ -166,7 +175,8 @@ export default class FilteredSearchVisualTokens {
element.innerHTML = Emoji.glEmojiTag(tokenValue); element.innerHTML = Emoji.glEmojiTag(tokenValue);
}) })
// ignore error and leave emoji name in the search bar // ignore error and leave emoji name in the search bar
.catch(() => { }); .catch(() => {})
);
} }
static renderVisualTokenValue(parentElement, tokenName, tokenValue) { static renderVisualTokenValue(parentElement, tokenName, tokenValue) {
...@@ -177,24 +187,23 @@ export default class FilteredSearchVisualTokens { ...@@ -177,24 +187,23 @@ export default class FilteredSearchVisualTokens {
const tokenType = tokenName.toLowerCase(); const tokenType = tokenName.toLowerCase();
if (tokenType === 'label') { if (tokenType === 'label') {
FilteredSearchVisualTokens.updateLabelTokenColor(tokenValueContainer, tokenValue); FilteredSearchVisualTokens.updateLabelTokenColor(tokenValueContainer, tokenValue);
} else if ((tokenType === 'author') || (tokenType === 'assignee')) { } else if (tokenType === 'author' || tokenType === 'assignee') {
FilteredSearchVisualTokens.updateUserTokenAppearance( FilteredSearchVisualTokens.updateUserTokenAppearance(
tokenValueContainer, tokenValueElement, tokenValue, tokenValueContainer,
tokenValueElement,
tokenValue,
); );
} else if (tokenType === 'my-reaction') { } else if (tokenType === 'my-reaction') {
FilteredSearchVisualTokens.updateEmojiTokenAppearance( FilteredSearchVisualTokens.updateEmojiTokenAppearance(
tokenValueContainer, tokenValueElement, tokenValue, tokenValueContainer,
tokenValueElement,
tokenValue,
); );
} }
} }
static addVisualTokenElement(name, value, options = {}) { static addVisualTokenElement(name, value, options = {}) {
const { const { isSearchTerm = false, canEdit, uppercaseTokenName, capitalizeTokenValue } = options;
isSearchTerm = false,
canEdit,
uppercaseTokenName,
capitalizeTokenValue,
} = options;
const li = document.createElement('li'); const li = document.createElement('li');
li.classList.add('js-visual-token'); li.classList.add('js-visual-token');
li.classList.add(isSearchTerm ? 'filtered-search-term' : 'filtered-search-token'); li.classList.add(isSearchTerm ? 'filtered-search-term' : 'filtered-search-token');
...@@ -217,8 +226,10 @@ export default class FilteredSearchVisualTokens { ...@@ -217,8 +226,10 @@ export default class FilteredSearchVisualTokens {
} }
static addValueToPreviousVisualTokenElement(value) { static addValueToPreviousVisualTokenElement(value) {
const { lastVisualToken, isLastVisualTokenValid } = const {
FilteredSearchVisualTokens.getLastVisualTokenBeforeInput(); lastVisualToken,
isLastVisualTokenValid,
} = FilteredSearchVisualTokens.getLastVisualTokenBeforeInput();
if (!isLastVisualTokenValid && lastVisualToken.classList.contains('filtered-search-token')) { if (!isLastVisualTokenValid && lastVisualToken.classList.contains('filtered-search-token')) {
const name = FilteredSearchVisualTokens.getLastTokenPartial(); const name = FilteredSearchVisualTokens.getLastTokenPartial();
...@@ -228,13 +239,15 @@ export default class FilteredSearchVisualTokens { ...@@ -228,13 +239,15 @@ export default class FilteredSearchVisualTokens {
} }
} }
static addFilterVisualToken(tokenName, tokenValue, { static addFilterVisualToken(
canEdit, tokenName,
uppercaseTokenName = false, tokenValue,
capitalizeTokenValue = false, { canEdit, uppercaseTokenName = false, capitalizeTokenValue = false } = {},
} = {}) { ) {
const { lastVisualToken, isLastVisualTokenValid } const {
= FilteredSearchVisualTokens.getLastVisualTokenBeforeInput(); lastVisualToken,
isLastVisualTokenValid,
} = FilteredSearchVisualTokens.getLastVisualTokenBeforeInput();
const { addVisualTokenElement } = FilteredSearchVisualTokens; const { addVisualTokenElement } = FilteredSearchVisualTokens;
if (isLastVisualTokenValid) { if (isLastVisualTokenValid) {
...@@ -308,8 +321,7 @@ export default class FilteredSearchVisualTokens { ...@@ -308,8 +321,7 @@ export default class FilteredSearchVisualTokens {
static tokenizeInput() { static tokenizeInput() {
const input = FilteredSearchContainer.container.querySelector('.filtered-search'); const input = FilteredSearchContainer.container.querySelector('.filtered-search');
const { isLastVisualTokenValid } = const { isLastVisualTokenValid } = FilteredSearchVisualTokens.getLastVisualTokenBeforeInput();
FilteredSearchVisualTokens.getLastVisualTokenBeforeInput();
if (input.value) { if (input.value) {
if (isLastVisualTokenValid) { if (isLastVisualTokenValid) {
...@@ -375,8 +387,7 @@ export default class FilteredSearchVisualTokens { ...@@ -375,8 +387,7 @@ export default class FilteredSearchVisualTokens {
FilteredSearchVisualTokens.tokenizeInput(); FilteredSearchVisualTokens.tokenizeInput();
if (!tokenContainer.lastElementChild.isEqualNode(inputLi)) { if (!tokenContainer.lastElementChild.isEqualNode(inputLi)) {
const { isLastVisualTokenValid } = const { isLastVisualTokenValid } = FilteredSearchVisualTokens.getLastVisualTokenBeforeInput();
FilteredSearchVisualTokens.getLastVisualTokenBeforeInput();
if (!isLastVisualTokenValid) { if (!isLastVisualTokenValid) {
const lastPartial = FilteredSearchVisualTokens.getLastTokenPartial(); const lastPartial = FilteredSearchVisualTokens.getLastTokenPartial();
......
import FilteredSearchTokenKeys from './filtered_search_token_keys'; import FilteredSearchTokenKeys from './filtered_search_token_keys';
export const tokenKeys = [{ export const tokenKeys = [
{
key: 'author', key: 'author',
type: 'string', type: 'string',
param: 'username', param: 'username',
symbol: '@', symbol: '@',
icon: 'pencil', icon: 'pencil',
tag: '@author', tag: '@author',
}, { },
{
key: 'assignee', key: 'assignee',
type: 'string', type: 'string',
param: 'username', param: 'username',
symbol: '@', symbol: '@',
icon: 'user', icon: 'user',
tag: '@assignee', tag: '@assignee',
}, { },
{
key: 'milestone', key: 'milestone',
type: 'string', type: 'string',
param: 'title', param: 'title',
symbol: '%', symbol: '%',
icon: 'clock', icon: 'clock',
tag: '%milestone', tag: '%milestone',
}, { },
{
key: 'label', key: 'label',
type: 'array', type: 'array',
param: 'name[]', param: 'name[]',
symbol: '~', symbol: '~',
icon: 'labels', icon: 'labels',
tag: '~label', tag: '~label',
}]; },
];
if (gon.current_user_id) { if (gon.current_user_id) {
// Appending tokenkeys only logged-in // Appending tokenkeys only logged-in
...@@ -42,36 +47,47 @@ if (gon.current_user_id) { ...@@ -42,36 +47,47 @@ if (gon.current_user_id) {
}); });
} }
export const alternativeTokenKeys = [{ export const alternativeTokenKeys = [
{
key: 'label', key: 'label',
type: 'string', type: 'string',
param: 'name', param: 'name',
symbol: '~', symbol: '~',
}]; },
];
export const conditions = [{ export const conditions = [
{
url: 'assignee_id=0', url: 'assignee_id=0',
tokenKey: 'assignee', tokenKey: 'assignee',
value: 'none', value: 'none',
}, { },
{
url: 'milestone_title=No+Milestone', url: 'milestone_title=No+Milestone',
tokenKey: 'milestone', tokenKey: 'milestone',
value: 'none', value: 'none',
}, { },
{
url: 'milestone_title=%23upcoming', url: 'milestone_title=%23upcoming',
tokenKey: 'milestone', tokenKey: 'milestone',
value: 'upcoming', value: 'upcoming',
}, { },
{
url: 'milestone_title=%23started', url: 'milestone_title=%23started',
tokenKey: 'milestone', tokenKey: 'milestone',
value: 'started', value: 'started',
}, { },
{
url: 'label_name[]=No+Label', url: 'label_name[]=No+Label',
tokenKey: 'label', tokenKey: 'label',
value: 'none', value: 'none',
}]; },
];
const IssuableFilteredSearchTokenKeys = const IssuableFilteredSearchTokenKeys = new FilteredSearchTokenKeys(
new FilteredSearchTokenKeys(tokenKeys, alternativeTokenKeys, conditions); tokenKeys,
alternativeTokenKeys,
conditions,
);
export default IssuableFilteredSearchTokenKeys; export default IssuableFilteredSearchTokenKeys;
...@@ -3,11 +3,7 @@ import RecentSearchesDropdownContent from './components/recent_searches_dropdown ...@@ -3,11 +3,7 @@ import RecentSearchesDropdownContent from './components/recent_searches_dropdown
import eventHub from './event_hub'; import eventHub from './event_hub';
class RecentSearchesRoot { class RecentSearchesRoot {
constructor( constructor(recentSearchesStore, recentSearchesService, wrapperElement) {
recentSearchesStore,
recentSearchesService,
wrapperElement,
) {
this.store = recentSearchesStore; this.store = recentSearchesStore;
this.service = recentSearchesService; this.service = recentSearchesService;
this.wrapperElement = wrapperElement; this.wrapperElement = wrapperElement;
...@@ -35,7 +31,9 @@ class RecentSearchesRoot { ...@@ -35,7 +31,9 @@ class RecentSearchesRoot {
components: { components: {
RecentSearchesDropdownContent, RecentSearchesDropdownContent,
}, },
data() { return state; }, data() {
return state;
},
template: ` template: `
<recent-searches-dropdown-content <recent-searches-dropdown-content
:items="recentSearches" :items="recentSearches"
...@@ -57,7 +55,6 @@ class RecentSearchesRoot { ...@@ -57,7 +55,6 @@ class RecentSearchesRoot {
this.vm.$destroy(); this.vm.$destroy();
} }
} }
} }
export default RecentSearchesRoot; export default RecentSearchesRoot;
...@@ -2,11 +2,14 @@ import _ from 'underscore'; ...@@ -2,11 +2,14 @@ import _ from 'underscore';
class RecentSearchesStore { class RecentSearchesStore {
constructor(initialState = {}, allowedKeys) { constructor(initialState = {}, allowedKeys) {
this.state = Object.assign({ this.state = Object.assign(
{
isLocalStorageAvailable: true, isLocalStorageAvailable: true,
recentSearches: [], recentSearches: [],
allowedKeys, allowedKeys,
}, initialState); },
initialState,
);
} }
addRecentSearch(newSearch) { addRecentSearch(newSearch) {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment