User:Dpv/beta popups.js

// This is currently a holding tank for popup improvements. // There are no functional improvements yet. // // GOALS: * Limit vertical position so that popups on links at the bottom //           of the screen stay in the screen. //       * When editing, make the fixing of redirects and and of dab //           links operate in the existing edit window. //       * When editing, find and fix all redirects; automate finding //           dabs

mw.loader.load(            'https://en.wikipedia.org/w/index.php?title=User:Lupin/popups.js'             + '&action=raw&ctype=text/javascript&dontcountme=s');

/* Bad news. This won't work just as an add-on. Probably need to subclass, etc. function checkPopupPosition { // stop the popup running off the right of the screen pg.current.link.navpopup.limitHorizontalPosition; pg.current.link.navpopup.limitVerticalPosition; };

Navpopup.prototype.reposition= function (x,y, noLimitHor) { log ('reposition('+x+','+y+','+noLimitHor+')'); if (typeof x != 'undefined' && x!==null) { this.left=x; } if (typeof y != 'undefined' && y!==null) { this.top=y; } if (typeof this.left != 'undefined' && typeof this.top != 'undefined') { this.mainDiv.style.left=this.left + 'px'; this.mainDiv.style.top=this.top + 'px'; } if (!noLimitHor) { this.limitHorizontalPosition; this.limitVerticalPosition; } };

/**  Copy of limitHorizontalPosition. It won't work like this, but I get annoyed when most of a pop-up is off the screen. @private Navpopup.prototype.limitVerticalPosition=function { if (!this.constrained || this.tooHigh) { return; } var y=parseInt(this.mainDiv.style.top, 10); var w=parseInt(this.mainDiv.offsetHeight, 10); var cHeight=document.body.clientHeight; log('limitVerticalPosition: y='+y+     ', this.top=' + this.top +      ', this.height=' + this.height +      ', cHeight=' + cHeight); if ( (y+w) >= cHeight ||      ( y > 0 && this.maxHeight && this.height < this.maxHeight && && y > cHeight - this.maxHeight ) ) { // This is a very nasty hack. There has to be a better way! // We find the "natural" width of the div by positioning it at the far left // then reset it so that it should be flush right (well, nearly) this.mainDiv.style.top='-10000px'; this.mainDiv.style.height = this.maxHeight + 'px'; var naturalHeight=parseInt(this.mainDiv.offsetHeight, 10); var newTop=cHeight - naturalHeight - 1; if (newTop < 0) { newTop = 0; this.tooHigh=true; } // still unstable for really wide popups? log ('limitVerticalPosition: moving to ('+ this.left + ','+ newTop +');' +	 ' naturalHeight=' + naturalHeight + ', clientHeight=' + cHeight); this.reposition(null, newTop, true); } };