text: // Licensed under the Apache License, Version 2.0 (the 'License'); you may not
  // use this file except in compliance with the License. You may obtain a copy of
  // the License at
  //
  //   http://www.apache.org/licenses/LICENSE-2.0
  //
  // Unless required by applicable law or agreed to in writing, software
  // distributed under the License is distributed on an 'AS IS' BASIS, WITHOUT
  // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  // License for the specific language governing permissions and limitations under
  // the License.
  
  define([
    'app',
    'api',
  
    // Modules
    "addons/documents/helpers",
    'addons/documents/shared-routes',
    'addons/documents/views',
    'addons/documents/index-editor/components.react',
    'addons/documents/index-editor/actions',
    'addons/databases/base',
    'addons/fauxton/components',
    'addons/documents/index-results/stores',
    'addons/documents/index-results/actions',
    'addons/documents/index-results/index-results.components.react',
    'addons/documents/pagination/pagination.react',
    'addons/documents/header/header.react',
    'addons/documents/header/header.actions',
  ],
  
  function (app, FauxtonAPI, Helpers, BaseRoute, Documents, IndexEditorComponents, ActionsIndexEditor,
            Databases, Components, IndexResultsStores, IndexResultsActions,
            IndexResultsComponents, ReactPagination, ReactHeader, ReactHeaderActions) {
  
  
    var IndexEditorAndResults = BaseRoute.extend({
      layout: 'two_pane',
      routes: {
        'database/:database/new_view': 'newViewEditor',
        'database/:database/new_view/:designDoc': 'newViewEditor',
        'database/:database/_design/:ddoc/_view/:view': {
          route: 'viewFn',
          roles: ['fx_loggedIn']
        }
      },
  
      initialize: function (route, masterLayout, options) {
        var databaseName = options[0];
  
        this.databaseName = databaseName;
        this.database = new Databases.Model({id: databaseName});
        this.allDatabases = new Databases.List();
        this.createDesignDocsCollection();
      },
  
      establish: function () {
        return [
          this.allDatabases.fetchOnce()
        ];
      },
  
      viewFn: function (databaseName, ddoc, viewName) {
        var params = this.createParams(),
            urlParams = params.urlParams,
            docParams = params.docParams,
            decodeDdoc = decodeURIComponent(ddoc);
  
        this.rightHeader = this.setView('#right-header', new Documents.Views.RightAllDocsHeader({
          database: this.database
        }));
  
        var url = FauxtonAPI.urls('allDocs', 'app', this.database.safeID(), '?limit=' + FauxtonAPI.constants.DATABASES.DOCUMENT_LIMIT);
        this.breadcrumbs = this.setView('#breadcrumbs', new Components.Breadcrumbs({
          toggleDisabled: true,
          crumbs: [
            {'type': 'back', 'link': Helpers.getPreviousPage(this.database)},
            {'name': this.database.id, 'link': url }
          ]
        }));
  
        viewName = viewName.replace(/\?.*$/, '');
  
        this.setComponent('#footer', ReactPagination.Footer);
  
        this.indexedDocs = new Documents.IndexCollection(null, {
          database: this.database,
          design: decodeDdoc,
          view: viewName,
          params: docParams,
          paging: {
            pageSize: IndexResultsStores.indexResultsStore.getPerPage()
          }
        });
  
        ActionsIndexEditor.clearIndex();
  
        IndexResultsActions.newResultsList({
          collection: this.indexedDocs,
          bulkCollection: new Documents.BulkDeleteDocCollection([], { databaseId: this.database.safeID() }),
        });
  
        ActionsIndexEditor.fetchDesignDocsBeforeEdit({
          viewName: viewName,
          newView: false,
          database: this.database,
          designDocs: this.designDocs,
          designDocId: '_design/' + decodeDdoc
        });
  
        this.setComponent('#react-headerbar', ReactHeader.BulkDocumentHeaderController, {showIncludeAllDocs: true});
  
        this.setComponent('#left-content', IndexEditorComponents.EditorController);
        this.setComponent('#dashboard-lower-content', IndexResultsComponents.List);
  
        this.apiUrl = function () {
          return [this.indexedDocs.urlRef('apiurl'), FauxtonAPI.constants.DOC_URLS.GENERAL];
        };
  
        this.showQueryOptions(urlParams, ddoc, viewName);
      },
  
      newViewEditor: function (database, _designDoc) {
        var newDesignDoc = true;
        var designDoc = 'new-doc';
  
        if (_designDoc) {
          designDoc = '_design/' + _designDoc;
          newDesignDoc = false;
        }
  
        var url = FauxtonAPI.urls('allDocs', 'app', this.database.safeID(), '?limit=' + FauxtonAPI.constants.DATABASES.DOCUMENT_LIMIT);
        this.breadcrumbs = this.setView('#breadcrumbs', new Components.Breadcrumbs({
          toggleDisabled: true,
          crumbs: [
            { type: 'back', link: Helpers.getPreviousPage(this.database) },
            { name: 'Create Index', link: url }
          ]
        }));
  
        ActionsIndexEditor.fetchDesignDocsBeforeEdit({
          viewName: 'new-view',
          newView: true,
          database: this.database,
          designDocs: this.designDocs,
          designDocId: designDoc,
          newDesignDoc: newDesignDoc
        });
  
        this.setComponent('#left-content', IndexEditorComponents.EditorController);
        this.setComponent('#dashboard-lower-content', IndexResultsComponents.List);
  
        IndexResultsActions.clearResults();
        IndexResultsActions.resultsListReset();
      }
  
    });
  
    return IndexEditorAndResults;
  });
  