@@ -170,7 +170,7 @@ module openflow {
170170 var msg : Message = new Message ( ) ; msg . command = "dropcollection" ; msg . data = JSON . stringify ( q ) ;
171171 q = await this . WebSocketClient . Send < DropCollectionMessage > ( msg ) ;
172172 }
173- async Query ( collection : string , query : any , projection : any = null , orderby : any = { _created : - 1 } , top : number = 100 , skip : number = 0 , queryas : string = null ) : Promise < any [ ] > {
173+ async Query ( collection : string , query : any , projection : any = null , orderby : any = { _id : - 1 } , top : number = 100 , skip : number = 0 , queryas : string = null ) : Promise < any [ ] > {
174174 var q : QueryMessage = new QueryMessage ( ) ;
175175 q . collectionname = collection ; q . query = query ; q . queryas = queryas ;
176176 q . query = JSON . stringify ( query , ( key , value ) => {
@@ -598,7 +598,7 @@ module openflow {
598598 public baseprojection : any = { } ;
599599 public collection : string = "entities" ;
600600 public models : T [ ] = [ ] ;
601- public orderby : any = { _created : - 1 } ;
601+ public orderby : any = { _id : - 1 } ;
602602 public autorefresh : boolean = false ;
603603 public autorefreshinterval : number = 30 * 1000 ;
604604 public autorefreshpromise : any = null ;
@@ -607,6 +607,7 @@ module openflow {
607607 public searchstring : string = "" ;
608608 public searchfields : string [ ] = [ "name" ] ;
609609 public basequeryas : string = null ;
610+ public errormessage : string = "" ;
610611
611612 public static $inject = [
612613 "$scope" ,
@@ -642,52 +643,60 @@ module openflow {
642643 }
643644 }
644645 async loadData ( ) : Promise < void > {
645- if ( this . loading == true ) { console . log ( "allready loading data, exit" ) ; return ; }
646- this . loading = true ;
647- if ( this . preloadData != null ) {
648- this . preloadData ( ) ;
649- }
650- var query = this . basequery ;
651- if ( this . searchstring !== "" ) {
652- var finalor = [ ] ;
653- for ( var i = 0 ; i < this . searchfields . length ; i ++ ) {
654- var newq : any = { } ;
655- // exact match case sensitive
656- // newq[this.searchfields[i]] = this.searchstring;
657- // exact match case insensitive
658- newq [ this . searchfields [ i ] ] = new RegExp ( [ "^" , this . searchstring , "$" ] . join ( "" ) , "i" ) ;
659-
660- // exact match string contains
661- newq [ this . searchfields [ i ] ] = new RegExp ( [ this . searchstring ] . join ( "" ) , "i" ) ;
662-
663- finalor . push ( newq ) ;
646+ try {
647+ if ( this . loading == true ) { console . log ( "allready loading data, exit" ) ; return ; }
648+ this . errormessage = "" ;
649+ this . loading = true ;
650+ if ( this . preloadData != null ) {
651+ this . preloadData ( ) ;
664652 }
665- if ( Object . keys ( query ) . length == 0 ) {
666- query = { $or : finalor . concat ( ) } ;
667- } else {
668- query = { $and : [ query , { $or : finalor . concat ( ) } ] } ;
653+ var query = this . basequery ;
654+ if ( this . searchstring !== "" ) {
655+ var finalor = [ ] ;
656+ for ( var i = 0 ; i < this . searchfields . length ; i ++ ) {
657+ var newq : any = { } ;
658+ // exact match case sensitive
659+ // newq[this.searchfields[i]] = this.searchstring;
660+ // exact match case insensitive
661+ newq [ this . searchfields [ i ] ] = new RegExp ( [ "^" , this . searchstring , "$" ] . join ( "" ) , "i" ) ;
662+
663+ // exact match string contains
664+ newq [ this . searchfields [ i ] ] = new RegExp ( [ this . searchstring ] . join ( "" ) , "i" ) ;
665+
666+ finalor . push ( newq ) ;
667+ }
668+ if ( Object . keys ( query ) . length == 0 ) {
669+ query = { $or : finalor . concat ( ) } ;
670+ } else {
671+ query = { $and : [ query , { $or : finalor . concat ( ) } ] } ;
672+ }
669673 }
670- }
671- this . models = await this . api . Query ( this . collection , query , this . baseprojection , this . orderby , 100 , 0 , this . basequeryas ) ;
672- this . loading = false ;
673- if ( this . autorefresh ) {
674- if ( this . models . length >= 100 ) {
675- // console.warn("Disabling auto refresh, result has more than 100 entries");
676- } else {
677- if ( this . autorefreshpromise == null && this . searchstring === "" ) {
678- //if (this.autorefreshpromise == null) {
679- this . autorefreshpromise = this . $interval ( ( ) => {
680- this . loadData ( ) ;
681- } , this . autorefreshinterval ) ;
682- this . $scope . $on ( '$destroy' , ( ) => {
683- this . $interval . cancel ( this . autorefreshpromise ) ;
684- } ) ;
674+ console . log ( this . orderby ) ;
675+ this . models = await this . api . Query ( this . collection , query , this . baseprojection , this . orderby , 100 , 0 , this . basequeryas ) ;
676+ this . loading = false ;
677+ if ( this . autorefresh ) {
678+ if ( this . models . length >= 100 ) {
679+ // console.warn("Disabling auto refresh, result has more than 100 entries");
680+ } else {
681+ if ( this . autorefreshpromise == null && this . searchstring === "" ) {
682+ //if (this.autorefreshpromise == null) {
683+ this . autorefreshpromise = this . $interval ( ( ) => {
684+ this . loadData ( ) ;
685+ } , this . autorefreshinterval ) ;
686+ this . $scope . $on ( '$destroy' , ( ) => {
687+ this . $interval . cancel ( this . autorefreshpromise ) ;
688+ } ) ;
689+ }
685690 }
686691 }
687- }
688- if ( this . postloadData != null ) {
689- this . postloadData ( ) ;
690- } else {
692+ if ( this . postloadData != null ) {
693+ this . postloadData ( ) ;
694+ } else {
695+ if ( ! this . $scope . $$phase ) { this . $scope . $apply ( ) ; }
696+ }
697+ } catch ( error ) {
698+ this . loading = false ;
699+ this . errormessage = JSON . stringify ( error ) ;
691700 if ( ! this . $scope . $$phase ) { this . $scope . $apply ( ) ; }
692701 }
693702 }
@@ -745,6 +754,7 @@ module openflow {
745754 public autorefreshpromise : any = null ;
746755 public preloadData : any = null ;
747756 public postloadData : any = null ;
757+ public errormessage : string = "" ;
748758
749759 public static $inject = [
750760 "$scope" ,
@@ -766,47 +776,54 @@ module openflow {
766776 this . basequery = { _id : this . id } ;
767777 }
768778 async loadData ( ) : Promise < void > {
769- if ( this . loading == true ) { console . log ( "allready loading data, exit" ) ; return ; }
770- var updated : boolean = false ;
771- this . loading = true ;
772- if ( this . preloadData != null ) {
773- this . preloadData ( ) ;
774- }
779+ try {
780+ if ( this . loading == true ) { console . log ( "allready loading data, exit" ) ; return ; }
781+ this . errormessage = "" ;
782+ var updated : boolean = false ;
783+ this . loading = true ;
784+ if ( this . preloadData != null ) {
785+ this . preloadData ( ) ;
786+ }
775787
776- var result = await this . api . Query ( this . collection , this . basequery , this . baseprojection , null , 1 ) ;
777- if ( result . length > 0 ) {
778- if ( this . model == null ) {
779- this . model = result [ 0 ] ;
780- updated = true ;
781- } else {
782- if ( ! angular . equals ( this . model , result [ 0 ] ) ) {
788+ var result = await this . api . Query ( this . collection , this . basequery , this . baseprojection , null , 1 ) ;
789+ if ( result . length > 0 ) {
790+ if ( this . model == null ) {
783791 this . model = result [ 0 ] ;
784792 updated = true ;
793+ } else {
794+ if ( ! angular . equals ( this . model , result [ 0 ] ) ) {
795+ this . model = result [ 0 ] ;
796+ updated = true ;
797+ }
785798 }
786- }
787799
788- }
789- if ( updated ) {
790- this . keys = Object . keys ( this . model ) ;
791- for ( var i : number = this . keys . length - 1 ; i >= 0 ; i -- ) {
792- if ( this . keys [ i ] . startsWith ( '_' ) ) this . keys . splice ( i , 1 ) ;
793800 }
794- }
795- this . loading = false ;
796- if ( this . postloadData != null ) {
797- this . postloadData ( ) ;
798- } else {
799- if ( ! this . $scope . $$phase ) { this . $scope . $apply ( ) ; }
800- }
801- if ( this . autorefresh ) {
802- if ( this . autorefreshpromise == null ) {
803- this . autorefreshpromise = this . $interval ( ( ) => {
804- this . loadData ( ) ;
805- } , this . autorefreshinterval ) ;
806- this . $scope . $on ( '$destroy' , ( ) => {
807- this . $interval . cancel ( this . autorefreshpromise ) ;
808- } ) ;
801+ if ( updated ) {
802+ this . keys = Object . keys ( this . model ) ;
803+ for ( var i : number = this . keys . length - 1 ; i >= 0 ; i -- ) {
804+ if ( this . keys [ i ] . startsWith ( '_' ) ) this . keys . splice ( i , 1 ) ;
805+ }
809806 }
807+ this . loading = false ;
808+ if ( this . postloadData != null ) {
809+ this . postloadData ( ) ;
810+ } else {
811+ if ( ! this . $scope . $$phase ) { this . $scope . $apply ( ) ; }
812+ }
813+ if ( this . autorefresh ) {
814+ if ( this . autorefreshpromise == null ) {
815+ this . autorefreshpromise = this . $interval ( ( ) => {
816+ this . loadData ( ) ;
817+ } , this . autorefreshinterval ) ;
818+ this . $scope . $on ( '$destroy' , ( ) => {
819+ this . $interval . cancel ( this . autorefreshpromise ) ;
820+ } ) ;
821+ }
822+ }
823+ } catch ( error ) {
824+ this . loading = false ;
825+ this . errormessage = JSON . stringify ( error ) ;
826+ if ( ! this . $scope . $$phase ) { this . $scope . $apply ( ) ; }
810827 }
811828 }
812829 }
0 commit comments