Skip to content

Commit

Permalink
Made _getScriptLocation smarter, and added it to SingleFile.js also. …
Browse files Browse the repository at this point in the history
…This means that single file builds no longer need to contain OpenLayers.js. r=eleomine (closes #2873)

git-svn-id: http://svn.openlayers.org/trunk/openlayers@10822 dc9f47b5-9b13-0410-9fdd-eb0c1a62fdaf
  • Loading branch information
ahocevar committed Oct 13, 2010
1 parent 8dc19c8 commit 485ebaa
Show file tree
Hide file tree
Showing 10 changed files with 129 additions and 67 deletions.
2 changes: 1 addition & 1 deletion build/full.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

[first]
OpenLayers/SingleFile.js
OpenLayers.js
OpenLayers/BaseTypes.js
OpenLayers/BaseTypes/Class.js
OpenLayers/Util.js
Expand All @@ -15,4 +14,5 @@ Rico/Corner.js

[exclude]
Firebug
OpenLayers.js
OpenLayers/Lang
2 changes: 1 addition & 1 deletion build/library.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

[first]
OpenLayers/SingleFile.js
OpenLayers.js
OpenLayers/BaseTypes.js
OpenLayers/BaseTypes/Class.js
OpenLayers/Util.js
Expand All @@ -16,6 +15,7 @@ Rico/Corner.js

[exclude]
Firebug
OpenLayers.js
OpenLayers/Format/GeoRSS.js
OpenLayers/Format/GML.js
OpenLayers/Format/WKT.js
Expand Down
1 change: 0 additions & 1 deletion build/lite.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

[first]
OpenLayers/SingleFile.js
OpenLayers.js
OpenLayers/BaseTypes.js
OpenLayers/BaseTypes/Class.js
OpenLayers/Util.js
Expand Down
41 changes: 10 additions & 31 deletions lib/OpenLayers.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,55 +14,34 @@
* Before creating the OpenLayers namespace, check to see if
* OpenLayers.singleFile is true. This occurs if the
* OpenLayers/SingleFile.js script is included before this one - as is the
* case with single file builds.
* case with old single file build profiles that included both
* OpenLayers.js and OpenLayers/SingleFile.js.
*/
var singleFile = (typeof OpenLayers == "object" && OpenLayers.singleFile);

/**
* Cache for the script location returned from
* OpenLayers._getScriptLocation
* Relative path of this script.
*/
var scriptLocation;
var scriptName = (!singleFile) ? "lib/OpenLayers.js" : "OpenLayers.js";

/**
* Namespace: OpenLayers
* The OpenLayers object provides a namespace for all things OpenLayers
*/
window.OpenLayers = {

/**
* Property: _scriptName
* {String} Relative path of this script.
*/
_scriptName: (!singleFile) ? "lib/OpenLayers.js" : "OpenLayers.js",

/**
* Function: _getScriptLocation
* Return the path to this script.
*
* Returns:
* {String} Path to this script
*/
_getScriptLocation: function () {
if (scriptLocation != undefined) {
return scriptLocation;
}
scriptLocation = "";
var isOL = new RegExp("(^|(.*?\\/))(" + OpenLayers._scriptName + ")(\\?|$)");

var scripts = document.getElementsByTagName('script');
for (var i=0, len=scripts.length; i<len; i++) {
var src = scripts[i].getAttribute('src');
if (src) {
var match = src.match(isOL);
if(match) {
scriptLocation = match[1];
break;
}
}
}
return scriptLocation;
}
_getScriptLocation: (function() {
var r = new RegExp("(^|(.*?\\/))(" + scriptName + ")(\\?|$)");
var s = document.getElementsByTagName('script');
var l = s[s.length-1].getAttribute("src").match(r)[1];
return (function() { return l; });
})()
};
/**
* OpenLayers.singleFile is a flag indicating this file is being included
Expand Down
7 changes: 6 additions & 1 deletion lib/OpenLayers/SingleFile.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,12 @@
* full text of the license. */

var OpenLayers = {
singleFile: true
singleFile: true,
_getScriptLocation: (function() {
var s = document.getElementsByTagName('script');
var l = s[s.length-1].getAttribute("src").match(/(^|(.*?\/))(OpenLayers\.js)(\?|$)/)[1];
return (function() { return l; });
})()
};


31 changes: 0 additions & 31 deletions tests/OpenLayers.html

This file was deleted.

32 changes: 32 additions & 0 deletions tests/OpenLayers1.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<html>
<head>
<script src="../lib/OpenLayers.js"></script>
<script type="text/javascript">
function test_OpenLayers(t) {
t.plan(1);

var script = document.getElementById("script");

t.eq(OpenLayers._getScriptLocation(), "../", "Script location correctly detected.");
}
</script>
</head>
<body>
</body>
</html>
<html>
<head>
<script src="../lib/OpenLayers.js"></script>
<script type="text/javascript">
function test_OpenLayers(t) {
t.plan(1);

var script = document.getElementById("script");

t.eq(OpenLayers._getScriptLocation(), "../", "Script location correctly detected.");
}
</script>
</head>
<body>
</body>
</html>
38 changes: 38 additions & 0 deletions tests/OpenLayers2.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<html>
<head>
<script src="bogus/1/OpenLayers.js-foo"></script>
<script src="bogus/2/foo-OpenLayers.js"></script>
<script src="../lib/OpenLayers.js?foo"></script>
<script src="bogus/3/after-OpenLayers.js"></script>
<script type="text/javascript">
function test_OpenLayers(t) {
t.plan(1);

var script = document.getElementById("script");

t.eq(OpenLayers._getScriptLocation(), "../", "Script location with search string correctly detected, and not fooled by other scripts.");
}
</script>
</head>
<body>
</body>
</html>
<html>
<head>
<script src="bogus/1/OpenLayers.js-foo"></script>
<script src="bogus/2/foo-OpenLayers.js"></script>
<script src="../lib/OpenLayers.js?foo"></script>
<script src="bogus/3/after-OpenLayers.js"></script>
<script type="text/javascript">
function test_OpenLayers(t) {
t.plan(1);

var script = document.getElementById("script");

t.eq(OpenLayers._getScriptLocation(), "../", "Script location with search string correctly detected, and not fooled by other scripts.");
}
</script>
</head>
<body>
</body>
</html>
38 changes: 38 additions & 0 deletions tests/OpenLayers3.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<html>
<head>
<script>
var OpenLayers = {singleFile: true};
</script>
<script src="../lib/OpenLayers.js"></script>
<script type="text/javascript">
function test_OpenLayers(t) {
t.plan(1);

var script = document.getElementById("script");

t.eq(OpenLayers._getScriptLocation(), "../lib/", "Script location for single file build correctly detected.");
}
</script>
</head>
<body>
</body>
</html>
<html>
<head>
<script>
var OpenLayers = {singleFile: true};
</script>
<script src="../lib/OpenLayers.js"></script>
<script type="text/javascript">
function test_OpenLayers(t) {
t.plan(1);

var script = document.getElementById("script");

t.eq(OpenLayers._getScriptLocation(), "../lib/", "Script location for single file build correctly detected.");
}
</script>
</head>
<body>
</body>
</html>
4 changes: 3 additions & 1 deletion tests/list-tests.html
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,9 @@
<li>Map.html</li>
<li>Marker.html</li>
<li>Marker/Box.html</li>
<li>OpenLayers.html</li>
<li>OpenLayers1.html</li>
<li>OpenLayers2.html</li>
<li>OpenLayers3.html</li>
<li>Popup.html</li>
<li>Popup/Anchored.html</li>
<li>Popup/AnchoredBubble.html</li>
Expand Down

0 comments on commit 485ebaa

Please sign in to comment.