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