c2s_widget_main = function() { var config; var productAttrName = 'data-c2s-wp'; var selectorAttrName = 'data-c2s-ws'; var widgetIdAttrName = 'data-c2s-wid'; var allWidgetAttrPrefixe = 'data-c2s-w'; var allWidgetId; var widgetContainerMap; var productWidgetCount = 0; var selectorWidgetCount = 0; var init = function(_config) { config = _config; searchWidgetContainer(); } function searchWidgetContainer() { let widgetContainerList = document.querySelectorAll('div[' + widgetIdAttrName + ']'); allWidgetId = new Array(); widgetContainerMap = new Map(); if (widgetContainerList !== null && widgetContainerList.length > 0) { for (let i = 0 ;i < widgetContainerList.length; i++) { let container = widgetContainerList[i]; let widgetId = container.getAttribute(widgetIdAttrName); if (widgetId != '') { allWidgetId.push(widgetId); addInWidgetContainerMap(widgetId, container); } } } let oldWidgetContainerList = document.querySelectorAll('div[' + productAttrName + ']'); if (oldWidgetContainerList !== null && oldWidgetContainerList.length > 0) { for (let i = 0 ;i < oldWidgetContainerList.length; i++) { let container = oldWidgetContainerList[i]; let widgetId = container.getAttribute(productAttrName); if (widgetId != '') { allWidgetId.push(widgetId); addInWidgetContainerMap(widgetId, container); } } } oldWidgetContainerList = document.querySelectorAll('div[' + selectorAttrName + ']'); if (oldWidgetContainerList !== null && oldWidgetContainerList.length > 0) { for (let i = 0 ;i < oldWidgetContainerList.length; i++) { let container = oldWidgetContainerList[i]; let widgetId = container.getAttribute(selectorAttrName); if (widgetId != '') { allWidgetId.push(widgetId); addInWidgetContainerMap(widgetId, container); } } } if (allWidgetId.length > 0) { loadStyles(); } if (typeof config.setvBaseUrl !== 'undefined') { let setvWidgetContainerList = document.querySelectorAll('div[data-c2ssetv-wid]'); if (setvWidgetContainerList !== null && setvWidgetContainerList.length > 0) { loadJavascriptAsync(config.setvBaseUrl + '/' + 'wmain.js'); } } } function loadStyles() { var postData = ''; for (let i = 0 ;i < allWidgetId.length; i++) { if (postData != '') postData += '&'; postData += encodeURIComponent('wid') + '=' + encodeURIComponent(allWidgetId[i]); } let httpRequest = new XMLHttpRequest(); httpRequest.open('POST', config.baseUrl + '/widget/styles.json', true); httpRequest.setRequestHeader('Content-type', 'application/x-www-form-urlencoded'); httpRequest.onreadystatechange = loadStylesCallback(httpRequest); httpRequest.send(postData); } function loadScripts() { var postData = ''; for (let i = 0 ;i < allWidgetId.length; i++) { if (postData != '') postData += '&'; postData += encodeURIComponent('wid') + '=' + encodeURIComponent(allWidgetId[i]); } let httpRequest = new XMLHttpRequest(); httpRequest.open('POST', config.baseUrl + '/' + 'widget/scripts.json', true); httpRequest.setRequestHeader('Content-type', 'application/x-www-form-urlencoded'); httpRequest.onreadystatechange = selectorNeededJavascriptListCallback(httpRequest); httpRequest.send(postData); } function loadWidgets() { if (allWidgetId.length > 0) { var singleCallback = new SingleCallback({ countToComplete: getWidgetCount(), callback: function() { loadScripts(); } }); for (let i = 0 ;i < allWidgetId.length; i++) { let widgetId = allWidgetId[i]; widgetContainerMap.get(widgetId).forEach(function(container) { let httpRequest = new XMLHttpRequest(); httpRequest.onreadystatechange = widgetCallback(httpRequest, container, singleCallback); httpRequest.open('GET', config.baseUrl + '/' + 'widget/affichage.html?id=' + encodeURIComponent(widgetId), true); httpRequest.send(); }); } } } function loadStylesCallback(httpRequest) { return function () { if (httpRequest.readyState === XMLHttpRequest.DONE) { if (httpRequest.status === 200) { let styleUrlList = eval(httpRequest.responseText); if (styleUrlList && styleUrlList.length > 0) { for (styleUrl of styleUrlList) { insertStylesheet(styleUrl); } } } loadWidgets(); } }; } function widgetCallback(httpRequest, container, singleCallback) { return function() { if (httpRequest.readyState === XMLHttpRequest.DONE) { if (httpRequest.status === 200) { container.innerHTML = httpRequest.responseText; } singleCallback.requestComplete(); } }; } function insertStylesheet(url) { let head = document.getElementsByTagName('HEAD')[0]; head.appendChild(createStylesheet(url)); } function selectorNeededJavascriptListCallback(httpRequest) { return function() { if (httpRequest.readyState === XMLHttpRequest.DONE && httpRequest.status === 200) { let js_url_list = eval(httpRequest.responseText); if (js_url_list && js_url_list.length > 0) { loadJavascriptOneAfterAnother(js_url_list); } } }; } function createStylesheet(url) { let link = document.createElement('link'); link.rel = 'stylesheet'; link.type = 'text/css'; link.href = url; return link; } function createJavascript(url, callback) { let scrpt = document.createElement('script'); scrpt.type = 'text/javascript'; scrpt.src = url; if (callback) { scrpt.onload = callback; } return scrpt; } function loadJavascriptOneAfterAnother(url_array) { loadJavascriptInArray(url_array, 0); } function loadJavascriptInArray(url_array, i) { if(typeof Promise !== "undefined" && Promise.toString().indexOf("[native code]") !== -1){ var promise_array = []; promise_array.push(loadJavascriptAsync(url_array[i])); Promise.all(promise_array).then(function() { let nextIndex = i + 1; if (nextIndex < url_array.length) { loadJavascriptInArray(url_array, nextIndex); } else { if (typeof c2s_widget_view !== "undefined") { c2s_widget_view.init(); } if (typeof c2s_countdown !== "undefined") { c2s_countdown.init(); } } }); } } function loadJavascriptAsync(uri) { return new Promise(function (resolve, reject) { var tag = document.createElement('script'); tag.src = uri; tag.async = true; tag.onload = function() { resolve(); }; document.body.appendChild(tag); }); } function addInWidgetContainerMap(widgetId, container) { if (widgetContainerMap.has(widgetId)) { widgetContainerMap.get(widgetId).push(container); } else { widgetContainerMap.set(widgetId, [container]); } } function getWidgetCount() { var toReturn = 0; for (var value of widgetContainerMap.values()) { toReturn += value.length; } return toReturn; } var SingleCallback = (function(){ var countToComplete, countCompleted, callback; return function(options) { if (!options) options = {}; countToComplete = options.countToComplete || 0; callback = options.callback; countCompleted = 0; this.requestComplete = function() { countCompleted++; if (countCompleted == countToComplete) {callback();} } } })(); return { init:init } }(); var cs2w_config = { 'pUrl':'https://gzd.clk2s.com/widget/produit.html', 'pCss':'https://gzd.clk2s.com/widget/produit.css', 'sUrl':'https://gzd.clk2s.com/widget/selecteur.html', 'sCss':[ 'https://gzd.clk2s.com/resources/styles/forfait/nouislider.min.css', 'https://gzd.clk2s.com/resources/styles/forfait/jquery.multiselect.css', 'https://gzd.clk2s.com/widget/selecteur.css' ], 'stlJs':'https://gzd.clk2s.com/widget/js-selecteur.json', 'smJs':'https://gzd.clk2s.com/widget/selecteur.js', 'pJs':'https://gzd.clk2s.com/widget/produit.js', 'baseUrl':'https://gzd.clk2s.com' }; if (document.readyState === 'ready' || document.readyState === 'complete' || document.readyState === 'interactive') { c2s_widget_main.init(cs2w_config); } else { document.addEventListener("DOMContentLoaded", function(){c2s_widget_main.init(cs2w_config);}); }