BigW Consortium Gitlab

build.coffee 2.99 KB
Newer Older
Phil Hughes committed
1
class @CiBuild
2
  @interval: null
3
  @state: null
4

Phil Hughes committed
5
  constructor: (@build_url, @build_status, @state) ->
6 7
    clearInterval(CiBuild.interval)

Phil Hughes committed
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
15

Phil Hughes committed
16 17 18
    $(window)
      .off 'resize.build'
      .on 'resize.build', @hideSidebar
19

Phil Hughes committed
20 21 22 23 24
    if $('#build-trace').length
      @getInitialBuildTrace()
      @initScrollButtonAffix()

    if @build_status is "running" or @build_status is "pending"
25 26 27
      #
      # Bind autoscroll button to follow build output
      #
Phil Hughes committed
28
      $('#autoscroll-button').on 'click', ->
29 30 31 32 33 34 35 36 37 38 39 40 41
        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 =>
Phil Hughes committed
42 43
        if window.location.href.split("#").first() is @build_url
          @getBuildTrace()
44 45
      , 4000

Phil Hughes committed
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
  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
      url: "#{@build_url}/trace.json?state=#{encodeURIComponent(@state)}"
      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
          Turbolinks.visit @build_url

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

76
  initScrollButtonAffix: ->
Phil Hughes committed
77 78 79
    $buildScroll = $('#js-build-scroll')
    $body = $('body')
    $buildTrace = $('#build-trace')
80

Phil Hughes committed
81
    $buildScroll.affix(
82 83
      offset:
        bottom: ->
Phil Hughes committed
84
          $body.outerHeight() - ($buildTrace.outerHeight() + $buildTrace.offset().top)
85 86
    )

Phil Hughes committed
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
  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'