@@ -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 } ,
0 commit comments