X-Git-Url: http://www.aleph1.co.uk/gitweb/?a=blobdiff_plain;f=web%2Fcore%2Fmodules%2Fviews%2Fjs%2Fbase.es6.js;fp=web%2Fcore%2Fmodules%2Fviews%2Fjs%2Fbase.es6.js;h=88c5db3346b3e2d8681a242648768738a32ed9f2;hb=9917807b03b64faf00f6a1f29dcb6eafc454efa5;hp=0000000000000000000000000000000000000000;hpb=aea91e65e895364e460983b890e295aa5d5540a5;p=yaffs-website diff --git a/web/core/modules/views/js/base.es6.js b/web/core/modules/views/js/base.es6.js new file mode 100644 index 000000000..88c5db334 --- /dev/null +++ b/web/core/modules/views/js/base.es6.js @@ -0,0 +1,106 @@ +/** + * @file + * Some basic behaviors and utility functions for Views. + */ + +(function ($, Drupal, drupalSettings) { + /** + * @namespace + */ + Drupal.Views = {}; + + /** + * Helper function to parse a querystring. + * + * @param {string} query + * The querystring to parse. + * + * @return {object} + * A map of query parameters. + */ + Drupal.Views.parseQueryString = function (query) { + const args = {}; + const pos = query.indexOf('?'); + if (pos !== -1) { + query = query.substring(pos + 1); + } + let pair; + const pairs = query.split('&'); + for (let 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 + * The href to check. + * @param {string} viewPath + * The views path to check. + * + * @return {object} + * An object containing `view_args` and `view_path`. + */ + Drupal.Views.parseViewArgs = function (href, viewPath) { + const returnObj = {}; + const path = Drupal.Views.getPath(href); + // Get viewPath url without baseUrl portion. + const viewHref = Drupal.url(viewPath).substring(drupalSettings.path.baseUrl.length); + // Ensure we have a correct path. + if (viewHref && path.substring(0, viewHref.length + 1) === `${viewHref}/`) { + returnObj.view_args = decodeURIComponent(path.substring(viewHref.length + 1, path.length)); + returnObj.view_path = path; + } + return returnObj; + }; + + /** + * Strip off the protocol plus domain from an href. + * + * @param {string} href + * The href to strip. + * + * @return {string} + * The href without the protocol and domain. + */ + Drupal.Views.pathPortion = function (href) { + // Remove e.g. http://example.com if present. + const 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 + * The href to check. + * + * @return {string} + * An internal path. + */ + 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); + } + const chars = ['#', '?', '&']; + for (let 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));