﻿/*
* jquery.renderhtml.js ver 0.10
*
*
* Written and maintained by Highmore Hill (highmore@163.com)
*/

;(function($) {
	if (/1\.(0|1|2)\.(0|1|2)/.test($.fn.jquery) || /^1.1/.test($.fn.jquery)) {
		alert("RenderHtml 需要 jQuery v1.2.3以上版本! 您使用的版本是 v" + $.fn.jquery);
		return;
	}

	$.fn.RenderHtml = function(options) {

		if (this.length === 0) {
			//alert("没有指定要处理的元素！");
			return this;
		}
		var opts = $.extend({}, $.fn.RenderHtml.defaults, options);

		return this.each(function(i, e) {
			var $this	= $(e);
			var _id		= $this.attr("id");
			var $status	= (opts.status == "self") ? $this : $(opts.status);

			var MODE	= $this.data("MODE");
			var PARS	= {
				object		: $this.data("OBJECT"),
				action		: $this.data("ACTION"),
				template	: $this.data("TEMPLATE"),
				condition	: $this.data("CONDITION"),
				pagemarker	: $this.data("PAGEMARKER")
			}

			if (PARS==undefined || PARS==null) {return;}

			var processFunction	= $.fn.RenderHtml.renderFunction[$this.data("RENDERFUNCTION") ? $this.data("RENDERFUNCTION") : opts.renderFunction];
			processFunction.begin($status);

			function getAjax($e, pars, mode) {
				var datatype;
				if (mode==2) {
					datatype = "json";
				} else {
					datatype = "html";
				}
				pars.mode = mode;

				return $.ajax({
					url: opts.url, data: pars, type: "GET", dataType: datatype, cache: false, global: false, async: true, processData: true, contentType: "application/x-www-form-urlencoded", crossDomain: false, 
					//username	: "highmore",
					//password	: "123456",
					context		: $e,
					beforeSend	: function(xhr) {
						xhr.setRequestHeader("If-Modified-Since","0");
						xhr.setRequestHeader("Cache-Control","no-cache");
					}
				});
			}

			switch (MODE) {
				case "server"		:
					$.when(getAjax($this, PARS, 0)).then(
						function(html) {processFunction.pass(html, $this);},
						function(o) {$this.html("获取元素[" + _id + "]的HTML内容时发生错误!");}
					);
					break;
				case "local"		:
					$.when(getAjax($this, PARS, 1), getAjax($this, PARS, 2)).then(
						function(t, d) {
							var data = ($this.data("EXTEND_DATA")==undefined) ? d[0] : $.extend(true, {}, d[0], $this.data("EXTEND_DATA"));
							var template = t[0];

							data._MODIFIERS = myModifiers;
							processFunction.pass(template.process(data, {throwExceptions:true}), $this);
							$this.removeData("EXTEND_DATA");
						},
						function(xhr, d){
							alert("错误");
						}
					);
					break;
				case "reqtemplate"	:
					$.when(getAjax($this, PARS, 1)).then(
						function(template) {
							var data = $this.data("LOCAL_DATA") ? $this.data("LOCAL_DATA") : {};

							data._MODIFIERS = myModifiers;
							processFunction.pass(template.process(data, {throwExceptions:true}), $this);
						},
						function(o) {
							$this.html("获取元素[" + _id + "]的模版时发生错误!");
						}
					);
					break;
				case "reqdata"	:
					$.when(getAjax($this, PARS, 2)).then(
						function(json) {
							return json;
						},
						function(o) {
							$this.html("获取元素[" + _id + "]的模版时发生错误!");
						}
					);
					break;
				default				:
					alert("渲染元素[" + _id + "]时没有指定 MODE 参数！");
					break;
			}
		});
	}

/*
********************************************************************************************************
*
* plugin options defaults
*
********************************************************************************************************
*/
	$.fn.RenderHtml.version = "0.11";
	$.fn.RenderHtml.defaults = {
		url				: "renderhtml.asp",
		status			: "self",
		statusText		: "",
		renderFunction	: "replaceContainerHtml"
	}

})(jQuery);


