@@ -465,7 +465,17 @@ var unbxdSearchInit = function(jQuery, Handlebars){
465
465
'{{/if}}' ,
466
466
'{{/options}}' ,
467
467
'</select>'
468
- ] . join ( '' )
468
+ ] . join ( '' ) ,
469
+ viewTypeContainerTemp :
470
+ '{{#options}}'
471
+ + '<li class="unbxd-{{#if selected}}current{{/if}}">'
472
+ + '<a title="{{value}} View" class="unbxd-{{value}}view-button" {{#unless selected}}unbxdviewtype="{{value}}"{{/unless}}>'
473
+ + '<span class="icon-{{value}}view">'
474
+ + '{{value}}{{#if selected}}-current{{/if}}'
475
+ + '</span>'
476
+ + '</a>'
477
+ + '</li>'
478
+ + '{{/options}}'
469
479
} ;
470
480
471
481
jQuery . extend ( Unbxd . setSearch . prototype , {
@@ -605,6 +615,16 @@ var unbxdSearchInit = function(jQuery, Handlebars){
605
615
, setEvents : function ( ) {
606
616
var self = this ;
607
617
618
+ var changeViewType = function ( e ) {
619
+ e . preventDefault ( ) ;
620
+ var $t = jQuery ( this ) ;
621
+ var selected = $t . attr ( "unbxdviewtype" ) ;
622
+ self . setViewType ( selected ) ;
623
+ if ( selected && self . options . viewTypes . indexOf ( selected ) > - 1 ) {
624
+ self . callResults ( self . paintResultSet , true ) ;
625
+ }
626
+ } ;
627
+
608
628
var changeSort = function ( e ) {
609
629
e . preventDefault ( ) ;
610
630
var $t = jQuery ( this ) ,
@@ -901,6 +921,10 @@ var unbxdSearchInit = function(jQuery, Handlebars){
901
921
}
902
922
} , 3000 ) ;
903
923
}
924
+
925
+ if ( this . options . searchResultSetTemp !== null && typeof this . options . searchResultSetTemp === 'object' ) {
926
+ jQuery ( this . options . viewTypeContainerSelector ) . on ( "click" , '[unbxdviewtype]' , changeViewType ) ;
927
+ }
904
928
}
905
929
, addSort : function ( field , dir ) {
906
930
this . params . sort [ field ] = dir || "desc" ;
@@ -980,6 +1004,15 @@ var unbxdSearchInit = function(jQuery, Handlebars){
980
1004
, getPageSize : function ( ) {
981
1005
return this . params . extra . rows ;
982
1006
}
1007
+
1008
+ , setViewType : function ( viewType ) {
1009
+ this . params . extra . view = viewType ;
1010
+ return this ;
1011
+ }
1012
+ , getViewType : function ( ) {
1013
+ return this . params . extra . view ;
1014
+ }
1015
+
983
1016
, addQueryParam : function ( key , value , dontOverried ) {
984
1017
if ( ! ( key in this . params . extra ) || ! dontOverried ) {
985
1018
this . params . extra [ key ] = value ;
@@ -1086,7 +1119,7 @@ var unbxdSearchInit = function(jQuery, Handlebars){
1086
1119
}
1087
1120
} else if ( key === 'wt' || key === 'format' ) {
1088
1121
nonhistoryPath += '&' + key + '=' + encodeURIComponent ( value ) ;
1089
- } else if ( this . isUsingPagination ( ) && key === 'rows' ) {
1122
+ } else if ( ( this . isUsingPagination ( ) && key === 'rows' ) || key === "view" ) {
1090
1123
url += '&' + key + '=' + encodeURIComponent ( value ) ;
1091
1124
} else if ( this . defaultParams . hasOwnProperty ( 'extra' ) && this . defaultParams . extra . hasOwnProperty ( key ) ) {
1092
1125
nonhistoryPath += '&' + key + '=' + encodeURIComponent ( value ) ;
@@ -1182,6 +1215,7 @@ var unbxdSearchInit = function(jQuery, Handlebars){
1182
1215
format : "json"
1183
1216
, page : 1
1184
1217
, rows : 12
1218
+ , view : ( this . options . viewTypes !== undefined && this . options . viewTypes . length > 0 ? this . options . viewTypes [ 0 ] : "" )
1185
1219
}
1186
1220
} ;
1187
1221
@@ -1275,6 +1309,12 @@ var unbxdSearchInit = function(jQuery, Handlebars){
1275
1309
if ( "start" in obj )
1276
1310
params . extra . page = ( parseInt ( obj . start ) / parseInt ( params . extra . rows ) ) + 1 ;
1277
1311
1312
+ if ( ! ( "view" in obj ) ) {
1313
+ params . extra . view = ( this . options . viewTypes !== undefined && this . options . viewTypes . length > 0 ? this . options . viewTypes [ 0 ] : "" ) ;
1314
+ } else {
1315
+ params . extra . view = obj [ "view" ] ;
1316
+ }
1317
+
1278
1318
return params ;
1279
1319
}
1280
1320
, paintResultSet : function ( obj ) {
@@ -1384,6 +1424,16 @@ var unbxdSearchInit = function(jQuery, Handlebars){
1384
1424
1385
1425
if ( this . getClass ( this . options . searchResultSetTemp ) == 'Function' ) {
1386
1426
this . options . searchResultSetTemp . call ( this , obj ) ;
1427
+ } else if ( this . options . searchResultSetTemp !== null && typeof this . options . searchResultSetTemp === 'object' ) {
1428
+ this . paintViewTypes ( obj ) ;
1429
+ var currentViewType = this . getViewType ( ) ;
1430
+ if ( ! this . compiledResultTemp ) {
1431
+ this . compiledResultTemp = { } ;
1432
+ this . options . viewTypes . forEach ( function ( val ) {
1433
+ this . compiledResultTemp [ val ] = Handlebars . compile ( this . options . searchResultSetTemp [ val ] ) ;
1434
+ } . bind ( this ) ) ;
1435
+ }
1436
+ jQuery ( this . options . searchResultContainer ) . html ( this . compiledResultTemp [ currentViewType ] ( obj . response ) ) ;
1387
1437
} else {
1388
1438
if ( ! this . compiledResultTemp )
1389
1439
this . compiledResultTemp = Handlebars . compile ( this . options . searchResultSetTemp ) ;
@@ -1452,6 +1502,28 @@ var unbxdSearchInit = function(jQuery, Handlebars){
1452
1502
options : pageSizeOptions
1453
1503
} ) ) ;
1454
1504
}
1505
+
1506
+ , paintViewTypes : function ( obj ) {
1507
+ if ( "error" in obj || this . options . viewTypeContainerSelector . length <= 0 ) {
1508
+ return ;
1509
+ }
1510
+ if ( ! this . compiledViewTypesContainerTemp ) {
1511
+ this . compiledViewTypesContainerTemp = Handlebars . compile ( this . options . viewTypeContainerTemp ) ;
1512
+ }
1513
+ var viewTypeOptions = this . options . viewTypes . map ( function ( opt ) {
1514
+
1515
+ var values = { } ;
1516
+ values [ "value" ] = opt ;
1517
+ values [ "selected" ] = this . getViewType ( ) == opt ? true : false ;
1518
+ return values ;
1519
+
1520
+ } . bind ( this ) ) ;
1521
+ jQuery ( this . options . viewTypeContainerSelector ) . html ( this . compiledViewTypesContainerTemp ( {
1522
+ options : viewTypeOptions
1523
+ } ) ) ;
1524
+ return this . getViewType ( ) ;
1525
+ }
1526
+
1455
1527
, paintPagination : function ( obj ) {
1456
1528
if ( "error" in obj )
1457
1529
return ;
0 commit comments