<commit_before>/**
 * Created by <NAME> from Ubrainians for imCode
 * 25.07.17.
 */
Imcms.define("imcms-selects-builder",
    ["imcms-bem-builder", "imcms-primitives-builder", "imcms-buttons-builder", "jquery"],
    function (BEM, primitives, buttons, $) {
        var fieldBEM = new BEM({
                block: "imcms-field",
                elements: {
                    "select": "imcms-select"
                }
            }),
            selectBEM = new BEM({
                block: "imcms-select",
                elements: {
                    "drop-down-list": "imcms-drop-down-list"
                }
            }),
            dropDownListBEM = new BEM({
                block: "imcms-drop-down-list",
                elements: {
                    "select-item": "",
                    "items": "",
                    "item": "",
                    "select-item-value": "",
                    "button": "imcms-button"
                }
            })
        ;

        function closeSelect(e) {
            if (!$(e.target).parents(".imcms-select").length) {
                $(".imcms-select__drop-down-list").removeClass("imcms-select__drop-down-list--active");
                e.stopPropagation();
            }
        }

        $(document).click(closeSelect);

        function toggleSelect() {
            $(this).closest(".imcms-select")
                .find(".imcms-drop-down-list")
                .toggleClass("imcms-select__drop-down-list--active")
                .children(".imcms-drop-down-list__items")
                .find(".imcms-drop-down-list__item")
                .click(onOptionSelected);
        }

        function onOptionSelected() {
            var $this = $(this),
                content = $this.text(),
                value = $this.data("value"),
                select = $this.closest(".imcms-select__drop-down-list"),
                itemValue = select.find(".imcms-drop-down-list__select-item-value").html(content)
            ;

            // todo: implement labeling selected item by [selected] attribute

            select.removeClass("imcms-select__drop-down-list--active")
                .parent()
                .find("input")
                .data("content", content)
                .val(value);

            return itemValue;
        }

        function mapOptionsToItemsArr(options, dropDownListBEM) {
            return options.map(function (option) {
                return dropDownListBEM.buildBlockElement("item", "<div>", option);
            });
        }

        function addOptionsToExistingDropDown(options, $select, dropDownListBEM) {
            return $select.find(".imcms-drop-down-list__items")
                .append(mapOptionsToItemsArr(options, dropDownListBEM))
                .end()
                .selectFirst();
        }

        function createSelectOptions(options, dropDownListBEM) {
            var $itemsContainer = dropDownListBEM.buildElement("items", "<div>")
                .append(mapOptionsToItemsArr(options, dropDownListBEM)),

                $button = dropDownListBEM.makeBlockElement("button", buttons.dropDownButton()),
                $selectedValue = dropDownListBEM.buildBlockElement("select-item-value", "<span>", {
                    text: (options[0] && options[0].text) || ""
                }),
                $selectItem = dropDownListBEM.buildElement("select-item", "<div>", {click: toggleSelect})
                    .append($selectedValue, $button),

                $dropDownList = dropDownListBEM.buildBlock("<div>", [
                    {"select-item": $selectItem},
                    {"items": $itemsContainer}
                ]);

            return selectBEM.makeBlockElement("drop-down-list", $dropDownList);
        }

        function apiSelectValue(resultImcmsSelect, $selectedValInput) {
            return function (value) {
                var selectCandidate = resultImcmsSelect.find("[data-value='" + value + "']");
                if (selectCandidate.length) {
                    onOptionSelected.call(selectCandidate);
                    $selectedValInput.val(value);
                    return resultImcmsSelect;
                } else {
                    throw new Error("Value '" + value + "' for select doesn't exist");
                }
            }
        }

        function apiSelectFirst(resultImcmsSelect) {
            return function () {
                var selectCandidate = resultImcmsSelect.find(".imcms-drop-down-list__items")
                    .find(".imcms-drop-down-list__item").first();
                if (selectCandidate.length) {
                    onOptionSelected.call(selectCandidate);
                    return resultImcmsSelect;
                } else {
                    throw new Error("Select is empty, nothing to choose");
                }
            }
        }

        function apiGetSelect($select) {
            return function () {
                return $select;
            }
        }

        function apiSelectedValue(resultImcmsSelect) {
            return function () {
                return resultImcmsSelect.parent().find("input").val();
            }
        }
<commit_msg>IMCMS-257 - Upgrade menu functionality to work with new menu editor API on client side:  - Replaced error message with the error.log.<commit_after>/**
 * Created by <NAME> from Ubrainians for imCode
 * 25.07.17.
 */
Imcms.define("imcms-selects-builder",
    ["imcms-bem-builder", "imcms-primitives-builder", "imcms-buttons-builder", "jquery"],
    function (BEM, primitives, buttons, $) {
        var fieldBEM = new BEM({
                block: "imcms-field",
                elements: {
                    "select": "imcms-select"
                }
            }),
            selectBEM = new BEM({
                block: "imcms-select",
                elements: {
                    "drop-down-list": "imcms-drop-down-list"
                }
            }),
            dropDownListBEM = new BEM({
                block: "imcms-drop-down-list",
                elements: {
                    "select-item": "",
                    "items": "",
                    "item": "",
                    "select-item-value": "",
                    "button": "imcms-button"
                }
            })
        ;

        function closeSelect(e) {
            if (!$(e.target).parents(".imcms-select").length) {
                $(".imcms-select__drop-down-list").removeClass("imcms-select__drop-down-list--active");
                e.stopPropagation();
            }
        }

        $(document).click(closeSelect);

        function toggleSelect() {
            $(this).closest(".imcms-select")
                .find(".imcms-drop-down-list")
                .toggleClass("imcms-select__drop-down-list--active")
                .children(".imcms-drop-down-list__items")
                .find(".imcms-drop-down-list__item")
                .click(onOptionSelected);
        }

        function onOptionSelected() {
            var $this = $(this),
                content = $this.text(),
                value = $this.data("value"),
                select = $this.closest(".imcms-select__drop-down-list"),
                itemValue = select.find(".imcms-drop-down-list__select-item-value").html(content)
            ;

            // todo: implement labeling selected item by [selected] attribute

            select.removeClass("imcms-select__drop-down-list--active")
                .parent()
                .find("input")
                .data("content", content)
                .val(value);

            return itemValue;
        }

        function mapOptionsToItemsArr(options, dropDownListBEM) {
            return options.map(function (option) {
                return dropDownListBEM.buildBlockElement("item", "<div>", option);
            });
        }

        function addOptionsToExistingDropDown(options, $select, dropDownListBEM) {
            return $select.find(".imcms-drop-down-list__items")
                .append(mapOptionsToItemsArr(options, dropDownListBEM))
                .end()
                .selectFirst();
        }

        function createSelectOptions(options, dropDownListBEM) {
            var $itemsContainer = dropDownListBEM.buildElement("items", "<div>")
                .append(mapOptionsToItemsArr(options, dropDownListBEM)),

                $button = dropDownListBEM.makeBlockElement("button", buttons.dropDownButton()),
                $selectedValue = dropDownListBEM.buildBlockElement("select-item-value", "<span>", {
                    text: (options[0] && options[0].text) || ""
                }),
                $selectItem = dropDownListBEM.buildElement("select-item", "<div>", {click: toggleSelect})
                    .append($selectedValue, $button),

                $dropDownList = dropDownListBEM.buildBlock("<div>", [
                    {"select-item": $selectItem},
                    {"items": $itemsContainer}
                ]);

            return selectBEM.makeBlockElement("drop-down-list", $dropDownList);
        }

        function apiSelectValue(resultImcmsSelect, $selectedValInput) {
            return function (value) {
                var selectCandidate = resultImcmsSelect.find("[data-value='" + value + "']");
                if (selectCandidate.length) {
                    onOptionSelected.call(selectCandidate);
                    $selectedValInput.val(value);
                    return resultImcmsSelect;
                } else {
                    throw new Error("Value '" + value + "' for select doesn't exist");
                }
            }
        }

        function apiSelectFirst(resultImcmsSelect) {
            return function () {
                var selectCandidate = resultImcmsSelect.find(".imcms-drop-down-list__items")
                    .find(".imcms-drop-down-list__item").first();
                if (selectCandidate.length) {
                    onOptionSelected.call(selectCandidate);
                    return resultImcmsSelect;
                } else {
                    console.error("Select is empty, nothing to choose");
                }
            }
        }

        function apiGetSelect($select) {
            return function () {
                return $select;
            }
        }

        function apiSelectedValue(resultImcmsSelect) {
            return function () {
                return resultImcmsSelect.parent().find("input").val();
            }
        }

{'fraction_non_alphanumeric': 0.12547637195121952, 'fraction_numerical': 0.0018102134146341464, 'mean_word_length': 1.5239240201971629, 'pattern_counts': {'":': 18, '<': 15, '<?xml version=': 0, '>': 15, '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': '5109022', 'n_tokens_mistral': 2847, 'n_tokens_neox': 2608, 'n_words': 425}