Index: chrome/browser/resources/options2/chromeos/internet_detail_ip_address_field.js |
diff --git a/chrome/browser/resources/options2/chromeos/internet_detail_ip_address_field.js b/chrome/browser/resources/options2/chromeos/internet_detail_ip_address_field.js |
new file mode 100644 |
index 0000000000000000000000000000000000000000..2009771051167627136ad068cef1cc3cebcdfdf4 |
--- /dev/null |
+++ b/chrome/browser/resources/options2/chromeos/internet_detail_ip_address_field.js |
@@ -0,0 +1,107 @@ |
+// Copyright (c) 2012 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
Dan Beam
2012/08/11 01:38:02
nit: one less \n here, IMO
|
+ |
+cr.define('options.internet', function() { |
+ /** @const */ var EditableTextField = options.EditableTextField; |
+ |
+ /** |
+ * The regular expression that matches an IP address, allowing for |
+ * leading/trailing/embedded whitespace. |
+ * @const |
+ * @private |
Dan Beam
2012/08/11 01:38:02
don't really need @private if this isn't a member
|
+ * @type {RegExp} |
+ */ |
+ var singleIp_ = new RegExp('^\\s*([0-9]+)\\s*\\.\\s*([0-9]+)\\s*\\.' + |
Dan Beam
2012/08/11 01:38:02
I'd highly recommend writing this in literal form
Greg Spencer (Chromium)
2012/08/13 19:20:04
Yeah, I tried that already, but the problem is tha
Dan Beam
2012/08/13 19:48:55
were you passing the /g (global) flag to the RegEx
Greg Spencer (Chromium)
2012/08/13 22:17:44
Yeah, I'm pretty familiar with regexps, and no, I
Dan Beam
2012/08/13 22:56:18
Yeah, OK.
|
+ '\\s*([0-9]+)\\s*\\.\\s*([0-9]+)\\s*$'); |
+ |
+ /** |
+ * Creates a new field specifically for entering IP addresses. |
+ * @constructor |
+ */ |
+ function IPAddressField() { |
+ var el = cr.doc.createElement('div'); |
+ IPAddressField.decorate(el); |
+ return el; |
+ }; |
Dan Beam
2012/08/11 01:38:02
nit: no ; when using function declarations() { }
Greg Spencer (Chromium)
2012/08/13 19:20:04
Done.
|
+ |
+ /** |
+ * Decorates an element as a inline-editable list item. Note that this is |
+ * a subclass of IPAddressField. |
+ * @param {!HTMLElement} el The element to decorate. |
+ */ |
+ IPAddressField.decorate = function(el) { |
+ el.__proto__ = IPAddressField.prototype; |
+ el.decorate(); |
+ }; |
+ |
+ IPAddressField.prototype = { |
+ __proto__: EditableTextField.prototype, |
+ |
+ /** @inheritDoc */ |
Dan Beam
2012/08/11 01:38:02
@inheritDoc is deprecated now, use @override inste
Greg Spencer (Chromium)
2012/08/13 19:20:04
Done.
|
+ decorate: function() { |
+ EditableTextField.prototype.decorate.call(this); |
+ }, |
+ |
+ /** |
+ * Indicates whether or not empty values are allowed. |
+ * @type {boolean} |
+ */ |
+ get allowEmpty() { |
+ return this.hasAttribute('allow-empty'); |
+ }, |
+ |
+ /** @inheritDoc */ |
+ get currentInputIsValid() { |
+ if (!this.editField.value && this.allowEmpty) |
+ return true; |
+ |
+ // Make sure it's only got numbers and ".", there are the correct |
+ // count of them, and they are all within the correct range. |
+ var matches = singleIp_.exec(this.editField.value); |
+ var range_correct = true; |
Dan Beam
2012/08/11 01:38:02
var rangeCorrent (camelCase in JavaScript)
Greg Spencer (Chromium)
2012/08/13 19:20:04
aaagh! Switching back and forth between C++ and J
|
+ if (matches != null) { |
+ for (var i = 1; i < matches.length; ++i) { |
+ var value = parseInt(matches[i], 10); |
+ if (value < 0 || value > 255) |
+ range_correct = false; |
Dan Beam
2012/08/11 01:38:02
why not break; here?
Greg Spencer (Chromium)
2012/08/13 19:20:04
Why indeed. Done.
|
+ } |
+ } |
+ return (this.editField.validity.valid && matches != null && |
Dan Beam
2012/08/11 01:38:02
nit: less ()
Greg Spencer (Chromium)
2012/08/13 19:20:04
Done.
|
+ range_correct && matches.length == 5); |
+ }, |
+ |
+ /** @inheritDoc */ |
+ get hasBeenEdited() { |
+ return this.editField.value != this.model.value; |
+ }, |
+ |
+ /** |
+ * Mutates the input during a successful commit. For the purposes of |
Dan Beam
2012/08/11 01:38:02
nit: 1 \s between sentences in comments (you have
Greg Spencer (Chromium)
2012/08/13 19:20:04
Done.
|
+ * entering IP addresses, this just means stripping off whitespace and |
+ * leading zeros from each of the octets so that they conform to the normal |
+ * format for IP addresses. |
+ * @param {Object} value Input IP address to be mutated. |
Dan Beam
2012/08/11 01:38:02
shouldn't @param and @return be a {string}?
Greg Spencer (Chromium)
2012/08/13 19:20:04
Yeah, I guess so. I was originally going to mutat
|
+ * @return {Object} Mutated IP address. |
+ */ |
+ mutateInput: function(value) { |
+ if (!value) |
+ return value; |
Dan Beam
2012/08/11 01:38:02
nit: \n after this if ()
Greg Spencer (Chromium)
2012/08/13 19:20:04
Done.
|
+ var model = this.model; |
+ var matches = singleIp_.exec(value); |
Dan Beam
2012/08/11 01:38:02
seems like you could simplify this to:
return
Greg Spencer (Chromium)
2012/08/13 19:20:04
Well, and the fact that it doesn't quite work.. :)
|
+ var result = []; |
+ if (matches != null) { // should never be null if we got this far |
Dan Beam
2012/08/11 01:38:02
make a full sentence if this loop sticks around
Greg Spencer (Chromium)
2012/08/13 19:20:04
Done.
|
+ for (var i = 1; i < matches.length; ++i) { |
+ var octet = parseInt(matches[i], 10); |
+ result.push(octet); |
Dan Beam
2012/08/11 01:38:02
inline octect if this loop sticks around
Greg Spencer (Chromium)
2012/08/13 19:20:04
Done.
|
+ } |
+ } |
+ return result.join('.'); |
+ }, |
+ }; |
+ |
+ return { |
+ IPAddressField: IPAddressField, |
+ }; |
+}); |