a5e926000ab989873725a9d1719cebf2388bad42
[yaffs-website] / web / core / modules / views / js / base.es6.js
1 /**
2  * @file
3  * Some basic behaviors and utility functions for Views.
4  */
5
6 (function($, Drupal, drupalSettings) {
7   /**
8    * @namespace
9    */
10   Drupal.Views = {};
11
12   /**
13    * Helper function to parse a querystring.
14    *
15    * @param {string} query
16    *   The querystring to parse.
17    *
18    * @return {object}
19    *   A map of query parameters.
20    */
21   Drupal.Views.parseQueryString = function(query) {
22     const args = {};
23     const pos = query.indexOf('?');
24     if (pos !== -1) {
25       query = query.substring(pos + 1);
26     }
27     let pair;
28     const pairs = query.split('&');
29     for (let i = 0; i < pairs.length; i++) {
30       pair = pairs[i].split('=');
31       // Ignore the 'q' path argument, if present.
32       if (pair[0] !== 'q' && pair[1]) {
33         args[
34           decodeURIComponent(pair[0].replace(/\+/g, ' '))
35         ] = decodeURIComponent(pair[1].replace(/\+/g, ' '));
36       }
37     }
38     return args;
39   };
40
41   /**
42    * Helper function to return a view's arguments based on a path.
43    *
44    * @param {string} href
45    *   The href to check.
46    * @param {string} viewPath
47    *   The views path to check.
48    *
49    * @return {object}
50    *   An object containing `view_args` and `view_path`.
51    */
52   Drupal.Views.parseViewArgs = function(href, viewPath) {
53     const returnObj = {};
54     const path = Drupal.Views.getPath(href);
55     // Get viewPath url without baseUrl portion.
56     const viewHref = Drupal.url(viewPath).substring(
57       drupalSettings.path.baseUrl.length,
58     );
59     // Ensure we have a correct path.
60     if (viewHref && path.substring(0, viewHref.length + 1) === `${viewHref}/`) {
61       returnObj.view_args = decodeURIComponent(
62         path.substring(viewHref.length + 1, path.length),
63       );
64       returnObj.view_path = path;
65     }
66     return returnObj;
67   };
68
69   /**
70    * Strip off the protocol plus domain from an href.
71    *
72    * @param {string} href
73    *   The href to strip.
74    *
75    * @return {string}
76    *   The href without the protocol and domain.
77    */
78   Drupal.Views.pathPortion = function(href) {
79     // Remove e.g. http://example.com if present.
80     const protocol = window.location.protocol;
81     if (href.substring(0, protocol.length) === protocol) {
82       // 2 is the length of the '//' that normally follows the protocol.
83       href = href.substring(href.indexOf('/', protocol.length + 2));
84     }
85     return href;
86   };
87
88   /**
89    * Return the Drupal path portion of an href.
90    *
91    * @param {string} href
92    *   The href to check.
93    *
94    * @return {string}
95    *   An internal path.
96    */
97   Drupal.Views.getPath = function(href) {
98     href = Drupal.Views.pathPortion(href);
99     href = href.substring(drupalSettings.path.baseUrl.length, href.length);
100     // 3 is the length of the '?q=' added to the url without clean urls.
101     if (href.substring(0, 3) === '?q=') {
102       href = href.substring(3, href.length);
103     }
104     const chars = ['#', '?', '&'];
105     for (let i = 0; i < chars.length; i++) {
106       if (href.indexOf(chars[i]) > -1) {
107         href = href.substr(0, href.indexOf(chars[i]));
108       }
109     }
110     return href;
111   };
112 })(jQuery, Drupal, drupalSettings);