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 cls : 'form-control tel-input',
85 autocomplete: 'new-password'
91 cls: 'hidden-tel-input'
95 hiddenInput.name = this.name;
99 input.disabled = true;
102 var flag_container = {
119 cls: this.hasFeedback ? 'has-feedback' : '',
125 cls: 'dial-code-holder',
132 cls: 'roo-select2-container input-group',
139 if (this.fieldLabel.length) {
142 tooltip: 'This field is required'
148 cls: 'control-label',
154 html: this.fieldLabel
157 indicator.cls = 'roo-required-indicator text-danger fa fa-lg fa-star left-indicator';
163 if(this.indicatorpos == 'right') {
164 indicator.cls = 'roo-required-indicator text-danger fa fa-lg fa-star right-indicator';
171 if(align == 'left') {
179 if(this.labelWidth > 12){
180 label.style = "width: " + this.labelWidth + 'px';
182 if(this.labelWidth < 13 && this.labelmd == 0){
183 this.labelmd = this.labelWidth;
185 if(this.labellg > 0){
186 label.cls += ' col-lg-' + this.labellg;
187 input.cls += ' col-lg-' + (12 - this.labellg);
189 if(this.labelmd > 0){
190 label.cls += ' col-md-' + this.labelmd;
191 container.cls += ' col-md-' + (12 - this.labelmd);
193 if(this.labelsm > 0){
194 label.cls += ' col-sm-' + this.labelsm;
195 container.cls += ' col-sm-' + (12 - this.labelsm);
197 if(this.labelxs > 0){
198 label.cls += ' col-xs-' + this.labelxs;
199 container.cls += ' col-xs-' + (12 - this.labelxs);
211 ['xs','sm','md','lg'].map(function(size){
212 if (settings[size]) {
213 cfg.cls += ' col-' + size + '-' + settings[size];
217 this.store = new Roo.data.Store({
218 proxy : new Roo.data.MemoryProxy({}),
219 reader : new Roo.data.JsonReader({
238 'name' : 'areaCodes',
245 if(!this.preferedCountries) {
246 this.preferedCountries = [
253 var p = this.preferedCountries.reverse();
256 for (var i = 0; i < p.length; i++) {
257 for (var j = 0; j < this.allCountries.length; j++) {
258 if(this.allCountries[j].iso2 == p[i]) {
259 var t = this.allCountries[j];
260 this.allCountries.splice(j,1);
261 this.allCountries.unshift(t);
267 this.store.proxy.data = {
269 data: this.allCountries
275 initEvents : function()
278 Roo.bootstrap.PhoneInput.superclass.initEvents.call(this);
280 this.indicator = this.indicatorEl();
281 this.flag = this.flagEl();
282 this.dialCodeHolder = this.dialCodeHolderEl();
284 this.trigger = this.el.select('div.flag-box',true).first();
285 this.trigger.on("click", this.onTriggerClick, this, {preventDefault:true});
290 var lw = _this.listWidth || Math.max(_this.inputEl().getWidth(), _this.minListWidth);
291 _this.list.setWidth(lw);
294 this.list.on('mouseover', this.onViewOver, this);
295 this.list.on('mousemove', this.onViewMove, this);
296 this.inputEl().on("keyup", this.onKeyUp, this);
298 this.tpl = '<li><a href="#"><div class="flag {iso2}"></div>{name} <span class="dial-code">+{dialCode}</span></a></li>';
300 this.view = new Roo.View(this.list, this.tpl, {
301 singleSelect:true, store: this.store, selectedClass: this.selectedClass
304 this.view.on('click', this.onViewClick, this);
305 this.setValue(this.defaultDialCode);
308 onTriggerClick : function(e)
310 Roo.log('trigger click');
315 if(this.isExpanded()){
317 this.hasFocus = false;
320 this.hasFocus = true;
325 isExpanded : function()
327 return this.list.isVisible();
330 collapse : function()
332 if(!this.isExpanded()){
336 Roo.get(document).un('mousedown', this.collapseIf, this);
337 Roo.get(document).un('mousewheel', this.collapseIf, this);
338 this.fireEvent('collapse', this);
346 if(this.isExpanded() || !this.hasFocus){
350 var lw = this.listWidth || Math.max(this.inputEl().getWidth(), this.minListWidth);
351 this.list.setWidth(lw);
354 this.restrictHeight();
356 Roo.get(document).on('mousedown', this.collapseIf, this);
357 Roo.get(document).on('mousewheel', this.collapseIf, this);
359 this.fireEvent('expand', this);
362 restrictHeight : function()
364 this.list.alignTo(this.inputEl(), this.listAlign);
365 this.list.alignTo(this.inputEl(), this.listAlign);
368 onViewOver : function(e, t)
373 var item = this.view.findItemFromChild(t);
376 var index = this.view.indexOf(item);
377 this.select(index, false);
382 onViewClick : function(view, doFocus, el, e)
384 var index = this.view.getSelectedIndexes()[0];
386 var r = this.store.getAt(index);
389 this.onSelect(r, index);
391 if(doFocus !== false && !this.blockFocus){
392 this.inputEl().focus();
396 onViewMove : function(e, t)
398 this.inKeyMode = false;
401 select : function(index, scrollIntoView)
403 this.selectedIndex = index;
404 this.view.select(index);
405 if(scrollIntoView !== false){
406 var el = this.view.getNode(index);
408 this.list.scrollChildIntoView(el, false);
413 createList : function()
415 this.list = Roo.get(document.body).createChild({
417 cls: 'typeahead typeahead-long dropdown-menu tel-list',
418 style: 'display:none'
421 this.list.setVisibilityMode(Roo.Element.DISPLAY).originalDisplay = 'block';
424 collapseIf : function(e)
426 var in_combo = e.within(this.el);
427 var in_list = e.within(this.list);
428 var is_list = (Roo.get(e.getTarget()).id == this.list.id) ? true : false;
430 if (in_combo || in_list || is_list) {
436 onSelect : function(record, index)
438 if(this.fireEvent('beforeselect', this, record, index) !== false){
440 this.setFlagClass(record.data.iso2);
441 this.setDialCode(record.data.dialCode);
442 this.hasFocus = false;
444 this.fireEvent('select', this, record, index);
450 var flag = this.el.select('div.flag',true).first();
457 dialCodeHolderEl : function()
459 var d = this.el.select('input.dial-code-holder',true).first();
466 setDialCode : function(v)
468 this.dialCodeHolder.dom.value = '+'+v;
471 setFlagClass : function(n)
473 this.flag.dom.className = 'flag '+n;
476 getValue : function()
478 var v = this.inputEl().getValue();
479 if(this.dialCodeHolder) {
480 v = this.dialCodeHolder.dom.value+this.inputEl().getValue();
485 setValue : function(v)
487 var d = this.getDialCode(v);
490 if(v.length == 0 || !d || d.length == 0) {
492 this.inputEl().dom.value = (v === null || v === undefined ? '' : v);
493 this.hiddenEl().dom.value = (v === null || v === undefined ? '' : v);
499 this.setFlagClass(this.dialCodeMapping[d].iso2);
501 this.inputEl().dom.value = v.replace('+'+d,'');
502 this.hiddenEl().dom.value = this.getValue();
507 getDialCode : function(v)
512 return this.dialCodeHolder.dom.value;
516 if (v.charAt(0) != "+") {
519 var numericChars = "";
520 for (var i = 1; i < v.length; i++) {
524 if (this.dialCodeMapping[numericChars]) {
525 dialCode = v.substr(1, i);
527 if (numericChars.length == 4) {
537 this.setValue(this.defaultDialCode);
541 hiddenEl : function()
543 return this.el.select('input.hidden-tel-input',true).first();
546 onKeyUp : function(e){
549 var c = e.getCharCode();
552 (String.fromCharCode(c) == '.' || String.fromCharCode(c) == '-') &&
553 this.allowed.indexOf(String.fromCharCode(c)) === -1
558 // if(!Roo.isIE && (e.isSpecialKey() || k == e.BACKSPACE || k == e.DELETE)){
561 if(this.allowed.indexOf(String.fromCharCode(c)) === -1){
565 this.setValue(this.getValue());