/**
 * Created by zlj on 2017/3/24.
 */

/**
 * 在容器内根据列表数据生成列表项的方法函数
 *
 * ===--参数说明--===
 * @containerId, 页面中盛放小列表项目的容器id
 * @smallBlockContainerId, 盛放小列表项字符串的<script/>标签的id
 * @dataObj, 生成列表项所需的列表数据,为对象数组形式[{prop:value},{},...]
 * @replaceSetter, 设置列表数据内数据项 与 列表小项字符串的关联(用于替换数据) 比如对象数组内的对象{name:''} name 属性对应小列表字符串内#name#
 * @listStart, 使用列表数据的起始项index（从0 开始）
 * @listCount,显示的列表的数量
 * @isAppend 生成的列表项是否追加到表项显示容器 true 则直接追加到所有现有列表项内容之后  false 则清空后在添加列表项到显示容器
 * */
function producePage(containerId, smallBlockContainerId, dataObj, replaceSetter, listStart, listCount, isAppend) {

    // console.log(">>>>>>>containerId" + containerId);
    //console.log(">>>>>>>smallBlockContainerId" + smallBlockContainerId);
    //console.log(">>>>>>>dataObj" + JSON.stringify(dataObj));
    //console.log(">>>>>>>replaceSetter" + JSON.stringify(replaceSetter));
    //console.log(">>>>>>>listStart" + listStart);
    //console.log(">>>>>>>listCount" + listCount);
    //console.log(">>>>>>>isAppend" + isAppend);
    var pageContainer = document.getElementById(containerId);
    var blockStr = document.getElementById(smallBlockContainerId).innerHTML;
    var listItem = null;
    var readData = "";
    var replaceReg = null;
    var allListString = ""; //一次性添加到页面（避免页面多次刷新）

    if (!isAppend) { //不是在原有内容上追加
        pageContainer.innerHTML = "";
    }

    //控制输入参数生成的值，对输入值验证
    listStart = Math.max(0, Math.min(listStart, dataObj.length - 1));
    var repeatCount = listStart + listCount;

    repeatCount = Math.max(0, Math.min(repeatCount, dataObj.length));

    for (var i = listStart; i < repeatCount; i++) {
        listItem = dataObj[i];
        listItem.article_type = article_type ? article_type : 5; //添加当前列表项一个新闻类型属性 //article_type为blade页面声明的全局变量
        if (listItem) {
            // console.log(">>>>>>>str"+typeof listItem );
            var smallBlockStr = blockStr; //复制一份列表字符串
            for (var prop in replaceSetter) {
                readData = listItem[prop];//读取需要的数据
                replaceReg = new RegExp(replaceSetter[prop], "g");//需要替换的内容
                smallBlockStr = smallBlockStr.replace(replaceReg, readData);
            }
            allListString += smallBlockStr;
            //console.log(">>>>>>>str"+allListString );
        }
    }
    pageContainer.innerHTML += allListString
}

