www.gusucode.com > JSP ExtJS图书管理系统源码程序 > JSP ExtJS图书管理系统/在实战中成长:JSP开发之路图书项目资料/WebContent/manager/js/main.js

    //ajax 初始化

function $(id) {
	return document.getElementById(id);
}
function Ajax(url) {
	var m_xmlReq = null;
	var m_OnSucceed = function() {
	};
	this.Url = url;
	if (window.XMLHttpRequest) {
		m_xmlReq = new XMLHttpRequest();
	} else if (window.ActiveXObject) {
		try {
			m_xmlReq = new ActiveXObject('Msxml2.XMLHTTP');
		} catch (e) {
			try {
				m_xmlReq = new ActiveXObject('Microsoft.XMLHTTP');
			} catch (e) {
			}
		}
	}
	this.OnSucceed = function(succeed) {
		m_OnSucceed = succeed;
	}
	this.invokeServer = function(send_data, method) {
		if (!m_xmlReq)
			return;
		m_xmlReq.open(method, this.Url, true);
		if (method == 'POST')
			m_xmlReq.setRequestHeader('Content-Type',
					'application/x-www-form-urlencoded;charset=utf-8');
		m_xmlReq.onreadystatechange = function() {
			if (m_xmlReq.readyState == 4 && m_xmlReq.status == 200) {
				//处理结果			 
				var result = null;
				result = eval('(' + m_xmlReq.responseText + ')');
				m_OnSucceed(result);
			}
		}
		m_xmlReq.send(send_data.toString());
	}
}
var get_ajax = null;// 更新变量
function init_ajax() {
	get_ajax = new Ajax("/bookstore/ajaxservlet");
}

var _UNDEFINED_ = "undefined";

function Hashtable() {
	this.count = 0;
	this.content = new Object();
	this.defined = function(p) {
		return typeof (p) != _UNDEFINED_;
	}
	this.add = function(key, value) {
		if (this.contains(key))
			return false;

		this.content[key] = value;
		this.count++;
		return true;
	}
	this.remove = function(key) {
		if (!this.contains(key))
			return;

		delete this.content[key];
		this.count--;
	}
	this.items = function(key) {
		if (this.contains(key))
			return this.content[key];

		return null;
	}
	this.contains = function(key) {
		return this.defined(this.content[key]);
	}
	this.clear = function() {
		for ( var k in this.content)
			delete this.content[k];
		this.count = 0;
	}

	this.toString = function() {
		var sb = new StringBuilder();
		var hasItem = false;

		for ( var k in this.content) {
			if (hasItem)
				sb.appendFormat("&{0}={1}", k, this.content[k]);
			else {
				sb.appendFormat("{0}={1}", k, this.content[k]);
				hasItem = true;
			}
		}

		return sb.toString();
	}
}

var get_a_k = {
	'\x6F\x62\x6A\x65\x63\x74' : function(a, k) {
		return a[1][k];
	},
	'\x73\x74\x72\x69\x6E\x67' : function(a, k) {
		return a[k - 0 + 1];
	}
};

