<commit_before># Switcher widget written by <NAME> / SC5

# Switches (reloads to another address) the dashboards in periodic manner
# <div id="container" data-switcher-interval="10000" data-switcher-dashboards="dashboard1 dashboard2">
#   <%= yield %>
# </div>
#
class DashboardSwitcher
  constructor: () ->
    @dashboardNames = []
    # Collect the dashboard names from attribute, if provided (otherwise skip switching)
    names = $('[data-switcher-dashboards]').first().attr('data-switcher-dashboards') || ''
    if names.length > 1
      # Get names separated with comma or space
      @dashboardNames = (name.trim() for name in names.split(/[ ,]+/).filter(Boolean))

  start: (interval=60000) ->
    interval = parseInt(interval, 10)
    @maxPos = @dashboardNames.length - 1

    # Skip switching if no names defined
    if @dashboardNames.length == 0
      return

    # Take the dashboard name from that last part of the path
    pathParts = window.location.pathname.split('/')
    @curName = pathParts[pathParts.length - 1]
    @curPos = @dashboardNames.indexOf(@curName)

    # If not found, default to first
    if @curPos == -1
      @curPos = 0
      @curName = @dashboardNames[@curPos]

    # instantiate switcher controls for countdown and manual switching
    @switcherControls = new DashboardSwitcherControls(interval, @)
    @switcherControls.start() if @switcherControls.present()

    @startLoop(interval)

  startLoop: (interval) ->
    self = @
    @handle = setTimeout(() ->
      # Increase the position or reset back to zero
      self.curPos += 1
      if self.curPos > self.maxPos
        self.curPos = 0

      # Switch to new dashboard
      self.curName = self.dashboardNames[self.curPos]
      window.location.pathname = "/#{self.curName}"

    , interval)

  stopLoop: () ->
    clearTimeout @handle

  currentName: () ->
    @curName

  nextName: () ->
    @dashboardNames[@curPos + 1] || @dashboardNames[0]

  previousName: () ->
    @dashboardNames[@curPos - 1] || @dashboardNames[@dashboardNames.length - 1]


# Switches (hides and shows) elements within on list item
# <li switcher-interval="3000">
#   <div widget-1></div>
#   <div widget-2></div>
#   <div widget-3></div>
# </li>
#
# Supports optional switcher interval, defaults to 5sec
class WidgetSwitcher
  constructor: (@elements) ->
    @$elements = $(@elements)

  start: (interval=5000) ->
    self = @
    @maxPos = @$elements.length - 1;
    @curPos = Math.min(1, @maxPos)

    # Show only first at start
    self.$elements.slice(1).hide()

    # Start loop
    @handle = setInterval(()->
      # Hide all at first - then show the current and ensure it uses table-cell display type
      self.$elements.hide()
      $(self.$elements[self.curPos]).show().css('display', 'table-cell')

      # Increase the position or reset back to zero
      self.curPos += 1
      if self.curPos > self.maxPos
        self.curPos = 0

    , parseInt(interval, 10))

  stop: () ->
    clearInterval(@handle)

# Adds a countdown timer to show when next dashboard will appear
# TODO:
#   - show the name of the next dashboard
#   - add controls for manually cycling through dashboards
class DashboardSwitcherControls<commit_msg>copy dashboard switcher logic for widget switcher<commit_after># Switcher widget written by <NAME> / SC5

# Switches (reloads to another address) the dashboards in periodic manner
# <div id="container" data-switcher-interval="10000" data-switcher-dashboards="dashboard1 dashboard2">
#   <%= yield %>
# </div>
#
class DashboardSwitcher
  constructor: () ->
    @dashboardNames = []
    # Collect the dashboard names from attribute, if provided (otherwise skip switching)
    names = $('[data-switcher-dashboards]').first().attr('data-switcher-dashboards') || ''
    if names.length > 1
      # Get names separated with comma or space
      @dashboardNames = (name.trim() for name in names.split(/[ ,]+/).filter(Boolean))

  start: (interval=60000) ->
    interval = parseInt(interval, 10)
    @maxPos = @dashboardNames.length - 1

    # Skip switching if no names defined
    if @dashboardNames.length == 0
      return

    # Take the dashboard name from that last part of the path
    pathParts = window.location.pathname.split('/')
    @curName = pathParts[pathParts.length - 1]
    @curPos = @dashboardNames.indexOf(@curName)

    # If not found, default to first
    if @curPos == -1
      @curPos = 0
      @curName = @dashboardNames[@curPos]

    # instantiate switcher controls for countdown and manual switching
    @switcherControls = new DashboardSwitcherControls(interval, @)
    @switcherControls.start() if @switcherControls.present()

    @startLoop(interval)

  startLoop: (interval) ->
    self = @
    @handle = setTimeout(() ->
      # Increase the position or reset back to zero
      self.curPos += 1
      if self.curPos > self.maxPos
        self.curPos = 0

      # Switch to new dashboard
      self.curName = self.dashboardNames[self.curPos]
      window.location.pathname = "/#{self.curName}"

    , interval)

  stopLoop: () ->
    clearTimeout @handle

  currentName: () ->
    @curName

  nextName: () ->
    @dashboardNames[@curPos + 1] || @dashboardNames[0]

  previousName: () ->
    @dashboardNames[@curPos - 1] || @dashboardNames[@dashboardNames.length - 1]


