<commit_before>class LabbookController
  ACTION_BTN_SEL = '.lb-action-btn'
  OPEN_ALBUM_SEL = '.lb-album-btn'
  DIALOG_SEL = '.lb-dialog-content'
  INTERACTIVE_SEL = '#interactive_' # incomplete, we need to provide ID
  WAIT_MSG_SEL = '.lb-wait-msg'

  constructor: (labbookElement) ->
    @$element = $(labbookElement)
    @$openAlbumBtn = @$element.find(OPEN_ALBUM_SEL)
    @$actionBtn = @$element.find(ACTION_BTN_SEL)
    @$dialog = @$element.find(DIALOG_SEL)
    @$iframe = @$dialog.find('iframe')

    @baseUrl = @$element.data('labbook-url')
    @laraUpdateUrl = @$element.data('lara-update-url')
    @albumId = @$element.data('labbook-album-id')

    @isUpload = @$element.data('is-upload')

    @implementsIFramePhone = false
    @hasUnsavedState = false

    unless @isUpload
      intId = @$element.data('interactive-id')
      @interactiveSel = if intId then "#{INTERACTIVE_SEL}#{intId}" else null

    @$dialog.dialog({
      autoOpen: false,
      width: Math.min(window.innerWidth - 10, 750),
      height: Math.min(window.innerHeight - 10, 750),
      title: 'Labbook',
      dialogClass: 'lb-dialog',
      modal: true,
      close: =>
        @onDialogClose()
      beforeClose: =>
        @onDialogClosing()
    })

    @registerListeners()

  registerListeners: ->
    @$openAlbumBtn.on 'click', =>
      @openAlbum()

    @$actionBtn.on 'click', =>
      if @isUpload
        @uploadImage()
      else
        @takeSnapshot()

  openAlbum: ->
    @setIframeUrl("#{@baseUrl}/albums?#{@albumId}")
    @showDialog()

  uploadImage: ->
    @setIframeUrl("#{@baseUrl}/albums?#{@albumId}&todo=new")
    @showDialog()

  takeSnapshot: ->
    unless @interactiveSel
      @showError(t('MISSING_INTERACTIVE'))
      return

    @startWaiting(t('PLEASE_WAIT_TAKING_SNAPSHOT'))
    @$iframe.hide()
    @showDialog()
    Shutterbug.snapshot({
      selector: @interactiveSel,
      done: (imgSrc) =>
        @stopWaiting()
        @setIframeUrl("#{@baseUrl}/albums?#{@albumId}&todo=create&source_url=#{imgSrc}")
      fail: =>
        @stopWaiting()
        @showError(t('SNAPSHOT_FAILED'))
    })

  showDialog: ->
    @$dialog.dialog('open')

  onDialogClosing: (event) ->
    if @implementsIFramePhone and @hasUnsavedState
      confirm("You have unsaved Labbook content.  Are you sure you want to close?")
    else
      true

  onDialogClose: ->
    # Notify LARA that dialog has been closed, so album has been (probably!) updated.
    # That information is needed to support Portal reports and run with collaborators.
    saveIndicator = SaveIndicator.instance()
    saveIndicator.showSaving()
    $.ajax({
      type: 'PUT',
      url: @laraUpdateUrl,
      success: ->
        saveIndicator.showSaved()
      error: (jqxhr)->
        if jqxhr.status is 401
          saveIndicator.showUnauthorized()
          $(document).trigger 'unauthorized'
        else
          saveIndicator.showSaveFailed()
    })

  setIframeUrl: (newUrl) ->
    @hasUnsavedState = false
    @implementsIFramePhone = false

    @startWaiting(t('LOADING_LABBOOK'))
    @$iframe.hide()
    @$iframe.attr('src', newUrl)
    @$iframe.one 'load', =>
      @phone?.disconnect()
      @phone = IframePhoneManager.getPhone(@$iframe[0], => @phoneAnswered())
      @stopWaiting()
      @$iframe.fadeIn()

  phoneAnswered: ->
    @implementsIFramePhone = true
    @phone.addListener 'interactiveState', (state) =>
      @hasUnsavedState = state isnt "nochange"<commit_msg>Changed Lara close protocol to use a canClose message<commit_after>class LabbookController
  ACTION_BTN_SEL = '.lb-action-btn'
  OPEN_ALBUM_SEL = '.lb-album-btn'
  DIALOG_SEL = '.lb-dialog-content'
  INTERACTIVE_SEL = '#interactive_' # incomplete, we need to provide ID
  WAIT_MSG_SEL = '.lb-wait-msg'

  constructor: (labbookElement) ->
    @$element = $(labbookElement)
    @$openAlbumBtn = @$element.find(OPEN_ALBUM_SEL)
    @$actionBtn = @$element.find(ACTION_BTN_SEL)
    @$dialog = @$element.find(DIALOG_SEL)
    @$iframe = @$dialog.find('iframe')

    @baseUrl = @$element.data('labbook-url')
    @laraUpdateUrl = @$element.data('lara-update-url')
    @albumId = @$element.data('labbook-album-id')

    @isUpload = @$element.data('is-upload')

    @implementsIFramePhone = false
    @canClose = false

    unless @isUpload
      intId = @$element.data('interactive-id')
      @interactiveSel = if intId then "#{INTERACTIVE_SEL}#{intId}" else null

    @$dialog.dialog({
      autoOpen: false,
      width: Math.min(window.innerWidth - 10, 750),
      height: Math.min(window.innerHeight - 10, 750),
      title: 'Labbook',
      dialogClass: 'lb-dialog',
      modal: true,
      close: =>
        @onDialogClose()
      beforeClose: =>
        @onDialogClosing()
    })

    @registerListeners()

  registerListeners: ->
    @$openAlbumBtn.on 'click', =>
      @openAlbum()

    @$actionBtn.on 'click', =>
      if @isUpload
        @uploadImage()
      else
        @takeSnapshot()

  openAlbum: ->
    @setIframeUrl("#{@baseUrl}/albums?#{@albumId}")
    @showDialog()

  uploadImage: ->
    @setIframeUrl("#{@baseUrl}/albums?#{@albumId}&todo=new")
    @showDialog()

  takeSnapshot: ->
    unless @interactiveSel
      @showError(t('MISSING_INTERACTIVE'))
      return

    @startWaiting(t('PLEASE_WAIT_TAKING_SNAPSHOT'))
    @$iframe.hide()
    @showDialog()
    Shutterbug.snapshot({
      selector: @interactiveSel,
      done: (imgSrc) =>
        @stopWaiting()
        @setIframeUrl("#{@baseUrl}/albums?#{@albumId}&todo=create&source_url=#{imgSrc}")
      fail: =>
        @stopWaiting()
        @showError(t('SNAPSHOT_FAILED'))
    })

  showDialog: ->
    @$dialog.dialog('open')

  onDialogClosing: (event) ->
    return true if not @implementsIFramePhone or @canClose
    @phone.post('canClose')
    return false

  onDialogClose: ->
    # Notify LARA that dialog has been closed, so album has been (probably!) updated.
    # That information is needed to support Portal reports and run with collaborators.
    saveIndicator = SaveIndicator.instance()
    saveIndicator.showSaving()
    $.ajax({
      type: 'PUT',
      url: @laraUpdateUrl,
      success: ->
        saveIndicator.showSaved()
      error: (jqxhr)->
        if jqxhr.status is 401
          saveIndicator.showUnauthorized()
          $(document).trigger 'unauthorized'
        else
          saveIndicator.showSaveFailed()
    })

  setIframeUrl: (newUrl) ->
    @canClose = false
    @implementsIFramePhone = false

    @startWaiting(t('LOADING_LABBOOK'))
    @$iframe.hide()
    @$iframe.attr('src', newUrl)
    @$iframe.one 'load', =>
      @setupPhone()
      @stopWaiting()
      @$iframe.fadeIn()

  setupPhone: ->
    @phone = IframePhoneManager.getPhone @$iframe[0], =>
    @phone.addListener 'close', =>
      @canClose = true
      @$dialog.dialog('close')
    @phone.addListener 'connected', (connected) =>
      @implementsIFramePhone = connected
{'fraction_non_alphanumeric': 0.14745541539799914, 'fraction_numerical': 0.0040597361171523854, 'mean_word_length': 3.0457478005865104, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 46, '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': '2632481', 'n_tokens_mistral': 2371, 'n_tokens_neox': 2294, 'n_words': 414}