var AdvertValidate = (function () { 'use strict'; /** * @constructor */ function AdvertValidate() { } /** * @private * @name validaterPromise * @type {Object|null} */ var validaterPromise; /** * @public * @property validateActionsUrl * @type {String} */ AdvertValidate.prototype.validateActionsUrl = ''; /** * @public * @property validationConfig * @type {Object|null} */ AdvertValidate.prototype.validationConfig = null; /** * @public * @property validationTexts * @type {Object|null} */ AdvertValidate.prototype.validationTexts = null; /** * @public * @property enableLogging * @type {boolean} */ AdvertValidate.prototype.enableLogging = true; /** * @public * @method setValidateActionsUrl * @param {String} url */ AdvertValidate.prototype.setValidateActionsUrl = function (url) { if (typeof url !== 'string') { throw 'setValidateActionsUrl: invalid parameter provided,' + ' url is not a string, ' + typeof url + ' given'; } this.validateActionsUrl = url; }; /** * @public * @method setValidationConfig * @param configData */ AdvertValidate.prototype.setValidationConfig = function (configData) { if (!jQuery.isPlainObject(configData)) { throw 'setValidationConfig: invalid parameter provided, configData is not an object, ' + typeof configData + ' given'; } this.validationConfig = configData; } /** * @public * @method setValidationTexts * @param texts */ AdvertValidate.prototype.setValidationTexts = function (texts) { if (!jQuery.isPlainObject(texts)) { throw 'setValidationTexts: invalid parameter provided, texts is not an object, ' + typeof texts + ' given'; } this.validationTexts = texts; } /** * @public * @method getValidationTexts * @param text * @returns {String} */ AdvertValidate.prototype.getValidationTexts = function (text) { if (!this.validationTexts.hasOwnProperty(text)) { this.logEvent('getValidationTexts', 'unknown text requested', 'warn'); return ''; } return this.validationTexts[text]; } /** * @public * @method setEnableLogging * @param {Boolean} enableLogging */ AdvertValidate.prototype.setEnableLogging = function (enableLogging) { if (typeof enableLogging !== 'boolean') { throw 'setEnableLogging: invalid parameter provided,' + ' enableLogging is not boolean, ' + typeof enableLogging + ' given'; } this.enableLogging = enableLogging; }; /** * @public * @method validate * @param {string} step * @param {string|int} newValue */ AdvertValidate.prototype.validate = function (step, newValue, additionalValues) { this.logEvent('validate', {step: step, newValue: newValue, additionalValues: additionalValues}); if (typeof step !== 'string') { throw 'validate: invalid parameter provided,' + ' step is not string, ' + typeof step + ' given'; } if (typeof additionalValues === 'undefined') { additionalValues = null; } if (!this.validationConfig || !this.validationConfig.hasOwnProperty(step)) { return true; } else { validaterPromise = jQuery.Deferred(); this.sendValidationRequest('validate', step, newValue, additionalValues); return validaterPromise.promise(); } } /** * * @param step */ AdvertValidate.prototype.resetValues = function (step) { this.logEvent('resetValues', step); this.sendValidationRequest('reset', step, null); } /** * @public * @method sendValidationRequest * @param {string} step * @param {string|int} newValue */ AdvertValidate.prototype.sendValidationRequest = function (action, step, newValue, additionalValues) { this.logEvent('sendValidationRequest', {action: action, step: step, newValue: newValue, additionalValues: additionalValues}); var data = { 'step': step, 'value': newValue, 'additionalValues': additionalValues, 'action': action, }; jQuery.ajax({ url: this.validateActionsUrl, type: 'POST', data: data, dataType: 'json', crossDomain: true, success: function (data) { if (data.isValid === true) { validaterPromise.resolve(data); } else if (data.isValid === false) { swal({ title: this.getValidationTexts('confirm_title'), html: data.message, type: 'warning', showCancelButton: true, confirmButtonText: this.getValidationTexts('confirm_yes'), cancelButtonText: this.getValidationTexts('confirm_no'), confirmButtonClass: "btn btn-primary margin-right-10", cancelButtonClass: "btn btn-default", buttonsStyling: false }).then(function(result) { if (result.value === true) { this.resetValues(step); return false; } else { validaterPromise.reject(); return true; } }.bind(this)); } else if (data.resetSuccess === true) { validaterPromise.resolve(data); } }.bind(this), error: function (data) { this.logEvent('sendValidationRequest', {}, 'error'), validaterPromise.resolve(); }.bind(this) }); } /** * @public * @method logEvent * @param {String} action * @param {String|Object} data * @description * ???? */ AdvertValidate.prototype.logEvent = function (action, data, logLevel) { if (this.enableLogging) { if (typeof logLevel !== 'string') { logLevel = ''; } switch (logLevel) { case 'warn': console.warn(action, data); break; case 'error': console.error(action, data); break; case 'info': console.info(action, data); break; default: console.log(action, data); break; } } }; return AdvertValidate; }( window, window.jQuery, window.swal )); jQuery(document).ready(function() { window.AdvertValidate = new AdvertValidate(); })