1 /*
  2  * JBoss, Home of Professional Open Source
  3  * Copyright 2013, Red Hat, Inc. and individual contributors
  4  * by the @authors tag. See the copyright.txt in the distribution for a
  5  * full listing of individual contributors.
  6  *
  7  * This is free software; you can redistribute it and/or modify it
  8  * under the terms of the GNU Lesser General Public License as
  9  * published by the Free Software Foundation; either version 2.1 of
 10  * the License, or (at your option) any later version.
 11  *
 12  * This software is distributed in the hope that it will be useful,
 13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
 14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 15  * Lesser General Public License for more details.
 16  *
 17  * You should have received a copy of the GNU Lesser General Public
 18  * License along with this software; if not, write to the Free
 19  * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 20  * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 21  */
 22 
 23 if (typeof jsf != 'undefined') {
 24     (function($, rf, jsf) {
 25 
 26         //JSF log adapter
 27         var identifyElement = function(elt) {
 28             var identifier = '<' + elt.tagName.toLowerCase();
 29             var e = $(elt);
 30             if (e.attr('id')) {
 31                 identifier += (' id=' + e.attr('id'));
 32             }
 33             if (e.attr('class')) {
 34                 identifier += (' class=' + e.attr('class'));
 35             }
 36 
 37             identifier += ' ...>';
 38 
 39             return identifier;
 40         }
 41 
 42         var formatPartialResponseElement = function(logElement, responseElement) {
 43             var change = $(responseElement);
 44 
 45             logElement.append("Element <b>" + responseElement.nodeName + "</b>");
 46             if (change.attr("id")) {
 47                 logElement.append(document.createTextNode(" for id=" + change.attr("id")));
 48             }
 49 
 50             $(document.createElement("br")).appendTo(logElement);
 51             $("<span style='color:dimgray'></span>").appendTo(logElement).text(change.toXML());
 52             $(document.createElement("br")).appendTo(logElement);
 53         }
 54 
 55         var formatPartialResponse = function(partialResponse) {
 56             var logElement = $(document.createElement("span"));
 57 
 58             partialResponse.children().each(function() {
 59                 var responseElement = $(this);
 60                 if (responseElement.is('changes')) {
 61                     logElement.append("Listing content of response <b>changes</b> element:<br />");
 62                     responseElement.children().each(function() {
 63                         formatPartialResponseElement(logElement, this);
 64                     });
 65                 } else {
 66                     formatPartialResponseElement(logElement, this);
 67                 }
 68             });
 69 
 70             return logElement;
 71         }
 72 
 73         var jsfAjaxLogAdapter = function(data) {
 74             try {
 75                 var log = rf.log;
 76 
 77                 var source = data.source;
 78                 var type = data.type;
 79 
 80                 var responseCode = data.responseCode;
 81                 var responseXML = data.responseXML;
 82                 var responseText = data.responseText;
 83 
 84                 if (type != 'error') {
 85                     log.info("Received '" + type + "' event from " + identifyElement(source));
 86 
 87                     if (type == 'beforedomupdate') {
 88                         var partialResponse;
 89 
 90                         if (responseXML) {
 91                             partialResponse = $(responseXML).children("partial-response");
 92                         }
 93 
 94                         var responseTextEntry = $("<span>Server returned responseText: </span><span style='color:dimgray'></span>").eq(1).text(responseText).end();
 95 
 96                         if (partialResponse && partialResponse.length) {
 97                             log.debug(responseTextEntry);
 98                             log.info(formatPartialResponse(partialResponse));
 99                         } else {
100                             log.info(responseTextEntry);
101                         }
102                     }
103                 } else {
104                     var status = data.status;
105                     log.error("Received '" + type + '@' + status + "' event from " + identifyElement(source));
106                     log.error("[" + data.responseCode + "] " + data.errorName + ": " + data.errorMessage);
107                 }
108             } catch (e) {
109                 //ignore logging errors
110             }
111         };
112 
113         var eventsListener = rf.createJSFEventsAdapter({
114                 begin: jsfAjaxLogAdapter,
115                 beforedomupdate: jsfAjaxLogAdapter,
116                 success: jsfAjaxLogAdapter,
117                 complete: jsfAjaxLogAdapter,
118                 error: jsfAjaxLogAdapter
119             });
120 
121         jsf.ajax.addOnEvent(eventsListener);
122         jsf.ajax.addOnError(eventsListener);
123         //
124     }(RichFaces.jQuery, RichFaces, jsf));
125 }
126 ;