(function ($, B) {    
    /**
     * 相关 Web 界面的类集合
     * @module Beyondbit.Controls
     * @submodule Controls
     */
	var controls = B.registerNameSpace("Controls");

	var c = controls;

	/**
      * 表单属性
      * @class Form
      * @constructor
      * @param {JSON} options 控件属性
      * @example
      *    
      */
	c.Form = function (options) {
		options = initOptions(options);
		
		var $form = $(options.target);

		bindEvent($form, options);
	};

    /**
      * 创建控件
      * @method create
      * @param {JSON} options 控件属性 参考Form属性
      * @example
      *
      *     B.Controls.Form.create({
      *         target:"#formID"
      *     })
      *    
      */
	c.Form.create = function (options) {
	    return new c.Form(options);
	}

	function bindEvent($form, options) {
		var regulate = bindRegulate($form, options.regulate);

		$form.submit(function () {
		    if (!$form.valid) {
		        loadingFormSubmit($form, regulate, options);
		        return;
		    }

		    if ($form.valid()) {
		        loadingFormSubmit($form, regulate, options);
		    }
		});
	}

	function loadingFormSubmit($form, regulate, options) {
	    if (options.isSubmitShowLoading == true) {
	        B.Web.MessageBox.loading(null, options.loadingMessage);
	    }

	    if (regulate != null) {
	        regulate.disabled();
	    }
	}

	function bindRegulate($form, regulateOptions)
	{
	    var regulate;

		if (regulateOptions && $.type(regulateOptions) === "string") {
			regulate = $form.Regulate(regulateOptions);
		}
		return regulate;
	}

	function initOptions(options){

	    var commonRegional = B.CultureInfo.Current.getCommonRegional();

		var newOptions = $.extend(true, {
		  /**
			* 控制按钮
			* @property regulate
			* @type B.Controls.Regulate
			*/
			regulate: null,
			target: null,
		  /**
			* 提交数据时是否显示Loading窗口
			* @property isSubmitShowLoading
			* @type Bool
			*/
			isSubmitShowLoading: true,
			loadingMessage: commonRegional.someThingSaving.bformat(commonRegional.data)
		}, {}, options);

		validateOptions(newOptions);

		return newOptions;
	}

	function validateOptions(options) {
		validateOption(options, "target", null, "{0}参数必填");
	}

	function validateOption(options, key, notExpect, message) {
		if (options[key] == notExpect) {
			throw new Error((message || "参数配置不正确").bformat(key));
		}
	}

})(jQuery, Beyondbit);

    
Top