(function (window) { 'use strict'; var glasseson = window.glasseson; glasseson.sdkWrapper = new function() { var results = { prescription: null, status: 'incomplete', success: false }; var bindListener = new function() { var copyGlassListeners = { sendSmsListeners : { onSuccess : function() {}, onError : function() {} }, prescriptionListeners : { onPresciptionReady : function() {}, onClose : function () {}, update: function(){}, onError: function(){}, onStageChange: function(){}, } }; window.addEventListener('message', function(event) { var origin = event.origin || event.originalEvent.origin; // For Chrome, the origin property is in the event.originalEvent object. if (origin !== glasseson.origin) // TODO : Should be rendered from the server return; var result = JSON.parse(event.data); var stringifyPrescirptionField = function(field, points) { if (field == "-") { return field; } var f = parseFloat(field); if (f == 0) { return f.toFixed(points).toString(); } var v = f.toFixed(points).toString(); return (v.indexOf("-") == 0 ? v : ("+" + v)); }; switch (result.type) { case 'sms': if (result.data.error == null) { copyGlassListeners.sendSmsListeners.onSuccess(); } else { results.status = 'ERROR'; copyGlassListeners.sendSmsListeners.onError(results); } break; case 'copy-glass.prescription-ready': // complete results.status = 'complete'; results.success = true; var prescription = result.data.prescription; prescription.l_sph = stringifyPrescirptionField(prescription.l_sph, 2); prescription.r_sph = stringifyPrescirptionField(prescription.r_sph, 2); prescription.l_cyl = stringifyPrescirptionField(prescription.l_cyl, 2); prescription.r_cyl = stringifyPrescirptionField(prescription.r_cyl, 2); prescription.l_axis = prescription.l_axis.toString(); prescription.r_axis = prescription.r_axis.toString(); prescription.pd = parseFloat(prescription.pd).toFixed(1).toString(); prescription.error_msg = prescription.error_msg; prescription.error_type = prescription.error_type; prescription.error_title = prescription.error_title; prescription.note = prescription.note; copyGlassListeners.prescriptionListeners.update(prescription); copyGlassListeners.prescriptionListeners.onPresciptionReady(results); break; case 'close-copy-glass': copyGlassListeners.prescriptionListeners.onClose(); break; case 'copy-glass.prescription-incomplete': // incomplete if (result.data.prescription != null) { var prescription = result.data.prescription; prescription.l_sph = stringifyPrescirptionField(prescription.l_sph, 2); prescription.r_sph = stringifyPrescirptionField(prescription.r_sph, 2); prescription.l_cyl = stringifyPrescirptionField(prescription.l_cyl, 2); prescription.r_cyl = stringifyPrescirptionField(prescription.r_cyl, 2); prescription.l_axis = prescription.l_axis.toString(); prescription.r_axis = prescription.r_axis.toString(); prescription.pd = parseFloat(prescription.pd).toFixed(1).toString(); prescription.error_msg = prescription.error_msg; prescription.error_type = prescription.error_type; prescription.error_title = prescription.error_title; prescription.note = prescription.note; copyGlassListeners.prescriptionListeners.update(prescription); if (prescription.error === true) { results.status = 'ERROR'; copyGlassListeners.prescriptionListeners.onError(results); } else { copyGlassListeners.prescriptionListeners.onStageChange({stage:'onIncompletePrescription'}); } } break; case 'copy-glass.stage-change': copyGlassListeners.prescriptionListeners.onStageChange(result.data); break; } }, false); var bindSendSmsListeners = function(listeners) { copyGlassListeners.sendSmsListeners = listeners; // We might need to use this here }; var bindPrescriptionListeners = function(listeners) { copyGlassListeners.prescriptionListeners = listeners; }; return { bindSendSmsListeners : bindSendSmsListeners, bindPrescriptionListeners : bindPrescriptionListeners, listeners : copyGlassListeners }; }; var _isInitialized = false; var wrapper; var init = function(clientId) { wrapper = document.createElement("iframe"); wrapper.style.position = 'absolute'; wrapper.style.left = 0; wrapper.style.top = 0; wrapper.style.display = 'none'; wrapper.onreadystatechange = function () { if (wrapper.readyState !== 'complete') { return; } }; wrapper.onload = function() { _isInitialized = true; // Once the SDK have been loaded (the iframe) we need to execute other method that have been called during initialization for (var i in glasseson._q || []) { var args = glasseson._q[i]; var params = [].slice.call(args); var method = params.shift(); if (method !== 'init') { glasseson[method].apply(glasseson, [].slice.call(params[0])); } } }; wrapper.src = glasseson.origin + '/sdk/wrapper/' + glasseson.clientId + '/' + document.domain; document.body.appendChild(wrapper); }; var sendSms = function(phoneNumber, onSuccess, onError) { bindListener.bindSendSmsListeners({ onSuccess : onSuccess, onError : onError }); wrapper.contentWindow.postMessage(JSON.stringify({ type: 'sms', data: { phoneNumber: phoneNumber } }), glasseson.origin); }; var highestZindex = function () { var highestZ; var lowestZ; var onefound = false; var divs = document.getElementsByTagName('*'); if( ! divs.length ) { return Array(highestZ,lowestZ); } for( var i=0; i highestZ ) { highestZ = ii; } if( ii < lowestZ ) { lowestZ = ii; } } } } return highestZ; }; var close = function(callback) { document.body.removeChild(glasseson.node); document.body.style.margin = glasseson.body.margin; document.body.style.padding = glasseson.body.padding; document.body.style.overflow = glasseson.body.overflow; document.body.style.scrollTop = glasseson.body.scrollTop; document.body.style.position = glasseson.body.position; if (callback != null) { callback(); } }; var startSession = function(onload, onfinish, onStageChange, options) { // TODO : If the user will call send sms after starting a session var node = document.createElement("iframe"); node.frameBorder = 0; node.marginwidth = '0'; node.marginheight = '0'; node.hspace = '0'; node.vspace = '0'; node.scrolling = 'no'; node.style.position = 'fixed'; node.style.left = 0; node.style.top = 0; node.style.width = '100%'; node.style.height = '100vh'; node.style.overflow = 'hidden'; node.style.zIndex = 999; node.width = '100%'; node.height = '100%'; node.setAttribute("allowfullscreen", "true"); node.onreadystatechange = function () { if (node.readyState !== 'complete') { return; } }; node.onload = function() { // Persiste the previous body style glasseson.body = { margin : document.body.style.margin, padding : document.body.style.padding, overflow : document.body.style.overflow, scrollTop: document.body.style.scrollTop, position: document.body.style.position }; document.body.style.margin = '0'; document.body.style.padding = '0'; document.body.style.overflow = 'hidden'; document.body.scrollTop = '0'; document.body.style.position = 'relative'; onload(); }; node.src = glasseson.origin + '/copy-glass?client_id=' + glasseson.clientId + '&client_domain=' + document.domain + '&skipSms=' + glasseson.smsWasSent; glasseson.node = node; bindListener.bindPrescriptionListeners({ onPresciptionReady : onfinish, onClose : function() { close(function() { onfinish(results); }); }, update: function(prescription){ if (prescription) { results.prescription = prescription } }, onError: onfinish, onStageChange: onStageChange }); document.body.appendChild(node); }; return { init : init, sendSms : sendSms, startSession: startSession, close: close, isInitialized : function() { return _isInitialized; } }; }; glasseson.smsWasSent = false; glasseson.started = false; glasseson.sendSms = function(phoneNumber, onSuccess, onError) { // We can't execute the send SMS before the sdkWrapper is ready if (glasseson.sdkWrapper.isInitialized()) { glasseson.sdkWrapper.sendSms(phoneNumber, function() {glasseson.smsWasSent = true; onSuccess(); }, onError); } else { // Once ready we will travers the queue glasseson._q.push(['sendSms', arguments]); } }; glasseson.init = function(clientId) { if (glasseson.clientId != null) { console.error('You have called glasseson.init more than once.'); return; } glasseson.clientId = clientId; glasseson.sdkWrapper.init(clientId); }; // Start the glasseson flow // Open an iframe glasseson.start = function(onload, onfinish, onStageChange, options) { if (glasseson.started == true) { console.error('You have called glasseson.start more than once.'); return; } glasseson.started = true; glasseson.sdkWrapper.startSession(onload, function(args){ glasseson.started = false; onfinish.apply(this, arguments); }, onStageChange, options); }; // Close the iframe glasseson.close = function() { glasseson.started = false; glasseson.sdkWrapper.close(); } // If the user tried to execute a method before the sdk have been downloaded // The first method that the user should execute should be init but if our users didn't use the SDK // as needed we will travers the queue for the init method and execute it first // Inside the sdkWrapper.init we load the rest of the methods after they have been loaded for (var i in glasseson._q || []) { var args = glasseson._q[i]; var params = [].slice.call(args); var method = params.shift(); if (method === 'init') { glasseson[method].apply(glasseson, [].slice.call(params[0])); } } }(window));