/** * Smart Links (the plugin's second domain) * * - Settings > Configuration > Smart Links Settings tab: delegated handlers only (the * configuration page is loaded through Ajax). Showing / hiding the value editor by * mode is done by Mautic's own data-show-on switcher. * - Smart Link form: short link preview, "Generate" a random slug, destination URL shown * only for external redirects. Started by Mautic.mt3sMpSmartLinkOnLoad(), which Mautic * calls after it loads any page whose mauticContent is "mt3sMpSmartLink". */ (function ($) { 'use strict'; if (typeof $ === 'undefined') { return; } var NS = '.smartlinks'; /** * Rows are renumbered 0..n-1 in DOM order after every change, so the submitted order is the * displayed order (Symfony collections keep the order of their keys). */ var reindexValues = function ($list) { $list.children('.utm-value-row').each(function (index) { $(this).find('input, select, textarea').each(function () { var $input = $(this); var name = $input.attr('name'); var id = $input.attr('id'); if (name) { $input.attr('name', name.replace(/^(.*\[values\]\[)[^\]]+(\].*)$/, '$1' + index + '$2')); } if (id) { $input.attr('id', id.replace(/^(.*_values_)[^_]+(_.*)$/, '$1' + index + '$2')); } }); }); refreshEditor($list.closest('.utm-values-editor')); }; var refreshEditor = function ($editor) { var count = $editor.find('.utm-value-row').length; $editor.find('.utm-values-empty').toggleClass('hide', count > 0); $editor.find('[data-utm-load-suggestions]').toggleClass('hide', count > 0); }; var getList = function ($editor) { return $editor.find('.utm-values-list').first(); }; var existingValues = function ($editor) { var values = {}; $editor.find('.utm-value-input').each(function () { values[$.trim($(this).val())] = true; }); return values; }; var addValueRow = function ($editor, value, label) { var $list = getList($editor); var index = $list.children('.utm-value-row').length; var $row = $($list.attr('data-prototype').replace(/__value__/g, index)); $list.append($row); if (typeof value !== 'undefined') { $row.find('.utm-value-input').val(value); $row.find('.utm-label-input').val(label || value); } reindexValues($list); return $row; }; /** * Appends rows for the given {value, label} items, skipping values already in the list. */ var appendValues = function ($editor, items) { var existing = existingValues($editor); $.each(items, function (i, item) { if (item.value === '' || existing[item.value]) { return; } existing[item.value] = true; addValueRow($editor, item.value, item.label); }); }; var refreshFieldStates = function ($element) { // Re-runs Mautic's data-show-on switcher (bound to the form's change event). $element.closest('form').trigger('change'); }; $(document) .on('click' + NS, '.smartlinks-utm-config [data-utm-add-value]', function () { var $row = addValueRow($(this).closest('.utm-values-editor')); $row.find('.utm-value-input').trigger('focus'); }) .on('click' + NS, '.smartlinks-utm-config [data-utm-remove-value]', function () { var $list = $(this).closest('.utm-values-list'); $(this).closest('.utm-value-row').remove(); reindexValues($list); }) .on('change' + NS, '.smartlinks-utm-config .utm-value-default', function () { // Only one default value per parameter. if ($(this).is(':checked')) { $(this).closest('.utm-values-list').find('.utm-value-default').not(this).prop('checked', false); } }) .on('mouseenter' + NS, '.smartlinks-utm-config .utm-values-list', function () { var $list = $(this); if ($list.data('utmSortable') || typeof $list.sortable !== 'function') { return; } $list.data('utmSortable', true).sortable({ items: '> .utm-value-row', handle: '.utm-value-handle', axis: 'y', stop: function () { reindexValues($list); } }); }) .on('click' + NS, '.smartlinks-utm-config [data-utm-import]', function () { var $editor = $(this).closest('.utm-values-editor'); var $text = $editor.find('.utm-import-text'); var items = []; $.each(($text.val() || '').split(/\r?\n/), function (i, line) { var parts = line.split(';'); var value = $.trim(parts.shift()); if (value !== '') { items.push({value: value, label: $.trim(parts.join(';')) || value}); } }); appendValues($editor, items); $text.val(''); }) .on('click' + NS, '.smartlinks-utm-config [data-utm-load-suggestions]', function () { appendValues($(this).closest('.utm-values-editor'), JSON.parse($(this).attr('data-utm-suggestions') || '[]')); }) .on('click' + NS, '.smartlinks-utm-config [data-utm-add-parameter]', function () { var $config = $(this).closest('.smartlinks-utm-config'); var $container = $config.find('.utm-custom-parameters'); var index = parseInt($container.attr('data-index'), 10) || 0; var $panel = $($container.attr('data-prototype').replace(/__param__/g, index)); $container.attr('data-index', index + 1).append($panel); $config.find('.utm-custom-empty').addClass('hide'); refreshFieldStates($container); $panel.find('.utm-custom-key').trigger('focus'); }) .on('click' + NS, '.smartlinks-utm-config [data-utm-remove-parameter]', function () { var $config = $(this).closest('.smartlinks-utm-config'); $(this).closest('[data-utm-parameter]').remove(); $config.find('.utm-custom-empty').toggleClass('hide', $config.find('.utm-custom-parameters [data-utm-parameter]').length > 0); }) .on('input' + NS, '.smartlinks-utm-config .utm-custom-key', function () { var key = $.trim($(this).val()); var $code = $(this).closest('[data-utm-parameter]').find('.utm-parameter-key').first(); $code.text(key !== '' ? key : $code.text()); }); // --------------------------------------------------------------------------- // Smart Link form (menu: Smart Links) // --------------------------------------------------------------------------- /** * The short link the user is building: https://{short link domain}/l/{slug}. Spaces are * replaced by the configured character, uppercase turned to lowercase, exactly like the * server does on save (SmartLinkModel::applySlugRules). */ Mautic.smartLinkPreview = function ($wrapper) { var $slugInput = $wrapper.find('.smart-link-slug'); var domain = $.trim($('#smartLinkDomainReminder').attr('data-domain') || ''); var slug = $.trim($slugInput.val() || ''); var replacement = $.trim($('#smartLinkDomainReminder').attr('data-separator') || '-'); slug = slug.replace(/\s+/g, replacement); if (parseInt($('#smartLinkDomainReminder').attr('data-lowercase') || '1', 10)) { slug = slug.toLowerCase(); } $('#smartLinkPreview').text(domain !== '' && slug !== '' ? domain + '/l/' + slug : ''); }; /** * The destination depends on the redirect type: the URL for external redirects, the * landing page or the standalone form for Mautic ones. */ Mautic.smartLinkToggleDestination = function ($wrapper) { var type = $wrapper.find('.smart-link-redirect-type').val(); $('#smartLinkDestinationUrlWrapper').toggleClass('hide', type === 'mautic'); $('#smartLinkMauticDestinationWrapper').toggleClass('hide', type !== 'mautic'); }; Mautic.mt3sMpSmartLinkOnLoad = function (container) { var $wrapper = $(container).find('.smart-link-slug-group').first(); if ($wrapper.length) { Mautic.smartLinkPreview($wrapper); // The redirect type lives outside the slug group: scope the toggle to the form. Mautic.smartLinkToggleDestination($wrapper.closest('form')); } }; $(document) .on('input', '.smart-link-slug', function () { Mautic.smartLinkPreview($(this).closest('.smart-link-slug-group')); }) .on('change', '.smart-link-redirect-type', function () { Mautic.smartLinkToggleDestination($(this).closest('form')); }) .on('click', '#smartLinkGenerateSlug', function () { var $button = $(this); var generated = $.trim($button.attr('data-generated') || ''); var $slugInput = $button.closest('.smart-link-slug-group').find('.smart-link-slug'); if (generated !== '') { $slugInput.val(generated).trigger('input'); // The next click proposes a different slug; the page isn't reloaded between clicks. $button.attr('data-generated', Mautic.smartLinkRandomSlug()); } }); /** * Random slug of 4 letters and digits for the "Generate" button (client-side fallback; the * server proposes one on each page load and the validator checks uniqueness on submit). */ Mautic.smartLinkRandomSlug = function () { var alphabet = 'abcdefghijklmnopqrstuvwxyz0123456789'; var slug = ''; for (var i = 0; i < 4; i++) { slug += alphabet.charAt(Math.floor(Math.random() * alphabet.length)); } return slug; }; })(window.mQuery);