const ckEditors = new Map(); /** * Takes a given route, retrieves the HTML, and then updates the content * * @param route * @param link * @param method * @param target * @param showPageLoading * @param callback * @param data */ Mautic.loadContent = function (route, link, method, target, showPageLoading, callback, data) { if (typeof Mautic.loadContentXhr == 'undefined') { Mautic.loadContentXhr = {}; } else if (typeof Mautic.loadContentXhr[target] != 'undefined') { Mautic.loadContentXhr[target].abort(); } showPageLoading = (typeof showPageLoading == 'undefined' || showPageLoading) ? true : false; Mautic.loadContentXhr[target] = mQuery.ajax({ showLoadingBar: showPageLoading, url: route, type: method, dataType: "json", data: data, success: function (response) { if (response) { response.stopPageLoading = showPageLoading; if (response.callback) { window["Mautic"][response.callback].apply('window', [response]); return; } if (response.redirect) { Mautic.redirectWithBackdrop(response.redirect); } else if (target || response.target) { if (target) response.target = target; Mautic.processPageContent(response); } else { //clear the live cache MauticVars.liveCache = new Array(); MauticVars.lastSearchStr = ''; //set route and activeLink if the response didn't override if (typeof response.route === 'undefined') { response.route = route; } if (typeof response.activeLink === 'undefined' && link) { response.activeLink = link; } Mautic.processPageContent(response); } //restore button class if applicable Mautic.stopIconSpinPostEvent(); } MauticVars.routeInProgress = ''; }, error: function (request, textStatus, errorThrown) { Mautic.processAjaxError(request, textStatus, errorThrown, true); //clear routeInProgress MauticVars.routeInProgress = ''; //restore button class if applicable Mautic.stopIconSpinPostEvent(); //stop loading bar Mautic.stopPageLoadingBar(); }, complete: function () { if (typeof callback !== 'undefined') { if (typeof callback == 'function') { callback(); } else { window["Mautic"][callback].apply('window', []); } } Mautic.generatePageTitle( route ); delete Mautic.loadContentXhr[target]; } }); //prevent firing of href link //mQuery(link).attr("href", "javascript: void(0)"); return false; }; /** * Load results with ajax in batch mode * * @param elementName * @param route * @param callback */ Mautic.loadAjaxColumn = function(elementName, route, callback){ var className = '.'+elementName; if (mQuery(className).length) { var ids = []; mQuery(className).each(function () { if(!mQuery(this).text()) { var id = mQuery(this).attr('data-value'); ids.push(id); } }); var batchIds; // If not gonna load any data, then just callback if(ids.length == 0) { Mautic.getCallback(callback); } // Get all stats numbers in batches of 10 while (ids.length > 0) { batchIds = ids.splice(0, 10); Mautic.ajaxActionRequest( route, {ids: batchIds, entityId: Mautic.getEntityId()}, function (response) { if (response.success && response.stats) { for (var i = 0; i < response.stats.length; i++) { var stat = response.stats[i]; if (mQuery('#' + elementName + '-' + stat.id).length) { mQuery('#' + elementName + '-' + stat.id).html(stat.data); } } if(batchIds.length < 10) { Mautic.getCallback(callback); } } }, false, true, "GET" ); } } } /** * Sort table by column * * @param tableId * @param sortElement */ Mautic.sortTableByColumn = function(tableId, sortElement, removeZero){ var tbody = mQuery(tableId).find('tbody'); tbody.find('tr').each(function () { if(parseInt(mQuery(this).find(sortElement).text()) == 0) { mQuery(this).remove(); } }) tbody.find('tr').sort(function(a, b) { var tda = parseFloat(mQuery(a).find(sortElement).text()); // target order attribute var tdb = parseFloat(mQuery(b).find(sortElement).text()); // target order attribute // if a < b return 1 return tda < tdb ? 1 // else if a > b return -1 : tda > tdb ? -1 // else they are equal - return 0 : 0; }).appendTo(tbody); } /** * @param callback */ Mautic.getCallback = function (callback) { if (callback && typeof callback !== 'undefined') { if (typeof callback == 'function') { callback(); } else { window["Mautic"][callback].apply('window', []); } } } /** * Generates the title of the current page * * @param route */ Mautic.generatePageTitle = function(route){ if (-1 !== route.indexOf('timeline')) { return } else if (-1 !== route.indexOf('/view')) { //loading view of module title var currentModule = route.split('/')[3]; //check if we find spans var titleWithHTML = mQuery('.page-header h3').find('span.span-block'); var currentModuleItem = ''; if( 1 < titleWithHTML.length ){ currentModuleItem = titleWithHTML.eq(0).text() + ' - ' + titleWithHTML.eq(1).text(); } else { currentModuleItem = mQuery('.page-header h3').text(); } // Encoded entites are decoded by this process and can cause a XSS currentModuleItem = mQuery('
'+currentModuleItem+'
').text(); mQuery('title').html( currentModule[0].toUpperCase() + currentModule.slice(1) + ' | ' + currentModuleItem + ' | Mautic' ); } else { //loading basic title mQuery('title').html( mQuery('.page-header h3').text() + ' | Mautic' ); } }; /** * Updates new content * @param response */ Mautic.processPageContent = function (response) { if (response) { Mautic.deactivateBackgroup(); if (response.errors && 'dev' == mauticEnv) { alert(response.errors[0].message); console.log(response.errors); } if (!response.target) { response.target = '#app-content'; } //inactive tooltips, etc Mautic.onPageUnload(response.target, response); //set content if (response.newContent) { if (response.replaceContent && response.replaceContent == 'true') { mQuery(response.target).replaceWith(response.newContent); } else { mQuery(response.target).html(response.newContent); } } if (response.notifications) { Mautic.setNotifications(response.notifications); } if (response.route) { //update URL in address bar history.pushState(null, "Mautic", response.route); //update Title Mautic.generatePageTitle( response.route ); } if (response.target == '#app-content') { //update type of content displayed if (response.mauticContent) { mauticContent = response.mauticContent; } if (response.activeLink) { var link = response.activeLink; if (link !== undefined && link.charAt(0) != '#') { link = "#" + link; } var parent = mQuery(link).parent(); //remove current classes from menu items mQuery(".nav-sidebar").find(".active").removeClass("active"); //add current to parent
  • parent.addClass("active"); //get parent var openParent = parent.closest('li.open'); //remove ancestor classes mQuery(".nav-sidebar").find(".open").each(function () { if (!openParent.hasClass('open') || (openParent.hasClass('open') && openParent[0] !== mQuery(this)[0])) { mQuery(this).removeClass('open'); } }); //add current_ancestor classes //mQuery(parent).parentsUntil(".nav-sidebar", "li").addClass("current_ancestor"); } mQuery('body').animate({ scrollTop: 0 }, 0); } else { var overflow = mQuery(response.target).css('overflow'); var overflowY = mQuery(response.target).css('overflowY'); if (overflow == 'auto' || overflow == 'scroll' || overflowY == 'auto' || overflowY == 'scroll') { mQuery(response.target).animate({ scrollTop: 0 }, 0); } } if (response.overlayEnabled) { mQuery(response.overlayTarget + ' .content-overlay').remove(); } //activate content specific stuff Mautic.onPageLoad(response.target, response); } }; /** * Initiate various functions on page load, manual or ajax */ Mautic.onPageLoad = function (container, response, inModal) { Mautic.initDateRangePicker(container + ' #daterange_date_from', container + ' #daterange_date_to'); //initiate links Mautic.makeLinksAlive(mQuery(container + " a[data-toggle='ajax']")); //initialize forms mQuery(container + " form[data-toggle='ajax']").each(function (index) { Mautic.ajaxifyForm(mQuery(this).attr('name')); }); //initialize ajax'd modals Mautic.makeModalsAlive(mQuery(container + " *[data-toggle='ajaxmodal']")) //initialize embedded modal forms Mautic.activateModalEmbeddedForms(container); //initalize live search boxes mQuery(container + " *[data-toggle='livesearch']").each(function (index) { Mautic.activateLiveSearch(mQuery(this), "lastSearchStr", "liveCache"); }); //initialize list filters mQuery(container + " *[data-toggle='listfilter']").each(function (index) { Mautic.activateListFilterSelect(mQuery(this)); }); //initialize tooltips var pageTooltips = mQuery(container + " *[data-toggle='tooltip']"); pageTooltips.tooltip({html: true, container: 'body'}); // Enable tooltips on checkbox & radio input's to // show when hovering their parent LABEL element pageTooltips.each(function(i) { var thisTooltip = mQuery(pageTooltips.get(i)); var elementParent = thisTooltip.parent(); if (elementParent.get(0).tagName === 'LABEL') { elementParent.append(''); elementParent.hover(function () { thisTooltip.tooltip('show') }, function () { thisTooltip.tooltip('hide'); }); } }); //initialize sortable lists mQuery(container + " *[data-toggle='sortablelist']").each(function (index) { Mautic.activateSortable(this); }); //downloads mQuery(container + " a[data-toggle='download']").off('click.download'); mQuery(container + " a[data-toggle='download']").on('click.download', function (event) { event.preventDefault(); Mautic.initiateFileDownload(mQuery(this).attr('href')); }); Mautic.makeConfirmationsAlive(mQuery(container + " a[data-toggle='confirmation']")); //initialize date/time mQuery(container + " *[data-toggle='datetime']").each(function() { Mautic.activateDateTimeInputs(this, 'datetime'); }); mQuery(container + " *[data-toggle='date']").each(function() { Mautic.activateDateTimeInputs(this, 'date'); }); mQuery(container + " *[data-toggle='time']").each(function() { Mautic.activateDateTimeInputs(this, 'time'); }); // Initialize callback options mQuery(container + " *[data-onload-callback]").each(function() { var callback = function(el) { if (typeof window["Mautic"][mQuery(el).attr('data-onload-callback')] == 'function') { window["Mautic"][mQuery(el).attr('data-onload-callback')].apply('window', [el]); } } mQuery(document).ready(callback(this)); }); mQuery(container + " input[data-toggle='color']").each(function() { Mautic.activateColorPicker(this); }); mQuery(container + " select").not('.multiselect, .not-chosen').each(function() { Mautic.activateChosenSelect(this); }); mQuery(container + " select.multiselect").each(function() { Mautic.activateMultiSelect(this); }); Mautic.activateLookupTypeahead(mQuery(container)); // Fix dropdowns in responsive tables - https://github.com/twbs/bootstrap/issues/11037#issuecomment-163746965 mQuery(container + " .table-responsive").on('shown.bs.dropdown', function (e) { var table = mQuery(this), menu = mQuery(e.target).find(".dropdown-menu"), tableOffsetHeight = table.offset().top + table.height(), menuOffsetHeight = menu.offset().top + menu.outerHeight(true); if (menuOffsetHeight > tableOffsetHeight) table.css("padding-bottom", menuOffsetHeight - tableOffsetHeight + 16) }); mQuery(container + " .table-responsive").on("hide.bs.dropdown", function () { mQuery(this).css("padding-bottom", 0); }) //initialize tab/hash activation mQuery(container + " .nav-tabs[data-toggle='tab-hash']").each(function() { // Show tab based on hash var hash = document.location.hash; var prefix = 'tab-'; if (hash) { var hashPieces = hash.split('?'); hash = hashPieces[0].replace("#", "#" + prefix); var activeTab = mQuery(this).find('a[href=' + hash + ']').first(); if (mQuery(activeTab).length) { mQuery('.nav-tabs li').removeClass('active'); mQuery('.tab-pane').removeClass('in active'); mQuery(activeTab).parent().addClass('active'); mQuery(hash).addClass('in active'); } } mQuery(this).find('a').on('shown.bs.tab', function (e) { window.location.hash = e.target.hash.replace("#" + prefix, "#"); }); }); // Initialize tab overflow mQuery(container + " .nav-overflow-tabs ul").each(function() { Mautic.activateOverflowTabs(this); }); mQuery(container + " .nav.sortable").each(function() { Mautic.activateSortableTabs(this); }); // Initialize tab delete buttons Mautic.activateTabDeleteButtons(container); //spin icons on button click mQuery(container + ' .btn:not(.btn-nospin)').on('click.spinningicons', function (event) { Mautic.startIconSpinOnEvent(event); }); mQuery(container + ' input[class=list-checkbox]').on('change', function () { var disabled = Mautic.batchActionPrecheck(container) ? false : true; var color = (disabled) ? 'btn-default' : 'btn-info'; var button = container + ' th.col-actions .input-group-btn button'; mQuery(button).prop('disabled', disabled); mQuery(button).removeClass('btn-default btn-info').addClass(color); }); //Copy form buttons to the toolbar mQuery(container + " .bottom-form-buttons").each(function() { if (inModal || mQuery(this).closest('.modal').length) { var modal = (inModal) ? container : mQuery(this).closest('.modal'); if (mQuery(modal).find('.modal-form-buttons').length) { //hide the bottom buttons mQuery(modal).find('.bottom-form-buttons').addClass('hide'); var buttons = mQuery(modal).find('.bottom-form-buttons').html(); //make sure working with a clean slate mQuery(modal).find('.modal-form-buttons').html(''); mQuery(buttons).filter("button").each(function (i, v) { //get the ID var id = mQuery(this).attr('id'); var button = mQuery("