Skip to content
Snippets Groups Projects
Select Git revision
  • 7e955aed48defa9f238f1cb54cddd46e65026980
  • main default protected
  • step-3263
  • feature/plugins-cli
  • feature/vite
  • step-2484-peerreview
  • biest/issue-5051
  • tests/simplify-jsonapi-tests
  • fix/typo-in-1a70031
  • feature/broadcasting
  • database-seeders-and-factories
  • feature/peer-review-2
  • feature-feedback-jsonapi
  • feature/peerreview
  • feature/balloon-plus
  • feature/stock-images-unsplash
  • tic-2588
  • 5.0
  • 5.2
  • biest/unlock-blocks
  • biest-1514
21 results

VirtualFolderType.php

Blame
  • Forked from Stud.IP / Stud.IP
    Source project has a limited visibility.
    Code owners
    Assign users and groups as approvers for specific file changes. Learn more.
    url_helper.ts 2.30 KiB
    /**
     * This class helps to handle URLs of hyperlinks and change their parameters.
     * For example a javascript-page may open an item and the user expects other links
     * on the same page to "know" that this item is now open. But because we don't use
     * PHP session-variables here, this is difficult to use. This class can help. You
     * can overwrite the href-attribute of the link by:
     *
     *  [code]
     *  link.href = STUDIP.URLHelper.getURL("adresse.php?hello=world#anchor");
     *  [/code]
     * Returns something like:
     * "http://uni-adresse.de/studip/adresse.php?hello=world&mandatory=parameter#anchor"
     */
    
    class URLHelper {
        base_url: string;
        parameters: Record<string, string>;
    
        constructor(base_url: string = "", parameters: any = {}) {
            //the base url for all links
            this.base_url = base_url;
    
            // bound link parameters
            this.parameters = parameters;
        }
    
        /**
         * returns a readily encoded URL with the mandatory parameters and additionally passed
         * parameters.
         *
         * @param url string: any url-string
         * @param param_object map: associative object for extra values
         * @param ignore_params boolean: ignore previously bound parameters
         * @return: url with all necessary and additional parameters, encoded
         */
        getURL(url: string, param_object: any = {}, ignore_params: boolean = false): string {
            let result;
    
            if (url === '' || url.match(/^[?#]/)) {
                result = new URL(url, location.href.replace(/\?.*/, ''));
            } else {
                result = new URL(url, this.base_url);
            }
    
            if (!ignore_params) {
                for (const key in this.parameters) {
                    if (result.searchParams.has(key) || this.parameters[key] === null) {
                        continue;
                    }
    
                    result.searchParams.set(key, this.parameters[key]);
                }
            }
    
            for (const key in param_object) {
                if (param_object[key] !== null) {
                    result.searchParams.set(key, param_object[key]);
                } else {
                    result.searchParams.delete(key);
                }
            }
    
            return result.href;
        }
    }
    
    export function createURLHelper(config: { base_url?: string, parameters?: Record<string, string>}): URLHelper {
        return new URLHelper(config?.base_url, config?.parameters);
    }