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