;(function (window, undefined) { var treeView = tools.$('#treeView'); var fileData = data; // 初始化 treeView.innerHTML = treeHtml(fileData, 0); // console.log(treeHtml(fileData,-1)); // 事件 var fileItem = tools.$('.treeNode'); var root_icon = tools.$('.icon-control', fileItem[0])[0]; root_icon.className = 'icon icon-control icon-minus'; tools.each(fileItem, function (item) { filesHandle(item); }); function treeHtml(fileData, fileId) { var _html = ''; var children = getChildById(fileData, fileId); var hideChild = fileId > 0 ? 'none' : ''; _html += '
'; return _html; }; function filesHandle(item) { tools.addEvent(item, 'click', function () { var treeNode_cur = tools.$('.treeNode-cur')[0]; var fileId = item.dataset.fileId; var curElem = document.querySelector('.treeNode[data-file-id="' + fileId + '"]'); var hasChild = hasChilds(fileData, fileId); var icon_control = tools.$('.icon-control', item)[0]; var openStatus = true; if (hasChild) { tools.removeClass(treeNode_cur, 'treeNode-cur'); tools.addClass(curElem, 'treeNode-cur'); openStatus = tools.toggleClass(item.nextElementSibling, 'none'); if (openStatus) { icon_control.className = 'icon icon-control icon-add'; } else { icon_control.className = 'icon icon-control icon-minus'; } } }); }; })(window);