Index: branches/RC/core/admin_templates/js/ajax.js =================================================================== diff -u -r9603 -r9639 --- branches/RC/core/admin_templates/js/ajax.js (.../ajax.js) (revision 9603) +++ branches/RC/core/admin_templates/js/ajax.js (.../ajax.js) (revision 9639) @@ -75,6 +75,15 @@ } } +Request.processRedirect = function($request) { + var $match_redirect = new RegExp('^#redirect#(.*)').exec($request.responseText); + if ($match_redirect != null) { + // redirect to external template requested + window.location.href = $match_redirect[1]; + return true; + } + return false; +} Request.sendHeaders = function($request) { for (var $header_name in Request.headers) { if (typeof Request.headers[$header_name] == 'function') { @@ -325,4 +334,110 @@ AjaxPopupManager.prototype.errorCallback = function($request, $params, $object) { alert('AJAX Error; class: AjaxPopupManager; ' + Request.getErrorHtml($request)); +} +// AJAX DropdownPreloader class +function AjaxDropdownPreloader($url, $input_mask, $filter_field, $dependend_field, value) { + this.URL = $url; + this.InputMask = $input_mask; + this.FilterField = $filter_field; + this.DependendField = $dependend_field; + this.Titles = this.prepareTitles(); + this.Value = value; + this.BusyRequest = false; +} +AjaxDropdownPreloader.prototype.prepareURL = function() +{ + return this.URL.replace('#DEPENDEND#', this.Dependend).replace('#FILTER_VALUE#', this.getValue(this.FilterField)); +} +AjaxDropdownPreloader.prototype.prepareTitles = function() { + var $control = this.getControl(this.DependendField); + var $i = 0; + var $ret = new Array (); + while ($i < $control.options.length) { + $ret[$control.options[$i].value] = $control.options[$i].innerHTML; + $i++; + } + return $ret; +} +AjaxDropdownPreloader.prototype.getValue = function($field_name) { + var $control = this.getControl($field_name); + if ($control.tagName == 'INPUT') return $control.value; + return $control.selectedIndex > 0 ? $control.options[$control.selectedIndex].value : ''; +} +AjaxDropdownPreloader.prototype.Query = function () { + var $url = this.prepareURL(); + var $selected_value = this.Value || this.getValue(this.DependendField); + // remove all existing options + this.removeOptions(); + Request.makeRequest($url, this.BusyRequest, '', this.successCallback, this.errorCallback, $selected_value, this); +} +AjaxDropdownPreloader.prototype.getControl = function($field) { + var $id = this.InputMask.replace('#FIELD#', $field); + return document.getElementById($id); +} +AjaxDropdownPreloader.prototype.successCallback = function($request, $params, $object) { + if (Request.processRedirect($request) === true) { + return ; + } + var control = $object.getControl($object.DependendField) + $object.ProcessXMLNode($request.responseXML, control, $params); + runOnChange(control); + $object.AfterProcess(); +} +AjaxDropdownPreloader.prototype.ProcessXMLNode = function($node, $dst_field, $selected_value) { + for (var i = 0; i < $node.childNodes.length; i++) { + var $child = $node.childNodes.item(i); + switch ($child.tagName) { + case 'option': + var opt_value = $child.getAttribute('value'); + var title; + if (opt_value) { // value is passed explicically + title = $child.firstChild.nodeValue + } + else { + opt_value = $child.firstChild.nodeValue; + title = this.Titles[$child.firstChild.nodeValue]; + } + this.addOption($dst_field, opt_value, title, $child.attributes); + if (opt_value == $selected_value) { + $dst_field.options[$dst_field.options.length - 1].selected = true; + } + break; + case 'field_options': + this.addOption($dst_field, '', ''); + // add new states + this.ProcessXMLNode($child, $dst_field, $selected_value); + if ($dst_field.options.length == 0 || $dst_field.options.length == 2) { + $dst_field.value = $dst_field.options[$dst_field.options.length - 1].value; + } + break; + } + } +} +AjaxDropdownPreloader.prototype.AfterProcess = function() +{ +} +AjaxDropdownPreloader.prototype.removeOptions = function($object) { + if (!$object) $object = this.getControl(this.DependendField); + if ($object.options.length > 0) { + while ($object.options.length > 0) { + $object.remove(0); + } + } +} +AjaxDropdownPreloader.prototype.addOption = function($object, $value, $title, attributes) { + var $option = document.createElement('OPTION'); + $object.options.add($option, $object.options.length); + $option.innerText = $title; + $option.innerHTML = $title; + $option.value = $value; + if (attributes) { + for (var i=0; i