OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 cr.define('options.accounts', function() { | 5 cr.define('options.accounts', function() { |
6 /** | 6 /** |
7 * Email alias only, assuming it's a gmail address. | 7 * Email alias only, assuming it's a gmail address. |
8 * e.g. 'john' | 8 * e.g. 'john' |
9 * {name: 'john', email: 'john@gmail.com'} | 9 * {name: 'john', email: 'john@gmail.com'} |
10 * @const | 10 * @const |
11 */ | 11 */ |
12 var format1String = | 12 var format1String = |
13 '^\\s*([\\w\\.!#\\$%&\'\\*\\+-\\/=\\?\\^`\\{\\|\\}~]+)\\s*$'; | 13 '^\\s*([\\w\\.!#\\$%&\'\\*\\+-\\/=\\?\\^`\\{\\|\\}~]+)\\s*$'; |
14 /** | 14 /** |
15 * Email address only. | 15 * Email address only. |
16 * e.g. 'john@chromium.org' | 16 * e.g. 'john@chromium.org' |
17 * {name: 'john', email: 'john@chromium.org'} | 17 * {name: 'john', email: 'john@chromium.org'} |
18 * @const | 18 * @const |
19 */ | 19 */ |
20 var format2String = | 20 var format2String = '^\\s*([\\w\\.!#\\$%&\'\\*\\+-\\/=\\?\\^`\\{\\|\\}~]+)@' + |
21 '^\\s*([\\w\\.!#\\$%&\'\\*\\+-\\/=\\?\\^`\\{\\|\\}~]+)@' + | |
22 '([A-Za-z0-9\-]{2,63}\\..+)\\s*$'; | 21 '([A-Za-z0-9\-]{2,63}\\..+)\\s*$'; |
23 /** | 22 /** |
24 * Full format. | 23 * Full format. |
25 * e.g. '"John Doe" <john@chromium.org>' | 24 * e.g. '"John Doe" <john@chromium.org>' |
26 * {name: 'John doe', email: 'john@chromium.org'} | 25 * {name: 'John doe', email: 'john@chromium.org'} |
27 * @const | 26 * @const |
28 */ | 27 */ |
29 var format3String = | 28 var format3String = '^\\s*"{0,1}([^"]+)"{0,1}\\s*' + |
30 '^\\s*"{0,1}([^"]+)"{0,1}\\s*' + | |
31 '<([\\w\\.!#\\$%&\'\\*\\+-\\/=\\?\\^`\\{\\|\\}~]+@' + | 29 '<([\\w\\.!#\\$%&\'\\*\\+-\\/=\\?\\^`\\{\\|\\}~]+@' + |
32 '[A-Za-z0-9\-]{2,63}\\..+)>\\s*$'; | 30 '[A-Za-z0-9\-]{2,63}\\..+)>\\s*$'; |
33 | 31 |
34 /** | 32 /** |
35 * Creates a new user name edit element. | 33 * Creates a new user name edit element. |
36 * @param {Object=} opt_propertyBag Optional properties. | 34 * @param {Object=} opt_propertyBag Optional properties. |
37 * @constructor | 35 * @constructor |
38 * @extends {HTMLInputElement} | 36 * @extends {HTMLInputElement} |
39 */ | 37 */ |
40 var UserNameEdit = cr.ui.define('input'); | 38 var UserNameEdit = cr.ui.define('input'); |
41 | 39 |
42 UserNameEdit.prototype = { | 40 UserNameEdit.prototype = { |
43 __proto__: HTMLInputElement.prototype, | 41 __proto__: HTMLInputElement.prototype, |
44 | 42 |
45 /** | 43 /** |
46 * Called when an element is decorated as a user name edit. | 44 * Called when an element is decorated as a user name edit. |
47 */ | 45 */ |
48 decorate: function() { | 46 decorate: function() { |
49 this.pattern = format1String + '|' + format2String + '|' + | 47 this.pattern = format1String + '|' + format2String + '|' + format3String; |
50 format3String; | |
51 | 48 |
52 this.onkeydown = this.handleKeyDown_.bind(this); | 49 this.onkeydown = this.handleKeyDown_.bind(this); |
53 }, | 50 }, |
54 | 51 |
55 | 52 |
56 /** | 53 /** |
57 * Parses given str for user info. | 54 * Parses given str for user info. |
58 * | 55 * |
59 * Note that the email parsing is based on RFC 5322 and does not support | 56 * Note that the email parsing is based on RFC 5322 and does not support |
60 * IMA (Internationalized Email Address). We take only the following chars | 57 * IMA (Internationalized Email Address). We take only the following chars |
61 * as valid for an email alias (aka local-part): | 58 * as valid for an email alias (aka local-part): |
62 * - Letters: a–z, A–Z | 59 * - Letters: a–z, A–Z |
63 * - Digits: 0-9 | 60 * - Digits: 0-9 |
64 * - Characters: ! # $ % & ' * + - / = ? ^ _ ` { | } ~ | 61 * - Characters: ! # $ % & ' * + - / = ? ^ _ ` { | } ~ |
65 * - Dot: . (Note that we did not cover the cases that dot should not | 62 * - Dot: . (Note that we did not cover the cases that dot should not |
66 * appear as first or last character and should not appear two or | 63 * appear as first or last character and should not appear two or |
67 * more times in a row.) | 64 * more times in a row.) |
68 * | 65 * |
69 * @param {string} str A string to parse. | 66 * @param {string} str A string to parse. |
70 * @return {?{name: string, email: string}} User info parsed from the | 67 * @return {?{name: string, email: string}} User info parsed from the |
71 * string. | 68 * string. |
72 */ | 69 */ |
73 parse: function(str) { | 70 parse: function(str) { |
74 /** @const */ var format1 = new RegExp(format1String); | 71 /** @const */ var format1 = new RegExp(format1String); |
75 /** @const */ var format2 = new RegExp(format2String); | 72 /** @const */ var format2 = new RegExp(format2String); |
76 /** @const */ var format3 = new RegExp(format3String); | 73 /** @const */ var format3 = new RegExp(format3String); |
77 | 74 |
78 var matches = format1.exec(str); | 75 var matches = format1.exec(str); |
79 if (matches) { | 76 if (matches) { |
80 return { | 77 return {name: matches[1], email: matches[1] + '@gmail.com'}; |
81 name: matches[1], | |
82 email: matches[1] + '@gmail.com' | |
83 }; | |
84 } | 78 } |
85 | 79 |
86 matches = format2.exec(str); | 80 matches = format2.exec(str); |
87 if (matches) { | 81 if (matches) { |
88 return { | 82 return {name: matches[1], email: matches[1] + '@' + matches[2]}; |
89 name: matches[1], | |
90 email: matches[1] + '@' + matches[2] | |
91 }; | |
92 } | 83 } |
93 | 84 |
94 matches = format3.exec(str); | 85 matches = format3.exec(str); |
95 if (matches) { | 86 if (matches) { |
96 return { | 87 return {name: matches[1], email: matches[2]}; |
97 name: matches[1], | |
98 email: matches[2] | |
99 }; | |
100 } | 88 } |
101 | 89 |
102 return null; | 90 return null; |
103 }, | 91 }, |
104 | 92 |
105 /** | 93 /** |
106 * Handler for key down event. | 94 * Handler for key down event. |
107 * @private | 95 * @private |
108 * @param {Event} e The keydown event object. | 96 * @param {Event} e The keydown event object. |
109 */ | 97 */ |
110 handleKeyDown_: function(e) { | 98 handleKeyDown_: function(e) { |
111 if (e.key == 'Enter') { | 99 if (e.key == 'Enter') { |
112 var user = this.parse(this.value); | 100 var user = this.parse(this.value); |
113 if (user) { | 101 if (user) { |
114 var event = new Event('add'); | 102 var event = new Event('add'); |
115 event.user = user; | 103 event.user = user; |
116 this.dispatchEvent(event); | 104 this.dispatchEvent(event); |
117 } | 105 } |
118 this.select(); | 106 this.select(); |
119 // Avoid double-handling so the dialog doesn't close. | 107 // Avoid double-handling so the dialog doesn't close. |
120 e.stopPropagation(); | 108 e.stopPropagation(); |
121 } | 109 } |
122 } | 110 } |
123 }; | 111 }; |
124 | 112 |
125 return { | 113 return {UserNameEdit: UserNameEdit}; |
126 UserNameEdit: UserNameEdit | |
127 }; | |
128 }); | 114 }); |
129 | |
OLD | NEW |