var CONTEXT = (function () { var componentTemplates = {}, renderedComponentTemplates = {}, exposed = {}; function init () { $(function () { $('ul.page-components > li.page-component').each(function () { setTemplate($(this).prop('id')); render($(this).prop('id')); }); if(context.offer?.displaytype === 'popup') initPopupCloseButton(); $('ul.page-components').removeClass('loading'); $(document).trigger('context.loaded'); }); exposed.init = null; } function initPopupCloseButton () { // For now, it just sets a handler for close popup button const closePopupButton = document.querySelector('#theme-popup-exit .btn'); if (closePopupButton && typeof WooControl !== 'undefined') closePopupButton.addEventListener('click', WooControl.closePopup, { capture: true }); } function setTemplate (id) { var rawTemplateEl = document.getElementById(id+'-tpl'), templateEl; if (rawTemplateEl) { // create a new element that stores the decoded version of the template outside of the DOM templateEl = document.createElement('div'); templateEl.innerHTML = util.htmlspecialchars_decode(String(String(rawTemplateEl.innerHTML))); // added page-component-wrap for backgrounding on partials componentTemplates[id] = templateEl.innerHTML; for (var i = templateEl.childNodes.length - 1; i > 0; i--) { if (typeof templateEl.childNodes[i].className != 'undefined' && templateEl.childNodes[i].className.indexOf('data-template-partial') > -1) { templateEl.removeChild(templateEl.childNodes[i]); } } } else { //otherwise, the template is already in the component li and will be replaced templateEl = document.querySelector('#'+id+' > .page-component-wrap'); } if (templateEl) { try { renderedComponentTemplates[id] = Handlebars.compile(templateEl.innerHTML); } catch (err) { return false; } return true; } } function refresh (params) { var deferred = new $.Deferred(); if (!params) params = {}; params['ajax'] = 1; $.ajax({ type: "GET", async: true, dataType: 'json', url: "/" + options.shard + "/context/" + options.page, data: params, success: function (data) { context = data; deferred.resolve(context); } }); return deferred; } function render (componentId) { var componentEl = document.getElementById(componentId); var componentWrapEl = componentEl.querySelector('.page-component-wrap'); if (!renderedComponentTemplates[componentId]) { console.error('missing template in render!', componentId, renderedComponentTemplates); return; } try { componentWrapEl.innerHTML = renderedComponentTemplates[componentId](context); // Convert data-style to style, to fix ie stripping context styles out $(componentEl).find('[data-style]').each(function(e) { this.setAttribute('style', this.getAttribute('data-style')); }); $(document).trigger('context.componentLoaded', componentId); } catch (err) { console.error('render error', err); componentWrapEl.innerHTML = '
"); return new Handlebars.SafeString(""); }); Handlebars.registerHelper('concat', function(params) { var result = ''; for (var i = 0; i < arguments.length; i++) { if (typeof arguments[i] == 'string') result = result + arguments[i]; } return result; }); Handlebars.registerHelper('shortNumber', function(number, precision, noDataFiller) { if (typeof noDataFiller == 'undefined') noDataFiller = 'N/A'; precision = precision || 1; if (number === false || number == null) return noDataFiller; var returnNumber = ''; switch (true) { case (number > 999999999): returnNumber = parseFloat(number/1000000000).toFixed(precision)+"B"; break; case (number > 999999): returnNumber = parseFloat(number/1000000).toFixed(precision)+"M"; break; case (number > 999): returnNumber = parseFloat(number/1000).toFixed(precision)+"K"; break; default: returnNumber = parseFloat(number, precision); break; } return returnNumber; }); Handlebars.registerHelper('nl2br', function(text) { text = Handlebars.Utils.escapeExpression(text); text = text.replace(/[\n\r]/g, ' '; } try { if (FACEBOOK && FACEBOOK.parse) //this is in its own block to prevent FB errors from crashing the element FACEBOOK.parse(componentWrapEl); } catch (err) { console.error('fb render error', err); } } function getContext () { return context; } function setContext (newContext) { $.extend(context, newContext); } // Handlebars Helpers Handlebars.registerHelper('pluralize', function(number, single, plural) { if (number === 1) { return single; } return plural; }); Handlebars.registerHelper("one_index", function (index){ return parseInt(index) + 1; }); Handlebars.registerHelper('if_eq', function(a, b, opts) { if(a == b) return opts.fn(this); return opts.inverse(this); }); Handlebars.registerHelper('if_includes', function(source, target, opts) { let arr = source.replace(/\s/g, '').split(','); // remove white spaces and split if(Array.isArray(arr) && target && arr.includes(target)) return opts.fn(this); return opts.inverse(this); }); Handlebars.registerHelper('compile_template', function(template) { var compiled_template = Handlebars.compile(template); return compiled_template(context); }); Handlebars.registerHelper('if_neq', function(a, b, opts) { if(a != b) return opts.fn(this); return opts.inverse(this); }); Handlebars.registerHelper('if_key', function(object, key, opts) { if (object[key]) return opts.fn(this); return opts.inverse(this); }); Handlebars.registerHelper('if_or', function (a, b, options) { return (a || b) ? options.fn(this) : options.inverse(this); }); Handlebars.registerHelper('if_lt', function (a, b, options) { return (a < b) ? options.fn(this) : options.inverse(this); }); Handlebars.registerHelper('img', function(src, classes_string, width, height) { var style_string = ""; if (typeof src == 'undefined' || !src) src = '/assets/images/dashboard/placeholders/missing-image_square.png'; //todo if (classes_string && typeof classes_string === 'string') classes_string = ' class="'+classes_string+'" '; else classes_string = ''; if (width || height) { style_string = ' style="'+(width ? 'width: '+width+'px;' : "")+(height ? 'height: '+height+'px;' : "")+'"'; } return new Handlebars.SafeString(''); }); Handlebars.registerHelper('bgimg', function(src) { if (typeof src == 'undefined' || !src) src = '/img/app/question-bracket.png'; //todo return new Handlebars.SafeString('background-image:url('+src+');'); }); Handlebars.registerHelper('video', function(url) { if (!url) return ""; if (url.indexOf('facebook.com') > -1) return new Handlebars.SafeString("