
/*
* infoGet 扩展 
* Date  : 2020-02-20
* author:lith

    
 */

/* 
    IgAttr数据格式DEMO为:
    var demo =  {      'ig_class': 'Text', 
                       'ig_id': '名称',   'ig_param': {xx:'xx'}    };

 
*/


; (function (infoGet) {
  

    /// <param name="errorList" type="Array">存放错误列表</param>
    infoGet.errorList = [];
     

    

    function WidgetCache() {
        // dom  -> widget
        var widgetMap = new Map();
   
        this.clear = function () {
            widgetMap.clear();
        };

        this.getWidgetByElem = function (elem) {
            return widgetMap.get(elem)
        };
        this.removeWidget = function (elem) {
            widgetMap.delete(elem);
        }
        this.addWidget = function (elem, widget) {
            widgetMap.set(elem, widget);
        }
        //callback:  function(widget,elem){}
        this.each = function (callback) {
            widgetMap.forEach(callback);
        };
    }

    var widgetCache =  new WidgetCache();
     

    //infoGet.getElems = function () {
    //    return $('*[ig_class]');
    //};


    infoGet.setMode = function (mode) {
        widgetCache.each(function (widget, elem) {
            widget.setMode(mode);
        });
    }

    infoGet.disable = function () {
        widgetCache.each(function (widget, elem) {
            widget.disable();
        });
    }
 

    //获取Widget控件
    infoGet.getWidget = function (elem) {
 
        if (!elem) return null;      
    
        var widget = widgetCache.getWidgetByElem(elem);
        if (!widget) {
            var je = $(elem);
            var igAttr = infoGet.getIgAttrFromJe(je);
            var className = igAttr['ig_class'];
            with (infoGet.widget) {
                var widgetClass = eval(className);
            }
            if ('function' != typeof (widgetClass)) return null;

            widget = new widgetClass(je, igAttr);
            widgetCache.addWidget(elem, widget);
        }
        return widget;
    }
 

    //通过igId获取控件
    infoGet.getWidgetByIgId = function (igId) { 
        return infoGet.getWidget($('*[ig_id="' + toJsStr("" + igId) + '"]')[0]);
    };



    infoGet.parse = function (jqParent) {
        var jqElems = (jqParent || $(document)).find('*[ig_class]');

        jqElems.each(function (i, elem) {
            infoGet.getWidget(elem);
        });
    };


    infoGet.buildHtml = function (igAttr) {

        var className = igAttr['ig_class'];
        with (infoGet.widget) {
            var widgetClass = eval(className);
        }
        if ('function' != typeof (widgetClass)) return null;
        return widgetClass.buildHtml(igAttr);
    }


    //获取dom控件ig参数
    infoGet.getIgAttrFromJe =  function getIgAttrFromJe(je) {        
        function getAttrEval(je, attrName) {
            try {
                var value = je.attr(attrName);
                return eval('(' + value + ')');
            } catch (e) {
                infoGet.errorList.push(e);
                console.log(e);
            }
            return null;
        }

        var igAttr = {
            'ig_class': je.attr('ig_class'),
            'ig_id': je.attr('ig_id'),
            'ig_param': getAttrEval(je, 'ig_param') || {}             
        };    
         
        return igAttr;
    };


    /// <field name='toXmlStr' type='fucntion'>向xml转换</field>
    function toXmlMinStr(str) {
        /// <summary> 向xml转换。
        /// 例如 转换  <a title=''>ok</a>  中title的值。    
        /// 转换     &  < > 单引号     为      &amp;  &lt; &gt; &#39;
        /// </summary>          
        /// <returns type="String">转换后的字符串</returns>
        return str.replace(/\&/g, "&amp;").replace(/\</g, "&lt;").replace(/\>/g, "&gt;").replace(/\'/g, "&#39;");
    };


    function toJsStr(str) {
        /// <summary> 向javascript的字符串转换。
        /// <para>例如转换为javascript 代码  var str="";  中str对象所赋的值（以引号包围）。 </para>   
        /// <para>转换   \b \t \n \f \r \" \' \\ 为 \\b \\t \\n \\f \\r \\" \\' \\\\        </para>
        /// </summary>          
        /// <returns type="string">转换后的字符串</returns>
        return str.replace(/\\/g, "\\\\").replace(/\x08/g, "\\b").replace(/\t/g, "\\t").replace(/\n/g, "\\n").replace(/\f/g, "\\f").replace(/\r/g, "\\r").replace(/\"/g, "\\\"").replace(/\'/g, "\\\'");
    };

    infoGet.getHtmlCtrl_attr = function (attrs) {
         
        var html = '';
        for (var name in attrs) {
            var item = attrs[name];
            if (!item) continue;
            html += ' ' + name + '=\'';
            html += toXmlMinStr('object' == typeof (item) ? JSON.stringify(item) : ('' + item)); 
            html += '\' ';
        }
        return html;
    };   
     
   
 
   

 

})(window.infoGet || (window.infoGet = {}));