No dual_mode on Win10+ shortcuts.
[chromium-blink-merge.git] / chrome / browser / resources / options / chromeos / internet_detail_ip_address_field.js
blobd4cd42a1f8beeb52a11834bf56112d2a71207b36
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 cr.define('options.internet', function() {
6 /** @const */ var EditableTextField = options.EditableTextField;
8 /**
9 * The regular expression that matches an IP address. String to match against
10 * should have all whitespace stripped already.
11 * @const
12 * @type {RegExp}
14 var singleIp_ = /^([0-9]+)\.([0-9]+)\.([0-9]+)\.([0-9]+)$/;
16 /**
17 * Creates a new field specifically for entering IP addresses.
18 * @constructor
19 * @extends {options.EditableTextField}
21 function IPAddressField() {
22 var el = cr.doc.createElement('div');
23 IPAddressField.decorate(el);
24 return el;
27 /**
28 * Decorates an element as a inline-editable list item. Note that this is
29 * a subclass of IPAddressField.
30 * @param {!HTMLElement} el The element to decorate.
32 IPAddressField.decorate = function(el) {
33 el.__proto__ = IPAddressField.prototype;
34 el.decorate();
37 IPAddressField.prototype = {
38 __proto__: EditableTextField.prototype,
40 /** @override */
41 decorate: function() {
42 EditableTextField.prototype.decorate.call(this);
45 /**
46 * Indicates whether or not empty values are allowed.
47 * @type {boolean}
49 get allowEmpty() {
50 return this.hasAttribute('allow-empty');
53 /** @override */
54 get currentInputIsValid() {
55 if (!this.editField.value && this.allowEmpty)
56 return true;
58 // Make sure it's only got numbers and ".", there are the correct
59 // count of them, and they are all within the correct range.
60 var fieldValue = this.editField.value.replace(/\s/g, '');
61 var matches = singleIp_.exec(fieldValue);
62 var rangeCorrect = true;
63 if (matches != null) {
64 for (var i = 1; i < matches.length; ++i) {
65 var value = parseInt(matches[i], 10);
66 if (value < 0 || value > 255) {
67 rangeCorrect = false;
68 break;
72 return this.editField.validity.valid && matches != null &&
73 rangeCorrect && matches.length == 5;
76 /** @override */
77 get hasBeenEdited() {
78 return this.editField.value != this.model.value;
81 /**
82 * Overrides superclass to mutate the input during a successful commit. For
83 * the purposes of entering IP addresses, this just means stripping off
84 * whitespace and leading zeros from each of the octets so that they conform
85 * to the normal format for IP addresses.
86 * @override
87 * @param {string} value Input IP address to be mutated.
88 * @return {string} mutated IP address.
90 mutateInput: function(value) {
91 if (!value)
92 return value;
94 var fieldValue = value.replace(/\s/g, '');
95 var matches = singleIp_.exec(fieldValue);
96 var result = [];
98 // If we got this far, matches shouldn't be null, but make sure.
99 if (matches != null) {
100 // starting at one because the first match element contains the entire
101 // match, and we don't care about that.
102 for (var i = 1; i < matches.length; ++i)
103 result.push(parseInt(matches[i], 10));
105 return result.join('.');
109 return {
110 IPAddressField: IPAddressField,