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
9 Roo.bootstrap.PhoneInput = function(config) {
10 Roo.bootstrap.PhoneInput.superclass.constructor.call(this, config);
13 Roo.extend(Roo.bootstrap.PhoneInput, Roo.bootstrap.TriggerField, {
19 selectedClass: 'active',
23 dialCodeMapping: false,
25 preferedCountries: false,
27 defaultDialCode: '+852',
29 getAutoCreate : function()
31 var data = Roo.bootstrap.PhoneInput.AllCountries;
32 var align = this.labelAlign || this.parentLabelAlign();
35 for (var i = 0; i < data.length; i++) {
37 this.allCountries[i] = {
42 areaCodes: c[4] || null
44 this.dialCodeMapping[c[2]] = {
48 areaCodes: c[4] || null
61 cls : 'form-control tel-input',
62 autocomplete: 'new-password'
66 input.name = this.name;
70 input.disabled = true;
73 var flag_container = {
90 cls: this.hasFeedback ? 'has-feedback' : '',
95 cls: 'dial-code-holder',
102 cls: 'roo-select2-container input-group',
109 if (this.fieldLabel.length) {
112 tooltip: 'This field is required'
118 cls: 'control-label',
124 html: this.fieldLabel
127 indicator.cls = 'roo-required-indicator text-danger fa fa-lg fa-star left-indicator';
133 if(this.indicatorpos == 'right') {
134 indicator.cls = 'roo-required-indicator text-danger fa fa-lg fa-star right-indicator';
141 if(align == 'left') {
149 if(this.labelWidth > 12){
150 label.style = "width: " + this.labelWidth + 'px';
152 if(this.labelWidth < 13 && this.labelmd == 0){
153 this.labelmd = this.labelWidth;
155 if(this.labellg > 0){
156 label.cls += ' col-lg-' + this.labellg;
157 input.cls += ' col-lg-' + (12 - this.labellg);
159 if(this.labelmd > 0){
160 label.cls += ' col-md-' + this.labelmd;
161 container.cls += ' col-md-' + (12 - this.labelmd);
163 if(this.labelsm > 0){
164 label.cls += ' col-sm-' + this.labelsm;
165 container.cls += ' col-sm-' + (12 - this.labelsm);
167 if(this.labelxs > 0){
168 label.cls += ' col-xs-' + this.labelxs;
169 container.cls += ' col-xs-' + (12 - this.labelxs);
181 ['xs','sm','md','lg'].map(function(size){
182 if (settings[size]) {
183 cfg.cls += ' col-' + size + '-' + settings[size];
187 this.store = new Roo.data.Store({
188 proxy : new Roo.data.MemoryProxy({}),
189 reader : new Roo.data.JsonReader({
208 'name' : 'areaCodes',
215 this.store.proxy.data = {
217 data: this.allCountries
223 initEvents : function()
226 Roo.bootstrap.PhoneInput.superclass.initEvents.call(this);
228 this.indicator = this.indicatorEl();
229 this.flag = this.flagEl();
230 this.dialCodeHolder = this.dialCodeHolderEl();
232 this.trigger = this.el.select('div.flag-box',true).first();
233 this.trigger.on("click", this.onTriggerClick, this, {preventDefault:true});
238 var lw = _this.listWidth || Math.max(_this.inputEl().getWidth(), _this.minListWidth);
239 _this.list.setWidth(lw);
242 this.list.on('mouseover', this.onViewOver, this);
243 this.list.on('mousemove', this.onViewMove, this);
244 //this.list.on('scroll', this.onViewScroll, this);
246 this.tpl = '<li><a href="#"><div class="flag {iso2}"></div>{name} <span class="dial-code">+{dialCode}</span></a></li>';
248 this.view = new Roo.View(this.list, this.tpl, {
249 singleSelect:true, store: this.store, selectedClass: this.selectedClass
252 this.view.on('click', this.onViewClick, this);
253 this.setValue(this.defaultDialCode);
256 onTriggerClick : function(e)
258 Roo.log('trigger click');
259 if(this.disabled || !this.triggerList){
263 if(this.isExpanded()){
265 this.hasFocus = false;
268 this.hasFocus = true;
273 isExpanded : function()
275 return this.list.isVisible();
278 collapse : function()
280 if(!this.isExpanded()){
284 Roo.get(document).un('mousedown', this.collapseIf, this);
285 Roo.get(document).un('mousewheel', this.collapseIf, this);
286 this.fireEvent('collapse', this);
294 if(this.isExpanded() || !this.hasFocus){
298 var lw = this.listWidth || Math.max(this.inputEl().getWidth(), this.minListWidth);
299 this.list.setWidth(lw);
302 this.restrictHeight();
304 Roo.get(document).on('mousedown', this.collapseIf, this);
305 Roo.get(document).on('mousewheel', this.collapseIf, this);
307 this.fireEvent('expand', this);
310 restrictHeight : function()
312 this.list.alignTo(this.inputEl(), this.listAlign);
313 this.list.alignTo(this.inputEl(), this.listAlign);
316 onViewOver : function(e, t)
321 var item = this.view.findItemFromChild(t);
324 var index = this.view.indexOf(item);
325 this.select(index, false);
330 onViewClick : function(view, doFocus, el, e)
332 var index = this.view.getSelectedIndexes()[0];
334 var r = this.store.getAt(index);
337 this.onSelect(r, index);
339 if(doFocus !== false && !this.blockFocus){
340 this.inputEl().focus();
344 onViewMove : function(e, t)
346 this.inKeyMode = false;
349 select : function(index, scrollIntoView)
351 this.selectedIndex = index;
352 this.view.select(index);
353 if(scrollIntoView !== false){
354 var el = this.view.getNode(index);
356 this.list.scrollChildIntoView(el, false);
361 createList : function()
363 this.list = Roo.get(document.body).createChild({
365 cls: 'typeahead typeahead-long dropdown-menu tel-list',
366 style: 'display:none'
368 this.list.setVisibilityMode(Roo.Element.DISPLAY).originalDisplay = 'block';;
371 collapseIf : function(e)
373 var in_combo = e.within(this.el);
374 var in_list = e.within(this.list);
375 var is_list = (Roo.get(e.getTarget()).id == this.list.id) ? true : false;
377 if (in_combo || in_list || is_list) {
383 onSelect : function(record, index)
385 if(this.fireEvent('beforeselect', this, record, index) !== false){
387 this.setFlagClass(record.data.iso2);
388 this.setDialCode(record.data.dialCode);
389 this.hasFocus = false;
391 this.fireEvent('select', this, record, index);
397 var flag = this.el.select('div.flag',true).first();
404 dialCodeHolderEl : function()
406 var d = this.el.select('input.dial-code-holder',true).first();
413 setDialCode : function(v)
415 this.dialCodeHolder.dom.value = '+'+v;
418 setFlagClass : function(n)
420 this.flag.dom.className = 'flag '+n;
423 getValue : function()
425 var v = this.inputEl().getValue();
426 if(this.dialCodeHolder) {
427 v = this.dialCodeHolder.dom.value+this.inputEl().getValue();
432 setValue : function(v)
434 var d = this.getDialCode(v);
437 if(!d || d.length == 0) {
439 this.inputEl().dom.value = (v === null || v === undefined ? '' : v);
444 this.setFlagClass(this.dialCodeMapping[d].iso2);
446 this.inputEl().dom.value = v.replace('+'+d,'');
449 getDialCode : function(v = '')
452 return this.dialCodeHolder.dom.value;
456 // only interested in international numbers (starting with a plus)
457 if (v.charAt(0) != "+") {
460 var numericChars = "";
461 for (var i = 1; i < v.length; i++) {
465 if (this.dialCodeMapping[numericChars]) {
466 dialCode = v.substr(1, i);
468 if (numericChars.length == 4) {
476 validate : function()