<commit_before>Imcms.define("imcms-categories-tab-builder",
    [
        "imcms-bem-builder", "imcms-components-builder", "imcms-category-types-rest-api",
        "imcms-page-info-tab-form-builder"
    ],
    function (BEM, components, categoriesTypesRestApi, tabContentBuilder) {

        return {
            name: "categories",
            data: {},
            buildTab: function (index, docId) {
                this.data.$categoriesBlock = tabContentBuilder.buildFormBlock([], index);
                docId || this.fillTabDataFromDocument();
                return this.data.$categoriesBlock;
            },
            fillTabDataFromDocument: function (document) {
                this.data.$categoriesBlock && this.data.$categoriesBlock.empty();
                this.data.multiSelects$ = [];
                this.data.singleSelects$ = [];

                var categoriesBlockElements = [],
                    parentContext = this;

                categoriesTypesRestApi.read(null).done(function (categoriesTypes) {
                    categoriesTypes.forEach(function (categoryType) {
                        var $categoryType = (categoryType.multiSelect)
                            ? createMultiSelectCategoryType(categoryType, document, parentContext.data.multiSelects$)
                            : createSingleSelectCategoryType(categoryType, document, parentContext.data.singleSelects$);

                        categoriesBlockElements.push($categoryType);
                    });

                    parentContext.data.$categoriesBlock.append(categoriesBlockElements);
                });

                function isDocumentContainsCategory(document, category) {
                    if (!document) {
                        return false;
                    }

                    var docCategoriesIds = document.categories.map(function (category) {
                        return category.id;
                    });

                    return docCategoriesIds.indexOf(category.id) !== -1;
                }

                function createMultiSelectCategoryType(categoryType, document, selectWareHouse) {

                    var categoryTypeAsCheckboxGroup = categoryType.categories.map(function (category) {
                        var $imcmsCheckbox = components.checkboxes.imcmsCheckbox("<div>", {
                            name: "category-type-" + categoryType.id,
                            value: category.id,
                            text: category.name
                        });

                        selectWareHouse.push($imcmsCheckbox);

                        return $imcmsCheckbox.setChecked(isDocumentContainsCategory(document, category));
                    });

                    return components.checkboxes.checkboxContainerField("<div>",
                        categoryTypeAsCheckboxGroup,
                        {title: categoryType.name}
                    );
                }

                function createSingleSelectCategoryType(categoryType, document, selectWareHouse) {
                    var mappedCategoriesForSelectContainer = categoryType.categories.map(function (category) {
                        return {
                            text: category.name,
                            "data-value": category.id
                        };
                    });

                    var $selectContainer = components.selects.selectContainer("<div>", {
                        id: "category-type-" + categoryType.id,
                        text: categoryType.name,
                        emptySelect: true
                    }, mappedCategoriesForSelectContainer);

                    var $imcmsSelect = $selectContainer.getSelect();
                    selectWareHouse.push($imcmsSelect);

                    var category = categoryType.categories.filter(function (category) {
                        return isDocumentContainsCategory(document, category);
                    })[0];

                    category && $imcmsSelect.selectValue(category.id);

                    return $selectContainer;
                }
            },
            saveData: function (documentDTO) {
                var multiSelectCategories = this.data.multiSelects$
                    .filter(function ($categoryCheckbox) {
                        return $categoryCheckbox.isChecked();
                    })
                    .map(function ($categoryCheckbox) {
                        return {
                            id: $categoryCheckbox.getValue()
                        };
                    });

                var singleSelectCategories = this.data.singleSelects$
                    .map(function ($categorySelect) {
                        return {
                            id: $categorySelect.getSelectedValue()
                        };
                    })
                    .filter(function (category) {
                        return category.id;
                    });

                documentDTO.categories = multiSelectCategories.concat(singleSelectCategories);

                return documentDTO;
            },
            clearTabData: function () {
                this.data.$categoriesBlock && this.data.$categoriesBlock.empty();
            }
        };
    }
);<commit_msg>IMCMS-255 - Upgrade server side to work with new client: - Fixed categories build for Page Info after changes in backend.<commit_after>Imcms.define("imcms-categories-tab-builder",
    [
        "imcms-bem-builder", "imcms-components-builder", "imcms-categories-rest-api",
        "imcms-page-info-tab-form-builder"
    ],
    function (BEM, components, categoriesRestApi, tabContentBuilder) {

        var tabData = {};

        function isDocumentContainsCategory(document, category) {
            if (!document) {
                return false;
            }

            var docCategoriesIds = document.categories.map(function (category) {
                return category.id;
            });

            return docCategoriesIds.indexOf(category.id) !== -1;
        }

        function createMultiSelectCategoryType(categoryType, document, selectWareHouse) {

            var categoryTypeAsCheckboxGroup = categoryType.categories.map(function (category) {
                var $imcmsCheckbox = components.checkboxes.imcmsCheckbox("<div>", {
                    name: "category-type-" + categoryType.id,
                    value: category.id,
                    text: category.name
                });

                selectWareHouse.push($imcmsCheckbox);

                return $imcmsCheckbox.setChecked(isDocumentContainsCategory(document, category));
            });

            return components.checkboxes.checkboxContainerField("<div>",
                categoryTypeAsCheckboxGroup,
                {title: categoryType.name}
            );
        }

        function createSingleSelectCategoryType(categoryType, document, selectWareHouse) {
            var mappedCategoriesForSelectContainer = categoryType.categories.map(function (category) {
                return {
                    text: category.name,
                    "data-value": category.id
                };
            });

            var $selectContainer = components.selects.selectContainer("<div>", {
                id: "category-type-" + categoryType.id,
                text: categoryType.name,
                emptySelect: true
            }, mappedCategoriesForSelectContainer);

            var $imcmsSelect = $selectContainer.getSelect();
            selectWareHouse.push($imcmsSelect);

            var category = categoryType.categories.filter(function (category) {
                return isDocumentContainsCategory(document, category);
            })[0];

            category && $imcmsSelect.selectValue(category.id);

            return $selectContainer;
        }

        function buildCategoryTypes(categoryTypes, document) {
            var categoriesBlockElements = [];

            categoryTypes.forEach(function (categoryType) {
                var $categoryType = (categoryType.multiSelect)
                    ? createMultiSelectCategoryType(categoryType, document, tabData.multiSelects$)
                    : createSingleSelectCategoryType(categoryType, document, tabData.singleSelects$);

                categoriesBlockElements.push($categoryType);
            });

            tabData.$categoriesBlock.append(categoriesBlockElements);
        }

        function extractCategoryTypes(categories) {
            var categoryTypeIdsPerType = {};

            categories.forEach(function (category) {
                var categoryTypeId = category.type.id;

                if (!categoryTypeIdsPerType[categoryTypeId]) {
                    categoryTypeIdsPerType[categoryTypeId] = category.type;
                    categoryTypeIdsPerType[categoryTypeId].categories = [];
                }

                delete category.type;
                categoryTypeIdsPerType[categoryTypeId].categories.push(category);
            });

            var categoryTypes = [];

            for (var categoryType in categoryTypeIdsPerType) {
                categoryTypes.push(categoryTypeIdsPerType[categoryType]);
            }

            return categoryTypes;
        }

        return {
            name: "categories",
            buildTab: function (index, docId) {
                tabData.$categoriesBlock = tabContentBuilder.buildFormBlock([], index);
                docId || this.fillTabDataFromDocument();
                return tabData.$categoriesBlock;
            },
            fillTabDataFromDocument: function (document) {
                tabData.$categoriesBlock && tabData.$categoriesBlock.empty();
                tabData.multiSelects$ = [];
                tabData.singleSelects$ = [];

                categoriesRestApi.read(null).done(function (categories) {
                    var categoryTypes = extractCategoryTypes(categories);
                    buildCategoryTypes(categoryTypes, document);
                });
            },
            saveData: function (documentDTO) {
                var multiSelectCategories = tabData.multiSelects$
                    .filter(function ($categoryCheckbox) {
                        return $categoryCheckbox.isChecked();
                    })
                    .map(function ($categoryCheckbox) {
                        return {
                            id: $categoryCheckbox.getValue()
                        };
                    });

                var singleSelectCategories = tabData.singleSelects$
                    .map(function ($categorySelect) {
                        return {
                            id: $categorySelect.getSelectedValue()
                        };
                    })
                    .filter(function (category) {
                        return category.id;
                    });

                documentDTO.categories = multiSelectCategories.concat(singleSelectCategories);

                return documentDTO;
            },
            clearTabData: function () {
                tabData.$categoriesBlock && tabData.$categoriesBlock.empty();
            }
        };
    }
);
{'fraction_non_alphanumeric': 0.08914520743919886, 'fraction_numerical': 0.0006258941344778255, 'mean_word_length': 1.507847533632287, 'pattern_counts': {'":': 2, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3638615', 'n_tokens_mistral': 2566, 'n_tokens_neox': 2327, 'n_words': 390}