@@ -605,6 +605,16 @@ var unbxdSearchInit = function(jQuery, Handlebars){
605
605
, setEvents : function ( ) {
606
606
var self = this ;
607
607
608
+ var changeViewType = function ( e ) {
609
+ e . preventDefault ( ) ;
610
+ var $t = jQuery ( this ) ;
611
+ var selected = $t . attr ( "unbxdviewtype" ) ;
612
+
613
+ self . setViewType ( selected ) ;
614
+
615
+ self . callResults ( self . paintResultSet , true ) ;
616
+ } ;
617
+
608
618
var changeSort = function ( e ) {
609
619
e . preventDefault ( ) ;
610
620
var $t = jQuery ( this ) ,
@@ -901,6 +911,10 @@ var unbxdSearchInit = function(jQuery, Handlebars){
901
911
}
902
912
} , 3000 ) ;
903
913
}
914
+
915
+ if ( this . options . searchResultSetTemp !== null && typeof this . options . searchResultSetTemp === 'object' ) {
916
+ jQuery ( this . options . viewTypeContainerSelector ) . on ( "click" , '[unbxdviewtype]' , changeViewType ) ;
917
+ }
904
918
}
905
919
, addSort : function ( field , dir ) {
906
920
this . params . sort [ field ] = dir || "desc" ;
@@ -980,6 +994,15 @@ var unbxdSearchInit = function(jQuery, Handlebars){
980
994
, getPageSize : function ( ) {
981
995
return this . params . extra . rows ;
982
996
}
997
+
998
+ , setViewType : function ( viewType ) {
999
+ this . params . extra . view = viewType ;
1000
+ return this ;
1001
+ }
1002
+ , getViewType : function ( ) {
1003
+ return this . params . extra . view ;
1004
+ }
1005
+
983
1006
, addQueryParam : function ( key , value , dontOverried ) {
984
1007
if ( ! ( key in this . params . extra ) || ! dontOverried ) {
985
1008
this . params . extra [ key ] = value ;
@@ -1086,7 +1109,7 @@ var unbxdSearchInit = function(jQuery, Handlebars){
1086
1109
}
1087
1110
} else if ( key === 'wt' || key === 'format' ) {
1088
1111
nonhistoryPath += '&' + key + '=' + encodeURIComponent ( value ) ;
1089
- } else if ( this . isUsingPagination ( ) && key === 'rows' ) {
1112
+ } else if ( ( this . isUsingPagination ( ) && key === 'rows' ) || key === "view" ) {
1090
1113
url += '&' + key + '=' + encodeURIComponent ( value ) ;
1091
1114
} else if ( this . defaultParams . hasOwnProperty ( 'extra' ) && this . defaultParams . extra . hasOwnProperty ( key ) ) {
1092
1115
nonhistoryPath += '&' + key + '=' + encodeURIComponent ( value ) ;
@@ -1182,6 +1205,7 @@ var unbxdSearchInit = function(jQuery, Handlebars){
1182
1205
format : "json"
1183
1206
, page : 1
1184
1207
, rows : 12
1208
+ , view : this . options . viewTypes [ 0 ]
1185
1209
}
1186
1210
} ;
1187
1211
@@ -1275,6 +1299,12 @@ var unbxdSearchInit = function(jQuery, Handlebars){
1275
1299
if ( "start" in obj )
1276
1300
params . extra . page = ( parseInt ( obj . start ) / parseInt ( params . extra . rows ) ) + 1 ;
1277
1301
1302
+ if ( ! ( "view" in obj ) ) {
1303
+ params . extra . view = this . options . viewTypes [ 0 ] ;
1304
+ } else {
1305
+ params . extra . view = obj [ "view" ] ;
1306
+ }
1307
+
1278
1308
return params ;
1279
1309
}
1280
1310
, paintResultSet : function ( obj ) {
@@ -1384,6 +1414,16 @@ var unbxdSearchInit = function(jQuery, Handlebars){
1384
1414
1385
1415
if ( this . getClass ( this . options . searchResultSetTemp ) == 'Function' ) {
1386
1416
this . options . searchResultSetTemp . call ( this , obj ) ;
1417
+ } else if ( this . options . searchResultSetTemp !== null && typeof this . options . searchResultSetTemp === 'object' ) {
1418
+ this . paintViewTypes ( obj ) ;
1419
+ var currentViewType = this . getViewType ( ) ;
1420
+ if ( ! this . compiledResultTemp ) {
1421
+ this . compiledResultTemp = { } ;
1422
+ this . options . viewTypes . forEach ( function ( val ) {
1423
+ this . compiledResultTemp [ val ] = Handlebars . compile ( this . options . searchResultSetTemp [ val ] ) ;
1424
+ } . bind ( this ) ) ;
1425
+ }
1426
+ jQuery ( this . options . searchResultContainer ) . html ( this . compiledResultTemp [ currentViewType ] ( obj . response ) ) ;
1387
1427
} else {
1388
1428
if ( ! this . compiledResultTemp )
1389
1429
this . compiledResultTemp = Handlebars . compile ( this . options . searchResultSetTemp ) ;
@@ -1452,6 +1492,28 @@ var unbxdSearchInit = function(jQuery, Handlebars){
1452
1492
options : pageSizeOptions
1453
1493
} ) ) ;
1454
1494
}
1495
+
1496
+ , paintViewTypes : function ( obj ) {
1497
+ if ( "error" in obj || this . options . viewTypeContainerSelector . length <= 0 ) {
1498
+ return ;
1499
+ }
1500
+ if ( ! this . compiledViewTypesContainerTemp ) {
1501
+ this . compiledViewTypesContainerTemp = Handlebars . compile ( this . options . viewTypeContainerTemp ) ;
1502
+ }
1503
+ var viewTypeOptions = this . options . viewTypes . map ( function ( opt ) {
1504
+
1505
+ var values = { } ;
1506
+ values [ "value" ] = opt ;
1507
+ values [ "selected" ] = this . getViewType ( ) == opt ? true : false ;
1508
+ return values ;
1509
+
1510
+ } . bind ( this ) ) ;
1511
+ jQuery ( this . options . viewTypeContainerSelector ) . html ( this . compiledViewTypesContainerTemp ( {
1512
+ options : viewTypeOptions
1513
+ } ) ) ;
1514
+ return this . getViewType ( ) ;
1515
+ }
1516
+
1455
1517
, paintPagination : function ( obj ) {
1456
1518
if ( "error" in obj )
1457
1519
return ;
0 commit comments