<commit_before>/**
 * The Item Preview widget shows a preview of items under a given folder.
 * For now, the only supported item previews are image previews.
 */

var MAX_JSON_SIZE = 2e6 /* bytes */;

var    _isSupportedItem = function (item) {
  return _isImageItem(item) || _isJSONItem(item);
};

var _isJSONItem = function (item) {
  return /\.(json)$/i.test(item.get('name'));
};

var _isImageItem = function (item) {
  return /\.(jpg|jpeg|png|gif)$/i.test(item.get('name'));
};


girder.views.ItemPreviewWidget = girder.View.extend({

  initialize: function (settings) {
    this.collection = new girder.collections.ItemCollection();

    this.collection.on('g:changed', function () {
      this.trigger('g:changed');
      this.render();
    }, this).fetch({
      folderId: settings.folderId
    });
  },

  events: {
    'click a.g-item-preview-link': function (event) {
      // TODO: trigger a 'g:itemClicked' based on cid
      // or other method rather than this lazy link proxy:
      var name = $(event.currentTarget).attr('g-item-name');
      $('.g-item-list a').filter(function(i, d) {
        return (name === $(d).text())
      }).click();
    },
  },

  render: function () {

    var supportedItems = this.collection.filter(_isSupportedItem);

    this.$el.html(girder.templates.itemPreviews({
      items: supportedItems,
      hasMore: this.collection.hasNextPage(),
      isImageItem: _isImageItem,
      isJSONItem: _isJSONItem,
      girder: girder,
    }));

    // Render any JSON files.
    supportedItems.filter(_isJSONItem).forEach(function (item) {
      var id = item.get('_id');

      // Don't process JSON files that are too big to preview.
      var size = item.get('size');
      if (size > MAX_JSON_SIZE) {
        return $('.json[data-id="' + id + '"]').text('JSON too big to preview.');
      }

      // Ajax request the JSON files to display them.
      var name = item.get('name');
      girder.restRequest({
        path: 'item/' + id + '/download',
        type: 'GET',
        error: null // don't do default error behavior (validation may fail)
      }).done(function (resp) {
        $('.json[data-id="' + id + '"]').text(JSON.stringify(resp, null, '\t'));
      }).error(function (err) {
        console.log('error',err);
      });
    })

    return this;
  }

});

girder.wrap(girder.views.HierarchyWidget, 'render', function (render) {
<commit_msg>move functions into ItemPreviewWidget prototype<commit_after>/**
 * The Item Preview widget shows a preview of items under a given folder.
 * For now, the only supported item previews are image previews.
 */


girder.views.ItemPreviewWidget = girder.View.extend({

  MAX_JSON_SIZE: 2e6 /* bytes */,

  _isSupportedItem: function (item) {
    return this._isImageItem(item) || this._isJSONItem(item);
  },

  _isJSONItem: function (item) {
    return /\.(json)$/i.test(item.get('name'));
  },

  _isImageItem: function (item) {
    return /\.(jpg|jpeg|png|gif)$/i.test(item.get('name'));
  },

  initialize: function (settings) {
    this.collection = new girder.collections.ItemCollection();

    this.collection.on('g:changed', function () {
      this.trigger('g:changed');
      this.render();
    }, this).fetch({
      folderId: settings.folderId
    });
  },

  events: {
    'click a.g-item-preview-link': function (event) {
      // TODO: trigger a 'g:itemClicked' based on cid
      // or other method rather than this lazy link proxy:
      var name = $(event.currentTarget).attr('g-item-name');
      $('.g-item-list a').filter(function(i, d) {
        return (name === $(d).text())
      }).click();
    },
  },

  render: function () {

    var supportedItems = this.collection.filter(this._isSupportedItem.bind(this));

    this.$el.html(girder.templates.itemPreviews({
      items: supportedItems,
      hasMore: this.collection.hasNextPage(),
      isImageItem: this._isImageItem,
      isJSONItem: this._isJSONItem,
      girder: girder,
    }));

    // Render any JSON files.
    supportedItems.filter(this._isJSONItem).forEach(function (item) {
      var id = item.get('_id');

      // Don't process JSON files that are too big to preview.
      var size = item.get('size');
      if (size > this.MAX_JSON_SIZE) {
        return $('.json[data-id="' + id + '"]').text('JSON too big to preview.');
      }

      // Ajax request the JSON files to display them.
      var name = item.get('name');
      girder.restRequest({
        path: 'item/' + id + '/download',
        type: 'GET',
        error: null // don't do default error behavior (validation may fail)
      }).done(function (resp) {
        $('.json[data-id="' + id + '"]').text(JSON.stringify(resp, null, '\t'));
      }).error(function (err) {
        console.log('error',err);
      });
    })

    return this;
  }

});

girder.wrap(girder.views.HierarchyWidget, 'render', function (render) {

{'fraction_non_alphanumeric': 0.1643779463004714, 'fraction_numerical': 0.000819840131174421, 'mean_word_length': 2.7947122861586315, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6340642', 'n_tokens_mistral': 1562, 'n_tokens_neox': 1525, 'n_words': 372}