var cdb = require('cartodb.js');
var RouterModel = require('../../../new_dashboard/router/model');
var NavigationView = require('./listing/navigation_view');
var DatasetsView = require('./listing/datasets_view');
var ImportsView = require('./listing/imports_view');

/**
 *  Create listing view
 *
 *  It will display all the possibilities to select
 *  any of your current datasets or connect a new dataset.
 *
 */

module.exports = cdb.core.View.extend({

  className: 'CreateDialog-listing',

  initialize: function() {
    this.user = this.options.user;
    this.createModel = this.options.createModel;
    this.template = cdb.templates.getTemplate('new_common/views/create/create_listing');
    this.model = new cdb.core.Model({
      state: !this.user.canCreateDatasets() || this.createModel.get('type') === "map" ? 'list' : 'import', // import or list states
      collectionFetched: false
    });

    this.routerModel = new RouterModel({
      content_type: 'datasets',
      library: this.createModel.isDatasetType()
    }, {
      dashboardUrl: this.user.viewUrl().dashboard()
    });

    this.collection = new cdb.admin.Visualizations();

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

  render: function() {
    this.clearSubViews();
    this.$el.html(this.template());
    this._initViews();
    return this;
  },

  _initBinds: function() {
    this.model.bind('change:state', this._setState, this);
    this.createModel.bind('change:option', this._optionCheck, this);
    this.routerModel.bind('change', this._fetchCollection, this);
    this.add_related_model(this.routerModel);
    this.add_related_model(this.createModel);
  },

  _initViews: function() {
    // Navigation view
    var navigationView = new NavigationView({
      el: this.$('.js-navigation'),
      user: this.user,
      routerModel: this.routerModel,
      createModel: this.createModel,
      collection: this.collection,
      model: this.model
    })
    navigationView.render();
    this.addView(navigationView);

    // Listing content pane
    // - Datasets view
    // - Imports view

    // Listing pane
    this.listingPane = new cdb.ui.common.TabPane({
      el: this.$(".ListingContent")
    });
    this.addView(this.listingPane);

    // Datasets view
    var datasetsView = new DatasetsView({
      defaultUrl: this.user.viewUrl().dashboard().datasets(),
      user: this.user,
      createModel: this.createModel,
      routerModel: this.routerModel,
      collection: this.collection
    });

    datasetsView.bind('remoteSelected', function(d) {
      this.trigger('remoteSelected', d, this);
    }, this);

    datasetsView.render();
    this.listingPane.addTab('list', datasetsView);

    // Imports view
    if (this.user.canCreateDatasets()) {
      var importsView = new ImportsView({
        user: this.user,
        createModel: this.createModel
      });

      importsView.render();
      this.listingPane.addTab('import', importsView);
    }
  },

  _setState: function() {
    if (this.listingPane) {
      this.listingPane.active(this.model.get('state'));
    }
    this.createModel.setListingState(this.model.get('state'));
  },

  _optionCheck: function() {
    var option = this.createModel.getOption();
    var datasetOption = this.createModel.getDatasetsState();

    // Fetch collection if it was never fetched
    if (option === "listing" && datasetOption && !this.model.get('collectionFetched')) {
      this.model.set('collectionFetched', true);
      this._fetchCollection();
    }

    // Set sub state
    if (option === "listing") {
      this._setState();
    }
  },

  _fetchCollection: function() {
    var params = this.routerModel.attributes;

    this.collection.options.set({
      locked:         '',
      q:              params.q,
      page:           params.page || 1,
      tags:           params.tag,
      per_page:       this.collection['_TABLES_PER_PAGE'],
      only_shared:    params.shared,
      exclude_shared: !params.shared,
      only_liked:     params.liked,
      order:          'updated_at',
      type:           params.library ? 'remote' : 'table'
    });

    this.collection.fetch();
  },

});

@@ -3679,16 +3679,285 @@
 ributes;
+%0A    var types = 'table';%0A%0A    // Requesting data library items?%0A    if (params.library) %7B%0A      types = 'remote'%0A    %7D%0A    %0A    // Supporting search in data library and user datasets at the same time%0A    if (params.q %7C%7C params.tag) %7B%0A      types = 'table,remote'%0A    %7D
 %0A%0A    th
@@ -4181,21 +4181,16 @@
 ,%0A      
-only_
 shared: 
@@ -4196,46 +4196,13 @@
     
-params.shared,%0A      exclude_shared: !
+     
 para
@@ -4310,43 +4310,39 @@
     
-params.library ? 'remote' : 'table'
+'',%0A      types:          types
 %0A   

Set shared and types parameters for visualizations collection within create dialog