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, {
28 * @cfg {Roo.data.Store} store [required] The data store to which this combo is bound (defaults to undefined)
32 selectedClass: 'active',
34 invalidClass : "has-warning",
36 validClass: 'has-success',
38 allowed: '0123456789',
43 * @cfg {String} defaultDialCode The default dial code when initializing the input
45 defaultDialCode: '+852',
48 * @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
50 preferedCountries: false,
52 getAutoCreate : function()
54 var data = Roo.bootstrap.PhoneInputData();
55 var align = this.labelAlign || this.parentLabelAlign();
58 this.allCountries = [];
59 this.dialCodeMapping = [];
61 for (var i = 0; i < data.length; i++) {
63 this.allCountries[i] = {
68 areaCodes: c[4] || null
70 this.dialCodeMapping[c[2]] = {
74 areaCodes: c[4] || null
86 // type: 'number', -- do not use number - we get the flaky up/down arrows.
87 maxlength: this.max_length,
88 cls : 'form-control tel-input',
89 autocomplete: 'new-password'
95 cls: 'hidden-tel-input'
99 hiddenInput.name = this.name;
103 input.disabled = true;
106 var flag_container = {
123 cls: this.hasFeedback ? 'has-feedback' : '',
129 cls: 'dial-code-holder',
136 cls: 'roo-select2-container input-group',
143 if (this.fieldLabel.length) {
146 tooltip: 'This field is required'
152 cls: 'control-label',
158 html: this.fieldLabel
161 indicator.cls = 'roo-required-indicator text-danger fa fa-lg fa-star left-indicator';
167 if(this.indicatorpos == 'right') {
168 indicator.cls = 'roo-required-indicator text-danger fa fa-lg fa-star right-indicator';
175 if(align == 'left') {
183 if(this.labelWidth > 12){
184 label.style = "width: " + this.labelWidth + 'px';
186 if(this.labelWidth < 13 && this.labelmd == 0){
187 this.labelmd = this.labelWidth;
189 if(this.labellg > 0){
190 label.cls += ' col-lg-' + this.labellg;
191 input.cls += ' col-lg-' + (12 - this.labellg);
193 if(this.labelmd > 0){
194 label.cls += ' col-md-' + this.labelmd;
195 container.cls += ' col-md-' + (12 - this.labelmd);
197 if(this.labelsm > 0){
198 label.cls += ' col-sm-' + this.labelsm;
199 container.cls += ' col-sm-' + (12 - this.labelsm);
201 if(this.labelxs > 0){
202 label.cls += ' col-xs-' + this.labelxs;
203 container.cls += ' col-xs-' + (12 - this.labelxs);
215 ['xs','sm','md','lg'].map(function(size){
216 if (settings[size]) {
217 cfg.cls += ' col-' + size + '-' + settings[size];
221 this.store = new Roo.data.Store({
222 proxy : new Roo.data.MemoryProxy({}),
223 reader : new Roo.data.JsonReader({
242 'name' : 'areaCodes',
249 if(!this.preferedCountries) {
250 this.preferedCountries = [
257 var p = this.preferedCountries.reverse();
260 for (var i = 0; i < p.length; i++) {
261 for (var j = 0; j < this.allCountries.length; j++) {
262 if(this.allCountries[j].iso2 == p[i]) {
263 var t = this.allCountries[j];
264 this.allCountries.splice(j,1);
265 this.allCountries.unshift(t);
271 this.store.proxy.data = {
273 data: this.allCountries
279 initEvents : function()
282 Roo.bootstrap.PhoneInput.superclass.initEvents.call(this);
284 this.indicator = this.indicatorEl();
285 this.flag = this.flagEl();
286 this.dialCodeHolder = this.dialCodeHolderEl();
288 this.trigger = this.el.select('div.flag-box',true).first();
289 this.trigger.on("click", this.onTriggerClick, this, {preventDefault:true});
294 var lw = _this.listWidth || Math.max(_this.inputEl().getWidth(), _this.minListWidth);
295 _this.list.setWidth(lw);
298 this.list.on('mouseover', this.onViewOver, this);
299 this.list.on('mousemove', this.onViewMove, this);
300 this.inputEl().on("keyup", this.onKeyUp, this);
301 this.inputEl().on("keypress", this.onKeyPress, this);
303 this.tpl = '<li><a href="#"><div class="flag {iso2}"></div>{name} <span class="dial-code">+{dialCode}</span></a></li>';
305 this.view = new Roo.View(this.list, this.tpl, {
306 singleSelect:true, store: this.store, selectedClass: this.selectedClass
309 this.view.on('click', this.onViewClick, this);
310 this.setValue(this.defaultDialCode);
313 onTriggerClick : function(e)
315 Roo.log('trigger click');
320 if(this.isExpanded()){
322 this.hasFocus = false;
325 this.hasFocus = true;
330 isExpanded : function()
332 return this.list.isVisible();
335 collapse : function()
337 if(!this.isExpanded()){
341 Roo.get(document).un('mousedown', this.collapseIf, this);
342 Roo.get(document).un('mousewheel', this.collapseIf, this);
343 this.fireEvent('collapse', this);
351 if(this.isExpanded() || !this.hasFocus){
355 var lw = this.listWidth || Math.max(this.inputEl().getWidth(), this.minListWidth);
356 this.list.setWidth(lw);
359 this.restrictHeight();
361 Roo.get(document).on('mousedown', this.collapseIf, this);
362 Roo.get(document).on('mousewheel', this.collapseIf, this);
364 this.fireEvent('expand', this);
367 restrictHeight : function()
369 this.list.alignTo(this.inputEl(), this.listAlign);
370 this.list.alignTo(this.inputEl(), this.listAlign);
373 onViewOver : function(e, t)
378 var item = this.view.findItemFromChild(t);
381 var index = this.view.indexOf(item);
382 this.select(index, false);
387 onViewClick : function(view, doFocus, el, e)
389 var index = this.view.getSelectedIndexes()[0];
391 var r = this.store.getAt(index);
394 this.onSelect(r, index);
396 if(doFocus !== false && !this.blockFocus){
397 this.inputEl().focus();
401 onViewMove : function(e, t)
403 this.inKeyMode = false;
406 select : function(index, scrollIntoView)
408 this.selectedIndex = index;
409 this.view.select(index);
410 if(scrollIntoView !== false){
411 var el = this.view.getNode(index);
413 this.list.scrollChildIntoView(el, false);
418 createList : function()
420 this.list = Roo.get(document.body).createChild({
422 cls: 'typeahead typeahead-long dropdown-menu tel-list',
423 style: 'display:none'
426 this.list.setVisibilityMode(Roo.Element.DISPLAY).originalDisplay = 'block';
429 collapseIf : function(e)
431 var in_combo = e.within(this.el);
432 var in_list = e.within(this.list);
433 var is_list = (Roo.get(e.getTarget()).id == this.list.id) ? true : false;
435 if (in_combo || in_list || is_list) {
441 onSelect : function(record, index)
443 if(this.fireEvent('beforeselect', this, record, index) !== false){
445 this.setFlagClass(record.data.iso2);
446 this.setDialCode(record.data.dialCode);
447 this.hasFocus = false;
449 this.fireEvent('select', this, record, index);
455 var flag = this.el.select('div.flag',true).first();
462 dialCodeHolderEl : function()
464 var d = this.el.select('input.dial-code-holder',true).first();
471 setDialCode : function(v)
473 this.dialCodeHolder.dom.value = '+'+v;
476 setFlagClass : function(n)
478 this.flag.dom.className = 'flag '+n;
481 getValue : function()
483 var v = this.inputEl().getValue();
484 if(this.dialCodeHolder) {
485 v = this.dialCodeHolder.dom.value+this.inputEl().getValue();
490 setValue : function(v)
492 var d = this.getDialCode(v);
495 if(v.length == 0 || !d || d.length == 0) {
497 this.inputEl().dom.value = (v === null || v === undefined ? '' : v);
498 this.hiddenEl().dom.value = (v === null || v === undefined ? '' : v);
504 this.setFlagClass(this.dialCodeMapping[d].iso2);
506 this.inputEl().dom.value = v.replace('+'+d,'');
507 this.hiddenEl().dom.value = this.getValue();
512 getDialCode : function(v)
517 return this.dialCodeHolder.dom.value;
521 if (v.charAt(0) != "+") {
524 var numericChars = "";
525 for (var i = 1; i < v.length; i++) {
529 if (this.dialCodeMapping[numericChars]) {
530 dialCode = v.substr(1, i);
532 if (numericChars.length == 4) {
542 this.setValue(this.defaultDialCode);
546 hiddenEl : function()
548 return this.el.select('input.hidden-tel-input',true).first();
552 onKeyUp : function(e){
553 this.setValue(this.getValue());
556 onKeyPress : function(e){
557 if(this.allowed.indexOf(String.fromCharCode(e.getCharCode())) === -1){