# Switches (hides and shows) elements within on list item
# <li switcher-interval="3000">
#   <div widget-1></div>
#   <div widget-2></div>
#   <div widget-3></div>
# </li>
#
# Supports optional switcher interval, defaults to 5sec
class WidgetSwitcher
  constructor: (@elements) ->
    @$elements = $(@elements)


  start: (interval=5000) ->
    self = @
    @maxPos = @$elements.length - 1;
    @curPos = Math.min(1, @maxPos)

    # Show only first at start
    self.$elements.slice(1).hide()

    # instantiate switcher controls for countdown and manual switching
    @switcherControls = new WidgetSwitcherControls(interval, @)
    @switcherControls.start() if @switcherControls.present()

    @startLoop(interval)


  startLoop: (interval) ->
    self = @
    @handle = setInterval(()->
        self.next()

    , parseInt(interval, 10))


  next: () ->
    @switcherControls.resetCountdown();
    # Hide all at first - then show the current and ensure it uses table-cell display type
    @$elements.hide()
    $(@$elements[@curPos]).show().css('display', 'table-cell')

    # Increase the position or reset back to zero
    @curPos += 1
    if @curPos > @maxPos
      @curPos = 0


  prev: () ->
    self.curPos -= 1
    if self.curPos < 0
      self.curPos = self.maxPos

    self.$elements.hide()
    $(self.$elements[self.curPos]).show().css('display', 'table-cell')

  stopLoop: () ->
    clearInterval(@handle)

#  currentName: () ->
#    @curName
#
  nextName: () ->
    "to do"
    #@dashboardNames[@curPos + 1] || @dashboardNames[0]
#
#  previousName: () ->
#    @dashboardNames[@curPos - 1] || @dashboardNames[@dashboardNames.length - 1]


class WidgetSwitcherControls
  arrowContent = "&#65515;"
  stopTimerContent = "stop timer"
  startTimerContent = "start timer"

  constructor: (interval=60000, widgetSwitcher) ->
    @currentTime = parseInt(interval, 10)
    @interval = parseInt(interval, 10)
    @$elements = $('#dc-wid-switcher-controls')
    @widgetSwitcher = widgetSwitcher
    @incrementTime = 1000 # refresh every 1000 milliseconds
    @arrowContent = @$elements.data('next-widget-content') || WidgetSwitcherControls.arrowContent
    @stopTimerContent = @$elements.data('stop-widget-timer-content') || WidgetSwitcherControls.stopTimerContent
    @startTimerContent = @$elements.data('start-widget-timer-content') || WidgetSwitcherControls.startTimerContent
    @

  present: () ->
    @$elements.length

  start: () ->
    @addElements()
    @$timer = $.timer(@updateTimer, @incrementTime, true)

  addElements: () ->
    template = @$elements.find('widget-name-template')
    if template.length
      @$nextWidgetNameTemplate = template
      @$nextWidgetNameTemplate.remove()
    else
      @$nextWidgetNameTemplate = $("<widget-name-template>Next widget: $nextName in </widget-name-template>")
    @$nextWidgetNameContainer = $("<span id='dc-wid-switcher-next-name'></span>")
    @$countdown = $("<span id='dc-wid-switcher-countdown'></span>")
    @$manualSwitcher = $("<span id='dc-wid-switcher-next' class='fa fa-forward'></span>").
      html(@arrowContent).
      click () =>
        @widgetSwitcher.stopLoop()
        @widgetSwitcher.next()
        @widgetSwitcher.startLoop(@interval)
    @$switcherStopper = $("<span id='dc-wid-switcher-pause-reset' class='fa fa-pause'></span>").
      html(@stopTimerContent).
      click(@pause)
    @$elements.
      append(@$nextWidgetNameContainer).
      append(@$countdown).
      append(@$manualSwitcher).
      append(@$switcherStopper)

  formatTime: (time) ->
    time = time / 10;
    min = parseInt(time / 6000, 10)
    sec = parseInt(time / 100, 10) - (min * 60)
    "#{(if min > 0 then @pad(min, 2) else "00")}:#{@pad(sec, 2)}"

  pad: (number, length) =>
    str = "#{number}"
    while str.length < length
      str = "0#{str}"
    str

  pause: () =>
    @$timer.toggle()
    if @isRunning()
      @widgetSwitcher.stopLoop()
      @$switcherStopper.removeClass('fa-pause').addClass('fa-play').html(@startTimerContent)
    else
      @widgetSwitcher.startLoop @currentTime
      @$switcherStopper.removeClass('fa-play').addClass('fa-pause').html(@stopTimerContent)

  isRunning: () =>
    @$switcherStopper.hasClass('fa-pause')

  resetCountdown: () ->
    # Get time from form
    newTime = @interval
    if newTime > 0
      @currentTime = newTime

    # Stop and reset timer
    @$timer.stop().play(true)

  updateTimer: () =>
    # Update widget name
    @$nextWidgetNameContainer.html(
      @$nextWidgetNameTemplate.html().replace('$nextName', @widgetSwitcher.nextName())
    )
    # Output timer position
    timeString = @formatTime(@currentTime)
    @$countdown.html(timeString)

    # If timer is complete, trigger alert
    if @currentTime is 0
      #@pause()
      #@resetCountdown()
      return

    # Increment timer position
    @currentTime -= @incrementTime
    if @currentTime < 0
      @currentTime = 0
        
# Adds a countdown timer to show when next dashboard will appear
# TODO:
#   - show the name of the next dashboard
#   - add controls for manually cycling through dashboards
class DashboardSwitcherControls
{'fraction_non_alphanumeric': 0.13839071640932196, 'fraction_numerical': 0.01390620504459576, 'mean_word_length': 3.2355808285946384, 'pattern_counts': {'":': 0, '<': 40, '<?xml version=': 0, '>': 86, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '254852', 'n_tokens_mistral': 3315, 'n_tokens_neox': 3219, 'n_words': 797}