2015-08-17 17:00:26 -07:00
/ * *
* @ file
* Some basic behaviors and utility functions for Views .
* /
( function ( $ , Drupal , drupalSettings ) {
2015-10-21 21:44:50 -07:00
'use strict' ;
2015-08-17 17:00:26 -07:00
/ * *
* @ namespace
* /
Drupal . Views = { } ;
/ * *
* Helper function to parse a querystring .
*
* @ param { string } query
2015-09-04 13:20:09 -07:00
* The querystring to parse .
2015-08-17 17:00:26 -07:00
*
* @ return { object }
2015-09-04 13:20:09 -07:00
* A map of query parameters .
2015-08-17 17:00:26 -07:00
* /
Drupal . Views . parseQueryString = function ( query ) {
var args = { } ;
var pos = query . indexOf ( '?' ) ;
if ( pos !== - 1 ) {
query = query . substring ( pos + 1 ) ;
}
var pair ;
var pairs = query . split ( '&' ) ;
for ( var i = 0 ; i < pairs . length ; i ++ ) {
pair = pairs [ i ] . split ( '=' ) ;
// Ignore the 'q' path argument, if present.
if ( pair [ 0 ] !== 'q' && pair [ 1 ] ) {
args [ decodeURIComponent ( pair [ 0 ] . replace ( /\+/g , ' ' ) ) ] = decodeURIComponent ( pair [ 1 ] . replace ( /\+/g , ' ' ) ) ;
}
}
return args ;
} ;
/ * *
* Helper function to return a view ' s arguments based on a path .
*
* @ param { string } href
2015-09-04 13:20:09 -07:00
* The href to check .
2015-08-17 17:00:26 -07:00
* @ param { string } viewPath
2015-09-04 13:20:09 -07:00
* The views path to check .
2015-08-17 17:00:26 -07:00
*
* @ return { object }
2015-09-04 13:20:09 -07:00
* An object containing ` view_args ` and ` view_path ` .
2015-08-17 17:00:26 -07:00
* /
Drupal . Views . parseViewArgs = function ( href , viewPath ) {
var returnObj = { } ;
var path = Drupal . Views . getPath ( href ) ;
// Ensure we have a correct path.
if ( viewPath && path . substring ( 0 , viewPath . length + 1 ) === viewPath + '/' ) {
2015-12-02 11:38:43 -08:00
returnObj . view _args = decodeURIComponent ( path . substring ( viewPath . length + 1 , path . length ) ) ;
2015-08-17 17:00:26 -07:00
returnObj . view _path = path ;
}
return returnObj ;
} ;
/ * *
* Strip off the protocol plus domain from an href .
*
* @ param { string } href
2015-09-04 13:20:09 -07:00
* The href to strip .
2015-08-17 17:00:26 -07:00
*
* @ return { string }
2015-09-04 13:20:09 -07:00
* The href without the protocol and domain .
2015-08-17 17:00:26 -07:00
* /
Drupal . Views . pathPortion = function ( href ) {
// Remove e.g. http://example.com if present.
var protocol = window . location . protocol ;
if ( href . substring ( 0 , protocol . length ) === protocol ) {
// 2 is the length of the '//' that normally follows the protocol.
href = href . substring ( href . indexOf ( '/' , protocol . length + 2 ) ) ;
}
return href ;
} ;
/ * *
* Return the Drupal path portion of an href .
*
* @ param { string } href
2015-09-04 13:20:09 -07:00
* The href to check .
2015-08-17 17:00:26 -07:00
*
* @ return { string }
2015-09-04 13:20:09 -07:00
* An internal path .
2015-08-17 17:00:26 -07:00
* /
Drupal . Views . getPath = function ( href ) {
href = Drupal . Views . pathPortion ( href ) ;
href = href . substring ( drupalSettings . path . baseUrl . length , href . length ) ;
// 3 is the length of the '?q=' added to the url without clean urls.
if ( href . substring ( 0 , 3 ) === '?q=' ) {
href = href . substring ( 3 , href . length ) ;
}
var chars = [ '#' , '?' , '&' ] ;
for ( var i = 0 ; i < chars . length ; i ++ ) {
if ( href . indexOf ( chars [ i ] ) > - 1 ) {
href = href . substr ( 0 , href . indexOf ( chars [ i ] ) ) ;
}
}
return href ;
} ;
} ) ( jQuery , Drupal , drupalSettings ) ;