<commit_before>var $ = require('jquery');
var _ = require('underscore');
var CoreView = require('backbone/core-view');
var Backbone = require('backbone');

var Utils = require('../../../../../../helpers/utils');
var MakiIcons = require('../assets/maki-icons');
var UserAssetsView = require('./user-assets-tab');
var OrganizationAssetsListView = require('./organization-assets-list-view');
var OrganizationAssetsCollection = require('../../../../../../data/organization-assets-collection');
var UploadAssetsView = require('./upload-assets-tab');

var checkAndBuildOpts = require('../../../../../../helpers/required-opts');
var AssetsCollection = require('../../../../../../data/assets-collection');
var createTextLabelsTabPane = require('../../../../../tab-pane/create-text-labels-tab-pane');
var ScrollView = require('../../../../../scroll/scroll-view');
var AssetsListView = require('./assets-list-view');
var loadingView = require('../../../../../loading/render-loading');

var ErrorView = require('../../../../../error/error-view');
var errorTemplate = require('./upload-assets-error.tpl');
var errorParser = require('../../../../../../helpers/error-parser');

var template = require('./assets-view.tpl');
var tabPaneTemplate = require('./tab-pane-template.tpl');

var KIND = 'custom-marker';

var REQUIRED_OPTS = [
  'modalModel',
  'configModel',
  'userModel'
];

