Security update for Core, with self-updated composer
[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[decodeURIComponent(pair[0].replace(/\+/g, ' '))] = decodeURIComponent(pair[1].replace(/\+/g, ' '));
34       }
35     }
36     return args;
37   };
38
39   /**
40    * Helper function to return a view's arguments based on a path.
41    *
42    * @param {string} href
43    *   The href to check.
44    * @param {string} viewPath
45    *   The views path to check.
46    *
47    * @return {object}
48    *   An object containing `view_args` and `view_path`.
49    */
50   Drupal.Views.parseViewArgs = function (href, viewPath) {
51     const returnObj = {};
52     const path = Drupal.Views.getPath(href);
53     // Get viewPath url without baseUrl portion.
54     const viewHref = Drupal.url(viewPath).substring(drupalSettings.path.baseUrl.length);
55     // Ensure we have a correct path.
56     if (viewHref && path.substring(0, viewHref.length + 1) === `${viewHref}/`) {
57       returnObj.view_args = decodeURIComponent(path.substring(viewHref.length + 1, path.length));
58       returnObj.view_path = path;
59     }
60     return returnObj;
61   };
62
63   /**
64    * Strip off the protocol plus domain from an href.
65    *
66    * @param {string} href
67    *   The href to strip.
68    *
69    * @return {string}
70    *   The href without the protocol and domain.
71    */
72   Drupal.Views.pathPortion = function (href) {
73     // Remove e.g. http://example.com if present.
74     const protocol = window.location.protocol;
75     if (href.substring(0, protocol.length) === protocol) {
76       // 2 is the length of the '//' that normally follows the protocol.
77       href = href.substring(href.indexOf('/', protocol.length + 2));
78     }
79     return href;
80   };
81
82   /**
83    * Return the Drupal path portion of an href.
84    *
85    * @param {string} href
86    *   The href to check.
87    *
88    * @return {string}
89    *   An internal path.
90    */
91   Drupal.Views.getPath = function (href) {
92     href = Drupal.Views.pathPortion(href);
93     href = href.substring(drupalSettings.path.baseUrl.length, href.length);
94     // 3 is the length of the '?q=' added to the url without clean urls.
95     if (href.substring(0, 3) === '?q=') {
96       href = href.substring(3, href.length);
97     }
98     const chars = ['#', '?', '&'];
99     for (let i = 0; i < chars.length; i++) {
100       if (href.indexOf(chars[i]) > -1) {
101         href = href.substr(0, href.indexOf(chars[i]));
102       }
103     }
104     return href;
105   };
106 }(jQuery, Drupal, drupalSettings));