summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/user_popovers.js
blob: 7a7518bcf83d2438acbda1eee372927edaed7b68 (plain)
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
import Vue from 'vue';
import UsersCache from './lib/utils/users_cache';
import UserPopover from './vue_shared/components/user_popover/user_popover.vue';

const removeTitle = (el) => {
  // Removing titles so its not showing tooltips also

  el.dataset.originalTitle = '';
  el.setAttribute('title', '');
};

const getPreloadedUserInfo = (dataset) => {
  const userId = dataset.user || dataset.userId;
  const { username, name, avatarUrl } = dataset;

  return {
    userId,
    username,
    name,
    avatarUrl,
  };
};

/**
 * Adds a UserPopover component to the body, hands over as much data as the target element has in data attributes.
 * loads based on data-user-id more data about a user from the API and sets it on the popover
 */
const populateUserInfo = (user) => {
  const { userId } = user;

  return Promise.all([UsersCache.retrieveById(userId), UsersCache.retrieveStatusById(userId)]).then(
    ([userData, status]) => {
      if (userData) {
        Object.assign(user, {
          avatarUrl: userData.avatar_url,
          bot: userData.bot,
          username: userData.username,
          name: userData.name,
          location: userData.location,
          bio: userData.bio,
          workInformation: userData.work_information,
          websiteUrl: userData.website_url,
          pronouns: userData.pronouns,
          loaded: true,
        });
      }

      if (status) {
        Object.assign(user, {
          status,
        });
      }

      return user;
    },
  );
};

const initializedPopovers = new Map();
let domObservedForChanges = false;

const addPopoversToModifiedTree = new MutationObserver(() => {
  const userLinks = document?.querySelectorAll('.js-user-link, .gfm-project_member');

  if (userLinks) {
    addPopovers(userLinks); /* eslint-disable-line no-use-before-define */
  }
});

function observeBody() {
  if (!domObservedForChanges) {
    addPopoversToModifiedTree.observe(document.body, {
      subtree: true,
      childList: true,
    });

    domObservedForChanges = true;
  }
}

export default function addPopovers(elements = document.querySelectorAll('.js-user-link')) {
  const userLinks = Array.from(elements);
  const UserPopoverComponent = Vue.extend(UserPopover);

  observeBody();

  return userLinks
    .filter(({ dataset }) => dataset.user || dataset.userId)
    .map((el) => {
      if (initializedPopovers.has(el)) {
        return initializedPopovers.get(el);
      }

      const user = {
        location: null,
        bio: null,
        workInformation: null,
        status: null,
        loaded: false,
      };
      const renderedPopover = new UserPopoverComponent({
        propsData: {
          target: el,
          user,
        },
      });

      initializedPopovers.set(el, renderedPopover);

      renderedPopover.$mount();

      el.addEventListener('mouseenter', ({ target }) => {
        removeTitle(target);
        const preloadedUserInfo = getPreloadedUserInfo(target.dataset);

        Object.assign(user, preloadedUserInfo);

        if (preloadedUserInfo.userId) {
          populateUserInfo(user);
        }
      });
      el.addEventListener('mouseleave', ({ target }) => {
        target.removeAttribute('aria-describedby');
      });

      return renderedPopover;
    });
}