module.exports = CoreView.extend({
  className: 'Dialog-content Dialog-content--expanded',

  events: {
    'click .js-add': '_onSetImage',
    'click .js-upload': '_initUpload',
    'change .js-fileInput': '_onFileSelected',
    'click .js-back': '_onClickBack'
  },

  initialize: function (opts) {
    checkAndBuildOpts(opts, REQUIRED_OPTS, this);

    this._initModels();
    this._initBinds();
  },

  render: function () {
    this.clearSubViews();
    this.$el.html(template({
      disclaimer: MakiIcons.disclaimer
    }));

    if (this._hasFetchedAllCollections()) {
      this._initTabPane();
    } else if (this._isLoading()) {
      this._renderLoading();
    } else if (this._hasError()) {
      this._renderError();
    }
    return this;
  },

  _hasFetchedAllCollections: function () {
    return this._stateModel.get('status') === 'show';
  },

  _initModels: function () {
    var self = this;

    this._selectedAsset = new Backbone.Model({
      url: this.model.get('image')
    });

    this.add_related_model(this._selectedAsset);

    this._stateModel = new Backbone.Model({
      status: 'loading',
      modalEnabled: false,
      uploads: 0
    });

    this.add_related_model(this._stateModel);

    this._uploadAssetCollection = new AssetsCollection(null, {
      configModel: this._configModel,
      userModel: this._userModel
    });

    this._assetCollections = [];

    this._userAssetCollection = new AssetsCollection(null, {
      configModel: this._configModel,
      userModel: this._userModel
    });

    this._assetCollections.push(this._userAssetCollection);

    if (this._userModel.isInsideOrg()) {
      this._organizationAssetCollection = new OrganizationAssetsCollection(null, {
        configModel: this._configModel,
        orgId: this._userModel.getOrganization().get('id')
      });
      this._assetCollections.push(this._organizationAssetCollection);
    }

    var onFetchAssetCollections = _.invoke(this._assetCollections, 'fetch');
    $.when.apply($, onFetchAssetCollections)
      .then(function () {
        self._stateModel.set('status', 'show');
      }, function (model, response) {
        self._setError(response);
      });
  },

  _initBinds: function () {
    this.listenTo(this._selectedAsset, 'change:url', this._onChangeSelectedAsset);
    this.listenTo(this._stateModel, 'change:status', this.render);
    this.listenTo(this._stateModel, 'change:modalEnabled', this._onChangeSetButton);
  },

  _onClickBack: function (e) {
    this.killEvent(e);
    this._stateModel.set('status', 'show');
    this._assetsTabPaneView.setSelectedTabPaneByName('upload-file');
  },

  _upload: function (data) {
    this._uploadAssetCollection.create(data, {
      beforeSend: this._beforeAssetUpload.bind(this),
      success: this._onAssetUploaded.bind(this),
      error: this._onAssetUploadError.bind(this),
      complete: this._onAssetUploadComplete.bind(this)
    });
  },

  _renderError: function () {
    this._hideDisclaimer();

    this.$('.js-body').html(
      new ErrorView({
        title: Utils.capitalize(this._stateModel.get('error_message')),
        desc: _t('components.modals.assets-picker.error-desc'),
        template: errorTemplate
      }).render().el<commit_msg>Removes unused collection<commit_after>var $ = require('jquery');
var _ = require('underscore');
var CoreView = require('backbone/core-view');
var Backbone = require('backbone');

var Utils = require('../../../../../../helpers/utils');
var MakiIcons = require('../assets/maki-icons');
var UserAssetsView = require('./user-assets-tab');
var OrganizationAssetsListView = require('./organization-assets-list-view');
var OrganizationAssetsCollection = require('../../../../../../data/organization-assets-collection');
var UploadAssetsView = require('./upload-assets-tab');

var checkAndBuildOpts = require('../../../../../../helpers/required-opts');
var AssetsCollection = require('../../../../../../data/assets-collection');
var createTextLabelsTabPane = require('../../../../../tab-pane/create-text-labels-tab-pane');
var ScrollView = require('../../../../../scroll/scroll-view');
var AssetsListView = require('./assets-list-view');
var loadingView = require('../../../../../loading/render-loading');

var ErrorView = require('../../../../../error/error-view');
var errorTemplate = require('./upload-assets-error.tpl');
var errorParser = require('../../../../../../helpers/error-parser');

var template = require('./assets-view.tpl');
var tabPaneTemplate = require('./tab-pane-template.tpl');

var KIND = 'custom-marker';

var REQUIRED_OPTS = [
  'modalModel',
  'configModel',
  'userModel'
];

module.exports = CoreView.extend({
  className: 'Dialog-content Dialog-content--expanded',

  events: {
    'click .js-add': '_onSetImage',
    'click .js-upload': '_initUpload',
    'change .js-fileInput': '_onFileSelected',
    'click .js-back': '_onClickBack'
  },

  initialize: function (opts) {
    checkAndBuildOpts(opts, REQUIRED_OPTS, this);

    this._initModels();
    this._initBinds();
  },

  render: function () {
    this.clearSubViews();
    this.$el.html(template({
      disclaimer: MakiIcons.disclaimer
    }));

    if (this._hasFetchedAllCollections()) {
      this._initTabPane();
    } else if (this._isLoading()) {
      this._renderLoading();
    } else if (this._hasError()) {
      this._renderError();
    }
    return this;
  },

  _hasFetchedAllCollections: function () {
    return this._stateModel.get('status') === 'show';
  },

  _initModels: function () {
    var self = this;

    this._selectedAsset = new Backbone.Model({
      url: this.model.get('image')
    });

    this.add_related_model(this._selectedAsset);

    this._stateModel = new Backbone.Model({
      status: 'loading',
      modalEnabled: false,
      uploads: 0
    });

    this.add_related_model(this._stateModel);

    this._assetCollections = [];

    this._userAssetCollection = new AssetsCollection(null, {
      configModel: this._configModel,
      userModel: this._userModel
    });

    this._assetCollections.push(this._userAssetCollection);

    if (this._userModel.isInsideOrg()) {
      this._organizationAssetCollection = new OrganizationAssetsCollection(null, {
        configModel: this._configModel,
        orgId: this._userModel.getOrganization().get('id')
      });
      this._assetCollections.push(this._organizationAssetCollection);
    }

    var onFetchAssetCollections = _.invoke(this._assetCollections, 'fetch');
    $.when.apply($, onFetchAssetCollections)
      .then(function () {
        self._stateModel.set('status', 'show');
      }, function (model, response) {
        self._setError(response);
      });
  },

  _initBinds: function () {
    this.listenTo(this._selectedAsset, 'change:url', this._onChangeSelectedAsset);
    this.listenTo(this._stateModel, 'change:status', this.render);
    this.listenTo(this._stateModel, 'change:modalEnabled', this._onChangeSetButton);
  },

  _onClickBack: function (e) {
    this.killEvent(e);
    this._stateModel.set('status', 'show');
    this._assetsTabPaneView.setSelectedTabPaneByName('upload-file');
  },

  _upload: function (data) {
    this._userAssetCollection.create(data, {
      beforeSend: this._beforeAssetUpload.bind(this),
      success: this._onAssetUploaded.bind(this),
      error: this._onAssetUploadError.bind(this),
      complete: this._onAssetUploadComplete.bind(this)
    });
  },

  _renderError: function () {
    this._hideDisclaimer();

    this.$('.js-body').html(
      new ErrorView({
        title: Utils.capitalize(this._stateModel.get('error_message')),
        desc: _t('components.modals.assets-picker.error-desc'),
        template: errorTemplate
      }).render().el
{'fraction_non_alphanumeric': 0.1776727832106362, 'fraction_numerical': 0.0002197560707614548, 'mean_word_length': 3.3363506431634113, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '2899317', 'n_tokens_mistral': 2717, 'n_tokens_neox': 2663, 'n_words': 438}