summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/ci/build.coffee
blob: 98d05e41273697cab35fcd3a472cf6b7a3adcebc (plain)
1
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
class CiBuild
  @interval: null
  @state: null

  constructor: (build_url, build_status, build_state) ->
    clearInterval(CiBuild.interval)

    @state = build_state

    @initScrollButtonAffix()

    if build_status == "running" || build_status == "pending"
      #
      # Bind autoscroll button to follow build output
      #
      $("#autoscroll-button").bind "click", ->
        state = $(this).data("state")
        if "enabled" is state
          $(this).data "state", "disabled"
          $(this).text "enable autoscroll"
        else
          $(this).data "state", "enabled"
          $(this).text "disable autoscroll"

      #
      # Check for new build output if user still watching build page
      # Only valid for runnig build when output changes during time
      #
      CiBuild.interval = setInterval =>
        if window.location.href.split("#").first() is build_url
          last_state = @state
          $.ajax
            url: build_url + "/trace.json?state=" + encodeURIComponent(@state)
            dataType: "json"
            success: (log) =>
              return unless last_state is @state

              if log.state and log.status is "running"
                @state = log.state
                if log.append
                  $('.fa-refresh').before log.html
                else
                  $('#build-trace code').html log.html
                  $('#build-trace code').append '<i class="fa fa-refresh fa-spin"/>'
                @checkAutoscroll()
              else if log.status isnt build_status
                Turbolinks.visit build_url
      , 4000

  checkAutoscroll: ->
    $("html,body").scrollTop $("#build-trace").height()  if "enabled" is $("#autoscroll-button").data("state")

  initScrollButtonAffix: ->
    $buildScroll = $('#js-build-scroll')
    $body = $('body')
    $buildTrace = $('#build-trace')

    $buildScroll.affix(
      offset:
        bottom: ->
          $body.outerHeight() - ($buildTrace.outerHeight() + $buildTrace.offset().top)
    )

@CiBuild = CiBuild