<commit_before>@Mercury.modal = (url, options = {}) ->
  instance = new Mercury.Modal(url, options)
  instance.show()
  return instance


class @Mercury.Modal

  constructor: (@url, @options = {}) ->


  show: (url = null, options = null) ->
    @url = url || @url
    @options = options || @options
    @options.minWidth ||= 400
    @options.ujsHandling = true unless @options.ujsHandling == false

    Mercury.trigger('focus:window')
    @initializeModal()
    if @visible then @update() else @appear()
    if @options.content
      setTimeout((=> @loadContent(@options.content)), 500)


  initializeModal: ->
    return if @initialized
    @build()
    @bindEvents()
    @initialized = true


  build: ->
    @element = jQuery('<div>', {class: 'mercury-modal loading'})
    @element.html('<h1 class="mercury-modal-title"><span></span><a>&times;</a></h1>')
    @element.append('<div class="mercury-modal-content-container"><div class="mercury-modal-content"></div></div>')

    @overlay = jQuery('<div>', {class: 'mercury-modal-overlay'})

    @titleElement = @element.find('.mercury-modal-title')
    @contentContainerElement = @element.find('.mercury-modal-content-container')

    @contentElement = @element.find('.mercury-modal-content')

    @element.appendTo(jQuery(@options.appendTo).get(0) ? 'body')
    @overlay.appendTo(jQuery(@options.appendTo).get(0) ? 'body')


  bindEvents: ->
    Mercury.on 'refresh', => @resize(true)
    Mercury.on 'resize', => @position()

    @overlay.on 'click', =><commit_msg>Reuse .mercury-modal and .mercury-modal-overlay divs instead of creating new ones every time a modal is opened.<commit_after>@Mercury.modal = (url, options = {}) ->
  instance = new Mercury.Modal(url, options)
  instance.show()
  return instance


class @Mercury.Modal

  constructor: (@url, @options = {}) ->


  show: (url = null, options = null) ->
    @url = url || @url
    @options = options || @options
    @options.minWidth ||= 400
    @options.ujsHandling = true unless @options.ujsHandling == false

    Mercury.trigger('focus:window')
    @initializeModal()
    if @visible then @update() else @appear()
    if @options.content
      setTimeout((=> @loadContent(@options.content)), 500)


  initializeModal: ->
    return if @initialized
    @build()
    @bindEvents()
    @initialized = true


  build: ->
    @element = jQuery('.mercury-modal')
    @overlay = jQuery('.mercury-modal-overlay')
    
    unless @element.get(0) && @overlay.get(0)
      @element = jQuery('<div>', {class: 'mercury-modal loading'})
      @element.html('<h1 class="mercury-modal-title"><span></span><a>&times;</a></h1>')
      @element.append('<div class="mercury-modal-content-container"><div class="mercury-modal-content"></div></div>')
      
      @overlay = jQuery('<div>', {class: 'mercury-modal-overlay'})
      
      @element.appendTo(jQuery(@options.appendTo).get(0) ? 'body')
      @overlay.appendTo(jQuery(@options.appendTo).get(0) ? 'body')
    
    @titleElement = @element.find('.mercury-modal-title')
    @contentContainerElement = @element.find('.mercury-modal-content-container')
    @contentElement = @element.find('.mercury-modal-content')


  bindEvents: ->
    Mercury.on 'refresh', => @resize(true)
    Mercury.on 'resize', => @position()

    @overlay.on 'click', =>
{'fraction_non_alphanumeric': 0.18344451182534868, 'fraction_numerical': 0.0066707095209217705, 'mean_word_length': 3.7061340941512126, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 47, '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': '3861904', 'n_tokens_mistral': 1056, 'n_tokens_neox': 1059, 'n_words': 198}