1 /*
  2  * ***** BEGIN LICENSE BLOCK *****
  3  * Zimbra Collaboration Suite Web Client
  4  * Copyright (C) 2006, 2007, 2009, 2010, 2013, 2014, 2016 Synacor, Inc.
  5  *
  6  * The contents of this file are subject to the Common Public Attribution License Version 1.0 (the "License");
  7  * you may not use this file except in compliance with the License.
  8  * You may obtain a copy of the License at: https://www.zimbra.com/license
  9  * The License is based on the Mozilla Public License Version 1.1 but Sections 14 and 15
 10  * have been added to cover use of software over a computer network and provide for limited attribution
 11  * for the Original Developer. In addition, Exhibit A has been modified to be consistent with Exhibit B.
 12  *
 13  * Software distributed under the License is distributed on an "AS IS" basis,
 14  * WITHOUT WARRANTY OF ANY KIND, either express or implied.
 15  * See the License for the specific language governing rights and limitations under the License.
 16  * The Original Code is Zimbra Open Source Web Client.
 17  * The Initial Developer of the Original Code is Zimbra, Inc.  All rights to the Original Code were
 18  * transferred by Zimbra, Inc. to Synacor, Inc. on September 14, 2015.
 19  *
 20  * All portions of the code are Copyright (C) 2006, 2007, 2009, 2010, 2013, 2014, 2016 Synacor, Inc. All Rights Reserved.
 21  * ***** END LICENSE BLOCK *****
 22  */
 23 
 24 /**
 25  * Minimal wrapper around XHR, with no dependencies.
 26  * 
 27  * @author Andy Clark
 28  * 
 29  * @private
 30  */
 31 AjxLoader = function() {}
 32 
 33 //
 34 // Data
 35 //
 36 
 37 AjxLoader.__createXHR;
 38 
 39 if (window.XMLHttpRequest) {
 40     AjxLoader.__createXHR = function() { return new XMLHttpRequest(); };
 41 }
 42 else if (window.ActiveXObject) {
 43     (function(){
 44         var vers = ["MSXML2.XMLHTTP.4.0", "MSXML2.XMLHTTP.3.0", "MSXML2.XMLHTTP", "Microsoft.XMLHTTP"];
 45         for (var i = 0; i < vers.length; i++) {
 46             try {
 47                 new ActiveXObject(vers[i]);
 48                 AjxLoader.__createXHR = function() { return new ActiveXObject(vers[i]); };
 49                 break;
 50             }
 51             catch (e) {
 52                 // ignore
 53             }
 54         }
 55     })();
 56 }
 57 
 58 //
 59 // Static functions
 60 //
 61 
 62 /**
 63  * This function uses XHR to load and return the contents at an arbitrary URL.
 64  * <p>
 65  * It can be called with either a URL string or a parameters object.
 66  *
 67  * @param	{hash}		urlOrParams			a hash of parameters
 68  * @param {string}	urlOrParams.url       the URL to load
 69  * @param {string}	[urlOrParams.method]    the load method (e.g. "GET").
 70  *                                  If this parameter is not specified, then
 71  *                                  the value is determined by whether content
 72  *                                  has been specified: "POST" if specified,
 73  *                                  "GET" otherwise.
 74  * @param {hash}	[urlOrParams.headers]  the map of request headers to set.
 75  * @param {boolean}	[urlOrParams.async]     determines whether the request
 76  *                                  is asynchronous or synchronous. If this
 77  *                                  parameter is not specified, then the value
 78  *                                  is determined by whether a callback has
 79  *                                  been specified: async if a callback is
 80  *                                  specified, sync if no callback.
 81  * @param {string}	[urlOrParams.content]   the content to POST to URL. If not specified, the request method is GET.
 82  * @param {string}	[urlOrParams.userName]  the username of the request.
 83  * @param {string}	[urlOrParams.password]  the password of the request.
 84  * @param {AjxCallback}	[urlOrParams.callback]  the callback to run at end of load.
 85  */
 86 AjxLoader.load = function(urlOrParams) {
 87     var params = urlOrParams;
 88     if (typeof urlOrParams == "string") {
 89         params = { url: urlOrParams };
 90     }
 91 
 92     var req = AjxLoader.__createXHR();
 93     var func = Boolean(params.callback) ? function() { AjxLoader._response(req, params.callback); } : null;
 94     var method = params.method || (params.content != null ? "POST" : "GET");
 95 	
 96 	if (func) {
 97 	    req.onreadystatechange = func;
 98 	}
 99     var async = params.async != null ? params.async : Boolean(func);
100     req.open(method, params.url, async, params.userName, params.password);
101     for (var name in params.headers) {
102         req.setRequestHeader(name, params.headers[name]);
103     }
104     req.send(params.content || "");
105 
106     return req;
107 };
108 
109 AjxLoader._response = function(req, callback) {
110     if (req.readyState == 4) {
111         callback.run(req);
112     }
113 };
114