build.coffee 3.2 KB
Newer Older
T
Takuya Noguchi 已提交
1
class @Build
D
Douwe Maan 已提交
2
  @interval: null
K
Kamil Trzcinski 已提交
3
  @state: null
D
Douwe Maan 已提交
4

5
  constructor: (@page_url, @build_url, @build_status, @state) ->
T
Takuya Noguchi 已提交
6
    clearInterval(Build.interval)
D
Douwe Maan 已提交
7

P
Phil Hughes 已提交
8 9 10 11 12 13 14
    # Init breakpoint checker
    @bp = Breakpoints.get()
    @hideSidebar()
    $('.js-build-sidebar').niceScroll()
    $(document)
      .off 'click', '.js-sidebar-build-toggle'
      .on 'click', '.js-sidebar-build-toggle', @toggleSidebar
K
Kamil Trzcinski 已提交
15

P
Phil Hughes 已提交
16 17 18
    $(window)
      .off 'resize.build'
      .on 'resize.build', @hideSidebar
19

20 21
    @updateArtifactRemoveDate()

P
Phil Hughes 已提交
22 23 24 25 26
    if $('#build-trace').length
      @getInitialBuildTrace()
      @initScrollButtonAffix()

    if @build_status is "running" or @build_status is "pending"
D
Douwe Maan 已提交
27 28 29
      #
      # Bind autoscroll button to follow build output
      #
P
Phil Hughes 已提交
30
      $('#autoscroll-button').on 'click', ->
D
Douwe Maan 已提交
31 32 33 34 35 36 37 38 39 40 41 42
        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
      #
T
Takuya Noguchi 已提交
43
      Build.interval = setInterval =>
44
        if window.location.href.split("#").first() is @page_url
P
Phil Hughes 已提交
45
          @getBuildTrace()
D
Douwe Maan 已提交
46 47
      , 4000

P
Phil Hughes 已提交
48 49 50 51 52 53 54 55 56 57 58 59
  getInitialBuildTrace: ->
    $.ajax
      url: @build_url
      dataType: 'json'
      success: (build_data) ->
        $('.js-build-output').html build_data.trace_html

        if build_data.status is 'success' or build_data.status is 'failed'
          $('.js-build-refresh').remove()

  getBuildTrace: ->
    $.ajax
60
      url: "#{@page_url}/trace.json?state=#{encodeURIComponent(@state)}"
P
Phil Hughes 已提交
61 62 63 64 65 66 67 68 69 70 71 72
      dataType: "json"
      success: (log) =>
        if log.state
          @state = log.state

        if log.status is "running"
          if log.append
            $('.js-build-output').append log.html
          else
            $('.js-build-output').html log.html
          @checkAutoscroll()
        else if log.status isnt @build_status
73
          Turbolinks.visit @page_url
P
Phil Hughes 已提交
74

D
Douwe Maan 已提交
75 76 77
  checkAutoscroll: ->
    $("html,body").scrollTop $("#build-trace").height()  if "enabled" is $("#autoscroll-button").data("state")

78
  initScrollButtonAffix: ->
P
Phil Hughes 已提交
79 80 81
    $buildScroll = $('#js-build-scroll')
    $body = $('body')
    $buildTrace = $('#build-trace')
82

P
Phil Hughes 已提交
83
    $buildScroll.affix(
84 85
      offset:
        bottom: ->
P
Phil Hughes 已提交
86
          $body.outerHeight() - ($buildTrace.outerHeight() + $buildTrace.offset().top)
87 88
    )

P
Phil Hughes 已提交
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
  shouldHideSidebar: ->
    bootstrapBreakpoint = @bp.getBreakpointSize()

    bootstrapBreakpoint is 'xs' or bootstrapBreakpoint is 'sm'

  toggleSidebar: =>
    if @shouldHideSidebar()
      $('.js-build-sidebar')
        .toggleClass 'right-sidebar-expanded right-sidebar-collapsed'

  hideSidebar: =>
    if @shouldHideSidebar()
      $('.js-build-sidebar')
        .removeClass 'right-sidebar-expanded'
        .addClass 'right-sidebar-collapsed'
    else
      $('.js-build-sidebar')
        .removeClass 'right-sidebar-collapsed'
        .addClass 'right-sidebar-expanded'
108 109 110 111 112 113 114

  updateArtifactRemoveDate: ->
    $date = $('.js-artifacts-remove')

    if $date.length
      date = $date.text()
      $date.text $.timefor(new Date(date), ' ')