<commit_before>/*
 * Copyright (C) 2013 salesforce.com, inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
({
    optionSeparator: ";",

    init: function(cmp) {
        var currentValue = cmp.get("v.value");

        if ($A.util.isEmpty(cmp.get("v.options")) && !$A.util.isEmpty(cmp.get("v.body"))) {
            cmp.set("v.renderBody", true);
        }

        if (!$A.util.isUndefined(currentValue)) {
            // if a "value" attribute is specified on initial render, it should win over what the individual options report
            this.updateOptionsFromValue(cmp);
        } else {
            // otherwise update the "value" attribute based on the options that claim to be selected
            this.updateValueFromOptions(cmp);
        }
    },

    updateMenuListWidth: function(cmp) {
        var menuListElement = cmp.find("options").getElement();
        if (menuListElement) {
            var triggerRect = cmp.find("selectTrigger").getElement().getBoundingClientRect();
            var width = typeof triggerRect.width !== 'undefined' ? triggerRect.width : triggerRect.right - triggerRect.left;
            if (width > 0) {
                menuListElement.style.width = width + "px";
                var minWidth = 200;
                // In case the width exceeds the max width we want to still limit to the width of the trigger,
                // unless it's smaller than minWidth
                menuListElement.style.maxWidth = Math.max(minWidth, width) + "px";
                menuListElement.style.minWidth = minWidth + "px";
            }
        }
    },

    /**
     * Iterates over the options in the select element and returns a semicolon-delimited string of the selected values
     */
    getDomElementValue: function(el) {
        var selectedOptions = [];
        for (var i = 0; i < el.options.length; i++) {
            if (el.options[i].selected) {
                selectedOptions.push(el.options[i].value);
            }
        }
        return selectedOptions.join(this.optionSeparator);
    },

    /**
     * Returns a package with the array of options (as either an array of components or an array of JS objects)
     * and the strategy to work with that array
     */
    getOptionsWithStrategy: function(cmp) {
        var strat = this.optionsStrategy,
            opts = strat.getOptions(cmp);

        if ($A.util.isEmpty(opts)) {
            strat = this.bodyStrategy;
            opts = strat.getOptions(cmp);
        }

        return {options: opts, strategy: strat};
    },

    menuOptionSelected: function(cmp) {
        var menuItems = cmp.find("options").get("v.body");

        var buildAListOfSelectedOptions = function(accumulator, menuItem) {
            if (menuItem.get("v.selected")) {
                accumulator.push({label: menuItem.get("v.label"), value: menuItem.get("v.value")});
            }
            return accumulator;
        };
        var selectedOptions = menuItems.reduce(buildAListOfSelectedOptions, []);

        var newSelectedLabel = selectedOptions.map(function(value) { return value.label; }).join(this.optionSeparator);
        var newValue = selectedOptions.map(function(value) { return value.value; }).join(this.optionSeparator);

        if (cmp.get("v.selectedLabel") === newSelectedLabel) {
            return;
        }

        cmp.set("v.selectedLabel", newSelectedLabel);
        cmp._suspendChangeHandlers = true;
        cmp.set("v.value", newValue);
        cmp.get("e.change").fire();
        cmp._suspendChangeHandlers = false;
    },

    createMenuItems: function(cmp) {
        var options = cmp.get("v.options");
        var menuItems = [];

        var handleCreatedMenuItem = function(menuItem, status) {
        	if (status === "SUCCESS") {
        		menuItems.push(menuItem);
                if (menuItems.length === options.length) {
                    cmp.find("options").set("v.body", menuItems);
                }
        	}
        };
        var multiSelect = cmp.get("v.multiple");
        var menuItemComponentName = multiSelect ? "ui:checkboxMenuItem" : "ui:radioMenuItem";

        $A.getDefinition(menuItemComponentName, function() {
            for (var i = 0; i < options.length; i++) {
                $A.createComponent(menuItemComponentName, {
                    "label": options[i].label,
                    "value": options[i].value,
                    "selected": $A.util.getBooleanValue(options[i].selected),
                    "hideMenuAfterSelected": !multiSelect
                }, handleCreatedMenuItem);
            }
        });
    },

    updateMenuLabel: function(cmp) {
        var options = cmp.get("v.options");

        var newLabel = options.filter(function(option) { return option.selected; })
            .map(function(option) { return option.label; })
            .join(this.optionSeparator);

        // If nothing was selected, just default the label to the first item
        if (newLabel === "" && options[0]) {
            newLabel = options[0].label;
        }

        cmp.set("v.selectedLabel", newLabel);
    },

    /**<commit_msg>inputSelect: when menu is used for selection, allow for class attribute on v.options and pass it to the individual menu items. @bug W-3724435@ @rev abakhtiari@<commit_after>/*
 * Copyright (C) 2013 salesforce.com, inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
({
    optionSeparator: ";",

    init: function(cmp) {
        var currentValue = cmp.get("v.value");

        if ($A.util.isEmpty(cmp.get("v.options")) && !$A.util.isEmpty(cmp.get("v.body"))) {
            cmp.set("v.renderBody", true);
        }

        if (!$A.util.isUndefined(currentValue)) {
            // if a "value" attribute is specified on initial render, it should win over what the individual options report
            this.updateOptionsFromValue(cmp);
        } else {
            // otherwise update the "value" attribute based on the options that claim to be selected
            this.updateValueFromOptions(cmp);
        }
    },

    updateMenuListWidth: function(cmp) {
        var menuListElement = cmp.find("options").getElement();
        if (menuListElement) {
            var triggerRect = cmp.find("selectTrigger").getElement().getBoundingClientRect();
            var width = typeof triggerRect.width !== 'undefined' ? triggerRect.width : triggerRect.right - triggerRect.left;
            if (width > 0) {
                menuListElement.style.width = width + "px";
                var minWidth = 200;
                // In case the width exceeds the max width we want to still limit to the width of the trigger,
                // unless it's smaller than minWidth
                menuListElement.style.maxWidth = Math.max(minWidth, width) + "px";
                menuListElement.style.minWidth = minWidth + "px";
            }
        }
    },

    /**
     * Iterates over the options in the select element and returns a semicolon-delimited string of the selected values
     */
    getDomElementValue: function(el) {
        var selectedOptions = [];
        for (var i = 0; i < el.options.length; i++) {
            if (el.options[i].selected) {
                selectedOptions.push(el.options[i].value);
            }
        }
        return selectedOptions.join(this.optionSeparator);
    },

    /**
     * Returns a package with the array of options (as either an array of components or an array of JS objects)
     * and the strategy to work with that array
     */
    getOptionsWithStrategy: function(cmp) {
        var strat = this.optionsStrategy,
            opts = strat.getOptions(cmp);

        if ($A.util.isEmpty(opts)) {
            strat = this.bodyStrategy;
            opts = strat.getOptions(cmp);
        }

        return {options: opts, strategy: strat};
    },

    menuOptionSelected: function(cmp) {
        var menuItems = cmp.find("options").get("v.body");

        var buildAListOfSelectedOptions = function(accumulator, menuItem) {
            if (menuItem.get("v.selected")) {
                accumulator.push({label: menuItem.get("v.label"), value: menuItem.get("v.value")});
            }
            return accumulator;
        };
        var selectedOptions = menuItems.reduce(buildAListOfSelectedOptions, []);

        var newSelectedLabel = selectedOptions.map(function(value) { return value.label; }).join(this.optionSeparator);
        var newValue = selectedOptions.map(function(value) { return value.value; }).join(this.optionSeparator);

        if (cmp.get("v.selectedLabel") === newSelectedLabel) {
            return;
        }

        cmp.set("v.selectedLabel", newSelectedLabel);
        cmp._suspendChangeHandlers = true;
        cmp.set("v.value", newValue);
        cmp.get("e.change").fire();
        cmp._suspendChangeHandlers = false;
    },

    createMenuItems: function(cmp) {
        var options = cmp.get("v.options");
        var menuItems = [];

        var handleCreatedMenuItem = function(menuItem, status) {
        	if (status === "SUCCESS") {
        		menuItems.push(menuItem);
                if (menuItems.length === options.length) {
                    cmp.find("options").set("v.body", menuItems);
                }
        	}
        };
        var multiSelect = cmp.get("v.multiple");
        var menuItemComponentName = multiSelect ? "ui:checkboxMenuItem" : "ui:radioMenuItem";

        $A.getDefinition(menuItemComponentName, function() {
            for (var i = 0; i < options.length; i++) {
                $A.createComponent(menuItemComponentName, {
                    "class": options[i].class,
                    "label": options[i].label,
                    "value": options[i].value,
                    "selected": $A.util.getBooleanValue(options[i].selected),
                    "hideMenuAfterSelected": !multiSelect
                }, handleCreatedMenuItem);
            }
        });
    },

    updateMenuLabel: function(cmp) {
        var options = cmp.get("v.options");

        var newLabel = options.filter(function(option) { return option.selected; })
            .map(function(option) { return option.label; })
            .join(this.optionSeparator);

        // If nothing was selected, just default the label to the first item
        if (newLabel === "" && options[0]) {
            newLabel = options[0].label;
        }

        cmp.set("v.selectedLabel", newLabel);
    },

    /**
{'fraction_non_alphanumeric': 0.11818261402433687, 'fraction_numerical': 0.003414164405147509, 'mean_word_length': 2.2044880785413743, 'pattern_counts': {'":': 9, '<': 7, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '6988855', 'n_tokens_mistral': 3107, 'n_tokens_neox': 2996, 'n_words': 814}