'use strict'; ;var booksy = booksy || {}; (function () { var version = '1.0.1'; var scriptRegex = /.*booksy.*\/.*widget\/code.js/; var script = currentScriptElement(scriptRegex); if (!script) errorBreak('cannot locate current script'); var config = parseScriptConfig(script.src); console.log("booksy.widget | config: ", config); config.mode = config.mode || 'dialog'; config.theme = config.theme || 'default'; config.iframeSrc = config.baseUrl + 'index.html?id=' + config.id + '&lang=' + config.lang + '&country=' + config.country + '&mode=' + config.mode + '&theme=' + config.theme; if (!(config.langauge && config.country)) errorBreak('uncomplete configuration', config); var widgetContainer = createWidgetContainer(script, config); if (!booksy.widgetCss) { booksy.widgetCss = loadWidgetStyle(config.baseUrl); }; if (config.mode == 'dialog') { var button = createWidgetButton(widgetContainer); button.addEventListener('click', function () { getOS() === 'other' ? dialogOpen(config.iframeSrc) : createDeepLink(config); }); return; }; if (config.mode == "inline") { createIframe(widgetContainer, config.iframeSrc); return; }; errorBreak('unexpected widget mode', config.mode); /** * * Functions * */ function errorBreak() { var args = Array.prototype.slice.call(arguments); try { console.log.apply(console, args.unshift('[Booksy][widget][error]')); } catch (e) {}; return; } function currentScriptElement(scriptRegex) { var scripts = document.getElementsByTagName('script'); for (var i = scripts.length - 1; i > -1; i--) { if (scripts[i].src.search(scriptRegex) > -1) { var selected = scripts[i]; if (!booksy.codejs) { booksy.codejs = [selected.src]; return selected; } else { if (booksy.codejs.indexOf(selected.src) === -1) { booksy.codejs.push(selected.src); return selected; } } } } }; function parseScriptConfig(src) { var config = { baseUrl: src.replace(/\/code\.js.*/, '\/').replace(/https:\/\/w\.booksy.(com|net)\/.._..\//, 'https://booksy.com/').replace(/https:\/\/widget-..\.booksy.(com|net)\/.._..\//, 'https://booksy.com/').replace(/https:\/\/booksy.net\//, 'https://booksy.com/') }; var params = src.split('code.js?')[1]; if (params) { var params = params.split('&'); for (var i = params.length - 1; i > -1; i--) { var kv = params[i].split('='); config[kv[0]] = kv[1]; } } // old format fallback if (!config.lang) { var match = src.match(/https?:\/\/[^\/]*\/([a-z][a-z])[_\-]([a-zA-Z][a-zA-Z])\/widget/); if (match) { config.lang = match[1]; config.country = match[2]; } } return config; } function createWidgetContainer(script, config) { var cls = 'booksy-widget-container'; var containerClass = [cls, cls + '-' + config.mode, cls + '-' + config.theme, cls + '-' + config.lang].join(' '); // widget container element (placed before script element) var div = document.createElement('div'); //div.setAttribute( 'id', widget.containerId ); div.setAttribute('class', containerClass); script.parentNode.insertBefore(div, script); return div; } function createWidgetButton(container) { var button = document.createElement('div'); button.setAttribute('class', 'booksy-widget-button'); container.appendChild(button); return button; }; function loadWidgetStyle(baseUrl) { document.head.insertAdjacentHTML('beforeend', '\n \n '); return true; } function generateUniqueId(pattern) { pattern = pattern || 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'; return pattern.replace(/[xy]/g, function (c) { var r = Math.random() * 16 | 0, v = c == 'x' ? r : r & 0x3 | 0x8; return v.toString(16); }); }; function iframeFactory(options) { var opts = options || {}; // inject unique identifier into iframe source address opts.src += (opts.src.indexOf('?') > -1 ? '&' : '?') + 'uniqueId=' + opts.uniqueId; var container = opts.container || document.body; var iframeId = undefined; return { create: create, element: function element() { return container.querySelector('iframe'); } }; function create() { if (iframeId) return; iframeId = generateUniqueId('iframe-xxxx'); container.insertAdjacentHTML('beforeend', '\n