Press n or j to go to the next uncovered block, b, p or k for the previous block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 | 68x 68x 68x 68x 68x 68x 68x 68x 68x 68x 68x 68x 68x 68x 5x 2x 2x 2x 1x 2x 68x 2x 1x 68x 68x 68x 2x 2x 2x 2x 2x 2x 5x 5x 1x 1x 2x 2x 1x 1x 4x 4x 4x 67x 7x 7x 7x 1x 6x 1x 1x 5x 2x 2x 2x 3x 2x 2x 2x 1x 2x 1x 4x 4x 1x 4x 4x 1x 3x 29x 1x 28x 15x 13x 4x 11x 2x 9x 15x 2x 15x 14x 2x 2x 1x 1x 4x 4x 4x 4x 2x 2x 1x 1x 15x 15x 3x 12x 2x 2x 12x 1x 1x 11x 8x 8x 8x 8x 4x 4x 4x 1x 3x 1x 1x 2x 8x 4x 4x 3x 3x 11x 1x 10x 12x 10x 10x 2x 1x | class AircallPhone { constructor(opts = { debug: true }) { // internal vars // window object of loaded aircall phone this.phoneWindow = null; this.integrationSettings = {}; this.path = null; this.userSettings = {}; this.eventsRegistered = {}; this.phoneLoginState = false; const URL_REGEX = /https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{2,256}\.[a-z]{2,6}\b([-a-zA-Z0-9@:%_\+.~#?&//=]*)/gi; // options passed this.phoneUrl = opts.phoneUrl !== undefined && URL_REGEX.test(opts.phoneUrl) === true ? opts.phoneUrl : 'https://phone.aircall.io'; this.domToLoadPhone = opts.domToLoadPhone; this.integrationToLoad = opts.integrationToLoad; this.path = opts.path; this.debug = opts.debug; // 3 different sizes: big/small/auto this.size = opts.size || 'big'; this.onLogin = () => { if (typeof opts.onLogin === 'function' && this.phoneLoginState === false) { this.phoneLoginState = true; const data = { user: this.userSettings, }; if (Object.keys(this.integrationSettings).length > 0) { data.settings = this.integrationSettings; } opts.onLogin(data); } }; this.onLogout = () => { if (typeof opts.onLogout === 'function') { opts.onLogout(); } }; // local window this.w = opts.window || window; // launch postmessage listener this._messageListener(); // load phone in specified dom if (!!this.domToLoadPhone) { this._createPhoneIframe(); } } _resetData() { this.phoneWindow = null; this.path = null; this.integrationSettings = {}; this.userSettings = {}; this.phoneLoginState = false; } _createPhoneIframe() { let sizeStyle = ''; switch (this.size) { case 'big': sizeStyle = 'height:666px; width:376px;'; break; case 'small': sizeStyle = 'height:600px; width:376px;'; break; case 'auto': sizeStyle = 'height:100%; width:100%;'; break; } // we get the passed dom try { const el = document.querySelector(this.domToLoadPhone); el.innerHTML = `<iframe allow="microphone; autoplay; clipboard-read; clipboard-write; hid" src="${this.getUrlToLoad()}" style="${sizeStyle}"></iframe>`; } catch (e) { // couldnt query the dom wanted this._log( 'error', `[AircallEverywhere] [iframe creation] ${this.domToLoadPhone} not be found. Error:`, e ); } } _messageListener() { this.w.addEventListener( 'message', (event) => { this._log('info', '[AircallEverywhere] [event listener] received event', event); // we test if our format object is present. if not, we stop const matchPrefixRegex = /^apm_phone_/; if (!event.data || !event.data.name || !matchPrefixRegex.test(event.data.name)) { return false; } // initialisation message if (event.data.name === 'apm_phone_loaded') { this._handleInitMessage(event); return; } // integration settings sent by phone if (event.data.name === 'apm_phone_integration_settings' && !!event.data.value) { this.integrationSettings = event.data.value; // init callback after settings received this.onLogin(); return; } // phone logout if (event.data.name === 'apm_phone_logout') { // we clean data related to user this._resetData(); this.onLogout(); return; } // loop over events registered for (const eventName in this.eventsRegistered) { if (event.data.name === `apm_phone_${eventName}`) { // event triggered => we execute callback this.eventsRegistered[eventName](event.data.value); } } }, false ); } _handleInitMessage(event) { // we keep the source this.phoneWindow = { source: event.source, origin: event.origin, }; if (!!event.data.value) { this.userSettings = event.data.value; } // we answer init this.phoneWindow.source.postMessage( { name: 'apm_app_isready', path: this.path }, this.phoneWindow.origin ); // we ask for integration settings if (!!this.integrationToLoad) { this.phoneWindow.source.postMessage( { name: 'apm_app_get_settings', value: this.integrationToLoad }, this.phoneWindow.origin ); } else { // init callback now if present this.onLogin(); } } _log(action, ...restArguments) { if (typeof action !== 'string') { throw new Error('[AircallEverywhere] [_log] Must provide valid console action'); } // logging turned off, don't do anything if (!this.debug) { return; } // if valid action, execute with given args, otherwise default to info console[action] ? console[action](...restArguments) : console.info(...restArguments); } getUrlToLoad() { return `${this.phoneUrl}?integration=generic`; } on(eventName, callback) { if (!eventName || typeof callback !== 'function') { throw new Error( '[AircallEverywhere] [on function] Invalid parameters format. Expected non empty string and function' ); } this.eventsRegistered[eventName] = callback; } _handleSendError(error, callback) { if (!error || !error.code) { // should not happen, unknown error error = { code: 'unknown_error', }; } // errors sent by the phone for specific events are not handled since they should have their code AND message if (!!error && !error.message) { switch (error.code) { case 'unknown_error': error.message = 'Unknown error. Contact aircall developers dev@aircall.io'; break; case 'no_event_name': error.message = 'Invalid parameter eventName. Expected an non empty string'; break; case 'not_ready': error.message = 'Aircall Phone has not been identified yet or is not ready. Wait for "onLogin" callback'; break; case 'no_answer': error.message = 'No answer from the phone. Check if the phone is logged in'; break; case 'invalid_response': error.message = 'Invalid response from the phone. Contact aircall developers dev@aircall.io'; break; default: // specific error without a message. Should not happen error.message = 'Generic error message'; break; } } // we log the error this._log('error', `[AircallEverywhere] [send function] ${error.message}`); // we send the callback with the error if (typeof callback === 'function') { callback(false, error); } } send(eventName, data, callback) { if (typeof data === 'function' && !callback) { callback = data; data = undefined; } if (!eventName) { this._handleSendError({ code: 'no_event_name' }, callback); return false; } if (!!this.phoneWindow && !!this.phoneWindow.source) { let responseTimeout = null; let timeoutLimit = 2000; // we send the message this.phoneWindow.source.postMessage( { name: `apm_app_${eventName}`, value: data }, this.phoneWindow.origin ); // we wait for a response to this message this.on(`${eventName}_response`, (response) => { // we have a response, we remove listener and return the callback this.removeListener(`${eventName}_response`); clearTimeout(responseTimeout); // we evaluate response if (!!response && response.success === false) { // phone answers with an error this._handleSendError( { code: response.errorCode, message: response.errorMessage }, callback ); } else if (!!response && response.success === true) { // phone answer a succes with its response Eif (typeof callback === 'function') { callback(true, response.data); } } else { // phone answer is invalid this._handleSendError({ code: 'invalid_response' }, callback); } }); responseTimeout = setTimeout(() => { // if no response, we remove listener this.removeListener(`${eventName}_response`); this._handleSendError({ code: 'no_answer' }, callback); }, timeoutLimit); } else { this._handleSendError({ code: 'not_ready' }, callback); return false; } } removeListener(eventName) { if (!this.eventsRegistered[eventName]) { return false; } Object.keys(this.eventsRegistered) .filter((key) => key === eventName) .forEach((key) => delete this.eventsRegistered[key]); return true; } isLoggedIn(callback) { // we simply send an event and send its result. this.send('is_logged_in', (success) => { callback(success); }); } } export default AircallPhone; |