r50183 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r50182‎ | r50183 | r50184 >
Date:16:21, 4 May 2009
Author:thomasv
Status:ok
Tags:
Comment:
various fixes for IE; indentation; use of the DOM
Modified paths:
  • /trunk/extensions/ProofreadPage/proofread.js (modified) (history)

Diff [purge]

Index: trunk/extensions/ProofreadPage/proofread.js
@@ -145,11 +145,11 @@
146146 if( h.style.cssText == ''){
147147 h.style.cssText = 'display:none';
148148 f.style.cssText = 'display:none';
149 - if(self.TextBoxHeight) box.style.cssText = "height:"+(TextBoxHeight-7)+"px";
 149+ if(self.vertHeight) box.style.cssText = "height:"+(vertHeight-7)+"px";
150150 } else {
151151 h.style.cssText = '';
152152 f.style.cssText = '';
153 - if(self.TextBoxHeight) box.style.cssText = "height:"+(TextBoxHeight-270)+"px";
 153+ if(self.vertHeight) box.style.cssText = "height:"+(vertHeight-270)+"px";
154154 }
155155 }
156156
@@ -189,6 +189,7 @@
190190
191191 /*relative coordinates of the mouse pointer*/
192192 function get_xy(evt){
 193+
193194 if(typeof(evt) == 'object') {
194195 evt = evt?evt:window.event?window.event:null; if(!evt){ return false;}
195196 if(evt.pageX) {
@@ -196,7 +197,6 @@
197198 yy=evt.pageY - ffoy;
198199 }
199200 else {
200 - if(typeof(document.getElementById("ImageContainer")+1) == 'number') {return true;}
201201 xx=evt.clientX - ieox;
202202 yy=evt.clientY - ieoy;
203203 }
@@ -206,21 +206,18 @@
207207 yy = lastyy;
208208 }
209209 lastxx = xx;
210 - lastyy = yy;
211 -
 210+ lastyy = yy;
 211+
212212 }
213213
214214 //mouse move
215215 function zoom_move(evt) {
216216
217217 if(zoom_status != 1) { return false;}
218 -
 218+ evt = evt?evt:window.event?window.event:null; if(!evt){ return false;}
219219 get_xy(evt);
220 -
221 - zp_clip.style.margin = ((yy > objh )?(objh*(1-zoomamount_h)):(yy*(1-zoomamount_h))) + 'px 0px 0px '
222 - + ((xx > objw )?(objw*(1-zoomamount_w)):(xx*(1-zoomamount_w)))
223 - + 'px';
224 -
 220+ zp_clip.style.margin = ((yy > objh )?(objh*(1-zoomamount_h)):(yy*(1-zoomamount_h))) + 'px 0px 0px '
 221+ + ((xx > objw )?(objw*(1-zoomamount_w)):(xx*(1-zoomamount_w))) + 'px';
225222 return false;
226223 }
227224
@@ -239,7 +236,7 @@
240237
241238
242239 function countoffset() {
243 - zme=document.getElementById("ImageContainer");
 240+ zme=document.getElementById("pr_container");
244241 ieox=0; ieoy=0;
245242 for(zmi=0;zmi<50;zmi++) {
246243 if(zme+1 == 1) {
@@ -262,21 +259,20 @@
263260
264261 function zoom_mouseup(evt) {
265262
266 - evt = evt?evt:window.event?window.event:null;
267 - if(!evt) return false;
 263+ evt = evt?evt:window.event?window.event:null; if(!evt){ return false;}
268264
269 - //only left button; see http://unixpapa.com/js/mouse.html for why it is this complicated
270 - if(evt.which == null) {
271 - if(evt.button != 1) return false;
272 - } else {
 265+ //only left button; see http://unixpapa.com/js/mouse.html for why it is this complicated
 266+ if(evt.which == null) {
 267+ if(evt.button != 1) return false;
 268+ } else {
273269 if(evt.which > 1) return false;
274 - }
 270+ }
275271
276272 if(zoom_status == 0) {
277273 zoom_on(evt);
278274 return false;
279 - }
280 - else if(zoom_status == 1) {
 275+ }
 276+ else if(zoom_status == 1) {
281277 zoom_status = 2;
282278 return false;
283279 }
@@ -288,9 +284,6 @@
289285 }
290286
291287
292 -
293 -
294 -
295288 function zoom_on(evt) {
296289 evt = evt?evt:window.event?window.event:null; if(!evt){ return false;}
297290 zoom_status=1;
@@ -309,7 +302,7 @@
310303 zoomamount_h = zp_clip.height/objh;
311304 zoomamount_w = zp_clip.width/objw;
312305
313 - zp_container.style.margin = '0px 0px 0px 0px';
 306+ zp_container.style.margin = '0px 0px 0px 0px';
314307 zp_container.style.width = objw+'px';
315308 zp_container.style.height = objh+'px';
316309
@@ -321,20 +314,17 @@
322315 //zoom using two images (magnification glass)
323316 function proofreadPageZoom(){
324317
325 - if(navigator.appName == "Microsoft Internet Explorer") return;
326318 if(!self.proofreadPageViewURL) return;
327319 if(self.DisplayWidth>800) return;
328320
329 - zp = document.getElementById("ImageContainer");
 321+ zp = document.getElementById("pr_container");
330322 if(zp){
331323 var hires_url = pr_image_url(800);
332324 self.objw = zp.firstChild.width;
333325 self.objh = zp.firstChild.height;
334326
335 - zp.setAttribute("onmouseup","zoom_mouseup(event);" );
336 - zp.setAttribute("onmousemove","zoom_move(event);" );
337 -
338 -
 327+ zp.onmouseup = zoom_mouseup;
 328+ zp.onmousemove = zoom_move;
339329 zp_container = document.createElement("div");
340330 zp_container.style.cssText ="position:absolute; width:0; height:0; overflow:hidden;";
341331 zp_clip = document.createElement("img");
@@ -353,13 +343,14 @@
354344 *
355345 ********************************/
356346
357 -var margin_x = 0;
 347+var margin_x = 0;
358348 var margin_y = 0;
359 -var prev_margin_x = 0;
 349+var prev_margin_x = 0;
360350 var prev_margin_y = 0;
361 -var init_x = 0;
 351+var init_x = 0;
362352 var init_y = 0;
363353
 354+
364355 function pr_drop(evt){
365356 prev_margin_x = margin_x;
366357 prev_margin_y = margin_y;
@@ -367,18 +358,15 @@
368359 document.onmousemove = null;
369360 document.onmousedown = null;
370361 zp_container.onmousemove = pr_move;
371 -
372362 return false;
373363 }
374364
375365 function pr_grab(evt){
376366
 367+ evt = evt?evt:window.event?window.event:null; if(!evt){ return false;}
377368 zp_img = document.getElementById("ProofReadImage");
378369 zp_container = document.getElementById("pr_container");
379370
380 - evt = evt?evt:window.event?window.event:null;
381 - if(!evt) return false;
382 -
383371 //only left button; see http://unixpapa.com/js/mouse.html for why it is this complicated
384372 if(evt.which == null) {
385373 if(evt.button != 1) return false;
@@ -392,37 +380,39 @@
393381 zp_container.onmousemove = pr_drag;
394382
395383 if(evt.pageX) {
396 - countoffset();
397 - lastxx=evt.pageX - ffox;
398 - lastyy=evt.pageY - ffoy;
 384+ countoffset();
 385+ lastxx=evt.pageX - ffox;
 386+ lastyy=evt.pageY - ffoy;
399387 }
400388 else {
401 - countoffset();
402 - lastxx=evt.clientX - ieox;
403 - lastyy=evt.clientY - ieoy;
 389+ countoffset();
 390+ lastxx=evt.clientX - ieox;
 391+ lastyy=evt.clientY - ieoy;
404392 }
405 -
 393+
406394 init_x = lastxx;
407395 init_y = lastyy;
408 -
 396+
409397 return false;
410398
411399 }
412400
413401
414402 function pr_move(evt) {
 403+ evt = evt?evt:window.event?window.event:null; if(!evt){ return false;}
415404 countoffset();
416405 get_xy(evt);
417406 }
418407
419408 function pr_drag(evt) {
 409+ evt = evt?evt:window.event?window.event:null; if(!evt){ return false;}
420410 get_xy(evt);
421 - //new
422 - margin_x = prev_margin_x - (init_x-xx); //*(1-zoomamount_w);
423 - margin_y = prev_margin_y - (init_y-yy); //*(1-zoomamount_h);
424 - zp_img.style.margin = margin_y + 'px 0px 0px ' + margin_x + 'px';
425 -
426 - if (evt.preventDefault) evt.preventDefault();
 411+ margin_x = prev_margin_x - (init_x-xx);
 412+ margin_y = prev_margin_y - (init_y-yy);
 413+ zp_img.style.margin = margin_y + 'px 0px 0px ' + margin_x + 'px';
 414+ image_container.style.cssText = self.container_css; //needed by IE6
 415+
 416+ if (evt.preventDefault) evt.preventDefault();
427417 evt.returnValue = false;
428418 return false;
429419 }
@@ -430,12 +420,15 @@
431421
432422 function pr_zoom(delta){
433423
 424+ image_container = document.getElementById("pr_container");
434425 zp_img = document.getElementById("ProofReadImage");
435426 if(!zp_img) return;
436427
437428 if (delta == 0) {
438 - zp_img.width = document.getElementById("ImageContainer").offsetWidth; //-20;
439 - zp_img.style.margin = '0px 0px 0px 0px';
 429+ zp_img.width = image_container.offsetWidth;
 430+ zp_img.style.margin = '0px 0px 0px 0px';
 431+ image_container.style.cssText = self.container_css; //needed by IE6
 432+
440433 prev_margin_x = margin_x = 0;
441434 prev_margin_y = margin_y = 0;
442435 }
@@ -444,40 +437,43 @@
445438 var new_width = Math.round(zp_img.width*Math.pow(1.1,delta));
446439 var delta_w = new_width - old_width;
447440 var s = (delta_w>0)?1:-1;
448 -
 441+
449442 for(var dw=s; dw != delta_w; dw=dw+s){
450443 zp_img.width = old_width + dw;//this adds 1 pixel
451 - //magnification factor
452 - var lambda = (old_width+dw)/old_width;
453 - margin_x = xx - lambda*(xx - prev_margin_x);
454 - margin_y = yy - lambda*(yy - prev_margin_y);
455 - zp_img.style.margin = margin_y + 'px 0px 0px ' + margin_x + 'px';
 444+ image_container.style.cssText = self.container_css; //needed by IE6
 445+ if(xx){
 446+ //magnification factor
 447+ var lambda = (old_width+dw)/old_width;
 448+ margin_x = xx - lambda*(xx - prev_margin_x);
 449+ margin_y = yy - lambda*(yy - prev_margin_y);
 450+ zp_img.style.margin = Math.round(margin_y) + 'px 0px 0px ' + Math.round(margin_x) + 'px';
 451+ }
456452 }
457453 prev_margin_x = margin_x;
458454 prev_margin_y = margin_y;
 455+
459456 }
460457 }
461458
462 -function pr_zoom_wheel(event){
463 -// see http://adomas.org/javascript-mouse-wheel/
464 -
465 - var delta = 0;
466 - if (!event) /* For IE. */
467 - event = window.event;
468 - if (event.wheelDelta) { /* IE/Opera. */
469 - delta = event.wheelDelta/120;
470 - /** In Opera 9, delta differs in sign as compared to IE.*/
471 - if (window.opera)
472 - delta = -delta;
473 - } else if (event.detail) { /** Mozilla case. */
474 - /** In Mozilla, sign of delta is different than in IE.
475 - * Also, delta is multiple of 3.
476 - */
477 - delta = -event.detail/3;
478 - }
 459+function pr_zoom_wheel(evt){
 460+ evt = evt?evt:window.event?window.event:null; if(!evt){ return false;}
 461+ var delta = 0;
 462+ if (evt.wheelDelta) {
 463+ /* IE/Opera. */
 464+ delta = evt.wheelDelta/120;
 465+ /** In Opera 9, delta differs in sign as compared to IE.*/
 466+ if (window.opera) delta = -delta;
 467+ }
 468+ else if (evt.detail) {
 469+ /** Mozilla case. */
 470+ /** In Mozilla, sign of delta is different than in IE.
 471+ * Also, delta is multiple of 3.
 472+ */
 473+ delta = -evt.detail/3;
 474+ }
479475 if(delta) pr_zoom(delta);
480 - if (event.preventDefault) event.preventDefault();
481 - event.returnValue = false;
 476+ if(evt.preventDefault) evt.preventDefault();
 477+ evt.returnValue = false;
482478 }
483479
484480
@@ -487,24 +483,25 @@
488484
489485
490486
491 -
 487+/*do not use a table in the horizontal case ?*/
492488 function pr_fill_table(horizontal_layout){
493489
494 - //remove existing body
495 - if(self.table.firstChild){
 490+ //remove existing content
 491+ while(self.table.firstChild){
496492 self.table.removeChild(self.table.firstChild);
497493 }
498494
499 - //create table body
500 - var t_body = document.createElement("tbody");
501 - self.table.appendChild(t_body);
502 - var cell_left = document.createElement("td");
503 - var cell_right = document.createElement("td");
504 -
505495 if(!proofreadPageIsEdit) horizontal_layout=false;
506496
507497 //first setup the layout
508498 if(!horizontal_layout) {
 499+ //use a table only here
 500+ var t_table = document.createElement("table");
 501+ var t_body = document.createElement("tbody");
 502+ var cell_left = document.createElement("td");
 503+ var cell_right = document.createElement("td");
 504+ t_table.appendChild(t_body);
 505+
509506 var t_row = document.createElement("tr");
510507 t_row.setAttribute("valign","top");
511508 cell_left.style.cssText = "width:50%; padding-right:0.5em;";
@@ -512,28 +509,30 @@
513510 t_row.appendChild(cell_left);
514511 t_row.appendChild(cell_right);
515512 t_body.appendChild(t_row);
 513+
 514+ self.table.appendChild(t_table);
516515 }
517516 else {
518 - var t_row1 = document.createElement("tr");
519 - var t_row2 = document.createElement("tr");
520 - t_body.appendChild(t_row2);
521 - t_body.appendChild(t_row1);
522 - t_row1.appendChild(cell_left);
523 - t_row2.appendChild(cell_right);
 517+ var cell_left = document.createElement("div");
 518+ var cell_right = document.createElement("div");
 519+ self.table.appendChild(cell_right);
 520+ self.table.appendChild(cell_left);
524521 }
525522
526523
527524 //create image and text containers
528525 var image_container = document.createElement("div");
529 - image_container.setAttribute("id", "ImageContainer");
 526+ image_container.setAttribute("id", "pr_container");
 527+ image_container.style.cssText = "background:#0000ff; overflow:hidden;";
 528+
530529 cell_right.appendChild(image_container);
531530 cell_left.appendChild(self.text_container);
532531
 532+ self.pr_horiz = horizontal_layout;
533533
534 - //fill the image container
 534+ //compute the dimensions of the image container
535535 if(!horizontal_layout){
536536
537 - self.pr_horiz = false;
538537 var desired_width = 400;
539538 if (parseInt(navigator.appVersion)>3) {
540539 if (navigator.appName.indexOf("Microsoft")!=-1) {
@@ -548,58 +547,50 @@
549548
550549 //self.DisplayHeight is known if the image is not external
551550 if(self.DisplayHeight)
552 - self.TextBoxHeight = self.DisplayHeight;
 551+ self.vertHeight = self.DisplayHeight;
553552 else
554 - self.TextBoxHeight = 700;
555 -
556 - //fill image container
557 - if(!proofreadPageIsEdit) {
558 - var image = document.createElement("img");
559 - image.setAttribute("src", thumb_url);
560 - image.setAttribute("id", "ProofReadImage");
561 - image.setAttribute("width", self.DisplayWidth);
562 - image.style.cssText = "padding:0;margin:0;border:0;";
563 - image_container.appendChild(image);
564 - }
565 - else{
566 - var s = "<div id=\"pr_container\" style=\"background:#000000; overflow: auto; width: "+self.DisplayWidth+"px; height:"+self.TextBoxHeight+"px;\">";
567 - s = s + "<img id=\"ProofReadImage\" src=\""+ proofreadPageViewURL +"\" alt=\""+ proofreadPageViewURL +"\"";
568 - s = s + " width=\"" + self.DisplayWidth +"\"></div>";
569 - image_container.innerHTML = s;
570 - document.getElementById("wpTextbox1").style.cssText = "height:"+(self.TextBoxHeight-7)+"px";
571 - pr_zoom(0);
572 - }
 553+ self.vertHeight = 700;
573554 }
574555 else{
575 - //horizontal layout
576 - self.pr_horiz = true;
577 -
578 - if(document.selection && !is_gecko)
579 - self.vertHeight=Math.ceil(document.body.clientHeight*0.4);
 556+ if(document.selection && !is_gecko){
 557+ self.vertHeight = Math.ceil(document.body.clientHeight*0.4);
 558+ }
580559 else
581 - self.vertHeight=Math.ceil(window.innerHeight*0.4);
 560+ self.vertHeight = Math.ceil(window.innerHeight*0.4);
 561+ }
582562
583 - var s = "<div id=\"pr_container\" style= \"background:#000000; overflow: auto; height: "
584 - + self.vertHeight + "px; width: 100%;\">";
585 - s = s + "<img id=\"ProofReadImage\" src=\""+ proofreadPageViewURL +"\" alt=\""+ proofreadPageViewURL +"\"";
586 - s = s + "\"></div>";
587563
588 - image_container.innerHTML = s;
589 -
590 - if(proofreadPageIsEdit){
591 - document.getElementById("wpTextbox1").style.cssText = "height:"+self.vertHeight+"px";
592 - pr_zoom(0);
 564+ //fill the image container
 565+ if(!proofreadPageIsEdit) {
 566+ var image = document.createElement("img");
 567+ image_container.appendChild(image);
 568+ image.setAttribute("id", "ProofReadImage");
 569+ image.setAttribute("src", thumb_url);
 570+ image.setAttribute("width", self.DisplayWidth);
 571+ image.style.cssText = "padding:0;margin:0;border:0;";
 572+ }
 573+ else{
 574+ if(!horizontal_layout){
 575+ cont_w = self.DisplayWidth+"px";
 576+ img_w = self.DisplayWidth;
593577 }
 578+ else{
 579+ cont_w = "100%";
 580+ img_w = 0; //this prevents the container from being resized.
 581+ }
 582+ self.container_css = "background:#000000; overflow:hidden; width:"+cont_w+"; height:"+self.vertHeight+"px;";
 583+ image_container.innerHTML = "<img id=\"ProofReadImage\" src=\""+proofreadPageViewURL+"\" width=\""+img_w+"\" />";
 584+ image_container.style.cssText = self.container_css;
 585+ document.getElementById("wpTextbox1").style.cssText = "height:"+self.vertHeight+"px";
 586+ pr_zoom(0);
594587 }
595588
596589 //setup the mouse wheel listener
597 - if(proofreadPageIsEdit) {
 590+ if(proofreadPageIsEdit) {
598591 if (image_container.addEventListener) image_container.addEventListener('DOMMouseScroll', pr_zoom_wheel, false);
599 - image_container.setAttribute("onmousewheel","pr_zoom_wheel(event);" );
600 - //image_container.setAttribute("onmousemove", "pr_zoom_move(event);" );
601 - //image_container.setAttribute("onmouseup", "pr_zoom_up(event);" );
602 - image_container.setAttribute("onmousedown", "pr_grab(event);" );
603 - image_container.onmousemove=pr_move;
 592+ image_container.onmousewheel = pr_zoom_wheel;
 593+ image_container.onmousedown = pr_grab;
 594+ image_container.onmousemove = pr_move;
604595
605596 zp_img = document.getElementById("ProofReadImage");
606597 zp_container = document.getElementById("pr_container");
@@ -614,14 +605,14 @@
615606
616607 function pr_setup() {
617608
618 - self.table = document.createElement("table");
 609+ self.table = document.createElement("div");
619610 self.text_container = document.createElement("div");
620611 self.image_container = document.createElement("div");
621612 table.setAttribute("id", "textBoxTable");
622613 table.style.cssText = "width:100%;";
623614
624615 //fill table
625 - if(self.proofreadpage_default_layout=='horizontal')
 616+ if(self.proofreadpage_default_layout=='horizontal')
626617 pr_fill_table(true);
627618 else
628619 pr_fill_table(false);
@@ -747,7 +738,7 @@
748739
749740 function pr_init() {
750741
751 - if( document.getElementById("ImageContainer")) return;
 742+ if( document.getElementById("pr_container")) return;
752743
753744 if(document.URL.indexOf("action=protect") > 0 || document.URL.indexOf("action=unprotect") > 0) return;
754745 if(document.URL.indexOf("action=delete") > 0 || document.URL.indexOf("action=undelete") > 0) return;
@@ -805,7 +796,7 @@
806797 if(self.pr_horiz)
807798 document.getElementById("wpTextbox1").style.cssText = "height:"+self.vertHeight+"px";
808799 else
809 - document.getElementById("wpTextbox1").style.cssText = "height:"+(self.TextBoxHeight-7)+"px";
 800+ document.getElementById("wpTextbox1").style.cssText = "height:"+(self.vertHeight-7)+"px";
810801 pr_zoom(0);
811802 }
812803 else proofreadPageZoom();

Status & tagging log