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