s in case we inherit text-align:center from map
in IE.
DragZoomUtil.style([G.mapCover, G.cornerTopDiv, G.cornerRightDiv, G.cornerBottomDiv, G.cornerLeftDiv],
{top: '0px', left: '0px', width: G.mapWidth + 'px', height: G.mapHeight +'px'});
};
/**
* Initializes styles based on global parameters
*/
DragZoomControl.prototype.initStyles_ = function(){
var G = this.globals;
DragZoomUtil.style([G.mapCover, G.cornerTopDiv, G.cornerRightDiv, G.cornerBottomDiv, G.cornerLeftDiv],
{filter: G.style.alphaIE, opacity: G.style.opacity, background:G.style.fillColor});
G.outlineDiv.style.border = G.style.border;
};
/**
* Function called when the zoom button's click event is captured.
*/
DragZoomControl.prototype.buttonclick_ = function(){
var G = this.globals;
G.backButtonDiv.style.display='none';
if (G.mapCover.style.display == 'block') { // reset if clicked before dragging
this.resetDragZoom_();
if (G.options.backButtonEnabled) {
this.restoreBackContext_(); // pop the backStack on a button reset
if (G.backStack.length==0) G.backButtonDiv.style.display='none';
}
} else {
this.initCover_();
if ( G.options.backButtonEnabled ) this.saveBackContext_(G.options.backButtonHTML,false); // save the map context for back button
}
};
/**
* Back Button functionality:
* Function called when the back button's click event is captured.
* calls the function to set the map context back to where it was before the zoom.
*/
DragZoomControl.prototype.backbuttonclick_ = function(){
var G = this.globals;
if (G.options.backButtonEnabled && G.backStack.length > 0) {
this.restoreBackContext_();
// invoke the callback if provided
if (G.callbacks['backbuttonclick'] != null) {
G.callbacks.backbuttonclick(G.methodCall);
}
}
};
/**
* Back Button functionality:
* Saves the map context and pushes it on the backStack for later use by the back button
*/
DragZoomControl.prototype.saveBackContext_ = function(text,methodCall) {
var G = this.globals;
var backFrame = {};
backFrame["center"] = G.map.getCenter();
backFrame["zoom"] = G.map.getZoom();
backFrame["maptype"] = G.map.getCurrentMapType();
backFrame["text"] = G.backButtonDiv.innerHTML; // this saves the previous button text
backFrame["methodCall"] = methodCall; //This determines if it was an internal or method call
G.backStack.push(backFrame);
G.backButtonDiv.innerHTML = text;
// Back Button is turned on in resetDragZoom_()
};
/**
* Back Button functionality:
* Pops the previous map context off of the backStack and restores the map to that context
*/
DragZoomControl.prototype.restoreBackContext_ = function() {
var G = this.globals;
var backFrame = G.backStack.pop();
G.map.setCenter(backFrame["center"],backFrame["zoom"],backFrame["maptype"]);
G.backButtonDiv.innerHTML = backFrame["text"];
G.methodCall = backFrame["methodCall"];
if (G.backStack.length==0) G.backButtonDiv.style.display = 'none'; // if we're at the top of the stack, hide the back botton
};
/**
* Shows the cover over the map
*/
DragZoomControl.prototype.initCover_ = function(){
var G = this.globals;
G.mapPosition = DragZoomUtil.getElementPosition(G.map.getContainer());
this.setDimensions_();
this.setButtonMode_('zooming');
DragZoomUtil.style([G.mapCover], {display: 'block', background: G.style.fillColor});
DragZoomUtil.style([G.outlineDiv], {width: '0px', height: '0px'});
//invoke callback if provided
if(G.callbacks['buttonclick'] != null){
G.callbacks.buttonclick();
}
};
/**
* Gets position of the mouse relative to the map
* @param {Object} e
*/
DragZoomControl.prototype.getRelPos_ = function(e) {
var pos = DragZoomUtil.getMousePosition(e);
var G = this.globals;
return {top: (pos.top - G.mapPosition.top),
left: (pos.left - G.mapPosition.left)};
};
/**
* Figures out the rectangle the user's trying to draw
* @param {Number} startX
* @param {Number} startY
* @param {Object} pos
* @param {Number} ratio
* @return {Object} Describes the rectangle
*/
DragZoomControl.prototype.getRectangle_ = function(startX, startY, pos, ratio){
var left = false;
var top = false;
var dX = pos.left - startX;
var dY = pos.top - startY;
if (dX < 0) {
dX = dX * -1;
left = true;
}
if (dY < 0) {
dY = dY * -1;
top = true;
}
delta = dX > dY ? dX : dY;
return {
startX: startX,
startY: startY,
/*
endX: startX + delta,
endY: startY + parseInt(delta * ratio),
width: delta,
height: parseInt(delta * ratio),*/
//以改后框能自定义的大小 2010-3-12 15:00:35 程剑虎
endX: startX + dX,
endY: startY + parseInt(dY),
width: dX,
height: parseInt(dY),
left:left,
top:top
}
};
/**
* Resets CSS and button display when drag zoom done
*/
DragZoomControl.prototype.resetDragZoom_ = function() {
var G = this.globals;
DragZoomUtil.style([G.mapCover, G.cornerTopDiv, G.cornerRightDiv, G.cornerBottomDiv, G.cornerLeftDiv],
{display: 'none', opacity: G.style.opacity, filter: G.style.alphaIE});
G.outlineDiv.style.display = 'none';
this.setButtonMode_('normal');
if (G.options.backButtonEnabled && (G.backStack.length > 0)) G.backButtonDiv.style.display = 'block'; // show the back button
};
/* utility functions in DragZoomUtil.namespace */
var DragZoomUtil={};
/**
* Alias function for getting element by id
* @param {String} sId
* @return {Object} DOM object with sId id
*/
DragZoomUtil.gE = function(sId) {
return document.getElementById(sId);
};
/**
* A general-purpose function to get the absolute position
* of the mouse.
* @param {Object} e Mouse event
* @return {Object} Describes position
*/
DragZoomUtil.getMousePosition = function(e) {
var posX = 0;
var posY = 0;
if (!e) var e = window.event;
if (e.pageX || e.pageY) {
posX = e.pageX;
posY = e.pageY;
} else if (e.clientX || e.clientY){
posX = e.clientX +
(document.documentElement.scrollLeft ? document.documentElement.scrollLeft : document.body.scrollLeft);
posY = e.clientY +
(document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop);
}
return {left: posX, top: posY};
};
/**
* Gets position of element
* @param {Object} element
* @return {Object} Describes position
*/
DragZoomUtil.getElementPosition = function(element) {
var leftPos = element.offsetLeft; // initialize var to store calculations
var topPos = element.offsetTop; // initialize var to store calculations
var parElement = element.offsetParent; // identify first offset parent element
while (parElement != null ) { // move up through element hierarchy
leftPos += parElement.offsetLeft; // appending left offset of each parent
topPos += parElement.offsetTop;
parElement = parElement.offsetParent; // until no more offset parents exist
}
return {left: leftPos, top: topPos};
};
/**
* Applies styles to DOM objects
* @param {String/Object} elements Either comma-delimited list of ids
* or an array of DOM objects
* @param {Object} styles Hash of styles to be applied
*/
DragZoomUtil.style = function(elements, styles){
if (typeof(elements) == 'string') {
elements = DragZoomUtil.getManyElements(elements);
}
for (var i = 0; i < elements.length; i++){
for (var s in styles) {
elements[i].style[s] = styles[s];
}
}
};
/**
* Gets DOM elements array according to list of IDs
* @param {String} elementsString Comma-delimited list of IDs
* @return {Array} Array of DOM elements corresponding to s
*/
DragZoomUtil.getManyElements = function(idsString){
var idsArray = idsString.split(',');
var elements = [];
for (var i = 0; i < idsArray.length; i++){
elements[elements.length] = DragZoomUtil.gE(idsArray[i])
};
return elements;
};