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
85 maxlength: this.max_length,
86 cls : 'form-control tel-input',
87 autocomplete: 'new-password'
93 cls: 'hidden-tel-input'
97 hiddenInput.name = this.name;
101 input.disabled = true;
104 var flag_container = {
121 cls: this.hasFeedback ? 'has-feedback' : '',
127 cls: 'dial-code-holder',
134 cls: 'roo-select2-container input-group',
141 if (this.fieldLabel.length) {
144 tooltip: 'This field is required'
150 cls: 'control-label',
156 html: this.fieldLabel
159 indicator.cls = 'roo-required-indicator text-danger fa fa-lg fa-star left-indicator';
165 if(this.indicatorpos == 'right') {
166 indicator.cls = 'roo-required-indicator text-danger fa fa-lg fa-star right-indicator';
173 if(align == 'left') {
181 if(this.labelWidth > 12){
182 label.style = "width: " + this.labelWidth + 'px';
184 if(this.labelWidth < 13 && this.labelmd == 0){
185 this.labelmd = this.labelWidth;
187 if(this.labellg > 0){
188 label.cls += ' col-lg-' + this.labellg;
189 input.cls += ' col-lg-' + (12 - this.labellg);
191 if(this.labelmd > 0){
192 label.cls += ' col-md-' + this.labelmd;
193 container.cls += ' col-md-' + (12 - this.labelmd);
195 if(this.labelsm > 0){
196 label.cls += ' col-sm-' + this.labelsm;
197 container.cls += ' col-sm-' + (12 - this.labelsm);
199 if(this.labelxs > 0){
200 label.cls += ' col-xs-' + this.labelxs;
201 container.cls += ' col-xs-' + (12 - this.labelxs);
213 ['xs','sm','md','lg'].map(function(size){
214 if (settings[size]) {
215 cfg.cls += ' col-' + size + '-' + settings[size];
219 this.store = new Roo.data.Store({
220 proxy : new Roo.data.MemoryProxy({}),
221 reader : new Roo.data.JsonReader({
240 'name' : 'areaCodes',
247 if(!this.preferedCountries) {
248 this.preferedCountries = [
255 var p = this.preferedCountries.reverse();
258 for (var i = 0; i < p.length; i++) {
259 for (var j = 0; j < this.allCountries.length; j++) {
260 if(this.allCountries[j].iso2 == p[i]) {
261 var t = this.allCountries[j];
262 this.allCountries.splice(j,1);
263 this.allCountries.unshift(t);
269 this.store.proxy.data = {
271 data: this.allCountries
277 initEvents : function()
280 Roo.bootstrap.PhoneInput.superclass.initEvents.call(this);
282 this.indicator = this.indicatorEl();
283 this.flag = this.flagEl();
284 this.dialCodeHolder = this.dialCodeHolderEl();
286 this.trigger = this.el.select('div.flag-box',true).first();
287 this.trigger.on("click", this.onTriggerClick, this, {preventDefault:true});
292 var lw = _this.listWidth || Math.max(_this.inputEl().getWidth(), _this.minListWidth);
293 _this.list.setWidth(lw);
296 this.list.on('mouseover', this.onViewOver, this);
297 this.list.on('mousemove', this.onViewMove, this);
298 this.inputEl().on("keypress", this.onKeyPress, this);
300 this.tpl = '<li><a href="#"><div class="flag {iso2}"></div>{name} <span class="dial-code">+{dialCode}</span></a></li>';
302 this.view = new Roo.View(this.list, this.tpl, {
303 singleSelect:true, store: this.store, selectedClass: this.selectedClass
306 this.view.on('click', this.onViewClick, this);
307 this.setValue(this.defaultDialCode);
310 onTriggerClick : function(e)
312 Roo.log('trigger click');
317 if(this.isExpanded()){
319 this.hasFocus = false;
322 this.hasFocus = true;
327 isExpanded : function()
329 return this.list.isVisible();
332 collapse : function()
334 if(!this.isExpanded()){
338 Roo.get(document).un('mousedown', this.collapseIf, this);
339 Roo.get(document).un('mousewheel', this.collapseIf, this);
340 this.fireEvent('collapse', this);
348 if(this.isExpanded() || !this.hasFocus){
352 var lw = this.listWidth || Math.max(this.inputEl().getWidth(), this.minListWidth);
353 this.list.setWidth(lw);
356 this.restrictHeight();
358 Roo.get(document).on('mousedown', this.collapseIf, this);
359 Roo.get(document).on('mousewheel', this.collapseIf, this);
361 this.fireEvent('expand', this);
364 restrictHeight : function()
366 this.list.alignTo(this.inputEl(), this.listAlign);
367 this.list.alignTo(this.inputEl(), this.listAlign);
370 onViewOver : function(e, t)
375 var item = this.view.findItemFromChild(t);
378 var index = this.view.indexOf(item);
379 this.select(index, false);
384 onViewClick : function(view, doFocus, el, e)
386 var index = this.view.getSelectedIndexes()[0];
388 var r = this.store.getAt(index);
391 this.onSelect(r, index);
393 if(doFocus !== false && !this.blockFocus){
394 this.inputEl().focus();
398 onViewMove : function(e, t)
400 this.inKeyMode = false;
403 select : function(index, scrollIntoView)
405 this.selectedIndex = index;
406 this.view.select(index);
407 if(scrollIntoView !== false){
408 var el = this.view.getNode(index);
410 this.list.scrollChildIntoView(el, false);
415 createList : function()
417 this.list = Roo.get(document.body).createChild({
419 cls: 'typeahead typeahead-long dropdown-menu tel-list',
420 style: 'display:none'
423 this.list.setVisibilityMode(Roo.Element.DISPLAY).originalDisplay = 'block';
426 collapseIf : function(e)
428 var in_combo = e.within(this.el);
429 var in_list = e.within(this.list);
430 var is_list = (Roo.get(e.getTarget()).id == this.list.id) ? true : false;
432 if (in_combo || in_list || is_list) {
438 onSelect : function(record, index)
440 if(this.fireEvent('beforeselect', this, record, index) !== false){
442 this.setFlagClass(record.data.iso2);
443 this.setDialCode(record.data.dialCode);
444 this.hasFocus = false;
446 this.fireEvent('select', this, record, index);
452 var flag = this.el.select('div.flag',true).first();
459 dialCodeHolderEl : function()
461 var d = this.el.select('input.dial-code-holder',true).first();
468 setDialCode : function(v)
470 this.dialCodeHolder.dom.value = '+'+v;
473 setFlagClass : function(n)
475 this.flag.dom.className = 'flag '+n;
478 getValue : function()
480 var v = this.inputEl().getValue();
481 if(this.dialCodeHolder) {
482 v = this.dialCodeHolder.dom.value+this.inputEl().getValue();
487 setValue : function(v)
489 var d = this.getDialCode(v);
492 if(v.length == 0 || !d || d.length == 0) {
494 this.inputEl().dom.value = (v === null || v === undefined ? '' : v);
495 this.hiddenEl().dom.value = (v === null || v === undefined ? '' : v);
501 this.setFlagClass(this.dialCodeMapping[d].iso2);
503 this.inputEl().dom.value = v.replace('+'+d,'');
504 this.hiddenEl().dom.value = this.getValue();
509 getDialCode : function(v)
514 return this.dialCodeHolder.dom.value;
518 if (v.charAt(0) != "+") {
521 var numericChars = "";
522 for (var i = 1; i < v.length; i++) {
526 if (this.dialCodeMapping[numericChars]) {
527 dialCode = v.substr(1, i);
529 if (numericChars.length == 4) {
539 this.setValue(this.defaultDialCode);
543 hiddenEl : function()
545 return this.el.select('input.hidden-tel-input',true).first();
548 onKeyPress : function(e){
550 if(this.allowed.indexOf(String.fromCharCode(e.getCharCode())) === -1){
554 Roo.log(this.getValue());
556 this.setValue(this.getValue());
558 Roo.log(this.getValue());