/** * @required $ jQuery */ (function(ns) { "use strict"; /** * @alias lib.form.element.InternationalTel * @constructor */ ns.InternationalTel = function () { this.construct.apply(this, arguments); }; var fn = ns.InternationalTel.prototype; fn.construct = function (options) { this.ns = 'feit'; this.validationEnabled = options.validationEnabled || false; this.messages = { wrongFormat: 'Phone Number is in wrong format', tooShort: 'Phone Number is too short', tooLong: 'Phone Number is too long', invalidCountryCode: 'Invalid Country Code' }; $.extend(this.messages, options.messages || {}); this.hiddenPreviousValue = undefined; this.hidden = $('#' + options.id); this.hidden.on('change.' + this.ns, _.bind(this.hiddenOnChange, this)); this.tel = $('#' + options.telId); this.tel.intlTelInput(options.tel || {}); this.tel.on('blur.' + this.ns, _.bind(this.onBlur, this)); this.tel.on('change.' + this.ns, _.bind(this.telOnChange, this)); this.initTimer = null; this.initTimerDelay = options.initTimerDelay || 500; this.tel.on('keyup.' + this.ns, _.bind(this.onKeyUp, this)); }; fn.telOnChange = function (event) { this.fixEmptyExt(); this.initHiddenValue(); }; fn.fixEmptyExt = function (event) { var value = this.tel.val(); if (value.substr(-6) == ' ext. ') { value = value.substr(0, value.length - 6); this.tel.val(value); } }; fn.hiddenOnChange = function (event) { if (this.hiddenPreviousValue == this.hidden.val()) { return; } this.hiddenOnValueChange(); this.hiddenPreviousValue = this.hidden.val(); }; fn.hiddenOnValueChange = function () { if (this.validationEnabled) { this.validate(); } }; fn.onBlur = function (event) { this.initHiddenValue(); }; fn.hideTip = function () { this.tel.popover('destroy'); }; fn.onKeyUp = function (event) { if (this.initTimer) { clearTimeout(this.initTimer); } this.initTimer = setTimeout(_.bind(this.initHiddenValue, this), this.initTimerDelay); }; fn.validate = function () { if (this.tel.intlTelInput('isValidNumber')) { this.hideTip(); return true; } var message; switch (this.tel.intlTelInput('getValidationError')) { case intlTelInputUtils.validationError.TOO_SHORT: message = this.messages.tooShort; break; case intlTelInputUtils.validationError.TOO_LONG: message = this.messages.tooLong; break; case intlTelInputUtils.validationError.INVALID_COUNTRY_CODE: message = this.messages.invalidCountryCode; break; case intlTelInputUtils.validationError.NOT_A_NUMBER: // break was intentionally omitted default: message = this.messages.wrongFormat; break; } this.tel.popover({html: true, content: '
' + message + '
', trigger: 'manual'}); this.tel.popover('show'); }; fn.initHiddenValue = function () { this.hidden.val( this.tel.intlTelInput('getNumber', intlTelInputUtils.numberFormat.INTERNATIONAL) ).trigger('change.' + this.ns); }; })(qs.defineNS('lib.form.element'));