Skip to content

Commit 7d73a82

Browse files
committed
Merge pull request #17 from cloudant-labs/fauxton-pouch-mr
Advanced _view options and editor
2 parents 829db1a + 2c81ee7 commit 7d73a82

File tree

10 files changed

+743
-146
lines changed

10 files changed

+743
-146
lines changed

src/fauxton/app/modules/documents/resources.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,8 +193,21 @@ function(app, FauxtonAPI, Views) {
193193
return url.join("/") + query;
194194
},
195195

196+
totalRows: function() {
197+
return this.viewMeta.total_rows || "unknown";
198+
},
199+
200+
updateSeq: function() {
201+
return this.viewMeta.update_seq || false;
202+
},
203+
196204
parse: function(resp) {
197205
that = this;
206+
this.viewMeta = {
207+
total_rows: resp.total_rows,
208+
offest: resp.offest,
209+
update_seq: resp.update_seq
210+
};
198211
return _.map(resp.rows, function(row) {
199212
return {
200213
value: row.value,

src/fauxton/app/modules/documents/routes.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -378,6 +378,7 @@ function(app, FauxtonAPI, Documents, Databases) {
378378

379379
var ddocInfo = {
380380
id: "_design/" + ddoc,
381+
currView: view,
381382
designDocs: data.designDocs
382383
};
383384

@@ -396,7 +397,8 @@ function(app, FauxtonAPI, Documents, Databases) {
396397
}),
397398

398399
"#sidebar-content": new Documents.Views.Sidebar({
399-
collection: data.designDocs
400+
collection: data.designDocs,
401+
ddocInfo: ddocInfo
400402
}),
401403

402404
"#tabs": new Documents.Views.Tabs({

src/fauxton/app/modules/documents/views.js

Lines changed: 85 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -240,18 +240,28 @@ function(app, FauxtonAPI, Codemirror, JSHint) {
240240
Views.IndexItem = FauxtonAPI.View.extend({
241241
template: "templates/documents/index_menu_item",
242242
tagName: "li",
243+
243244
initialize: function(options){
244245
this.index = options.index;
245246
this.ddoc = options.ddoc;
246247
this.database = options.database;
248+
this.selected = !! options.selected;
247249
},
248250

249251
serialize: function() {
250252
return {
251253
index: this.index,
252254
ddoc: this.ddoc,
253-
database: this.database
255+
database: this.database,
256+
selected: this.selected
254257
};
258+
},
259+
260+
afterRender: function() {
261+
if (this.selected) {
262+
$("#sidenav ul.nav-list li").removeClass("active");
263+
this.$el.addClass("active");
264+
}
255265
}
256266
});
257267

@@ -320,7 +330,8 @@ function(app, FauxtonAPI, Codemirror, JSHint) {
320330
database: this.collection,
321331
viewList: this.viewList,
322332
hasReduce: false,
323-
params: this.params
333+
params: this.params,
334+
ddocs: this.designDocs
324335
};
325336
if (this.ddoc) {
326337
data.ddoc = this.ddoc;
@@ -363,14 +374,14 @@ function(app, FauxtonAPI, Codemirror, JSHint) {
363374
return FauxtonAPI.addNotification({
364375
msg: "JSON Parse Error on field: "+param.name,
365376
type: "error",
366-
selector: ".view.show .errors-container"
377+
selector: ".view.show .all-docs-list.errors-container"
367378
});
368379
});
369380

370381
FauxtonAPI.addNotification({
371382
msg: "Make sure that strings are properly quoted and any other values are valid JSON structures",
372383
type: "warning",
373-
selector: ".view.show .errors-container"
384+
selector: ".view.show .all-docs-list.errors-container"
374385
});
375386

376387
return false;
@@ -401,7 +412,7 @@ function(app, FauxtonAPI, Codemirror, JSHint) {
401412
var notification = FauxtonAPI.addNotification({
402413
msg: "include_docs has been disabled as you cannot include docs on a reduced view",
403414
type: "warn",
404-
selector: ".view.show .errors-container"
415+
selector: ".view.show .all-docs-list.errors-container"
405416
});
406417
}
407418
$form.find("input[name=include_docs]").prop("disabled", true);
@@ -451,6 +462,14 @@ function(app, FauxtonAPI, Codemirror, JSHint) {
451462
},
452463

453464
beforeRender: function() {
465+
this.setDdocInfo();
466+
if (this.viewList) {
467+
this.viewEditorView = this.insertView("#edit-index-container", new Views.ViewEditor({
468+
model: this.ddoc,
469+
ddocs: this.designDocs,
470+
viewCollection: this.collection
471+
}));
472+
}
454473
this.collection.each(function(doc) {
455474
this.rows[doc.id] = this.insertView("table.all-docs tbody", new this.nestedView({
456475
model: doc
@@ -470,6 +489,9 @@ function(app, FauxtonAPI, Codemirror, JSHint) {
470489
$form.find("select[name='"+key+"']").val(val);
471490
break;
472491
case "include_docs":
492+
case "stale":
493+
case "descending":
494+
case "inclusive_end":
473495
$form.find("input[name='"+key+"']").prop('checked', true);
474496
break;
475497
case "reduce":
@@ -619,9 +641,11 @@ function(app, FauxtonAPI, Codemirror, JSHint) {
619641

620642
Views.ViewEditor = FauxtonAPI.View.extend({
621643
template: "templates/documents/view_editor",
644+
builtinReduces: ['_sum', '_count', '_stats'],
622645

623646
events: {
624647
"click button.save": "saveView",
648+
"click button.preview": "previewView",
625649
"change select#reduce-function-selector": "updateReduce"
626650
},
627651

@@ -636,6 +660,9 @@ function(app, FauxtonAPI, Codemirror, JSHint) {
636660

637661
initialize: function(options) {
638662
this.ddocs = options.ddocs;
663+
this.viewCollection = options.viewCollection;
664+
this.reduceFunStr = this.model.viewHasReduce(this.viewCollection.view);
665+
this.newView = false;
639666
},
640667

641668
updateValues: function() {
@@ -661,18 +688,40 @@ function(app, FauxtonAPI, Codemirror, JSHint) {
661688
},
662689

663690
establish: function() {
664-
return [this.ddocs.fetch(), this.model.fetch()];
691+
//return [this.ddocs.fetch(), this.model.fetch()];
692+
return [];
693+
},
694+
695+
previewView: function(event) {
696+
FauxtonAPI.addNotification({
697+
msg: "<strong>Warning!</strong> Preview executes the Map/Reduce functions in your browser, and may behave differently from CouchDB.",
698+
type: "warning",
699+
selector: "#define-view .errors-container",
700+
fade: false
701+
});
702+
FauxtonAPI.addNotification({
703+
msg: "Preview Functionality Coming Soon",
704+
type: "warning",
705+
selector: "#define-view .errors-container"
706+
});
665707
},
666708

667709
saveView: function(event) {
668710
var json, notification;
669711
if (this.hasValidCode()) {
670712
var mapVal = this.mapEditor.getValue();
671713
var reduceVal = this.reduceEditor.getValue();
714+
/*
672715
notification = FauxtonAPI.addNotification({
673716
msg: "Saving document.",
674717
selector: "#define-view .errors-container"
675718
});
719+
*/
720+
FauxtonAPI.addNotification({
721+
msg: "Save Functionality Coming Soon",
722+
type: "warning",
723+
selector: "#define-view .errors-container"
724+
});
676725
/*
677726
this.model.save().error(function(xhr) {
678727
var responseText = JSON.parse(xhr.responseText).reason;
@@ -745,18 +794,28 @@ function(app, FauxtonAPI, Codemirror, JSHint) {
745794

746795
serialize: function() {
747796
return {
748-
database: this.model,
749-
ddocs: this.ddocs
797+
//database: this.model,
798+
ddocs: this.ddocs,
799+
ddoc: this.model,
800+
viewCollection: this.viewCollection,
801+
reduceFunStr: this.reduceFunStr,
802+
isCustomReduce: this.hasCustomReduce(),
803+
newView: this.newView
750804
};
751805
},
752806

807+
hasCustomReduce: function() {
808+
return this.reduceFunStr && ! _.contains(this.builtinReduces, this.reduceFunStr);
809+
},
810+
753811
afterRender: function() {
754-
this.model.on("sync", this.updateValues, this);
755812
var that = this;
756813
var mapFun = $("#map-function");
757-
mapFun.val(this.langTemplates[this.defaultLang].map);
758814
var reduceFun = $("#reduce-function");
759-
reduceFun.val(this.langTemplates[this.defaultLang].reduce);
815+
if (this.newView) {
816+
mapFun.val(this.langTemplates[this.defaultLang].map);
817+
reduceFun.val(this.langTemplates[this.defaultLang].reduce);
818+
}
760819
this.mapEditor = Codemirror.fromTextArea(mapFun.get()[0], {
761820
mode: "javascript",
762821
lineNumbers: true,
@@ -766,7 +825,7 @@ function(app, FauxtonAPI, Codemirror, JSHint) {
766825
that.runJSHint("mapEditor");
767826
},
768827
extraKeys: {
769-
"Ctrl-S": function(instance) { that.saveDoc(); },
828+
"Ctrl-S": function(instance) { that.saveView(); },
770829
"Ctrl-/": "undo"
771830
}
772831
});
@@ -779,14 +838,16 @@ function(app, FauxtonAPI, Codemirror, JSHint) {
779838
that.runJSHint("reduceEditor");
780839
},
781840
extraKeys: {
782-
"Ctrl-S": function(instance) { that.saveDoc(); },
841+
"Ctrl-S": function(instance) { that.saveView(); },
783842
"Ctrl-/": "undo"
784843
}
785844
});
786845
// HACK: this should be in the html
787846
// but CodeMirror's head explodes and it won't set the hight properly.
788847
// So render it first, set the editor, then hide.
789-
$(".control-group.reduce-function").hide();
848+
if ( ! this.hasCustomReduce()) {
849+
$(".control-group.reduce-function").hide();
850+
}
790851
}
791852
});
792853

@@ -799,6 +860,13 @@ function(app, FauxtonAPI, Codemirror, JSHint) {
799860
"click .nav-list a.toggle-view#design-docs": "toggleView"
800861
},
801862

863+
initialize: function(options) {
864+
if (options.ddocInfo) {
865+
this.ddocID = options.ddocInfo.id;
866+
this.currView = options.ddocInfo.currView;
867+
}
868+
},
869+
802870
establish: function() {
803871
if (this.collection) {
804872
return [this.collection.fetch()];
@@ -836,10 +904,12 @@ function(app, FauxtonAPI, Codemirror, JSHint) {
836904
buildIndexList: function(collection, selector, design){
837905

838906
_.each(_.keys(collection), function(key){
907+
var selected = this.ddocID == "_design/"+design;
839908
this.insertView("ul.nav." + selector, new Views.IndexItem({
840909
ddoc: design,
841910
index: key,
842-
database: this.collection.database.id
911+
database: this.collection.database.id,
912+
selected: selected && key == this.currView
843913
}));
844914
}, this);
845915
},
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
// Licensed under the Apache License, Version 2.0 (the "License"); you may not
2+
// use this file except in compliance with the License. You may obtain a copy of
3+
// the License at
4+
//
5+
// http://www.apache.org/licenses/LICENSE-2.0
6+
//
7+
// Unless required by applicable law or agreed to in writing, software
8+
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
9+
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
10+
// License for the specific language governing permissions and limitations under
11+
// the License.
12+
13+
14+
/*
15+
* NOTE:
16+
* This temporarily uses the PouchDB map reduce implementation
17+
* These files are modified locally until we make a more general version and
18+
* push it back upstream.
19+
*/
20+
21+
define([
22+
"app",
23+
24+
"api",
25+
26+
// Modules
27+
"modules/pouchdb/pouchdb.mapreduce.js"
28+
],
29+
30+
function(app, FauxtonAPI, MapReduce) {
31+
var Pouch = {};
32+
Pouch.MapReduce = MapReduce;
33+
34+
Pouch.runViewQuery = function(fun, docs) {
35+
docs = [
36+
{_id: 'test_doc_1', foo: 'bar-1'},
37+
{_id: 'test_doc_2', foo: 'bar-2'},
38+
{_id: 'test_doc_3', foo: 'bar-3'},
39+
{_id: 'test_doc_4', foo: 'bar-4'},
40+
{_id: 'test_doc_5', foo: 'bar-5'},
41+
{_id: 'test_doc_6', foo: 'bar-6'},
42+
{_id: 'test_doc_7', foo: 'bar-7'},
43+
{_id: 'test_doc_8', foo: 'bar-8'},
44+
{_id: 'test_doc_9', foo: 'bar-9'},
45+
{_id: 'test_doc_10', foo: 'bar-10'}
46+
];
47+
48+
var deferred = FauxtonAPI.Deferred();
49+
var complete = function(resp) {
50+
console.log("COMPLETE TRIGGERED", arguments);
51+
};
52+
53+
return Pouch.MapReduce.query(fun, {docs: docs, complete:complete});
54+
};
55+
56+
return Pouch;
57+
});

0 commit comments

Comments
 (0)