- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / browser / resources / options / chromeos / accounts_user_name_edit.js
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   /**
7    * Email alias only, assuming it's a gmail address.
8    *   e.g. 'john'
9    *        {name: 'john', email: 'john@gmail.com'}
10    * @const
11    */
12   var format1String =
13       '^\\s*([\\w\\.!#\\$%&\'\\*\\+-\\/=\\?\\^`\\{\\|\\}~]+)\\s*$';
14   /**
15    * Email address only.
16    *   e.g. 'john@chromium.org'
17    *        {name: 'john', email: 'john@chromium.org'}
18    * @const
19    */
20   var format2String =
21       '^\\s*([\\w\\.!#\\$%&\'\\*\\+-\\/=\\?\\^`\\{\\|\\}~]+)@' +
22       '([A-Za-z0-9\-]{2,63}\\..+)\\s*$';
23   /**
24    * Full format.
25    *   e.g. '"John Doe" <john@chromium.org>'
26    *        {name: 'John doe', email: 'john@chromium.org'}
27    * @const
28    */
29   var format3String =
30       '^\\s*"{0,1}([^"]+)"{0,1}\\s*' +
31       '<([\\w\\.!#\\$%&\'\\*\\+-\\/=\\?\\^`\\{\\|\\}~]+@' +
32       '[A-Za-z0-9\-]{2,63}\\..+)>\\s*$';
33
34   /**
35    * Creates a new user name edit element.
36    * @param {Object=} opt_propertyBag Optional properties.
37    * @constructor
38    * @extends {HTMLInputElement}
39    */
40   var UserNameEdit = cr.ui.define('input');
41
42   UserNameEdit.prototype = {
43     __proto__: HTMLInputElement.prototype,
44
45     /**
46      * Called when an element is decorated as a user name edit.
47      */
48     decorate: function() {
49       this.pattern = format1String + '|' + format2String + '|' +
50                      format3String;
51
52       this.onkeydown = this.handleKeyDown_.bind(this);
53     },
54
55
56     /**
57      * Parses given str for user info.
58      *
59      * 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
61      * as valid for an email alias (aka local-part):
62      *   - Letters: a–z, A–Z
63      *   - Digits: 0-9
64      *   - Characters: ! # $ % & ' * + - / = ? ^ _ ` { | } ~
65      *   - 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
67      *       more times in a row.)
68      *
69      * @param {string} str A string to parse.
70      * @return {{name: string, email: string}} User info parsed from the string.
71      */
72     parse: function(str) {
73       /** @const */ var format1 = new RegExp(format1String);
74       /** @const */ var format2 = new RegExp(format2String);
75       /** @const */ var format3 = new RegExp(format3String);
76
77       var matches = format1.exec(str);
78       if (matches) {
79         return {
80           name: matches[1],
81           email: matches[1] + '@gmail.com'
82         };
83       }
84
85       matches = format2.exec(str);
86       if (matches) {
87         return {
88           name: matches[1],
89           email: matches[1] + '@' + matches[2]
90         };
91       }
92
93       matches = format3.exec(str);
94       if (matches) {
95         return {
96           name: matches[1],
97           email: matches[2]
98         };
99       }
100
101       return null;
102     },
103
104     /**
105      * Handler for key down event.
106      * @private
107      * @param {!Event} e The keydown event object.
108      */
109     handleKeyDown_: function(e) {
110       if (e.keyIdentifier == 'Enter') {
111         var user = this.parse(this.value);
112         if (user) {
113           var event = new Event('add');
114           event.user = user;
115           this.dispatchEvent(event);
116         }
117         this.select();
118         // Avoid double-handling so the dialog doesn't close.
119         e.stopPropagation();
120       }
121     }
122   };
123
124   return {
125     UserNameEdit: UserNameEdit
126   };
127 });
128