<commit_before>- object = form.object.class.to_s.tableize.singularize
javascript:
  $(function() {
    var hiddenField = "#{object}_#{attribute}";
    var nextField = hiddenField + '_next';
    var prevField = hiddenField + '_prev';
    var imagesModal = hiddenField + '_images';
    var imagePicker = hiddenField + '_image_picker';
    var imageRemove = hiddenField + '_image_remove';
    var imagePreview = hiddenField + '_image_preview';
    var page = 1;

    $('#' + imagePicker).click(function(event) {
      event.preventDefault();
      $('#' + imagesModal).modal('show');
      getImages();
    });

    $('#' + imageRemove).click(function(event) {
      event.preventDefault();
      $('#' + hiddenField).val('');
    });

    function getImages() {
      $.getJSON('/admin/images', { page: page, per_page: 64 }, function(data) {
        var items = [];
        if(data.images.length == 0) {
          items.push('<li>No results found</li>');
        }
        if(data.current_page > 1 || data.current_page < data.total_pages) {
          items.push('<li><div class="btn-group">');
          items.push(arrowButton(prevField, 'Previous', 'left', data.current_page > 1));
          items.push(arrowButton(nextField, 'Next', 'right', data.current_page < data.total_pages));
          items.push('</div></li>');
        }
        $.each(data.images, function(key, image) {
          var i = '<li class="picker-image"><a href="#" data-id="' + image.id + '" id="image-' + image.id + '"><img src="/up/images/' + image.id + '/constrained.192x144.jpg" title="' + image.name + '"></a></li>';
          items.push(i);
        });
        $('#' + imagesModal + ' .results-body').replaceWith(
          $('<ul/>', {
            'class': 'clearfix results-body',
            html: items.join('')
          })
        );
        $('#' + nextField).click(function() {
          page += 1;
          getImages();
          setTimeout(function() { $('#' + imagesModal).modal('show') }, 500);
        });
        $('#' + prevField).click(function() {
          page -= 1;
          getImages();
          setTimeout(function() { $('#' + imagesModal).modal('show') }, 500);
        });
        $('#' + imagesModal + ' .results-body a').click(function() {
          $('#' + hiddenField).val($(this).data('id'));
          $('#' + imagesModal).modal('hide');
          $('#' + imagePreview + ' img').attr('src', $(this).find('img').attr('src'));
        });
      });
    }

    function arrowButton(id, title, icon, active) {
      var items = [];
      items.push('<a');
      if(active) {
        items.push(' id="' + id + '"');
      }
      items.push(' class="btn btn-default');
      if(!active) {
        items.push(' disabled');
      }
      items.push('" title="' + title + '">');
      items.push('<span class="glyphicon glyphicon-arrow-' + icon + '">');
      items.push('</span></a>');
      return items.join('');
    }
  });
.modal.fade.modal-picker id="#{object}_#{attribute}_images" role="dialog" aria-labelledby="Image Picker" aria-hidden="true"
  .modal-dialog.modal-lg
    .modal-content
      .modal-header
        button.close type="button" data-dismiss="modal" aria-label="Close"
          span aria-hidden="true" &times;
        h4.modal-title Images
      .modal-body
        span.results-body

