PageRenderTime 32ms CodeModel.GetById 14ms app.highlight 12ms RepoModel.GetById 1ms app.codeStats 0ms

/Objective-J/Bootstrap.js

http://github.com/cacaodev/cappuccino
JavaScript | 157 lines | 93 code | 34 blank | 30 comment | 21 complexity | 6e261dc92a56b3b0c8aad9fd8a6475ee MD5 | raw file
  1/*
  2 * Bootstrap.js
  3 * Objective-J
  4 *
  5 * Created by Francisco Tolmasky.
  6 * Copyright 2010, 280 North, Inc.
  7 *
  8 * This library is free software; you can redistribute it and/or
  9 * modify it under the terms of the GNU Lesser General Public
 10 * License as published by the Free Software Foundation; either
 11 * version 2.1 of the License, or (at your option) any later version.
 12 *
 13 * This library is distributed in the hope that it will be useful,
 14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 16 * Lesser General Public License for more details.
 17 *
 18 * You should have received a copy of the GNU Lesser General Public
 19 * License along with this library; if not, write to the Free Software
 20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 21 */
 22
 23
 24#ifdef COMMONJS
 25var mainBundleURL = new CFURL("file:" + require("file").cwd()).asDirectoryPathURL();
 26#elif defined(BROWSER)
 27// This is automatic when importing, but we'd like these important URLs to
 28// be taken into consideration in the cache as well.
 29enableCFURLCaching();
 30
 31// To determine where our application lives, start with the current URL of the page.
 32var pageURL = new CFURL(window.location.href),
 33
 34// Look for any <base> tags and choose the last one (which is the one that will take effect).
 35    DOMBaseElements = document.getElementsByTagName("base"),
 36    DOMBaseElementsCount = DOMBaseElements.length;
 37
 38if (DOMBaseElementsCount > 0)
 39{
 40    var DOMBaseElement = DOMBaseElements[DOMBaseElementsCount - 1],
 41        DOMBaseElementHref = DOMBaseElement && DOMBaseElement.getAttribute("href");
 42
 43    // If we have one, use it instead.
 44    if (DOMBaseElementHref)
 45        pageURL = new CFURL(DOMBaseElementHref, pageURL);
 46}
 47
 48// Turn the main file into a URL.
 49var mainFileURL = new CFURL(window.OBJJ_MAIN_FILE || "main.j"),
 50
 51// The main bundle is the containing folder of the main file.
 52    mainBundleURL = new CFURL(".", new CFURL(mainFileURL, pageURL)).absoluteURL(),
 53
 54// We assume the "first part" of the path is completely resolved.
 55    assumedResolvedURL = new CFURL("..", mainBundleURL).absoluteURL();
 56
 57// .. doesn't work if we're already at root, so "go back" one more level to the scheme and authority.
 58if (mainBundleURL === assumedResolvedURL)
 59    assumedResolvedURL = new CFURL(assumedResolvedURL.schemeAndAuthority());
 60
 61StaticResource.resourceAtURL(assumedResolvedURL, YES);
 62
 63exports.pageURL = pageURL;
 64
 65exports.bootstrap = function()
 66{
 67    resolveMainBundleURL();
 68}
 69
 70function resolveMainBundleURL()
 71{
 72    StaticResource.resolveResourceAtURL(mainBundleURL, YES, function(/*StaticResource*/ aResource)
 73    {
 74        var includeURLs = StaticResource.includeURLs(),
 75            index = 0,
 76            count = includeURLs.length;
 77
 78        for (; index < count; ++index)
 79            aResource.resourceAtURL(includeURLs[index], YES);
 80
 81        Executable.fileImporterForURL(mainBundleURL)(mainFileURL.lastPathComponent(), YES, function()
 82        {
 83            disableCFURLCaching();
 84            afterDocumentLoad(function()
 85            {
 86                var hashString = window.location.hash.substring(1),
 87                    args = [];
 88
 89                if (hashString.length)
 90                {
 91                    args = hashString.split("/");
 92                    for (var i = 0, count = args.length; i < count; i++)
 93                        args[i] = decodeURIComponent(args[i]);
 94                }
 95
 96                var namedArgsArray = window.location.search.substring(1).split("&"),
 97                    namedArgs = new CFMutableDictionary();
 98
 99                for (var i = 0, count = namedArgsArray.length; i < count; i++)
100                {
101                    var thisArg = namedArgsArray[i].split("=");
102
103                    if (!thisArg[0])
104                        continue;
105
106                    if (thisArg[1] == null)
107                        thisArg[1] = true;
108
109                    namedArgs.setValueForKey(decodeURIComponent(thisArg[0]), decodeURIComponent(thisArg[1]));
110                }
111
112                main(args, namedArgs);
113            });
114        });
115    });
116}
117
118var documentLoaded = NO;
119
120function afterDocumentLoad(/*Function*/ aFunction)
121{
122    if (documentLoaded || document.readyState === "complete")
123        return aFunction();
124
125    if (window.addEventListener)
126        window.addEventListener("load", aFunction, NO);
127
128    else if (window.attachEvent)
129        window.attachEvent("onload", aFunction);
130}
131
132afterDocumentLoad(function()
133{
134    documentLoaded = YES;
135});
136
137if (typeof OBJJ_AUTO_BOOTSTRAP === "undefined" || OBJJ_AUTO_BOOTSTRAP)
138    exports.bootstrap();
139
140#endif
141
142function makeAbsoluteURL(/*CFURL|String*/ aURL)
143{
144    if (aURL instanceof CFURL && aURL.scheme())
145        return aURL;
146
147    return new CFURL(aURL, mainBundleURL);
148}
149
150GLOBAL(objj_importFile) = Executable.fileImporterForURL(mainBundleURL);
151GLOBAL(objj_executeFile) = Executable.fileExecuterForURL(mainBundleURL);
152
153GLOBAL(objj_import) = function()
154{
155    CPLog.warn("objj_import is deprecated, use objj_importFile instead");
156    objj_importFile.apply(this, arguments);
157}