$(function () { $(".header .nav .nav-li").eq(9).addClass("ac").find(".dropdown-menu li:eq(0)").addClass('ac'); var tagsInput = $(".tagsInput"), indefiniteList = $(".indefinite-list"), list = $(".sf-typeHelper-list"), inp = $(".sf-typeHelper-input"), addTag = $("#addTag"), taglist = $("#taglist"); taglist.find(".tagPopup").each(function (i, v) { $(v).css({ "zIndex": (10000 - i) }); }); tagsInput.on("click", function (event) { inp.focus(); }); var inputCompleteFlag = true; inp.on('compositionstart', function () { inputCompleteFlag = false; }); inp.on('compositionend', function () { inputCompleteFlag = true; }); inp.on("keyup", function (e) { if (indefiniteList.children().length >= 5) { inp.val(""); /* alert("一次最多只能添加五个");*/ return; } if (inputCompleteFlag) { var keywords = $(this).val(), positionLeft = $(this).position().left; if (keywords == '') { list.empty().hide(); return; } if (list.children("li").length != 0) { if (e.keyCode == 13) { list.find("li.ac a").trigger("click"); return; } else if (e.keyCode == 38) { if (!list.find("li.ac").prev('li').length == 0) { list.find("li.ac").removeClass('ac').prev('li').addClass('ac'); } return; } else if (e.keyCode == 40) { if (!list.find("li.ac").next('li').length == 0) { list.find("li.ac").removeClass('ac').next('li').addClass('ac'); } return; } } $.ajax({ url: 'GetTagsByKey?wd=' + keywords, dataType: 'json', jsonp: 'cb', beforeSend: function () { list.append('
加载中...
'); }, success: function (data) { list.empty().show().css({ "left": positionLeft - 5 }); if (data.length == 0) { list.append('
暂无标签 "' + keywords + '"
'); return; } var str = ""; $.each(data, function (i, v) { if (i == 0) { str += '
  • ' + this.name + '
  • '; } else { str += '
  • ' + this.name + '
  • '; } }); list.append(str); }, error: function () { list.empty().show(); list.append('
    暂无标签 "' + keywords + '"
    '); } }); } }); list.on("click", "li a", function (e) { e.stopPropagation(); var text = $(this).text(); var tagid = $(this).attr("tagid"); var existFlag = false; indefiniteList.find(".labelname").each(function (i, v) { if ($(v).text().trim() == text.trim()) { existFlag = true; } }); taglist.find(".tagPopup a").each(function (i, v) { if ($(v).text().trim() == text.trim()) { existFlag = true; } }); // taglist if (existFlag) { layer.msg("要关注的标签已存在"); return; } indefiniteList.append(' ' + text + ' × '); list.empty().hide(); inp.val(""); }); indefiniteList.on("click", ".remove", function (e) { e.stopPropagation(); $(this).parent(".sf-typeHelper-item").remove(); }); addTag.on("click", function () { $.get("../Account/IsLogin", function (data) { if (data) { if (indefiniteList.children().length == 0) { layer.msg("请先选择标签"); return; } else { var str = ""; var orderno = parseInt(taglist.find(".operate").last().children().eq(0).attr("orderno") || "0"); //debugger; var ids = []; indefiniteList.children().each(function (i, v) { var text = $(v).find(".labelname").text(); var tagid = parseInt($(v).find(".labelname").attr("tagid")); ids.push(tagid); str += '
  • ' + text + '
    上移
    下移
    取消关注
  • '; }); $.post("SaveUserTag", { ids: ids.join(",") }, function () { taglist.append(str); indefiniteList.empty(); taglist.find(".tagPopup").each(function (i, v) { $(v).css({ "zIndex": (10000 - i) }); }); }); } } else { ShowLogin(); //layer.confirm("登录后才能继续操作,是否前去登录?", { icon: 3, title: '提示' }, // function (index) {//确定回调 // layer.close(index); // ShowLogin(); // }, function (index) {//取消回调 // layer.close(index); // } //); } }); }); taglist.on("click", ".fa-ellipsis-v", function (e) { e.stopPropagation(); $(this).parents(".tagPopup").eq(0).siblings(".tagPopup").find(".operate").hide(); $(this).siblings('.operate').slideDown(); }); $(document).on("click", function () { taglist.find(".operate").slideUp(); }); var flag = true; taglist.on("click", " .moveUp", function (e) { if (flag) { flag = false; e.stopPropagation(); var prevP = $(this).parents(".tagPopup").prev(".tagPopup").eq(0), thisP = $(this).parents(".tagPopup").eq(0), thisH = thisP.html(); if (!prevP.length == 0) { var thisOrderno = parseInt($(this).attr("orderno")), thisId = parseInt($(this).attr("tagid")), prevOrderno = parseInt(prevP.find(".moveUp").attr("orderno")), prevId = parseInt(prevP.find(".moveUp").attr("tagid")); $.post("ChangeUserTagOrder", { id1: thisId, order1: prevOrderno, id2: prevId, order2: thisOrderno }, function () { thisP.html(prevP.html()).find(".moveUp,.moveDown").attr("orderno", thisOrderno); prevP.html(thisH).find(".moveUp,.moveDown").attr("orderno", prevOrderno); taglist.find(".operate").hide(); flag = true; }) } else flag = true; } //taglist.find(".operate").hide(); }); taglist.on("click", " .moveDown", function (e) { if (flag) { flag = false; e.stopPropagation(); var nextP = $(this).parents(".tagPopup").next(".tagPopup").eq(0), thisP = $(this).parents(".tagPopup").eq(0), thisH = thisP.html(); if (!nextP.length == 0) { var thisOrderno = parseInt($(this).attr("orderno")), thisId = parseInt($(this).attr("tagid")), nextOrderno = parseInt(nextP.find(".moveUp").attr("orderno")), nextId = parseInt(nextP.find(".moveUp").attr("tagid")); $.post("ChangeUserTagOrder", { id1: thisId, order1: nextOrderno, id2: nextId, order2: thisOrderno }, function () { thisP.html(nextP.html()).find(".moveUp,.moveDown").attr("orderno", thisOrderno); nextP.html(thisH).find(".moveUp,.moveDown").attr("orderno", nextOrderno); taglist.find(".operate").hide(); flag = true; }) } else flag = true; } }); taglist.on("click", " .cancel", function (e) { if (flag) { flag = false; var t = $(this); var id = t.attr("tagid"); $.post("DelUserTag?id=" + id, function (data) { if (data) { e.stopPropagation(); t.parents(".tagPopup").remove(); } flag = true; }) } }); });