.form-action
  = form.hidden_field(attribute)
  .image-preview id="#{object}_#{attribute}_image_preview"
    - image_url = Image.find_by(id: form.object.send(attribute)).try(:url)
    - image_url ||= '#'
    img src="#{image_url}"
  p
    .btn-group
      button.btn.btn-default id="#{object}_#{attribute}_image_picker" Pick
      button.btn.btn-default id="#{object}_#{attribute}_image_remove" Remove<commit_msg>Use image-missing.png instead (# causes a new GET on current page)<commit_after>- object = form.object.class.to_s.tableize.singularize
javascript:
  $(function() {
    var hiddenField = "#{object}_#{attribute}";
    var nextField = hiddenField + '_next';
    var prevField = hiddenField + '_prev';
    var imagesModal = hiddenField + '_images';
    var imagePicker = hiddenField + '_image_picker';
    var imageRemove = hiddenField + '_image_remove';
    var imagePreview = hiddenField + '_image_preview';
    var page = 1;

    $('#' + imagePicker).click(function(event) {
      event.preventDefault();
      $('#' + imagesModal).modal('show');
      getImages();
    });

    $('#' + imageRemove).click(function(event) {
      event.preventDefault();
      $('#' + hiddenField).val('');
    });

    function getImages() {
      $.getJSON('/admin/images', { page: page, per_page: 64 }, function(data) {
        var items = [];
        if(data.images.length == 0) {
          items.push('<li>No results found</li>');
        }
        if(data.current_page > 1 || data.current_page < data.total_pages) {
          items.push('<li><div class="btn-group">');
          items.push(arrowButton(prevField, 'Previous', 'left', data.current_page > 1));
          items.push(arrowButton(nextField, 'Next', 'right', data.current_page < data.total_pages));
          items.push('</div></li>');
        }
        $.each(data.images, function(key, image) {
          var i = '<li class="picker-image"><a href="#" data-id="' + image.id + '" id="image-' + image.id + '"><img src="/up/images/' + image.id + '/constrained.192x144.jpg" title="' + image.name + '"></a></li>';
          items.push(i);
        });
        $('#' + imagesModal + ' .results-body').replaceWith(
          $('<ul/>', {
            'class': 'clearfix results-body',
            html: items.join('')
          })
        );
        $('#' + nextField).click(function() {
          page += 1;
          getImages();
          setTimeout(function() { $('#' + imagesModal).modal('show') }, 500);
        });
        $('#' + prevField).click(function() {
          page -= 1;
          getImages();
          setTimeout(function() { $('#' + imagesModal).modal('show') }, 500);
        });
        $('#' + imagesModal + ' .results-body a').click(function() {
          $('#' + hiddenField).val($(this).data('id'));
          $('#' + imagesModal).modal('hide');
          $('#' + imagePreview + ' img').attr('src', $(this).find('img').attr('src'));
        });
      });
    }

    function arrowButton(id, title, icon, active) {
      var items = [];
      items.push('<a');
      if(active) {
        items.push(' id="' + id + '"');
      }
      items.push(' class="btn btn-default');
      if(!active) {
        items.push(' disabled');
      }
      items.push('" title="' + title + '">');
      items.push('<span class="glyphicon glyphicon-arrow-' + icon + '">');
      items.push('</span></a>');
      return items.join('');
    }
  });
.modal.fade.modal-picker id="#{object}_#{attribute}_images" role="dialog" aria-labelledby="Image Picker" aria-hidden="true"
  .modal-dialog.modal-lg
    .modal-content
      .modal-header
        button.close type="button" data-dismiss="modal" aria-label="Close"
          span aria-hidden="true" &times;
        h4.modal-title Images
      .modal-body
        span.results-body

.form-action
  = form.hidden_field(attribute)
  .image-preview id="#{object}_#{attribute}_image_preview"
    - image_url = Image.find_by(id: form.object.send(attribute)).try(:url)
    - image_url ||= '/images/image-missing.png'
    img src="#{image_url}"
  p
    .btn-group
      button.btn.btn-default id="#{object}_#{attribute}_image_picker" Pick
      button.btn.btn-default id="#{object}_#{attribute}_image_remove" Remove
{'fraction_non_alphanumeric': 0.20257875847401302, 'fraction_numerical': 0.005582879170543666, 'mean_word_length': 2.5777460770328102, 'pattern_counts': {'":': 0, '<': 39, '<?xml version=': 0, '>': 39, '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': '4255329', 'n_tokens_mistral': 2330, 'n_tokens_neox': 2293, 'n_words': 389}