Github user garrensmith commented on a diff in the pull request: https://github.com/apache/couchdb-fauxton/pull/106#discussion_r19393549 --- Diff: app/addons/indexes/views.js --- @@ -0,0 +1,641 @@ +// 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', + 'addons/fauxton/components', + + 'addons/documents/resources', + 'addons/databases/resources', + 'addons/pouchdb/base', + //views + 'addons/documents/views-queryoptions', + // Libs + 'addons/fauxton/resizeColumns', + + // Plugins + 'plugins/beautify', + 'plugins/prettify' +], + +function (app, FauxtonAPI, Components, Documents, Databases, pouchdb, + QueryOptions, resizeColumns, beautify, prettify) { + + var Views = {}; + + Views.RightHeader = FauxtonAPI.View.extend({ + className: 'header-right', + template: 'addons/indexes/templates/header_right', + + initialize: function (options) { + _.bindAll(this); + this.database = options.database; + this.title = options.title; + this.api = options.api; + this.endpoint = options.endpoint; + this.documentation = options.documentation; + FauxtonAPI.Events.on('advancedOptions:updateView', this.updateView); + }, + + updateApiUrl: function (api) { + //this will update the api bar when the route changes + //you can find the method that updates it in components.js Components.ApiBar() + this.apiBar && this.apiBar.update(api); + }, + + beforeRender: function () { + this.apiBar = this.insertView('#header-api-bar', new Components.ApiBar({ + endpoint: this.endpoint, + documentation: this.documentation + })); + + this.advancedOptions = this.insertView('#query-options', new QueryOptions.QueryOptionsTray({ + database: this.database, + viewName: this.viewName, + ddocName: this.model.id, + hasReduce: this.hasReduce(), + showStale: true + })); + }, + + cleanup: function () { + FauxtonAPI.Events.unbind('advancedOptions:updateView'); + }, + + hasReduce: function () { + + }, + + updateView: function (event, paramInfo) { + event.preventDefault(); + + var errorParams = paramInfo.errorParams, + params = paramInfo.params; + + if (_.any(errorParams)) { + _.map(errorParams, function (param) { + return FauxtonAPI.addNotification({ + msg: 'JSON Parse Error on field: '+param.name, + type: 'error', + clear: true + }); + }); + FauxtonAPI.addNotification({ + msg: 'Make sure that strings are properly quoted and any other values are valid JSON structures', + type: 'warning', + clear: true + }); + + return false; + } + + var fragment = window.location.hash.replace(/\?.*$/, ''); + if (!_.isEmpty(params)) { + fragment = fragment + '?' + $.param(params); + } + + FauxtonAPI.navigate(fragment, {trigger: false}); + FauxtonAPI.triggerRouteEvent('updateAllDocs', {ddoc: this.ddocID, view: this.viewName}); + } + }); + + Views.PreviewScreen = FauxtonAPI.View.extend({ + template: 'addons/indexes/templates/preview_screen', + className: 'watermark-logo', + + initialize: function () { + this.listenTo(FauxtonAPI.Events, 'indexes:newView', this.changeText); + }, + + changeText: function () { + this.$('.js-status').text('Index is building'); + } + }); + + + Views.Row = FauxtonAPI.View.extend({ + template: 'addons/indexes/templates/index_row_docular', + className: 'doc-row', + events: { + 'click button.delete': 'destroy' + }, + + destroy: function (event) { + event.preventDefault(); + + window.alert('Cannot delete a document generated from a view.'); + }, + + beforeRender: function () { + var newLinks = [{ + links: [{ + title: 'JSON', + icon: 'fonticon-json' + }] + }]; + + this.insertView('.view-doc-menu', new Components.MenuDropDown({ + icon: 'fonticon-drop-down-dots', + links: newLinks + })); + }, + + serialize: function() { + return { + docID: this.model.get('id'), + doc: this.model, + url: this.model.url('app') + }; + } + }); + + Views.IndexCore = FauxtonAPI.View.extend({ + initialize: function (options) { + this.newView = options.newView || false; + this.ddocs = options.ddocs; + this.params = options.params; + this.database = options.database; + this.currentDdoc = options.currentddoc; + + if (this.newView) { + this.viewName = 'newView'; + } else { + this.ddocID = options.ddocInfo ? options.ddocInfo.id : ''; + this.viewName = options.viewName; + this.ddocInfo = new Documents.DdocInfo({_id: this.ddocID},{database: this.database}); + } + + this.showIndex = false; + _.bindAll(this); + }, + + establish: function () { + if (this.ddocInfo) { + return this.ddocInfo.fetch(); + } + }, + + serialize: function () { + return { + database: this.database.get('id'), + ddocs: this.ddocs, + ddoc: this.model, + ddocName: this.model.id, + viewName: this.viewName, + newView: this.newView, + langTemplates: this.langTemplates.javascript + }; + } + }); + + Views.ViewEditor = FauxtonAPI.View.extend({ + template: 'addons/indexes/templates/view_editor', + builtinReduces: ['_sum', '_count', '_stats'], + + events: { + 'click button.save': 'saveView', + 'click button.delete': 'deleteView', + 'change select#reduce-function-selector': 'updateReduce', + 'click #db-views-tabs-nav': 'toggleIndexNav', + 'click .beautify_map': 'beautifyCode', + 'click .beautify_reduce': 'beautifyCode' + }, + + langTemplates: { + 'javascript': { + map: 'function(doc) {\n emit(doc._id, 1);\n}', + reduce: 'function(keys, values, rereduce){\n if (rereduce){\n return sum(values);\n } else {\n return values.length;\n }\n}' + } + }, + + defaultLang: 'javascript', + + initialize: function (options) { + this.newView = options.newView || false; + this.ddocs = options.ddocs; + this.params = options.params; + this.database = options.database; + this.currentDdoc = options.currentddoc; + + if (this.newView) { + this.viewName = 'newView'; + } else { + this.ddocID = options.ddocInfo.id; + this.viewName = options.viewName; + this.ddocInfo = new Documents.DdocInfo({_id: this.ddocID},{database: this.database}); + } + + this.showIndex = false; + _.bindAll(this); --- End diff -- Can we remove this.
--- If your project is set up for it, you can reply to this email and have your reply appear on GitHub as well. If your project does not have this feature enabled and wishes so, or if the feature is enabled but not working, please contact infrastructure at infrastruct...@apache.org or file a JIRA ticket with INFRA. ---