001: //** Copyright Statement ***************************************************
002: //The Salmon Open Framework for Internet Applications (SOFIA)
003: //Copyright (C) 1999 - 2002, Salmon LLC
004: //
005: //This program is free software; you can redistribute it and/or
006: //modify it under the terms of the GNU General Public License version 2
007: //as published by the Free Software Foundation;
008: //
009: //This program is distributed in the hope that it will be useful,
010: //but WITHOUT ANY WARRANTY; without even the implied warranty of
011: //MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
012: //GNU General Public License for more details.
013: //
014: //You should have received a copy of the GNU General Public License
015: //along with this program; if not, write to the Free Software
016: //Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
017: //
018: //For more information please visit http://www.salmonllc.com
019: //** End Copyright Statement ***************************************************
020: package com.salmonllc.html;
021:
022: import com.salmonllc.jsp.Constants;
023: import com.salmonllc.jsp.JspDataTable;
024:
025: /**
026: * A utility class that can be used by other components to generate various
027: * useful javascripts. Currently handles various scripts for popup windows
028: */
029: public class HtmlScriptGenerator {
030:
031: HtmlPage _page;
032:
033: public HtmlScriptGenerator(HtmlPage page) {
034: _page = page;
035: }
036:
037: public String generatePopupDivScript(String url, int popupWidth,
038: int popupHeight, HtmlComponent relativeComponent) {
039: return generatePopupDivScript(url,
040: Constants.POPUP_POSITION_RELATIVE, -1, -1, popupWidth,
041: popupHeight, false, null, "no", relativeComponent, -1);
042: }
043:
044: public String generatePopupDivScript(String url,
045: String popupPosition, int popupTop, int popupLeft,
046: int popupWidth, int popupHeight, boolean useModal,
047: String borderStyle, String scrolling,
048: HtmlComponent relativeComponent, int row) {
049:
050: if (!_page.isScriptAdded("RelativeDivPositionScript"))
051: _page.addScript("RelativeDivPositionScript",
052: generateRelativeDivPositionScript());
053:
054: if (!_page.isScriptAdded("DivPositionScript"))
055: _page.addScript("DivPositionScript",
056: generateDivPositionScripts());
057:
058: if (!_page.isScriptAdded("OpenDivScript"))
059: _page.addScript("OpenDivScript", generateOpenDivScript());
060:
061: if (!_page.isHtmlAdded("PopupDiv"))
062: _page.addHtml("PopupDiv", generatePopupDivHtml());
063:
064: StringBuffer openPopupScript = new StringBuffer();
065: String tBodyId = null;
066: if (relativeComponent != null) {
067: openPopupScript.append("relativeComp=");
068: openPopupScript.append(relativeComponent.getFormString());
069: openPopupScript.append(relativeComponent.getFullName());
070: openPopupScript.append(((row == -1) ? "" : ("_" + row)));
071: openPopupScript.append(";");
072: HtmlComponent parent = relativeComponent.getParent();
073: while (parent != null) {
074: if (parent instanceof JspDataTable
075: || parent instanceof HtmlDataTable) {
076: tBodyId = parent.getFullName() + "-tbody";
077: break;
078: }
079: parent = parent.getParent();
080: }
081: }
082: openPopupScript.append("OpenPopupDiv("
083: + (relativeComponent != null ? "relativeComp" : "''")
084: + ","
085: + url
086: + ",'"
087: + popupPosition
088: + "'"
089: + ","
090: + popupTop
091: + ","
092: + popupLeft
093: + ","
094: + popupWidth
095: + ","
096: + popupHeight
097: + ","
098: + useModal
099: + ","
100: + (borderStyle == null ? "null" : "'" + borderStyle
101: + "'") + ","
102: + (scrolling == null ? "null" : "'" + scrolling + "'")
103: + ",'" + tBodyId + "');");
104: return openPopupScript.toString();
105: }
106:
107: private String generateOpenDivScript() {
108: StringBuffer openPopupScript;
109: openPopupScript = new StringBuffer();
110: openPopupScript.append(generateCheckModalScript());
111: openPopupScript.append("var windowPopupDiv=null;\n\n");
112: openPopupScript
113: .append("function OpenPopupDiv(relativeComp,url,position,popupTop,popupLeft,popupWidth,popupHeight,modal, borderStyle, scrolling, tBodyId){\n");
114: openPopupScript
115: .append(" windowPopupDiv = document.getElementById(\"pseudoWindow\");\n");
116: openPopupScript.append(" if (position=='"
117: + Constants.POPUP_POSITION_RELATIVE + "') {\n");
118: openPopupScript
119: .append(" popupLeft=getDivRelativeLeftPosition(relativeComp,window,popupWidth,popupHeight); \n");
120: openPopupScript
121: .append(" popupTop=getDivRelativeTopPosition(relativeComp,window,popupWidth,popupHeight,tBodyId); \n");
122: openPopupScript.append(" } else if (position =='"
123: + Constants.POPUP_POSITION_CENTER + "') {\n");
124: openPopupScript
125: .append(" popupLeft=getDivCenterLeftPosition(popupWidth,popupHeight);\n ");
126: openPopupScript
127: .append(" popupTop=getDivCenterTopPosition(popupWidth,popupHeight);\n ");
128: openPopupScript.append("}\n");
129: openPopupScript
130: .append(" if (windowPopupDiv.style.display==\"none\") {\n");
131: openPopupScript.append(" if (!modal) {\n");
132: openPopupScript
133: .append(" nonModalDiv=windowPopupDiv;\n");
134: openPopupScript.append(" modalDiv=null;\n");
135: openPopupScript.append(" }\n");
136: openPopupScript.append(" else {\n");
137: openPopupScript.append(" nonModalDiv=null;\n");
138: openPopupScript.append(" modalDiv=windowPopupDiv;\n");
139: openPopupScript.append(" }\n");
140: openPopupScript
141: .append(" var contentFrameScroll=document.getElementById(\"contentFrameScroll\");\n");
142: openPopupScript
143: .append(" var contentFrameNoScroll=document.getElementById(\"contentFrameNoScroll\");\n");
144: openPopupScript
145: .append(" var contentFrame=contentFrameNoScroll;\n");
146: openPopupScript
147: .append(" contentFrameScroll.style.display=\"none\";\n");
148: openPopupScript
149: .append(" contentFrameNoScroll.style.display=\"none\";\n");
150: openPopupScript
151: .append(" if (scrolling!=null && scrolling=='yes' || scrolling=='auto') contentFrame=contentFrameScroll;\n");
152: openPopupScript.append(" contentFrame.src=url;\n");
153: openPopupScript
154: .append(" contentFrame.width=popupWidth;\n");
155: openPopupScript
156: .append(" contentFrame.height=popupHeight;\n");
157: openPopupScript
158: .append(" windowPopupDiv.style.height=popupHeight;\n");
159: openPopupScript
160: .append(" windowPopupDiv.style.left=popupLeft;\n");
161: openPopupScript
162: .append(" windowPopupDiv.style.width=popupWidth;\n");
163: openPopupScript
164: .append(" windowPopupDiv.style.top=popupTop;\n");
165: openPopupScript
166: .append(" if (borderStyle != null) windowPopupDiv.style.border=borderStyle;\n");
167: openPopupScript
168: .append(" windowPopupDiv.style.display=\"block\";\n");
169: openPopupScript
170: .append(" contentFrame.style.display=\"block\";\n");
171: openPopupScript.append(" }\n");
172: openPopupScript.append("}\n\n");
173:
174: openPopupScript.append(" function ClosePopupDiv() {");
175: openPopupScript
176: .append(" windowPopupDiv = document.getElementById(\"pseudoWindow\");\n");
177: openPopupScript
178: .append(" var contentFrame1=document.getElementById(\"contentFrameScroll\");\n");
179: openPopupScript
180: .append(" var contentFrame2=document.getElementById(\"contentFrameNoScroll\");\n");
181: openPopupScript.append(" contentFrame1.src=\"\";\n");
182: openPopupScript.append(" contentFrame2.src=\"\";\n");
183: openPopupScript
184: .append(" windowPopupDiv.style.display=\"none\";\n");
185: openPopupScript.append("}\n\n");
186:
187: return openPopupScript.toString();
188: }
189:
190: private String generatePopupDivHtml() {
191: StringBuffer sb = new StringBuffer();
192:
193: sb
194: .append("<div id=\"pseudoWindow\" style=\"position:absolute; top:100px; left:100px; width:100px; height:100px; border:1px solid black; background-color:#ffffff; display:none;\">");
195: sb
196: .append("<iframe id=\"contentFrameNoScroll\" src=\"\" frameborder=\"0\" vspace=\"0\" hspace=\"0\" marginwidth=\"0\" marginHeight=\"0\" scrolling=\"no\"></iframe>");
197: sb
198: .append("<iframe id=\"contentFrameScroll\" src=\"\" frameborder=\"0\" vspace=\"0\" hspace=\"0\" marginwidth=\"0\" marginHeight=\"0\" scrolling=\"auto\"></iframe>");
199: sb.append("</div>");
200: return sb.toString();
201: }
202:
203: /**
204: * @see this.generatePopupScript
205: */
206: public String generateOpenPopupScript(String url, int popupWidth,
207: int popupHeight, HtmlComponent relativeComponent,
208: boolean useModal) {
209: return generateOpenPopupScript(url,
210: Constants.POPUP_POSITION_RELATIVE, -1, -1, popupWidth,
211: popupHeight, useModal, null, relativeComponent, -1);
212: }
213:
214: /**
215: * @see this.generatePopupScript
216: */
217: public String generateOpenPopupScript(String url, int popupWidth,
218: int popupHeight, boolean useModal) {
219: return generateOpenPopupScript(url,
220: Constants.POPUP_POSITION_CENTER, -1, -1, popupWidth,
221: popupHeight, useModal, null, null, -1);
222: }
223:
224: /**
225: * Generates the necessary javascript to open a popup window for the given
226: * URL.
227: *
228: * @param url
229: * url for the popup.
230: * @param popupPosition
231: * position in which the popup will be opened (relative, center
232: * or custom).
233: * @param popupTop
234: * top coordinate for the popup (Applies only to custom
235: * positions).
236: * @param popupLeft
237: * left coordinate for the popup (Applies only to custom
238: * positions).
239: * @param popupWidth
240: * popup width
241: * @param popupHeight
242: * popup heigth
243: * @param useModal
244: * indicates if the popup should be opened in a modal or non
245: * modal fashion.
246: * @param relativeComponent
247: * relative component to position the popup (must be provided for
248: * the relative position).
249: * @param row
250: * row number of the relative component (-1 if it isn't nested
251: * inside a datatable).
252: * @param popupAttibutes
253: * other then size and position, a comma seperated list of popup
254: * attributes (see javascript window.open() for more info)
255: * @author Claudio Pi - claudiopi@users.sourceforge.net
256: * @return
257: */
258: public String generateOpenPopupScript(String url,
259: String popupPosition, int popupTop, int popupLeft,
260: int popupWidth, int popupHeight, boolean useModal,
261: String popupAttibutes, HtmlComponent relativeComponent,
262: int row) {
263:
264: StringBuffer openPopupScript = new StringBuffer();
265:
266: if (!_page.isScriptAdded("OpenPopupScript"))
267: _page.addScript("OpenPopupScript",
268: generateOpenPopupScript());
269:
270: if (!_page.isScriptAdded("RelativePositionScript"))
271: _page.addScript("RelativePositionScript",
272: generateRelativePositionScript());
273:
274: if (!_page.isScriptAdded("CenterPositionScript"))
275: _page.addScript("CenterPositionScript",
276: generateCenterPositionScript());
277:
278: if (relativeComponent != null) {
279: openPopupScript.append("relativeComp=");
280: openPopupScript.append(relativeComponent.getFormString());
281: openPopupScript.append(relativeComponent.getFullName());
282: openPopupScript.append(((row == -1) ? "" : ("_" + row)));
283: openPopupScript.append(";");
284: }
285: openPopupScript.append("OpenPopupWindow("
286: + (relativeComponent != null ? "relativeComp" : "''")
287: + ","
288: + url
289: + ",'"
290: + popupPosition
291: + "'"
292: + ","
293: + popupTop
294: + ","
295: + popupLeft
296: + ","
297: + popupWidth
298: + ","
299: + popupHeight
300: + ","
301: + useModal
302: + ","
303: + (popupAttibutes == null ? "null" : "'"
304: + popupAttibutes + "'") + ");");
305:
306: return openPopupScript.toString();
307: }
308:
309: private String generateOpenPopupScript() {
310: StringBuffer openPopupScript;
311: openPopupScript = new StringBuffer();
312:
313: openPopupScript.append(generateCheckModalScript());
314: openPopupScript.append("function ");
315: openPopupScript
316: .append("OpenPopupWindow(relativeComp,url,position,popupTop,popupLeft,popupWidth,popupHeight,modal, popupAtt){\n");
317: openPopupScript.append(" if (position=='"
318: + Constants.POPUP_POSITION_RELATIVE + "') {\n");
319: openPopupScript
320: .append(" popupLeft=getPopupRelativeLeftPosition(relativeComp,window,popupWidth,popupHeight); \n");
321: openPopupScript
322: .append(" popupTop=getPopupRelativeTopPosition(relativeComp,window,popupWidth,popupHeight); \n");
323: openPopupScript.append(" } else if (position =='"
324: + Constants.POPUP_POSITION_CENTER + "') {\n");
325: openPopupScript
326: .append(" popupLeft=getPopupCenterLeftPosition(popupWidth,popupHeight);\n ");
327: openPopupScript
328: .append(" popupTop=getPopupCenterTopPosition(popupWidth,popupHeight);\n ");
329: openPopupScript.append("}\n");
330: openPopupScript
331: .append(" var windname = \"ModPopWin\" + (new Date()).getSeconds().toString();\n");
332: openPopupScript
333: .append(" var attrib = 'status=yes,menubar=no,resizable,dependent=yes,scrollbars=yes,titlebar=no';\n");
334: openPopupScript.append(" if (popupAtt != null)\n");
335: openPopupScript.append(" attrib=popupAtt;\n");
336:
337: openPopupScript.append("attrib+=',width=' + popupWidth + '");
338: openPopupScript.append(",height=' + popupHeight + '");
339: openPopupScript.append(",left=' + popupLeft + '");
340: openPopupScript.append(",top=' + popupTop;\n");
341:
342: openPopupScript.append(" if (modal) {\n");
343: openPopupScript
344: .append(" // Author: Danny Goodman (http://www.dannyg.com)\n");
345: openPopupScript
346: .append(" if ((!modalPopup) || (!modalPopup.win) || (modalPopup.win && modalPopup.win.closed)) {\n");
347: openPopupScript.append(" modalPopup=new Object();\n");
348: openPopupScript
349: .append(" modalPopup.win=window.open(url, windname, attrib);\n");
350: openPopupScript
351: .append(" if (modalPopup.win) modalPopup.win.focus();\n");
352: openPopupScript.append(" } else {\n ");
353: openPopupScript.append(" modalPopup.win.focus();\n");
354: openPopupScript.append(" }\n ");
355: openPopupScript.append(" } else {\n");
356: openPopupScript
357: .append(" windhandle1=window.open(url,windname,attrib); \n");
358: openPopupScript.append(" windhandle1.focus();\n");
359: openPopupScript.append(" }\n");
360: openPopupScript.append("}\n\n");
361:
362: return openPopupScript.toString();
363: }
364:
365: private String generateCheckModalScript() {
366: StringBuffer script = new StringBuffer();
367: script.append("var windhandle1=null;\n\n");
368: script.append("var modalPopup = null;\n");
369: script.append("var nonModalDiv=null;\n");
370: script.append("var modalDiv=null;\n");
371:
372: script.append("function checkModal() {\n");
373: script
374: .append(" if (nonModalDiv) {\n ClosePopupDiv(); nonModalDiv=null; }\n");
375: script
376: .append(" if (windhandle1) {\n windhandle1.close(); windhandle1=null; }\n");
377: script
378: .append(" if (modalPopup || modalDiv) { setTimeout(\"finishChecking()\", 50);}"
379: + "\n");
380: script.append(" return true;\n");
381: script.append("}\n\n");
382:
383: script.append("function finishChecking() {\n");
384: script
385: .append(" if (modalPopup && modalPopup.win && !modalPopup.win.closed) \n");
386: script.append(" modalPopup.win.focus()\n");
387: script
388: .append(" else if (modalDiv && modalDiv.style.display==\"block\")");
389: script.append(" modalDiv.focus();\n");
390: script.append("}\n\n");
391:
392: _page.writeScript("document.onclick=checkModal;");
393: if (_page.getBrowserType() == HtmlPage.BROWSER_MICROSOFT)
394: _page.writeScript("document.onclick=checkModal;");
395: return script.toString();
396: }
397:
398: private String generateRelativePositionScript() {
399:
400: StringBuffer relativePositionScript = new StringBuffer();
401: relativePositionScript
402: .append("function getPopupRelativeLeftPosition(relativeComp,relativeCompWindow,currentHeight,currentWidth) {\n");
403: relativePositionScript.append(" newLeft = 0;\n");
404: relativePositionScript
405: .append(" if (isNaN(relativeCompWindow.screenLeft)) {\n");
406: relativePositionScript
407: .append(" newLeft=relativeCompWindow.screenX +(relativeCompWindow.outerWidth-relativeCompWindow.innerWidth)-relativeCompWindow.pageXOffset;\n");
408: relativePositionScript.append(" } else {\n");
409: relativePositionScript
410: .append(" newLeft=relativeCompWindow.screenLeft;\n");
411: relativePositionScript.append(" }\n");
412: relativePositionScript
413: .append(" if (relativeComp.offsetParent){\n");
414: relativePositionScript
415: .append(" while (relativeComp.offsetParent) {\n");
416: relativePositionScript
417: .append(" newLeft += relativeComp.offsetLeft;\n");
418: relativePositionScript
419: .append(" relativeComp = relativeComp.offsetParent;\n");
420: relativePositionScript.append(" }\n");
421: relativePositionScript.append(" }\n");
422: relativePositionScript
423: .append(" if ((newLeft + currentWidth) > screen.availWidth)\n");
424: relativePositionScript
425: .append(" newLeft -= ((newLeft + currentWidth + 100) - screen.availWidth);\n");
426: relativePositionScript.append(" return newLeft;\n");
427: relativePositionScript.append("}\n\n");
428:
429: relativePositionScript
430: .append("function getPopupRelativeTopPosition(relativeComp,relativeCompWindow,currentHeight,currentWidth) {\n");
431: relativePositionScript.append(" newTop = 0;\n");
432: relativePositionScript
433: .append(" newTopAbove = relativeCompWindow.screenTop - currentHeight;\n");
434: relativePositionScript
435: .append(" if (isNaN(relativeCompWindow.screenLeft)) {\n");
436: relativePositionScript
437: .append(" newTop=relativeCompWindow.screenY + (relativeCompWindow.outerHeight-24-relativeCompWindow.innerHeight)-relativeCompWindow.pageYOffset + 26;\n");
438: relativePositionScript
439: .append(" newTopAbove = newTop - currentHeight;\n");
440: relativePositionScript.append(" } else {\n");
441: relativePositionScript
442: .append(" newTop=relativeCompWindow.screenTop + relativeComp.height + 26;\n");
443: relativePositionScript.append(" }\n");
444: relativePositionScript
445: .append(" if (relativeComp.offsetParent){\n");
446: relativePositionScript
447: .append(" while (relativeComp.offsetParent) {\n");
448: relativePositionScript
449: .append(" newTop += relativeComp.offsetTop;\n");
450: relativePositionScript
451: .append(" newTopAbove += relativeComp.offsetTop;\n");
452: relativePositionScript
453: .append(" relativeComp = relativeComp.offsetParent;\n");
454: relativePositionScript.append(" }\n");
455: relativePositionScript.append(" }\n");
456: relativePositionScript
457: .append(" if ((newTop + currentHeight) > screen.availHeight)\n");
458: relativePositionScript.append(" newTop = newTopAbove;\t");
459: relativePositionScript.append(" return newTop;\n");
460: relativePositionScript.append("}\n\n");
461:
462: return relativePositionScript.toString();
463:
464: }
465:
466: private String generateRelativeDivPositionScript() {
467:
468: StringBuffer relativePositionScript = new StringBuffer();
469: relativePositionScript
470: .append("function getDivRelativeLeftPosition(relativeComp,relativeCompWindow,currentWidth,currentHeight) {\n");
471: relativePositionScript.append(" newLeft = 0;\n");
472: relativePositionScript
473: .append(" if (relativeComp.offsetParent){\n");
474: relativePositionScript
475: .append(" while (relativeComp.offsetParent) {\n");
476: relativePositionScript
477: .append(" newLeft += relativeComp.offsetLeft;\n");
478: relativePositionScript
479: .append(" relativeComp = relativeComp.offsetParent;\n");
480: relativePositionScript.append(" }\n");
481: relativePositionScript.append(" }\n");
482: relativePositionScript
483: .append(" return checkOffScreenX(newLeft,currentWidth);\n");
484: relativePositionScript.append("}\n\n");
485:
486: relativePositionScript
487: .append("function getDivRelativeTopPosition(relativeComp,relativeCompWindow,currentWidth,currentHeight,tBodyId) {\n");
488:
489: relativePositionScript.append(" newTop = 0;\n");
490: relativePositionScript
491: .append(" if (relativeComp.offsetParent){\n");
492: relativePositionScript
493: .append(" while (relativeComp.offsetParent) {\n");
494: relativePositionScript
495: .append(" newTop += relativeComp.offsetTop;\n");
496: relativePositionScript
497: .append(" newTop -= relativeComp.scrollTop;\n");
498: relativePositionScript
499: .append(" relativeComp = relativeComp.offsetParent;\n");
500: relativePositionScript.append(" }\n");
501: relativePositionScript.append(" }\n");
502: relativePositionScript.append(" newTop += 28;\n");
503: relativePositionScript.append(" if (tBodyId) {\n");
504: relativePositionScript
505: .append(" var tb=document.getElementById(tBodyId);\n");
506: relativePositionScript
507: .append(" if(tb) newTop-=tb.scrollTop;\n");
508: relativePositionScript.append(" }\n");
509: relativePositionScript
510: .append(" return checkOffScreenY(newTop,currentHeight);\n");
511: relativePositionScript.append("}\n\n");
512:
513: relativePositionScript
514: .append("function checkOffScreenY(newTop, newHeight) {\n");
515: relativePositionScript
516: .append(" var scrollY=getScrollXY()[1];\n");
517: relativePositionScript
518: .append(" var winHeight=window.innerHeight ? window.innerHeight : document.body.offsetHeight;\n");
519: relativePositionScript
520: .append(" var newBottom = newTop + newHeight - scrollY;\n");
521: relativePositionScript
522: .append(" if (newBottom > winHeight) \n");
523: relativePositionScript
524: .append(" newTop=newTop -(newHeight+32);\n");
525: relativePositionScript.append(" if (newTop < scrollY) \n");
526: relativePositionScript.append(" newTop=scrollY;\n");
527: relativePositionScript.append(" return newTop;\n");
528: relativePositionScript.append("}\n\n");
529:
530: relativePositionScript
531: .append("function checkOffScreenX(newLeft, newWidth) {\n");
532: relativePositionScript
533: .append(" var scrollX=getScrollXY()[0];\n");
534: relativePositionScript
535: .append(" var winWidth=window.innerWidth ? window.innerWidth : document.body.offsetWidth;\n");
536: relativePositionScript
537: .append(" var newEnd = newLeft + newWidth - scrollX;\n");
538: relativePositionScript
539: .append(" if ((newEnd+20) > winWidth) \n");
540: relativePositionScript
541: .append(" newLeft=(winWidth-newWidth)-20;\n");
542: relativePositionScript.append(" if (newLeft < scrollX) \n");
543: relativePositionScript.append(" newLeft=scrollX;\n");
544: relativePositionScript.append(" return newLeft;\n");
545: relativePositionScript.append("}\n\n");
546: return relativePositionScript.toString();
547:
548: }
549:
550: private String generateCenterPositionScript() {
551:
552: StringBuffer centerPositionScript = new StringBuffer();
553:
554: centerPositionScript
555: .append("function getPopupCenterTopPosition(currentWidth,currentHeight) {\n");
556: centerPositionScript
557: .append(" return (screen.height - currentHeight) / 2;\n");
558: centerPositionScript.append("}\n\n");
559: centerPositionScript
560: .append("function getPopupCenterLeftPosition(currentWidth,currentHeight) {\n");
561: centerPositionScript
562: .append(" return (screen.width - currentWidth) / 2;\n");
563: centerPositionScript.append("}\n\n");
564:
565: return centerPositionScript.toString();
566:
567: }
568:
569: private String generateDivPositionScripts() {
570: StringBuffer centerPositionScript = new StringBuffer();
571: centerPositionScript.append("function getScrollXY() {\n");
572: centerPositionScript
573: .append(" var scrOfX = 0, scrOfY = 0;\n");
574: centerPositionScript
575: .append(" if( typeof( window.pageYOffset ) == 'number' ) {\n");
576: centerPositionScript
577: .append(" scrOfY = window.pageYOffset;\n");
578: centerPositionScript
579: .append(" scrOfX = window.pageXOffset;\n");
580: centerPositionScript
581: .append(" } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {\n");
582: centerPositionScript
583: .append(" scrOfY = document.body.scrollTop;\n");
584: centerPositionScript
585: .append(" scrOfX = document.body.scrollLeft;\n");
586: centerPositionScript
587: .append(" } else if( document.documentElement &&\n");
588: centerPositionScript
589: .append(" ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {\n");
590: centerPositionScript
591: .append(" scrOfY = document.documentElement.scrollTop;\n");
592: centerPositionScript
593: .append(" scrOfX = document.documentElement.scrollLeft;\n");
594: centerPositionScript.append(" }\n");
595: centerPositionScript.append(" return [ scrOfX, scrOfY ];\n");
596: centerPositionScript.append(" } \n");
597:
598: centerPositionScript.append("function getWindowSize() {\n");
599: centerPositionScript
600: .append(" var myWidth = 0, myHeight = 0;\n");
601: centerPositionScript
602: .append(" if( typeof( window.innerWidth ) == 'number' ) {\n");
603: centerPositionScript
604: .append(" myWidth = window.innerWidth;\n");
605: centerPositionScript
606: .append(" myHeight = window.innerHeight;\n");
607: centerPositionScript
608: .append(" } else if( document.documentElement &&\n");
609: centerPositionScript
610: .append(" ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {\n");
611: centerPositionScript
612: .append(" myWidth = document.documentElement.clientWidth;\n");
613: centerPositionScript
614: .append(" myHeight = document.documentElement.clientHeight;\n");
615: centerPositionScript
616: .append(" } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {\n");
617: centerPositionScript
618: .append(" myWidth = document.body.clientWidth;\n");
619: centerPositionScript
620: .append(" myHeight = document.body.clientHeight;\n");
621: centerPositionScript.append(" }\n");
622: centerPositionScript.append(" return [myWidth,myHeight];\n");
623: centerPositionScript.append(" }\n");
624:
625: centerPositionScript
626: .append("function getDivCenterTopPosition(currentWidth,currentHeight) {\n");
627: centerPositionScript.append(" var y=getScrollXY()[1];\n");
628: centerPositionScript
629: .append(" var height=window.innerHeight ? window.innerHeight : document.body.offsetHeight;\n");
630: centerPositionScript
631: .append(" return ((height-currentHeight)/2) + y;\n");
632: centerPositionScript.append("}\n\n");
633: centerPositionScript
634: .append("function getDivCenterLeftPosition(currentWidth,currentHeight) {\n");
635: centerPositionScript.append(" var x=getScrollXY()[0];\n");
636: centerPositionScript
637: .append(" var width=window.innerWidth ? window.innerWidth : document.body.offsetWidth;\n");
638: centerPositionScript
639: .append(" return ((width-currentWidth)/2) + x;\n");
640: centerPositionScript.append("}\n\n");
641:
642: return centerPositionScript.toString();
643:
644: }
645:
646: /**
647: * @see this.generateRelativePopupPositionScript()
648: */
649: public String generateRelativePopupPositionScript(
650: HtmlComponent relativeComponent) {
651: return generateRelativePopupPositionScript(relativeComponent,
652: -1);
653: }
654:
655: /**
656: * Generates the necessary javascript to move a popup window to a position
657: * relative to other component of the opener window.
658: *
659: * @param relativeComponent
660: * relative component of the opener window to position the popup.
661: * @param row
662: * row number of the relative component (-1 if it isn't nested
663: * inside a datatable).
664: * @author Claudio Pi - claudiopi@users.sourceforge.net
665: * @return
666: */
667:
668: public String generateRelativePopupPositionScript(
669: HtmlComponent relativeComponent, int row) {
670:
671: String result = null;
672: if (relativeComponent != null) {
673: StringBuffer relativePopupPositionScript = new StringBuffer();
674:
675: if (!_page.isScriptAdded("RelativePositionScript")) {
676: _page.addScript("RelativePositionScript",
677: generateRelativePositionScript());
678: }
679:
680: relativePopupPositionScript.append("\n\nrelativeComp=");
681: relativePopupPositionScript.append("top.opener.");
682: relativePopupPositionScript.append(relativeComponent
683: .getFormString());
684: relativePopupPositionScript.append(relativeComponent
685: .getFullName());
686: relativePopupPositionScript.append(((row == -1) ? ""
687: : ("_" + row)));
688: relativePopupPositionScript.append(";\n");
689: relativePopupPositionScript
690: .append("var popupLeft=getPopupRelativeLeftPosition(relativeComp,top.opener.window,popupHeight,popupWidth);\n");
691: relativePopupPositionScript
692: .append("var popupTop=getPopupRelativeTopPosition(relativeComp,top.opener.window,popupHeight,popupWidth);\n");
693: relativePopupPositionScript
694: .append("var popupWidth=window.outerWidth;\n");
695: relativePopupPositionScript
696: .append("var popupHeight=window.outerHeight;\n");
697: relativePopupPositionScript.append("\n");
698: relativePopupPositionScript
699: .append("window.moveTo(popupLeft,popupTop);");
700:
701: result = relativePopupPositionScript.toString();
702:
703: }
704:
705: return result;
706:
707: }
708:
709: /**
710: * Generates the necessary javascript to return a key and a description to
711: * the lookup component that called this page.
712: *
713: * @param key
714: * the key to return to the edit field of the lookup.
715: * @param description
716: * the description to return to the lookup.
717: * @author Claudio Pi - claudiopi@users.sourceforge.net
718: * @return the resulting javascript.
719: */
720: public String generateReturnValueToLookupScript(String key,
721: String description) {
722: HtmlLookUpComponent lookup = _page.getPopupLookupComponent();
723: int rowNum = _page.getPopupLookupComponentRow();
724: String editField = lookup.getEditFieldFullName(rowNum);
725: String descField = lookup.getHiddenDescrFieldFullName(rowNum);
726:
727: if (lookup.getEditDescription()) {
728: editField = lookup.getHiddenKeyFieldFullName(rowNum);
729: descField = lookup.getEditFieldFullName(rowNum);
730: }
731:
732: if (!lookup.getUseDiv()) {
733: return ("if (top.opener && !top.opener.closed) {"
734: + "top.opener."
735: + editField
736: + ".value='"
737: + key
738: + "';"
739: + "top.opener."
740: + descField
741: + ".value='"
742: + description
743: + "';"
744: + "var theSpan=top.opener.document.getElementById('"
745: + _page.getPopupLookupComponent().getDivFullName(
746: rowNum) + "');"
747: + "if (theSpan) theSpan.innerHTML='" + description
748: + "';" + "window.close();}");
749: } else {
750: return ("parent."
751: + editField
752: + ".value='"
753: + key
754: + "';"
755: + "parent."
756: + descField
757: + ".value='"
758: + description
759: + "';"
760: + "var theSpan=parent.document.getElementById('"
761: + _page.getPopupLookupComponent().getDivFullName(
762: rowNum) + "');"
763: + "if (theSpan) theSpan.innerHTML='" + description
764: + "';" + "parent.ClosePopupDiv();");
765: }
766: }
767:
768: /**
769: * Generates the necessary javascript to close a popup lookup
770: *
771: * @return the resulting javascript.
772: */
773: public String generateCancelLookupScript() {
774: HtmlLookUpComponent lookup = _page.getPopupLookupComponent();
775: if (!lookup.getUseDiv()) {
776: return ("if (top.opener && !top.opener.closed) window.close();");
777: } else {
778: return "parent.ClosePopupDiv();";
779: }
780: }
781:
782: /**
783: * Generates a script for selecting rows in a datatable
784: */
785: public void generateSelectRowScript() {
786: if (!_page.isScriptAdded("DataTableSelectRowScript")) {
787: StringBuffer sb = new StringBuffer();
788: sb.append("document.onselectstart = doSelect;\n");
789: sb.append("var doSelect=true;\n");
790: sb.append("var anchorRow=-1;\n");
791: sb.append("function doSelect(){\n");
792: sb.append("return doSelect;\n");
793: sb.append("}\n");
794: sb.append("\n");
795: sb
796: .append("function selectRows(pageForm,prefix, row, rowStart, rowEnd, event, trueValue, falseValue, selectedColor, selectOne) {\n");
797: sb
798: .append(" if ((! selectOne) && shiftPressed(event) && anchorRow != -1) {\n");
799: sb.append(" for (var i=rowStart; i <=rowEnd; i++)\n");
800: sb
801: .append(" selectRow(pageForm,prefix,i,false, trueValue, falseValue, selectedColor);\n");
802: sb.append(" var first=row;\n");
803: sb.append(" var last=anchorRow;\n");
804: sb.append(" if (first > last) {\n");
805: sb.append(" first=anchorRow;\n");
806: sb.append(" last=row;\n");
807: sb.append(" }\n");
808: sb.append(" for (var i=first; i <=last; i++)\n");
809: sb
810: .append(" selectRow(pageForm,prefix,i,true, trueValue, falseValue, selectedColor);\n");
811: sb.append(" }\n");
812: sb
813: .append(" else if ((!selectOne) && ctrlPressed(event)) {\n");
814: sb.append(" anchorRow=-1;\n");
815: sb
816: .append(" var hdn=pageForm.elements[prefix + '_select_' + row];\n");
817: sb.append(" if (hdn.value==trueValue)\n");
818: sb
819: .append(" selectRow(pageForm,prefix,row,false, trueValue, falseValue, selectedColor);\n");
820: sb.append(" else\n");
821: sb
822: .append(" selectRow(pageForm,prefix,row,true, trueValue, falseValue, selectedColor);\n");
823: sb.append(" }\n");
824: sb.append(" else {\n");
825: sb.append(" for (var i=rowStart; i <=rowEnd; i++)\n");
826: sb
827: .append(" selectRow(pageForm,prefix,i,false, trueValue, falseValue, selectedColor);\n");
828: sb
829: .append(" selectRow(pageForm,prefix,row,true, trueValue, falseValue, selectedColor);\n");
830: sb.append(" anchorRow=row;\n");
831: sb.append(" }\n");
832: sb.append(" return false;\n");
833: sb.append("}\n");
834: sb
835: .append(" function selectRow(pageForm,prefix,row,select, trueValue, falseValue, selectedColor) {\n");
836: sb
837: .append(" var hdn=pageForm.elements[prefix + '_select_' + row];\n");
838: sb.append(" if (hdn.value == trueValue && select)\n");
839: sb.append(" return;\n");
840: sb
841: .append(" if (hdn.value == falseValue && !select)\n");
842: sb.append(" return;\n");
843: sb.append(" var rowIn=findParentTR(hdn);\n");
844: sb
845: .append(" var newColor=pageForm.elements[prefix + 'r0_color_' + row].value;\n");
846: sb.append(" if (! select) \n");
847: sb.append(" hdn.value=falseValue;\n");
848: sb.append(" else {\n");
849: sb.append(" hdn.value=trueValue;\n");
850: sb.append(" newColor=selectedColor;\n");
851: sb.append(" }\n");
852: sb.append(" rowNo=0;\n");
853: sb.append(" while (rowIn != null) {\n");
854: sb.append(" rowNo++;\n");
855: sb.append(" rowIn.bgColor=newColor;\n");
856: sb.append(" var cells=rowIn.cells;\n");
857: sb.append(" for (var i=0;i<cells.length;i++)\n");
858: sb.append(" cells[i].bgColor=newColor;\n");
859: sb
860: .append(" hdn=pageForm.elements[prefix + 'r' + rowNo + '_color_' + row];\n");
861: sb.append(" if (hdn == null)\n");
862: sb.append(" rowIn=null;\n");
863: sb.append(" else\n");
864: sb.append(" rowIn=findParentTR(hdn);\n");
865: sb.append(" }\n");
866: sb.append(" }\n");
867: sb.append(" function findParentTR(hdn) {\n");
868: sb.append(" if (hdn==null)\n");
869: sb.append(" return null;\n");
870: sb.append(" var parentNode=hdn.parentNode;\n");
871: sb
872: .append(" while(parentNode != null && parentNode.tagName != 'TR')\n");
873: sb.append(" parentNode=parentNode.parentNode;\n");
874: sb.append(" return parentNode;\n");
875: sb.append("}\n");
876: sb.append(" function ctrlPressed(e) {\n");
877: sb.append(" var nav4 = window.Event ? true : false;\n");
878: sb.append(" if (nav4) \n");
879: sb.append(" return e.ctrlKey;\n");
880: sb.append(" else \n");
881: sb.append(" return window.event.ctrlKey;\n");
882: sb.append(" }\n");
883: sb.append(" function shiftPressed(e) {\n");
884: sb
885: .append(" var nav4 = window.Event ? true : false;\n");
886: sb.append(" if (nav4) \n");
887: sb.append(" return e.shiftKey;\n");
888: sb.append(" else \n");
889: sb.append(" return window.event.shiftKey;\n");
890: sb.append(" }\n");
891: _page.addScript("DataTableSelectRowScript", sb.toString());
892: }
893:
894: }
895:
896: }
|