/** * Glass API wrapper script. * requires jquery v1.9 or newer */ //namespace glassapi var glassapi = new (function () { var API_REQUEST_URI = 'https://www.misterspex.de/__service/lens-service/api/request'; /** function to serialize an object to url including an optional suffix */ var serializeToUrl = function (obj, suffix) { var str = []; for (var field in obj) str.push(encodeURIComponent(field) + suffix + '=' + encodeURIComponent(obj[field])); return str.join('&'); }; var isArray = function (o) { return Object.prototype.toString.call(o) === '[object Array]'; }; var attrToString = function (obj, prefix, wasArray) { if (wasArray === 'undefined') wasArray = false; var str = []; for (var p in obj) { var v = obj[p]; var bIsArray = isArray(v); var bIsObject = typeof v == 'object'; var bIsString = typeof v == 'string'; var bUseDot = !wasArray; //if (!bUseDot) alert(p); var k = prefix ? prefix + (bUseDot ? '.' : '[') + p + (bUseDot ? '' : ']') : p; str.push( bIsObject ? attrToString(v, k, bIsArray) + '\n' : bIsString ? k + '="' + v + '"' : k + '=' + v, ); } return str.join('\n'); }; //see editor backend for value doc var GlassAttributes = function () { this.glassType = 'monofocal'; //monofocal, multifocal this.refractionIndex = 1.5; //1.5, 1.6, 1.67, 1.74 this.tingePercentage = 0; //0 = no tinge, 75, 85, 100 = multifocal this.gradientPercentage = 0; //0 = no tinge, 40 this.tingeColor = 'none'; //"none", "brown", "green", "gray" this.tingeType = 'none'; //"none", "mirrored", "polarized", "original" this.lotus = false; //bool this.digitalRelax = false; //bool this.fieldOfVision = 'none'; //string: "none", "normal", "extended" this.PDKnown = false; //bool (new IIP-9809) this.nearDistanceSupport = false; //bool }; var CorrectionValues = function () { this.sphere = 0.0; //from -9 to +8 in 0.25 steps this.cylinder = 0.0; //from -4 to +4 in 0.25 steps this.axis = 0.0; //0 to 180 this.addition = 0.0; //from +1 to +3 in 0.25 steps - just for multifocal glasses this.PD = 0.0; //from 25 to 38 in 0.5 steps }; var Glass = function () { var attributes = new GlassAttributes(); var correctionLeft = new CorrectionValues(); var correctionRight = new CorrectionValues(); //setter / getter for correction values - left eye: this.setLeftSphere = function (sphere) { correctionLeft.sphere = parseFloat(sphere); }; this.getLeftSphere = function () { return correctionLeft.sphere; }; this.setLeftCylinder = function (cylinder) { correctionLeft.cylinder = parseFloat(cylinder); }; this.getLeftCylinder = function () { return correctionLeft.cylinder; }; this.setLeftAxis = function (axis) { correctionLeft.axis = parseInt(axis); }; this.getLeftAxis = function () { return correctionLeft.axis; }; this.setLeftAddition = function (addition) { correctionLeft.addition = parseFloat(addition); }; this.getLeftAddition = function () { return correctionLeft.addition; }; this.setLeftPupilDistance = function (PD) { correctionLeft.PD = parseFloat(PD); }; this.getLeftPupilDistance = function () { return correctionLeft.PD; }; //setter / getter for correction values - right eye: this.setRightSphere = function (sphere) { correctionRight.sphere = parseFloat(sphere); }; this.getRightSphere = function () { return correctionRight.sphere; }; this.setRightCylinder = function (cylinder) { correctionRight.cylinder = parseFloat(cylinder); }; this.getRightCylinder = function () { return correctionRight.cylinder; }; this.setRightAxis = function (axis) { correctionRight.axis = parseInt(axis); }; this.getRightAxis = function () { return correctionRight.axis; }; this.setRightAddition = function (addition) { correctionRight.addition = parseFloat(addition); }; this.getRightAddition = function () { return correctionRight.addition; }; this.setRightPupilDistance = function (PD) { correctionRight.PD = parseFloat(PD); }; this.getRightPupilDistance = function () { return correctionRight.PD; }; //setter / getter for glass attributes: this.setAttrGlassType = function (glassType) { attributes.glassType = glassType; }; this.getAttrGlassType = function () { return attributes.glassType; }; this.setAttrRefractionIndex = function (refractionIndex) { attributes.refractionIndex = parseFloat(refractionIndex); }; this.getAttrRefractionIndex = function () { return attributes.refractionIndex; }; this.setAttrTingePercentage = function (tingePercentage) { attributes.tingePercentage = parseInt(tingePercentage); }; this.getAttrTingePercentage = function () { return attributes.tingePercentage; }; this.setAttrGradientPercentage = function (gradientPercentage) { attributes.gradientPercentage = parseInt(gradientPercentage); }; this.getAttrGradientPercentage = function () { return attributes.gradientPercentage; }; this.setAttrTingeColor = function (tingeColor) { attributes.tingeColor = tingeColor; }; this.getAttrTingeColor = function () { return attributes.tingeColor; }; this.setAttrTingeType = function (tingeType) { attributes.tingeType = tingeType; }; this.getAttrTingeType = function () { return attributes.tingeType; }; this.setAttrLotusEffect = function (lotusEffect) { attributes.lotus = lotusEffect; }; //boolean this.getAttrLotusEffect = function () { return attributes.lotus; }; this.setAttrDigitalRelax = function (digitalRelax) { attributes.digitalRelax = digitalRelax; }; //boolean this.getAttrDigitalRelax = function () { return attributes.digitalRelax; }; this.setAttrFieldOfVision = function (fieldOfVision) { attributes.fieldOfVision = fieldOfVision; }; this.getAttrFieldOfVision = function () { return attributes.fieldOfVision; }; this.setPDKnown = function (pdKnown) { attributes.PDKnown = pdKnown; }; this.getPDKnown = function () { return attributes.PDKnown; }; this.setAdjustToRecommendation = function (adjustToRecommendation) { attributes.adjustToRecommendation = adjustToRecommendation; }; //boolean this.getAdjustToRecommendation = function () { return attributes.adjustToRecommendation; }; //boolean this.setAttrNearDistanceSupport = function (nearDistanceSupport) { attributes.nearDistanceSupport = nearDistanceSupport; }; //boolean this.getAttrNearDistanceSupport = function () { return attributes.nearDistanceSupport; }; this.toData = function () { var sData = serializeToUrl(attributes, '') + '&' + serializeToUrl(correctionLeft, 'L') + '&' + serializeToUrl(correctionRight, 'R'); return sData; }; this.toString = function () { var s = attrToString(attributes, 'attributes') + '\n\n' + attrToString(correctionLeft, 'correctionLeft') + '\n\n' + attrToString(correctionRight, 'correctionRight'); return s; }; }; var GlassApiResult = function (data, requestArgs) { this.input = function () { return data.input; }; this.getRequestArguments = function () { return requestArgs; }; //document accessors: this.isValid = function () { return data.output.valid; }; this.hasError = function () { return !(data.output.errors == null || data.output.errors.length > 0); }; this.getErrors = function () { return data.output.errors; }; this.hasAdjustment = function () { return data.output.adjustments != null && data.output.adjustments.length > 0; }; this.getAdjustments = function () { return data.output.adjustments; }; this.getRecommendedIndex = function () { return data.output.recommendedIndex; }; this.getRecommendedFieldOfVision = function () { return data.output.recommendedFieldOfVision; }; this.getTotalPrice = function () { return data.output.totalPrice; }; this.getAttributeSurcharge = function () { return data.output.attributeSurcharge; }; this.getMultifocalSurcharge = function () { return data.output.glassTypeOptions.multifocal.surcharge; }; this.getSunglassSurcharge = function () { return data.output.sunglassSurcharge; }; this.getGlazingToSampleSurcharge = function () { return data.output.glazingToSampleSurcharge; }; this.getCorvalSurchargeLeft = function () { return data.output.corvalSurchargeLeft; }; this.getCorvalSurchargeRight = function () { return data.output.corvalSurchargeRight; }; this.getLotusSurcharge = function () { return data.output.lotusSurcharge; }; this.getDigitalRelaxSurcharge = function () { return data.output.digitalRelaxSurcharge; }; this.isLotusIncluded = function () { return data.output.lotusIncluded; }; this.isLotusPossible = function () { return data.output.lotusPossible; }; this.isDigitalRelaxPossible = function () { return data.output.digitalRelaxPossible; }; this.getIndexes = function () { return data.output.indexOptions; }; this.getColorOptions = function () { return data.output.tingeOptions; }; this.getFieldOfVisionOptions = function () { return data.output.fieldOfVisionOptions; }; this.getGlassTypeOptions = function () { return data.output.glassTypeOptions; }; this.hasInternalError = function () { return data.InternalError; }; this.getInternalErrorText = function () { return data.InternalErrorText; }; this.getRawData = function () { return data; }; this.toString = function () { return attrToString(data); }; }; /** GlassApi object (Singleton) */ this.glassApi = new (function () { var _this = this; var config = { channel: 'de', category: 'glasses', multifocalCapableFrame: false, source: 'shop', }; var configFile = null; this.requestParam = ''; var callbackFunc = null; this.setup = function (configurationFile, conf, callback) { config = conf; configFile = configurationFile; this.requestParam = serializeToUrl(config, ''); callbackFunc = callback; }; this.glass = new Glass(); var getRequestUri = function () { var c = ''; if (configFile) { c = configFile + '/'; } var url = API_REQUEST_URI + '/'; url += c; return url; }; /** request api */ this.request = function () { if (!callbackFunc) { alert('No callback function defined, set it before request!'); console.error('Callback function needs to be defined when setting up the glassApi'); return; } var uri = getRequestUri(); var x = { url: uri, data: this.requestParam + '&' + this.glass.toData(), }, args = [].slice.call(arguments); $.ajax(x) .done(function (data) { _this.glassApiCallback(data, args); }) .fail(function (jqXHR, textStatus, errorThrown) { alert('ajax error'); console.error('Fetching data from ' + JSON.stringify(x) + ' failed.', errorThrown); }); }; this.glassApiCallback = function (data, requestArgs) { var go = new GlassApiResult(data, requestArgs); callbackFunc(go); }; })(); })();