DomUtil, DomEvent, namespace config improvements, some specs

This commit is contained in:
mourner 2010-09-07 14:27:44 +03:00
parent f65df4972f
commit 42bcb9b3b3
10 changed files with 139 additions and 24 deletions

View File

@ -12,7 +12,7 @@
L = 'test'; //to test L#noConflict later
</script>
<script type="text/javascript" src="../src/env.js"></script>
<script type="text/javascript" src="../src/Leaflet.js"></script>
<!-- /core -->
<script type="text/javascript" src="../src/core/Util.js"></script>
@ -27,10 +27,15 @@
<!-- /geo -->
<script type="text/javascript" src="../src/geo/LatLng.js"></script>
<script type="text/javascript" src="../src/geo/Projection.js"></script>
<!-- /dom -->
<script type="text/javascript" src="../src/dom/DomEvent.js"></script>
<script type="text/javascript" src="../src/dom/DomUtil.js"></script>
<!-- spec files -->
<script type="text/javascript" src="suites/envSpec.js"></script>
<script type="text/javascript" src="suites/SpecHelper.js"></script>
<script type="text/javascript" src="suites/LeafletSpec.js"></script>
<!-- /core -->
<script type="text/javascript" src="suites/core/UtilSpec.js"></script>
@ -45,6 +50,11 @@
<!-- /geo -->
<script type="text/javascript" src="suites/geo/LatLngSpec.js"></script>
<script type="text/javascript" src="suites/geo/ProjectionSpec.js"></script>
<!-- /dom -->
<script type="text/javascript" src="suites/dom/DomEventSpec.js"></script>
<script type="text/javascript" src="suites/dom/DomUtilSpec.js"></script>
</head>
<body>

View File

@ -0,0 +1,5 @@
function noSpecs() {
it('should have specs', function() {
expect('specs').toBe();
});
}

View File

@ -51,12 +51,8 @@ describe('Util', function() {
var a = {},
id = L.Util.stamp(a);
expect(typeof a.id).toEqual('number');
expect(a.id).toEqual(id);
L.Util.stamp(a);
expect(a.id).toEqual(id);
expect(typeof id).toEqual('number');
expect(L.Util.stamp(a)).toEqual(id);
var b = {},
id2 = L.Util.stamp(b);

View File

@ -0,0 +1,8 @@
describe('DomEvent', function() {
describe('#addListener', noSpecs);
describe('#removeListener', noSpecs);
describe('#stopPropagation', noSpecs);
describe('#preventDefault', noSpecs);
});

View File

@ -0,0 +1 @@
describe('DomUtil', noSpecs);

14
src/Leaflet.js Normal file
View File

@ -0,0 +1,14 @@
/*
* Leaflet namespace config, allows using any global variable instead of L & restoring the original value
*/
L = {
version: '0.0.1',
_originalL: window.L,
noConflict: function() {
window.L = this._originalL;
return this;
}
};

66
src/dom/DomEvent.js Normal file
View File

@ -0,0 +1,66 @@
/*
* L.DomEvent contains functions for working with DOM events.
*/
L.DomEvent = {
/* inpired by John Resig, Dean Edwards and YUI addEvent implementations */
addListener: function(/*HTMLElement*/ obj, /*String*/ type, /*Function*/ fn, /*Object*/ context) {
var id = L.Util.stamp(fn);
obj['_leaflet_' + type + id] = function handler(e) {
return fn.call(context || obj, e || L.DomEvent._getEvent());
};
if ('addEventListener' in obj) {
if (type == 'mousewheel') {
obj.addEventListener('DOMMouseScroll', handler, false);
}
obj.addEventListener(type, handler, false);
} else if ('attachEvent' in obj) {
obj.attachEvent("on" + type, handler);
}
},
removeListener: function(/*HTMLElement*/ obj, /*String*/ type, /*Function*/ fn) {
var id = L.Util.stamp(fn),
key = '_leaflet_' + type + id;
handler = obj[key];
if ('removeEventListener' in obj) {
if (type == 'mousewheel') {
obj.removeEventListener('DOMMouseScroll', handler, false);
}
obj.removeEventListener(type, handler, false);
} else if ('detachEvent' in obj) {
obj.detachEvent("on" + type, handler);
delete obj[key];
}
},
_getEvent: function()/*->Event*/ {
var e = window.event;
if (!e) {
var caller = arguments.callee.caller;
while (caller) {
e = caller['arguments'][0];
if (e && Event == e.constructor) { break; }
caller = caller.caller;
}
}
return e;
},
stopPropagation: function(/*Event*/ e) {
if (e.stopPropagation) {
e.stopPropagation();
} else {
e.cancelBubble = true;
}
},
preventDefault: function(/*Event*/ e) {
if (e.preventDefault) {
e.preventDefault();
} else {
e.returnValue = false;
}
}
};

31
src/dom/DomUtil.js Normal file
View File

@ -0,0 +1,31 @@
/*
* L.DomUtil contains various utility functions for working with DOM
*/
L.DomUtil = {
getCumulativeOffset: function(el) {
var top = 0,
left = 0;
do {
top += (el.offsetTop - el.scrollTop) || 0;
left += el.offsetLeft || 0;
el = el.offsetParent;
} while (el);
return new L.Point(left, top);
},
// used for disabling text selection while dragging
disableTextSelection: function() {
if (document.selection && document.selection.empty) {
document.selection.empty();
}
if (!L.DomUtil._onselectstart) {
L.DomUtil._onselectstart = document.onselectstart;
document.onselectstart = function() { return false; };
}
},
enableTextSelection: function() {
document.onselectstart = L.DomUtil._onselectstart;
CM.DomEvent._onselectstart = null;
}
};

View File

@ -1,16 +0,0 @@
/*
* Leaflet namespace config, allows using any global variable instead of L & restoring the original value
*/
(function() {
var originalL = window.L;
L = {
version: '0.0.1'
};
L.noConflict = function() {
window.L = originalL;
return this;
};
})();