gl_mentions.vue 3.4 KB
Newer Older
1
<script>
2
import { escape } from 'lodash';
3 4 5
import Tribute from 'tributejs';
import axios from '~/lib/utils/axios_utils';
import { spriteIcon } from '~/lib/utils/common_utils';
6
import SidebarMediator from '~/sidebar/sidebar_mediator';
7 8 9 10

/**
 * Creates the HTML template for each row of the mentions dropdown.
 *
11 12
 * @param original - An object from the array returned from the `autocomplete_sources/members` API
 * @returns {string} - An HTML template
13
 */
14
function menuItemTemplate({ original }) {
15 16 17
  const rectAvatarClass = original.type === 'Group' ? 'rect-avatar' : '';

  const avatarClasses = `avatar avatar-inline center s26 ${rectAvatarClass}
18
    gl-display-inline-flex! gl-align-items-center gl-justify-content-center`;
19 20 21 22 23 24 25 26

  const avatarTag = original.avatar_url
    ? `<img
        src="${original.avatar_url}"
        alt="${original.username}'s avatar"
        class="${avatarClasses}"/>`
    : `<div class="${avatarClasses}">${original.username.charAt(0).toUpperCase()}</div>`;

27
  const name = escape(original.name);
28 29 30 31

  const count = original.count && !original.mentionsDisabled ? ` (${original.count})` : '';

  const icon = original.mentionsDisabled
32
    ? spriteIcon('notifications-off', 's16 gl-vertical-align-middle gl-ml-3')
33 34 35 36
    : '';

  return `${avatarTag}
    ${original.username}
37
    <small class="gl-text-small gl-font-weight-normal gl-reset-color">${name}${count}</small>
38 39 40 41 42 43 44 45 46 47 48 49 50 51
    ${icon}`;
}

export default {
  name: 'GlMentions',
  props: {
    dataSources: {
      type: Object,
      required: false,
      default: () => gl.GfmAutoComplete?.dataSources || {},
    },
  },
  data() {
    return {
52
      assignees: undefined,
53 54 55 56
      members: undefined,
    };
  },
  mounted() {
57 58 59 60 61 62 63 64
    this.tribute = new Tribute({
      trigger: '@',
      fillAttr: 'username',
      lookup: value => value.name + value.username,
      menuItemTemplate,
      values: this.getMembers,
    });

65 66 67 68 69
    const input = this.$slots.default[0].elm;
    this.tribute.attach(input);
  },
  beforeDestroy() {
    const input = this.$slots.default[0].elm;
70 71 72 73 74 75 76 77 78 79 80
    this.tribute.detach(input);
  },
  methods: {
    /**
     * Creates the list of users to show in the mentions dropdown.
     *
     * @param inputText - The text entered by the user in the mentions input field
     * @param processValues - Callback function to set the list of users to show in the mentions dropdown
     */
    getMembers(inputText, processValues) {
      if (this.members) {
81
        processValues(this.getFilteredMembers());
82 83 84 85 86
      } else if (this.dataSources.members) {
        axios
          .get(this.dataSources.members)
          .then(response => {
            this.members = response.data;
87
            processValues(this.getFilteredMembers());
88 89 90 91 92 93
          })
          .catch(() => {});
      } else {
        processValues([]);
      }
    },
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
    getFilteredMembers() {
      const fullText = this.$slots.default[0].elm.value;

      if (!this.assignees) {
        this.assignees =
          SidebarMediator.singleton?.store?.assignees?.map(assignee => assignee.username) || [];
      }

      if (fullText.startsWith('/assign @')) {
        return this.members.filter(member => !this.assignees.includes(member.username));
      }

      if (fullText.startsWith('/unassign @')) {
        return this.members.filter(member => this.assignees.includes(member.username));
      }

      return this.members;
    },
112
  },
113 114
  render(createElement) {
    return createElement('div', this.$slots.default);
115 116 117
  },
};
</script>