1 /***************************************************************************
2 * Copyright (C) 2011 by Lukáš Karas <lukas.karas@centrum.cz> *
4 * This program is free software; you can redistribute it and/or modify *
5 * it under the terms of the GNU General Public License as published by *
6 * the Free Software Foundation; either version 2 of the License, or *
7 * (at your option) any later version. *
9 * This program is distributed in the hope that it will be useful, *
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
12 * GNU General Public License for more details. *
14 * You should have received a copy of the GNU General Public License *
15 * along with this program; if not, write to the *
16 * Free Software Foundation, Inc., *
17 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
18 ***************************************************************************/
20 function MaknetoWhiteboard(workArea
){
21 this.workArea
= workArea
;
22 this.mouseIsDown
= false;
23 this.connector
= connector
;
26 MaknetoWhiteboard
.prototype.init = function(weigth
, height
){
30 this.minZoom
= 0.5; // FIXME: compute this
35 this.whiteboard
.destroy();
38 //this.thumb = new Thumbnail( $(this.thumbArea), weigth, height, false, this);
40 this.whiteboard
= new Whiteboard($(this.workArea
) , weigth
, height
, true, this, this.connector
.getUID());
42 console
.error("error while initializing svg whiteboard: "+JSON
.stringify(e
));
46 this.thumb.registerViewportCallback(this);
49 this.workArea.scroll(function(){
50 inst.viewportChanged();
53 // initial update of thumbnail viewport
54 this.viewportChanged();
58 MaknetoWhiteboard
.prototype.zoomIn = function(){
59 var z
= this.zoom
+ 0.1;
65 MaknetoWhiteboard
.prototype.zoomOut = function(){
66 var z
= this.zoom
- 0.1;
72 MaknetoWhiteboard
.prototype.setZoom = function(z
){
73 //$('#zoom').html( Math.round(z*10) / 10);
76 var x = (this.workArea.scrollLeft() + ((this.workArea.width() ) / 2) )/ this.zoom;
77 var y = (this.workArea.scrollTop() + ((this.workArea.height() ) / 2) )/ this.zoom;
78 console.log("zoom center "+x+"x"+y+" ("+this.zoom+")");
82 this.whiteboard
.setZoom(this.zoom
);
83 //this.whiteboard.showInCenter(x*this.zoom, y*this.zoom);
87 * calback function from thumbnail... mouse action on thumnail can change viewport
89 MaknetoWhiteboard
.prototype.showInCenter = function(x
,y
){
90 this.whiteboard
.showInCenter(x
, y
);
94 * function registered on canvas scrool events. it inform thumnail that viewport was changed
96 MaknetoWhiteboard
.prototype.viewportChanged = function(){
98 var x = (this.workArea.scrollLeft() / this.zoom) - this.whiteboard.canvasWidth;
99 var y = (this.workArea.scrollTop() / this.zoom) - this.whiteboard.canvasHeight;
100 var w = this.workArea.width() / this.zoom;
101 var h = this.workArea.height() / this.zoom;
103 this.thumb.viewportChanged(x,y,w,h);
108 MaknetoWhiteboard
.prototype.buildNewXmlTag = function(wb
){
109 var str
= "<"+wb
.element
+" ";
110 for (var attr
in wb
.attr
){
111 if (typeof wb
.attr
[attr
] == 'function')
112 continue; // workaround for Object.clone method
114 str
+= attr
+"='"+wb
.attr
[attr
]+"' ";
119 MaknetoWhiteboard
.prototype.buildConfigureXmlTag = function(wb
){
121 for (var i
in wb
.attr
){
122 var attr
= wb
.attr
[i
];
123 if (attr
== undefined || attr
.value
=== undefined)
125 str
+= "<attribute name='"+attr
.name
+"'>"+attr
.value
+"</attribute>\n";
130 * convert json data from own html whiteboard to SVG Whiteboardind XMPP extension XML
132 MaknetoWhiteboard
.prototype.buildXmlMessage = function(msg
){
133 var xmlMessage
= "<wb xmlns='http://jabber.org/protocol/svgwb'>\n";
134 if (msg
.wb
.type
== "new"){
135 xmlMessage
+= "<new id='"+msg
.wb
.id
+"' version='"+msg
.wb
.version
+"' index='"+msg
.wb
.index
+"'>\n";
136 xmlMessage
+= this.buildNewXmlTag(msg
.wb
);
137 xmlMessage
+= "\n</new>";
139 if (msg
.wb
.type
== "configure"){
140 xmlMessage
+= "<configure target='"+msg
.wb
.target
+"' version='"+msg
.wb
.version
+"'>\n";
141 xmlMessage
+= this.buildConfigureXmlTag(msg
.wb
);
142 xmlMessage
+= "\n</configure>";
144 xmlMessage
+= "\n</wb>";
148 MaknetoWhiteboard
.prototype.processLocalCommand = function(drawCommand
){
151 from : this.connector
.getUID(),
156 // TODO: colect more events to one big message
157 if (this.connector
.sendMessage( this.buildXmlMessage(msg
))){
158 this.processCommand(drawCommand
);
161 console
.error("error while building, sending or processing command "+JSON
.stringify(e
));
165 MaknetoWhiteboard
.prototype.processMessage = function(message
){
166 if (message
.type
!= "groupchat" || message
.to
!= this.room
|| (!message
.wb
))
168 this.processCommand(message
.wb
);
171 MaknetoWhiteboard
.prototype.processCommand = function(drawCommand
){
172 //console.log(" sending command..."+JSON.stringify(drawCommand));
174 this.whiteboard
.processCommand(drawCommand
);
176 console
.error("error processing command "+JSON
.stringify(e
));
178 //console.log("command: "+JSON.stringify(drawCommand));
179 //this.thumb.processCommand(drawCommand);
182 MaknetoWhiteboard
.prototype.handleNewCommands = function(newElems
){
184 for (var i
=0; i
< newElems
.length
; i
++){
186 var elem
= newElems
[i
];
188 if (!elem
.hasChildNodes())
190 var child
= elem
.childNodes
[0];
191 for (var chi
= 0; chi
< elem
.childNodes
.length
; chi
++){
192 if (child
.nodeName
!== "#text")
194 child
= elem
.childNodes
[chi
];
196 if (child
.nodeName
=== "#text")
198 //console.log("child name "+child.nodeName+" ");
202 for (var ati
= 0; ati
< child
.attributes
.length
; ati
++){
203 if (child
.attributes
[ati
].nodeName
!== undefined){
204 //console.log(" "+child.nodeName+" "+ child.attributes[ati].nodeName+" = "+child.attributes[ati].nodeValue);
205 attributes
[ child
.attributes
[ati
].nodeName
] = child
.attributes
[ati
].nodeValue
;
209 this.processCommand({
211 id
: elem
.getAttribute ("id"),
212 index
: elem
.getAttribute ("index"),
213 element
: child
.nodeName
, // FIXME ?
214 version
: elem
.getAttribute ("version"),
221 MaknetoWhiteboard
.prototype.handleConfigureCommands = function(configureElems
){
222 for (var i
=0; i
< configureElems
.length
; i
++){
223 //console.log("iterate elems...");
224 var elem
= configureElems
[i
];
225 var target
= elem
.getAttribute("target");
226 var version
= elem
.getAttribute("version");
227 //console.log(" configure "+target);
229 var attributes
= elem
.getElementsByTagName ("attribute");
231 for (var ai
= 0; ai
< attributes
.length
; ai
++){
232 var attribute
= attributes
[ai
];
233 var name
= attribute
.getAttribute("name");
234 var value
= attribute
.hasChildNodes()? attribute
.childNodes
[0].nodeValue
:"";
235 //console.log(" configure "+target+" "+name+"="+value);
247 this.processCommand(command
);
251 MaknetoWhiteboard
.prototype.processXmlCommand = function(message
){
252 //console.log("xml message ");
255 var doc
= Utils
.text2xml(message
);
257 var newElems
= doc
.getElementsByTagName ("new");
258 this.handleNewCommands(newElems
);
260 var configureElems
= doc
.getElementsByTagName("configure");
261 this.handleConfigureCommands(configureElems
);
262 //console.log("...done");
265 console
.error("PARSE ERROR");
270 * callback method. notify that outgoing message was successfuly sent
272 MaknetoWhiteboard
.prototype.messageSent= function(message
){
273 //console.log("sent");
275 MaknetoWhiteboard
.prototype.setForegroundColor = function(color
){
276 this.whiteboard
.foregroundColor
= color
;