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 combines with 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, {
32 selectedClass: 'active',
34 invalidClass : "has-warning",
41 validClass: 'has-success',
43 defaultDialCode: '+852',
51 //white list / black list for countries?
53 getAutoCreate : function()
55 var data = Roo.bootstrap.PhoneInputData();
56 var align = this.labelAlign || this.parentLabelAlign();
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 cls : 'form-control tel-input',
85 autocomplete: 'new-password'
89 input.name = this.name;
93 input.disabled = true;
96 var flag_container = {
113 cls: this.hasFeedback ? 'has-feedback' : '',
118 cls: 'dial-code-holder',
125 cls: 'roo-select2-container input-group',
132 if (this.fieldLabel.length) {
135 tooltip: 'This field is required'
141 cls: 'control-label',
147 html: this.fieldLabel
150 indicator.cls = 'roo-required-indicator text-danger fa fa-lg fa-star left-indicator';
156 if(this.indicatorpos == 'right') {
157 indicator.cls = 'roo-required-indicator text-danger fa fa-lg fa-star right-indicator';
164 if(align == 'left') {
172 if(this.labelWidth > 12){
173 label.style = "width: " + this.labelWidth + 'px';
175 if(this.labelWidth < 13 && this.labelmd == 0){
176 this.labelmd = this.labelWidth;
178 if(this.labellg > 0){
179 label.cls += ' col-lg-' + this.labellg;
180 input.cls += ' col-lg-' + (12 - this.labellg);
182 if(this.labelmd > 0){
183 label.cls += ' col-md-' + this.labelmd;
184 container.cls += ' col-md-' + (12 - this.labelmd);
186 if(this.labelsm > 0){
187 label.cls += ' col-sm-' + this.labelsm;
188 container.cls += ' col-sm-' + (12 - this.labelsm);
190 if(this.labelxs > 0){
191 label.cls += ' col-xs-' + this.labelxs;
192 container.cls += ' col-xs-' + (12 - this.labelxs);
204 ['xs','sm','md','lg'].map(function(size){
205 if (settings[size]) {
206 cfg.cls += ' col-' + size + '-' + settings[size];
210 this.store = new Roo.data.Store({
211 proxy : new Roo.data.MemoryProxy({}),
212 reader : new Roo.data.JsonReader({
231 'name' : 'areaCodes',
238 var p = this.preferedCountries.reverse();
241 for (var i = 0; i < p.length; i++) {
242 for (var j = 0; j < this.allCountries.length; j++) {
243 if(this.allCountries[j].iso2 == p[i]) {
244 var t = this.allCountries[j];
245 this.allCountries.splice(j,1);
246 this.allCountries.unshift(t);
252 this.store.proxy.data = {
254 data: this.allCountries
260 initEvents : function()
263 Roo.bootstrap.PhoneInput.superclass.initEvents.call(this);
265 this.indicator = this.indicatorEl();
266 this.flag = this.flagEl();
267 this.dialCodeHolder = this.dialCodeHolderEl();
269 this.trigger = this.el.select('div.flag-box',true).first();
270 this.trigger.on("click", this.onTriggerClick, this, {preventDefault:true});
275 var lw = _this.listWidth || Math.max(_this.inputEl().getWidth(), _this.minListWidth);
276 _this.list.setWidth(lw);
279 this.list.on('mouseover', this.onViewOver, this);
280 this.list.on('mousemove', this.onViewMove, this);
281 //this.list.on('scroll', this.onViewScroll, this);
283 this.tpl = '<li><a href="#"><div class="flag {iso2}"></div>{name} <span class="dial-code">+{dialCode}</span></a></li>';
285 this.view = new Roo.View(this.list, this.tpl, {
286 singleSelect:true, store: this.store, selectedClass: this.selectedClass
289 this.view.on('click', this.onViewClick, this);
290 this.setValue(this.defaultDialCode);
293 onTriggerClick : function(e)
295 Roo.log('trigger click');
296 if(this.disabled || !this.triggerList){
300 if(this.isExpanded()){
302 this.hasFocus = false;
305 this.hasFocus = true;
310 isExpanded : function()
312 return this.list.isVisible();
315 collapse : function()
317 if(!this.isExpanded()){
321 Roo.get(document).un('mousedown', this.collapseIf, this);
322 Roo.get(document).un('mousewheel', this.collapseIf, this);
323 this.fireEvent('collapse', this);
331 if(this.isExpanded() || !this.hasFocus){
335 var lw = this.listWidth || Math.max(this.inputEl().getWidth(), this.minListWidth);
336 this.list.setWidth(lw);
339 this.restrictHeight();
341 Roo.get(document).on('mousedown', this.collapseIf, this);
342 Roo.get(document).on('mousewheel', this.collapseIf, this);
344 this.fireEvent('expand', this);
347 restrictHeight : function()
349 this.list.alignTo(this.inputEl(), this.listAlign);
350 this.list.alignTo(this.inputEl(), this.listAlign);
353 onViewOver : function(e, t)
358 var item = this.view.findItemFromChild(t);
361 var index = this.view.indexOf(item);
362 this.select(index, false);
367 onViewClick : function(view, doFocus, el, e)
369 var index = this.view.getSelectedIndexes()[0];
371 var r = this.store.getAt(index);
374 this.onSelect(r, index);
376 if(doFocus !== false && !this.blockFocus){
377 this.inputEl().focus();
381 onViewMove : function(e, t)
383 this.inKeyMode = false;
386 select : function(index, scrollIntoView)
388 this.selectedIndex = index;
389 this.view.select(index);
390 if(scrollIntoView !== false){
391 var el = this.view.getNode(index);
393 this.list.scrollChildIntoView(el, false);
398 createList : function()
400 this.list = Roo.get(document.body).createChild({
402 cls: 'typeahead typeahead-long dropdown-menu tel-list',
403 style: 'display:none'
405 this.list.setVisibilityMode(Roo.Element.DISPLAY).originalDisplay = 'block';;
408 collapseIf : function(e)
410 var in_combo = e.within(this.el);
411 var in_list = e.within(this.list);
412 var is_list = (Roo.get(e.getTarget()).id == this.list.id) ? true : false;
414 if (in_combo || in_list || is_list) {
420 onSelect : function(record, index)
422 if(this.fireEvent('beforeselect', this, record, index) !== false){
424 this.setFlagClass(record.data.iso2);
425 this.setDialCode(record.data.dialCode);
426 this.hasFocus = false;
428 this.fireEvent('select', this, record, index);
434 var flag = this.el.select('div.flag',true).first();
441 dialCodeHolderEl : function()
443 var d = this.el.select('input.dial-code-holder',true).first();
450 setDialCode : function(v)
452 this.dialCodeHolder.dom.value = '+'+v;
455 setFlagClass : function(n)
457 this.flag.dom.className = 'flag '+n;
460 getValue : function()
462 var v = this.inputEl().getValue();
463 if(this.dialCodeHolder) {
464 v = this.dialCodeHolder.dom.value+this.inputEl().getValue();
469 setValue : function(v)
471 var d = this.getDialCode(v);
475 if(!d || d.length == 0) {
477 this.inputEl().dom.value = (v === null || v === undefined ? '' : v);
482 this.setFlagClass(this.dialCodeMapping[d].iso2);
484 this.inputEl().dom.value = v.replace('+'+d,'');
489 getDialCode : function(v = '')
492 return this.dialCodeHolder.dom.value;
496 // only interested in international numbers (starting with a plus)
497 if (v.charAt(0) != "+") {
500 var numericChars = "";
501 for (var i = 1; i < v.length; i++) {
505 if (this.dialCodeMapping[numericChars]) {
506 dialCode = v.substr(1, i);
508 if (numericChars.length == 4) {
518 this.setValue(this.defaultDialCode);