BigW Consortium Gitlab

project_label_subscription.js 1.62 KB
Newer Older
1
/* eslint-disable wrap-iife, func-names, space-before-function-paren, object-shorthand, comma-dangle, one-var, one-var-declaration-per-line, no-restricted-syntax, max-len, no-param-reassign */
2

3
(function(global) {
4
  class ProjectLabelSubscription {
5
    constructor(container) {
6 7 8 9
      this.$container = $(container);
      this.$buttons = this.$container.find('.js-subscribe-button');

      this.$buttons.on('click', this.toggleSubscription.bind(this));
10 11 12 13 14 15 16 17
    }

    toggleSubscription(event) {
      event.preventDefault();

      const $btn = $(event.currentTarget);
      const $span = $btn.find('span');
      const url = $btn.attr('data-url');
18
      const oldStatus = $btn.attr('data-status');
19 20 21 22 23 24 25 26 27 28

      $btn.addClass('disabled');
      $span.toggleClass('hidden');

      $.ajax({
        type: 'POST',
        url: url
      }).done(() => {
        let newStatus, newAction;

29
        if (oldStatus === 'unsubscribed') {
30
          [newStatus, newAction] = ['subscribed', 'Unsubscribe'];
31 32
        } else {
          [newStatus, newAction] = ['unsubscribed', 'Subscribe'];
33 34 35 36
        }

        $span.toggleClass('hidden');
        $btn.removeClass('disabled');
37 38 39 40

        this.$buttons.attr('data-status', newStatus);
        this.$buttons.find('> span').text(newAction);

41
        this.$buttons.map((button) => {
42
          const $button = $(button);
43 44 45 46

          if ($button.attr('data-original-title')) {
            $button.tooltip('hide').attr('data-original-title', newAction).tooltip('fixTitle');
          }
47 48 49

          return button;
        });
50 51 52 53
      });
    }
  }

54
  global.ProjectLabelSubscription = ProjectLabelSubscription;
55
})(window.gl || (window.gl = {}));