BigW Consortium Gitlab

right_sidebar.js 8.19 KB
Newer Older
1
/* eslint-disable func-names, space-before-function-paren, no-var, prefer-rest-params, wrap-iife, no-unused-vars, consistent-return, one-var, one-var-declaration-per-line, quotes, prefer-template, object-shorthand, comma-dangle, no-else-return, no-param-reassign, max-len */
2 3

import Cookies from 'js-cookie';
4

Fatih Acet committed
5
(function() {
6
  var bind = function(fn, me) { return function() { return fn.apply(me, arguments); }; };
Fatih Acet committed
7 8 9 10 11

  this.Sidebar = (function() {
    function Sidebar(currentUser) {
      this.toggleTodo = bind(this.toggleTodo, this);
      this.sidebar = $('aside');
12
      this.removeListeners();
Fatih Acet committed
13 14 15
      this.addEventListeners();
    }

16 17 18 19 20 21
    Sidebar.prototype.removeListeners = function () {
      this.sidebar.off('click', '.sidebar-collapsed-icon');
      $('.dropdown').off('hidden.gl.dropdown');
      $('.dropdown').off('loading.gl.dropdown');
      $('.dropdown').off('loaded.gl.dropdown');
      $(document).off('click', '.js-sidebar-toggle');
22
    };
23

Fatih Acet committed
24
    Sidebar.prototype.addEventListeners = function() {
25 26 27
      const $document = $(document);
      const throttledSetSidebarHeight = _.throttle(this.setSidebarHeight, 10);

Fatih Acet committed
28 29 30 31
      this.sidebar.on('click', '.sidebar-collapsed-icon', this, this.sidebarCollapseClicked);
      $('.dropdown').on('hidden.gl.dropdown', this, this.onSidebarDropdownHidden);
      $('.dropdown').on('loading.gl.dropdown', this.sidebarDropdownLoading);
      $('.dropdown').on('loaded.gl.dropdown', this.sidebarDropdownLoaded);
32 33 34
      $(window).on('resize', () => throttledSetSidebarHeight());
      $document.on('scroll', () => throttledSetSidebarHeight());
      $document.on('click', '.js-sidebar-toggle', function(e, triggered) {
Fatih Acet committed
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
        var $allGutterToggleIcons, $this, $thisIcon;
        e.preventDefault();
        $this = $(this);
        $thisIcon = $this.find('i');
        $allGutterToggleIcons = $('.js-sidebar-toggle i');
        if ($thisIcon.hasClass('fa-angle-double-right')) {
          $allGutterToggleIcons.removeClass('fa-angle-double-right').addClass('fa-angle-double-left');
          $('aside.right-sidebar').removeClass('right-sidebar-expanded').addClass('right-sidebar-collapsed');
          $('.page-with-sidebar').removeClass('right-sidebar-expanded').addClass('right-sidebar-collapsed');
        } else {
          $allGutterToggleIcons.removeClass('fa-angle-double-left').addClass('fa-angle-double-right');
          $('aside.right-sidebar').removeClass('right-sidebar-collapsed').addClass('right-sidebar-expanded');
          $('.page-with-sidebar').removeClass('right-sidebar-collapsed').addClass('right-sidebar-expanded');
        }
        if (!triggered) {
50
          return Cookies.set("collapsed_gutter", $('.right-sidebar').hasClass('right-sidebar-collapsed'));
Fatih Acet committed
51 52 53 54 55 56 57 58 59 60 61 62 63 64
        }
      });
      return $(document).off('click', '.js-issuable-todo').on('click', '.js-issuable-todo', this.toggleTodo);
    };

    Sidebar.prototype.toggleTodo = function(e) {
      var $btnText, $this, $todoLoading, ajaxType, url;
      $this = $(e.currentTarget);
      ajaxType = $this.attr('data-delete-path') ? 'DELETE' : 'POST';
      if ($this.attr('data-delete-path')) {
        url = "" + ($this.attr('data-delete-path'));
      } else {
        url = "" + ($this.data('url'));
      }
65 66 67

      $this.tooltip('hide');

Fatih Acet committed
68 69 70 71 72 73 74 75 76 77
      return $.ajax({
        url: url,
        type: ajaxType,
        dataType: 'json',
        data: {
          issuable_id: $this.data('issuable-id'),
          issuable_type: $this.data('issuable-type')
        },
        beforeSend: (function(_this) {
          return function() {
78 79
            $('.js-issuable-todo').disable()
              .addClass('is-loading');
Fatih Acet committed
80 81 82 83
          };
        })(this)
      }).done((function(_this) {
        return function(data) {
84
          return _this.todoUpdateDone(data);
Fatih Acet committed
85 86 87 88
        };
      })(this));
    };

89 90 91 92
    Sidebar.prototype.todoUpdateDone = function(data) {
      const deletePath = data.delete_path ? data.delete_path : null;
      const attrPrefix = deletePath ? 'mark' : 'todo';
      const $todoBtns = $('.js-issuable-todo');
Fatih Acet committed
93

Clement Ho committed
94 95
      $(document).trigger('todo:toggle', data.count);

96 97 98
      $todoBtns.each((i, el) => {
        const $el = $(el);
        const $elText = $el.find('.js-issuable-todo-inner');
Clement Ho committed
99

100 101 102 103 104 105
        $el.removeClass('is-loading')
          .enable()
          .attr('aria-label', $el.data(`${attrPrefix}-text`))
          .attr('data-delete-path', deletePath)
          .attr('title', $el.data(`${attrPrefix}-text`));

106 107 108 109 110 111 112 113 114
        if ($el.hasClass('has-tooltip')) {
          $el.tooltip('fixTitle');
        }

        if ($el.data(`${attrPrefix}-icon`)) {
          $elText.html($el.data(`${attrPrefix}-icon`));
        } else {
          $elText.text($el.data(`${attrPrefix}-text`));
        }
115
      });
Fatih Acet committed
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210
    };

    Sidebar.prototype.sidebarDropdownLoading = function(e) {
      var $loading, $sidebarCollapsedIcon, i, img;
      $sidebarCollapsedIcon = $(this).closest('.block').find('.sidebar-collapsed-icon');
      img = $sidebarCollapsedIcon.find('img');
      i = $sidebarCollapsedIcon.find('i');
      $loading = $('<i class="fa fa-spinner fa-spin"></i>');
      if (img.length) {
        img.before($loading);
        return img.hide();
      } else if (i.length) {
        i.before($loading);
        return i.hide();
      }
    };

    Sidebar.prototype.sidebarDropdownLoaded = function(e) {
      var $sidebarCollapsedIcon, i, img;
      $sidebarCollapsedIcon = $(this).closest('.block').find('.sidebar-collapsed-icon');
      img = $sidebarCollapsedIcon.find('img');
      $sidebarCollapsedIcon.find('i.fa-spin').remove();
      i = $sidebarCollapsedIcon.find('i');
      if (img.length) {
        return img.show();
      } else {
        return i.show();
      }
    };

    Sidebar.prototype.sidebarCollapseClicked = function(e) {
      var $block, sidebar;
      if ($(e.currentTarget).hasClass('dont-change-state')) {
        return;
      }
      sidebar = e.data;
      e.preventDefault();
      $block = $(this).closest('.block');
      return sidebar.openDropdown($block);
    };

    Sidebar.prototype.openDropdown = function(blockOrName) {
      var $block;
      $block = _.isString(blockOrName) ? this.getBlock(blockOrName) : blockOrName;
      $block.find('.edit-link').trigger('click');
      if (!this.isOpen()) {
        this.setCollapseAfterUpdate($block);
        return this.toggleSidebar('open');
      }
    };

    Sidebar.prototype.setCollapseAfterUpdate = function($block) {
      $block.addClass('collapse-after-update');
      return $('.page-with-sidebar').addClass('with-overlay');
    };

    Sidebar.prototype.onSidebarDropdownHidden = function(e) {
      var $block, sidebar;
      sidebar = e.data;
      e.preventDefault();
      $block = $(this).closest('.block');
      return sidebar.sidebarDropdownHidden($block);
    };

    Sidebar.prototype.sidebarDropdownHidden = function($block) {
      if ($block.hasClass('collapse-after-update')) {
        $block.removeClass('collapse-after-update');
        $('.page-with-sidebar').removeClass('with-overlay');
        return this.toggleSidebar('hide');
      }
    };

    Sidebar.prototype.triggerOpenSidebar = function() {
      return this.sidebar.find('.js-sidebar-toggle').trigger('click');
    };

    Sidebar.prototype.toggleSidebar = function(action) {
      if (action == null) {
        action = 'toggle';
      }
      if (action === 'toggle') {
        this.triggerOpenSidebar();
      }
      if (action === 'open') {
        if (!this.isOpen()) {
          this.triggerOpenSidebar();
        }
      }
      if (action === 'hide') {
        if (this.isOpen()) {
          return this.triggerOpenSidebar();
        }
      }
    };

211
    Sidebar.prototype.setSidebarHeight = function() {
212
      const $navHeight = $('.navbar-gitlab').outerHeight() + $('.layout-nav').outerHeight() + $('.sub-nav-scroll').outerHeight();
213
      const $rightSidebar = $('.js-right-sidebar');
214
      const diff = $navHeight - $(window).scrollTop();
215
      if (diff > 0) {
216
        $rightSidebar.outerHeight($(window).height() - diff);
217
      } else {
218
        $rightSidebar.outerHeight('100%');
219 220 221
      }
    };

Fatih Acet committed
222 223 224 225 226 227 228 229 230 231
    Sidebar.prototype.isOpen = function() {
      return this.sidebar.is('.right-sidebar-expanded');
    };

    Sidebar.prototype.getBlock = function(name) {
      return this.sidebar.find(".block." + name);
    };

    return Sidebar;
  })();
232
}).call(window);