relative thRoot path

classic Classic list List threaded Threaded
9 messages Options
Reply | Threaded
Open this post in threaded view
|

relative thRoot path

dwallace
Is there anyway to specify the thRoot as a relative path so that developers don't have to put the project in the same directory as others?
Reply | Threaded
Open this post in threaded view
|

Re: relative thRoot path

jjbenson
Administrator
I don't think there's an easy generic solution to this problem because ultimately it's the end-user's browser that controls file access.

Having said that, Thymol has 3 basic file loading control parameters that can be used cover most scenarios these are: thProtocol, thRoot and thPath. Each of these has a one-to-one mapping to a Thymol internal variable:

  thProtocol => thymol.protocol
  thRoot => thymol.root
  thPath => thymol.path

If you need to, you can force the value of any Thymol internal value in a configurePreExecution function.

For example:

        thymol.ready(function () {
          thymol.configurePreExecution( function() {
                  ...
                  thymol.root = <some specified value>;
                  ...
          });
        });


Reply | Threaded
Open this post in threaded view
|

Re: relative thRoot path

dwallace
This post was updated on .
Post edit... oops. I just realized I was testing the wrong file. I'll post again with my findings. Thanks.
Reply | Threaded
Open this post in threaded view
|

Re: relative thRoot path

dwallace
What I ended up doing was writing a helper function to figure out the root path for you given the page uri, the thymol script path (relative), and the relative path from the directory thymol is in to the root path. So, in the thymol.js file code just after the "path" variable is defined, I set the global variable thRoot using the function:
    thRoot = getRootPath(location.href, path, '..');

This enables people to check the project out to arbitrary directory locations on their workstation and have thymol still work statically. I was not able to get the configurePreExecution approach to work. It would fire, but seemingly not affect the loaded thymol instance. I might be putting it in the wrong place though.
Reply | Threaded
Open this post in threaded view
|

Re: relative thRoot path

dwallace
Here are my helper functions if anyone finds them useful. A lot of assumptions are made so they could likely be improved quite a bit:
function getRootPath(pageUri, scriptHref, relRootPath) {
    var pageProtocol = getUriProtocol(pageUri);
    var rootPath = '';
    if (pageProtocol == 'file' || pageProtocol == 'http') {
        rootPath = processPath(getPathBase(pageUri) + '/' + getPathBase(scriptHref) + '/' + relRootPath);
        rootPath = rootPath.substring(7);
    }
    if (rootPath.lastIndexOf('/') == rootPath.length - 1)
        rootPath = rootPath.substring(0, rootPath.length - 1);
    return rootPath;
}
function getUriProtocol(uri) {
    var protocol = uri.match(/^[^:]*:/);
    if (protocol != null)
        return protocol[0].substring(0, protocol[0].length - 1);
    return null;
}
function getPathBase(path) {
    var pathBaseEnd = path.lastIndexOf('/');
    if (pathBaseEnd > 0)
        return path.substring(0, pathBaseEnd - 1);
    return '';
}
function processPath(path) {
    var result = path;
    while (result.indexOf('..') >= 0 && result.match(/^\/?(\.\.\/)+\/?$/) == null) {
        var temp = result.replace(/([^\/]*)\/\.\.\/?/,'');
        if (temp == result)
            break;
        result = temp;
    }
    return result;
}
Reply | Threaded
Open this post in threaded view
|

Re: relative thRoot path

dwallace
Along the lines of making the configuration flexible for different developers, I also make use of one of those functions in my thymol configuration object:
        thDefaultProtocol: getUriProtocol(location.href) + "://",
This sets the default protocol to match the protocol used to load the page. It seems like this could be the automatic default thymol uses unless specifically overridden. I can't think of a case where you'd want to though.
Reply | Threaded
Open this post in threaded view
|

Re: relative thRoot path

jjbenson
Administrator
Many thanks for sharing your work!

These suggestions might become useful additions to the Thymol code-base.

Could you please raise an enhancement request at https://github.com/thymol/thymol.js/issues ?
Reply | Threaded
Open this post in threaded view
|

Re: relative thRoot path

dwallace
And thanks for your hard work on thymol! The issue is posted here:
https://github.com/thymol/thymol.js/issues/3
Reply | Threaded
Open this post in threaded view
|

Re: relative thRoot path

jjbenson
Administrator
In reply to this post by dwallace
I've just pushed the code that sets the value of thymol.protocol (thProtocol) to the value used by the current document. The changes haven't been released yet but you can find the updated files here: https://github.com/thymol/thymol.js/tree/2.x-master/dist.