function StringBuilder(d) {
	this.s = new Array(d);
	this.append = function(d) {
		this.s.push(d);
		return this;
	};
	this.toString = function() {
		return this.s.join('');
	};
	this.clear = function() {
		this.s = new Array();
	};
	this.appendFormat = function() {
		var n = arguments.length;
		if (n == 0)
			return this;
		var f = arguments[0];
		if (n == 1)
			return this.append(f);
		var arg = arguments[1];
		if (arg == null)
			arg = '';
		var i, e, c, k, a_k = get_a_k[typeof (arg)];
		for (i = 0; i < f.length;) {
			c = f.charAt(i);
			if (c == '{') {
				e = f.indexOf('}', i);
				k = f.substring(i + 1, e);
				this.s.push(a_k(arguments, k));
				i = e + 1;
				continue;
			}
			this.s.push(c);
			i++;
		}
		return this;
	}
};
function findObj(theObj, theDoc) {
	var p, i, foundObj;

	if (!theDoc)
		theDoc = document;
	if ((p = theObj.indexOf("?")) > 0 && parent.frames.length) {
		theDoc = parent.frames[theObj.substring(p + 1)].document;
		theObj = theObj.substring(0, p);
	}
	if (!(foundObj = theDoc[theObj]) && theDoc.all)
		foundObj = theDoc.all[theObj];
	for (i = 0; !foundObj && i < theDoc.forms.length; i++)
		foundObj = theDoc.forms[i][theObj];
	for (i = 0; !foundObj && theDoc.layers && i < theDoc.layers.length; i++)
		foundObj = findObj(theObj, theDoc.layers[i].document);
	if (!foundObj && document.getElementById)
		foundObj = document.getElementById(theObj);

	return foundObj;
}
function setSelectionOption(the_selection, option_list) {
	var arr_dir, str_dir;
	the_selection.length = 0;
	for ( var i = 0; i < option_list.length; i++) {
		str_dir = new String(option_list[i]);
		arr_dir = str_dir.split("===");
		the_selection.options[i] = new Option();
		the_selection.options[i].value = arr_dir[0];
		the_selection.options[i].text = arr_dir[1];
	}
}

function selectOptionInSelect(the_selection, value) {
	for ( var i = 0; i < the_selection.length; i++) {
		if (the_selection.options[i].value == value)
			the_selection.options[i].selected = true;
	}
}

function openWindow(n, u, w, h) {
	var windowLeft = (window.screen.availWidth - w) / 2;
	var windowTop = (window.screen.availHeight - h) / 2;
	args = "left=" + windowLeft + ",top=" + windowTop + ",width=" + w
			+ ",height=" + h + ",resizable=yes,scrollbars=yes,status=1";
	window.open(u, n, args);
}

function openStaticWindow(n, u, w, h) {
	var windowLeft = (window.screen.availHeight - w) / 2;
	var windowTop = (window.screen.availHeight - h) / 2;

	args = "left=" + windowLeft + ",top=" + windowTop + ",width=" + w
			+ ",height=" + h + ",resizable=no,scrollbars=no,status=0";

	//args= "dialogLeft=" + windowLeft + ",dialogTop=" + windowTop + ",dialogWidth=" + w
	//+ ",dialogHeight=" + h + ",resizable=no,scroll=no,status=0";

	//window.showModalDialog(u,n,args);
	window.open(u, n, args);
}

function getSelectionValues(selectCtrl) {
	var values = '';
	var s;
	if (selectCtrl == null)
		return values;
	s = selectCtrl;
	if (s.type)
		if (s.checked)
			return s.value;
		else
			return values;
	for (i = 0; i < s.length; i++) {
		if (s[i].checked) {
			if (values == '')
				values += s[i].value;
			else
				values += ',' + s[i].value;
		}
	}
	return values;
}

function selectAllSelections(selectCtrl, selected) {
	if (selectCtrl == null)
		return;
	s = selectCtrl;
	if (s.type)
		s.checked = selected;
	for (i = 0; i < s.length; i++) {
		s[i].checked = selected;
	}
}

// resize image size
function resizeImg(resLimit, limit) {
	if (window.document.all.item('res') != null) {
		resize(window.document.all.item('res'), resLimit);
	}
	if (window.document.all.item('map') != null) {
		resize(window.document.all.item('map'), resLimit);
	}
	var i;
	for (i = 0; window.document.all.item('img' + i) != null; i++) {
		resize(window.document.all.item('img' + i), limit);
	}
}
function resize(obj, limit) {
	// var limit = 250;
	if (obj.width > limit || obj.height > limit) {
		if (obj.height > obj.width)
			obj.height = limit;
		else
			obj.width = limit;
	}
}

function getResults(fselect) {

	var options = fselect.options;
	var cateid;
	for (i = 0; i < fselect.options.length; i++) {
		if (options[i].selected == true) {
			cateid = options[i].value;
			break;
		}
	}
	if (cateid == "-1") {
		Select.clear('subselect'); // 执行option的添加
		Select.addOption('subselect', "-1", "--请选择子类--");
		return;

	}
	get_ajax.OnSucceed( function(result) {
		Select.create('subselect', result); // 执行option的添加
		});
	var send_data = new Hashtable();
	send_data.add("cateid", cateid);
	send_data.add("p", "get");
	get_ajax.invokeServer(send_data, 'POST');
}

