/*
 * 特别声明：本技术材料受《中华人民共和国著作权法》、《计算机软件保护条例》等法律、法规、行政
 * 规章以及有关国际条约的保护，赞同科技享有知识产权、保留一切权利并视其为技术秘密。未经本公司书
 * 面许可，任何人不得擅自（包括但不限于：以非法的方式复制、传播、展示、镜像、上载、下载）使用，
 * 不得向第三方泄露、透露、披露。否则，本公司将依法追究侵权者的法律责任。特此声明！
 *
 * Special Declaration: These technical material reserved as the technical secrets by AGREE
 * TECHNOLOGY have been protected by the "Copyright Law" "ordinances on Protection of Computer
 * Software" and other relevant administrative regulations and international treaties. Without
 * the written permission of the Company, no person may use (including but not limited to the
 * illegal copy, distribute, display, image, upload, and download) and disclose the above
 * technical documents to any third party. Otherwise, any infringer shall afford the legal
 * liability to the company.
 */

/**
 * Created by huang.dy on 2016/04/08 15:59
 */
define(["adoreapp/widget/BaseWidget"], function (BaseWidget) {
    return BaseWidget.extend(function (base) {
        return {
            _initWidget: function (attrs) {
                this.jqueryNode = $(attrs.template).attr({id: this.id});
                this.cssPaths = [];
            },

            _html: function (attrs) {
                if (attrs.html) {
                    this.jqueryNode.html(attrs.html);
                }
            },
            /**
             * htmlFragments:[{'fragmentID':'xxx','html':'*'},...]
             */
            _setHtmlFragments: function (attrs) {
                var htmls = attrs.htmlFragments;
                if (htmls && htmls.length > 0) {
                    var tempArr = [];
                    for (var i = 0; i < htmls.length; i++) {
                        var fragmentNode = this.jqueryNode.find("#" + htmls[i].fragmentID);
                        if (fragmentNode.length > 0) {
                            fragmentNode.html(htmls[i].html);
                        } else {
                            tempArr.push(htmls[i]);
                        }
                    }
                    //最多尝试2次(若DOM还没准备好，tempArr就可能不为空，需要继续执行剩下的tempArr，下同)
                    var count = attrs.count ? 0 : attrs.count;
                    if (tempArr.length > 0 && count <= 2) {
                        setTimeout(this._setHtmlFragments({htmlFragments: tempArr, count: (count + 1)}), 200);
                    }
                }
            },

            /**
             * images:[{'imageID':'xxx','imagePath':'*'},...]
             */
            _setImages: function (attrs) {
                var images = attrs.images;
                if (images && images.length > 0) {
                    var tempArr = [];
                    for (var i = 0; i < images.length; i++) {
                        var imageNode = this.jqueryNode.find("#" + images[i].imageID);
                        if (imageNode.length > 0) {
                            imageNode.attr("src", images[i].imagePath);
                        } else {
                            tempArr.push(images[i]);
                        }
                    }
                    //最多尝试2次
                    var count = attrs.count ? 0 : attrs.count;
                    if (tempArr.length > 0 && count <= 2) {
                        setTimeout(this._setImages({images: tempArr, count: (count + 1)}), 200);
                    }
                }
            },

            /**
             * texts:[{'textID':'xxx','text':'*'},...]
             */
            _setTexts: function (attrs) {
                var texts = attrs.texts;
                if (texts && texts.length > 0) {
                    var tempArr = [];
                    for (var i = 0; i < texts.length; i++) {
                        var textNode = this.jqueryNode.find("#" + texts[i].textID);
                        if (textNode.length > 0) {
                            textNode.text(texts[i].text);
                        } else {
                            tempArr.push(texts[i]);
                        }
                    }
                    //最多尝试2次
                    var count = attrs.count ? 0 : attrs.count;
                    if (tempArr.length > 0 && count <= 2) {
                        setTimeout(this._setTexts({texts: tempArr, count: (count + 1)}), 200);
                    }
                }
            },

            /**
             * cssPaths:[{'cssID':'xxx','cssPath':'*.css'},...]
             */
            _setCssPaths: function (attrs) {
                var cssPaths = attrs.cssPaths;
                if (cssPaths && cssPaths.length > 0) {
                    var headNode = $("head");
                    for (var i = 0; i < cssPaths.length; i++) {
                        if (headNode.find("#" + cssPaths[i].cssID).length === 0) {
                            $("<link href='" + cssPaths[i].cssPath + "' type='text/css' rel='stylesheet'>").attr("id", cssPaths[i].cssID).appendTo(headNode);
                            this.cssPaths.push(cssPaths[i].cssID);
                        }
                    }

                }
            },

            _dispose: function () {
                base._dispose.call(this);
                var headNode = $("head");
                for (var i = 0; i < this.cssPaths.length; i++) {
                    headNode.find("#" + this.cssPaths[i]).remove();
                }
                this.cssPaths.splice(0, this.cssPaths.length);//删除
            }

        };
    });
});

