Commit f436780d authored by Gavin Williams's avatar Gavin Williams Committed by Chromium LUCI CQ

scanning: Move settings dropdown functionality to SelectBehavior

Move the common dropdown code for sorting and choosing the default
option to SelectBehavior.

Add getSelectedOption(), sortOptions(), and isDefaultOption() to each
settings dropdown. These functions are called from SelectBehavior.

Bug: 1168346
Change-Id: I186a6e2ef18b335fdba0f81e4002f4859d27d0eb
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2638499
Commit-Queue: Gavin Williams <gavinwill@chromium.org>
Reviewed-by: default avatarJesse Schettler <jschettler@chromium.org>
Cr-Commit-Position: refs/heads/master@{#845545}
parent 1b7c1479
...@@ -42,7 +42,7 @@ export function colorModeSelectTest() { ...@@ -42,7 +42,7 @@ export function colorModeSelectTest() {
const firstColorMode = ColorMode.COLOR; const firstColorMode = ColorMode.COLOR;
const secondColorMode = ColorMode.GRAYSCALE; const secondColorMode = ColorMode.GRAYSCALE;
colorModeSelect.colorModes = [firstColorMode, secondColorMode]; colorModeSelect.options = [firstColorMode, secondColorMode];
flush(); flush();
// Verify that adding color modes results in the dropdown displaying the // Verify that adding color modes results in the dropdown displaying the
...@@ -58,28 +58,25 @@ export function colorModeSelectTest() { ...@@ -58,28 +58,25 @@ export function colorModeSelectTest() {
}); });
test('colorModesSortedAlphabetically', () => { test('colorModesSortedAlphabetically', () => {
colorModeSelect.colorModes = colorModeSelect.options =
[ColorMode.GRAYSCALE, ColorMode.BLACK_AND_WHITE, ColorMode.COLOR]; [ColorMode.GRAYSCALE, ColorMode.BLACK_AND_WHITE, ColorMode.COLOR];
flush(); flush();
// Verify the color modes are sorted alphabetically and that color is // Verify the color modes are sorted alphabetically and that color is
// selected by default. // selected by default.
assertOrderedAlphabetically( assertOrderedAlphabetically(
colorModeSelect.colorModes, colorModeSelect.options, (colorMode) => getColorModeString(colorMode));
(colorMode) => getColorModeString(colorMode)); assertEquals(ColorMode.COLOR.toString(), colorModeSelect.selectedOption);
assertEquals(ColorMode.COLOR.toString(), colorModeSelect.selectedColorMode);
}); });
test('firstColorModeUsedWhenDefaultNotAvailable', () => { test('firstColorModeUsedWhenDefaultNotAvailable', () => {
colorModeSelect.colorModes = colorModeSelect.options = [ColorMode.GRAYSCALE, ColorMode.BLACK_AND_WHITE];
[ColorMode.GRAYSCALE, ColorMode.BLACK_AND_WHITE];
flush(); flush();
// Verify the first color mode in the sorted color mode array is selected by // Verify the first color mode in the sorted color mode array is selected by
// default when color is not an available option. // default when color is not an available option.
assertEquals( assertEquals(
ColorMode.BLACK_AND_WHITE.toString(), ColorMode.BLACK_AND_WHITE.toString(), colorModeSelect.selectedOption);
colorModeSelect.selectedColorMode);
}); });
// Verify the correct default option is selected when a scanner is selected // Verify the correct default option is selected when a scanner is selected
...@@ -87,22 +84,22 @@ export function colorModeSelectTest() { ...@@ -87,22 +84,22 @@ export function colorModeSelectTest() {
test('selectDefaultWhenOptionsChange', () => { test('selectDefaultWhenOptionsChange', () => {
const select = const select =
/** @type {!HTMLSelectElement} */ (colorModeSelect.$$('select')); /** @type {!HTMLSelectElement} */ (colorModeSelect.$$('select'));
colorModeSelect.colorModes = colorModeSelect.options =
[ColorMode.GRAYSCALE, ColorMode.BLACK_AND_WHITE, ColorMode.COLOR]; [ColorMode.GRAYSCALE, ColorMode.BLACK_AND_WHITE, ColorMode.COLOR];
flush(); flush();
return changeSelect(select, /* value */ null, /* selectedIndex */ 0) return changeSelect(select, /* value */ null, /* selectedIndex */ 0)
.then(() => { .then(() => {
assertEquals( assertEquals(
ColorMode.BLACK_AND_WHITE.toString(), ColorMode.BLACK_AND_WHITE.toString(),
colorModeSelect.selectedColorMode); colorModeSelect.selectedOption);
assertEquals( assertEquals(
ColorMode.BLACK_AND_WHITE.toString(), ColorMode.BLACK_AND_WHITE.toString(),
select.options[select.selectedIndex].value); select.options[select.selectedIndex].value);
colorModeSelect.colorModes = [ColorMode.GRAYSCALE, ColorMode.COLOR]; colorModeSelect.options = [ColorMode.GRAYSCALE, ColorMode.COLOR];
flush(); flush();
assertEquals( assertEquals(
ColorMode.COLOR.toString(), colorModeSelect.selectedColorMode); ColorMode.COLOR.toString(), colorModeSelect.selectedOption);
assertEquals( assertEquals(
ColorMode.COLOR.toString(), ColorMode.COLOR.toString(),
select.options[select.selectedIndex].value); select.options[select.selectedIndex].value);
......
...@@ -43,7 +43,7 @@ export function pageSizeSelectTest() { ...@@ -43,7 +43,7 @@ export function pageSizeSelectTest() {
const firstPageSize = PageSize.A4; const firstPageSize = PageSize.A4;
const secondPageSize = PageSize.Max; const secondPageSize = PageSize.Max;
pageSizeSelect.pageSizes = [firstPageSize, secondPageSize]; pageSizeSelect.options = [firstPageSize, secondPageSize];
flush(); flush();
// Verify that adding page sizes results in the dropdown displaying the // Verify that adding page sizes results in the dropdown displaying the
...@@ -61,33 +61,33 @@ export function pageSizeSelectTest() { ...@@ -61,33 +61,33 @@ export function pageSizeSelectTest() {
select, secondPageSize.toString(), /* selectedIndex */ null) select, secondPageSize.toString(), /* selectedIndex */ null)
.then(() => { .then(() => {
assertEquals( assertEquals(
secondPageSize.toString(), pageSizeSelect.selectedPageSize); secondPageSize.toString(), pageSizeSelect.selectedOption);
}); });
}); });
test('pageSizesSortedCorrectly', () => { test('pageSizesSortedCorrectly', () => {
pageSizeSelect.pageSizes = [PageSize.Letter, PageSize.Max, PageSize.A4]; pageSizeSelect.options = [PageSize.Letter, PageSize.Max, PageSize.A4];
flush(); flush();
// Verify the page sizes are sorted alphabetically except for the fit to // Verify the page sizes are sorted alphabetically except for the fit to
// scan area option, which should always be last. Verify that Letter is // scan area option, which should always be last. Verify that Letter is
// selected by default. // selected by default.
assertOrderedAlphabetically( assertOrderedAlphabetically(
pageSizeSelect.pageSizes.slice(0, pageSizeSelect.pageSizes.length - 1), pageSizeSelect.options.slice(0, pageSizeSelect.options.length - 1),
(pageSize) => getPageSizeString(pageSize)); (pageSize) => getPageSizeString(pageSize));
assertEquals( assertEquals(
PageSize.Max, PageSize.Max,
pageSizeSelect.pageSizes[pageSizeSelect.pageSizes.length - 1]); pageSizeSelect.options[pageSizeSelect.options.length - 1]);
assertEquals(PageSize.Letter.toString(), pageSizeSelect.selectedPageSize); assertEquals(PageSize.Letter.toString(), pageSizeSelect.selectedOption);
}); });
test('firstPageSizeUsedWhenDefaultNotAvailable', () => { test('firstPageSizeUsedWhenDefaultNotAvailable', () => {
pageSizeSelect.pageSizes = [PageSize.Max, PageSize.A4]; pageSizeSelect.options = [PageSize.Max, PageSize.A4];
flush(); flush();
// Verify the first page size in the sorted page sizes array is selected by // Verify the first page size in the sorted page sizes array is selected by
// default when Letter is not an available option. // default when Letter is not an available option.
assertEquals(PageSize.A4.toString(), pageSizeSelect.selectedPageSize); assertEquals(PageSize.A4.toString(), pageSizeSelect.selectedOption);
}); });
// Verify the correct default option is selected when a scanner is selected // Verify the correct default option is selected when a scanner is selected
...@@ -95,19 +95,19 @@ export function pageSizeSelectTest() { ...@@ -95,19 +95,19 @@ export function pageSizeSelectTest() {
test('selectDefaultWhenOptionsChange', () => { test('selectDefaultWhenOptionsChange', () => {
const select = const select =
/** @type {!HTMLSelectElement} */ (pageSizeSelect.$$('select')); /** @type {!HTMLSelectElement} */ (pageSizeSelect.$$('select'));
pageSizeSelect.pageSizes = [PageSize.Letter, PageSize.Max, PageSize.A4]; pageSizeSelect.options = [PageSize.Letter, PageSize.Max, PageSize.A4];
flush(); flush();
return changeSelect(select, /* value */ null, /* selectedIndex */ 0) return changeSelect(select, /* value */ null, /* selectedIndex */ 0)
.then(() => { .then(() => {
assertEquals(PageSize.A4.toString(), pageSizeSelect.selectedPageSize); assertEquals(PageSize.A4.toString(), pageSizeSelect.selectedOption);
assertEquals( assertEquals(
PageSize.A4.toString(), PageSize.A4.toString(),
select.options[select.selectedIndex].value); select.options[select.selectedIndex].value);
pageSizeSelect.pageSizes = [PageSize.Letter, PageSize.Max]; pageSizeSelect.options = [PageSize.Letter, PageSize.Max];
flush(); flush();
assertEquals( assertEquals(
PageSize.Letter.toString(), pageSizeSelect.selectedPageSize); PageSize.Letter.toString(), pageSizeSelect.selectedOption);
assertEquals( assertEquals(
PageSize.Letter.toString(), PageSize.Letter.toString(),
select.options[select.selectedIndex].value); select.options[select.selectedIndex].value);
......
...@@ -36,7 +36,7 @@ export function resolutionSelectTest() { ...@@ -36,7 +36,7 @@ export function resolutionSelectTest() {
const firstResolution = 75; const firstResolution = 75;
const secondResolution = 300; const secondResolution = 300;
resolutionSelect.resolutions = [firstResolution, secondResolution]; resolutionSelect.options = [firstResolution, secondResolution];
flush(); flush();
// Verify that adding resolutions results in the dropdown displaying the // Verify that adding resolutions results in the dropdown displaying the
...@@ -55,31 +55,29 @@ export function resolutionSelectTest() { ...@@ -55,31 +55,29 @@ export function resolutionSelectTest() {
select, secondResolution.toString(), /* selectedIndex */ null) select, secondResolution.toString(), /* selectedIndex */ null)
.then(() => { .then(() => {
assertEquals( assertEquals(
secondResolution.toString(), resolutionSelect.selectedResolution); secondResolution.toString(), resolutionSelect.selectedOption);
}); });
}); });
test('resolutionsSortedCorrectly', () => { test('resolutionsSortedCorrectly', () => {
resolutionSelect.resolutions = [150, 300, 75, 600, 1200, 200]; resolutionSelect.options = [150, 300, 75, 600, 1200, 200];
flush(); flush();
// Verify the resolutions are sorted in descending order and that 300 is // Verify the resolutions are sorted in descending order and that 300 is
// selected by default. // selected by default.
for (let i = 0; i < resolutionSelect.resolutions.length - 1; i++) { for (let i = 0; i < resolutionSelect.options.length - 1; i++) {
assert( assert(resolutionSelect.options[i] > resolutionSelect.options[i + 1]);
resolutionSelect.resolutions[i] >
resolutionSelect.resolutions[i + 1]);
} }
assertEquals('300', resolutionSelect.selectedResolution); assertEquals('300', resolutionSelect.selectedOption);
}); });
test('firstResolutionUsedWhenDefaultNotAvailable', () => { test('firstResolutionUsedWhenDefaultNotAvailable', () => {
resolutionSelect.resolutions = [150, 75, 600, 1200, 200]; resolutionSelect.options = [150, 75, 600, 1200, 200];
flush(); flush();
// Verify the first resolution in the sorted resolution array is selected by // Verify the first resolution in the sorted resolution array is selected by
// default when 300 is not an available option. // default when 300 is not an available option.
assertEquals('1200', resolutionSelect.selectedResolution); assertEquals('1200', resolutionSelect.selectedOption);
}); });
// Verify the correct default option is selected when a scanner is selected // Verify the correct default option is selected when a scanner is selected
...@@ -87,16 +85,16 @@ export function resolutionSelectTest() { ...@@ -87,16 +85,16 @@ export function resolutionSelectTest() {
test('selectDefaultWhenOptionsChange', () => { test('selectDefaultWhenOptionsChange', () => {
const select = const select =
/** @type {!HTMLSelectElement} */ (resolutionSelect.$$('select')); /** @type {!HTMLSelectElement} */ (resolutionSelect.$$('select'));
resolutionSelect.resolutions = [600, 300, 150]; resolutionSelect.options = [600, 300, 150];
flush(); flush();
return changeSelect(select, /* value */ null, /* selectedIndex */ 0) return changeSelect(select, /* value */ null, /* selectedIndex */ 0)
.then(() => { .then(() => {
assertEquals('600', resolutionSelect.selectedResolution); assertEquals('600', resolutionSelect.selectedOption);
assertEquals('600', select.options[select.selectedIndex].value); assertEquals('600', select.options[select.selectedIndex].value);
resolutionSelect.resolutions = [300, 150]; resolutionSelect.options = [300, 150];
flush(); flush();
assertEquals('300', resolutionSelect.selectedResolution); assertEquals('300', resolutionSelect.selectedOption);
assertEquals('300', select.options[select.selectedIndex].value); assertEquals('300', select.options[select.selectedIndex].value);
}); });
}); });
......
...@@ -60,7 +60,7 @@ export function sourceSelectTest() { ...@@ -60,7 +60,7 @@ export function sourceSelectTest() {
const secondSource = const secondSource =
createScannerSource(SourceType.FLATBED, 'platen', pageSizes); createScannerSource(SourceType.FLATBED, 'platen', pageSizes);
const sourceArr = [firstSource, secondSource]; const sourceArr = [firstSource, secondSource];
sourceSelect.sources = sourceArr; sourceSelect.options = sourceArr;
flush(); flush();
// Verify that adding sources results in the dropdown displaying the correct // Verify that adding sources results in the dropdown displaying the correct
...@@ -83,10 +83,10 @@ export function sourceSelectTest() { ...@@ -83,10 +83,10 @@ export function sourceSelectTest() {
createScannerSource(SourceType.FLATBED, 'D', pageSizes), createScannerSource(SourceType.FLATBED, 'D', pageSizes),
createScannerSource(SourceType.ADF_DUPLEX, 'A', pageSizes), createScannerSource(SourceType.ADF_DUPLEX, 'A', pageSizes),
]; ];
sourceSelect.sources = sources; sourceSelect.options = sources;
flush(); flush();
assertOrderedAlphabetically( assertOrderedAlphabetically(
sourceSelect.sources, (source) => getSourceTypeString(source.type)); sourceSelect.options, (source) => getSourceTypeString(source.type));
}); });
test('flatbedSelectedByDefaultIfProvided', () => { test('flatbedSelectedByDefaultIfProvided', () => {
...@@ -95,11 +95,11 @@ export function sourceSelectTest() { ...@@ -95,11 +95,11 @@ export function sourceSelectTest() {
createScannerSource(SourceType.ADF_SIMPLEX, 'B', pageSizes), createScannerSource(SourceType.ADF_SIMPLEX, 'B', pageSizes),
createScannerSource(SourceType.ADF_DUPLEX, 'A', pageSizes), createScannerSource(SourceType.ADF_DUPLEX, 'A', pageSizes),
]; ];
sourceSelect.sources = sources; sourceSelect.options = sources;
flush(); flush();
const flatbedSource = const flatbedSource =
sourceSelect.sources.find(source => source.type === SourceType.FLATBED); sourceSelect.options.find(source => source.type === SourceType.FLATBED);
assertEquals(sourceSelect.selectedSource, flatbedSource.name); assertEquals(sourceSelect.selectedOption, flatbedSource.name);
}); });
test('firstSourceUsedWhenFlatbedNotProvided', () => { test('firstSourceUsedWhenFlatbedNotProvided', () => {
...@@ -107,8 +107,8 @@ export function sourceSelectTest() { ...@@ -107,8 +107,8 @@ export function sourceSelectTest() {
createScannerSource(SourceType.ADF_SIMPLEX, 'C', pageSizes), createScannerSource(SourceType.ADF_SIMPLEX, 'C', pageSizes),
createScannerSource(SourceType.ADF_DUPLEX, 'B', pageSizes), createScannerSource(SourceType.ADF_DUPLEX, 'B', pageSizes),
]; ];
sourceSelect.sources = sources; sourceSelect.options = sources;
flush(); flush();
assertEquals(sourceSelect.selectedSource, sourceSelect.sources[0].name); assertEquals(sourceSelect.selectedOption, sourceSelect.options[0].name);
}); });
} }
...@@ -40,7 +40,6 @@ js_library("color_mode_select") { ...@@ -40,7 +40,6 @@ js_library("color_mode_select") {
js_library("file_type_select") { js_library("file_type_select") {
deps = [ deps = [
":select_behavior",
"//chromeos/components/scanning/mojom:mojom_js_library_for_compile", "//chromeos/components/scanning/mojom:mojom_js_library_for_compile",
"//third_party/polymer/v3_0/components-chromium/polymer:polymer_bundled", "//third_party/polymer/v3_0/components-chromium/polymer:polymer_bundled",
"//ui/webui/resources/js:i18n_behavior.m", "//ui/webui/resources/js:i18n_behavior.m",
...@@ -63,7 +62,6 @@ js_library("resolution_select") { ...@@ -63,7 +62,6 @@ js_library("resolution_select") {
"//chromeos/components/scanning/mojom:mojom_js_library_for_compile", "//chromeos/components/scanning/mojom:mojom_js_library_for_compile",
"//third_party/polymer/v3_0/components-chromium/polymer:polymer_bundled", "//third_party/polymer/v3_0/components-chromium/polymer:polymer_bundled",
"//ui/webui/resources/js:i18n_behavior.m", "//ui/webui/resources/js:i18n_behavior.m",
"//ui/webui/resources/js:load_time_data.m",
] ]
} }
...@@ -72,7 +70,6 @@ js_library("scan_done_section") { ...@@ -72,7 +70,6 @@ js_library("scan_done_section") {
":scanning_app_types", ":scanning_app_types",
"//third_party/polymer/v3_0/components-chromium/polymer:polymer_bundled", "//third_party/polymer/v3_0/components-chromium/polymer:polymer_bundled",
"//ui/webui/resources/js:i18n_behavior.m", "//ui/webui/resources/js:i18n_behavior.m",
"//ui/webui/resources/js:load_time_data.m",
] ]
} }
...@@ -83,13 +80,11 @@ js_library("scan_preview") { ...@@ -83,13 +80,11 @@ js_library("scan_preview") {
"//chromeos/components/scanning/mojom:mojom_js_library_for_compile", "//chromeos/components/scanning/mojom:mojom_js_library_for_compile",
"//third_party/polymer/v3_0/components-chromium/polymer:polymer_bundled", "//third_party/polymer/v3_0/components-chromium/polymer:polymer_bundled",
"//ui/webui/resources/js:i18n_behavior.m", "//ui/webui/resources/js:i18n_behavior.m",
"//ui/webui/resources/js:load_time_data.m",
] ]
} }
js_library("scan_to_select") { js_library("scan_to_select") {
deps = [ deps = [
":select_behavior",
"//chromeos/components/scanning/mojom:mojom_js_library_for_compile", "//chromeos/components/scanning/mojom:mojom_js_library_for_compile",
"//third_party/polymer/v3_0/components-chromium/polymer:polymer_bundled", "//third_party/polymer/v3_0/components-chromium/polymer:polymer_bundled",
"//ui/webui/resources/js:i18n_behavior.m", "//ui/webui/resources/js:i18n_behavior.m",
...@@ -100,7 +95,6 @@ js_library("scanner_select") { ...@@ -100,7 +95,6 @@ js_library("scanner_select") {
deps = [ deps = [
":scanning_app_types", ":scanning_app_types",
":scanning_app_util", ":scanning_app_util",
":select_behavior",
"//chromeos/components/scanning/mojom:mojom_js_library_for_compile", "//chromeos/components/scanning/mojom:mojom_js_library_for_compile",
"//third_party/polymer/v3_0/components-chromium/polymer:polymer_bundled", "//third_party/polymer/v3_0/components-chromium/polymer:polymer_bundled",
"//ui/webui/resources/js:i18n_behavior.m", "//ui/webui/resources/js:i18n_behavior.m",
......
...@@ -5,11 +5,11 @@ ...@@ -5,11 +5,11 @@
</span> </span>
<div slot="settings"> <div slot="settings">
<select id="colorModeSelect" class="md-select" <select id="colorModeSelect" class="md-select"
value="{{selectedColorMode::change}}" disabled="[[disabled]]" value="{{selectedOption::change}}" disabled="[[disabled]]"
aria-labelledby="colorModeLabel"> aria-labelledby="colorModeLabel">
<template is="dom-repeat" items="[[colorModes]]" as="colorMode"> <template is="dom-repeat" items="[[options]]" as="colorMode">
<option value="[[colorMode]]" <option value="[[colorMode]]"
selected$="[[isDefaultColorMode_(colorMode)]]"> selected$="[[isDefaultOption(colorMode)]]">
[[getColorModeString_(colorMode)]] [[getColorModeString_(colorMode)]]
</option> </option>
</template> </template>
......
...@@ -27,24 +27,18 @@ Polymer({ ...@@ -27,24 +27,18 @@ Polymer({
behaviors: [I18nBehavior, SelectBehavior], behaviors: [I18nBehavior, SelectBehavior],
properties: { /**
/** @type {!Array<chromeos.scanning.mojom.ColorMode>} */ * @param {number} index
colorModes: { * @return {string}
type: Array, */
value: () => [], getOptionAtIndex(index) {
}, assert(index < this.options.length);
/** @type {string} */ return this.options[index].toString();
selectedColorMode: {
type: String,
notify: true,
},
}, },
observers: ['onColorModesChange_(colorModes.*)'],
/** /**
* @param {chromeos.scanning.mojom.ColorMode} mojoColorMode * @param {!chromeos.scanning.mojom.ColorMode} mojoColorMode
* @return {string} * @return {string}
* @private * @private
*/ */
...@@ -52,49 +46,17 @@ Polymer({ ...@@ -52,49 +46,17 @@ Polymer({
return getColorModeString(mojoColorMode); return getColorModeString(mojoColorMode);
}, },
/** sortOptions() {
* Get the index of the default option if it exists. If not, use the index of this.options.sort((a, b) => {
* the first color mode in the color modes array. return alphabeticalCompare(getColorModeString(a), getColorModeString(b));
* @return {number}
* @private
*/
getDefaultSelectedColorModeIndex_() {
assert(this.colorModes.length > 0);
const defaultColorModeIndex = this.colorModes.findIndex((colorMode) => {
return this.isDefaultColorMode_(colorMode);
}); });
return defaultColorModeIndex === -1 ? 0 : defaultColorModeIndex;
},
/**
* Sorts the color modes and sets the selected color mode when the color modes
* array changes.
* @private
*/
onColorModesChange_() {
if (this.colorModes.length > 1) {
this.colorModes.sort((a, b) => {
return alphabeticalCompare(
getColorModeString(a), getColorModeString(b));
});
}
if (this.colorModes.length > 0) {
const selectedColorModeIndex = this.getDefaultSelectedColorModeIndex_();
this.selectedColorMode =
this.colorModes[selectedColorModeIndex].toString();
this.$.colorModeSelect.selectedIndex = selectedColorModeIndex;
}
}, },
/** /**
* @param {!chromeos.scanning.mojom.ColorMode} colorMode * @param {!chromeos.scanning.mojom.ColorMode} option
* @return {boolean} * @return {boolean}
* @private
*/ */
isDefaultColorMode_(colorMode) { isDefaultOption(option) {
return colorMode === DEFAULT_COLOR_MODE; return option === DEFAULT_COLOR_MODE;
}, },
}); });
...@@ -9,8 +9,6 @@ import './strings.m.js'; ...@@ -9,8 +9,6 @@ import './strings.m.js';
import {I18nBehavior} from 'chrome://resources/js/i18n_behavior.m.js'; import {I18nBehavior} from 'chrome://resources/js/i18n_behavior.m.js';
import {html, Polymer} from 'chrome://resources/polymer/v3_0/polymer/polymer_bundled.min.js'; import {html, Polymer} from 'chrome://resources/polymer/v3_0/polymer/polymer_bundled.min.js';
import {SelectBehavior} from './select_behavior.js';
/** /**
* @fileoverview * @fileoverview
* 'file-type-select' displays the available file types in a dropdown. * 'file-type-select' displays the available file types in a dropdown.
...@@ -20,9 +18,12 @@ Polymer({ ...@@ -20,9 +18,12 @@ Polymer({
_template: html`{__html_template__}`, _template: html`{__html_template__}`,
behaviors: [I18nBehavior, SelectBehavior], behaviors: [I18nBehavior],
properties: { properties: {
/** @type {boolean} */
disabled: Boolean,
/** @type {string} */ /** @type {string} */
selectedFileType: { selectedFileType: {
type: String, type: String,
......
...@@ -5,11 +5,10 @@ ...@@ -5,11 +5,10 @@
</span> </span>
<div slot="settings"> <div slot="settings">
<select id="pageSizeSelect" class="md-select" <select id="pageSizeSelect" class="md-select"
value="{{selectedPageSize::change}}" disabled="[[disabled]]" value="{{selectedOption::change}}" disabled="[[disabled]]"
aria-labelledby="pageSizeLabel"> aria-labelledby="pageSizeLabel">
<template is="dom-repeat" items="[[pageSizes]]" as="pageSize"> <template is="dom-repeat" items="[[options]]" as="pageSize">
<option value="[[pageSize]]" <option value="[[pageSize]]" selected$="[[isDefaultOption(pageSize)]]">
selected$="[[isDefaultPageSize_(pageSize)]]">
[[getPageSizeString_(pageSize)]] [[getPageSizeString_(pageSize)]]
</option> </option>
</template> </template>
......
...@@ -6,6 +6,7 @@ import './scanning.mojom-lite.js'; ...@@ -6,6 +6,7 @@ import './scanning.mojom-lite.js';
import './scan_settings_section.js'; import './scan_settings_section.js';
import './strings.m.js'; import './strings.m.js';
import {assert} from 'chrome://resources/js/assert.m.js';
import {I18nBehavior} from 'chrome://resources/js/i18n_behavior.m.js'; import {I18nBehavior} from 'chrome://resources/js/i18n_behavior.m.js';
import {html, Polymer} from 'chrome://resources/polymer/v3_0/polymer/polymer_bundled.min.js'; import {html, Polymer} from 'chrome://resources/polymer/v3_0/polymer/polymer_bundled.min.js';
...@@ -26,22 +27,16 @@ Polymer({ ...@@ -26,22 +27,16 @@ Polymer({
behaviors: [I18nBehavior, SelectBehavior], behaviors: [I18nBehavior, SelectBehavior],
properties: { /**
/** @type {!Array<chromeos.scanning.mojom.PageSize>} */ * @param {number} index
pageSizes: { * @return {string}
type: Array, */
value: () => [], getOptionAtIndex(index) {
}, assert(index < this.options.length);
/** @type {string} */ return this.options[index].toString();
selectedPageSize: {
type: String,
notify: true,
},
}, },
observers: ['onPageSizesChange_(pageSizes.*)'],
/** /**
* @param {!chromeos.scanning.mojom.PageSize} pageSize * @param {!chromeos.scanning.mojom.PageSize} pageSize
* @return {string} * @return {string}
...@@ -51,54 +46,28 @@ Polymer({ ...@@ -51,54 +46,28 @@ Polymer({
return getPageSizeString(pageSize); return getPageSizeString(pageSize);
}, },
/** sortOptions() {
* Get the index of the default option if it exists. If not, use the index of this.options.sort((a, b) => {
* the first page size in the page sizes array. return alphabeticalCompare(getPageSizeString(a), getPageSizeString(b));
* @return {number}
* @private
*/
getDefaultSelectedPageSizeIndex_() {
let defaultPageSizeIndex = this.pageSizes.findIndex((pageSize) => {
return this.isDefaultPageSize_(pageSize);
}); });
return defaultPageSizeIndex === -1 ? 0 : defaultPageSizeIndex; // If the fit to scan area option exists, move it to the end of the page
}, // sizes array.
const fitToScanAreaIndex = this.options.findIndex((pageSize) => {
/** return pageSize === chromeos.scanning.mojom.PageSize.kMax;
* Sorts the page sizes and sets the selected page size when the page sizes });
* array changes.
* @private
*/
onPageSizesChange_() {
if (this.pageSizes.length > 1) {
this.pageSizes.sort((a, b) => {
return alphabeticalCompare(getPageSizeString(a), getPageSizeString(b));
});
// If the fit to scan area option exists, move it to the end of the page
// sizes array.
const fitToScanAreaIndex = this.pageSizes.findIndex((pageSize) => {
return pageSize === chromeos.scanning.mojom.PageSize.kMax;
});
if (fitToScanAreaIndex !== -1) {
this.pageSizes.push(this.pageSizes.splice(fitToScanAreaIndex, 1)[0]);
}
}
if (this.pageSizes.length > 0) { if (fitToScanAreaIndex !== -1) {
const selectedPageSizeIndex = this.getDefaultSelectedPageSizeIndex_(); this.options.push(this.options.splice(fitToScanAreaIndex, 1)[0]);
this.selectedPageSize = this.pageSizes[selectedPageSizeIndex].toString();
this.$.pageSizeSelect.selectedIndex = selectedPageSizeIndex;
} }
}, },
/** /**
* @param {!chromeos.scanning.mojom.PageSize} pageSize * @param {!chromeos.scanning.mojom.PageSize} option
* @return {boolean} * @return {boolean}
* @private
*/ */
isDefaultPageSize_(pageSize) { isDefaultOption(option) {
return pageSize === DEFAULT_PAGE_SIZE; return option === DEFAULT_PAGE_SIZE;
}, },
}); });
...@@ -5,11 +5,11 @@ ...@@ -5,11 +5,11 @@
</span> </span>
<div slot="settings"> <div slot="settings">
<select id="resolutionSelect" class="md-select" <select id="resolutionSelect" class="md-select"
value="{{selectedResolution::change}}" disabled="[[disabled]]" value="{{selectedOption::change}}" disabled="[[disabled]]"
aria-labelledby="resolutionLabel"> aria-labelledby="resolutionLabel">
<template is="dom-repeat" items="[[resolutions]]" as="resolution"> <template is="dom-repeat" items="[[options]]" as="resolution">
<option value="[[resolution]]" <option value="[[resolution]]"
selected$="[[isDefaultResolution_(resolution)]]"> selected$="[[isDefaultOption(resolution)]]">
[[getResolutionString_(resolution)]] [[getResolutionString_(resolution)]]
</option> </option>
</template> </template>
......
...@@ -6,8 +6,8 @@ import './scanning.mojom-lite.js'; ...@@ -6,8 +6,8 @@ import './scanning.mojom-lite.js';
import './scan_settings_section.js'; import './scan_settings_section.js';
import './strings.m.js'; import './strings.m.js';
import {assert} from 'chrome://resources/js/assert.m.js';
import {I18nBehavior} from 'chrome://resources/js/i18n_behavior.m.js'; import {I18nBehavior} from 'chrome://resources/js/i18n_behavior.m.js';
import {loadTimeData} from 'chrome://resources/js/load_time_data.m.js';
import {html, Polymer} from 'chrome://resources/polymer/v3_0/polymer/polymer_bundled.min.js'; import {html, Polymer} from 'chrome://resources/polymer/v3_0/polymer/polymer_bundled.min.js';
import {SelectBehavior} from './select_behavior.js'; import {SelectBehavior} from './select_behavior.js';
...@@ -26,73 +26,37 @@ Polymer({ ...@@ -26,73 +26,37 @@ Polymer({
behaviors: [I18nBehavior, SelectBehavior], behaviors: [I18nBehavior, SelectBehavior],
properties: { /**
/** @type {!Array<number>} */ * @param {number} index
resolutions: { * @return {string}
type: Array, */
value: () => [], getOptionAtIndex(index) {
}, assert(index < this.options.length);
/** @type {string} */ return this.options[index].toString();
selectedResolution: {
type: String,
notify: true,
},
}, },
observers: ['onResolutionsChange_(resolutions.*)'],
/** /**
* @param {number} resolution * @param {number} resolution
* @return {string} * @return {string}
* @private * @private
*/ */
getResolutionString_(resolution) { getResolutionString_(resolution) {
return loadTimeData.getStringF( return this.i18n('resolutionOptionText', resolution);
'resolutionOptionText', resolution.toString());
}, },
/** sortOptions() {
* Get the index of the default option if it exists. If not, use the index of // Sort the resolutions in descending order.
* the first resolution in the resolutions array. this.options.sort(function(a, b) {
* @return {number} return b - a;
* @private
*/
getDefaultSelectedResolutionIndex_() {
const defaultResolutionIndex = this.resolutions.findIndex((resolution) => {
return this.isDefaultResolution_(resolution);
}); });
return defaultResolutionIndex === -1 ? 0 : defaultResolutionIndex;
}, },
/** /**
* Sorts the resolutions and sets the selected resolution when the resolutions * @param {number} option
* array changes.
* @private
*/
onResolutionsChange_() {
if (this.resolutions.length > 1) {
// Sort the resolutions in descending order.
this.resolutions.sort(function(a, b) {
return b - a;
});
}
if (this.resolutions.length > 0) {
const selectedResolutionIndex = this.getDefaultSelectedResolutionIndex_();
this.selectedResolution =
this.resolutions[selectedResolutionIndex].toString();
this.$.resolutionSelect.selectedIndex = selectedResolutionIndex;
}
},
/**
* @param {number} resolution
* @return {boolean} * @return {boolean}
* @private
*/ */
isDefaultResolution_(resolution) { isDefaultOption(option) {
return resolution === DEFAULT_RESOLUTION; return option === DEFAULT_RESOLUTION;
}, },
}); });
...@@ -9,7 +9,6 @@ import {I18nBehavior} from 'chrome://resources/js/i18n_behavior.m.js'; ...@@ -9,7 +9,6 @@ import {I18nBehavior} from 'chrome://resources/js/i18n_behavior.m.js';
import {html, Polymer} from 'chrome://resources/polymer/v3_0/polymer/polymer_bundled.min.js'; import {html, Polymer} from 'chrome://resources/polymer/v3_0/polymer/polymer_bundled.min.js';
import {ScanningBrowserProxy, ScanningBrowserProxyImpl, SelectedPath} from './scanning_browser_proxy.js'; import {ScanningBrowserProxy, ScanningBrowserProxyImpl, SelectedPath} from './scanning_browser_proxy.js';
import {SelectBehavior} from './select_behavior.js';
/** /**
* @fileoverview * @fileoverview
...@@ -20,12 +19,15 @@ Polymer({ ...@@ -20,12 +19,15 @@ Polymer({
_template: html`{__html_template__}`, _template: html`{__html_template__}`,
behaviors: [I18nBehavior, SelectBehavior], behaviors: [I18nBehavior],
/** @private {?ScanningBrowserProxy}*/ /** @private {?ScanningBrowserProxy}*/
browserProxy_: null, browserProxy_: null,
properties: { properties: {
/** @type {boolean} */
disabled: Boolean,
/** /**
* The lowest level directory in |selectedFilePath|. * The lowest level directory in |selectedFilePath|.
* @type {string} * @type {string}
......
...@@ -15,7 +15,6 @@ import {html, Polymer} from 'chrome://resources/polymer/v3_0/polymer/polymer_bun ...@@ -15,7 +15,6 @@ import {html, Polymer} from 'chrome://resources/polymer/v3_0/polymer/polymer_bun
import {ScannerArr} from './scanning_app_types.js'; import {ScannerArr} from './scanning_app_types.js';
import {alphabeticalCompare, getScannerDisplayName, tokenToString} from './scanning_app_util.js'; import {alphabeticalCompare, getScannerDisplayName, tokenToString} from './scanning_app_util.js';
import {SelectBehavior} from './select_behavior.js';
/** /**
* @fileoverview * @fileoverview
...@@ -26,9 +25,12 @@ Polymer({ ...@@ -26,9 +25,12 @@ Polymer({
_template: html`{__html_template__}`, _template: html`{__html_template__}`,
behaviors: [I18nBehavior, SelectBehavior], behaviors: [I18nBehavior],
properties: { properties: {
/** @type {boolean} */
disabled: Boolean,
/** @type {!ScannerArr} */ /** @type {!ScannerArr} */
scanners: { scanners: {
type: Array, type: Array,
......
...@@ -190,9 +190,9 @@ ...@@ -190,9 +190,9 @@
loaded="[[scannersLoaded_]]" loaded="[[scannersLoaded_]]"
disabled="[[settingsDisabled_]]" disabled="[[settingsDisabled_]]"
selected-scanner-id="{{selectedScannerId}}"></scanner-select> selected-scanner-id="{{selectedScannerId}}"></scanner-select>
<source-select id="sourceSelect" sources="[[capabilities_.sources]]" <source-select id="sourceSelect" options="[[capabilities_.sources]]"
disabled="[[settingsDisabled_]]" disabled="[[settingsDisabled_]]"
selected-source="{{selectedSource}}"></source-select> selected-option="{{selectedSource}}"></source-select>
<scan-to-select id="scanToSelect" <scan-to-select id="scanToSelect"
disabled="[[settingsDisabled_]]" disabled="[[settingsDisabled_]]"
selected-file-path="{{selectedFilePath}}" selected-file-path="{{selectedFilePath}}"
...@@ -211,19 +211,19 @@ ...@@ -211,19 +211,19 @@
</cr-button> </cr-button>
<iron-collapse id="collapse" opened="{{opened_}}"> <iron-collapse id="collapse" opened="{{opened_}}">
<color-mode-select id="colorModeSelect" <color-mode-select id="colorModeSelect"
color-modes="[[capabilities_.colorModes]]" options="[[capabilities_.colorModes]]"
disabled="[[settingsDisabled_]]" disabled="[[settingsDisabled_]]"
selected-color-mode="{{selectedColorMode}}"> selected-option="{{selectedColorMode}}">
</color-mode-select> </color-mode-select>
<page-size-select id="pageSizeSelect" <page-size-select id="pageSizeSelect"
page-sizes="[[selectedSourcePageSizes_]]" options="[[selectedSourcePageSizes_]]"
disabled="[[settingsDisabled_]]" disabled="[[settingsDisabled_]]"
selected-page-size="{{selectedPageSize}}"> selected-option="{{selectedPageSize}}">
</page-size-select> </page-size-select>
<resolution-select id="resolutionSelect" <resolution-select id="resolutionSelect"
resolutions="[[capabilities_.resolutions]]" options="[[capabilities_.resolutions]]"
disabled="[[settingsDisabled_]]" disabled="[[settingsDisabled_]]"
selected-resolution="{{selectedResolution}}"> selected-option="{{selectedResolution}}">
</resolution-select> </resolution-select>
</iron-collapse> </iron-collapse>
</template> </template>
......
...@@ -2,16 +2,64 @@ ...@@ -2,16 +2,64 @@
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
import {assert} from 'chrome://resources/js/assert.m.js';
import {html, Polymer} from 'chrome://resources/polymer/v3_0/polymer/polymer_bundled.min.js'; import {html, Polymer} from 'chrome://resources/polymer/v3_0/polymer/polymer_bundled.min.js';
/** /**
* Helper functions for custom elements that implement a scan setting using a * Helper functions for custom elements that implement a scan setting using a
* single select element. * single select element. Elements that use this behavior are required to
* implement getOptionAtIndex(), sortOptions(), and isDefaultOption().
* @polymerBehavior * @polymerBehavior
*/ */
export const SelectBehavior = { export const SelectBehavior = {
properties: { properties: {
/** @type {boolean} */ /** @type {boolean} */
disabled: Boolean, disabled: Boolean,
/** @type {!Array} */
options: {
type: Array,
value: () => [],
},
/** @type {string} */
selectedOption: {
type: String,
notify: true,
},
},
observers: ['onOptionsChange_(options.*)'],
/**
* Get the index of the default option if it exists. If not, use the index of
* the first option in the options array.
* @return {number}
* @private
*/
getDefaultSelectedIndex_() {
assert(this.options.length > 0);
const defaultIndex = this.options.findIndex((option) => {
return this.isDefaultOption(option);
});
return defaultIndex === -1 ? 0 : defaultIndex;
},
/**
* Sorts the options and sets the selected option.
* @private
*/
onOptionsChange_() {
if (this.options.length > 1) {
this.sortOptions();
}
if (this.options.length > 0) {
const selectedOptionIndex = this.getDefaultSelectedIndex_();
this.selectedOption = this.getOptionAtIndex(selectedOptionIndex);
this.$$('select').selectedIndex = selectedOptionIndex;
}
}, },
}; };
...@@ -5,11 +5,10 @@ ...@@ -5,11 +5,10 @@
</span> </span>
<div slot="settings"> <div slot="settings">
<select id="sourceSelect" class="md-select" <select id="sourceSelect" class="md-select"
value="{{selectedSource::change}}" disabled="[[disabled]]" value="{{selectedOption::change}}" disabled="[[disabled]]"
aria-labelledby="sourceLabel"> aria-labelledby="sourceLabel">
<template is="dom-repeat" items="[[sources]]" as="source"> <template is="dom-repeat" items="[[options]]" as="source">
<option value="[[source.name]]" <option value="[[source.name]]" selected$='[[isDefaultOption(source)]]'>
selected$='[[isDefaultSource_(source.type)]]'>
[[getSourceTypeString_(source.type)]] [[getSourceTypeString_(source.type)]]
</option> </option>
</template> </template>
......
...@@ -6,12 +6,16 @@ import './scanning.mojom-lite.js'; ...@@ -6,12 +6,16 @@ import './scanning.mojom-lite.js';
import './scan_settings_section.js'; import './scan_settings_section.js';
import './strings.m.js'; import './strings.m.js';
import {assert} from 'chrome://resources/js/assert.m.js';
import {I18nBehavior} from 'chrome://resources/js/i18n_behavior.m.js'; import {I18nBehavior} from 'chrome://resources/js/i18n_behavior.m.js';
import {html, Polymer} from 'chrome://resources/polymer/v3_0/polymer/polymer_bundled.min.js'; import {html, Polymer} from 'chrome://resources/polymer/v3_0/polymer/polymer_bundled.min.js';
import {alphabeticalCompare, getSourceTypeString} from './scanning_app_util.js'; import {alphabeticalCompare, getSourceTypeString} from './scanning_app_util.js';
import {SelectBehavior} from './select_behavior.js'; import {SelectBehavior} from './select_behavior.js';
/** @type {chromeos.scanning.mojom.SourceType} */
const DEFAULT_SOURCE_TYPE = chromeos.scanning.mojom.SourceType.kFlatbed;
/** /**
* @fileoverview * @fileoverview
* 'source-select' displays the available scanner sources in a dropdown. * 'source-select' displays the available scanner sources in a dropdown.
...@@ -23,22 +27,16 @@ Polymer({ ...@@ -23,22 +27,16 @@ Polymer({
behaviors: [I18nBehavior, SelectBehavior], behaviors: [I18nBehavior, SelectBehavior],
properties: { /**
/** @type {!Array<!chromeos.scanning.mojom.ScanSource>} */ * @param {number} index
sources: { * @return {string}
type: Array, */
value: () => [], getOptionAtIndex(index) {
}, assert(index < this.options.length);
/** @type {string} */ return this.options[index].name;
selectedSource: {
type: String,
notify: true,
},
}, },
observers: ['onSourcesChange_(sources.*)'],
/** /**
* @param {chromeos.scanning.mojom.SourceType} mojoSourceType * @param {chromeos.scanning.mojom.SourceType} mojoSourceType
* @return {string} * @return {string}
...@@ -48,44 +46,18 @@ Polymer({ ...@@ -48,44 +46,18 @@ Polymer({
return getSourceTypeString(mojoSourceType); return getSourceTypeString(mojoSourceType);
}, },
/** sortOptions() {
* "Flatbed" should always be the default option if it exists. If not, use this.options.sort((a, b) => {
* the first source in the sources array. return alphabeticalCompare(
* @return {string} getSourceTypeString(a.type), getSourceTypeString(b.type));
* @private
*/
getDefaultSelectedSource_() {
const flatbedSourceIndex = this.sources.findIndex((source) => {
return source.type === chromeos.scanning.mojom.SourceType.kFlatbed;
}); });
return flatbedSourceIndex === -1 ? this.sources[0].name :
this.sources[flatbedSourceIndex].name;
},
/**
* Sorts the sources and sets the selected source when sources change.
* @private
*/
onSourcesChange_() {
if (this.sources.length > 1) {
this.sources.sort((a, b) => {
return alphabeticalCompare(
getSourceTypeString(a.type), getSourceTypeString(b.type));
});
}
if (this.sources.length > 0) {
this.selectedSource = this.getDefaultSelectedSource_();
}
}, },
/** /**
* @param {!chromeos.scanning.mojom.SourceType} sourceType * @param {!chromeos.scanning.mojom.ScanSource} option
* @return {boolean} * @return {boolean}
* @private
*/ */
isDefaultSource_(sourceType) { isDefaultOption(option) {
return sourceType === chromeos.scanning.mojom.SourceType.kFlatbed; return option.type === DEFAULT_SOURCE_TYPE;
}, },
}); });
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment