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',
39 * @cfg {String} defaultDialCode The default dial code when initializing the input
41 defaultDialCode: '+852',
44 * @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
46 preferedCountries: false,
48 getAutoCreate : function()
50 var data = Roo.bootstrap.PhoneInputData();
51 var align = this.labelAlign || this.parentLabelAlign();
54 this.allCountries = [];
55 this.dialCodeMapping = [];
57 for (var i = 0; i < data.length; i++) {
59 this.allCountries[i] = {
64 areaCodes: c[4] || null
66 this.dialCodeMapping[c[2]] = {
70 areaCodes: c[4] || null
82 cls : 'form-control tel-input',
83 autocomplete: 'new-password'
89 cls: 'hidden-tel-input'
93 hiddenInput.name = this.name;
97 input.disabled = true;
100 var flag_container = {
117 cls: this.hasFeedback ? 'has-feedback' : '',
123 cls: 'dial-code-holder',
130 cls: 'roo-select2-container input-group',
137 if (this.fieldLabel.length) {
140 tooltip: 'This field is required'
146 cls: 'control-label',
152 html: this.fieldLabel
155 indicator.cls = 'roo-required-indicator text-danger fa fa-lg fa-star left-indicator';
161 if(this.indicatorpos == 'right') {
162 indicator.cls = 'roo-required-indicator text-danger fa fa-lg fa-star right-indicator';
169 if(align == 'left') {
177 if(this.labelWidth > 12){
178 label.style = "width: " + this.labelWidth + 'px';
180 if(this.labelWidth < 13 && this.labelmd == 0){
181 this.labelmd = this.labelWidth;
183 if(this.labellg > 0){
184 label.cls += ' col-lg-' + this.labellg;
185 input.cls += ' col-lg-' + (12 - this.labellg);
187 if(this.labelmd > 0){
188 label.cls += ' col-md-' + this.labelmd;
189 container.cls += ' col-md-' + (12 - this.labelmd);
191 if(this.labelsm > 0){
192 label.cls += ' col-sm-' + this.labelsm;
193 container.cls += ' col-sm-' + (12 - this.labelsm);
195 if(this.labelxs > 0){
196 label.cls += ' col-xs-' + this.labelxs;
197 container.cls += ' col-xs-' + (12 - this.labelxs);
209 ['xs','sm','md','lg'].map(function(size){
210 if (settings[size]) {
211 cfg.cls += ' col-' + size + '-' + settings[size];
215 this.store = new Roo.data.Store({
216 proxy : new Roo.data.MemoryProxy({}),
217 reader : new Roo.data.JsonReader({
236 'name' : 'areaCodes',
243 if(!this.preferedCountries) {
244 this.preferedCountries = [
251 var p = this.preferedCountries.reverse();
254 for (var i = 0; i < p.length; i++) {
255 for (var j = 0; j < this.allCountries.length; j++) {
256 if(this.allCountries[j].iso2 == p[i]) {
257 var t = this.allCountries[j];
258 this.allCountries.splice(j,1);
259 this.allCountries.unshift(t);
265 this.store.proxy.data = {
267 data: this.allCountries
273 initEvents : function()
276 Roo.bootstrap.PhoneInput.superclass.initEvents.call(this);
278 this.indicator = this.indicatorEl();
279 this.flag = this.flagEl();
280 this.dialCodeHolder = this.dialCodeHolderEl();
282 this.trigger = this.el.select('div.flag-box',true).first();
283 this.trigger.on("click", this.onTriggerClick, this, {preventDefault:true});
288 var lw = _this.listWidth || Math.max(_this.inputEl().getWidth(), _this.minListWidth);
289 _this.list.setWidth(lw);
292 this.list.on('mouseover', this.onViewOver, this);
293 this.list.on('mousemove', this.onViewMove, this);
294 this.inputEl().on("keyup", this.onKeyUp, this);
296 this.tpl = '<li><a href="#"><div class="flag {iso2}"></div>{name} <span class="dial-code">+{dialCode}</span></a></li>';
298 this.view = new Roo.View(this.list, this.tpl, {
299 singleSelect:true, store: this.store, selectedClass: this.selectedClass
302 this.view.on('click', this.onViewClick, this);
303 this.setValue(this.defaultDialCode);
306 onTriggerClick : function(e)
308 Roo.log('trigger click');
313 if(this.isExpanded()){
315 this.hasFocus = false;
318 this.hasFocus = true;
323 isExpanded : function()
325 return this.list.isVisible();
328 collapse : function()
330 if(!this.isExpanded()){
334 Roo.get(document).un('mousedown', this.collapseIf, this);
335 Roo.get(document).un('mousewheel', this.collapseIf, this);
336 this.fireEvent('collapse', this);
344 if(this.isExpanded() || !this.hasFocus){
348 var lw = this.listWidth || Math.max(this.inputEl().getWidth(), this.minListWidth);
349 this.list.setWidth(lw);
352 this.restrictHeight();
354 Roo.get(document).on('mousedown', this.collapseIf, this);
355 Roo.get(document).on('mousewheel', this.collapseIf, this);
357 this.fireEvent('expand', this);
360 restrictHeight : function()
362 this.list.alignTo(this.inputEl(), this.listAlign);
363 this.list.alignTo(this.inputEl(), this.listAlign);
366 onViewOver : function(e, t)
371 var item = this.view.findItemFromChild(t);
374 var index = this.view.indexOf(item);
375 this.select(index, false);
380 onViewClick : function(view, doFocus, el, e)
382 var index = this.view.getSelectedIndexes()[0];
384 var r = this.store.getAt(index);
387 this.onSelect(r, index);
389 if(doFocus !== false && !this.blockFocus){
390 this.inputEl().focus();
394 onViewMove : function(e, t)
396 this.inKeyMode = false;
399 select : function(index, scrollIntoView)
401 this.selectedIndex = index;
402 this.view.select(index);
403 if(scrollIntoView !== false){
404 var el = this.view.getNode(index);
406 this.list.scrollChildIntoView(el, false);
411 createList : function()
413 this.list = Roo.get(document.body).createChild({
415 cls: 'typeahead typeahead-long dropdown-menu tel-list',
416 style: 'display:none'
419 this.list.setVisibilityMode(Roo.Element.DISPLAY).originalDisplay = 'block';
422 collapseIf : function(e)
424 var in_combo = e.within(this.el);
425 var in_list = e.within(this.list);
426 var is_list = (Roo.get(e.getTarget()).id == this.list.id) ? true : false;
428 if (in_combo || in_list || is_list) {
434 onSelect : function(record, index)
436 if(this.fireEvent('beforeselect', this, record, index) !== false){
438 this.setFlagClass(record.data.iso2);
439 this.setDialCode(record.data.dialCode);
440 this.hasFocus = false;
442 this.fireEvent('select', this, record, index);
448 var flag = this.el.select('div.flag',true).first();
455 dialCodeHolderEl : function()
457 var d = this.el.select('input.dial-code-holder',true).first();
464 setDialCode : function(v)
466 this.dialCodeHolder.dom.value = '+'+v;
469 setFlagClass : function(n)
471 this.flag.dom.className = 'flag '+n;
474 getValue : function()
476 var v = this.inputEl().getValue();
477 if(this.dialCodeHolder) {
478 v = this.dialCodeHolder.dom.value+this.inputEl().getValue();
483 setValue : function(v)
485 var d = this.getDialCode(v);
488 if(v.length == 0 || !d || d.length == 0) {
490 this.inputEl().dom.value = (v === null || v === undefined ? '' : v);
491 this.hiddenEl().dom.value = (v === null || v === undefined ? '' : v);
497 this.setFlagClass(this.dialCodeMapping[d].iso2);
499 this.inputEl().dom.value = v.replace('+'+d,'');
500 this.hiddenEl().dom.value = this.getValue();
505 getDialCode : function(v)
510 return this.dialCodeHolder.dom.value;
514 if (v.charAt(0) != "+") {
517 var numericChars = "";
518 for (var i = 1; i < v.length; i++) {
522 if (this.dialCodeMapping[numericChars]) {
523 dialCode = v.substr(1, i);
525 if (numericChars.length == 4) {
535 this.setValue(this.defaultDialCode);
539 hiddenEl : function()
541 return this.el.select('input.hidden-tel-input',true).first();
544 onKeyUp : function(e){
547 var c = e.getCharCode();
550 (String.fromCharCode(c) == '.' || String.fromCharCode(c) == '-') &&
551 this.allowed.indexOf(String.fromCharCode(c)) === -1
556 // if(!Roo.isIE && (e.isSpecialKey() || k == e.BACKSPACE || k == e.DELETE)){
559 if(this.allowed.indexOf(String.fromCharCode(c)) === -1){
563 this.setValue(this.getValue());