pgadmin4/web/pgadmin/static/js/url_for.js
Ashesh Vashi fd3ddbeafb Implementation of the client side 'url_for(...)' function.
Created a url_for function module to be used by different javascript
modules to use the same functionality of flask at client side.

A python module can expose the list of endpoints, used by its
javascripts, by overriding the 'get_exposed_url_endpoints(self)'
function.

In this patch, we have modified all the browser modules to use this
function. It will allow us to move the majority of the javascript
modules of browser nodes from templates directory to the static
directory.

TODO::
- Move these javascripts modules to the static directory.
- Use this function in all the applicable javascript modules.
  e.g. tools, miscellaneous modules
2017-06-12 12:01:25 +05:30

42 lines
1.1 KiB
JavaScript

define(["pgadmin.browser.endpoints"], function (endpoints) {
/***
* This method behaves as a drop-in replacement for flask url_for function.
* It uses the exposed URLs file under the hood, and replace the substitions provided by the modules.
*
* ex.
* url_for("help.static", {filename: "server_dialog.html"}) will produce the
* output string '/help/help/server_dialog.html' from the url ->
* '/help/help/<path:filename>'.
*
* @param {String} text
* @param {Object} substitutions
*/
return function url_for(endpoint, substitutions) {
var rawURL = endpoints[endpoint];
// captures things of the form <path:substitutionName>
var substitutionGroupsRegExp = /([<])([^:^>]*:)?([^>]+)([>])/g;
var matchFound;
var interpolated = rawURL;
if (!rawURL)
return rawURL;
interpolated = interpolated.replace(
substitutionGroupsRegExp,
function (_origin, _1, _2, substitutionName) {
if (substitutionName in substitutions) {
return substitutions[substitutionName];
}
return _origin;
}
);
return interpolated;
};
});