| 1 | efrain | 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 | /**
 | 
        
           |  |  | 17 |  * URL utility functions.
 | 
        
           |  |  | 18 |  *
 | 
        
           |  |  | 19 |  * @module     core/url
 | 
        
           |  |  | 20 |  * @copyright  2015 Damyon Wiese <damyon@moodle.com>
 | 
        
           |  |  | 21 |  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 | 
        
           |  |  | 22 |  * @since      2.9
 | 
        
           |  |  | 23 |  */
 | 
        
           |  |  | 24 | define(['jquery', 'core/config'], function($, config) {
 | 
        
           |  |  | 25 |   | 
        
           |  |  | 26 |   | 
        
           |  |  | 27 |     return /** @alias module:core/url */ {
 | 
        
           |  |  | 28 |         // Public variables and functions.
 | 
        
           |  |  | 29 |         /**
 | 
        
           |  |  | 30 |          * Construct a file url
 | 
        
           |  |  | 31 |          *
 | 
        
           |  |  | 32 |          * @method fileUrl
 | 
        
           |  |  | 33 |          * @param {string} relativeScript
 | 
        
           |  |  | 34 |          * @param {string} slashArg
 | 
        
           |  |  | 35 |          * @return {string}
 | 
        
           |  |  | 36 |          */
 | 
        
           |  |  | 37 |         fileUrl: function(relativeScript, slashArg) {
 | 
        
           |  |  | 38 |   | 
        
           |  |  | 39 |             var url = config.wwwroot + relativeScript;
 | 
        
           |  |  | 40 |   | 
        
           |  |  | 41 |             // Force a /
 | 
        
           |  |  | 42 |             if (slashArg.charAt(0) != '/') {
 | 
        
           |  |  | 43 |                 slashArg = '/' + slashArg;
 | 
        
           |  |  | 44 |             }
 | 
        
           |  |  | 45 |             if (config.slasharguments) {
 | 
        
           |  |  | 46 |                 url += slashArg;
 | 
        
           |  |  | 47 |             } else {
 | 
        
           |  |  | 48 |                 url += '?file=' + encodeURIComponent(slashArg);
 | 
        
           |  |  | 49 |             }
 | 
        
           |  |  | 50 |             return url;
 | 
        
           |  |  | 51 |         },
 | 
        
           |  |  | 52 |   | 
        
           |  |  | 53 |         /**
 | 
        
           |  |  | 54 |          * Take a path relative to the moodle basedir and do some fixing (see class moodle_url in php).
 | 
        
           |  |  | 55 |          *
 | 
        
           |  |  | 56 |          * @method relativeUrl
 | 
        
           |  |  | 57 |          * @param {string} relativePath The path relative to the moodle basedir.
 | 
        
           |  |  | 58 |          * @param {object} params The query parameters for the URL.
 | 
        
           |  |  | 59 |          * @param {bool} includeSessKey Add the session key to the query params.
 | 
        
           |  |  | 60 |          * @return {string}
 | 
        
           |  |  | 61 |          */
 | 
        
           |  |  | 62 |         relativeUrl: function(relativePath, params, includeSessKey) {
 | 
        
           |  |  | 63 |   | 
        
           |  |  | 64 |             if (relativePath.indexOf('http:') === 0 || relativePath.indexOf('https:') === 0 || relativePath.indexOf('://') >= 0) {
 | 
        
           |  |  | 65 |                 throw new Error('relativeUrl function does not accept absolute urls');
 | 
        
           |  |  | 66 |             }
 | 
        
           |  |  | 67 |   | 
        
           |  |  | 68 |             // Fix non-relative paths;
 | 
        
           |  |  | 69 |             if (relativePath.charAt(0) != '/') {
 | 
        
           |  |  | 70 |                 relativePath = '/' + relativePath;
 | 
        
           |  |  | 71 |             }
 | 
        
           |  |  | 72 |   | 
        
           |  |  | 73 |             // Fix admin urls.
 | 
        
           |  |  | 74 |             if (config.admin !== 'admin') {
 | 
        
           |  |  | 75 |                 relativePath = relativePath.replace(/^\/admin\//, '/' + config.admin + '/');
 | 
        
           |  |  | 76 |             }
 | 
        
           |  |  | 77 |   | 
        
           |  |  | 78 |             params = params || {};
 | 
        
           |  |  | 79 |             if (includeSessKey) {
 | 
        
           |  |  | 80 |                 params.sesskey = config.sesskey;
 | 
        
           |  |  | 81 |             }
 | 
        
           |  |  | 82 |   | 
        
           |  |  | 83 |             var queryString = '';
 | 
        
           |  |  | 84 |             if (Object.keys(params).length) {
 | 
        
           |  |  | 85 |                 queryString = $.map(params, function(value, param) {
 | 
        
           |  |  | 86 |                     return param + '=' + value;
 | 
        
           |  |  | 87 |                 }).join('&');
 | 
        
           |  |  | 88 |             }
 | 
        
           |  |  | 89 |   | 
        
           |  |  | 90 |             if (queryString !== '') {
 | 
        
           |  |  | 91 |                 return config.wwwroot + relativePath + '?' + queryString;
 | 
        
           |  |  | 92 |             } else {
 | 
        
           |  |  | 93 |                 return config.wwwroot + relativePath;
 | 
        
           |  |  | 94 |             }
 | 
        
           |  |  | 95 |         },
 | 
        
           |  |  | 96 |   | 
        
           |  |  | 97 |         /**
 | 
        
           |  |  | 98 |          * Wrapper for image_url function.
 | 
        
           |  |  | 99 |          *
 | 
        
           |  |  | 100 |          * @method imageUrl
 | 
        
           |  |  | 101 |          * @param {string} imagename The image name (e.g. t/edit).
 | 
        
           |  |  | 102 |          * @param {string} component The component (e.g. mod_feedback).
 | 
        
           |  |  | 103 |          * @return {string}
 | 
        
           |  |  | 104 |          */
 | 
        
           |  |  | 105 |         imageUrl: function(imagename, component) {
 | 
        
           |  |  | 106 |             return M.util.image_url(imagename, component);
 | 
        
           |  |  | 107 |         }
 | 
        
           |  |  | 108 |     };
 | 
        
           |  |  | 109 | });
 |