1 /* Licensed to the Apache Software Foundation (ASF) under one or more
  2  * contributor license agreements.  See the NOTICE file distributed with
  3  * this work for additional information regarding copyright ownership.
  4  * The ASF licenses this file to you under the Apache License, Version 2.0
  5  * (the "License"); you may not use this file except in compliance with
  6  * the License.  You may obtain a copy of the License at
  7  *
  8  *      http://www.apache.org/licenses/LICENSE-2.0
  9  *
 10  * Unless required by applicable law or agreed to in writing, software
 11  * distributed under the License is distributed on an "AS IS" BASIS,
 12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 13  * See the License for the specific language governing permissions and
 14  * limitations under the License.
 15  */
 16 /**
 17  * @class
 18  * @name _AjaxUtils
 19  * @memberOf myfaces._impl.xhrCore
 20  * @description
 21  *
 22  * A set of helper routines which are utilized within our Ajax subsystem and nowhere else
 23  *
 24  * TODO move this into a singleton, the current structure is
 25  * still a j4fry legacy we need to get rid of it in the long run
 26  */
 27 _MF_SINGLTN(_PFX_XHR+"_AjaxUtils", _MF_OBJECT,
 28 /** @lends myfaces._impl.xhrCore._AjaxUtils.prototype */
 29 {
 30 
 31 
 32     /**
 33      * determines fields to submit
 34      * @param {Object} targetBuf - the target form buffer receiving the data
 35      * @param {Node} parentItem - form element item is nested in
 36      * @param {Array} partialIds - ids fo PPS
 37      */
 38     encodeSubmittableFields : function(targetBuf,
 39                                        parentItem, partialIds) {
 40             if (!parentItem) throw "NO_PARITEM";
 41             if (partialIds ) {
 42                 this.encodePartialSubmit(parentItem, false, partialIds, targetBuf);
 43             } else {
 44                 // add all nodes
 45                 var eLen = parentItem.elements.length;
 46                 for (var e = 0; e < eLen; e++) {
 47                     this.encodeElement(parentItem.elements[e], targetBuf);
 48                 } // end of for (formElements)
 49             }
 50 
 51     },
 52 
 53      /**
 54      * appends the issuing item if not given already
 55      * @param item
 56      * @param targetBuf
 57      */
 58     appendIssuingItem: function (item, targetBuf) {
 59         // if triggered by a Button send it along
 60         if (item && item.type && item.type.toLowerCase() == "submit") {
 61             targetBuf.append(item.name, item.value);
 62         }
 63     },
 64 
 65 
 66     /**
 67      * encodes a single input element for submission
 68      *
 69      * @param {Node} element - to be encoded
 70      * @param {} targetBuf - a target array buffer receiving the encoded strings
 71      */
 72     encodeElement : function(element, targetBuf) {
 73 
 74         //browser behavior no element name no encoding (normal submit fails in that case)
 75         //https://issues.apache.org/jira/browse/MYFACES-2847
 76         if (!element.name) {
 77             return;
 78         }
 79 
 80         var _RT = this._RT;
 81         var name = element.name;
 82         var tagName = element.tagName.toLowerCase();
 83         var elemType = element.type;
 84         if (elemType != null) {
 85             elemType = elemType.toLowerCase();
 86         }
 87 
 88         // routine for all elements
 89         // rules:
 90         // - process only inputs, textareas and selects
 91         // - elements muest have attribute "name"
 92         // - elements must not be disabled
 93         if (((tagName == "input" || tagName == "textarea" || tagName == "select") &&
 94                 (name != null && name != "")) && !element.disabled) {
 95 
 96             // routine for select elements
 97             // rules:
 98             // - if select-one and value-Attribute exist => "name=value"
 99             // (also if value empty => "name=")
100             // - if select-one and value-Attribute don't exist =>
101             // "name=DisplayValue"
102             // - if select multi and multple selected => "name=value1&name=value2"
103             // - if select and selectedIndex=-1 don't submit
104             if (tagName == "select") {
105                 // selectedIndex must be >= 0 sein to be submittet
106                 if (element.selectedIndex >= 0) {
107                     var uLen = element.options.length;
108                     for (var u = 0; u < uLen; u++) {
109                         // find all selected options
110                         //var subBuf = [];
111                         if (element.options[u].selected) {
112                             var elementOption = element.options[u];
113                             targetBuf.append(name, (elementOption.getAttribute("value") != null) ?
114                                     elementOption.value : elementOption.text);
115                         }
116                     }
117                 }
118             }
119 
120             // routine for remaining elements
121             // rules:
122             // - don't submit no selects (processed above), buttons, reset buttons, submit buttons,
123             // - submit checkboxes and radio inputs only if checked
124             if ((tagName != "select" && elemType != "button"
125                     && elemType != "reset" && elemType != "submit" && elemType != "image")
126                     && ((elemType != "checkbox" && elemType != "radio") || element.checked)) {
127                 if ('undefined' != typeof element.files && element.files != null && _RT.getXHRLvl() >= 2 && element.files.length) {
128                     //xhr level2
129                     targetBuf.append(name, element.files[0]);
130                 } else {
131                     targetBuf.append(name, element.value);
132                 }
133             }
134 
135         }
136     }
137 });