BigW Consortium Gitlab

project_label_subscription.js.es6 1.43 KB
Newer Older
1 2
/* eslint-disable */
(function(global) {
3
  class ProjectLabelSubscription {
4
    constructor(container) {
5 6 7 8
      this.$container = $(container);
      this.$buttons = this.$container.find('.js-subscribe-button');

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

    toggleSubscription(event) {
      event.preventDefault();

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

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

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

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

        $span.toggleClass('hidden');
        $btn.removeClass('disabled');
36 37 38 39 40 41 42 43 44 45 46

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

        for (let button of this.$buttons) {
          let $button = $(button);

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

51
  global.ProjectLabelSubscription = ProjectLabelSubscription;
52 53

})(window.gl || (window.gl = {}));