@@ -24,8 +24,7 @@ import {
2424 DefaultSortOption ,
2525 CollectionSortOption ,
2626 HAS_DATE_TAKEN ,
27- TAKEN_SORT ,
28- SortOptions
27+ TAKEN_SORT
2928} from "../components/gr-sort-control/gr-sort-control-config" ;
3029
3130export var query = angular . module ( 'kahuna.search.query' , [
@@ -190,7 +189,7 @@ query.controller('SearchQueryCtrl', [
190189 }
191190
192191 function checkForCollection ( query ) {
193- return / ~ " [ a - z A - Z 0 - 9 # - _ . : / / ] + " / . test ( query )
192+ return / ~ " [ a - z A - Z 0 - 9 # - _ . : / / ] + " / . test ( query ) ;
194193 } ;
195194
196195 function storeCollection ( query ) {
@@ -214,6 +213,29 @@ query.controller('SearchQueryCtrl', [
214213 storage . setJs ( "priorOrderBy" , priorOrderBy ) ;
215214 }
216215
216+ function revisedOrderBy ( collectionSearch ) {
217+ if ( collectionSearch ) {
218+ return CollectionSortOption . value ;
219+ } else {
220+ return DefaultSortOption . value ;
221+ }
222+ }
223+
224+ function priorRevisedOrderBy ( collectionSearch , newCollection , oldCollection ) {
225+ const priorOrderBy = getPiorOrderBy ( ) ;
226+ if ( collectionSearch && ( ( oldCollection !== newCollection ) || ( "" !== priorOrderBy ) ) ) {
227+ if ( priorOrderBy != "" ) {
228+ setPriorOrderBy ( "" ) ;
229+ return priorOrderBy ;
230+ } else {
231+ setPriorOrderBy ( CollectionSortOption . value ) ;
232+ return null ;
233+ }
234+ } else {
235+ return null ;
236+ }
237+ }
238+
217239 // eslint-disable-next-line complexity
218240 function watchSearchChange ( newFilter , sender ) {
219241 let showPaid = newFilter . nonFree ? newFilter . nonFree : false ;
@@ -223,7 +245,6 @@ query.controller('SearchQueryCtrl', [
223245 storage . setJs ( "isNonFree" , showPaid , true ) ;
224246
225247 // check for taken date sort contradiction
226- const oldOrderBy = storage . getJs ( "orderBy" ) ;
227248 const curCollectionSearch = ctrl . collectionSearch ;
228249 ctrl . collectionSearch = newFilter . query ? checkForCollection ( newFilter . query ) : false ;
229250 const oldCollection = getCollection ( ) ;
@@ -234,22 +255,10 @@ query.controller('SearchQueryCtrl', [
234255 ctrl . ordering [ "orderBy" ] = $stateParams . orderBy ;
235256 }
236257 if ( $stateParams . orderBy && $stateParams . orderBy . includes ( TAKEN_SORT ) && ( ! newFilter . query || ! newFilter . query . includes ( HAS_DATE_TAKEN ) ) ) {
237- if ( ctrl . collectionSearch ) {
238- ctrl . ordering [ "orderBy" ] = CollectionSortOption . value ;
239- } else {
240- ctrl . ordering [ "orderBy" ] = DefaultSortOption . value ;
241- }
258+ ctrl . ordering [ "orderBy" ] = revisedOrderBy ( ctrl . collectionSearch ) ;
242259 } else {
243- const priorOrderBy = getPiorOrderBy ( ) ;
244- if ( ctrl . collectionSearch && ( ( oldCollection !== newCollection ) || ( "" !== priorOrderBy ) ) ) {
245- if ( priorOrderBy != "" ) {
246- ctrl . ordering [ "orderBy" ] = priorOrderBy ;
247- setPriorOrderBy ( "" ) ;
248- } else {
249- //console.log("Old collection="+oldCollection + ", New collection=" + newCollection + ", PriorOrderBy=" +priorOrderBy);
250- setPriorOrderBy ( CollectionSortOption . value ) ;
251- }
252- }
260+ const prior = priorRevisedOrderBy ( ctrl . collectionSearch , newCollection , oldCollection ) ;
261+ ctrl . ordering [ "orderBy" ] = prior ? prior : ctrl . ordering [ "orderBy" ] ;
253262 }
254263 }
255264 let sortBy = ctrl . ordering [ "orderBy" ] ? ctrl . ordering [ "orderBy" ] : DefaultSortOption . value ;
0 commit comments