couchdb-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From gar...@apache.org
Subject [2/2] git commit: updated refs/heads/fauxton-view-improvements to 4470dfa
Date Thu, 16 May 2013 15:44:51 GMT
able to save view and regenerate results


Project: http://git-wip-us.apache.org/repos/asf/couchdb/repo
Commit: http://git-wip-us.apache.org/repos/asf/couchdb/commit/4470dfa1
Tree: http://git-wip-us.apache.org/repos/asf/couchdb/tree/4470dfa1
Diff: http://git-wip-us.apache.org/repos/asf/couchdb/diff/4470dfa1

Branch: refs/heads/fauxton-view-improvements
Commit: 4470dfa154f0343250ce9e5daaf4e2ff8a9d5247
Parents: 82951f5
Author: Garren Smith <garren.smith@gmail.com>
Authored: Thu May 16 17:42:34 2013 +0200
Committer: Garren Smith <garren.smith@gmail.com>
Committed: Thu May 16 17:42:34 2013 +0200

----------------------------------------------------------------------
 src/fauxton/app/addons/stats/views.js              |    3 -
 src/fauxton/app/api.js                             |    5 +-
 src/fauxton/app/modules/documents/resources.js     |   51 +++++---
 src/fauxton/app/modules/documents/routes.js        |    7 +-
 src/fauxton/app/modules/documents/views.js         |  104 +++++++++++----
 .../app/templates/documents/view_editor.html       |    6 +-
 6 files changed, 119 insertions(+), 57 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/couchdb/blob/4470dfa1/src/fauxton/app/addons/stats/views.js
