001: package com.salmonllc.jsp;
002:
003: import com.salmonllc.html.HtmlPage;
004: import com.salmonllc.html.HtmlComponent;
005:
006: /**
007: * This tag creates an expand component
008: * @author Nicolás Genta
009: */
010: public class JspExpand extends HtmlComponent {
011:
012: private String _div;
013: private String _imgexp;
014: private String _imgcont;
015: private String _altexp;
016: private String _altcont;
017: private boolean _keepstate;
018:
019: public JspExpand(String name, String div, String imgexp,
020: String imgcont, HtmlPage p) {
021: super (name, p);
022: setTheme(null);
023: _div = div;
024: _imgexp = imgexp;
025: _imgcont = imgcont;
026: _altexp = null;
027: _altcont = null;
028: _keepstate = true;
029: }
030:
031: public void generateHTML(java.io.PrintWriter p, int rowNo)
032: throws Exception {
033: if (!getVisible())
034: return;
035:
036: String name = getFullName();
037: if (rowNo != -1)
038: name += "_" + rowNo;
039: String altExp = ((_altexp == null) ? "" : _altexp);
040: String altCont = ((_altcont == null) ? "" : _altcont);
041: String src = translateSiteMapURL(_imgcont);
042: String alt = altCont;
043: String onClick = "";
044: String onLoad = "";
045:
046: JspController controller = (JspController) getPage();
047: HtmlComponent comp = controller.getComponent(_div);
048: if (comp != null && comp instanceof JspDiv) {
049: JspDiv div = (JspDiv) comp;
050: onLoad = "init('" + div.getFullName() + "','"
051: + getFullName() + "','"
052: + translateSiteMapURL(_imgexp) + "','"
053: + translateSiteMapURL(_imgcont) + "','" + altExp
054: + "','" + altCont + "'," + _keepstate + ");";
055: if (div.isHidden()) {
056: src = translateSiteMapURL(_imgexp);
057: alt = altExp;
058: }
059: onClick = "expand('" + div.getFullName() + "'," + rowNo
060: + ");";
061: }
062:
063: StringBuffer sbOut = new StringBuffer("<IMG NAME=\"" + name
064: + "\"" + " ID=\"" + name + "\"" + " SRC=\"" + src
065: + "\"" + " ALT=\"" + alt + "\"" + " ONCLICK=\""
066: + onClick + "\"/>");
067: p.print(sbOut.toString());
068: addScript(_keepstate);
069:
070: String currentOnLoad = controller.getOnLoad();
071: if (currentOnLoad != null) {
072: if (!JspExpand.isSubString(onLoad, currentOnLoad)) {
073: controller.setOnLoad(currentOnLoad + "\n" + onLoad);
074: }
075: } else {
076: controller.setOnLoad(onLoad);
077: }
078: }
079:
080: public String getDiv() {
081: return _div;
082: }
083:
084: public void setDiv(String div) {
085: this ._div = div;
086: }
087:
088: public String getImgexp() {
089: return _imgexp;
090: }
091:
092: public void setImgexp(String imgexp) {
093: this ._imgexp = imgexp;
094: }
095:
096: public String getImgcont() {
097: return _imgcont;
098: }
099:
100: public void setImgcont(String imgcont) {
101: this ._imgcont = imgcont;
102: }
103:
104: public String getAltexp() {
105: return _altexp;
106: }
107:
108: public void setAltexp(String altexp) {
109: this ._altexp = altexp;
110: }
111:
112: public String getAltcont() {
113: return _altcont;
114: }
115:
116: public void setAltcont(String altcont) {
117: this ._altcont = altcont;
118: }
119:
120: public boolean isKeepstate() {
121: return _keepstate;
122: }
123:
124: public void setKeepstate(boolean keepstate) {
125: this ._keepstate = keepstate;
126: }
127:
128: private void addScript(boolean keepState) {
129: if (!getPage().isScriptAdded("ExpandScript")) {
130: StringBuffer jsExpand = new StringBuffer();
131:
132: jsExpand
133: .append("var tagsData = new Array();\n"
134: + "var tagsState = new Array();\n"
135: + "\n"
136: + "function TagData(divId, imgId, srcExp, srcCont, altExp, altCont, keepState) {\n"
137: + "\tthis.divId = divId;\n"
138: + "\tthis.imgId = imgId;\n"
139: + "\tthis.srcExp = srcExp;\n"
140: + "\tthis.srcCont = srcCont;\n"
141: + "\tthis.altExp = altExp;\n"
142: + "\tthis.altCont = altCont;\n"
143: + "\tthis.keepState = keepState;\n"
144: + "}\n"
145: + "\n"
146: + "function TagState(divId, expanded, contracted) {\n"
147: + "\tthis.divId = divId;\n"
148: + "\tthis.expanded = expanded;\n"
149: + "\tthis.contracted = contracted;\n"
150: + "}\n\n");
151: jsExpand.append("function find(array, divId) {\n"
152: + "\tvar i = 0;\n" + "\tvar found = false;\n"
153: + "\twhile (i<array.length && !found) {\n"
154: + "\t\tif (array[i].divId == divId) {\n"
155: + "\t\t\tfound = true;\n" + "\t\t} else {\n"
156: + "\t\t\ti++;\n" + "\t\t}\n" + "\t}\n"
157: + "\tif (found) {\n" + "\t\treturn array[i];\n"
158: + "\t} else {\n" + "\t\treturn null;\n" + "\t}\n"
159: + "}\n\n");
160: jsExpand
161: .append("function init(divId, imgId, srcExp, srcCont, altExp, altCont, keepState) {\n"
162: + "\tvar tagData = find(tagsData, divId);\n"
163: + "\tif (tagData == null) {\n"
164: + "\t\ttagData = new TagData(divId, imgId, srcExp, srcCont, altExp, altCont, keepState);\n"
165: + "\t\ttagsData.push(tagData);\n"
166: + "\t}\n"
167: + "\tif (keepState) {\n"
168: + "\t\tload(divId);\n" + "\t}\n" + "}\n\n");
169: jsExpand
170: .append("function expand(divId, row) {\n"
171: + "\tvar tagData = find(tagsData, divId);\n"
172: + "\tvar imgId = tagData.imgId;\n"
173: + "\tvar srcExp = tagData.srcExp;\n"
174: + "\tvar srcCont = tagData.srcCont;\n"
175: + "\tvar altExp = tagData.altExp;\n"
176: + "\tvar altCont = tagData.altCont;\n"
177: + "\tvar keepState = tagData.keepState;\n"
178: + "\tif (row == -1) {\n"
179: + "\t\tvar img = document.getElementById(imgId);\n"
180: + "\t\tvar div = document.getElementById(divId);\n"
181: + "\t}\n"
182: + "\telse {\n"
183: + "\t\tvar img = document.getElementById(imgId + \"_\" + row);\n"
184: + "\t\tvar div = document.getElementById(divId + \"_\" + row);\n"
185: + "\t}\n"
186: + "\tif (keepState) {\n"
187: + "\t\tchangeState(divId, row, div);\n"
188: + "\t}\n"
189: + "\tif (div.style.display == \"none\") {\n"
190: + "\t\tshow(img, div, srcCont, altCont);\n"
191: + "\t} else {\n"
192: + "\t\thide(img, div, srcExp, altExp);\n"
193: + "\t}\n" + "}\n\n");
194: jsExpand.append("function show(img, div, src, alt) {\n"
195: + "\tdiv.style.display = \"block\";\n"
196: + "\timg.src = src;\n" + "\timg.alt = alt;\n"
197: + "}\n" + "\n"
198: + "function hide(img, div, src, alt) {\n"
199: + "\tdiv.style.display = \"none\";\n"
200: + "\timg.src = src;\n" + "\timg.alt = alt;\n"
201: + "}\n\n");
202:
203: getPage().addScript("ExpandScript", jsExpand.toString());
204: }
205:
206: if (keepState && !getPage().isScriptAdded("KeepStateScript")) {
207: StringBuffer jsKeepState = new StringBuffer();
208:
209: jsKeepState.append("function remove(array, row) {\n"
210: + "\tvar i = 0;\n" + "\tvar found = false;\n"
211: + "\twhile (i<array.length && !found) {\n"
212: + "\t\tif (row == array[i]) {\n"
213: + "\t\t\tfound = true;\n" + "\t\t} else {\n"
214: + "\t\t\ti++;\n" + "\t\t}\n" + "\t}\n"
215: + "\tif (found) {\n" + "\t\tarray.splice(i,1);\n"
216: + "\t}\n" + "}\n\n");
217: jsKeepState
218: .append("function changeState(divId, row, div) {\n"
219: + "\tvar tagState = find(tagsState, divId);\n"
220: + "\tif (tagState == null) {\n"
221: + "\t\texpanded = new Array();\n"
222: + "\t\tcontracted = new Array();\n"
223: + "\t\ttagState = new TagState(divId, expanded, contracted);\n"
224: + "\t\ttagsState.push(tagState);\n"
225: + "\t} else {\n"
226: + "\t\texpanded = tagState.expanded;\n"
227: + "\t\tcontracted = tagState.contracted;\n"
228: + "\t}\n"
229: + "\tif (div.style.display == \"none\") {\n"
230: + "\t\texpanded.push(row);\n"
231: + "\t\tremove(contracted, row);\n"
232: + "\t} else {\n"
233: + "\t\tcontracted.push(row);\n"
234: + "\t\tremove(expanded, row);\n" + "\t}\n"
235: + "\tsave();\n" + "}\n\n");
236: jsKeepState
237: .append("function save() {\n"
238: + "\tsetCookie(\"ExpandTag\", array2String(tagsState));\n"
239: + "}\n"
240: + "\n"
241: + "function load(divId) {\n"
242: + "\tvar cookie = getCookie(\"ExpandTag\");\n"
243: + "\tif (cookie != null) {\n"
244: + "\t\ttagsState = string2Array(cookie);\n"
245: + "\t\tvar tagState = find(tagsState, divId);\n"
246: + "\t\tif (tagState != null) {\n"
247: + "\t\t\tvar expanded = tagState.expanded;\n"
248: + "\t\t\tvar contracted = tagState.contracted;\n"
249: + "\t\t\tvar tagData = find(tagsData, divId);\n"
250: + "\t\t\tvar imgId = tagData.imgId;\n"
251: + "\t\t\tvar divId = tagData.divId;\n"
252: + "\t\t\tvar srcExp = tagData.srcExp;\n"
253: + "\t\t\tvar srcCont = tagData.srcCont;\n"
254: + "\t\t\tvar altExp = tagData.altExp;\n"
255: + "\t\t\tvar altCont = tagData.altCont;\n"
256: + "\t\t\tfor (var i=0; i<expanded.length; i++) {\n"
257: + "\t\t\t\tvar row = expanded[i];\n"
258: + "\t\t\t\tif (row == -1) {\n"
259: + "\t\t\t\t\tvar img = document.getElementById(imgId);\n"
260: + "\t\t\t\t\tvar div = document.getElementById(divId);\n"
261: + "\t\t\t\t}\n"
262: + "\t\t\t\telse {\n"
263: + "\t\t\t\t\tvar img = document.getElementById(imgId + \"_\" + row);\n"
264: + "\t\t\t\t\tvar div = document.getElementById(divId + \"_\" + row);\n"
265: + "\t\t\t\t}\n"
266: + "\t\t\t\tif (div != null) {\n"
267: + "\t\t\t\t\tshow(img, div, srcCont, altCont);\n"
268: + "\t\t\t\t}\n"
269: + "\t\t\t}\n"
270: + "\t\t\tfor (var j=0; j<contracted.length; j++) {\n"
271: + "\t\t\t\tvar row = contracted[j];\n"
272: + "\t\t\t\tif (row == -1) {\n"
273: + "\t\t\t\t\tvar img = document.getElementById(imgId);\n"
274: + "\t\t\t\t\tvar div = document.getElementById(divId);\n"
275: + "\t\t\t\t}\n"
276: + "\t\t\t\telse {\n"
277: + "\t\t\t\t\tvar img = document.getElementById(imgId + \"_\" + row);\n"
278: + "\t\t\t\t\tvar div = document.getElementById(divId + \"_\" + row);\n"
279: + "\t\t\t\t}\n"
280: + "\t\t\t\tif (div != null) {\n"
281: + "\t\t\t\t\thide(img, div, srcExp, altExp);\n"
282: + "\t\t\t\t}\n" + "\t\t\t}\n" + "\t\t}\n"
283: + "\t}\n" + "}\n\n");
284: jsKeepState
285: .append("function getCookieVal(offset) {\n"
286: + "\tvar endstr = document.cookie.indexOf (\";\", offset);\n"
287: + "\tif (endstr == -1) {\n"
288: + "\t\tendstr = document.cookie.length;\n"
289: + "\t}\n"
290: + "\treturn unescape(document.cookie.substring(offset, endstr));\n"
291: + "}\n"
292: + "\n"
293: + "function getCookie(name) {\n"
294: + "\tvar arg = name + \"=\";\n"
295: + "\tvar alen = arg.length;\n"
296: + "\tvar clen = document.cookie.length;\n"
297: + "\tvar i = 0;\n"
298: + "\twhile (i < clen) {\n"
299: + "\t\tvar j = i + alen;\n"
300: + "\t\tif (document.cookie.substring(i, j) == arg) {\n"
301: + "\t\t\treturn getCookieVal(j);\n"
302: + "\t\t}\n"
303: + "\t\ti = document.cookie.indexOf(\" \", i) + 1;\n"
304: + "\t\tif (i == 0) break; \n"
305: + "\t}\n"
306: + "\treturn null;\n"
307: + "}\n"
308: + "\n"
309: + "function setCookie(name, value, expires, path, domain, secure) {\n"
310: + "\tdocument.cookie = name + \"=\" + escape (value) +\n"
311: + "\t\t((expires) ? \"; expires=\" + expires : \"\") +\n"
312: + "\t\t((path) ? \"; path=\" + path : \"\") +\n"
313: + "\t\t((domain) ? \"; domain=\" + domain : \"\") +\n"
314: + "\t\t((secure) ? \"; secure\" : \"\");\n"
315: + "}\n\n");
316: jsKeepState
317: .append("function object2String(obj) {\n"
318: + "\tvar val, output = \"\";\n"
319: + "\tif (obj.length ==0) {\n"
320: + "\t\treturn \"[]\";\n"
321: + "\t}\n"
322: + "\tif (obj) { \n"
323: + "\t\toutput += \"{\";\n"
324: + "\t\tfor (var i in obj) {\n"
325: + "\t\t\tval = obj[i];\n"
326: + "\t\t\tswitch (typeof val) {\n"
327: + "\t\t\t\tcase (\"object\"):\n"
328: + "\t\t\t\t\tif (val[0]!=null) {\n"
329: + "\t\t\t\t\t\toutput += i + \":\" + array2String(val) + \",\";\n"
330: + "\t\t\t\t\t} else {\n"
331: + "\t\t\t\t\t\toutput += i + \":\" + object2String(val) + \",\";\n"
332: + "\t\t\t\t\t}\n"
333: + "\t\t\t\t\tbreak;\n"
334: + "\t\t\t\tcase (\"string\"):\n"
335: + "\t\t\t\t\toutput += i + \":'\" + escape(val) + \"',\";\n"
336: + "\t\t\t\t\tbreak;\n"
337: + "\t\t\t\tdefault:\n"
338: + "\t\t\t\t\toutput += i + \":\" + val + \",\";\n"
339: + "\t\t\t}\n"
340: + "\t\t}\n"
341: + "\t\toutput = output.substring(0, output.length-1) + \"}\";\n"
342: + "\t}\n" + "\treturn output;\n" + "}\n"
343: + "\n"
344: + "function string2Object(string) {\n"
345: + "\teval(\"var result = \" + string);\n"
346: + "\treturn result;\n" + "}\n\n");
347: jsKeepState
348: .append("function array2String(array) {\n"
349: + "\tvar output = \"\";\n"
350: + "\tif (array.length ==0) {\n"
351: + "\t\treturn \"[]\";\n"
352: + "\t}\n"
353: + "\tif (array) {\n"
354: + "\t\toutput += \"[\";\n"
355: + "\t\tfor (var i in array) {\n"
356: + "\t\tval = array[i];\n"
357: + "\t\t\tswitch (typeof val) {\n"
358: + "\t\t\t\tcase (\"object\"):\n"
359: + "\t\t\t\t\tif (val[0]!=null) {\n"
360: + "\t\t\t\t\t\toutput += array2String(val) + \",\";\n"
361: + "\t\t\t\t\t} else {\n"
362: + "\t\t\t\t\t\toutput += object2String(val) + \",\";\n"
363: + "\t\t\t\t\t}\n"
364: + "\t\t\t\t\tbreak;\n"
365: + "\t\t\t\tcase (\"string\"):\n"
366: + "\t\t\t\t\toutput += \"'\" + escape(val) + \"',\";\n"
367: + "\t\t\t\t\tbreak;\n"
368: + "\t\t\t\tdefault:\n"
369: + "\t\t\t\t\toutput += val + \",\";\n"
370: + "\t\t\t}\n"
371: + "\t\t}\n"
372: + "\t\toutput = output.substring(0, output.length-1) + \"]\";\n"
373: + "\t}\n" + "\treturn output;\n" + "}\n"
374: + "\n"
375: + "function string2Array(string) {\n"
376: + "\teval(\"var result = \" + string);\n"
377: + "\treturn result;\n" + "}\n\n");
378:
379: getPage().addScript("KeepStateScript",
380: jsKeepState.toString());
381: }
382: }
383:
384: public static boolean isSubString(String sub, String str) {
385: for (int j = 0; j < str.length(); j++) {
386: if (str.regionMatches(j, sub, 0, sub.length())) {
387: return true;
388: }
389: }
390: return false;
391: }
392: }
|