wisemapping-open-source/mindplot/src/main/javascript/Workspace.js

228 lines
8.0 KiB
JavaScript
Raw Normal View History

2010-11-20 23:43:54 +01:00
/*
2011-07-26 20:07:53 +02:00
* Copyright [2011] [wisemapping]
*
* Licensed under WiseMapping Public License, Version 1.0 (the "License").
* It is basically the Apache License, Version 2.0 (the "License") plus the
* "powered by wisemapping" text requirement on every single page;
* you may not use this file except in compliance with the License.
* You may obtain a copy of the license at
*
* http://www.wisemapping.org/license
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
mindplot.Workspace = new Class({
initialize: function(screenManager, zoom) {
2011-07-26 20:07:53 +02:00
// Create a suitable container ...
$assert(screenManager, 'Div container can not be null');
$assert(zoom, 'zoom container can not be null');
2011-07-26 20:07:53 +02:00
this._zoom = zoom;
this._screenManager = screenManager;
var divContainer = screenManager.getContainer();
this._screenWidth = parseInt(divContainer.getStyle('width'));
this._screenHeight = parseInt(divContainer.getStyle('height'));
2011-07-26 20:07:53 +02:00
// Initialize web2d workspace.
var workspace = this._createWorkspace();
this._workspace = workspace;
2011-07-26 20:07:53 +02:00
// Append to the workspace...
workspace.addItAsChildTo(divContainer);
2011-07-26 20:07:53 +02:00
this.setZoom(zoom, true);
// Register drag events ...
this._registerDragEvents();
this._eventsEnabled = true;
2011-07-26 20:07:53 +02:00
},
_createWorkspace: function() {
2011-07-26 20:07:53 +02:00
// Initialize workspace ...
var coordOriginX = -(this._screenWidth / 2);
var coordOriginY = -(this._screenHeight / 2);
var workspaceProfile = {
width: this._screenWidth + "px",
height: this._screenHeight + "px",
coordSizeWidth:this._screenWidth,
coordSizeHeight:this._screenHeight,
coordOriginX:coordOriginX,
coordOriginY:coordOriginY,
fillColor:'transparent',
strokeWidth:0
};
web2d.peer.Toolkit.init();
return new web2d.Workspace(workspaceProfile);
},
appendChild: function(shape) {
if ($defined(shape.addToWorkspace)) {
2011-07-26 20:07:53 +02:00
shape.addToWorkspace(this);
} else {
this._workspace.appendChild(shape);
}
},
removeChild: function(shape) {
// Element is a node, not a web2d element?
if ($defined(shape.removeFromWorkspace)) {
2011-07-26 20:07:53 +02:00
shape.removeFromWorkspace(this);
} else {
this._workspace.removeChild(shape);
}
},
2011-08-21 17:42:00 +02:00
addEvent: function(type, listener) {
this._workspace.addEvent(type, listener);
2011-07-26 20:07:53 +02:00
},
2011-08-21 17:42:00 +02:00
removeEvent: function(type, listener) {
2011-10-09 22:59:16 +02:00
$assert(type, 'type can not be null');
$assert(listener, 'listener can not be null');
2011-08-21 17:42:00 +02:00
this._workspace.removeEvent(type, listener);
2011-07-26 20:07:53 +02:00
},
getSize: function() {
return this._workspace.getCoordSize();
},
setZoom: function(zoom, center) {
this._zoom = zoom;
var workspace = this._workspace;
// Update coord scale...
var coordWidth = zoom * this._screenWidth;
var coordHeight = zoom * this._screenHeight;
workspace.setCoordSize(coordWidth, coordHeight);
2011-08-28 15:58:41 +02:00
// View port coords ...
if (this._viewPort) {
this._viewPort.width = this._viewPort.width * zoom;
this._viewPort.height = this._viewPort.height * zoom;
}
2011-07-26 20:07:53 +02:00
// Center topic....
var coordOriginX;
var coordOriginY;
2011-08-28 15:58:41 +02:00
if (center) {
if (this._viewPort) {
coordOriginX = -(this._viewPort.width / 2);
coordOriginY = -(this._viewPort.height / 2);
} else {
coordOriginX = -(coordWidth / 2);
coordOriginY = -(coordHeight / 2);
}
2011-07-26 20:07:53 +02:00
} else {
var coordOrigin = workspace.getCoordOrigin();
coordOriginX = coordOrigin.x;
coordOriginY = coordOrigin.y;
}
2009-06-07 20:59:43 +02:00
2011-07-26 20:07:53 +02:00
workspace.setCoordOrigin(coordOriginX, coordOriginY);
// Update screen.
this._screenManager.setOffset(coordOriginX, coordOriginY);
this._screenManager.setScale(zoom);
2011-08-20 15:50:48 +02:00
// Some changes in the screen. Let's fire an update event...
2011-08-22 03:53:37 +02:00
this._screenManager.fireEvent('update');
2011-07-26 20:07:53 +02:00
},
getScreenManager: function() {
return this._screenManager;
},
enableWorkspaceEvents: function(value) {
this._eventsEnabled = value;
},
isWorkspaceEventsEnabled: function() {
return this._eventsEnabled;
},
dumpNativeChart: function() {
return this._workspace.dumpNativeChart();
2011-07-26 20:07:53 +02:00
},
2011-07-26 20:07:53 +02:00
_registerDragEvents: function() {
var workspace = this._workspace;
var screenManager = this._screenManager;
var mWorkspace = this;
2011-07-27 19:53:32 +02:00
var mouseDownListener = function(event) {
2011-08-20 15:50:48 +02:00
if (!$defined(workspace._mouseMoveListener)) {
if (mWorkspace.isWorkspaceEventsEnabled()) {
2011-07-26 20:07:53 +02:00
mWorkspace.enableWorkspaceEvents(false);
2009-06-07 20:59:43 +02:00
2011-07-26 20:07:53 +02:00
var mouseDownPosition = screenManager.getWorkspaceMousePosition(event);
var originalCoordOrigin = workspace.getCoordOrigin();
2009-06-07 20:59:43 +02:00
var wasDragged = false;
workspace._mouseMoveListener = function(event) {
2009-06-07 20:59:43 +02:00
var currentMousePosition = screenManager.getWorkspaceMousePosition(event);
var offsetX = currentMousePosition.x - mouseDownPosition.x;
var coordOriginX = -offsetX + originalCoordOrigin.x;
var offsetY = currentMousePosition.y - mouseDownPosition.y;
var coordOriginY = -offsetY + originalCoordOrigin.y;
workspace.setCoordOrigin(coordOriginX, coordOriginY);
// Change cursor.
2011-08-09 07:27:59 +02:00
if (Browser.firefox) {
2009-06-07 20:59:43 +02:00
window.document.body.style.cursor = "-moz-grabbing";
2011-07-26 20:07:53 +02:00
} else {
2009-06-07 20:59:43 +02:00
window.document.body.style.cursor = "move";
}
2011-07-26 20:07:53 +02:00
event.preventDefault();
// Fire drag event ...
2011-08-22 03:53:37 +02:00
screenManager.fireEvent('update');
wasDragged = true;
2011-08-20 15:50:48 +02:00
}.bind(this);
screenManager.addEvent('mousemove', workspace._mouseMoveListener);
2011-07-26 20:07:53 +02:00
// Register mouse up listeners ...
workspace._mouseUpListener = function(event) {
2011-07-26 20:07:53 +02:00
screenManager.removeEvent('mousemove', workspace._mouseMoveListener);
screenManager.removeEvent('mouseup', workspace._mouseUpListener);
workspace._mouseUpListener = null;
workspace._mouseMoveListener = null;
2011-07-26 20:07:53 +02:00
window.document.body.style.cursor = 'default';
// Update screen manager offset.
var coordOrigin = workspace.getCoordOrigin();
screenManager.setOffset(coordOrigin.x, coordOrigin.y);
mWorkspace.enableWorkspaceEvents(true);
2011-08-20 15:50:48 +02:00
if (!wasDragged) {
2011-08-22 03:53:37 +02:00
screenManager.fireEvent('click');
}
};
screenManager.addEvent('mouseup', workspace._mouseUpListener);
2011-07-26 20:07:53 +02:00
}
} else {
workspace._mouseUpListener();
2009-06-07 20:59:43 +02:00
}
2011-07-26 20:07:53 +02:00
};
screenManager.addEvent('mousedown', mouseDownListener);
},
setViewPort : function(size) {
this._viewPort = size;
2011-07-26 20:07:53 +02:00
}
});
2009-06-07 20:59:43 +02:00