Esempio n. 1
0
AppDispatcher.register((payload) => {
    let action = payload.action;
    switch (action.actionType) {
        case AppConstants.CLIENT_DATA_ALL:
            o.data.all = action.res.data;
            o.data.filter = sort(o.config.sortBy, o.data.all);
            for (var i in o.config.filterValue);
            if (i) {
                o.data.filter = filter(o.config.filter, o.config.filterValue, o.data.all);
            }

            //
            if (action.res.tagId) {
                let tmp = [];
                for (var i in o.data.filter) {
                    //無標籤時
                    if (action.res.tagId == "none" && !o.data.filter[i].tag.length) {
                        tmp.push(o.data.filter[i]);
                    } else {
                        for (let j in o.data.filter[i].tag) {
                            if (o.data.filter[i].tag[j].id == action.res.tagId) {
                                tmp.push(o.data.filter[i]);
                                break;
                            }
                        }
                        for (let j in o.data.filter[i].career) {
                            if (o.data.filter[i].career[j].id == action.res.tagId) {
                                tmp.push(o.data.filter[i]);
                                break;
                            }
                        }
                    }
                }
                o.data.filter = tmp;
            }


            if (o.data.checkbox.length) {
                let checkbox = [];
                for (var i = 0; i < o.data.all.length; i++) {
                    for (let j = 0; j < o.data.checkbox.length; j++) {
                        if (o.data.all[i].id == o.data.checkbox[j]) {
                            o.data.all[i].checked = true;
                            checkbox.push(o.data.all[i].id);
                            break;
                        }
                    }
                }
                if (checkbox.length) {
                    checkbox.sort();
                }
                o.data.checkbox = checkbox;
            }
            break;
        case AppConstants.CLIENT_DATA_ONE:
            o.data.one = action.res.data;
            break;
        case AppConstants.CLIENT_DATA_SORT:
            o.config.sortBy = action.sortBy;
            o.data.filter = sort(o.config.sortBy, o.data.filter);
            break;
        case AppConstants.CLIENT_DATA_PAGE:
            o.config.currentPage = action.currentPage;
            break;
        case AppConstants.CLIENT_DATA_FILTER:
            o.config.filterValue[action.name] = action.value;
            o.data.filter = filter(o.config.filter, o.config.filterValue, o.data.all);
            o.config.currentPage = 0;
            break;
        case AppConstants.CLIENT_ADD:
        case AppConstants.CLIENT_EDIT:
            o.bool = action.res.bool;
            o.message = action.res.message;
            if (action.res.data) {
                o.data.one = action.res.data;
            }
            break;
        case AppConstants.CLIENT_CHECKBOX:
            if (action.id == "all") {
                for (var i = 0; i < o.data.all.length; i++) {
                    for (let j = 0; j < o.data.page.length; j++) {
                        if (o.data.all[i].id == o.data.page[j].id) {
                            if (action.className) {
                                o.data.all[i].checked = false;
                                o.data.checkbox.splice(o.data.checkbox.indexOf(o.data.all[i].id), 1);
                            } else {
                                o.data.all[i].checked = true;
                                o.data.checkbox.push(o.data.all[i].id);
                                o.data.checkbox.sort();
                            }
                            break;
                        }
                    }
                }
            } else {
                let id = parseInt(action.id);
                let index = o.data.checkbox.indexOf(id);
                if (index < 0) {
                    o.data.checkbox.push(id);
                    o.data.checkbox.sort();
                } else {
                    o.data.checkbox.splice(index, 1);
                }
                for (var i = 0; i < o.data.all.length; i++) {
                    if (o.data.all[i].id == id) {
                        o.data.all[i].checked = o.data.all[i].checked ? false : true;
                        break;
                    }
                }
            }
            break;
        default:
    }

    if (!action.actionType.match(/ONE|ADD|EDIT/)) {
        o.data.page = o.data.filter.slice(o.config.pageNum * o.config.currentPage, o.config.pageNum * o.config.currentPage + o.config.pageNum);
        o.config.totalPages = Math.ceil(o.data.filter.length / o.config.pageNum);
    }
    if (action.actionType.match(/CLIENT/)) {
        _COMMON.storageSave("client", o);
        Store.emitChange();
    }

    return true;
});
Esempio n. 2
0
let dispatch = (action) => {
    let TYPE = action.actionType.split(/\_/)[1];
    let type = TYPE.toLowerCase();
    if (!type.match(/group|item|industry|career/)) {
        return;
    }

    switch (action.actionType) {
        case AppConstants["TAG_" + TYPE + "_DATA_ALL"]:
            o.type = type;
            o[type].data.all = action.res.data;
            o[type].data.filter = sort(o[type].config.sortBy, o[type].data.all);
            for (var i in o[type].config.filterValue);
            if (i) {
                o[type].data.filter = filter(o[type].config.filter, o[type].config.filterValue, o[type].data.all);
            }
            if (o[type].data.checkbox.length) {
                let checkbox = [];
                for (var i = 0; i < o[type].data.all.length; i++) {
                    for (var j = 0; j < o[type].data.checkbox.length; j++) {
                        if (o[type].data.all[i].id == o[type].data.checkbox[j]) {
                            o[type].data.all[i].checked = true;
                            checkbox.push(o[type].data.all[i].id);
                            break;
                        }
                    }
                }
                if (checkbox.length) {
                    checkbox.sort();
                }
                o[type].data.checkbox = checkbox;
            }

            //reload menu tag.
            TagAction.data("all");
            break;
        case AppConstants["TAG_" + TYPE + "_DATA_ONE"]:
            o[type].data.one = action.res.data;
            break;
        case AppConstants["TAG_" + TYPE + "_DATA_SORT"]:
            o[type].config.sortBy = action.sortBy;
            o[type].data.filter = sort(o[type].config.sortBy, o[type].data.filter);
            break;
        case AppConstants["TAG_" + TYPE + "_DATA_PAGE"]:
            o[type].config.currentPage = action.currentPage;
            break;
        case AppConstants["TAG_" + TYPE + "_DATA_FILTER"]:
            o[type].config.filterValue[action.name] = action.value;
            o[type].data.filter = filter(o[type].config.filter, o[type].config.filterValue, o[type].data.all);
            o[type].config.currentPage = 0;
            break;
        case AppConstants["TAG_" + TYPE + "_CHECKBOX"]:
            if (action.id == "all") {
                for (var i = 0; i < o[type].data.all.length; i++) {
                    for (var j = 0; j < o[type].data.page.length; j++) {
                        if (o[type].data.all[i].id == o[type].data.page[j].id) {
                            if (action.className) {
                                o[type].data.all[i].checked = false;
                                o[type].data.checkbox.splice(o[type].data.checkbox.indexOf(o[type].data.all[i].id), 1);
                            } else {
                                o[type].data.all[i].checked = true;
                                o[type].data.checkbox.push(o[type].data.all[i].id);
                                o[type].data.checkbox.sort();
                            }
                            break;
                        }
                    }
                }
            } else {
                let id = parseInt(action.id);
                let index = o[type].data.checkbox.indexOf(id);
                if (index < 0) {
                    o[type].data.checkbox.push(id);
                    o[type].data.checkbox.sort();
                } else {
                    o[type].data.checkbox.splice(index, 1);
                }
                for (var i = 0; i < o[type].data.all.length; i++) {
                    if (o[type].data.all[i].id == id) {
                        o[type].data.all[i].checked = o[type].data.all[i].checked ? false : true;
                        break;
                    }
                }
            }
            break;
    }
}