categoryManageAdd.js 3.67 KB
/**
 * Created by ty on 2016/5/31.
 */

var $ = require('jquery'),
    common = require('../common/common'),
    Category = require('./partials/Category');

var edit = new common.edit2(".row", {bucket:"yhb-img01"});

var type = $("#info-content").attr("type");
var action = $("#info-content").attr("action");
var id = window.location.href.substring(location.href.lastIndexOf("/") + 1);
var categories = [];
var sortsArr = ["firstSortId", "secondSortId", "threadSortId"];

var Bll = {
    __render: function () {
        //初始化页面
        $("#info-content").html(common.util.__template2($("#content-template").html(), Category));
        Bll.__renderCategories();
        edit.init();
        edit.on("file_onComplete", function (obj) {
            var name = obj.field;
            Category[name] = obj.data;
        });
    },
    __renderCategories: function () {
        $("#categories-content").html(common.util.__template2($("#categories-template").html(), Category));
        new common.edit2("#categories-content").init();
        new common.dropDown({el: "#firstSortId"});
        new common.dropDown({el: "#secondSortId"});
        new common.dropDown({el: "#threadSortId"});
        //$("#firstSortId").change();
        //$("#secondSortId").change();
        //$("#threadSortId").change();
    }
};
common.util.__ajax({
    async: false,
    url: "/category/getCategories",
    data: {}
}, function (res) {
    categories = res.data;
},true);

if(type == "edit") {
    common.util.__ajax({
        async: false,
        url: "/category/getOneCategory",
        data: {id:id}
    }, function (res) {
        Category = res.data;
        var sorts = Category.sortCode.split(",");
        for(var i = 0; i < Category.sortLevel*1-1; i++) {
            Category[sortsArr[i]] = sorts[i];
        }
    },true);
}
Category.categories = $.extend(true, {}, categories);
Bll.__render();

$(document).on("change", ".observe", function() {
    var $this = $(this);
    var name = $this.data("field");
    Category[name] = $this.val();
    if(name == "firstSortId") {
        Category.secondSortId = "";
        Category.threadSortId = "";
        Bll.__renderCategories();
    } else if(name == "secondSortId") {
        Category.threadSortId = "";
        Bll.__renderCategories();
    }
});

$(document).on("click", "#addUrl", function () {
    common.dialog.confirm("添加链接", common.util.__template2($("#template2").html(), {}), function () {
        var url = JSON.stringify({
            action: $("#goTo").val(),
            url: $("#goToUrl").val()
        });
        $("#sortUrl").val(url);
        Category.sortUrl = url;
    });
});

$(document).on("click", "#saveBtn", function () {
    if(!edit.validate()) {
        return false;
    }
    var isNew = $("#isNew").val();
    var isHot = $("#isHot").val();
    var separativeSign = $("#separativeSign").val();
    var isNewPage = $("#isNewPage").val();
    var platform = $("#platform").val();
    Category.isNew = !!isNew ? isNew : Category.isNew;
    Category.isHot = !!isHot ? isHot : Category.isHot;
    Category.separativeSign = !!separativeSign ? separativeSign : Category.separativeSign;
    Category.isNewPage = !!isNewPage ? isNewPage : Category.isNewPage;
    Category.platform = platform.replace(/\|/g, ",");
    delete Category.categories;
    //console.log(Category);

    common.util.__ajax2({
        url: action,
        data: Category
    }, function () {
        common.util.__tip("保存成功","success");
        setTimeout(function () {
            window.location.href = "/operations/category/index";
        },600);
    }, function () {
        common.util.__tip("保存失败");
        Category.categories = $.extend(true, {}, categories);
    });
});