2 * This script refer to:
3 * Title: International Telephone Input
4 * Author: Jack O'Connor
5 * Code version: v12.1.12
6 * Availability: https://github.com/jackocnr/intl-tel-input.git
10 * @class Roo.bootstrap.PhoneInput
11 * @extends Roo.bootstrap.TriggerField
12 * An input with International dial-code selection
14 * @cfg {String} defaultDialCode default '+852'
15 * @cfg {Array} preferedCountries default []
18 * Create a new PhoneInput.
19 * @param {Object} config Configuration options
22 Roo.bootstrap.PhoneInput = function(config) {
23 Roo.bootstrap.PhoneInput.superclass.constructor.call(this, config);
26 Roo.extend(Roo.bootstrap.PhoneInput, Roo.bootstrap.TriggerField, {
30 selectedClass: 'active',
32 invalidClass : "has-warning",
34 validClass: 'has-success',
36 allowed: '0123456789',
41 * @cfg {String} defaultDialCode The default dial code when initializing the input
43 defaultDialCode: '+852',
46 * @cfg {Array} preferedCountries A list of iso2 in array (e.g. ['hk','us']). Those related countries will show at the top of the input's choices
48 preferedCountries: false,
50 getAutoCreate : function()
52 var data = Roo.bootstrap.PhoneInputData();
53 var align = this.labelAlign || this.parentLabelAlign();
56 this.allCountries = [];
57 this.dialCodeMapping = [];
59 for (var i = 0; i < data.length; i++) {
61 this.allCountries[i] = {
66 areaCodes: c[4] || null
68 this.dialCodeMapping[c[2]] = {
72 areaCodes: c[4] || null
84 maxlength: this.max_length,
85 cls : 'form-control tel-input',
86 autocomplete: 'new-password'
92 cls: 'hidden-tel-input'
96 hiddenInput.name = this.name;
100 input.disabled = true;
103 var flag_container = {
120 cls: this.hasFeedback ? 'has-feedback' : '',
126 cls: 'dial-code-holder',
133 cls: 'roo-select2-container input-group',
140 if (this.fieldLabel.length) {
143 tooltip: 'This field is required'
149 cls: 'control-label',
155 html: this.fieldLabel
158 indicator.cls = 'roo-required-indicator text-danger fa fa-lg fa-star left-indicator';
164 if(this.indicatorpos == 'right') {
165 indicator.cls = 'roo-required-indicator text-danger fa fa-lg fa-star right-indicator';
172 if(align == 'left') {
180 if(this.labelWidth > 12){
181 label.style = "width: " + this.labelWidth + 'px';
183 if(this.labelWidth < 13 && this.labelmd == 0){
184 this.labelmd = this.labelWidth;
186 if(this.labellg > 0){
187 label.cls += ' col-lg-' + this.labellg;
188 input.cls += ' col-lg-' + (12 - this.labellg);
190 if(this.labelmd > 0){
191 label.cls += ' col-md-' + this.labelmd;
192 container.cls += ' col-md-' + (12 - this.labelmd);
194 if(this.labelsm > 0){
195 label.cls += ' col-sm-' + this.labelsm;
196 container.cls += ' col-sm-' + (12 - this.labelsm);
198 if(this.labelxs > 0){
199 label.cls += ' col-xs-' + this.labelxs;
200 container.cls += ' col-xs-' + (12 - this.labelxs);
212 ['xs','sm','md','lg'].map(function(size){
213 if (settings[size]) {
214 cfg.cls += ' col-' + size + '-' + settings[size];
218 this.store = new Roo.data.Store({
219 proxy : new Roo.data.MemoryProxy({}),
220 reader : new Roo.data.JsonReader({
239 'name' : 'areaCodes',
246 if(!this.preferedCountries) {
247 this.preferedCountries = [
254 var p = this.preferedCountries.reverse();
257 for (var i = 0; i < p.length; i++) {
258 for (var j = 0; j < this.allCountries.length; j++) {
259 if(this.allCountries[j].iso2 == p[i]) {
260 var t = this.allCountries[j];
261 this.allCountries.splice(j,1);
262 this.allCountries.unshift(t);
268 this.store.proxy.data = {
270 data: this.allCountries
276 initEvents : function()
279 Roo.bootstrap.PhoneInput.superclass.initEvents.call(this);
281 this.indicator = this.indicatorEl();
282 this.flag = this.flagEl();
283 this.dialCodeHolder = this.dialCodeHolderEl();
285 this.trigger = this.el.select('div.flag-box',true).first();
286 this.trigger.on("click", this.onTriggerClick, this, {preventDefault:true});
291 var lw = _this.listWidth || Math.max(_this.inputEl().getWidth(), _this.minListWidth);
292 _this.list.setWidth(lw);
295 this.list.on('mouseover', this.onViewOver, this);
296 this.list.on('mousemove', this.onViewMove, this);
297 this.inputEl().on("keyup", this.onKeyUp, this);
299 this.tpl = '<li><a href="#"><div class="flag {iso2}"></div>{name} <span class="dial-code">+{dialCode}</span></a></li>';
301 this.view = new Roo.View(this.list, this.tpl, {
302 singleSelect:true, store: this.store, selectedClass: this.selectedClass
305 this.view.on('click', this.onViewClick, this);
306 this.setValue(this.defaultDialCode);
309 onTriggerClick : function(e)
311 Roo.log('trigger click');
316 if(this.isExpanded()){
318 this.hasFocus = false;
321 this.hasFocus = true;
326 isExpanded : function()
328 return this.list.isVisible();
331 collapse : function()
333 if(!this.isExpanded()){
337 Roo.get(document).un('mousedown', this.collapseIf, this);
338 Roo.get(document).un('mousewheel', this.collapseIf, this);
339 this.fireEvent('collapse', this);
347 if(this.isExpanded() || !this.hasFocus){
351 var lw = this.listWidth || Math.max(this.inputEl().getWidth(), this.minListWidth);
352 this.list.setWidth(lw);
355 this.restrictHeight();
357 Roo.get(document).on('mousedown', this.collapseIf, this);
358 Roo.get(document).on('mousewheel', this.collapseIf, this);
360 this.fireEvent('expand', this);
363 restrictHeight : function()
365 this.list.alignTo(this.inputEl(), this.listAlign);
366 this.list.alignTo(this.inputEl(), this.listAlign);
369 onViewOver : function(e, t)
374 var item = this.view.findItemFromChild(t);
377 var index = this.view.indexOf(item);
378 this.select(index, false);
383 onViewClick : function(view, doFocus, el, e)
385 var index = this.view.getSelectedIndexes()[0];
387 var r = this.store.getAt(index);
390 this.onSelect(r, index);
392 if(doFocus !== false && !this.blockFocus){
393 this.inputEl().focus();
397 onViewMove : function(e, t)
399 this.inKeyMode = false;
402 select : function(index, scrollIntoView)
404 this.selectedIndex = index;
405 this.view.select(index);
406 if(scrollIntoView !== false){
407 var el = this.view.getNode(index);
409 this.list.scrollChildIntoView(el, false);
414 createList : function()
416 this.list = Roo.get(document.body).createChild({
418 cls: 'typeahead typeahead-long dropdown-menu tel-list',
419 style: 'display:none'
422 this.list.setVisibilityMode(Roo.Element.DISPLAY).originalDisplay = 'block';
425 collapseIf : function(e)
427 var in_combo = e.within(this.el);
428 var in_list = e.within(this.list);
429 var is_list = (Roo.get(e.getTarget()).id == this.list.id) ? true : false;
431 if (in_combo || in_list || is_list) {
437 onSelect : function(record, index)
439 if(this.fireEvent('beforeselect', this, record, index) !== false){
441 this.setFlagClass(record.data.iso2);
442 this.setDialCode(record.data.dialCode);
443 this.hasFocus = false;
445 this.fireEvent('select', this, record, index);
451 var flag = this.el.select('div.flag',true).first();
458 dialCodeHolderEl : function()
460 var d = this.el.select('input.dial-code-holder',true).first();
467 setDialCode : function(v)
469 this.dialCodeHolder.dom.value = '+'+v;
472 setFlagClass : function(n)
474 this.flag.dom.className = 'flag '+n;
477 getValue : function()
479 var v = this.inputEl().getValue();
480 if(this.dialCodeHolder) {
481 v = this.dialCodeHolder.dom.value+this.inputEl().getValue();
486 setValue : function(v)
488 var d = this.getDialCode(v);
491 if(v.length == 0 || !d || d.length == 0) {
493 this.inputEl().dom.value = (v === null || v === undefined ? '' : v);
494 this.hiddenEl().dom.value = (v === null || v === undefined ? '' : v);
500 this.setFlagClass(this.dialCodeMapping[d].iso2);
502 this.inputEl().dom.value = v.replace('+'+d,'');
503 this.hiddenEl().dom.value = this.getValue();
508 getDialCode : function(v)
513 return this.dialCodeHolder.dom.value;
517 if (v.charAt(0) != "+") {
520 var numericChars = "";
521 for (var i = 1; i < v.length; i++) {
525 if (this.dialCodeMapping[numericChars]) {
526 dialCode = v.substr(1, i);
528 if (numericChars.length == 4) {
538 this.setValue(this.defaultDialCode);
542 hiddenEl : function()
544 return this.el.select('input.hidden-tel-input',true).first();
547 onKeyUp : function(e){
550 var c = e.getCharCode();
553 (String.fromCharCode(c) == '.' || String.fromCharCode(c) == '-') &&
554 this.allowed.indexOf(String.fromCharCode(c)) === -1
559 // if(!Roo.isIE && (e.isSpecialKey() || k == e.BACKSPACE || k == e.DELETE)){
562 if(this.allowed.indexOf(String.fromCharCode(c)) === -1){
566 this.setValue(this.getValue());