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',
38 defaultDialCode: '+852',
40 preferedCountries: false,
42 //white list / black list for countries?
44 getAutoCreate : function()
46 var data = Roo.bootstrap.PhoneInputData();
47 var align = this.labelAlign || this.parentLabelAlign();
50 this.allCountries = [];
51 this.dialCodeMapping = [];
53 for (var i = 0; i < data.length; i++) {
55 this.allCountries[i] = {
60 areaCodes: c[4] || null
62 this.dialCodeMapping[c[2]] = {
66 areaCodes: c[4] || null
78 cls : 'form-control tel-input',
79 autocomplete: 'new-password'
85 cls: 'hidden-tel-input'
89 hiddenInput.name = this.name;
93 input.disabled = true;
96 var flag_container = {
113 cls: this.hasFeedback ? 'has-feedback' : '',
119 cls: 'dial-code-holder',
126 cls: 'roo-select2-container input-group',
133 if (this.fieldLabel.length) {
136 tooltip: 'This field is required'
142 cls: 'control-label',
148 html: this.fieldLabel
151 indicator.cls = 'roo-required-indicator text-danger fa fa-lg fa-star left-indicator';
157 if(this.indicatorpos == 'right') {
158 indicator.cls = 'roo-required-indicator text-danger fa fa-lg fa-star right-indicator';
165 if(align == 'left') {
173 if(this.labelWidth > 12){
174 label.style = "width: " + this.labelWidth + 'px';
176 if(this.labelWidth < 13 && this.labelmd == 0){
177 this.labelmd = this.labelWidth;
179 if(this.labellg > 0){
180 label.cls += ' col-lg-' + this.labellg;
181 input.cls += ' col-lg-' + (12 - this.labellg);
183 if(this.labelmd > 0){
184 label.cls += ' col-md-' + this.labelmd;
185 container.cls += ' col-md-' + (12 - this.labelmd);
187 if(this.labelsm > 0){
188 label.cls += ' col-sm-' + this.labelsm;
189 container.cls += ' col-sm-' + (12 - this.labelsm);
191 if(this.labelxs > 0){
192 label.cls += ' col-xs-' + this.labelxs;
193 container.cls += ' col-xs-' + (12 - this.labelxs);
205 ['xs','sm','md','lg'].map(function(size){
206 if (settings[size]) {
207 cfg.cls += ' col-' + size + '-' + settings[size];
211 this.store = new Roo.data.Store({
212 proxy : new Roo.data.MemoryProxy({}),
213 reader : new Roo.data.JsonReader({
232 'name' : 'areaCodes',
239 if(!this.preferedCountries) {
240 this.preferedCountries = [
247 var p = this.preferedCountries.reverse();
250 for (var i = 0; i < p.length; i++) {
251 for (var j = 0; j < this.allCountries.length; j++) {
252 if(this.allCountries[j].iso2 == p[i]) {
253 var t = this.allCountries[j];
254 this.allCountries.splice(j,1);
255 this.allCountries.unshift(t);
261 this.store.proxy.data = {
263 data: this.allCountries
269 initEvents : function()
272 Roo.bootstrap.PhoneInput.superclass.initEvents.call(this);
274 this.indicator = this.indicatorEl();
275 this.flag = this.flagEl();
276 this.dialCodeHolder = this.dialCodeHolderEl();
278 this.trigger = this.el.select('div.flag-box',true).first();
279 this.trigger.on("click", this.onTriggerClick, this, {preventDefault:true});
284 var lw = _this.listWidth || Math.max(_this.inputEl().getWidth(), _this.minListWidth);
285 _this.list.setWidth(lw);
288 this.list.on('mouseover', this.onViewOver, this);
289 this.list.on('mousemove', this.onViewMove, this);
290 this.inputEl().on("keypress", this.onKeyPress, this);
292 this.tpl = '<li><a href="#"><div class="flag {iso2}"></div>{name} <span class="dial-code">+{dialCode}</span></a></li>';
294 this.view = new Roo.View(this.list, this.tpl, {
295 singleSelect:true, store: this.store, selectedClass: this.selectedClass
298 this.view.on('click', this.onViewClick, this);
299 this.setValue(this.defaultDialCode);
302 onTriggerClick : function(e)
304 Roo.log('trigger click');
305 if(this.disabled || !this.triggerList){
309 if(this.isExpanded()){
311 this.hasFocus = false;
314 this.hasFocus = true;
319 isExpanded : function()
321 return this.list.isVisible();
324 collapse : function()
326 if(!this.isExpanded()){
330 Roo.get(document).un('mousedown', this.collapseIf, this);
331 Roo.get(document).un('mousewheel', this.collapseIf, this);
332 this.fireEvent('collapse', this);
340 if(this.isExpanded() || !this.hasFocus){
344 var lw = this.listWidth || Math.max(this.inputEl().getWidth(), this.minListWidth);
345 this.list.setWidth(lw);
348 this.restrictHeight();
350 Roo.get(document).on('mousedown', this.collapseIf, this);
351 Roo.get(document).on('mousewheel', this.collapseIf, this);
353 this.fireEvent('expand', this);
356 restrictHeight : function()
358 this.list.alignTo(this.inputEl(), this.listAlign);
359 this.list.alignTo(this.inputEl(), this.listAlign);
362 onViewOver : function(e, t)
367 var item = this.view.findItemFromChild(t);
370 var index = this.view.indexOf(item);
371 this.select(index, false);
376 onViewClick : function(view, doFocus, el, e)
378 var index = this.view.getSelectedIndexes()[0];
380 var r = this.store.getAt(index);
383 this.onSelect(r, index);
385 if(doFocus !== false && !this.blockFocus){
386 this.inputEl().focus();
390 onViewMove : function(e, t)
392 this.inKeyMode = false;
395 select : function(index, scrollIntoView)
397 this.selectedIndex = index;
398 this.view.select(index);
399 if(scrollIntoView !== false){
400 var el = this.view.getNode(index);
402 this.list.scrollChildIntoView(el, false);
407 createList : function()
409 this.list = Roo.get(document.body).createChild({
411 cls: 'typeahead typeahead-long dropdown-menu tel-list',
412 style: 'display:none'
414 this.list.setVisibilityMode(Roo.Element.DISPLAY).originalDisplay = 'block';;
417 collapseIf : function(e)
419 var in_combo = e.within(this.el);
420 var in_list = e.within(this.list);
421 var is_list = (Roo.get(e.getTarget()).id == this.list.id) ? true : false;
423 if (in_combo || in_list || is_list) {
429 onSelect : function(record, index)
431 if(this.fireEvent('beforeselect', this, record, index) !== false){
433 this.setFlagClass(record.data.iso2);
434 this.setDialCode(record.data.dialCode);
435 this.hasFocus = false;
437 this.fireEvent('select', this, record, index);
443 var flag = this.el.select('div.flag',true).first();
450 dialCodeHolderEl : function()
452 var d = this.el.select('input.dial-code-holder',true).first();
459 setDialCode : function(v)
461 this.dialCodeHolder.dom.value = '+'+v;
464 setFlagClass : function(n)
466 this.flag.dom.className = 'flag '+n;
469 getValue : function()
471 var v = this.inputEl().getValue();
472 if(this.dialCodeHolder) {
473 v = this.dialCodeHolder.dom.value+this.inputEl().getValue();
478 setValue : function(v)
480 var d = this.getDialCode(v);
483 if(v.length == 0 || !d || d.length == 0) {
485 this.inputEl().dom.value = (v === null || v === undefined ? '' : v);
486 this.hiddenEl().dom.value = (v === null || v === undefined ? '' : v);
492 this.setFlagClass(this.dialCodeMapping[d].iso2);
494 this.inputEl().dom.value = v.replace('+'+d,'');
495 this.hiddenEl().dom.value = this.getValue();
500 getDialCode : function(v = '')
503 return this.dialCodeHolder.dom.value;
507 if (v.charAt(0) != "+") {
510 var numericChars = "";
511 for (var i = 1; i < v.length; i++) {
515 if (this.dialCodeMapping[numericChars]) {
516 dialCode = v.substr(1, i);
518 if (numericChars.length == 4) {
528 this.setValue(this.defaultDialCode);
532 hiddenEl : function()
534 return this.el.select('input.hidden-tel-input',true).first();
537 onKeyPress : function(e){
540 var c = e.getCharCode();
543 (String.fromCharCode(c) == '.' || String.fromCharCode(c) == '-') &&
544 this.allowed.indexOf(String.fromCharCode(c)) === -1
550 if(!Roo.isIE && (e.isSpecialKey() || k == e.BACKSPACE || k == e.DELETE)){
553 if(this.allowed.indexOf(String.fromCharCode(c)) === -1){
557 this.setValue(this.getValue());