Welcome to mirror list, hosted at ThFree Co, Russian Federation.

star.js « projects « javascripts « assets « app - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 578e22ca25da84c8754e00d6c61111a6ca6c0545 (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
import $ from 'jquery';
import createFlash from '~/flash';
import axios from '~/lib/utils/axios_utils';
import { spriteIcon } from '~/lib/utils/common_utils';
import { __, s__ } from '~/locale';

export default class Star {
  constructor(container = '.project-home-panel') {
    $(`${container} .toggle-star`).on('click', function toggleStarClickCallback() {
      const $this = $(this);
      const $starSpan = $this.find('span');
      const $starIcon = $this.find('svg');
      const iconClasses = $starIcon.attr('class').split(' ');

      axios
        .post($this.data('endpoint'))
        .then(({ data }) => {
          const isStarred = $starSpan.hasClass('starred');
          $this.parent().find('.count').text(data.star_count);

          if (isStarred) {
            $starSpan.removeClass('starred').text(s__('StarProject|Star'));
            $starIcon.remove();
            $this.prepend(spriteIcon('star-o', iconClasses));
          } else {
            $starSpan.addClass('starred').text(__('Unstar'));
            $starIcon.remove();
            $this.prepend(spriteIcon('star', iconClasses));
          }
        })
        .catch(() =>
          createFlash({
            message: __('Star toggle failed. Try again later.'),
          }),
        );
    });
  }
}