----------------------------------------------------------------------
diff --git a/src/fauxton/app/addons/stats/views.js b/src/fauxton/app/addons/stats/views.js
index 9fda708..9dd9cbc 100644
--- a/src/fauxton/app/addons/stats/views.js
+++ b/src/fauxton/app/addons/stats/views.js
@@ -21,7 +21,6 @@ define([
 ],
 
 function(app, FauxtonAPI,Stats) {
-  console.log(arguments);
   Views = {};
 
   datatypeEventer = {};
@@ -69,8 +68,6 @@ function(app, FauxtonAPI,Stats) {
 
         series = _.filter(series, function(d){return d.y > 0;});
         series = _.sortBy(series, function(d){return -d.y;});
-        console.log('series');
-        console.log(series);
 
         nv.addGraph(function() {
             var width = 550,

http://git-wip-us.apache.org/repos/asf/couchdb/blob/4470dfa1/src/fauxton/app/api.js
----------------------------------------------------------------------
diff --git a/src/fauxton/app/api.js b/src/fauxton/app/api.js
index 40fb4cd..b3e4399 100644
--- a/src/fauxton/app/api.js
+++ b/src/fauxton/app/api.js
@@ -55,8 +55,9 @@ function(app, Fauxton) {
     }
   });
 
-  FauxtonAPI.navigate = function(url) {
-    Backbone.history.navigate(url, true);
+  FauxtonAPI.navigate = function(url, _opts) {
+    var options = _.extend({trigger: true}, _opts );
+    app.router.navigate(url,options);
   };
 
   FauxtonAPI.addHeaderLink = function(link) {

http://git-wip-us.apache.org/repos/asf/couchdb/blob/4470dfa1/src/fauxton/app/modules/documents/resources.js
----------------------------------------------------------------------
diff --git a/src/fauxton/app/modules/documents/resources.js b/src/fauxton/app/modules/documents/resources.js
index d231f67..2bd6ddf 100644
--- a/src/fauxton/app/modules/documents/resources.js
+++ b/src/fauxton/app/modules/documents/resources.js
@@ -13,15 +13,10 @@
 define([
   "app",
 
-  "api",
-
-  // Views
-  "modules/documents/views"
-
-  // Plugins
+  "api"
 ],
 
-function(app, FauxtonAPI, Views) {
+function(app, FauxtonAPI) {
   var Documents = app.module();
 
   Documents.Doc = Backbone.Model.extend({
@@ -64,30 +59,54 @@ function(app, FauxtonAPI, Views) {
     hasViews: function() {
       if (!this.isDdoc()) return false;
       var doc = this.get('doc');
-      return doc && doc.views && _.keys(doc.views).length > 0;
+      if (doc) {
+        return doc && doc.views && _.keys(doc.views).length > 0;
+      }
+
+      var views = this.get('views');
+      return views && _.keys(views).length > 0;
     },
 
     getDdocView: function(view) {
       if (!this.isDdoc() || !this.hasViews()) return false;
 
       var doc = this.get('doc');
-      return doc.views[view];
+      if (doc) {
+        return doc.views[view];
+      }
+
+      return this.get('views')[view];
     },
 
     setDdocView: function (view, map, reduce) {
       if (!this.isDdoc()) return false;
-      var doc = this.get('doc');
+      var views = this.get('views');
 
-      if (!doc.views) { doc.views = {}; }
+      if (reduce) {
+        views[view] = {
+          map: map,
+          reduce: reduce
+        }; 
+      } else {
+        views[view].map = map;
+      }
 
-      doc.views[view] = {
-        map: map,
-        reduce: reduce
-      };
+      this.set({views: views});
 
       return true;
     },
 
+    dDocModel: function () {
+      if (!this.isDdoc()) return false;
+      var doc = this.get('doc');
+
+      if (doc) {
+        return new Documents.Doc(doc, {database: this.database});
+      } 
+
+      return this;
+    },
+
     viewHasReduce: function(viewName) {
       var view = this.getDdocView(viewName);
 
@@ -271,7 +290,5 @@ function(app, FauxtonAPI, Views) {
     }
   });
 
-  Documents.Views = Views;
-
   return Documents;
 });

http://git-wip-us.apache.org/repos/asf/couchdb/blob/4470dfa1/src/fauxton/app/modules/documents/routes.js
----------------------------------------------------------------------
diff --git a/src/fauxton/app/modules/documents/routes.js b/src/fauxton/app/modules/documents/routes.js
index 6f16896..79506d9 100644
--- a/src/fauxton/app/modules/documents/routes.js
+++ b/src/fauxton/app/modules/documents/routes.js
@@ -16,7 +16,7 @@ define([
        "api",
 
        // Modules
-       "modules/documents/resources",
+       "modules/documents/views",
        "modules/databases/base"
 ],
 
@@ -32,7 +32,7 @@ function(app, FauxtonAPI, Documents, Databases) {
       var databaseName = options[0], docID = options[1];
 
       this.database = this.database || new Databases.Model({id: databaseName});
-      this.doc = this.doc || new Documents.Doc({
+      this.doc = new Documents.Doc({
         _id: docID
       }, {
         database: this.database
@@ -251,6 +251,7 @@ function(app, FauxtonAPI, Documents, Databases) {
       };
 
       this.setView("#dashboard-content", new Documents.Views.AllDocsList({
+        database: this.data.database,
         collection: this.data.indexedDocs,
         nestedView: Documents.Views.Row,
         viewList: true,
@@ -287,9 +288,7 @@ function(app, FauxtonAPI, Documents, Databases) {
       }));
 
       this.sidebar.setSelectedTab('new-view');
-
     }
-
   });
 
   var ChangesRouteObject = FauxtonAPI.RouteObject.extend({

http://git-wip-us.apache.org/repos/asf/couchdb/blob/4470dfa1/src/fauxton/app/modules/documents/views.js
----------------------------------------------------------------------
diff --git a/src/fauxton/app/modules/documents/views.js b/src/fauxton/app/modules/documents/views.js
index 199b551..f5219e8 100644
--- a/src/fauxton/app/modules/documents/views.js
+++ b/src/fauxton/app/modules/documents/views.js
@@ -14,6 +14,8 @@ define([
   "app",
 
   "api",
+ 
+  "modules/documents/resources",
 
   // Libs
   "codemirror",
@@ -22,9 +24,10 @@ define([
   // Plugins
   "plugins/codemirror-javascript",
   "plugins/prettify"
+
 ],
 
-function(app, FauxtonAPI, Codemirror, JSHint) {
+function(app, FauxtonAPI, Documents, Codemirror, JSHint) {
   var Views = {};
 
   Views.Tabs = FauxtonAPI.View.extend({
@@ -287,6 +290,7 @@ function(app, FauxtonAPI, Codemirror, JSHint) {
       this.rows = {};
       this.viewList = !! options.viewList;
       this.params = options.params;
+      this.database = options.database;
       if (options.ddocInfo) {
         this.designDocs = options.ddocInfo.designDocs;
         this.ddocID = options.ddocInfo.id;
@@ -342,13 +346,26 @@ function(app, FauxtonAPI, Codemirror, JSHint) {
       if (this.ddoc) {
         data.ddoc = this.ddoc;
         data.hasReduce = this.ddoc.viewHasReduce(this.collection.view);
+        this.view = this.collection.view;
       }
       return data;
     },
 
+    updateViewOnSave: function(ddoc, view) {
+      this.ddoc = ddoc;
+      this.view = view;
+      this.updateView();
+    },
+
     updateView: function(event) {
-      event.preventDefault();
-      var $form = $(event.currentTarget);
+      var $form;
+
+      if (event && event.preventDefault) { 
+        event.preventDefault();
+        $form = $(event.currentTarget);
+      } else {
+        $form = this.$('.view-query-update');
+      }
 
       // Ignore params without a value
       var params = _.filter($form.serializeArray(), function(param) {
@@ -395,7 +412,22 @@ function(app, FauxtonAPI, Codemirror, JSHint) {
 
       var fragment = window.location.hash.replace(/\?.*$/, '');
       fragment = fragment + '?' + $.param(params);
-      FauxtonAPI.navigate(fragment);
+      FauxtonAPI.navigate(fragment, {trigger: false});
+
+      this.params = app.getParams();
+
+      this.collection = new Documents.IndexCollection(null, {
+        database: this.database,
+        design: this.ddoc.id.replace('_design/',''),
+        view: this.view,
+        params: this.params
+      });
+
+      var that = this;
+      FauxtonAPI.when(this.establish()).then(function () {
+        that.render();
+      });
+
     },
 
     updateFilters: function(event) {
@@ -471,10 +503,12 @@ function(app, FauxtonAPI, Codemirror, JSHint) {
       this.setDdocInfo();
       if (this.viewList) {
         this.viewEditorView = this.insertView("#edit-index-container", new Views.ViewEditor({
-          model: this.ddoc,
+          model: this.ddoc.dDocModel(),
           ddocs: this.designDocs,
           viewCollection: this.collection
         }));
+
+        this.listenTo(this.viewEditorView, 'view_updated', this.updateViewOnSave);
       }
       this.collection.each(function(doc) {
         this.rows[doc.id] = this.insertView("table.all-docs tbody", new this.nestedView({
@@ -668,10 +702,9 @@ function(app, FauxtonAPI, Codemirror, JSHint) {
     initialize: function(options) {
       this.newView = options.newView || false;
       this.ddocs = options.ddocs;
-      console.log('DDOC', this.ddocs);
       this.viewCollection = options.viewCollection;
       if (this.newView) {
-
+        //TODO: CREATE NEW HERE
       } else {
         this.reduceFunStr = this.model.viewHasReduce(this.viewCollection.view);
       } 
@@ -720,34 +753,45 @@ function(app, FauxtonAPI, Codemirror, JSHint) {
     },
 
     saveView: function(event) {
-      var json, notification;
+      var json, notification,
+          that = this;
 
       event.preventDefault();
 
       if (this.hasValidCode()) {
         var mapVal = this.mapEditor.getValue(), 
-            reduceVal = this.reduceEditor.getValue(),
+            reduceVal = "",
             viewName = this.$('#index-name').val(),
             ddocName = this.$('#ddoc :selected').val();
 
+         var reduceOption = this.$('#reduce-function-selector :selected').val();
+
+         if (reduceOption === 'CUSTOM') {
+            reduceVal = this.reduceEditor.getValue();
+         } else if ( reduceOption !== 'NONE') {
+           reduceVal = reduceOption;
+         }
+
          var ddoc = this.ddocs.find(function (ddoc) {
            return ddoc.id === ddocName;
-         });
-         console.log('found ddoc', ddocName, ddoc);
-        /*
+         }).dDocModel();
+
         notification = FauxtonAPI.addNotification({
           msg: "Saving document.",
           selector: "#define-view .errors-container"
         });
-        */
+
         ddoc.setDdocView(viewName, mapVal, reduceVal);
-        FauxtonAPI.addNotification({
-          msg: "Save Functionality Coming Soon",
-          type: "warning",
-          selector: "#define-view .errors-container"
-        });
-        
-        ddoc.save().fail(function(xhr) {
+
+        ddoc.save().then(function () {
+          FauxtonAPI.addNotification({
+            msg: "View has been saved.",
+            type: "success",
+            selector: "#define-view .errors-container"
+          });
+
+          that.trigger('view_updated', ddoc, viewName);
+        }, function(xhr) {
           var responseText = JSON.parse(xhr.responseText).reason;
           notification = FauxtonAPI.addNotification({
             msg: "Save failed: " + responseText,
@@ -817,7 +861,6 @@ function(app, FauxtonAPI, Codemirror, JSHint) {
 
     serialize: function() {
       return {
-        //database: this.model,
         ddocs: this.ddocs,
         ddoc: this.model,
         viewCollection: this.viewCollection,
@@ -846,7 +889,7 @@ function(app, FauxtonAPI, Codemirror, JSHint) {
         matchBrackets: true,
         lineWrapping: true,
         onChange: function() {
-          that.runJSHint("mapEditor");
+          //that.runJSHint("mapEditor");
         },
         extraKeys: {
           "Ctrl-S": function(instance) { that.saveView(); },
@@ -859,7 +902,7 @@ function(app, FauxtonAPI, Codemirror, JSHint) {
         matchBrackets: true,
         lineWrapping: true,
         onChange: function() {
-          that.runJSHint("reduceEditor");
+          //that.runJSHint("reduceEditor");
         },
         extraKeys: {
           "Ctrl-S": function(instance) { that.saveView(); },
@@ -946,7 +989,14 @@ function(app, FauxtonAPI, Codemirror, JSHint) {
       }, this);
     },
 
+    afterRender: function () {
+      if (this.selectedTab) {
+        this.setSelectedTab(this.selectedTab);
+      }
+    },
+
     setSelectedTab: function (selectedTab) {
+      this.selectedTab = selectedTab;
       this.$('li').removeClass('active');
       this.$('#' + selectedTab).parent().addClass('active');
     }
@@ -959,9 +1009,7 @@ function(app, FauxtonAPI, Codemirror, JSHint) {
     template: "templates/documents/changes",
 
     establish: function() {
-      return [
-        this.model.changes.fetch()
-      ];
+      return [ this.model.changes.fetch()];
     },
 
     serialize: function () {
@@ -973,6 +1021,6 @@ function(app, FauxtonAPI, Codemirror, JSHint) {
 
   });
 
-
-  return Views;
+  Documents.Views = Views;
+  return Documents;
 });

http://git-wip-us.apache.org/repos/asf/couchdb/blob/4470dfa1/src/fauxton/app/templates/documents/view_editor.html
----------------------------------------------------------------------
diff --git a/src/fauxton/app/templates/documents/view_editor.html b/src/fauxton/app/templates/documents/view_editor.html
index 6fbc71a..8ee3a4a 100644
--- a/src/fauxton/app/templates/documents/view_editor.html
+++ b/src/fauxton/app/templates/documents/view_editor.html
@@ -54,12 +54,12 @@ the License.
                 <% if (newView) { %>
                   <textarea class="js-editor" id="map-function"><%= langTemplates.map
%></textarea>
                 <% } else { %>
-                  <textarea class="js-editor" id="map-function"><%= ddoc.get('doc').views[viewCollection.view].map
%></textarea>
+                  <textarea class="js-editor" id="map-function"><%= ddoc.get('views')[viewCollection.view].map
%></textarea>
                 <% } %>
               </div>
             </div>
             <div class="control-group">
-              <label class="control-label" for="reduce-function-selector">Reduce function
<a target="_couch_docs" href="http://docs.couchdb.org/en/latest/ddocs/#reduce-and-rereduce-functions"><i
class="icon-question-sign"></i></a></label>
+              <label class="control-label" for="reduce-function-selector">Reduce function
<a target="_couch_docs" href="http://docs.couchdb.org/en/latest/ddocs.html#reduce-and-rereduce-functions"><i
class="icon-question-sign"></i></a></label>
               <div class="controls">
                 <select id="reduce-function-selector">
                   <option value="" <%= !reduceFunStr ? 'selected="selected"' : '' %>>None</option>
@@ -77,7 +77,7 @@ the License.
                 <% if (newView) { %>
                   <textarea class="js-editor" id="reduce-function"><%= langTemplates.reduce
%></textarea>
                 <% } else { %>
-                  <textarea class="js-editor" id="reduce-function"><%= ddoc.get('doc').views[viewCollection.view].reduce
%></textarea>
+                  <textarea class="js-editor" id="reduce-function"><%= ddoc.get('views')[viewCollection.view].reduce
%></textarea>
                 <% } %>
               </div>
             </div>


Mime
View raw message