All files / app/assets/javascripts/filtered_search available_dropdown_mappings.js

80.48% Statements 33/41
66.66% Branches 10/15
68.42% Functions 13/19
84.61% Lines 33/39

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 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 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 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 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247                                                                    42x 42x 42x 42x 42x 42x 42x 42x 42x               42x                         42x 218x 218x       42x       42x                                                                                                                                                                                                                                       42x       42x       42x   42x       42x 42x     42x       42x       42x 42x       42x 42x       84x 84x         84x       42x       84x       84x      
import { sortMilestonesByDueDate } from '~/milestones/utils';
import { mergeUrlParams } from '~/lib/utils/url_utility';
import {
  TOKEN_TYPE_APPROVED_BY,
  TOKEN_TYPE_MERGE_USER,
  TOKEN_TYPE_ASSIGNEE,
  TOKEN_TYPE_AUTHOR,
  TOKEN_TYPE_CONFIDENTIAL,
  TOKEN_TYPE_LABEL,
  TOKEN_TYPE_MILESTONE,
  TOKEN_TYPE_MY_REACTION,
  TOKEN_TYPE_RELEASE,
  TOKEN_TYPE_REVIEWER,
  TOKEN_TYPE_TARGET_BRANCH,
  TOKEN_TYPE_SOURCE_BRANCH,
} from '~/vue_shared/components/filtered_search_bar/constants';
import DropdownEmoji from './dropdown_emoji';
import DropdownHint from './dropdown_hint';
import DropdownNonUser from './dropdown_non_user';
import DropdownOperator from './dropdown_operator';
import DropdownUser from './dropdown_user';
import DropdownUtils from './dropdown_utils';
 
export default class AvailableDropdownMappings {
  constructor({
    container,
    labelsEndpoint,
    milestonesEndpoint,
    releasesEndpoint,
    environmentsEndpoint,
    groupsOnly,
    includeAncestorGroups,
    includeDescendantGroups,
  }) {
    this.container = container;
    this.labelsEndpoint = labelsEndpoint;
    this.milestonesEndpoint = milestonesEndpoint;
    this.releasesEndpoint = releasesEndpoint;
    this.environmentsEndpoint = environmentsEndpoint;
    this.groupsOnly = groupsOnly;
    this.includeAncestorGroups = includeAncestorGroups;
    this.includeDescendantGroups = includeDescendantGroups;
    this.filteredSearchInput = this.container.querySelector('.filtered-search');
  }
 
  getAllowedMappings(supportedTokens) {
    return this.buildMappings(supportedTokens, this.getMappings());
  }
 
  buildMappings(supportedTokens, availableMappings) {
    const allowedMappings = {
      hint: {
        reference: null,
        gl: DropdownHint,
        element: this.container.querySelector('#js-dropdown-hint'),
      },
      operator: {
        reference: null,
        gl: DropdownOperator,
        element: this.container.querySelector('#js-dropdown-operator'),
      },
    };
 
    supportedTokens.forEach((type) => {
      Eif (availableMappings[type]) {
        allowedMappings[type] = availableMappings[type];
      }
    });
 
    return allowedMappings;
  }
 
