//xmlRequest - объект реализующий механизм связи в AJAX //by Bach //Объект xmlRequest //Свойства: // cStatus - статус объекта: free - свободен => может быть осуществлён запрос // busy - занят => не может быть осуществлён запрос // aQueue - массив - очередь запросов. В этот массив помещаются вновь поступившие запросы // если в момент поступления объект был занят // aButtons - массив кнопок который необходимо отключить при обработке запросов // (сделано для предотвращения нажатия например на кнопки "Сохранить", "Отменить" // до конца обработке запроса) // intervalID - идентификатор таймера производящего опрос объёкта XMLHttpRequest на пердмет занятости // и подставляющая новый запрос из очереди если объект освобождается // таймер включается сразу как только появляется хотя бы один запрос в очереди и // выключатеся при отправри последнего запроса из очереди на исполнение // oXmlHttpRequest - объёкт XMLHttpRequest //ДЛЯ РАБОТЫ НЕОБХОДИМ ehtms.js var xmlRequest=new Object(); xmlRequest.cStatus='free'; xmlRequest.aQueue=new Array(); xmlRequest.aButtons=new Array(); xmlRequest.intervalID=-1; xmlRequest.userWaitFunc=null; oXmlHttpRequest = null; xmlRequest.init = function (buttons, pUserWaitFunc) { var i; if (buttons) { for (i in buttons) { if (buttons[i]) xmlRequest.aButtons[i] = buttons[i]; } } this.userWaitFunc = pUserWaitFunc; }; xmlRequest.setUserWaitFunc = function(pFunc) { userWaitFunc = pFunc; } xmlRequest.setButtonsDisable = function (ldisabled) { var i; if (xmlRequest.aButtons) { for (i in xmlRequest.aButtons) xmlRequest.aButtons[i].disabled = ldisabled; } }; xmlRequest.doJob = function () { var oParameters; if (this.isFree()) { if (oParameters = this.aQueue.shift()) { this.startQuery(oParameters.url, oParameters.Parameters, oParameters.Handler, oParameters.handlerParams, oParameters.UserWaitFunc); } } }; xmlRequest.setBusy = function () { this.setButtonsDisable(true); this.cStatus = 'busy'; }; xmlRequest.setFree = function () { this.setButtonsDisable(false); this.cStatus = 'free'; }; xmlRequest.isFree = function () { if (this.cStatus == 'free') return 1; return 0; }; xmlRequest.standardHandler = function (pUserHandler, pHandlerParams, pUserWaitFunc) { var odiv, otxt; if (!oXmlHttpRequest) return; if (oXmlHttpRequest.readyState == 4) { if (oXmlHttpRequest.status == 200) { if (oXmlHttpRequest.responseXML) { if (oXmlHttpRequest.responseXML.documentElement) { pUserHandler(oXmlHttpRequest.responseXML.documentElement, pHandlerParams); } } } xmlRequest.HideWaitBox(pUserWaitFunc); this.setFree(); } else { xmlRequest.ShowWaitBox(pUserWaitFunc); } } xmlRequest.loadXMLDoc = function (cUrl, cParameters, pHandler, pHandlerParams, pUserWaitFunc) { oXmlHttpRequest = null; if (window.XMLHttpRequest) { oXmlHttpRequest = new XMLHttpRequest(); } else if (window.ActiveXObject) { oXmlHttpRequest = new ActiveXObject("Microsoft.XMLHTTP"); } if (oXmlHttpRequest) { oXmlHttpRequest.onreadystatechange = function () { xmlRequest.standardHandler(pHandler, pHandlerParams, pUserWaitFunc); }; oXmlHttpRequest.open('POST', cUrl); oXmlHttpRequest.setRequestHeader('Content-Type','application/x-www-form-urlencoded;'); oXmlHttpRequest.setRequestHeader('Charset','koi8-r'); oXmlHttpRequest.setRequestHeader('Accept-Language', 'ru, en'); oXmlHttpRequest.setRequestHeader('Accept-Charset', 'koi8-r'); oXmlHttpRequest.send(cParameters); } } xmlRequest.startQuery = function (cUrl, cParameters, pHandler, pHandlerParams, pUserWaitFunc) { if (this.isFree()) { this.setBusy(); this.loadXMLDoc(cUrl, cParameters, pHandler, pHandlerParams, pUserWaitFunc); if (this.aQueue.length==0) { clearInterval(xmlRequest.intervalID); this.intervalID=-1; } } else { this.aQueue.push({url:cUrl,Parameters:cParameters,Handler:pHandler,UserWaitFunc:pUserWaitFunc,handlerParams:pHandlerParams}); if (this.intervalID==-1) this.intervalID=setInterval('xmlRequest.doJob()',5); xmlRequest.ShowWaitBox(pUserWaitFunc); } } xmlRequest.ShowWaitBox = function (pUserWaitFunc) { var obox; if (pUserWaitFunc) { pUserWaitFunc('show'); } else if (this.userWaitFunc) { this.userWaitFunc('show'); } else if (obox = getItemByID('xmlRequestWaitBox')) { obox.style.display = ''; } else { if (!(odiv = getItemByID('xmlHttpRequestFlag'))) odiv = div('xmlHttpRequestFlag'); otxt = bold('Wait, please...'); fillObj(odiv, [otxt]); odiv.style.padding = '5px'; odiv.style.background = '#aebed6'; odiv.style.border = 'solid 1px black'; odiv.style.position = 'absolute'; //odiv.style.top = '50px'; odiv.style.top = '50%'; odiv.style.left = '50%'; odiv.style.display = ''; document.body.appendChild(odiv); odiv.style.left = screen.width / 2 - odiv.offsetWidth; } } xmlRequest.HideWaitBox = function (pUserWaitFunc) { var obox; if (pUserWaitFunc) { pUserWaitFunc('hide'); } else if (this.userWaitFunc) { this.userWaitFunc('hide'); } else if (obox = getItemByID('xmlRequestWaitBox')) { obox.style.display = 'none'; } else { if (odiv = getItemByID('xmlHttpRequestFlag')) odiv.style.display = 'none'; } } xmlRequest.init();