weight_select.js 2.27 KB
Newer Older
Phil Hughes's avatar
Phil Hughes committed
1
/* eslint-disable */
Fatih Acet's avatar
Fatih Acet committed
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
(function() {
  this.WeightSelect = (function() {
    function WeightSelect() {
      $('.js-weight-select').each(function(i, dropdown) {
        var $block, $dropdown, $loading, $selectbox, $sidebarCollapsedValue, $value, abilityName, updateUrl, updateWeight;
        $dropdown = $(dropdown);
        updateUrl = $dropdown.data('issueUpdate');
        $selectbox = $dropdown.closest('.selectbox');
        $block = $selectbox.closest('.block');
        $sidebarCollapsedValue = $block.find('.sidebar-collapsed-icon span');
        $value = $block.find('.value');
        abilityName = $dropdown.data('ability-name');
        $loading = $block.find('.block-loading').fadeOut();
        updateWeight = function(selected) {
          var data;
          data = {};
          data[abilityName] = {};
          data[abilityName].weight = selected != null ? selected : null;
          $loading.fadeIn();
          $dropdown.trigger('loading.gl.dropdown');
          return $.ajax({
            type: 'PUT',
            dataType: 'json',
            url: updateUrl,
            data: data
          }).done(function(data) {
            $dropdown.trigger('loaded.gl.dropdown');
            $loading.fadeOut();
            $selectbox.hide();
            if (data.weight != null) {
              $value.html(data.weight);
            } else {
              $value.html('None');
            }
            return $sidebarCollapsedValue.html(data.weight);
          });
        };
        return $dropdown.glDropdown({
          selectable: true,
          fieldName: $dropdown.data("field-name"),
          hidden: function(e) {
            $selectbox.hide();
            return $value.css('display', '');
          },
          id: function(obj, el) {
            if ($(el).data("none") == null) {
              return $(el).data("id");
            }
          },
          clicked: function(selected) {
            if ($(dropdown).is(".js-filter-submit")) {
              return $(dropdown).parents('form').submit();
            } else {
              selected = $dropdown.closest('.selectbox').find("input[name='" + ($dropdown.data('field-name')) + "']").val();
              return updateWeight(selected);
            }
          }
        });
      });
    }

    return WeightSelect;

  })();

}).call(this);