  getMappings() {
    return {
      [TOKEN_TYPE_AUTHOR]: {
        reference: null,
        gl: DropdownUser,
        element: this.container.querySelector('#js-dropdown-author'),
      },
      [TOKEN_TYPE_ASSIGNEE]: {
        reference: null,
        gl: DropdownUser,
        element: this.container.querySelector('#js-dropdown-assignee'),
      },
      [TOKEN_TYPE_REVIEWER]: {
        reference: null,
        gl: DropdownUser,
        element: this.container.querySelector('#js-dropdown-reviewer'),
      },
      [TOKEN_TYPE_MERGE_USER]: {
        reference: null,
        gl: DropdownUser,
        element: this.container.querySelector('#js-dropdown-merge-user'),
      },
      attention: {
        reference: null,
        gl: DropdownUser,
        element: this.container.getElementById('js-dropdown-attention-requested'),
      },
      [TOKEN_TYPE_APPROVED_BY]: {
        reference: null,
        gl: DropdownUser,
        element: this.container.querySelector('#js-dropdown-approved-by'),
      },
      [TOKEN_TYPE_MILESTONE]: {
        reference: null,
        gl: DropdownNonUser,
        extraArguments: {
          endpoint: this.getMilestoneEndpoint(),
          symbol: '%',
          preprocessing: (milestones) => milestones.sort(sortMilestonesByDueDate),
        },
        element: this.container.querySelector('#js-dropdown-milestone'),
      },
      [TOKEN_TYPE_RELEASE]: {
        reference: null,
        gl: DropdownNonUser,
        extraArguments: {
          endpoint: this.getReleasesEndpoint(),
          symbol: '',
 
          // The DropdownNonUser class is hardcoded to look for and display a
          // "title" property, so we need to add this property to each release object
          preprocessing: (releases) => releases.map((r) => ({ ...r, title: r.tag })),
        },
        element: this.container.querySelector('#js-dropdown-release'),
      },
      [TOKEN_TYPE_LABEL]: {
        reference: null,
        gl: DropdownNonUser,
        extraArguments: {
          endpoint: this.getLabelsEndpoint(),
          symbol: '~',
          preprocessing: DropdownUtils.duplicateLabelPreprocessing,
        },
        element: this.container.querySelector('#js-dropdown-label'),
      },
      [TOKEN_TYPE_MY_REACTION]: {
        reference: null,
        gl: DropdownEmoji,
        element: this.container.querySelector('#js-dropdown-my-reaction'),
      },
      draft: {
        reference: null,
        gl: DropdownNonUser,
        element: this.container.querySelector('#js-dropdown-wip'),
      },
      approved: {
        reference: null,
        gl: DropdownNonUser,
        element: this.container.querySelector('#js-dropdown-approved'),
      },
      [TOKEN_TYPE_CONFIDENTIAL]: {
        reference: null,
        gl: DropdownNonUser,
        element: this.container.querySelector('#js-dropdown-confidential'),
      },
      [TOKEN_TYPE_TARGET_BRANCH]: {
        reference: null,
        gl: DropdownNonUser,
        extraArguments: {
          endpoint: this.getMergeRequestTargetBranchesEndpoint(),
          symbol: '',
        },
        element: this.container.querySelector('#js-dropdown-target-branch'),
      },
      [TOKEN_TYPE_SOURCE_BRANCH]: {
        reference: null,
        gl: DropdownNonUser,
        extraArguments: {
          endpoint: this.getMergeRequestSourceBranchesEndpoint(),
          symbol: '',
        },
        element: this.container.querySelector('#js-dropdown-source-branch'),
      },
      environment: {
        reference: null,
        gl: DropdownNonUser,
        extraArguments: {
          endpoint: this.getEnvironmentsEndpoint(),
          symbol: '',
          preprocessing: (data) => data.map((env) => ({ title: env })),
        },
        element: this.container.querySelector('#js-dropdown-environment'),
      },
    };
  }
 
  getMilestoneEndpoint() {
    return `${this.milestonesEndpoint}.json`;
  }
 
  getReleasesEndpoint() {
    return `${this.releasesEndpoint}.json`;
  }
 
  getLabelsEndpoint() {
    let endpoint = `${this.labelsEndpoint}.json?`;
 
    Iif (this.groupsOnly) {
      endpoint = `${endpoint}only_group_labels=true&`;
    }
 
    Eif (this.includeAncestorGroups) {
      endpoint = `${endpoint}include_ancestor_groups=true&`;
    }
 
    Iif (this.includeDescendantGroups) {
      endpoint = `${endpoint}include_descendant_groups=true`;
    }
 
    return endpoint;
  }
 
  getMergeRequestTargetBranchesEndpoint() {
    const targetBranchEndpointPath = '/-/autocomplete/merge_request_target_branches.json';
    return this.getMergeRequestBranchesEndpoint(targetBranchEndpointPath);
  }
 
  getMergeRequestSourceBranchesEndpoint() {
    const sourceBranchEndpointPath = '/-/autocomplete/merge_request_source_branches.json';
    return this.getMergeRequestBranchesEndpoint(sourceBranchEndpointPath);
  }
 
  getMergeRequestBranchesEndpoint(endpointPath = '') {
    const endpoint = `${gon.relative_url_root || ''}${endpointPath}`;
    const params = {
      group_id: this.getGroupId(),
      project_id: this.getProjectId(),
    };
 
    return mergeUrlParams(params, endpoint);
  }
 
  getEnvironmentsEndpoint() {
    return `${this.environmentsEndpoint}.json`;
  }
 
  getGroupId() {
    return this.filteredSearchInput.dataset.groupId || '';
  }
 
  getProjectId() {
    return this.filteredSearchInput.dataset.projectId || '';
  }
}