/** * Copyright © Magento, Inc. All rights reserved. * See COPYING.txt for license details. */ define([ 'ko', 'mageUtils' ], function (ko, utils) { 'use strict'; var captionPlaceholder = {}, optgroupTmpl = '', nbspRe = / /g, optionsText, optionsValue, optionTitle; ko.bindingHandlers.optgroup = { /** * @param {*} element * @returns {Object} */ init: function (element) { if (ko.utils.tagNameLower(element) !== 'select') { throw new Error('options binding applies only to SELECT elements'); } // Remove all existing '; } else { option = utils.template(optgroupTmpl, { label: arrayEntry[optionsText], title: arrayEntry[optionsText + 'title'] }); } option = ko.utils.parseHtmlFragment(option)[0]; } else { option = element.ownerDocument.createElement('option'); option.setAttribute('data-title', arrayEntry[optionsText + 'title']); ko.selectExtensions.writeValue(option, arrayEntry[optionsValue]); ko.utils.setTextContent(option, arrayEntry[optionsText]); } return [option]; } /** * @param {*} newOptions */ function setSelectionCallback(newOptions) { var isSelected; // IE6 doesn't like us to assign selection to OPTION nodes before they're added to the document. // That's why we first added them without selection. Now it's time to set the selection. if (previousSelectedValues.length && newOptions.value) { isSelected = ko.utils.arrayIndexOf( previousSelectedValues, ko.selectExtensions.readValue(newOptions.value) ) >= 0; ko.utils.setOptionNodeSelectionState(newOptions.value, isSelected); // If this option was changed from being selected during a single-item update, notify the change if (itemUpdate && !isSelected) { ko.dependencyDetection.ignore(ko.utils.triggerEvent, null, [element, 'change']); } } } /** * @param {*} string * @param {Number} times * @returns {Array} */ function strPad(string, times) { return (new Array(times + 1)).join(string); } /** * @param {*} options * @returns {Array} */ function formatOptions(options) { var res = []; nestedOptionsLevel++; if (!nestedOptionsLevel) { // zero level // If caption is included, add it to the array if (allBindings.has('optionsCaption')) { captionValue = ko.utils.unwrapObservable(allBindings.get('optionsCaption')); // If caption value is null or undefined, don't show a caption if (//eslint-disable-line max-depth captionValue !== null && captionValue !== undefined && captionValue !== false ) { res.push(captionPlaceholder); } } } ko.utils.arrayForEach(options, function (option) { var value = applyToObject(option, optionsValue, option), label = applyToObject(option, optionsText, value) || '', disabled = applyToObject(option, 'disabled', false) || false, obj = {}, space = '\u2007\u2007\u2007'; obj[optionTitle] = applyToObject(option, optionsText + 'title', value); if (disabled) { obj.disabled = disabled; } if (option.hasOwnProperty('__disableTmpl')) { obj.__disableTmpl = option.__disableTmpl; } label = label.replace(nbspRe, '').trim(); if (Array.isArray(value)) { obj[optionsText] = strPad(' ', nestedOptionsLevel * 4) + label; res.push(obj); res = res.concat(formatOptions(value)); } else { obj[optionsText] = strPad(space, nestedOptionsLevel * 2) + label; obj[optionsValue] = value; res.push(obj); } }); nestedOptionsLevel--; return res; } } }; ko.bindingHandlers.selectedOptions.after.push('optgroup'); });