BigW Consortium Gitlab

build.js 6.92 KB
Newer Older
1
/* eslint-disable */
Fatih Acet committed
2 3 4 5 6 7 8 9
(function() {
  var bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; };

  this.Build = (function() {
    Build.interval = null;

    Build.state = null;

10 11 12 13 14 15
    function Build(options) {
      this.page_url = options.page_url;
      this.build_url = options.build_url;
      this.build_status = options.build_status;
      this.state = options.state1;
      this.build_stage = options.build_stage;
Fatih Acet committed
16 17
      this.hideSidebar = bind(this.hideSidebar, this);
      this.toggleSidebar = bind(this.toggleSidebar, this);
18
      this.updateDropdown = bind(this.updateDropdown, this);
19
      this.$document = $(document);
Fatih Acet committed
20
      clearInterval(Build.interval);
21
      // Init breakpoint checker
Fatih Acet committed
22
      this.bp = Breakpoints.get();
23
      this.initSidebar();
24

25 26
      this.populateJobs(this.build_stage);
      this.updateStageDropdownText(this.build_stage);
27

Fatih Acet committed
28
      $(window).off('resize.build').on('resize.build', this.hideSidebar);
29
      this.$document.off('click', '.stage-item').on('click', '.stage-item', this.updateDropdown);
30
      $('#js-build-scroll > a').off('click').on('click', this.stepTrace);
Fatih Acet committed
31 32 33
      this.updateArtifactRemoveDate();
      if ($('#build-trace').length) {
        this.getInitialBuildTrace();
34
        this.initScrollButtons();
Fatih Acet committed
35 36 37 38 39 40 41 42 43 44 45 46
      }
      if (this.build_status === "running" || this.build_status === "pending") {
        $('#autoscroll-button').on('click', function() {
          var state;
          state = $(this).data("state");
          if ("enabled" === state) {
            $(this).data("state", "disabled");
            return $(this).text("enable autoscroll");
          } else {
            $(this).data("state", "enabled");
            return $(this).text("disable autoscroll");
          }
47 48 49
        //
        // Bind autoscroll button to follow build output
        //
Fatih Acet committed
50 51 52 53 54 55 56
        });
        Build.interval = setInterval((function(_this) {
          return function() {
            if (window.location.href.split("#").first() === _this.page_url) {
              return _this.getBuildTrace();
            }
          };
57 58 59 60
        //
        // Check for new build output if user still watching build page
        // Only valid for runnig build when output changes during time
        //
Fatih Acet committed
61 62 63 64
        })(this), 4000);
      }
    }

65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
    Build.prototype.initSidebar = function() {
      this.$sidebar = $('.js-build-sidebar');
      this.sidebarTranslationLimits = {
        min: $('.navbar-gitlab').outerHeight() + $('.layout-nav').outerHeight()
      }
      this.sidebarTranslationLimits.max = this.sidebarTranslationLimits.min + $('.scrolling-tabs-container').outerHeight();
      this.$sidebar.css({
        top: this.sidebarTranslationLimits.max
      });
      this.$sidebar.niceScroll();
      this.hideSidebar();
      this.$document.off('click', '.js-sidebar-build-toggle').on('click', '.js-sidebar-build-toggle', this.toggleSidebar);
      this.$document.off('scroll.translateSidebar').on('scroll.translateSidebar', this.translateSidebar.bind(this));
    };

Fatih Acet committed
80
    Build.prototype.getInitialBuildTrace = function() {
81 82
      var removeRefreshStatuses = ['success', 'failed', 'canceled', 'skipped']

Fatih Acet committed
83 84 85 86 87
      return $.ajax({
        url: this.build_url,
        dataType: 'json',
        success: function(build_data) {
          $('.js-build-output').html(build_data.trace_html);
88
          if (removeRefreshStatuses.indexOf(build_data.status) >= 0) {
Fatih Acet committed
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
            return $('.js-build-refresh').remove();
          }
        }
      });
    };

    Build.prototype.getBuildTrace = function() {
      return $.ajax({
        url: this.page_url + "/trace.json?state=" + (encodeURIComponent(this.state)),
        dataType: "json",
        success: (function(_this) {
          return function(log) {
            if (log.state) {
              _this.state = log.state;
            }
            if (log.status === "running") {
              if (log.append) {
                $('.js-build-output').append(log.html);
              } else {
                $('.js-build-output').html(log.html);
              }
              return _this.checkAutoscroll();
            } else if (log.status !== _this.build_status) {
              return Turbolinks.visit(_this.page_url);
            }
          };
        })(this)
      });
    };

    Build.prototype.checkAutoscroll = function() {
      if ("enabled" === $("#autoscroll-button").data("state")) {
        return $("html,body").scrollTop($("#build-trace").height());
      }
    };

125
    Build.prototype.initScrollButtons = function() {
Fatih Acet committed
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
      var $body, $buildScroll, $buildTrace;
      $buildScroll = $('#js-build-scroll');
      $body = $('body');
      $buildTrace = $('#build-trace');
      return $buildScroll.affix({
        offset: {
          bottom: function() {
            return $body.outerHeight() - ($buildTrace.outerHeight() + $buildTrace.offset().top);
          }
        }
      });
    };

    Build.prototype.shouldHideSidebar = function() {
      var bootstrapBreakpoint;
      bootstrapBreakpoint = this.bp.getBreakpointSize();
      return bootstrapBreakpoint === 'xs' || bootstrapBreakpoint === 'sm';
    };

    Build.prototype.toggleSidebar = function() {
      if (this.shouldHideSidebar()) {
147
        return this.$sidebar.toggleClass('right-sidebar-expanded right-sidebar-collapsed');
Fatih Acet committed
148 149 150
      }
    };

151
    Build.prototype.translateSidebar = function(e) {
152
      var newPosition = this.sidebarTranslationLimits.max - (document.body.scrollTop || document.documentElement.scrollTop);
153 154 155 156 157 158
      if (newPosition < this.sidebarTranslationLimits.min) newPosition = this.sidebarTranslationLimits.min;
      this.$sidebar.css({
        top: newPosition
      });
    };

Fatih Acet committed
159 160
    Build.prototype.hideSidebar = function() {
      if (this.shouldHideSidebar()) {
161
        return this.$sidebar.removeClass('right-sidebar-expanded').addClass('right-sidebar-collapsed');
Fatih Acet committed
162
      } else {
163
        return this.$sidebar.removeClass('right-sidebar-collapsed').addClass('right-sidebar-expanded');
Fatih Acet committed
164 165 166 167 168 169 170 171
      }
    };

    Build.prototype.updateArtifactRemoveDate = function() {
      var $date, date;
      $date = $('.js-artifacts-remove');
      if ($date.length) {
        date = $date.text();
172
        return $date.text($.timefor(new Date(date.replace(/([0-9]+)-([0-9]+)-([0-9]+)/g, '$1/$2/$3')), ' '));
Fatih Acet committed
173 174 175
      }
    };

176
    Build.prototype.populateJobs = function(stage) {
177 178 179 180
      $('.build-job').hide();
      $('.build-job[data-stage="' + stage + '"]').show();
    };

181 182 183 184
    Build.prototype.updateStageDropdownText = function(stage) {
      $('.stage-selection').text(stage);
    };

185 186
    Build.prototype.updateDropdown = function(e) {
      e.preventDefault();
187
      var stage = e.currentTarget.text;
188
      this.updateStageDropdownText(stage);
189
      this.populateJobs(stage);
190 191
    };

192
    Build.prototype.stepTrace = function(e) {
193 194 195 196 197
      e.preventDefault();
      $currentTarget = $(e.currentTarget);
      $.scrollTo($currentTarget.attr('href'), {
        offset: -($('.navbar-gitlab').outerHeight() + $('.layout-nav').outerHeight())
      });
198 199
    };

Fatih Acet committed
200 201 202 203 204
    return Build;

  })();

}).call(this);