1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to you under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18 /** 19 *MyFaces core javascripting libraries 20 * 21 * Those are the central public API functions in the JSF2 22 * Ajax API! They handle the entire form submit and ajax send 23 * and resolve cycle! 24 */ 25 26 /** 27 * reserve the root namespace 28 */ 29 if ('undefined' != typeof OpenAjax && ('undefined' == typeof jsf || null == typeof jsf)) { 30 OpenAjax.hub.registerLibrary("jsf", "www.sun.com", "1.0", null); 31 } 32 //just in case openajax has failed (testing environment) 33 /** 34 * @ignore 35 */ 36 if (!window.jsf) { 37 /** 38 * @namespace jsf 39 */ 40 var jsf = new function() { 41 /* 42 * Version of the implementation for the jsf.js. 43 * <p /> 44 * as specified within the jsf specifications jsf.html: 45 * <ul> 46 * <li>left two digits major release number</li> 47 * <li>middle two digits minor spec release number</li> 48 * <li>right two digits bug release number</li> 49 * </ul> 50 * @constant 51 */ 52 this.specversion = 200000; 53 /** 54 * Implementation version as specified within the jsf specification. 55 * <p /> 56 * A number increased with every implementation version 57 * and reset by moving to a new spec release number 58 * 59 * @constant 60 */ 61 this.implversion = 6; 62 63 /** 64 * This method is responsible for the return of a given project stage as defined 65 * by the jsf specification. 66 * <p/> 67 * Valid return values are: 68 * <ul> 69 * <li>"Production"</li> 70 * <li>"Development"</li> 71 * <li>"SystemTest"</li> 72 * <li>"UnitTest"</li> 73 * </li> 74 * 75 * @return {String} the current project state emitted by the server side method: 76 * <i>javax.faces.application.Application.getProjectStage()</i> 77 */ 78 this.getProjectStage = function() { 79 var impl = myfaces._impl.core._Runtime.getGlobalConfig("jsfAjaxImpl", myfaces._impl.core.Impl); 80 return impl.getProjectStage(); 81 }; 82 83 /** 84 * collect and encode data for a given form element (must be of type form) 85 * find the javax.faces.ViewState element and encode its value as well! 86 * return a concatenated string of the encoded values! 87 * 88 * @throws an exception in case of the given element not being of type form! 89 * https://issues.apache.org/jira/browse/MYFACES-2110 90 */ 91 this.getViewState = function(formElement) { 92 /*we are not allowed to add the impl on a global scope so we have to inline the code*/ 93 var impl = myfaces._impl.core._Runtime.getGlobalConfig("jsfAjaxImpl", myfaces._impl.core.Impl); 94 return impl.getViewState(formElement); 95 }; 96 }; 97 //jsdoc helper to avoid warnings, we map later 98 window.jsf = jsf; 99 } 100 101 /** 102 * just to make sure no questions arise, I simply prefer here a weak 103 * typeless comparison just in case some frameworks try to interfere 104 * by overriding null or fiddeling around with undefined or typeof in some ways 105 * it is safer in this case than the standard way of doing a strong comparison 106 **/ 107 if (!jsf.ajax) { 108 /** 109 * @namespace jsf.ajax 110 */ 111 jsf.ajax = new function() { 112 113 114 /** 115 * this function has to send the ajax requests 116 * 117 * following request conditions must be met: 118 * <ul> 119 * <li> the request must be sent asynchronously! </li> 120 * <li> the request must be a POST!!! request </li> 121 * <li> the request url must be the form action attribute </li> 122 * <li> all requests must be queued with a client side request queue to ensure the request ordering!</li> 123 * </ul> 124 * 125 * @param {String|Node} element: any dom element no matter being it html or jsf, from which the event is emitted 126 * @param {EVENT} event: any javascript event supported by that object 127 * @param {Map} options : map of options being pushed into the ajax cycle 128 */ 129 this.request = function(element, event, options) { 130 if (!options) { 131 options = {}; 132 } 133 /*we are not allowed to add the impl on a global scope so we have to inline the code*/ 134 var impl = myfaces._impl.core._Runtime.getGlobalConfig("jsfAjaxImpl", myfaces._impl.core.Impl); 135 return impl.request(element, event, options); 136 }; 137 138 /** 139 * Adds an error handler to our global error queue. 140 * the error handler must be of the format <i>function errorListener(<errorData>)</i> 141 * with errorData being of following format: 142 * <ul> 143 * <li> errorData.type : "error"</li> 144 * <li> errorData.status : the error status message</li> 145 * <li> errorData.serverErrorName : the server error name in case of a server error</li> 146 * <li> errorData.serverErrorMessage : the server error message in case of a server error</li> 147 * <li> errorData.source : the issuing source element which triggered the request </li> 148 * <li> eventData.responseCode: the response code (aka http request response code, 401 etc...) </li> 149 * <li> eventData.responseText: the request response text </li> 150 * <li> eventData.responseXML: the request response xml </li> 151 * </ul> 152 * 153 * @param {function} errorListener error handler must be of the format <i>function errorListener(<errorData>)</i> 154 */ 155 this.addOnError = function(/*function*/errorListener) { 156 var impl = myfaces._impl.core._Runtime.getGlobalConfig("jsfAjaxImpl", myfaces._impl.core.Impl); 157 return impl.addOnError(errorListener); 158 }; 159 160 /** 161 * Adds a global event listener to the ajax event queue. The event listener must be a function 162 * of following format: <i>function eventListener(<eventData>)</i> 163 * 164 * @param {function} eventListener event must be of the format <i>function eventListener(<eventData>)</i> 165 */ 166 this.addOnEvent = function(/*function*/eventListener) { 167 var impl = myfaces._impl.core._Runtime.getGlobalConfig("jsfAjaxImpl", myfaces._impl.core.Impl); 168 return impl.addOnEvent(eventListener); 169 }; 170 171 /** 172 * processes the ajax response if the ajax request completes successfully 173 * @param request the ajax request! 174 * @param context the ajax context! 175 */ 176 this.response = function(/*xhr request object*/request, context) { 177 var impl = myfaces._impl.core._Runtime.getGlobalConfig("jsfAjaxImpl", myfaces._impl.core.Impl); 178 return impl.response(request, context); 179 }; 180 } 181 } 182 183 if (!jsf.util) { 184 /** 185 * @namespace jsf.util 186 */ 187 jsf.util = new function() { 188 189 /** 190 * varargs function which executes a chain of code (functions or any other code) 191 * 192 * if any of the code returns false, the execution 193 * is terminated prematurely skipping the rest of the code! 194 * 195 * @param {DomNode} source, the callee object 196 * @param {Event} event, the event object of the callee event triggering this function 197 * 198 */ 199 this.chain = function(source, event) { 200 var impl = myfaces._impl.core._Runtime.getGlobalConfig("jsfAjaxImpl", myfaces._impl.core.Impl); 201 return impl.chain.apply(impl, arguments); 202 }; 203 } 204 } 205 206 207