2 Copyright (c) 2009, Yahoo! Inc. All rights reserved.
3 Code licensed under the BSD License:
4 http://developer.yahoo.net/yui/license.txt
14 * The DataSource utility provides a common configurable interface for widgets to
15 * access a variety of data, from JavaScript arrays to online database servers.
18 * @requires yahoo, event
19 * @optional json, get, connection
20 * @title DataSource Utility
23 /****************************************************************************/
24 /****************************************************************************/
25 /****************************************************************************/
28 * Base class for the YUI DataSource utility.
30 * @namespace YAHOO.util
31 * @class YAHOO.util.DataSourceBase
33 * @param oLiveData {HTMLElement} Pointer to live data.
34 * @param oConfigs {object} (optional) Object literal of configuration values.
36 util.DataSourceBase = function(oLiveData, oConfigs) {
37 if(oLiveData === null || oLiveData === undefined) {
41 this.liveData = oLiveData;
42 this._oQueue = {interval:null, conn:null, requests:[]};
43 this.responseSchema = {};
45 // Set any config params passed in to override defaults
46 if(oConfigs && (oConfigs.constructor == Object)) {
47 for(var sConfig in oConfigs) {
49 this[sConfig] = oConfigs[sConfig];
54 // Validate and initialize public configs
55 var maxCacheEntries = this.maxCacheEntries;
56 if(!lang.isNumber(maxCacheEntries) || (maxCacheEntries < 0)) {
60 // Initialize interval tracker
61 this._aIntervals = [];
63 /////////////////////////////////////////////////////////////////////////////
67 /////////////////////////////////////////////////////////////////////////////
70 * Fired when a request is made to the local cache.
72 * @event cacheRequestEvent
73 * @param oArgs.request {Object} The request object.
74 * @param oArgs.callback {Object} The callback object.
75 * @param oArgs.caller {Object} (deprecated) Use callback.scope.
77 this.createEvent("cacheRequestEvent");
80 * Fired when data is retrieved from the local cache.
82 * @event cacheResponseEvent
83 * @param oArgs.request {Object} The request object.
84 * @param oArgs.response {Object} The response object.
85 * @param oArgs.callback {Object} The callback object.
86 * @param oArgs.caller {Object} (deprecated) Use callback.scope.
88 this.createEvent("cacheResponseEvent");
91 * Fired when a request is sent to the live data source.
94 * @param oArgs.request {Object} The request object.
95 * @param oArgs.callback {Object} The callback object.
96 * @param oArgs.tId {Number} Transaction ID.
97 * @param oArgs.caller {Object} (deprecated) Use callback.scope.
99 this.createEvent("requestEvent");
102 * Fired when live data source sends response.
104 * @event responseEvent
105 * @param oArgs.request {Object} The request object.
106 * @param oArgs.response {Object} The raw response object.
107 * @param oArgs.callback {Object} The callback object.
108 * @param oArgs.tId {Number} Transaction ID.
109 * @param oArgs.caller {Object} (deprecated) Use callback.scope.
111 this.createEvent("responseEvent");
114 * Fired when response is parsed.
116 * @event responseParseEvent
117 * @param oArgs.request {Object} The request object.
118 * @param oArgs.response {Object} The parsed response object.
119 * @param oArgs.callback {Object} The callback object.
120 * @param oArgs.caller {Object} (deprecated) Use callback.scope.
122 this.createEvent("responseParseEvent");
125 * Fired when response is cached.
127 * @event responseCacheEvent
128 * @param oArgs.request {Object} The request object.
129 * @param oArgs.response {Object} The parsed response object.
130 * @param oArgs.callback {Object} The callback object.
131 * @param oArgs.caller {Object} (deprecated) Use callback.scope.
133 this.createEvent("responseCacheEvent");
135 * Fired when an error is encountered with the live data source.
137 * @event dataErrorEvent
138 * @param oArgs.request {Object} The request object.
139 * @param oArgs.response {String} The response object (if available).
140 * @param oArgs.callback {Object} The callback object.
141 * @param oArgs.caller {Object} (deprecated) Use callback.scope.
142 * @param oArgs.message {String} The error message.
144 this.createEvent("dataErrorEvent");
147 * Fired when the local cache is flushed.
149 * @event cacheFlushEvent
151 this.createEvent("cacheFlushEvent");
153 var DS = util.DataSourceBase;
154 this._sName = "DataSource instance" + DS._nIndex;
158 var DS = util.DataSourceBase;
160 lang.augmentObject(DS, {
162 /////////////////////////////////////////////////////////////////////////////
164 // DataSourceBase public constants
166 /////////////////////////////////////////////////////////////////////////////
171 * @property TYPE_UNKNOWN
179 * Type is a JavaScript Array.
181 * @property TYPE_JSARRAY
189 * Type is a JavaScript Function.
191 * @property TYPE_JSFUNCTION
199 * Type is hosted on a server via an XHR connection.
211 * @property TYPE_JSON
229 * Type is plain text.
231 * @property TYPE_TEXT
239 * Type is an HTML TABLE element. Data is parsed out of TR elements from all TBODY elements.
241 * @property TYPE_HTMLTABLE
249 * Type is hosted on a server via a dynamic script node.
251 * @property TYPE_SCRIPTNODE
261 * @property TYPE_LOCAL
269 * Error message for invalid dataresponses.
271 * @property ERROR_DATAINVALID
274 * @default "Invalid data"
276 ERROR_DATAINVALID : "Invalid data",
279 * Error message for null data responses.
281 * @property ERROR_DATANULL
284 * @default "Null data"
286 ERROR_DATANULL : "Null data",
288 /////////////////////////////////////////////////////////////////////////////
290 // DataSourceBase private static properties
292 /////////////////////////////////////////////////////////////////////////////
295 * Internal class variable to index multiple DataSource instances.
297 * @property DataSourceBase._nIndex
305 * Internal class variable to assign unique transaction IDs.
307 * @property DataSourceBase._nTransactionId
314 /////////////////////////////////////////////////////////////////////////////
316 // DataSourceBase private static methods
318 /////////////////////////////////////////////////////////////////////////////
321 * Get an XPath-specified value for a given field from an XML node or document.
323 * @method _getLocationValue
324 * @param field {String | Object} Field definition.
325 * @param context {Object} XML node or document to search within.
326 * @return {Object} Data value or null.
330 _getLocationValue: function(field, context) {
331 var locator = field.locator || field.key || field,
332 xmldoc = context.ownerDocument || context,
333 result, res, value = null;
337 if(!lang.isUndefined(xmldoc.evaluate)) {
338 result = xmldoc.evaluate(locator, context, xmldoc.createNSResolver(!context.ownerDocument ? context.documentElement : context.ownerDocument.documentElement), 0, null);
339 while(res = result.iterateNext()) {
340 value = res.textContent;
345 xmldoc.setProperty("SelectionLanguage", "XPath");
346 result = context.selectNodes(locator)[0];
347 value = result.value || result.text || null;
356 /////////////////////////////////////////////////////////////////////////////
358 // DataSourceBase public static methods
360 /////////////////////////////////////////////////////////////////////////////
363 * Executes a configured callback. For object literal callbacks, the third
364 * param determines whether to execute the success handler or failure handler.
366 * @method issueCallback
367 * @param callback {Function|Object} the callback to execute
368 * @param params {Array} params to be passed to the callback method
369 * @param error {Boolean} whether an error occurred
370 * @param scope {Object} the scope from which to execute the callback
371 * (deprecated - use an object literal callback)
374 issueCallback : function (callback,params,error,scope) {
375 if (lang.isFunction(callback)) {
376 callback.apply(scope, params);
377 } else if (lang.isObject(callback)) {
378 scope = callback.scope || scope || window;
379 var callbackFunc = callback.success;
381 callbackFunc = callback.failure;
384 callbackFunc.apply(scope, params.concat([callback.argument]));
390 * Converts data to type String.
392 * @method DataSourceBase.parseString
393 * @param oData {String | Number | Boolean | Date | Array | Object} Data to parse.
394 * The special values null and undefined will return null.
395 * @return {String} A string, or null.
398 parseString : function(oData) {
399 // Special case null and undefined
400 if(!lang.isValue(oData)) {
405 var string = oData + "";
408 if(lang.isString(string)) {
417 * Converts data to type Number.
419 * @method DataSourceBase.parseNumber
420 * @param oData {String | Number | Boolean} Data to convert. Note, the following
421 * values return as null: null, undefined, NaN, "".
422 * @return {Number} A number, or null.
425 parseNumber : function(oData) {
426 if(!lang.isValue(oData) || (oData === "")) {
431 var number = oData * 1;
434 if(lang.isNumber(number)) {
441 // Backward compatibility
442 convertNumber : function(oData) {
443 return DS.parseNumber(oData);
447 * Converts data to type Date.
449 * @method DataSourceBase.parseDate
450 * @param oData {Date | String | Number} Data to convert.
451 * @return {Date} A Date instance.
454 parseDate : function(oData) {
458 if(!(oData instanceof Date)) {
459 date = new Date(oData);
466 if(date instanceof Date) {
473 // Backward compatibility
474 convertDate : function(oData) {
475 return DS.parseDate(oData);
480 // Done in separate step so referenced functions are defined.
482 * Data parsing functions.
483 * @property DataSource.Parser
488 string : DS.parseString,
489 number : DS.parseNumber,
493 // Prototype properties and methods
496 /////////////////////////////////////////////////////////////////////////////
498 // DataSourceBase private properties
500 /////////////////////////////////////////////////////////////////////////////
503 * Name of DataSource instance.
512 * Local cache of data result object literals indexed chronologically.
521 * Local queue of request connections, enabled if queue needs to be managed.
530 * Array of polling interval IDs that have been enabled, needed to clear all intervals.
532 * @property _aIntervals
538 /////////////////////////////////////////////////////////////////////////////
540 // DataSourceBase public properties
542 /////////////////////////////////////////////////////////////////////////////
545 * Max size of the local cache. Set to 0 to turn off caching. Caching is
546 * useful to reduce the number of server connections. Recommended only for data
547 * sources that return comprehensive results for queries or when stale data is
550 * @property maxCacheEntries
557 * Pointer to live database.
565 * Where the live data is held:
568 * <dt>TYPE_UNKNOWN</dt>
569 * <dt>TYPE_LOCAL</dt>
571 * <dt>TYPE_SCRIPTNODE</dt>
572 * <dt>TYPE_JSFUNCTION</dt>
577 * @default YAHOO.util.DataSourceBase.TYPE_UNKNOWN
580 dataType : DS.TYPE_UNKNOWN,
583 * Format of response:
586 * <dt>TYPE_UNKNOWN</dt>
587 * <dt>TYPE_JSARRAY</dt>
591 * <dt>TYPE_HTMLTABLE</dt>
594 * @property responseType
596 * @default YAHOO.util.DataSourceBase.TYPE_UNKNOWN
598 responseType : DS.TYPE_UNKNOWN,
601 * Response schema object literal takes a combination of the following properties:
604 * <dt>resultsList</dt> <dd>Pointer to array of tabular data</dd>
605 * <dt>resultNode</dt> <dd>Pointer to node name of row data (XML data only)</dd>
606 * <dt>recordDelim</dt> <dd>Record delimiter (text data only)</dd>
607 * <dt>fieldDelim</dt> <dd>Field delimiter (text data only)</dd>
608 * <dt>fields</dt> <dd>Array of field names (aka keys), or array of object literals
609 * such as: {key:"fieldname",parser:YAHOO.util.DataSourceBase.parseDate}</dd>
610 * <dt>metaFields</dt> <dd>Object literal of keys to include in the oParsedResponse.meta collection</dd>
611 * <dt>metaNode</dt> <dd>Name of the node under which to search for meta information in XML response data</dd>
614 * @property responseSchema
617 responseSchema : null,
620 * Additional arguments passed to the JSON parse routine. The JSON string
621 * is the assumed first argument (where applicable). This property is not
622 * set by default, but the parse methods will use it if present.
624 * @property parseJSONArgs
625 * @type {MIXED|Array} If an Array, contents are used as individual arguments.
626 * Otherwise, value is used as an additional argument.
628 // property intentionally undefined
631 * When working with XML data, setting this property to true enables support for
632 * XPath-syntaxed locators in schema definitions.
640 /////////////////////////////////////////////////////////////////////////////
642 // DataSourceBase public methods
644 /////////////////////////////////////////////////////////////////////////////
647 * Public accessor to the unique name of the DataSource instance.
650 * @return {String} Unique name of the DataSource instance.
652 toString : function() {
657 * Overridable method passes request to cache and returns cached response if any,
658 * refreshing the hit in the cache as the newest item. Returns null if there is
661 * @method getCachedResponse
662 * @param oRequest {Object} Request object.
663 * @param oCallback {Object} Callback object.
664 * @param oCaller {Object} (deprecated) Use callback object.
665 * @return {Object} Cached response object or null.
667 getCachedResponse : function(oRequest, oCallback, oCaller) {
668 var aCache = this._aCache;
670 // If cache is enabled...
671 if(this.maxCacheEntries > 0) {
672 // Initialize local cache
676 // Look in local cache
678 var nCacheLength = aCache.length;
679 if(nCacheLength > 0) {
680 var oResponse = null;
681 this.fireEvent("cacheRequestEvent", {request:oRequest,callback:oCallback,caller:oCaller});
683 // Loop through each cached element
684 for(var i = nCacheLength-1; i >= 0; i--) {
685 var oCacheElem = aCache[i];
687 // Defer cache hit logic to a public overridable method
688 if(this.isCacheHit(oRequest,oCacheElem.request)) {
689 // The cache returned a hit!
690 // Grab the cached response
691 oResponse = oCacheElem.response;
692 this.fireEvent("cacheResponseEvent", {request:oRequest,response:oResponse,callback:oCallback,caller:oCaller});
694 // Refresh the position of the cache hit
695 if(i < nCacheLength-1) {
696 // Remove element from its original location
699 this.addToCache(oRequest, oResponse);
703 oResponse.cached = true;
718 * Default overridable method matches given request to given cached request.
719 * Returns true if is a hit, returns false otherwise. Implementers should
720 * override this method to customize the cache-matching algorithm.
723 * @param oRequest {Object} Request object.
724 * @param oCachedRequest {Object} Cached request object.
725 * @return {Boolean} True if given request matches cached request, false otherwise.
727 isCacheHit : function(oRequest, oCachedRequest) {
728 return (oRequest === oCachedRequest);
732 * Adds a new item to the cache. If cache is full, evicts the stalest item
733 * before adding the new item.
736 * @param oRequest {Object} Request object.
737 * @param oResponse {Object} Response object to cache.
739 addToCache : function(oRequest, oResponse) {
740 var aCache = this._aCache;
745 // If the cache is full, make room by removing stalest element (index=0)
746 while(aCache.length >= this.maxCacheEntries) {
750 // Add to cache in the newest position, at the end of the array
751 var oCacheElem = {request:oRequest,response:oResponse};
752 aCache[aCache.length] = oCacheElem;
753 this.fireEvent("responseCacheEvent", {request:oRequest,response:oResponse});
761 flushCache : function() {
764 this.fireEvent("cacheFlushEvent");
769 * Sets up a polling mechanism to send requests at set intervals and forward
770 * responses to given callback.
772 * @method setInterval
773 * @param nMsec {Number} Length of interval in milliseconds.
774 * @param oRequest {Object} Request object.
775 * @param oCallback {Function} Handler function to receive the response.
776 * @param oCaller {Object} (deprecated) Use oCallback.scope.
777 * @return {Number} Interval ID.
779 setInterval : function(nMsec, oRequest, oCallback, oCaller) {
780 if(lang.isNumber(nMsec) && (nMsec >= 0)) {
782 var nId = setInterval(function() {
783 oSelf.makeConnection(oRequest, oCallback, oCaller);
785 this._aIntervals.push(nId);
793 * Disables polling mechanism associated with the given interval ID.
795 * @method clearInterval
796 * @param nId {Number} Interval ID.
798 clearInterval : function(nId) {
799 // Remove from tracker if there
800 var tracker = this._aIntervals || [];
801 for(var i=tracker.length-1; i>-1; i--) {
802 if(tracker[i] === nId) {
810 * Disables all known polling intervals.
812 * @method clearAllIntervals
814 clearAllIntervals : function() {
815 var tracker = this._aIntervals || [];
816 for(var i=tracker.length-1; i>-1; i--) {
817 clearInterval(tracker[i]);
823 * First looks for cached response, then sends request to live data. The
824 * following arguments are passed to the callback function:
826 * <dt><code>oRequest</code></dt>
827 * <dd>The same value that was passed in as the first argument to sendRequest.</dd>
828 * <dt><code>oParsedResponse</code></dt>
829 * <dd>An object literal containing the following properties:
831 * <dt><code>tId</code></dt>
832 * <dd>Unique transaction ID number.</dd>
833 * <dt><code>results</code></dt>
834 * <dd>Schema-parsed data results.</dd>
835 * <dt><code>error</code></dt>
836 * <dd>True in cases of data error.</dd>
837 * <dt><code>cached</code></dt>
838 * <dd>True when response is returned from DataSource cache.</dd>
839 * <dt><code>meta</code></dt>
840 * <dd>Schema-parsed meta data.</dd>
842 * <dt><code>oPayload</code></dt>
843 * <dd>The same value as was passed in as <code>argument</code> in the oCallback object literal.</dd>
846 * @method sendRequest
847 * @param oRequest {Object} Request object.
848 * @param oCallback {Object} An object literal with the following properties:
850 * <dt><code>success</code></dt>
851 * <dd>The function to call when the data is ready.</dd>
852 * <dt><code>failure</code></dt>
853 * <dd>The function to call upon a response failure condition.</dd>
854 * <dt><code>scope</code></dt>
855 * <dd>The object to serve as the scope for the success and failure handlers.</dd>
856 * <dt><code>argument</code></dt>
857 * <dd>Arbitrary data that will be passed back to the success and failure handlers.</dd>
859 * @param oCaller {Object} (deprecated) Use oCallback.scope.
860 * @return {Number} Transaction ID, or null if response found in cache.
862 sendRequest : function(oRequest, oCallback, oCaller) {
863 // First look in cache
864 var oCachedResponse = this.getCachedResponse(oRequest, oCallback, oCaller);
865 if(oCachedResponse) {
866 DS.issueCallback(oCallback,[oRequest,oCachedResponse],false,oCaller);
871 // Not in cache, so forward request to live data
872 return this.makeConnection(oRequest, oCallback, oCaller);
876 * Overridable default method generates a unique transaction ID and passes
877 * the live data reference directly to the handleResponse function. This
878 * method should be implemented by subclasses to achieve more complex behavior
879 * or to access remote data.
881 * @method makeConnection
882 * @param oRequest {Object} Request object.
883 * @param oCallback {Object} Callback object literal.
884 * @param oCaller {Object} (deprecated) Use oCallback.scope.
885 * @return {Number} Transaction ID.
887 makeConnection : function(oRequest, oCallback, oCaller) {
888 var tId = DS._nTransactionId++;
889 this.fireEvent("requestEvent", {tId:tId, request:oRequest,callback:oCallback,caller:oCaller});
891 /* accounts for the following cases:
892 YAHOO.util.DataSourceBase.TYPE_UNKNOWN
893 YAHOO.util.DataSourceBase.TYPE_JSARRAY
894 YAHOO.util.DataSourceBase.TYPE_JSON
895 YAHOO.util.DataSourceBase.TYPE_HTMLTABLE
896 YAHOO.util.DataSourceBase.TYPE_XML
897 YAHOO.util.DataSourceBase.TYPE_TEXT
899 var oRawResponse = this.liveData;
901 this.handleResponse(oRequest, oRawResponse, oCallback, oCaller, tId);
906 * Receives raw data response and type converts to XML, JSON, etc as necessary.
907 * Forwards oFullResponse to appropriate parsing function to get turned into
908 * oParsedResponse. Calls doBeforeCallback() and adds oParsedResponse to
909 * the cache when appropriate before calling issueCallback().
911 * The oParsedResponse object literal has the following properties:
913 * <dd><dt>tId {Number}</dt> Unique transaction ID</dd>
914 * <dd><dt>results {Array}</dt> Array of parsed data results</dd>
915 * <dd><dt>meta {Object}</dt> Object literal of meta values</dd>
916 * <dd><dt>error {Boolean}</dt> (optional) True if there was an error</dd>
917 * <dd><dt>cached {Boolean}</dt> (optional) True if response was cached</dd>
920 * @method handleResponse
921 * @param oRequest {Object} Request object
922 * @param oRawResponse {Object} The raw response from the live database.
923 * @param oCallback {Object} Callback object literal.
924 * @param oCaller {Object} (deprecated) Use oCallback.scope.
925 * @param tId {Number} Transaction ID.
927 handleResponse : function(oRequest, oRawResponse, oCallback, oCaller, tId) {
928 this.fireEvent("responseEvent", {tId:tId, request:oRequest, response:oRawResponse,
929 callback:oCallback, caller:oCaller});
930 var xhr = (this.dataType == DS.TYPE_XHR) ? true : false;
931 var oParsedResponse = null;
932 var oFullResponse = oRawResponse;
934 // Try to sniff data type if it has not been defined
935 if(this.responseType === DS.TYPE_UNKNOWN) {
936 var ctype = (oRawResponse && oRawResponse.getResponseHeader) ? oRawResponse.getResponseHeader["Content-Type"] : null;
939 if(ctype.indexOf("text/xml") > -1) {
940 this.responseType = DS.TYPE_XML;
942 else if(ctype.indexOf("application/json") > -1) { // json
943 this.responseType = DS.TYPE_JSON;
945 else if(ctype.indexOf("text/plain") > -1) { // text
946 this.responseType = DS.TYPE_TEXT;
950 if(YAHOO.lang.isArray(oRawResponse)) { // array
951 this.responseType = DS.TYPE_JSARRAY;
954 else if(oRawResponse && oRawResponse.nodeType && (oRawResponse.nodeType === 9 || oRawResponse.nodeType === 1 || oRawResponse.nodeType === 11)) {
955 this.responseType = DS.TYPE_XML;
957 else if(oRawResponse && oRawResponse.nodeName && (oRawResponse.nodeName.toLowerCase() == "table")) { // table
958 this.responseType = DS.TYPE_HTMLTABLE;
960 else if(YAHOO.lang.isObject(oRawResponse)) { // json
961 this.responseType = DS.TYPE_JSON;
963 else if(YAHOO.lang.isString(oRawResponse)) { // text
964 this.responseType = DS.TYPE_TEXT;
969 switch(this.responseType) {
970 case DS.TYPE_JSARRAY:
971 if(xhr && oRawResponse && oRawResponse.responseText) {
972 oFullResponse = oRawResponse.responseText;
975 // Convert to JS array if it's a string
976 if(lang.isString(oFullResponse)) {
977 var parseArgs = [oFullResponse].concat(this.parseJSONArgs);
978 // Check for YUI JSON Util
980 oFullResponse = lang.JSON.parse.apply(lang.JSON,parseArgs);
982 // Look for JSON parsers using an API similar to json2.js
983 else if(window.JSON && JSON.parse) {
984 oFullResponse = JSON.parse.apply(JSON,parseArgs);
986 // Look for JSON parsers using an API similar to json.js
987 else if(oFullResponse.parseJSON) {
988 oFullResponse = oFullResponse.parseJSON.apply(oFullResponse,parseArgs.slice(1));
990 // No JSON lib found so parse the string
992 // Trim leading spaces
993 while (oFullResponse.length > 0 &&
994 (oFullResponse.charAt(0) != "{") &&
995 (oFullResponse.charAt(0) != "[")) {
996 oFullResponse = oFullResponse.substring(1, oFullResponse.length);
999 if(oFullResponse.length > 0) {
1000 // Strip extraneous stuff at the end
1002 Math.max(oFullResponse.lastIndexOf("]"),oFullResponse.lastIndexOf("}"));
1003 oFullResponse = oFullResponse.substring(0,arrayEnd+1);
1005 // Turn the string into an object literal...
1006 // ...eval is necessary here
1007 oFullResponse = eval("(" + oFullResponse + ")");
1015 oFullResponse = this.doBeforeParseData(oRequest, oFullResponse, oCallback);
1016 oParsedResponse = this.parseArrayData(oRequest, oFullResponse);
1019 if(xhr && oRawResponse && oRawResponse.responseText) {
1020 oFullResponse = oRawResponse.responseText;
1023 // Convert to JSON object if it's a string
1024 if(lang.isString(oFullResponse)) {
1025 var parseArgs = [oFullResponse].concat(this.parseJSONArgs);
1026 // Check for YUI JSON Util
1028 oFullResponse = lang.JSON.parse.apply(lang.JSON,parseArgs);
1030 // Look for JSON parsers using an API similar to json2.js
1031 else if(window.JSON && JSON.parse) {
1032 oFullResponse = JSON.parse.apply(JSON,parseArgs);
1034 // Look for JSON parsers using an API similar to json.js
1035 else if(oFullResponse.parseJSON) {
1036 oFullResponse = oFullResponse.parseJSON.apply(oFullResponse,parseArgs.slice(1));
1038 // No JSON lib found so parse the string
1040 // Trim leading spaces
1041 while (oFullResponse.length > 0 &&
1042 (oFullResponse.charAt(0) != "{") &&
1043 (oFullResponse.charAt(0) != "[")) {
1044 oFullResponse = oFullResponse.substring(1, oFullResponse.length);
1047 if(oFullResponse.length > 0) {
1048 // Strip extraneous stuff at the end
1049 var objEnd = Math.max(oFullResponse.lastIndexOf("]"),oFullResponse.lastIndexOf("}"));
1050 oFullResponse = oFullResponse.substring(0,objEnd+1);
1052 // Turn the string into an object literal...
1053 // ...eval is necessary here
1054 oFullResponse = eval("(" + oFullResponse + ")");
1063 oFullResponse = this.doBeforeParseData(oRequest, oFullResponse, oCallback);
1064 oParsedResponse = this.parseJSONData(oRequest, oFullResponse);
1066 case DS.TYPE_HTMLTABLE:
1067 if(xhr && oRawResponse.responseText) {
1068 var el = document.createElement('div');
1069 el.innerHTML = oRawResponse.responseText;
1070 oFullResponse = el.getElementsByTagName('table')[0];
1072 oFullResponse = this.doBeforeParseData(oRequest, oFullResponse, oCallback);
1073 oParsedResponse = this.parseHTMLTableData(oRequest, oFullResponse);
1076 if(xhr && oRawResponse.responseXML) {
1077 oFullResponse = oRawResponse.responseXML;
1079 oFullResponse = this.doBeforeParseData(oRequest, oFullResponse, oCallback);
1080 oParsedResponse = this.parseXMLData(oRequest, oFullResponse);
1083 if(xhr && lang.isString(oRawResponse.responseText)) {
1084 oFullResponse = oRawResponse.responseText;
1086 oFullResponse = this.doBeforeParseData(oRequest, oFullResponse, oCallback);
1087 oParsedResponse = this.parseTextData(oRequest, oFullResponse);
1090 oFullResponse = this.doBeforeParseData(oRequest, oFullResponse, oCallback);
1091 oParsedResponse = this.parseData(oRequest, oFullResponse);
1096 // Clean up for consistent signature
1097 oParsedResponse = oParsedResponse || {};
1098 if(!oParsedResponse.results) {
1099 oParsedResponse.results = [];
1101 if(!oParsedResponse.meta) {
1102 oParsedResponse.meta = {};
1106 if(!oParsedResponse.error) {
1107 // Last chance to touch the raw response or the parsed response
1108 oParsedResponse = this.doBeforeCallback(oRequest, oFullResponse, oParsedResponse, oCallback);
1109 this.fireEvent("responseParseEvent", {request:oRequest,
1110 response:oParsedResponse, callback:oCallback, caller:oCaller});
1111 // Cache the response
1112 this.addToCache(oRequest, oParsedResponse);
1116 // Be sure the error flag is on
1117 oParsedResponse.error = true;
1118 this.fireEvent("dataErrorEvent", {request:oRequest, response: oRawResponse, callback:oCallback,
1119 caller:oCaller, message:DS.ERROR_DATANULL});
1122 // Send the response back to the caller
1123 oParsedResponse.tId = tId;
1124 DS.issueCallback(oCallback,[oRequest,oParsedResponse],oParsedResponse.error,oCaller);
1128 * Overridable method gives implementers access to the original full response
1129 * before the data gets parsed. Implementers should take care not to return an
1130 * unparsable or otherwise invalid response.
1132 * @method doBeforeParseData
1133 * @param oRequest {Object} Request object.
1134 * @param oFullResponse {Object} The full response from the live database.
1135 * @param oCallback {Object} The callback object.
1136 * @return {Object} Full response for parsing.
1139 doBeforeParseData : function(oRequest, oFullResponse, oCallback) {
1140 return oFullResponse;
1144 * Overridable method gives implementers access to the original full response and
1145 * the parsed response (parsed against the given schema) before the data
1146 * is added to the cache (if applicable) and then sent back to callback function.
1147 * This is your chance to access the raw response and/or populate the parsed
1148 * response with any custom data.
1150 * @method doBeforeCallback
1151 * @param oRequest {Object} Request object.
1152 * @param oFullResponse {Object} The full response from the live database.
1153 * @param oParsedResponse {Object} The parsed response to return to calling object.
1154 * @param oCallback {Object} The callback object.
1155 * @return {Object} Parsed response object.
1157 doBeforeCallback : function(oRequest, oFullResponse, oParsedResponse, oCallback) {
1158 return oParsedResponse;
1162 * Overridable method parses data of generic RESPONSE_TYPE into a response object.
1165 * @param oRequest {Object} Request object.
1166 * @param oFullResponse {Object} The full Array from the live database.
1167 * @return {Object} Parsed response object with the following properties:<br>
1168 * - results {Array} Array of parsed data results<br>
1169 * - meta {Object} Object literal of meta values<br>
1170 * - error {Boolean} (optional) True if there was an error<br>
1172 parseData : function(oRequest, oFullResponse) {
1173 if(lang.isValue(oFullResponse)) {
1174 var oParsedResponse = {results:oFullResponse,meta:{}};
1175 return oParsedResponse;
1182 * Overridable method parses Array data into a response object.
1184 * @method parseArrayData
1185 * @param oRequest {Object} Request object.
1186 * @param oFullResponse {Object} The full Array from the live database.
1187 * @return {Object} Parsed response object with the following properties:<br>
1188 * - results (Array) Array of parsed data results<br>
1189 * - error (Boolean) True if there was an error
1191 parseArrayData : function(oRequest, oFullResponse) {
1192 if(lang.isArray(oFullResponse)) {
1198 if(lang.isArray(this.responseSchema.fields)) {
1199 var fields = this.responseSchema.fields;
1200 for (i = fields.length - 1; i >= 0; --i) {
1201 if (typeof fields[i] !== 'object') {
1202 fields[i] = { key : fields[i] };
1206 var parsers = {}, p;
1207 for (i = fields.length - 1; i >= 0; --i) {
1208 p = (typeof fields[i].parser === 'function' ?
1210 DS.Parser[fields[i].parser+'']) || fields[i].converter;
1212 parsers[fields[i].key] = p;
1216 var arrType = lang.isArray(oFullResponse[0]);
1217 for(i=oFullResponse.length-1; i>-1; i--) {
1219 rec = oFullResponse[i];
1220 if (typeof rec === 'object') {
1221 for(j=fields.length-1; j>-1; j--) {
1223 data = arrType ? rec[j] : rec[field.key];
1225 if (parsers[field.key]) {
1226 data = parsers[field.key].call(this,data);
1230 if(data === undefined) {
1234 oResult[field.key] = data;
1237 else if (lang.isString(rec)) {
1238 for(j=fields.length-1; j>-1; j--) {
1242 if (parsers[field.key]) {
1243 data = parsers[field.key].call(this,data);
1247 if(data === undefined) {
1251 oResult[field.key] = data;
1254 results[i] = oResult;
1257 // Return entire data set
1259 results = oFullResponse;
1261 var oParsedResponse = {results:results};
1262 return oParsedResponse;
1269 * Overridable method parses plain text data into a response object.
1271 * @method parseTextData
1272 * @param oRequest {Object} Request object.
1273 * @param oFullResponse {Object} The full text response from the live database.
1274 * @return {Object} Parsed response object with the following properties:<br>
1275 * - results (Array) Array of parsed data results<br>
1276 * - error (Boolean) True if there was an error
1278 parseTextData : function(oRequest, oFullResponse) {
1279 if(lang.isString(oFullResponse)) {
1280 if(lang.isString(this.responseSchema.recordDelim) &&
1281 lang.isString(this.responseSchema.fieldDelim)) {
1282 var oParsedResponse = {results:[]};
1283 var recDelim = this.responseSchema.recordDelim;
1284 var fieldDelim = this.responseSchema.fieldDelim;
1285 if(oFullResponse.length > 0) {
1286 // Delete the last line delimiter at the end of the data if it exists
1287 var newLength = oFullResponse.length-recDelim.length;
1288 if(oFullResponse.substr(newLength) == recDelim) {
1289 oFullResponse = oFullResponse.substr(0, newLength);
1291 if(oFullResponse.length > 0) {
1292 // Split along record delimiter to get an array of strings
1293 var recordsarray = oFullResponse.split(recDelim);
1294 // Cycle through each record
1295 for(var i = 0, len = recordsarray.length, recIdx = 0; i < len; ++i) {
1297 sRecord = recordsarray[i];
1298 if (lang.isString(sRecord) && (sRecord.length > 0)) {
1299 // Split each record along field delimiter to get data
1300 var fielddataarray = recordsarray[i].split(fieldDelim);
1303 // Filter for fields data
1304 if(lang.isArray(this.responseSchema.fields)) {
1305 var fields = this.responseSchema.fields;
1306 for(var j=fields.length-1; j>-1; j--) {
1308 // Remove quotation marks from edges, if applicable
1309 var data = fielddataarray[j];
1310 if (lang.isString(data)) {
1311 if(data.charAt(0) == "\"") {
1312 data = data.substr(1);
1314 if(data.charAt(data.length-1) == "\"") {
1315 data = data.substr(0,data.length-1);
1317 var field = fields[j];
1318 var key = (lang.isValue(field.key)) ? field.key : field;
1319 // Backward compatibility
1320 if(!field.parser && field.converter) {
1321 field.parser = field.converter;
1323 var parser = (typeof field.parser === 'function') ?
1325 DS.Parser[field.parser+''];
1327 data = parser.call(this, data);
1330 if(data === undefined) {
1333 oResult[key] = data;
1344 // No fields defined so pass along all data as an array
1346 oResult = fielddataarray;
1349 oParsedResponse.results[recIdx++] = oResult;
1355 return oParsedResponse;
1363 * Overridable method parses XML data for one result into an object literal.
1365 * @method parseXMLResult
1366 * @param result {XML} XML for one result.
1367 * @return {Object} Object literal of data for one result.
1369 parseXMLResult : function(result) {
1371 schema = this.responseSchema;
1374 // Loop through each data field in each result using the schema
1375 for(var m = schema.fields.length-1; m >= 0 ; m--) {
1376 var field = schema.fields[m];
1377 var key = (lang.isValue(field.key)) ? field.key : field;
1381 data = YAHOO.util.DataSource._getLocationValue(field, result);
1384 // Values may be held in an attribute...
1385 var xmlAttr = result.attributes.getNamedItem(key);
1387 data = xmlAttr.value;
1391 var xmlNode = result.getElementsByTagName(key);
1392 if(xmlNode && xmlNode.item(0)) {
1393 var item = xmlNode.item(0);
1394 // For IE, then DOM...
1395 data = (item) ? ((item.text) ? item.text : (item.textContent) ? item.textContent : null) : null;
1396 // ...then fallback, but check for multiple child nodes
1398 var datapieces = [];
1399 for(var j=0, len=item.childNodes.length; j<len; j++) {
1400 if(item.childNodes[j].nodeValue) {
1401 datapieces[datapieces.length] = item.childNodes[j].nodeValue;
1404 if(datapieces.length > 0) {
1405 data = datapieces.join("");
1417 // Backward compatibility
1418 if(!field.parser && field.converter) {
1419 field.parser = field.converter;
1421 var parser = (typeof field.parser === 'function') ?
1423 DS.Parser[field.parser+''];
1425 data = parser.call(this, data);
1428 if(data === undefined) {
1431 oResult[key] = data;
1443 * Overridable method parses XML data into a response object.
1445 * @method parseXMLData
1446 * @param oRequest {Object} Request object.
1447 * @param oFullResponse {Object} The full XML response from the live database.
1448 * @return {Object} Parsed response object with the following properties<br>
1449 * - results (Array) Array of parsed data results<br>
1450 * - error (Boolean) True if there was an error
1452 parseXMLData : function(oRequest, oFullResponse) {
1454 schema = this.responseSchema,
1455 oParsedResponse = {meta:{}},
1457 metaNode = schema.metaNode,
1458 metaLocators = schema.metaFields || {},
1461 // In case oFullResponse is something funky
1463 // Pull any meta identified
1465 for (k in metaLocators) {
1466 oParsedResponse.meta[k] = YAHOO.util.DataSource._getLocationValue(metaLocators[k], oFullResponse);
1470 metaNode = metaNode ? oFullResponse.getElementsByTagName(metaNode)[0] :
1474 for (k in metaLocators) {
1475 if (lang.hasOwnProperty(metaLocators, k)) {
1476 loc = metaLocators[k];
1478 v = metaNode.getElementsByTagName(loc)[0];
1481 v = v.firstChild.nodeValue;
1483 // Look for an attribute
1484 v = metaNode.attributes.getNamedItem(loc);
1490 if (lang.isValue(v)) {
1491 oParsedResponse.meta[k] = v;
1499 xmlList = (schema.resultNode) ?
1500 oFullResponse.getElementsByTagName(schema.resultNode) :
1505 if(!xmlList || !lang.isArray(schema.fields)) {
1508 // Loop through each result
1510 oParsedResponse.results = [];
1511 for(i = xmlList.length-1; i >= 0 ; --i) {
1512 var oResult = this.parseXMLResult(xmlList.item(i));
1513 // Capture each array of values into an array of results
1514 oParsedResponse.results[i] = oResult;
1518 oParsedResponse.error = true;
1522 return oParsedResponse;
1526 * Overridable method parses JSON data into a response object.
1528 * @method parseJSONData
1529 * @param oRequest {Object} Request object.
1530 * @param oFullResponse {Object} The full JSON from the live database.
1531 * @return {Object} Parsed response object with the following properties<br>
1532 * - results (Array) Array of parsed data results<br>
1533 * - error (Boolean) True if there was an error
1535 parseJSONData : function(oRequest, oFullResponse) {
1536 var oParsedResponse = {results:[],meta:{}};
1538 if(lang.isObject(oFullResponse) && this.responseSchema.resultsList) {
1539 var schema = this.responseSchema,
1540 fields = schema.fields,
1541 resultsList = oFullResponse,
1543 metaFields = schema.metaFields || {},
1548 i,len,j,v,key,parser,path;
1550 // Function to convert the schema's fields into walk paths
1551 var buildPath = function (needle) {
1552 var path = null, keys = [], i = 0;
1554 // Strip the ["string keys"] and [1] array indexes
1556 replace(/\[(['"])(.*?)\1\]/g,
1557 function (x,$1,$2) {keys[i]=$2;return '.@'+(i++);}).
1558 replace(/\[(\d+)\]/g,
1559 function (x,$1) {keys[i]=parseInt($1,10)|0;return '.@'+(i++);}).
1560 replace(/^\./,''); // remove leading dot
1562 // If the cleaned needle contains invalid characters, the
1564 if (!/[^\w\.\$@]/.test(needle)) {
1565 path = needle.split('.');
1566 for (i=path.length-1; i >= 0; --i) {
1567 if (path[i].charAt(0) === '@') {
1568 path[i] = keys[parseInt(path[i].substr(1),10)];
1579 // Function to walk a path and return the pot of gold
1580 var walkPath = function (path, origin) {
1581 var v=origin,i=0,len=path.length;
1582 for (;i<len && v;++i) {
1588 // Parse the response
1589 // Step 1. Pull the resultsList from oFullResponse (default assumes
1590 // oFullResponse IS the resultsList)
1591 path = buildPath(schema.resultsList);
1593 resultsList = walkPath(path, oFullResponse);
1594 if (resultsList === undefined) {
1605 if (!lang.isArray(resultsList)) {
1606 resultsList = [resultsList];
1610 // Step 2. Parse out field data if identified
1613 // Build the field parser map and location paths
1614 for (i=0, len=fields.length; i<len; i++) {
1616 key = field.key || field;
1617 parser = ((typeof field.parser === 'function') ?
1619 DS.Parser[field.parser+'']) || field.converter;
1620 path = buildPath(key);
1623 fieldParsers[fieldParsers.length] = {key:key,parser:parser};
1627 if (path.length > 1) {
1628 fieldPaths[fieldPaths.length] = {key:key,path:path};
1630 simpleFields[simpleFields.length] = {key:key,path:path[0]};
1636 // Process the results, flattening the records and/or applying parsers if needed
1637 for (i = resultsList.length - 1; i >= 0; --i) {
1638 var r = resultsList[i], rec = {};
1640 for (j = simpleFields.length - 1; j >= 0; --j) {
1641 // Bug 1777850: data might be held in an array
1642 rec[simpleFields[j].key] =
1643 (r[simpleFields[j].path] !== undefined) ?
1644 r[simpleFields[j].path] : r[j];
1647 for (j = fieldPaths.length - 1; j >= 0; --j) {
1648 rec[fieldPaths[j].key] = walkPath(fieldPaths[j].path,r);
1651 for (j = fieldParsers.length - 1; j >= 0; --j) {
1652 var p = fieldParsers[j].key;
1653 rec[p] = fieldParsers[j].parser(rec[p]);
1654 if (rec[p] === undefined) {
1663 results = resultsList;
1666 for (key in metaFields) {
1667 if (lang.hasOwnProperty(metaFields,key)) {
1668 path = buildPath(metaFields[key]);
1670 v = walkPath(path, oFullResponse);
1671 oParsedResponse.meta[key] = v;
1678 oParsedResponse.error = true;
1681 oParsedResponse.results = results;
1684 oParsedResponse.error = true;
1687 return oParsedResponse;
1691 * Overridable method parses an HTML TABLE element reference into a response object.
1692 * Data is parsed out of TR elements from all TBODY elements.
1694 * @method parseHTMLTableData
1695 * @param oRequest {Object} Request object.
1696 * @param oFullResponse {Object} The full HTML element reference from the live database.
1697 * @return {Object} Parsed response object with the following properties<br>
1698 * - results (Array) Array of parsed data results<br>
1699 * - error (Boolean) True if there was an error
1701 parseHTMLTableData : function(oRequest, oFullResponse) {
1703 var elTable = oFullResponse;
1704 var fields = this.responseSchema.fields;
1705 var oParsedResponse = {results:[]};
1707 if(lang.isArray(fields)) {
1708 // Iterate through each TBODY
1709 for(var i=0; i<elTable.tBodies.length; i++) {
1710 var elTbody = elTable.tBodies[i];
1712 // Iterate through each TR
1713 for(var j=elTbody.rows.length-1; j>-1; j--) {
1714 var elRow = elTbody.rows[j];
1717 for(var k=fields.length-1; k>-1; k--) {
1718 var field = fields[k];
1719 var key = (lang.isValue(field.key)) ? field.key : field;
1720 var data = elRow.cells[k].innerHTML;
1722 // Backward compatibility
1723 if(!field.parser && field.converter) {
1724 field.parser = field.converter;
1726 var parser = (typeof field.parser === 'function') ?
1728 DS.Parser[field.parser+''];
1730 data = parser.call(this, data);
1733 if(data === undefined) {
1736 oResult[key] = data;
1738 oParsedResponse.results[j] = oResult;
1747 oParsedResponse.error = true;
1751 return oParsedResponse;
1756 // DataSourceBase uses EventProvider
1757 lang.augmentProto(DS, util.EventProvider);
1761 /****************************************************************************/
1762 /****************************************************************************/
1763 /****************************************************************************/
1766 * LocalDataSource class for in-memory data structs including JavaScript arrays,
1767 * JavaScript object literals (JSON), XML documents, and HTML tables.
1769 * @namespace YAHOO.util
1770 * @class YAHOO.util.LocalDataSource
1771 * @extends YAHOO.util.DataSourceBase
1773 * @param oLiveData {HTMLElement} Pointer to live data.
1774 * @param oConfigs {object} (optional) Object literal of configuration values.
1776 util.LocalDataSource = function(oLiveData, oConfigs) {
1777 this.dataType = DS.TYPE_LOCAL;
1780 if(YAHOO.lang.isArray(oLiveData)) { // array
1781 this.responseType = DS.TYPE_JSARRAY;
1784 else if(oLiveData.nodeType && oLiveData.nodeType == 9) {
1785 this.responseType = DS.TYPE_XML;
1787 else if(oLiveData.nodeName && (oLiveData.nodeName.toLowerCase() == "table")) { // table
1788 this.responseType = DS.TYPE_HTMLTABLE;
1789 oLiveData = oLiveData.cloneNode(true);
1791 else if(YAHOO.lang.isString(oLiveData)) { // text
1792 this.responseType = DS.TYPE_TEXT;
1794 else if(YAHOO.lang.isObject(oLiveData)) { // json
1795 this.responseType = DS.TYPE_JSON;
1800 this.responseType = DS.TYPE_JSARRAY;
1803 util.LocalDataSource.superclass.constructor.call(this, oLiveData, oConfigs);
1806 // LocalDataSource extends DataSourceBase
1807 lang.extend(util.LocalDataSource, DS);
1809 // Copy static members to LocalDataSource class
1810 lang.augmentObject(util.LocalDataSource, DS);
1824 /****************************************************************************/
1825 /****************************************************************************/
1826 /****************************************************************************/
1829 * FunctionDataSource class for JavaScript functions.
1831 * @namespace YAHOO.util
1832 * @class YAHOO.util.FunctionDataSource
1833 * @extends YAHOO.util.DataSourceBase
1835 * @param oLiveData {HTMLElement} Pointer to live data.
1836 * @param oConfigs {object} (optional) Object literal of configuration values.
1838 util.FunctionDataSource = function(oLiveData, oConfigs) {
1839 this.dataType = DS.TYPE_JSFUNCTION;
1840 oLiveData = oLiveData || function() {};
1842 util.FunctionDataSource.superclass.constructor.call(this, oLiveData, oConfigs);
1845 // FunctionDataSource extends DataSourceBase
1846 lang.extend(util.FunctionDataSource, DS, {
1848 /////////////////////////////////////////////////////////////////////////////
1850 // FunctionDataSource public properties
1852 /////////////////////////////////////////////////////////////////////////////
1855 * Context in which to execute the function. By default, is the DataSource
1856 * instance itself. If set, the function will receive the DataSource instance
1857 * as an additional argument.
1866 /////////////////////////////////////////////////////////////////////////////
1868 // FunctionDataSource public methods
1870 /////////////////////////////////////////////////////////////////////////////
1873 * Overriding method passes query to a function. The returned response is then
1874 * forwarded to the handleResponse function.
1876 * @method makeConnection
1877 * @param oRequest {Object} Request object.
1878 * @param oCallback {Object} Callback object literal.
1879 * @param oCaller {Object} (deprecated) Use oCallback.scope.
1880 * @return {Number} Transaction ID.
1882 makeConnection : function(oRequest, oCallback, oCaller) {
1883 var tId = DS._nTransactionId++;
1884 this.fireEvent("requestEvent", {tId:tId,request:oRequest,callback:oCallback,caller:oCaller});
1886 // Pass the request in as a parameter and
1887 // forward the return value to the handler
1890 var oRawResponse = (this.scope) ? this.liveData.call(this.scope, oRequest, this) : this.liveData(oRequest);
1892 // Try to sniff data type if it has not been defined
1893 if(this.responseType === DS.TYPE_UNKNOWN) {
1894 if(YAHOO.lang.isArray(oRawResponse)) { // array
1895 this.responseType = DS.TYPE_JSARRAY;
1898 else if(oRawResponse && oRawResponse.nodeType && oRawResponse.nodeType == 9) {
1899 this.responseType = DS.TYPE_XML;
1901 else if(oRawResponse && oRawResponse.nodeName && (oRawResponse.nodeName.toLowerCase() == "table")) { // table
1902 this.responseType = DS.TYPE_HTMLTABLE;
1904 else if(YAHOO.lang.isObject(oRawResponse)) { // json
1905 this.responseType = DS.TYPE_JSON;
1907 else if(YAHOO.lang.isString(oRawResponse)) { // text
1908 this.responseType = DS.TYPE_TEXT;
1912 this.handleResponse(oRequest, oRawResponse, oCallback, oCaller, tId);
1918 // Copy static members to FunctionDataSource class
1919 lang.augmentObject(util.FunctionDataSource, DS);
1933 /****************************************************************************/
1934 /****************************************************************************/
1935 /****************************************************************************/
1938 * ScriptNodeDataSource class for accessing remote data via the YUI Get Utility.
1940 * @namespace YAHOO.util
1941 * @class YAHOO.util.ScriptNodeDataSource
1942 * @extends YAHOO.util.DataSourceBase
1944 * @param oLiveData {HTMLElement} Pointer to live data.
1945 * @param oConfigs {object} (optional) Object literal of configuration values.
1947 util.ScriptNodeDataSource = function(oLiveData, oConfigs) {
1948 this.dataType = DS.TYPE_SCRIPTNODE;
1949 oLiveData = oLiveData || "";
1951 util.ScriptNodeDataSource.superclass.constructor.call(this, oLiveData, oConfigs);
1954 // ScriptNodeDataSource extends DataSourceBase
1955 lang.extend(util.ScriptNodeDataSource, DS, {
1957 /////////////////////////////////////////////////////////////////////////////
1959 // ScriptNodeDataSource public properties
1961 /////////////////////////////////////////////////////////////////////////////
1964 * Alias to YUI Get Utility, to allow implementers to use a custom class.
1966 * @property getUtility
1968 * @default YAHOO.util.Get
1970 getUtility : util.Get,
1973 * Defines request/response management in the following manner:
1975 * <!--<dt>queueRequests</dt>
1976 * <dd>If a request is already in progress, wait until response is returned before sending the next request.</dd>
1977 * <dt>cancelStaleRequests</dt>
1978 * <dd>If a request is already in progress, cancel it before sending the next request.</dd>-->
1979 * <dt>ignoreStaleResponses</dt>
1980 * <dd>Send all requests, but handle only the response for the most recently sent request.</dd>
1982 * <dd>Send all requests and handle all responses.</dd>
1985 * @property asyncMode
1987 * @default "allowAll"
1989 asyncMode : "allowAll",
1992 * Callback string parameter name sent to the remote script. By default,
1993 * requests are sent to
1994 * <URI>?<scriptCallbackParam>=callbackFunction
1996 * @property scriptCallbackParam
1998 * @default "callback"
2000 scriptCallbackParam : "callback",
2003 /////////////////////////////////////////////////////////////////////////////
2005 // ScriptNodeDataSource public methods
2007 /////////////////////////////////////////////////////////////////////////////
2010 * Creates a request callback that gets appended to the script URI. Implementers
2011 * can customize this string to match their server's query syntax.
2013 * @method generateRequestCallback
2014 * @return {String} String fragment that gets appended to script URI that
2015 * specifies the callback function
2017 generateRequestCallback : function(id) {
2018 return "&" + this.scriptCallbackParam + "=YAHOO.util.ScriptNodeDataSource.callbacks["+id+"]" ;
2022 * Overridable method gives implementers access to modify the URI before the dynamic
2023 * script node gets inserted. Implementers should take care not to return an
2026 * @method doBeforeGetScriptNode
2027 * @param {String} URI to the script
2028 * @return {String} URI to the script
2030 doBeforeGetScriptNode : function(sUri) {
2035 * Overriding method passes query to Get Utility. The returned
2036 * response is then forwarded to the handleResponse function.
2038 * @method makeConnection
2039 * @param oRequest {Object} Request object.
2040 * @param oCallback {Object} Callback object literal.
2041 * @param oCaller {Object} (deprecated) Use oCallback.scope.
2042 * @return {Number} Transaction ID.
2044 makeConnection : function(oRequest, oCallback, oCaller) {
2045 var tId = DS._nTransactionId++;
2046 this.fireEvent("requestEvent", {tId:tId,request:oRequest,callback:oCallback,caller:oCaller});
2048 // If there are no global pending requests, it is safe to purge global callback stack and global counter
2049 if(util.ScriptNodeDataSource._nPending === 0) {
2050 util.ScriptNodeDataSource.callbacks = [];
2051 util.ScriptNodeDataSource._nId = 0;
2054 // ID for this request
2055 var id = util.ScriptNodeDataSource._nId;
2056 util.ScriptNodeDataSource._nId++;
2058 // Dynamically add handler function with a closure to the callback stack
2060 util.ScriptNodeDataSource.callbacks[id] = function(oRawResponse) {
2061 if((oSelf.asyncMode !== "ignoreStaleResponses")||
2062 (id === util.ScriptNodeDataSource.callbacks.length-1)) { // Must ignore stale responses
2064 // Try to sniff data type if it has not been defined
2065 if(oSelf.responseType === DS.TYPE_UNKNOWN) {
2066 if(YAHOO.lang.isArray(oRawResponse)) { // array
2067 oSelf.responseType = DS.TYPE_JSARRAY;
2070 else if(oRawResponse.nodeType && oRawResponse.nodeType == 9) {
2071 oSelf.responseType = DS.TYPE_XML;
2073 else if(oRawResponse.nodeName && (oRawResponse.nodeName.toLowerCase() == "table")) { // table
2074 oSelf.responseType = DS.TYPE_HTMLTABLE;
2076 else if(YAHOO.lang.isObject(oRawResponse)) { // json
2077 oSelf.responseType = DS.TYPE_JSON;
2079 else if(YAHOO.lang.isString(oRawResponse)) { // text
2080 oSelf.responseType = DS.TYPE_TEXT;
2084 oSelf.handleResponse(oRequest, oRawResponse, oCallback, oCaller, tId);
2089 delete util.ScriptNodeDataSource.callbacks[id];
2092 // We are now creating a request
2093 util.ScriptNodeDataSource._nPending++;
2094 var sUri = this.liveData + oRequest + this.generateRequestCallback(id);
2095 sUri = this.doBeforeGetScriptNode(sUri);
2096 this.getUtility.script(sUri,
2098 onsuccess: util.ScriptNodeDataSource._bumpPendingDown,
2099 onfail: util.ScriptNodeDataSource._bumpPendingDown});
2106 // Copy static members to ScriptNodeDataSource class
2107 lang.augmentObject(util.ScriptNodeDataSource, DS);
2109 // Copy static members to ScriptNodeDataSource class
2110 lang.augmentObject(util.ScriptNodeDataSource, {
2112 /////////////////////////////////////////////////////////////////////////////
2114 // ScriptNodeDataSource private static properties
2116 /////////////////////////////////////////////////////////////////////////////
2119 * Unique ID to track requests.
2129 * Counter for pending requests. When this is 0, it is safe to purge callbacks
2132 * @property _nPending
2140 * Global array of callback functions, one for each request sent.
2142 * @property callbacks
2163 /****************************************************************************/
2164 /****************************************************************************/
2165 /****************************************************************************/
2168 * XHRDataSource class for accessing remote data via the YUI Connection Manager
2171 * @namespace YAHOO.util
2172 * @class YAHOO.util.XHRDataSource
2173 * @extends YAHOO.util.DataSourceBase
2175 * @param oLiveData {HTMLElement} Pointer to live data.
2176 * @param oConfigs {object} (optional) Object literal of configuration values.
2178 util.XHRDataSource = function(oLiveData, oConfigs) {
2179 this.dataType = DS.TYPE_XHR;
2180 this.connMgr = this.connMgr || util.Connect;
2181 oLiveData = oLiveData || "";
2183 util.XHRDataSource.superclass.constructor.call(this, oLiveData, oConfigs);
2186 // XHRDataSource extends DataSourceBase
2187 lang.extend(util.XHRDataSource, DS, {
2189 /////////////////////////////////////////////////////////////////////////////
2191 // XHRDataSource public properties
2193 /////////////////////////////////////////////////////////////////////////////
2196 * Alias to YUI Connection Manager, to allow implementers to use a custom class.
2200 * @default YAHOO.util.Connect
2205 * Defines request/response management in the following manner:
2207 * <dt>queueRequests</dt>
2208 * <dd>If a request is already in progress, wait until response is returned
2209 * before sending the next request.</dd>
2211 * <dt>cancelStaleRequests</dt>
2212 * <dd>If a request is already in progress, cancel it before sending the next
2215 * <dt>ignoreStaleResponses</dt>
2216 * <dd>Send all requests, but handle only the response for the most recently
2217 * sent request.</dd>
2220 * <dd>Send all requests and handle all responses.</dd>
2224 * @property connXhrMode
2226 * @default "allowAll"
2228 connXhrMode: "allowAll",
2231 * True if data is to be sent via POST. By default, data will be sent via GET.
2233 * @property connMethodPost
2237 connMethodPost: false,
2240 * The connection timeout defines how many milliseconds the XHR connection will
2241 * wait for a server response. Any non-zero value will enable the Connection Manager's
2242 * Auto-Abort feature.
2244 * @property connTimeout
2250 /////////////////////////////////////////////////////////////////////////////
2252 // XHRDataSource public methods
2254 /////////////////////////////////////////////////////////////////////////////
2257 * Overriding method passes query to Connection Manager. The returned
2258 * response is then forwarded to the handleResponse function.
2260 * @method makeConnection
2261 * @param oRequest {Object} Request object.
2262 * @param oCallback {Object} Callback object literal.
2263 * @param oCaller {Object} (deprecated) Use oCallback.scope.
2264 * @return {Number} Transaction ID.
2266 makeConnection : function(oRequest, oCallback, oCaller) {
2268 var oRawResponse = null;
2269 var tId = DS._nTransactionId++;
2270 this.fireEvent("requestEvent", {tId:tId,request:oRequest,callback:oCallback,caller:oCaller});
2272 // Set up the callback object and
2273 // pass the request in as a URL query and
2274 // forward the response to the handler
2276 var oConnMgr = this.connMgr;
2277 var oQueue = this._oQueue;
2280 * Define Connection Manager success handler
2282 * @method _xhrSuccess
2283 * @param oResponse {Object} HTTPXMLRequest object
2286 var _xhrSuccess = function(oResponse) {
2287 // If response ID does not match last made request ID,
2288 // silently fail and wait for the next response
2289 if(oResponse && (this.connXhrMode == "ignoreStaleResponses") &&
2290 (oResponse.tId != oQueue.conn.tId)) {
2293 // Error if no response
2294 else if(!oResponse) {
2295 this.fireEvent("dataErrorEvent", {request:oRequest, response:null,
2296 callback:oCallback, caller:oCaller,
2297 message:DS.ERROR_DATANULL});
2299 // Send error response back to the caller with the error flag on
2300 DS.issueCallback(oCallback,[oRequest, {error:true}], true, oCaller);
2304 // Forward to handler
2306 // Try to sniff data type if it has not been defined
2307 if(this.responseType === DS.TYPE_UNKNOWN) {
2308 var ctype = (oResponse.getResponseHeader) ? oResponse.getResponseHeader["Content-Type"] : null;
2311 if(ctype.indexOf("text/xml") > -1) {
2312 this.responseType = DS.TYPE_XML;
2314 else if(ctype.indexOf("application/json") > -1) { // json
2315 this.responseType = DS.TYPE_JSON;
2317 else if(ctype.indexOf("text/plain") > -1) { // text
2318 this.responseType = DS.TYPE_TEXT;
2322 this.handleResponse(oRequest, oResponse, oCallback, oCaller, tId);
2327 * Define Connection Manager failure handler
2329 * @method _xhrFailure
2330 * @param oResponse {Object} HTTPXMLRequest object
2333 var _xhrFailure = function(oResponse) {
2334 this.fireEvent("dataErrorEvent", {request:oRequest, response: oResponse,
2335 callback:oCallback, caller:oCaller,
2336 message:DS.ERROR_DATAINVALID});
2338 // Backward compatibility
2339 if(lang.isString(this.liveData) && lang.isString(oRequest) &&
2340 (this.liveData.lastIndexOf("?") !== this.liveData.length-1) &&
2341 (oRequest.indexOf("?") !== 0)){
2344 // Send failure response back to the caller with the error flag on
2345 oResponse = oResponse || {};
2346 oResponse.error = true;
2347 DS.issueCallback(oCallback,[oRequest,oResponse],true, oCaller);
2353 * Define Connection Manager callback object
2355 * @property _xhrCallback
2356 * @param oResponse {Object} HTTPXMLRequest object
2359 var _xhrCallback = {
2360 success:_xhrSuccess,
2361 failure:_xhrFailure,
2365 // Apply Connection Manager timeout
2366 if(lang.isNumber(this.connTimeout)) {
2367 _xhrCallback.timeout = this.connTimeout;
2370 // Cancel stale requests
2371 if(this.connXhrMode == "cancelStaleRequests") {
2372 // Look in queue for stale requests
2374 if(oConnMgr.abort) {
2375 oConnMgr.abort(oQueue.conn);
2383 // Get ready to send the request URL
2384 if(oConnMgr && oConnMgr.asyncRequest) {
2385 var sLiveData = this.liveData;
2386 var isPost = this.connMethodPost;
2387 var sMethod = (isPost) ? "POST" : "GET";
2389 var sUri = (isPost || !lang.isValue(oRequest)) ? sLiveData : sLiveData+oRequest;
2390 var sRequest = (isPost) ? oRequest : null;
2392 // Send the request right away
2393 if(this.connXhrMode != "queueRequests") {
2394 oQueue.conn = oConnMgr.asyncRequest(sMethod, sUri, _xhrCallback, sRequest);
2396 // Queue up then send the request
2398 // Found a request already in progress
2400 var allRequests = oQueue.requests;
2401 // Add request to queue
2402 allRequests.push({request:oRequest, callback:_xhrCallback});
2404 // Interval needs to be started
2405 if(!oQueue.interval) {
2406 oQueue.interval = setInterval(function() {
2407 // Connection is in progress
2408 if(oConnMgr.isCallInProgress(oQueue.conn)) {
2412 // Send next request
2413 if(allRequests.length > 0) {
2415 sUri = (isPost || !lang.isValue(allRequests[0].request)) ? sLiveData : sLiveData+allRequests[0].request;
2416 sRequest = (isPost) ? allRequests[0].request : null;
2417 oQueue.conn = oConnMgr.asyncRequest(sMethod, sUri, allRequests[0].callback, sRequest);
2419 // Remove request from queue
2420 allRequests.shift();
2424 clearInterval(oQueue.interval);
2425 oQueue.interval = null;
2431 // Nothing is in progress
2433 oQueue.conn = oConnMgr.asyncRequest(sMethod, sUri, _xhrCallback, sRequest);
2438 // Send null response back to the caller with the error flag on
2439 DS.issueCallback(oCallback,[oRequest,{error:true}],true,oCaller);
2447 // Copy static members to XHRDataSource class
2448 lang.augmentObject(util.XHRDataSource, DS);
2462 /****************************************************************************/
2463 /****************************************************************************/
2464 /****************************************************************************/
2467 * Factory class for creating a BaseDataSource subclass instance. The sublcass is
2468 * determined by oLiveData's type, unless the dataType config is explicitly passed in.
2470 * @namespace YAHOO.util
2471 * @class YAHOO.util.DataSource
2473 * @param oLiveData {HTMLElement} Pointer to live data.
2474 * @param oConfigs {object} (optional) Object literal of configuration values.
2476 util.DataSource = function(oLiveData, oConfigs) {
2477 oConfigs = oConfigs || {};
2479 // Point to one of the subclasses, first by dataType if given, then by sniffing oLiveData type.
2480 var dataType = oConfigs.dataType;
2482 if(dataType == DS.TYPE_LOCAL) {
2483 lang.augmentObject(util.DataSource, util.LocalDataSource);
2484 return new util.LocalDataSource(oLiveData, oConfigs);
2486 else if(dataType == DS.TYPE_XHR) {
2487 lang.augmentObject(util.DataSource, util.XHRDataSource);
2488 return new util.XHRDataSource(oLiveData, oConfigs);
2490 else if(dataType == DS.TYPE_SCRIPTNODE) {
2491 lang.augmentObject(util.DataSource, util.ScriptNodeDataSource);
2492 return new util.ScriptNodeDataSource(oLiveData, oConfigs);
2494 else if(dataType == DS.TYPE_JSFUNCTION) {
2495 lang.augmentObject(util.DataSource, util.FunctionDataSource);
2496 return new util.FunctionDataSource(oLiveData, oConfigs);
2500 if(YAHOO.lang.isString(oLiveData)) { // strings default to xhr
2501 lang.augmentObject(util.DataSource, util.XHRDataSource);
2502 return new util.XHRDataSource(oLiveData, oConfigs);
2504 else if(YAHOO.lang.isFunction(oLiveData)) {
2505 lang.augmentObject(util.DataSource, util.FunctionDataSource);
2506 return new util.FunctionDataSource(oLiveData, oConfigs);
2508 else { // ultimate default is local
2509 lang.augmentObject(util.DataSource, util.LocalDataSource);
2510 return new util.LocalDataSource(oLiveData, oConfigs);
2514 // Copy static members to DataSource class
2515 lang.augmentObject(util.DataSource, DS);
2519 /****************************************************************************/
2520 /****************************************************************************/
2521 /****************************************************************************/
2524 * The static Number class provides helper functions to deal with data of type
2527 * @namespace YAHOO.util
2532 YAHOO.util.Number = {
2535 * Takes a native JavaScript Number and formats to string for display to user.
2538 * @param nData {Number} Number.
2539 * @param oConfig {Object} (Optional) Optional configuration values:
2541 * <dt>prefix {String}</dd>
2542 * <dd>String prepended before each number, like a currency designator "$"</dd>
2543 * <dt>decimalPlaces {Number}</dd>
2544 * <dd>Number of decimal places to round.</dd>
2545 * <dt>decimalSeparator {String}</dd>
2546 * <dd>Decimal separator</dd>
2547 * <dt>thousandsSeparator {String}</dd>
2548 * <dd>Thousands separator</dd>
2549 * <dt>suffix {String}</dd>
2550 * <dd>String appended after each number, like " items" (note the space)</dd>
2551 * <dt>negativeFormat</dt>
2552 * <dd>String used as a guide for how to indicate negative numbers. The first '#' character in the string will be replaced by the number. Default '-#'.</dd>
2554 * @return {String} Formatted number for display. Note, the following values
2555 * return as "": null, undefined, NaN, "".
2557 format : function(n, cfg) {
2558 if (!isFinite(+n)) {
2562 n = !isFinite(+n) ? 0 : +n;
2563 cfg = YAHOO.lang.merge(YAHOO.util.Number.format.defaults, (cfg || {}));
2565 var neg = n < 0, absN = Math.abs(n),
2566 places = cfg.decimalPlaces,
2567 sep = cfg.thousandsSeparator,
2571 // Get rid of the decimal info
2572 s = absN - (absN % 1) + '';
2573 i = s.length + places;
2575 // avoid 123 vs decimalPlaces -4 (should return "0")
2577 // leverage toFixed by making 123 => 0.123 for the rounding
2578 // operation, then add the appropriate number of zeros back on
2579 s = Number('.' + s).toFixed(i).slice(2) +
2580 new Array(s.length - i + 1).join('0');
2584 } else { // There is a bug in IE's toFixed implementation:
2585 // for n in {(-0.94, -0.5], [0.5, 0.94)} n.toFixed() returns 0
2586 // instead of -1 and 1. Manually handle that case.
2587 s = absN < 1 && absN >= 0.5 && !places ? '1' : absN.toFixed(places);
2591 bits = s.split(/\D/);
2592 i = bits[0].length % 3 || 3;
2594 bits[0] = bits[0].slice(0,i) +
2595 bits[0].slice(i).replace(/(\d{3})/g, sep + '$1');
2597 s = bits.join(cfg.decimalSeparator);
2600 s = cfg.prefix + s + cfg.suffix;
2602 return neg ? cfg.negativeFormat.replace(/#/,s) : s;
2605 YAHOO.util.Number.format.defaults = {
2606 decimalSeparator : '.',
2607 decimalPlaces : null,
2608 thousandsSeparator : '',
2611 negativeFormat : '-#'
2615 /****************************************************************************/
2616 /****************************************************************************/
2617 /****************************************************************************/
2621 var xPad=function (x, pad, r)
2623 if(typeof r === 'undefined')
2627 for( ; parseInt(x, 10)<r && r>1; r/=10) {
2628 x = pad.toString() + x;
2630 return x.toString();
2635 * The static Date class provides helper functions to deal with data of type Date.
2637 * @namespace YAHOO.util
2644 a: function (d, l) { return l.a[d.getDay()]; },
2645 A: function (d, l) { return l.A[d.getDay()]; },
2646 b: function (d, l) { return l.b[d.getMonth()]; },
2647 B: function (d, l) { return l.B[d.getMonth()]; },
2648 C: function (d) { return xPad(parseInt(d.getFullYear()/100, 10), 0); },
2649 d: ['getDate', '0'],
2650 e: ['getDate', ' '],
2651 g: function (d) { return xPad(parseInt(Dt.formats.G(d)%100, 10), 0); },
2653 var y = d.getFullYear();
2654 var V = parseInt(Dt.formats.V(d), 10);
2655 var W = parseInt(Dt.formats.W(d), 10);
2659 } else if(W===0 && V>=52) {
2665 H: ['getHours', '0'],
2666 I: function (d) { var I=d.getHours()%12; return xPad(I===0?12:I, 0); },
2668 var gmd_1 = new Date('' + d.getFullYear() + '/1/1 GMT');
2669 var gmdate = new Date('' + d.getFullYear() + '/' + (d.getMonth()+1) + '/' + d.getDate() + ' GMT');
2670 var ms = gmdate - gmd_1;
2671 var doy = parseInt(ms/60000/60/24, 10)+1;
2672 return xPad(doy, 0, 100);
2674 k: ['getHours', ' '],
2675 l: function (d) { var I=d.getHours()%12; return xPad(I===0?12:I, ' '); },
2676 m: function (d) { return xPad(d.getMonth()+1, 0); },
2677 M: ['getMinutes', '0'],
2678 p: function (d, l) { return l.p[d.getHours() >= 12 ? 1 : 0 ]; },
2679 P: function (d, l) { return l.P[d.getHours() >= 12 ? 1 : 0 ]; },
2680 s: function (d, l) { return parseInt(d.getTime()/1000, 10); },
2681 S: ['getSeconds', '0'],
2682 u: function (d) { var dow = d.getDay(); return dow===0?7:dow; },
2684 var doy = parseInt(Dt.formats.j(d), 10);
2685 var rdow = 6-d.getDay();
2686 var woy = parseInt((doy+rdow)/7, 10);
2687 return xPad(woy, 0);
2690 var woy = parseInt(Dt.formats.W(d), 10);
2691 var dow1_1 = (new Date('' + d.getFullYear() + '/1/1')).getDay();
2692 // First week is 01 and not 00 as in the case of %U and %W,
2693 // so we add 1 to the final result except if day 1 of the year
2694 // is a Monday (then %W returns 01).
2695 // We also need to subtract 1 if the day 1 of the year is
2696 // Friday-Sunday, so the resulting equation becomes:
2697 var idow = woy + (dow1_1 > 4 || dow1_1 <= 1 ? 0 : 1);
2698 if(idow === 53 && (new Date('' + d.getFullYear() + '/12/31')).getDay() < 4)
2704 idow = Dt.formats.V(new Date('' + (d.getFullYear()-1) + '/12/31'));
2707 return xPad(idow, 0);
2711 var doy = parseInt(Dt.formats.j(d), 10);
2712 var rdow = 7-Dt.formats.u(d);
2713 var woy = parseInt((doy+rdow)/7, 10);
2714 return xPad(woy, 0, 10);
2716 y: function (d) { return xPad(d.getFullYear()%100, 0); },
2719 var o = d.getTimezoneOffset();
2720 var H = xPad(parseInt(Math.abs(o/60), 10), 0);
2721 var M = xPad(Math.abs(o%60), 0);
2722 return (o>0?'-':'+') + H + M;
2725 var tz = d.toString().replace(/^.*:\d\d( GMT[+-]\d+)? \(?([A-Za-z ]+)\)?\d*$/, '$2').replace(/[a-z ]/g, '');
2727 tz = Dt.formats.z(d);
2731 '%': function (d) { return '%'; }
2746 //'+': '%a %b %e %T %Z %Y'
2750 * Takes a native JavaScript Date and formats to string for display to user.
2753 * @param oDate {Date} Date.
2754 * @param oConfig {Object} (Optional) Object literal of configuration values:
2756 * <dt>format <String></dt>
2759 * Any strftime string is supported, such as "%I:%M:%S %p". strftime has several format specifiers defined by the Open group at
2760 * <a href="http://www.opengroup.org/onlinepubs/007908799/xsh/strftime.html">http://www.opengroup.org/onlinepubs/007908799/xsh/strftime.html</a>
2763 * PHP added a few of its own, defined at <a href="http://www.php.net/strftime">http://www.php.net/strftime</a>
2766 * This javascript implementation supports all the PHP specifiers and a few more. The full list is below:
2769 * <dt>%a</dt> <dd>abbreviated weekday name according to the current locale</dd>
2770 * <dt>%A</dt> <dd>full weekday name according to the current locale</dd>
2771 * <dt>%b</dt> <dd>abbreviated month name according to the current locale</dd>
2772 * <dt>%B</dt> <dd>full month name according to the current locale</dd>
2773 * <dt>%c</dt> <dd>preferred date and time representation for the current locale</dd>
2774 * <dt>%C</dt> <dd>century number (the year divided by 100 and truncated to an integer, range 00 to 99)</dd>
2775 * <dt>%d</dt> <dd>day of the month as a decimal number (range 01 to 31)</dd>
2776 * <dt>%D</dt> <dd>same as %m/%d/%y</dd>
2777 * <dt>%e</dt> <dd>day of the month as a decimal number, a single digit is preceded by a space (range ' 1' to '31')</dd>
2778 * <dt>%F</dt> <dd>same as %Y-%m-%d (ISO 8601 date format)</dd>
2779 * <dt>%g</dt> <dd>like %G, but without the century</dd>
2780 * <dt>%G</dt> <dd>The 4-digit year corresponding to the ISO week number</dd>
2781 * <dt>%h</dt> <dd>same as %b</dd>
2782 * <dt>%H</dt> <dd>hour as a decimal number using a 24-hour clock (range 00 to 23)</dd>
2783 * <dt>%I</dt> <dd>hour as a decimal number using a 12-hour clock (range 01 to 12)</dd>
2784 * <dt>%j</dt> <dd>day of the year as a decimal number (range 001 to 366)</dd>
2785 * <dt>%k</dt> <dd>hour as a decimal number using a 24-hour clock (range 0 to 23); single digits are preceded by a blank. (See also %H.)</dd>
2786 * <dt>%l</dt> <dd>hour as a decimal number using a 12-hour clock (range 1 to 12); single digits are preceded by a blank. (See also %I.) </dd>
2787 * <dt>%m</dt> <dd>month as a decimal number (range 01 to 12)</dd>
2788 * <dt>%M</dt> <dd>minute as a decimal number</dd>
2789 * <dt>%n</dt> <dd>newline character</dd>
2790 * <dt>%p</dt> <dd>either `AM' or `PM' according to the given time value, or the corresponding strings for the current locale</dd>
2791 * <dt>%P</dt> <dd>like %p, but lower case</dd>
2792 * <dt>%r</dt> <dd>time in a.m. and p.m. notation equal to %I:%M:%S %p</dd>
2793 * <dt>%R</dt> <dd>time in 24 hour notation equal to %H:%M</dd>
2794 * <dt>%s</dt> <dd>number of seconds since the Epoch, ie, since 1970-01-01 00:00:00 UTC</dd>
2795 * <dt>%S</dt> <dd>second as a decimal number</dd>
2796 * <dt>%t</dt> <dd>tab character</dd>
2797 * <dt>%T</dt> <dd>current time, equal to %H:%M:%S</dd>
2798 * <dt>%u</dt> <dd>weekday as a decimal number [1,7], with 1 representing Monday</dd>
2799 * <dt>%U</dt> <dd>week number of the current year as a decimal number, starting with the
2800 * first Sunday as the first day of the first week</dd>
2801 * <dt>%V</dt> <dd>The ISO 8601:1988 week number of the current year as a decimal number,
2802 * range 01 to 53, where week 1 is the first week that has at least 4 days
2803 * in the current year, and with Monday as the first day of the week.</dd>
2804 * <dt>%w</dt> <dd>day of the week as a decimal, Sunday being 0</dd>
2805 * <dt>%W</dt> <dd>week number of the current year as a decimal number, starting with the
2806 * first Monday as the first day of the first week</dd>
2807 * <dt>%x</dt> <dd>preferred date representation for the current locale without the time</dd>
2808 * <dt>%X</dt> <dd>preferred time representation for the current locale without the date</dd>
2809 * <dt>%y</dt> <dd>year as a decimal number without a century (range 00 to 99)</dd>
2810 * <dt>%Y</dt> <dd>year as a decimal number including the century</dd>
2811 * <dt>%z</dt> <dd>numerical time zone representation</dd>
2812 * <dt>%Z</dt> <dd>time zone name or abbreviation</dd>
2813 * <dt>%%</dt> <dd>a literal `%' character</dd>
2817 * @param sLocale {String} (Optional) The locale to use when displaying days of week,
2818 * months of the year, and other locale specific strings. The following locales are
2824 * <dd>US English</dd>
2826 * <dd>British English</dd>
2828 * <dd>Australian English (identical to British English)</dd>
2830 * More locales may be added by subclassing of YAHOO.util.DateLocale.
2831 * See YAHOO.util.DateLocale for more information.
2832 * @return {String} Formatted date for display.
2833 * @sa YAHOO.util.DateLocale
2835 format : function (oDate, oConfig, sLocale) {
2836 oConfig = oConfig || {};
2838 if(!(oDate instanceof Date)) {
2839 return YAHOO.lang.isValue(oDate) ? oDate : "";
2842 var format = oConfig.format || "%m/%d/%Y";
2844 // Be backwards compatible, support strings that are
2845 // exactly equal to YYYY/MM/DD, DD/MM/YYYY and MM/DD/YYYY
2846 if(format === 'YYYY/MM/DD') {
2847 format = '%Y/%m/%d';
2848 } else if(format === 'DD/MM/YYYY') {
2849 format = '%d/%m/%Y';
2850 } else if(format === 'MM/DD/YYYY') {
2851 format = '%m/%d/%Y';
2853 // end backwards compatibility block
2855 sLocale = sLocale || "en";
2857 // Make sure we have a definition for the requested locale, or default to en.
2858 if(!(sLocale in YAHOO.util.DateLocale)) {
2859 if(sLocale.replace(/-[a-zA-Z]+$/, '') in YAHOO.util.DateLocale) {
2860 sLocale = sLocale.replace(/-[a-zA-Z]+$/, '');
2866 var aLocale = YAHOO.util.DateLocale[sLocale];
2868 var replace_aggs = function (m0, m1) {
2869 var f = Dt.aggregates[m1];
2870 return (f === 'locale' ? aLocale[m1] : f);
2873 var replace_formats = function (m0, m1) {
2874 var f = Dt.formats[m1];
2875 if(typeof f === 'string') { // string => built in date function
2877 } else if(typeof f === 'function') { // function => our own function
2878 return f.call(oDate, oDate, aLocale);
2879 } else if(typeof f === 'object' && typeof f[0] === 'string') { // built in function with padding
2880 return xPad(oDate[f[0]](), f[1]);
2886 // First replace aggregates (run in a loop because an agg may be made up of other aggs)
2887 while(format.match(/%[cDFhnrRtTxX]/)) {
2888 format = format.replace(/%([cDFhnrRtTxX])/g, replace_aggs);
2891 // Now replace formats (do not run in a loop otherwise %%a will be replace with the value of %a)
2892 var str = format.replace(/%([aAbBCdegGHIjklmMpPsSuUVwWyYzZ%])/g, replace_formats);
2894 replace_aggs = replace_formats = undefined;
2900 YAHOO.namespace("YAHOO.util");
2901 YAHOO.util.Date = Dt;
2904 * The DateLocale class is a container and base class for all
2905 * localised date strings used by YAHOO.util.Date. It is used
2906 * internally, but may be extended to provide new date localisations.
2908 * To create your own DateLocale, follow these steps:
2910 * <li>Find an existing locale that matches closely with your needs</li>
2911 * <li>Use this as your base class. Use YAHOO.util.DateLocale if nothing
2913 * <li>Create your own class as an extension of the base class using
2914 * YAHOO.lang.merge, and add your own localisations where needed.</li>
2916 * See the YAHOO.util.DateLocale['en-US'] and YAHOO.util.DateLocale['en-GB']
2917 * classes which extend YAHOO.util.DateLocale['en'].
2919 * For example, to implement locales for French french and Canadian french,
2920 * we would do the following:
2922 * <li>For French french, we have no existing similar locale, so use
2923 * YAHOO.util.DateLocale as the base, and extend it:
2925 * YAHOO.util.DateLocale['fr'] = YAHOO.lang.merge(YAHOO.util.DateLocale, {
2926 * a: ['dim', 'lun', 'mar', 'mer', 'jeu', 'ven', 'sam'],
2927 * A: ['dimanche', 'lundi', 'mardi', 'mercredi', 'jeudi', 'vendredi', 'samedi'],
2928 * b: ['jan', 'fév', 'mar', 'avr', 'mai', 'jun', 'jui', 'aoû', 'sep', 'oct', 'nov', 'déc'],
2929 * B: ['janvier', 'février', 'mars', 'avril', 'mai', 'juin', 'juillet', 'août', 'septembre', 'octobre', 'novembre', 'décembre'],
2930 * c: '%a %d %b %Y %T %Z',
2938 * <li>For Canadian french, we start with French french and change the meaning of \%x:
2940 * YAHOO.util.DateLocale['fr-CA'] = YAHOO.lang.merge(YAHOO.util.DateLocale['fr'], {
2947 * With that, you can use your new locales:
2949 * var d = new Date("2008/04/22");
2950 * YAHOO.util.Date.format(d, {format: "%A, %d %B == %x"}, "fr");
2954 * mardi, 22 avril == 22.04.2008
2958 * YAHOO.util.Date.format(d, {format: "%A, %d %B == %x"}, "fr-CA");
2962 * mardi, 22 avril == 2008-04-22
2964 * @namespace YAHOO.util
2968 YAHOO.util.DateLocale = {
2969 a: ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'],
2970 A: ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'],
2971 b: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'],
2972 B: ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'],
2973 c: '%a %d %b %Y %T %Z',
2981 YAHOO.util.DateLocale['en'] = YAHOO.lang.merge(YAHOO.util.DateLocale, {});
2983 YAHOO.util.DateLocale['en-US'] = YAHOO.lang.merge(YAHOO.util.DateLocale['en'], {
2984 c: '%a %d %b %Y %I:%M:%S %p %Z',
2989 YAHOO.util.DateLocale['en-GB'] = YAHOO.lang.merge(YAHOO.util.DateLocale['en'], {
2992 YAHOO.util.DateLocale['en-AU'] = YAHOO.lang.merge(YAHOO.util.DateLocale['en']);
2996 YAHOO.register("datasource", YAHOO.util.DataSource, {version: "2.8.0r4", build: "2449"});