lib/amd/src/url.js

  1. // This file is part of Moodle - http://moodle.org/
  2. //
  3. // Moodle is free software: you can redistribute it and/or modify
  4. // it under the terms of the GNU General Public License as published by
  5. // the Free Software Foundation, either version 3 of the License, or
  6. // (at your option) any later version.
  7. //
  8. // Moodle is distributed in the hope that it will be useful,
  9. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. // GNU General Public License for more details.
  12. //
  13. // You should have received a copy of the GNU General Public License
  14. // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
  15. /**
  16. * URL utility functions.
  17. *
  18. * @module core/url
  19. * @copyright 2015 Damyon Wiese <damyon@moodle.com>
  20. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  21. * @since 2.9
  22. */
  23. define(['jquery', 'core/config'], function($, config) {
  24. return /** @alias module:core/url */ {
  25. // Public variables and functions.
  26. /**
  27. * Construct a file url
  28. *
  29. * @method fileUrl
  30. * @param {string} relativeScript
  31. * @param {string} slashArg
  32. * @return {string}
  33. */
  34. fileUrl: function(relativeScript, slashArg) {
  35. var url = config.wwwroot + relativeScript;
  36. // Force a /
  37. if (slashArg.charAt(0) != '/') {
  38. slashArg = '/' + slashArg;
  39. }
  40. if (config.slasharguments) {
  41. url += slashArg;
  42. } else {
  43. url += '?file=' + encodeURIComponent(slashArg);
  44. }
  45. return url;
  46. },
  47. /**
  48. * Take a path relative to the moodle basedir and do some fixing (see class moodle_url in php).
  49. *
  50. * @method relativeUrl
  51. * @param {string} relativePath The path relative to the moodle basedir.
  52. * @param {object} params The query parameters for the URL.
  53. * @param {bool} includeSessKey Add the session key to the query params.
  54. * @return {string}
  55. */
  56. relativeUrl: function(relativePath, params, includeSessKey) {
  57. if (relativePath.indexOf('http:') === 0 || relativePath.indexOf('https:') === 0 || relativePath.indexOf('://') >= 0) {
  58. throw new Error('relativeUrl function does not accept absolute urls');
  59. }
  60. // Fix non-relative paths;
  61. if (relativePath.charAt(0) != '/') {
  62. relativePath = '/' + relativePath;
  63. }
  64. // Fix admin urls.
  65. if (config.admin !== 'admin') {
  66. relativePath = relativePath.replace(/^\/admin\//, '/' + config.admin + '/');
  67. }
  68. params = params || {};
  69. if (includeSessKey) {
  70. params.sesskey = config.sesskey;
  71. }
  72. var queryString = '';
  73. if (Object.keys(params).length) {
  74. queryString = $.map(params, function(value, param) {
  75. return param + '=' + value;
  76. }).join('&');
  77. }
  78. if (queryString !== '') {
  79. return config.wwwroot + relativePath + '?' + queryString;
  80. } else {
  81. return config.wwwroot + relativePath;
  82. }
  83. },
  84. /**
  85. * Wrapper for image_url function.
  86. *
  87. * @method imageUrl
  88. * @param {string} imagename The image name (e.g. t/edit).
  89. * @param {string} component The component (e.g. mod_feedback).
  90. * @return {string}
  91. */
  92. imageUrl: function(imagename, component) {
  93. return M.util.image_url(imagename, component);
  94. }
  95. };
  96. });