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("keyup", this.onKeyUp, this);
299 this.inputEl().on("keypress", this.onKeyPress, this);
301 this.tpl = '<li><a href="#"><div class="flag {iso2}"></div>{name} <span class="dial-code">+{dialCode}</span></a></li>';
303 this.view = new Roo.View(this.list, this.tpl, {
304 singleSelect:true, store: this.store, selectedClass: this.selectedClass
307 this.view.on('click', this.onViewClick, this);
308 this.setValue(this.defaultDialCode);
311 onTriggerClick : function(e)
313 Roo.log('trigger click');
318 if(this.isExpanded()){
320 this.hasFocus = false;
323 this.hasFocus = true;
328 isExpanded : function()
330 return this.list.isVisible();
333 collapse : function()
335 if(!this.isExpanded()){
339 Roo.get(document).un('mousedown', this.collapseIf, this);
340 Roo.get(document).un('mousewheel', this.collapseIf, this);
341 this.fireEvent('collapse', this);
349 if(this.isExpanded() || !this.hasFocus){
353 var lw = this.listWidth || Math.max(this.inputEl().getWidth(), this.minListWidth);
354 this.list.setWidth(lw);
357 this.restrictHeight();
359 Roo.get(document).on('mousedown', this.collapseIf, this);
360 Roo.get(document).on('mousewheel', this.collapseIf, this);
362 this.fireEvent('expand', this);
365 restrictHeight : function()
367 this.list.alignTo(this.inputEl(), this.listAlign);
368 this.list.alignTo(this.inputEl(), this.listAlign);
371 onViewOver : function(e, t)
376 var item = this.view.findItemFromChild(t);
379 var index = this.view.indexOf(item);
380 this.select(index, false);
385 onViewClick : function(view, doFocus, el, e)
387 var index = this.view.getSelectedIndexes()[0];
389 var r = this.store.getAt(index);
392 this.onSelect(r, index);
394 if(doFocus !== false && !this.blockFocus){
395 this.inputEl().focus();
399 onViewMove : function(e, t)
401 this.inKeyMode = false;
404 select : function(index, scrollIntoView)
406 this.selectedIndex = index;
407 this.view.select(index);
408 if(scrollIntoView !== false){
409 var el = this.view.getNode(index);
411 this.list.scrollChildIntoView(el, false);
416 createList : function()
418 this.list = Roo.get(document.body).createChild({
420 cls: 'typeahead typeahead-long dropdown-menu tel-list',
421 style: 'display:none'
424 this.list.setVisibilityMode(Roo.Element.DISPLAY).originalDisplay = 'block';
427 collapseIf : function(e)
429 var in_combo = e.within(this.el);
430 var in_list = e.within(this.list);
431 var is_list = (Roo.get(e.getTarget()).id == this.list.id) ? true : false;
433 if (in_combo || in_list || is_list) {
439 onSelect : function(record, index)
441 if(this.fireEvent('beforeselect', this, record, index) !== false){
443 this.setFlagClass(record.data.iso2);
444 this.setDialCode(record.data.dialCode);
445 this.hasFocus = false;
447 this.fireEvent('select', this, record, index);
453 var flag = this.el.select('div.flag',true).first();
460 dialCodeHolderEl : function()
462 var d = this.el.select('input.dial-code-holder',true).first();
469 setDialCode : function(v)
471 this.dialCodeHolder.dom.value = '+'+v;
474 setFlagClass : function(n)
476 this.flag.dom.className = 'flag '+n;
479 getValue : function()
481 var v = this.inputEl().getValue();
482 if(this.dialCodeHolder) {
483 v = this.dialCodeHolder.dom.value+this.inputEl().getValue();
488 setValue : function(v)
490 var d = this.getDialCode(v);
493 if(v.length == 0 || !d || d.length == 0) {
495 this.inputEl().dom.value = (v === null || v === undefined ? '' : v);
496 this.hiddenEl().dom.value = (v === null || v === undefined ? '' : v);
502 this.setFlagClass(this.dialCodeMapping[d].iso2);
504 this.inputEl().dom.value = v.replace('+'+d,'');
505 this.hiddenEl().dom.value = this.getValue();
510 getDialCode : function(v)
515 return this.dialCodeHolder.dom.value;
519 if (v.charAt(0) != "+") {
522 var numericChars = "";
523 for (var i = 1; i < v.length; i++) {
527 if (this.dialCodeMapping[numericChars]) {
528 dialCode = v.substr(1, i);
530 if (numericChars.length == 4) {
540 this.setValue(this.defaultDialCode);
544 hiddenEl : function()
546 return this.el.select('input.hidden-tel-input',true).first();
550 onKeyUp : function(e){
551 this.setValue(this.getValue());
554 onKeyDown : function(e){
555 if(this.allowed.indexOf(String.fromCharCode(e.getCharCode())) === -1){