1 33 34 package com.icesoft.faces.context.effects; 35 36 37 import com.icesoft.faces.application.D2DViewHandler; 38 import com.icesoft.faces.application.StartupTime; 39 import com.icesoft.faces.context.BridgeFacesContext; 40 41 import javax.faces.component.UIComponent; 42 import javax.faces.context.ExternalContext; 43 import javax.faces.context.FacesContext; 44 import java.util.ArrayList ; 45 import java.util.Collections ; 46 import java.util.HashMap ; 47 import java.util.Iterator ; 48 import java.util.List ; 49 import java.util.Map ; 50 import java.util.Random ; 51 52 56 public class JavascriptContext { 57 private static final Random RANDOM = new Random (); 58 59 62 private static final String REQUEST_KEY = 63 "icesoft_javascript_request_key_7698193"; 64 67 private static final String EFFECTS_REQUEST_KEY = 68 "icesoft_javascript_effects_request_key_9072451"; 69 72 private static final String FOCUS_COMP_KEY = 73 "icesoft_javascript_focus_comp"; 74 77 private static final String FOCUS_APP_KEY = "icesoft_javascript_focus_app"; 78 81 public static final String LIB_KEY = 82 "icesoft_javascript_required_libs_897241"; 83 86 public static final String DYNAMIC_CODE_ID = "dynamic-code"; 87 88 91 public static final String ICE_BRIDGE = "/xmlhttp" + StartupTime.getStartupInc() + "icefaces-d2d.js"; 92 93 96 public static final String ICE_EXTRAS = "/xmlhttp" + StartupTime.getStartupInc() + "ice-extras.js"; 97 98 106 public static void includeLib(String libname, FacesContext facesContext) { 107 if (facesContext == null) 108 return; 109 ExternalContext externalContext = facesContext.getExternalContext(); 110 if (externalContext == null) 111 return; 112 Map sessionMap = externalContext.getSessionMap(); 113 if (sessionMap == null) 114 return; 115 List libs = (List ) sessionMap.get(LIB_KEY); 116 if (libs == null) { 117 libs = new ArrayList (); 118 sessionMap.put(LIB_KEY, libs); 119 } 120 if (!libs.contains(libname)) 121 libs.add(libname); 122 } 123 124 130 public static String [] getIncludedLibs(FacesContext facesContext) { 131 List libs = (List ) facesContext.getExternalContext().getSessionMap() 132 .get(LIB_KEY); 133 String [] result = new String [0]; 134 if (libs != null) { 135 result = new String [libs.size()]; 136 libs.toArray(result); 137 } 138 return result; 139 } 140 141 155 public static void addJavascriptCall(FacesContext facesContext, 156 String call) { 157 Map map = facesContext.getExternalContext().getRequestMap(); 158 addJavascriptCall(map, call); 159 } 160 161 166 private static void addJavascriptCall(Map map, String call) { 167 String currentValue = (String ) map.get(REQUEST_KEY); 168 if (currentValue == null) { 169 map.put(REQUEST_KEY, call); 170 } else { 171 map.put(REQUEST_KEY, currentValue + call); 172 } 173 } 174 175 180 public static String getJavascriptCalls(FacesContext facesContext) { 181 Map map = facesContext.getExternalContext().getRequestMap(); 182 return getJavascriptCalls(map); 183 } 184 185 190 public static String getJavascriptCalls(Map map) { 191 addtEffectJavascriptCalls(map); 192 String code = (String ) map.get(REQUEST_KEY); 193 map.put(REQUEST_KEY, ""); 194 String focus = getFocusCall(map); 195 code = replaceDupes( 196 code == null || "".equals(code) ? focus : code + ';' + focus); 197 if ("".equals(code)) { 200 return ""; 201 } else { 202 return "var c = function(){" + code + 203 "};if(window.application){c();}else{window.onLoad(c)};" 204 + randomComment(); 205 } 206 } 207 208 213 private static String getFocusCall(Map map) { 214 String focus = (String ) map.get(FOCUS_APP_KEY); 215 if (focus == null) { 216 focus = (String ) map.get(FOCUS_COMP_KEY); 217 } 218 if (focus == null) { 219 return ""; 220 } else { 221 map.remove(FOCUS_APP_KEY); 222 map.remove(FOCUS_COMP_KEY); 223 return "Ice.Focus.setFocus('" + focus + "');"; 224 } 225 } 226 227 228 237 public static String applyEffect(Effect effect, String id, 238 FacesContext context) { 239 242 UIComponent uiComponent = D2DViewHandler.findComponent(id, context.getViewRoot()); 243 if (uiComponent != null) { 244 id = uiComponent.getClientId(context); 245 } 246 247 String name = genFunctionName(id, effect); 248 249 String call = "window['" + name + "'] = function (){" + 250 "id = '" + id + "';" + effect.toString() + "};"; 251 252 addJavascriptCall(context, call); 253 return name + "();"; 254 } 255 256 263 public static void fireEffect(Effect effect, String id, 264 FacesContext context) { 265 if (effect == null || effect.isFired()) return; 266 effect.setFired(true); 267 Object viewRoot = context.getViewRoot(); 268 try{ 269 270 271 UIComponent uiComponent = D2DViewHandler.findComponent(id, context.getViewRoot()); 272 if (uiComponent != null) { 273 id = uiComponent.getClientId(context); 274 } 275 }catch(Exception e){ 276 286 System.err.println("View Root is [" + viewRoot.getClass().getName() + "]"); 287 e.printStackTrace(); 288 } 289 effect.setId(id); 290 addEffect(effect, context); 291 292 } 293 294 300 public static void fireEffect(Effect effect, UIComponent component) { 301 if (effect == null || effect.isFired()) return; 302 FacesContext facesContext = FacesContext.getCurrentInstance(); 303 fireEffect(effect, component, facesContext); 304 } 305 306 313 public static void fireEffect(Effect effect, UIComponent component, 314 FacesContext facesContext) { 315 if (effect == null || effect.isFired()) return; 316 String id = component.getClientId(facesContext); 317 fireEffect(effect, id, facesContext); 318 } 319 320 326 327 public static void fireEffect(UIComponent uiComponent, 328 FacesContext facesContext) { 329 Effect effect = (Effect) uiComponent.getAttributes().get("effect"); 330 if (effect != null) 331 fireEffect(effect, uiComponent, facesContext); 332 } 333 334 342 public static String getEffectFunctionForEvent(UIComponent uiComponent, 343 String event, String id, 344 FacesContext facesContext) { 345 String result = null; 346 Effect fx = getEffectForEvent(uiComponent, event); 347 if (fx != null) { 348 result = applyEffect(fx, id, facesContext); 349 } 350 return result; 351 } 352 353 359 private static Effect getEffectForEvent(UIComponent uiComponent, 360 String event) { 361 Effect result = null; 362 Object o = uiComponent.getAttributes().get(event); 363 if (o != null && o instanceof Effect) { 364 result = (Effect) o; 365 } 366 return result; 367 } 368 369 370 377 private static String genFunctionName(String id, Effect effect) { 378 StringBuffer sb = new StringBuffer ("iceEffect"); 379 char[] ca = id.toCharArray(); 380 for (int i = 0; i < ca.length; i++) { 385 int c = (int) ca[i]; 386 if ((c > 64 && c < 91) || (c > 96 && c < 123) || (c > 47 && c < 58)) 387 { 388 sb.append(ca[i]); 389 } else { 390 sb.append('_'); 391 } 392 } 393 sb.append(effect.hashCode()); 394 return sb.toString(); 395 } 396 397 402 private static String replaceDupes(String s) { 403 if (s.indexOf(";;") == -1) return s; 404 s = s.replaceAll(";;", ";"); 405 return s; 406 } 407 408 414 public static void focus(FacesContext context, String id) { 415 if (!id.equals("")) { 417 Map map = context.getExternalContext().getRequestMap(); 418 map.put(FOCUS_COMP_KEY, id); 419 } 420 } 421 422 427 public static void applicationFocus(FacesContext facesContext, String id) { 428 if (!id.equals("")) { 429 430 Map map = facesContext.getExternalContext().getRequestMap(); 431 map.put(FOCUS_APP_KEY, id); 432 } 433 } 434 435 440 public static String getFocus(FacesContext context) { 441 String focusId = ((BridgeFacesContext) context).getFocusId(); 442 Map map = context.getExternalContext().getRequestMap(); 443 if (map.containsKey(FOCUS_COMP_KEY)) 444 focusId = (String ) map.get(FOCUS_COMP_KEY); 445 if (map.containsKey(FOCUS_APP_KEY)) 446 focusId = (String ) map.get(FOCUS_APP_KEY); 447 return focusId; 448 } 449 450 455 private static void addEffect(Effect effect, FacesContext facesContext) { 456 Map map = facesContext.getExternalContext().getRequestMap(); 457 List list = (ArrayList ) map.get(EFFECTS_REQUEST_KEY); 458 if (list == null) { 459 list = new ArrayList (); 460 map.put(EFFECTS_REQUEST_KEY, list); 461 } 462 list.add(effect); 463 } 464 465 469 private static void addtEffectJavascriptCalls(Map map) { 470 471 List list = (ArrayList ) map.get(EFFECTS_REQUEST_KEY); 472 if (list == null) { 473 return; 474 } 475 Map sequencedEffects = new HashMap (); 476 Iterator iter = list.iterator(); 477 while (iter.hasNext()) { 478 Effect effect = (Effect) iter.next(); 479 if (effect.getSequence() == null && 480 !(effect instanceof EffectQueue)) { 481 String call = 482 "id = '" + effect.getId() + "';" + effect.toString(); 483 addJavascriptCall(map, call); 484 } else { 485 486 extractEffectSequence(effect, sequencedEffects); 487 } 488 } 489 490 if (sequencedEffects.size() > 0) { 491 iter = sequencedEffects.values().iterator(); 492 int sequence = 0; 493 while (iter.hasNext()) { 494 list = (List ) iter.next(); 495 Collections.sort(list, new EffectComparator()); 496 String call = buildSequenceEffectCall(list, sequence); 497 addJavascriptCall(map, call); 498 sequence++; 499 } 500 } 501 map.remove(EFFECTS_REQUEST_KEY); 502 } 503 504 510 private static String buildSequenceEffectCall(List list, int sequence) { 511 StringBuffer sb = new StringBuffer (); 512 Iterator effectIter = list.iterator(); 513 int effect = 0; 514 String lastCall = null; 515 while (effectIter.hasNext()) { 516 Effect fx = (Effect) effectIter.next(); 517 String var = getVariableName(sequence, effect); 518 lastCall = "var " + var + " = '" + fx.getId() + "';" + 519 fx.toString(var, lastCall); 520 effect++; 521 } 522 return lastCall; 523 } 524 525 530 private static void extractEffectSequence(Effect effect, 531 Map sequencedEffects) { 532 String seqName = effect.getSequence(); 533 534 List seq = (List ) sequencedEffects.get(seqName); 535 if (seq == null) { 536 seq = new ArrayList (); 537 sequencedEffects.put(seqName, seq); 538 } 539 if (effect instanceof EffectQueue) { 540 List eqList = ((EffectQueue) effect).getEffects(); 541 Iterator eqIter = eqList.iterator(); 542 int seqId = 0; 543 while (eqIter.hasNext()) { 544 Effect fx = (Effect) eqIter.next(); 545 fx.setSequence(seqName); 546 fx.setSequenceId(seqId); 547 fx.setId(effect.getId()); 548 seq.add(fx); 549 seqId++; 550 551 } 552 } else { 553 seq.add(effect); 554 } 555 } 556 557 563 private static String getVariableName(int sequence, int effect) { 564 return "s" + sequence + "e" + effect; 565 } 566 567 571 private static String randomComment() { 572 return "//" + RANDOM.nextInt(); 573 } 574 } 575 | Popular Tags |