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",
42 validClass: 'has-success',
44 defaultDialCode: '+852',
52 //white list / black list for countries?
54 getAutoCreate : function()
56 var data = Roo.bootstrap.PhoneInputData();
57 var align = this.labelAlign || this.parentLabelAlign();
60 for (var i = 0; i < data.length; i++) {
62 this.allCountries[i] = {
67 areaCodes: c[4] || null
69 this.dialCodeMapping[c[2]] = {
73 areaCodes: c[4] || null
85 cls : 'form-control tel-input',
86 autocomplete: 'new-password'
92 cls: 'hidden-tel-input'
96 hiddenInput.name = this.name;
100 input.disabled = true;
103 var flag_container = {
120 cls: this.hasFeedback ? 'has-feedback' : '',
126 cls: 'dial-code-holder',
133 cls: 'roo-select2-container input-group',
140 if (this.fieldLabel.length) {
143 tooltip: 'This field is required'
149 cls: 'control-label',
155 html: this.fieldLabel
158 indicator.cls = 'roo-required-indicator text-danger fa fa-lg fa-star left-indicator';
164 if(this.indicatorpos == 'right') {
165 indicator.cls = 'roo-required-indicator text-danger fa fa-lg fa-star right-indicator';
172 if(align == 'left') {
180 if(this.labelWidth > 12){
181 label.style = "width: " + this.labelWidth + 'px';
183 if(this.labelWidth < 13 && this.labelmd == 0){
184 this.labelmd = this.labelWidth;
186 if(this.labellg > 0){
187 label.cls += ' col-lg-' + this.labellg;
188 input.cls += ' col-lg-' + (12 - this.labellg);
190 if(this.labelmd > 0){
191 label.cls += ' col-md-' + this.labelmd;
192 container.cls += ' col-md-' + (12 - this.labelmd);
194 if(this.labelsm > 0){
195 label.cls += ' col-sm-' + this.labelsm;
196 container.cls += ' col-sm-' + (12 - this.labelsm);
198 if(this.labelxs > 0){
199 label.cls += ' col-xs-' + this.labelxs;
200 container.cls += ' col-xs-' + (12 - this.labelxs);
212 ['xs','sm','md','lg'].map(function(size){
213 if (settings[size]) {
214 cfg.cls += ' col-' + size + '-' + settings[size];
218 this.store = new Roo.data.Store({
219 proxy : new Roo.data.MemoryProxy({}),
220 reader : new Roo.data.JsonReader({
239 'name' : 'areaCodes',
246 var p = this.preferedCountries.reverse();
249 for (var i = 0; i < p.length; i++) {
250 for (var j = 0; j < this.allCountries.length; j++) {
251 if(this.allCountries[j].iso2 == p[i]) {
252 var t = this.allCountries[j];
253 this.allCountries.splice(j,1);
254 this.allCountries.unshift(t);
260 this.store.proxy.data = {
262 data: this.allCountries
268 initEvents : function()
271 Roo.bootstrap.PhoneInput.superclass.initEvents.call(this);
273 this.indicator = this.indicatorEl();
274 this.flag = this.flagEl();
275 this.dialCodeHolder = this.dialCodeHolderEl();
277 this.trigger = this.el.select('div.flag-box',true).first();
278 this.trigger.on("click", this.onTriggerClick, this, {preventDefault:true});
283 var lw = _this.listWidth || Math.max(_this.inputEl().getWidth(), _this.minListWidth);
284 _this.list.setWidth(lw);
287 this.list.on('mouseover', this.onViewOver, this);
288 this.list.on('mousemove', this.onViewMove, this);
289 //this.list.on('scroll', this.onViewScroll, this);
290 this.inputEl().on("keyup", this.onKeyUp, 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(!d || d.length == 0 || v.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);
533 hiddenEl : function()
535 return this.el.select('input.hidden-tel-input',true).first();
538 onKeyUp : function(e)
540 this.setValue(this.getValue());