1
1
import Vue from 'vue/dist/vue.esm'
2
2
3
3
import matestackEventHub from 'js/event-hub'
4
+ import queryParamsHelper from 'js/helpers/query-params-helper'
4
5
5
6
import componentMixin from 'component/component'
6
7
@@ -21,8 +22,8 @@ const componentDef = {
21
22
this . ordering [ key ] = undefined
22
23
}
23
24
var url ;
24
- url = this . updateQueryParams ( this . componentConfig [ "id" ] + "-order-" + key , this . ordering [ key ] )
25
- url = this . updateQueryParams ( this . componentConfig [ "id" ] + "-offset" , 0 , url )
25
+ url = queryParamsHelper . updateQueryParams ( this . componentConfig [ "id" ] + "-order-" + key , this . ordering [ key ] )
26
+ url = queryParamsHelper . updateQueryParams ( this . componentConfig [ "id" ] + "-offset" , 0 , url )
26
27
window . history . pushState ( { matestackApp : true , url : url } , null , url ) ;
27
28
matestackEventHub . $emit ( this . componentConfig [ "id" ] + "-update" )
28
29
this . $forceUpdate ( )
@@ -33,67 +34,17 @@ const componentDef = {
33
34
resetFilter : function ( ) {
34
35
var url ;
35
36
for ( var key in this . filter ) {
36
- url = this . updateQueryParams ( this . componentConfig [ "id" ] + "-filter-" + key , null )
37
+ url = queryParamsHelper . updateQueryParams ( this . componentConfig [ "id" ] + "-filter-" + key , null )
37
38
this . filter [ key ] = null ;
38
39
this . $forceUpdate ( ) ;
39
40
}
40
41
window . history . pushState ( { matestackApp : true , url : url } , null , url ) ;
41
42
matestackEventHub . $emit ( this . componentConfig [ "id" ] + "-update" )
42
- } ,
43
- updateQueryParams : function ( key , value , url ) {
44
- if ( ! url ) url = window . location . href ;
45
- var re = new RegExp ( "([?&])" + key + "=.*?(&|#|$)(.*)" , "gi" ) ,
46
- hash ;
47
-
48
- if ( re . test ( url ) ) {
49
- if ( typeof value !== 'undefined' && value !== null )
50
- return url . replace ( re , '$1' + key + "=" + value + '$2$3' ) ;
51
- else {
52
- hash = url . split ( '#' ) ;
53
- url = hash [ 0 ] . replace ( re , '$1$3' ) . replace ( / ( & | \? ) $ / , '' ) ;
54
- if ( typeof hash [ 1 ] !== 'undefined' && hash [ 1 ] !== null )
55
- url += '#' + hash [ 1 ] ;
56
- return url ;
57
- }
58
- }
59
- else {
60
- if ( typeof value !== 'undefined' && value !== null ) {
61
- var separator = url . indexOf ( '?' ) !== - 1 ? '&' : '?' ;
62
- hash = url . split ( '#' ) ;
63
- url = hash [ 0 ] + separator + key + '=' + value ;
64
- if ( typeof hash [ 1 ] !== 'undefined' && hash [ 1 ] !== null )
65
- url += '#' + hash [ 1 ] ;
66
- return url ;
67
- }
68
- else
69
- return url ;
70
- }
71
- } ,
72
- getQueryParam : function ( name , url ) {
73
- if ( ! url ) url = window . location . href ;
74
- name = name . replace ( / [ \[ \] ] / g, '\\$&' ) ;
75
- var regex = new RegExp ( '[?&]' + name + '(=([^&#]*)|&|#|$)' ) ,
76
- results = regex . exec ( url ) ;
77
- if ( ! results ) return null ;
78
- if ( ! results [ 2 ] ) return '' ;
79
- return decodeURIComponent ( results [ 2 ] . replace ( / \+ / g, ' ' ) ) ;
80
- } ,
81
- queryParamsToObject : function ( ) {
82
- var search = window . location . search . substring ( 1 ) ;
83
- if ( search . length === 0 ) {
84
- return { }
85
- } else {
86
- var result = JSON . parse (
87
- '{"' + search . replace ( / & / g, '","' ) . replace ( / = / g, '":"' ) + '"}' ,
88
- function ( key , value ) { return key === "" ?value :decodeURIComponent ( value ) }
89
- )
90
- return result ;
91
- }
92
43
}
93
44
} ,
94
45
created : function ( ) {
95
46
var self = this ;
96
- var queryParamsObject = this . queryParamsToObject ( )
47
+ var queryParamsObject = queryParamsHelper . queryParamsToObject ( )
97
48
Object . keys ( queryParamsObject ) . forEach ( function ( key ) {
98
49
if ( key . startsWith ( self . componentConfig [ "id" ] + "-order-" ) ) {
99
50
self . ordering [ key . replace ( self . componentConfig [ "id" ] + "-order-" , "" ) ] = queryParamsObject [ key ]
0 commit comments