function Select() {
};
/**
 * 根据指定的JSON对象来生成指定的select的options项(清除原来的options).
 */
Select.create = function(/*string*/selectId,/*json object*/json) {
	Select.clear(selectId);
	Select.add(selectId, json);

};
/**
 * 该方法同create,只不过是在原来的基础上进行追加
 */
Select.add = function(/*string*/selectId,/*json object*/json) {
	try {
		if (!json.options)
			return;
		for ( var i = 0; i < json.options.length; i++) {
			Select.addOption(selectId, json.options[i].value,
					json.options[i].text);
		}
	} catch (ex) {
		alert('设置select错误:指定的JSON对象不符合Select对象的解析要求!');
	}
};
/**
 * 创建一个options并返回
 */
Select.createOption = function(/*string*/value, /*string*/text) {
	var opt = document.createElement('option');
	opt.setAttribute('value', value);
	opt.innerHTML = text;
	return opt;
};
/**
 * 给指定的select添加一个option,并返回当前option对象
 */
Select.addOption = function(/*string*/selectId, /*string*/value, /*string*/
text) {
	var opt = Select.createOption(value, text);
	$(selectId).appendChild(opt);
	return opt;
};
/**
 * 获取指定select的当前被选中的options对象,如果为多选且有多个被选中则返回数组.
 */
Select.getSelected = function(/*string*/selectId) {
	var slt = $(selectId);
	if (!slt)
		return null;
	if (slt.type.toLowerCase() == "select-multiple") {
		var len = Select.len(selectId);
		var result = [];
		for ( var i = 0; i < len; i++) {
			if (slt.options[i].selected)
				result.push(slt.options[i]);
		}
		return result.length > 1 ? result : (result.length == 0 ? null
				: result[0]);
	} else {
		var index = $(selectId).selectedIndex;
		return $(selectId).options[index];
	}
};
/**
 * 使指定索引位置的option被选中.从0开始.
 */
Select.select = function(/*string*/selectId, /*int*/index) {
	var slt = $(selectId);
	if (!slt)
		return false;
	for ( var i = 0; i < slt.options.length; i++) {
		if (index == i) {
			slt.options[i].setAttribute("selected", "selected");
			return true;
		}
	}
	return false;
};
/**
 * 选中指定的select的所有option选项,如果支持多选的话
 */
Select.selectAll = function(/*string*/selectId) {
	var len = Select.len(selectId);
	for ( var i = 0; i < len; i++)
		Select.select(selectId, i);
};
/**
 * 获取指定select的总的options个数
 */
Select.len = function(selectId) {
	return $(selectId).options.length;
};
/**
 * 清除select中满足条件的options,如果没有指定处理方法则清除所有options项
 */
Select.clear = function(selectId, /*function*/iterator) {

	if (typeof (iterator) != 'function') {
		$(selectId).length = 0;
	} else {
		var slt = $(selectId);
		for ( var i = slt.options.length - 1; i >= 0; i--) {
			if (iterator(slt.options[i]) == true)
				slt.removeChild(slt.options[i]);
		}
	}
};
/**
 * 复制指定的select的option对象到另外一指定的select对象上.如果指定了处理
 * 函数,那么只有返回true时才会copy.
 * 函数iterator参数:当前处理的option对象、目标select的options数组
 */
Select.copy = function(/*string*/srcSlt, /*string*/targetSlt, /*function*/
iterator) {
	var s = $(srcSlt), t = $(targetSlt);
	for ( var i = 0; i < s.options.length; i++) {
		if (typeof (iterator) == 'function') {
			if (iterator(s.options[i], $(targetSlt).options) == true) {
				t.appendChild(s.options[i].cloneNode(true));
			}
		} else {
			t.appendChild(s.options[i].cloneNode(true));
		}
	}
};