| 1 | package GFweb; | ||
| 2 | use strict; | ||
| 3 | use warnings; | ||
| 4 | |||
| 5 | our $VERSION = '0.10'; | ||
| 6 | |||
| 7 | use Exporter 'import'; | ||
| 8 | our @EXPORT = qw<addWidget addVar setScale MainLoop $frame $winScale $winWidth $winHeight $winTitle>; | ||
| 9 | our $frame; | ||
| 10 | |||
| 11 | use Net::WebSocket::Server; | ||
| 12 | use IO::Socket::PortState qw(check_ports); # requiredso that multiple listeners are not attempted | ||
| 13 | our $connection; | ||
| 14 | our $clientFileName; | ||
| 15 | our $webApp; | ||
| 16 | our $host; | ||
| 17 | our $port; | ||
| 18 | our $serverRunning=0; | ||
| 19 | |||
| 20 | our $winX=30; | ||
| 21 | our $winY=30; | ||
| 22 | our $winWidth; | ||
| 23 | our $winHeight; | ||
| 24 | our $winTitle="title"; | ||
| 25 | our $winScale=6.5; | ||
| 26 | |||
| 27 | # these arrays will contain the widgets each as an arrayref of the parameters | ||
| 28 | my @widgets=(); | ||
| 29 | my %iVars=(); #vars for interface operation (e.g. state of interface) | ||
| 30 | my %oVars=(); #vars for interface creation (e.g. list of options) | ||
| 31 | my %styles; | ||
| 32 | |||
| 33 | our $debug; | ||
| 34 | my %msgFlags; | ||
| 35 | my %dialogDispatch; # dispatch table for dialog actions | ||
| 36 | my $uploadFileName; | ||
| 37 | |||
| 38 | |||
| 39 | my $lastMenuLabel; #bug workaround in menu generator may be needed for submenus | ||
| 40 | |||
| 41 | sub new | ||
| 42 | { | ||
| 43 | my $class = shift; | ||
| 44 | |||
| 45 | # Allow the host and the port for the server to be defined by main:: | ||
| 46 | # for web app creation this is an extra parameter added in the GUIDeFATE::new | ||
| 47 | # Allows | ||
| 48 | # my $gui=GUIDeFATE->new($window,"web","q", 8085); | ||
| 49 | # my $gui=GUIDeFATE->new($window,"web","q", "localhost:8085"); | ||
| 50 | |||
| 51 | $port=shift || 8085; | ||
| 52 | $debug=shift; | ||
| 53 | $debug=$debug ? 1:0; | ||
| 54 | if ($port =~/:/) { | ||
| 55 | ($host,$port)=split(":",$port) | ||
| 56 | } | ||
| 57 | else {$host="localhost"}; | ||
| 58 | |||
| 59 | |||
| 60 | # applications attemting to run with a used port will cause a | ||
| 61 | # "Failed to listen error.This just finds the next available port. | ||
| 62 | my $portAvailable = 0; | ||
| 63 | while(!$portAvailable){ | ||
| 64 | my %ports = ( # required for check_hosts from IO::Socket::PortState | ||
| 65 | tcp => { | ||
| 66 | $port => {}, | ||
| 67 | } | ||
| 68 | ); | ||
| 69 | my $timeout=10; | ||
| 70 | my $host_hr = check_ports($host, $timeout, \%ports); # check the ports | ||
| 71 | $portAvailable = $host_hr->{tcp}{$port}{open} ? 0:1; # if already open | ||
| 72 | $port++; | ||
| 73 | } | ||
| 74 | $port--; | ||
| 75 | |||
| 76 | my $self={}; | ||
| 77 | bless( $self, $class ); | ||
| 78 | $self->{html}=$self->header(); | ||
| 79 | |||
| 80 | $self->{html}.="<title>$winTitle $0 </title>\n"; | ||
| 81 | $self->{html}.= " <head>\n<style>\n".css()."</style>\n"; | ||
| 82 | $self->{html}.= " <script>\n".js()."</script>\n"; | ||
| 83 | if (-e "$0.js"){ $self->{html}.= " <script type=\"text/javascript\" src=\"$0.js\"></script>\n";} | ||
| 84 | $self->{html}.= " </head>\n<body>\n<div style=\"width:$winWidth"."px;height:$winHeight"."px\">\n"; | ||
| 85 | $self->{content}="<div id=window style=\"position:relative;width:$winWidth"."px;height:$winHeight"."px\">\n"; | ||
| 86 | setupContent($self,$self->{content}); | ||
| 87 | $self->{html}.=$self->{content}; | ||
| 88 | $self->{html}.=dialogBoxDiv(); | ||
| 89 | |||
| 90 | $self->{html}.= "\n</div>\n</body>\n</html>"; | ||
| 91 | |||
| 92 | $clientFileName = "$0$port.html"; | ||
| 93 | open(my $fh, '>', $clientFileName) or die "Could not open file '$clientFileName' $!"; | ||
| 94 | print $fh $self->{html}; | ||
| 95 | close $fh; | ||
| 96 | |||
| 97 | return $self; | ||
| 98 | }; | ||
| 99 | |||
| 100 | sub MainLoop{ #activate UI | ||
| 101 | my $self=shift; | ||
| 102 | |||
| 103 | # create new server | ||
| 104 | $webApp= Net::WebSocket::Server->new( | ||
| 105 | listen => $port, | ||
| 106 | on_connect => sub { | ||
| 107 | (my $serv, $connection) = @_; | ||
| 108 | $connection->on( | ||
| 109 | utf8 => sub { | ||
| 110 | parseMessage(@_); | ||
| 111 | }, | ||
| 112 | binary => sub { | ||
| 113 | parseBinary(@_); | ||
| 114 | }, | ||
| 115 | ); | ||
| 116 | }, | ||
| 117 | ); | ||
| 118 | |||
| 119 | |||
| 120 | if ($^O =~/linux/){ system("xdg-open ./".$clientFileName." 2> /dev/null &\n"); } # for Linux | ||
| 121 | elsif ($^O =~/Win/){ system("start .\\".$clientFileName."\n"); } # for windows? | ||
| 122 | else{ system("open ./".$clientFileName." &\n"); } # for Macs? | ||
| 123 | |||
| 124 | $webApp->start(); | ||
| 125 | } | ||
| 126 | |||
| 127 | sub parseMessage{ | ||
| 128 | my($conn, $message)=@_; | ||
| 129 | $conn->send_utf8( "Message received by server: - $message\n"); | ||
| 130 | if ($message=~/^[A-z]*=/){ | ||
| 131 | my %hash = map{split /\=/, $_,2}(split /\&/, $message,3); | ||
| 132 | if($hash{Function}) { | ||
| 133 | if ($hash{Function} eq "Dialog"){ # function from Dialog buttons | ||
| 134 | &{$dialogDispatch{$hash{Label}}}; | ||
| 135 | } | ||
| 136 | elsif ($hash{Function} eq "FileLoaded"){ # function from file selector | ||
| 137 | |||
| 138 | } | ||
| 139 | else { # function triggered by widgets | ||
| 140 | mapAction($hash{Function}); | ||
| 141 | } | ||
| 142 | } | ||
| 143 | elsif ($hash{ID}) { | ||
| 144 | $msgFlags{$hash{ID}}=1; | ||
| 145 | my $tmp=$hash{Value}; | ||
| 146 | $tmp=~s/^'|'$//; | ||
| 147 | $iVars{$hash{ID}}=$tmp; | ||
| 148 | #$conn->send_utf8( "Server stores ".$hash{ID}. " the value ".$hash{Value}); | ||
| 149 | } | ||
| 150 | } | ||
| 151 | elsif ($message=~/^File follows:(.*)\n/){ # Get ready to receive file | ||
| 152 | $uploadFileName=$1; | ||
| 153 | if (! -d "dataFiles") {mkdir "dataFiles";} # ensure upload directory exists | ||
| 154 | unlink "dataFiles/$uploadFileName"; # delete file if already exists | ||
| 155 | $conn->send_utf8( "Binary data expected: Ready to receive $uploadFileName \n"); # Announce ready to receive binary Data | ||
| 156 | } | ||
| 157 | elsif ($message=~/^File ends:(.*)\n/){ | ||
| 158 | $conn->send_utf8( "Message Binary data upload complete:\n"); | ||
| 159 | if (defined $dialogDispatch{File}){ #Funtion to run after file has been downloaded | ||
| 160 | &{$dialogDispatch{File}}($1); | ||
| 161 | } | ||
| 162 | $uploadFileName=undef; | ||
| 163 | } | ||
| 164 | } | ||
| 165 | |||
| 166 | sub parseBinary{ | ||
| 167 | my($conn, $binary)=@_; | ||
| 168 | open(my $fh, '>>', "dataFiles/$uploadFileName") or die "Could not open file 'dataFiles/$uploadFileName' $!"; | ||
| 169 | print $fh $binary; | ||
| 170 | close $fh; | ||
| 171 | $conn->send_utf8( "Binary data received: -(". (length $binary). " bytes inserted into $uploadFileName)\n"); | ||
| 172 | } | ||
| 173 | |||
| 174 | sub mapAction{ # maps id of widget with action on change | ||
| 175 | my $item=shift; | ||
| 176 | my $widgetIndex=getItem(undef,$item); | ||
| 177 | my @widget=@{$widgets[$widgetIndex]}; | ||
| 178 | my $wType=shift @widget; | ||
| 179 | if ($widgetIndex !=-1){ | ||
| 180 | if ($wType eq "mb") { &{$widget[3]};} | ||
| 181 | elsif ($wType eq "btn") { &{$widget[4]};} | ||
| 182 | elsif ($wType eq "combo") { &{$widget[4]};} | ||
| 183 | } | ||
| 184 | } | ||
| 185 | |||
| 186 | # setupContent sets up the initial content before Mainloop can be run. | ||
| 187 | sub setupContent{ | ||
| 188 | my ($self, $canvas)=@_; # pass both object as well as the frame element | ||
| 189 | $self ->{"menubar"}=undef; # menu not yet defined | ||
| 190 | my $currentMenu; # undefined menu | ||
| 191 | |||
| 192 | foreach my $widget (@widgets){ # read each widget data and call gnerator | ||
| 193 | my @params=@$widget; | ||
| 194 | my $wtype=shift @params; | ||
| 195 | if ($wtype eq "btn") {aBt($self, $canvas, @params);} | ||
| 196 | elsif ($wtype eq "textctrl") {aTC($self, $canvas, @params);} | ||
| 197 | elsif ($wtype eq "stattext") {aST($self, $canvas, @params);} | ||
| 198 | elsif ($wtype eq "sp") {aSP($self, $canvas, @params);} | ||
| 199 | elsif ($wtype eq "combo") {aCB($self, $canvas, @params);} | ||
| 200 | elsif ($wtype eq "sp") {aSP($self, $canvas, @params);} | ||
| 201 | elsif ($wtype eq "mb") | ||
| 202 | { | ||
| 203 | if (! $self->{"menubar"}){ | ||
| 204 | $self ->{"menubar"} = " <div style=\"width:$winWidth"."px;height:16px;\">\n <ul class=menubar>\n"; | ||
| 205 | } | ||
| 206 | $currentMenu=aMB($self,$canvas,$currentMenu,@params) | ||
| 207 | } | ||
| 208 | } | ||
| 209 | if ($self->{"menubar"}) { $self->{"menubar"}.=" </div>\n </li>\n </ul>\n </div>"; } | ||
| 210 | if ($self->{menubar}){ $self->{content}.= $self->{menubar} . "\n<br>\n";} | ||
| 211 | $self->{content}.="\n</div>\n"; | ||
| 212 | |||
| 213 | sub aBt{ | ||
| 214 | my ($self,$canvas, $id, $label, $location, $size, $action)=@_;# Button generator | ||
| 215 | $self->{content}.="<input id=btn$id type=button style=\"position:absolute;left:".${$location}[0]."px;top:".${$location}[1]."px;". | ||
| 216 | "width:".${$size}[0]."px;height:".${$size}[1]."px;\" value=\"$label\" onclick=\'act(\"btn$id\",\"$label\")\'>\n"; | ||
| 217 | } | ||
| 218 | sub aTC{ | ||
| 219 | my ($self,$canvas, $id, $text, $location, $size, $action)=@_;# Single line input generator | ||
| 220 | $self->{content}.="<input id=textctrl$id style=\"position:absolute;left:".${$location}[0]."px;top:".${$location}[1]."px;". | ||
| 221 | "width:".${$size}[0]."px;height:".${$size}[1]."px;\" value=\"$text\" onchange=\'act(\"textctrl$id\",this.value)\'>\n"; | ||
| 222 | } | ||
| 223 | sub aST{ | ||
| 224 | my ($self,$canvas, $id, $text, $location)=@_; #Static text element generator | ||
| 225 | $self->{content}.="<div id=stattext$id style=\"position:absolute;left:".${$location}[0]."px;top:".${$location}[1]."px\">".$text."</div>\n"; | ||
| 226 | } | ||
| 227 | sub aCB{ | ||
| 228 | my ($self,$canvas, $id, $label, $location, $size, $action)=@_; #gnerator for comoboxes | ||
| 229 | if (defined $oVars{$label}){ | ||
| 230 | my @strings2 = split(",",$oVars{$label}); # extract the defined options | ||
| 231 | $self->{content}.="<select id=combo$id style=\"position:absolute;left:".${$location}[0]."px;top:".${$location}[1]."px;". | ||
| 232 | "width:".${$size}[0]."px;height:".${$size}[1]."px;\" value=\"".$strings2[0]."\" onchange=\'act(\"combo$id\",this.value)\'>\n"; | ||
| 233 | foreach (@strings2){ | ||
| 234 | $self->{content}.=" <option>$_</option>\n"; | ||
| 235 | } | ||
| 236 | $self->{content}.="</select>\n"; | ||
| 237 | } | ||
| 238 | else {print "Combo options not defined for 'combo$id' with label $label\n"}; | ||
| 239 | } | ||
| 240 | |||
| 241 | sub aMB{ | ||
| 242 | my ($self,$canvas,$currentMenu, $id, $label, $type, $action)=@_; | ||
| 243 | if (($lastMenuLabel) &&($label eq $lastMenuLabel)){return $currentMenu} # bug workaround | ||
| 244 | else {$lastMenuLabel=$label}; # in menu generator | ||
| 245 | if ($type eq "menuhead"){ | ||
| 246 | if (defined $currentMenu){$canvas.=" </div>\n </li>";} | ||
| 247 | $currentMenu="menu".$id; | ||
| 248 | $self ->{"menubar"}.=" <li class=\"menuhead dropdown\">\n <a id=menu$id class=\"dropbtn\">$label</a>\n <div class=\"dropdown-content\">\n"; | ||
| 249 | } | ||
| 250 | elsif ($type eq "radio"){ | ||
| 251 | |||
| 252 | } | ||
| 253 | elsif ($type eq "check"){ | ||
| 254 | |||
| 255 | } | ||
| 256 | elsif ($type eq "separator"){ | ||
| 257 | |||
| 258 | } | ||
| 259 | else{ | ||
| 260 | if($currentMenu!~m/$label/){ | ||
| 261 | $self ->{"menubar"}.=" <a href=# id=menu$id class=menuitem onclick=\'act(\"menu$id\",\"$label\")\'>$label</a>\n" | ||
| 262 | } | ||
| 263 | } | ||
| 264 | return $currentMenu; | ||
| 265 | } | ||
| 266 | sub aSP{ | ||
| 267 | my ($self,$canvas, $id, $panelType, $content, $location, $size)=@_; | ||
| 268 | |||
| 269 | if ($panelType eq "I"){ # Image panels start with I | ||
| 270 | $self->{content}.="<img id=Image$id src=\"$content\" style=\"position:absolute;left:".${$location}[0]."px;top:".${$location}[1]."px;". | ||
| 271 | "width:".${$size}[0]."px;height:".${$size}[1]."px;\" onclick=\"this.src=(this.src+\'?\'+Math.random)\">\n"; | ||
| 272 | } | ||
| 273 | if ($panelType eq "T"){ | ||
| 274 | $id++; | ||
| 275 | $self->{content}.="<textarea id=TextCtrl$id style=\"position:absolute;left:".${$location}[0]."px;top:".${$location}[1]."px;". | ||
| 276 | "width:".${$size}[0]."px;height:".${$size}[1]."px;\">$content</textArea>\n"; | ||
| 277 | } | ||
| 278 | } | ||
| 279 | } | ||
| 280 | |||
| 281 | #functions for GUIDeFATE to load the widgets into the backend | ||
| 282 | sub addWidget{ | ||
| 283 | push (@widgets,shift ); | ||
| 284 | } | ||
| 285 | sub addStyle{ | ||
| 286 | my ($name,$style)=@_; | ||
| 287 | $styles{$name}=$style; | ||
| 288 | } | ||
| 289 | sub addVar{ | ||
| 290 | my ($varName,$value)=@_; | ||
| 291 | $oVars{$varName}=$value; | ||
| 292 | } | ||
| 293 | |||
| 294 | # Functions for internal use | ||
| 295 | sub getSize{ | ||
| 296 | my ($self,$id)=@_; | ||
| 297 | my $found=getItem($self,$id); | ||
| 298 | return ( $found!=-1) ? $widgets[$found][5]:0; | ||
| 299 | |||
| 300 | } | ||
| 301 | sub getLocation{ | ||
| 302 | my ($self,$id)=@_; | ||
| 303 | my $found=getItem($self,$id); | ||
| 304 | return ( $found!=-1) ? $widgets[$found][4]:0; | ||
| 305 | |||
| 306 | } | ||
| 307 | sub getItem{ | ||
| 308 | my ($self,$id)=@_; | ||
| 309 | $id=~s/[^\d]//g; | ||
| 310 | my $i=0; my $found=-1; | ||
| 311 | while ($i<@widgets){ | ||
| 312 | if ($widgets[$i][1]==$id) { | ||
| 313 | $found=$i; | ||
| 314 | } | ||
| 315 | $i++; | ||
| 316 | } | ||
| 317 | return $found; | ||
| 318 | } | ||
| 319 | |||
| 320 | sub dialogAction{ | ||
| 321 | my ($self,$name,$action)=@_; | ||
| 322 | $dialogDispatch{$name}=$action; | ||
| 323 | |||
| 324 | } | ||
| 325 | sub setScale{ | ||
| 326 | $winScale=shift; | ||
| 327 | }; | ||
| 328 | |||
| 329 | sub getFrame{ | ||
| 330 | my $self=shift; | ||
| 331 | return $self; | ||
| 332 | |||
| 333 | }; | ||
| 334 | |||
| 335 | # The functions for GUI Interactions | ||
| 336 | #Static Text functions | ||
| 337 | sub setLabel{ | ||
| 338 | my ($self,$id,$text)=@_; | ||
| 339 | $connection->send_utf8("action=setLabel&id=$id&value=$text"); | ||
| 340 | |||
| 341 | } | ||
| 342 | |||
| 343 | #Image functions | ||
| 344 | sub setImage{ | ||
| 345 | my ($self,$id,$file)=@_; | ||
| 346 | $connection->send_utf8("action=setImage&id=$id&value=$file"); | ||
| 347 | |||
| 348 | } | ||
| 349 | |||
| 350 | #Text input functions | ||
| 351 | sub getValue{ | ||
| 352 | my ($self,$id)=@_; | ||
| 353 | $connection->send_utf8("action=getValue&id=$id"); | ||
| 354 | $connection->send_utf8("action=getValue&id=$id"); | ||
| 355 | return $iVars{$id}; | ||
| 356 | } | ||
| 357 | |||
| 358 | sub setValue{ | ||
| 359 | my ($self,$id,$text)=@_; | ||
| 360 | $connection->send_utf8("action=setValue&id=$id&value=$text"); | ||
| 361 | $iVars{$id}=$text; | ||
| 362 | } | ||
| 363 | sub appendValue{ | ||
| 364 | my ($self,$id,$text)=@_; | ||
| 365 | $connection->send_utf8("action=appendValue&id=$id&value=$text"); | ||
| 366 | } | ||
| 367 | |||
| 368 | #Message box, Fileselector and Dialog Boxes | ||
| 369 | sub showFileSelectorDialog{ | ||
| 370 | my ($self, $message,$load,$file) = @_; | ||
| 371 | $connection->send_utf8("action=showFileSelector&id=fileSelector&value=$load#$message#$file"); | ||
| 372 | }; | ||
| 373 | sub showDialog{ | ||
| 374 | my ($self, $title, $message,$response,$icon) = @_; | ||
| 375 | $connection->send_utf8("action=showDialog&id=dialog&value=$icon#$response#$title#$message"); | ||
| 376 | }; | ||
| 377 | |||
| 378 | # Quit | ||
| 379 | sub quit{ | ||
| 380 | $webApp->shutdown(); | ||
| 381 | } | ||
| 382 | |||
| 383 | sub DESTROY { | ||
| 384 | unlink $clientFileName | ||
| 385 | } | ||
| 386 | |||
| 387 | sub css{ | ||
| 388 | return <<ENDCSS | ||
| 389 | ul.menubar { | ||
| 390 | list-style-type: none; | ||
| 391 | margin: 0; | ||
| 392 | padding: 0; | ||
| 393 | overflow: hidden; | ||
| 394 | background-color: #333; | ||
| 395 | } | ||
| 396 | |||
| 397 | .menuhead { | ||
| 398 | float: left; | ||
| 399 | } | ||
| 400 | |||
| 401 | .menuhead a, .dropbtn { | ||
| 402 | display: inline-block; | ||
| 403 | color: white; | ||
| 404 | text-align: center; | ||
| 405 | padding: 1px 3px; | ||
| 406 | text-decoration: none; | ||
| 407 | } | ||
| 408 | |||
| 409 | li.menuhead a:hover, .dropdown:hover .dropbtn { | ||
| 410 | background-color: red; | ||
| 411 | } | ||
| 412 | |||
| 413 | .menuhead .dropdown { | ||
| 414 | display: inline-block; | ||
| 415 | } | ||
| 416 | |||
| 417 | .dropdown-content { | ||
| 418 | display: none; | ||
| 419 | position: absolute; | ||
| 420 | background-color: #f9f9f9; | ||
| 421 | min-width: 160px; | ||
| 422 | box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2); | ||
| 423 | z-index: 1; | ||
| 424 | } | ||
| 425 | |||
| 426 | .dropdown-content a { | ||
| 427 | color: black; | ||
| 428 | padding: 1px 3x; | ||
| 429 | text-decoration: none; | ||
| 430 | display: block; | ||
| 431 | text-align: left; | ||
| 432 | } | ||
| 433 | |||
| 434 | .dropdown-content a:hover {background-color: #f1f1f1} | ||
| 435 | |||
| 436 | .dropdown:hover .dropdown-content { | ||
| 437 | display: block; | ||
| 438 | } | ||
| 439 | |||
| 440 | .dialogBox{ | ||
| 441 | width:$winWidth; | ||
| 442 | height:$winHeight; | ||
| 443 | background-color:grey; | ||
| 444 | opacity: 0.5; | ||
| 445 | } | ||
| 446 | |||
| 447 | .buttonBox{ | ||
| 448 | width: 400px; | ||
| 449 | height: 100px; | ||
| 450 | background-color:lightgrey; | ||
| 451 | position:relative; | ||
| 452 | left:0; right:0; | ||
| 453 | top:20%; bottom:0; | ||
| 454 | margin:auto; | ||
| 455 | } | ||
| 456 | |||
| 457 | |||
| 458 | ENDCSS | ||
| 459 | |||
| 460 | } | ||
| 461 | |||
| 462 | sub header{ | ||
| 463 | return <<END | ||
| 464 | <html> | ||
| 465 | END | ||
| 466 | |||
| 467 | } | ||
| 468 | |||
| 469 | sub js{ | ||
| 470 | return <<END | ||
| 471 | |||
| 472 | var ws; | ||
| 473 | var logWin; | ||
| 474 | var start=new Date(); | ||
| 475 | var binaryBuffer; | ||
| 476 | var logWin=$debug; | ||
| 477 | |||
| 478 | function WebSocketStart() | ||
| 479 | { | ||
| 480 | if (logWin){ | ||
| 481 | logWin=window.open('','Logs', target='_blank', 'toolbar=0,scrollbars=1,statusbar=0,menubar=0,resizable=0,height=500,width=433'); | ||
| 482 | if (logWin){ logWin.document.write("<h1>GUIDeFATE Websocket Log opened</h1><br>\\n"); | ||
| 483 | logWin.blur(); | ||
| 484 | window.focus(); | ||
| 485 | } | ||
| 486 | } | ||
| 487 | |||
| 488 | |||
| 489 | if ("WebSocket" in window) { | ||
| 490 | ws = new WebSocket("ws://$host:$port"); | ||
| 491 | |||
| 492 | ws.binaryType = "blob"; | ||
| 493 | |||
| 494 | ws.onopen = function(){ log("red","Web App Started")}; | ||
| 495 | |||
| 496 | ws.onmessage = function (evt) { | ||
| 497 | var received_msg = evt.data; | ||
| 498 | log("blue",received_msg); | ||
| 499 | parseMessage(received_msg); | ||
| 500 | }; | ||
| 501 | |||
| 502 | ws.onclose = function(){ log("red","Connection is closed..."); }; | ||
| 503 | } | ||
| 504 | else { log("blue","WebSocket NOT supported by your Browser!"); } }; | ||
| 505 | |||
| 506 | function send(msg){ | ||
| 507 | log("red",msg); | ||
| 508 | ws.send(msg); | ||
| 509 | return false; | ||
| 510 | } | ||
| 511 | |||
| 512 | function sendBinary(msg){ | ||
| 513 | log("red","sending binary data"); | ||
| 514 | ws.send(msg); | ||
| 515 | return false; | ||
| 516 | } | ||
| 517 | |||
| 518 | |||
| 519 | // The handful of commands are parsed here and results reported back to server | ||
| 520 | function parseMessage(msg){ | ||
| 521 | if (msg.indexOf("Mess")==0){return;} //do nothing with informational messages | ||
| 522 | else if(msg.indexOf("Binary")==0){ | ||
| 523 | binaryBuffer.sendNext(); | ||
| 524 | return; | ||
| 525 | } | ||
| 526 | var actionRE=/^action=([^&]*)&/; | ||
| 527 | var IDRE=/&id=([^&]*)&?/; | ||
| 528 | var valueRE=/&value=([\\s\\S]*)\$/; | ||
| 529 | var action=actionRE.exec(msg); | ||
| 530 | var id=IDRE.exec(msg); | ||
| 531 | var value=valueRE.exec(msg); | ||
| 532 | log("brown",action+"<br>"+id + "<br>"+value); | ||
| 533 | |||
| 534 | |||
| 535 | |||
| 536 | if (!id || !id[1]) { | ||
| 537 | |||
| 538 | log("green","NO ID or no item with ID '"+id[1] +"'"); | ||
| 539 | //send ("Error:NO ID or no item with ID '"+id[1] +"'") | ||
| 540 | } | ||
| 541 | else{ | ||
| 542 | switch (action[1]){ | ||
| 543 | case "getValue": | ||
| 544 | log("green", "client replies to getValue " + id[1]+ " with result :" +document.getElementById(id[1]).value); | ||
| 545 | send ("ID="+id[1]+"&Value='"+document.getElementById(id[1]).value+"'"); | ||
| 546 | break; | ||
| 547 | case "setValue": | ||
| 548 | document.getElementById(id[1]).value=value[1]; | ||
| 549 | log("green", "Success:"+id[1]+" now has value "+document.getElementById(id[1]).value); | ||
| 550 | send ("ID="+id[1]+"&Value="+document.getElementById(id[1]).value); | ||
| 551 | break; | ||
| 552 | case "appendValue": | ||
| 553 | document.getElementById(id[1]).value+=value[1]; | ||
| 554 | log("green", "Success:"+id[1]+" now has value "+document.getElementById(id[1]).value); | ||
| 555 | send ("ID="+id[1]+"&Value="+document.getElementById(id[1]).value); | ||
| 556 | break; | ||
| 557 | case "setLabel": | ||
| 558 | document.getElementById(id[1]).innerHTML=value[1]; | ||
| 559 | log("green", "Success:"+id[1]+" now has text.. "+document.getElementById(id[1]).innerHTML); | ||
| 560 | break; | ||
| 561 | case "setImage": | ||
| 562 | document.getElementById(id[1]).src=value[1]; | ||
| 563 | log("green", "Success:"+id[1]+" now has src.. "+value[1]); | ||
| 564 | |||
| 565 | break; | ||
| 566 | case "showFileSelector": | ||
| 567 | var dlg=(value[1]).split("#") | ||
| 568 | log("green","showing File Selector, Load/Save "+dlg[0] + ", Message "+dlg[1] + ", File "+dlg[2]) | ||
| 569 | document.getElementById("dialogTitle").innerHTML="<strong>File Operation</strong>"; | ||
| 570 | document.getElementById("dialogMessage").innerHTML=dlg[1]+"<br>"; | ||
| 571 | document.getElementById("dialogButtons").innerHTML=""; | ||
| 572 | if (dlg[0]=="1") {addFileButton();} | ||
| 573 | else { | ||
| 574 | document.getElementById("dialogMessage").innerHTML+="<em>Right click to Download and save-as desired</em><br>"; | ||
| 575 | document.getElementById("dialogMessage").innerHTML+="<a href='dataFiles/"+dlg[2] +"' download>DOWNLOAD "+dlg[2]+" </a>" | ||
| 576 | } | ||
| 577 | addDialogButton("Dialog","Cancel"); | ||
| 578 | hideDiv("window"); | ||
| 579 | showDiv("dialogBox"); | ||
| 580 | break; | ||
| 581 | case "showDialog": | ||
| 582 | var dlg=(value[1]).split("#") | ||
| 583 | log("green","showing Dialog with Icon "+dlg[0] + ", Response "+dlg[1] + ", Title "+dlg[2] + ", Message "+dlg[3]); | ||
| 584 | document.getElementById("dialogTitle").innerHTML=dlg[2]; | ||
| 585 | document.getElementById("dialogMessage").innerHTML=dlg[3]+"<br>"; | ||
| 586 | document.getElementById("dialogButtons").innerHTML=""; | ||
| 587 | if (dlg[1].indexOf("O")!=-1) addDialogButton("Dialog","OK"); | ||
| 588 | if (dlg[1].indexOf("Y")!=-1) addDialogButton("Dialog","Yes"); | ||
| 589 | if (dlg[1].indexOf("N")!=-1) addDialogButton("Dialog","No"); | ||
| 590 | if (dlg[1].indexOf("C")!=-1) addDialogButton("Dialog","Cancel"); | ||
| 591 | hideDiv("window"); | ||
| 592 | showDiv("dialogBox"); | ||
| 593 | break; | ||
| 594 | default: | ||
| 595 | log ("teal","unrecognised command "+action[1]) | ||
| 596 | } | ||
| 597 | } | ||
| 598 | }; | ||
| 599 | |||
| 600 | |||
| 601 | //object that stores a binary file data and send is 64000 bytes at a time | ||
| 602 | function BinaryBuffer(blob){ | ||
| 603 | this.data=blob; | ||
| 604 | this.name=blob.name; | ||
| 605 | this.size=blob.size; | ||
| 606 | this.sendIndex=0; | ||
| 607 | |||
| 608 | this.sendNext=function(){ | ||
| 609 | var endIndex=this.sendIndex+64000; | ||
| 610 | if (endIndex>this.size) | ||
| 611 | { endIndex=this.size }; | ||
| 612 | if (this.sendIndex<this.size) | ||
| 613 | {sendBinary(this.data.slice(this.sendIndex,endIndex) ); | ||
| 614 | this.sendIndex+=64000} | ||
| 615 | else | ||
| 616 | { send("File ends:"+this.name+"\\n") } | ||
| 617 | } | ||
| 618 | } | ||
| 619 | |||
| 620 | function log(colour, message){ | ||
| 621 | if(logWin) logWin.document.write("<p style='margin:0;color:"+colour+"'>"+message+"</p>\\n"); | ||
| 622 | else { console.log(message) } | ||
| 623 | } | ||
| 624 | |||
| 625 | |||
| 626 | function act(command,label){ | ||
| 627 | if (typeof window[command] === "function") { //these are functions internal to the javascript engine or in the external javascrpt file | ||
| 628 | window[strOfFunction](label); | ||
| 629 | } | ||
| 630 | else if (command=="UploadFile"){ //label contains a blob the file | ||
| 631 | binaryBuffer=new BinaryBuffer(label); | ||
| 632 | send("Sending File with size "+binaryBuffer.size+"\\n"); | ||
| 633 | send("File follows:"+binaryBuffer.name+"\\n"); | ||
| 634 | } | ||
| 635 | else{ | ||
| 636 | if (command.match(/textctrl|combo/i)){ // for text ctrls and combos send ID and value/content | ||
| 637 | var content=(document.getElementId(command).value!="")?document.getElementId(command).value:"EmptyString" | ||
| 638 | send("ID="+ command + "&Value='"+content + "'") | ||
| 639 | } | ||
| 640 | send("Function="+ command + "&Label="+encodeURIComponent(label)) //for buttons and menu items send ID and label | ||
| 641 | } | ||
| 642 | } | ||
| 643 | |||
| 644 | function addFileButton(){ | ||
| 645 | var btn=document.createElement("input"); | ||
| 646 | btn.type="file"; | ||
| 647 | btn.onchange=function(){ | ||
| 648 | act("UploadFile",this.files[0]); | ||
| 649 | hideDiv("dialogBox"); | ||
| 650 | showDiv("window"); | ||
| 651 | } | ||
| 652 | document.getElementById("dialogButtons").appendChild(btn); | ||
| 653 | } | ||
| 654 | function addDialogButton(command,label){ | ||
| 655 | var btn=document.createElement("input"); | ||
| 656 | btn.type="button"; | ||
| 657 | btn.value=label; | ||
| 658 | btn.command=command; | ||
| 659 | btn.onclick=function(){ | ||
| 660 | hideDiv("dialogBox"); | ||
| 661 | showDiv("window"); | ||
| 662 | act(this.command,this.value); | ||
| 663 | } | ||
| 664 | document.getElementById("dialogButtons").appendChild(btn); | ||
| 665 | } | ||
| 666 | |||
| 667 | //functions to hide and show divs by ID | ||
| 668 | function hideDiv(id){ | ||
| 669 | document.getElementById(id).style.visibility="hidden"; | ||
| 670 | document.getElementById(id).style.display="none"; | ||
| 671 | } | ||
| 672 | function showDiv(id){ | ||
| 673 | document.getElementById(id).style.visibility="visible"; | ||
| 674 | document.getElementById(id).style.display="block"; | ||
| 675 | } | ||
| 676 | |||
| 677 | |||
| 678 | window.onbeforeunload = function() { | ||
| 679 | websocket.onclose = function () {}; // disable onclose handler first | ||
| 680 | websocket.close() | ||
| 681 | } | ||
| 682 | |||
| 683 | window.onload=function(){ | ||
| 684 | window.resizeTo($winWidth,$winHeight); | ||
| 685 | hideDiv("dialogBox"); | ||
| 686 | WebSocketStart(); | ||
| 687 | |||
| 688 | } | ||
| 689 | |||
| 690 | // this allows the websocket to restart incase focus was lost by starting another listener | ||
| 691 | window.onfocus=function(){ | ||
| 692 | WebSocketStart(); | ||
| 693 | |||
| 694 | } | ||
| 695 | |||
| 696 | END | ||
| 697 | |||
| 698 | } | ||
| 699 | |||
| 700 | |||
| 701 | sub dialogBoxDiv{ | ||
| 702 | return <<END | ||
| 703 | <!-Container for Dialog Box--> | ||
| 704 | <div id=dialogBox class=dialogBox> | ||
| 705 | <div id=dialogButtonBox class=buttonBox> | ||
| 706 | <center> | ||
| 707 | <div id=dialogTitle></div> | ||
| 708 | <div id=dialogMessage></div> | ||
| 709 | <div id=dialogButtons></div> | ||
| 710 | </center> | ||
| 711 | </div> | ||
| 712 | |||
| 713 | </div> | ||
| 714 | END | ||
| 715 | |||
| 716 | } | ||
| 717 | |||
| 718 | 1; | ||
| 719 |