/** * Plan plugin scripts. * * - Plan form: editable, sortable rows (objectives, description items, allocation items), the selected * item type's description, and tab error markers. Started by Mautic.mt3sMpProgramOnLoad(), which Mautic calls after * it loads any page whose mauticContent is "mt3sMpProgram", including pages loaded through Ajax. * - Task form (mauticContent "mt3sMpProgramTask", started by Mautic.mt3sMpProgramTaskOnLoad()): the dependencies and * the RACI rows work like the program form's collections; the selected kind enslaves a milestone's * end date; the selected type's description and pictogram sit next to the type dropdown. * - Audience form (mauticContent "mt3sMpProgramAudience", started by Mautic.mt3sMpProgramAudienceOnLoad()): the selected * type's pictogram next to the type dropdown (fixed per type, not admin-configured). * - Settings > Configuration > Plan: adding and removing item types (delegated handlers, * because that page isn't a "program" page). */ Mautic.mt3sMpProgramAudienceOnLoad = function (container) { // The type dropdown's pictogram is fixed per type (AudienceType), not admin-configured: it is // rendered from the value server-side (the mt3s_marketing_program_audience_type_icon filter), and updated here. mQuery(container).find('select[name$="[audienceType]"]').each(function () { var $select = mQuery(this); if ($select.data('mt3sMpProgramAudienceInitialized')) { return; } $select.data('mt3sMpProgramAudienceInitialized', true); // The same icons as AudienceType::getIcon(). var icons = { persona: 'ri-user-heart-line', icp: 'ri-building-line', broad_audience: 'ri-group-line', lifecycle_cohort: 'ri-time-line', partner_influencer: 'ri-hand-heart-line' }; $select.on('change', function () { var icon = icons[mQuery(this).val()] || 'ri-user-heart-line'; mQuery(this).closest('form').find('.program-item-type-icon').first() .attr('class', 'program-item-type-icon ' + icon); }); }); }; Mautic.mt3sMpProgramTaskOnLoad = function (container) { // The dependencies and RACI rows work like the program form's collections. mQuery(container).find('.program-collection').each(function () { Mautic.mt3sMpProgramInitCollection(mQuery(this)); }); // The type dropdown works the same way as in the program form. mQuery(container).find('select.program-item-type').each(function () { var $select = mQuery(this); if ($select.data('mt3sMpProgramTaskInitialized')) { return; } $select.data('mt3sMpProgramTaskInitialized', true); $select.on('change', function () { var $option = $select.find('option:selected'); var $scope = $select.closest('form'); $scope.find('.program-item-type-help').first().text($option.attr('data-description') || ''); $scope.find('.program-item-type-icon').first().attr('class', 'program-item-type-icon ' + ($option.attr('data-icon') || '')); }); }); // Kind rules: a milestone's end date is enslaved to its start date (copied and read-only); // a phase has no status of its own (the field is left out server-side, nothing to do here). mQuery(container).find('select[name$="[taskKind]"]').each(function () { var $kind = mQuery(this); if ($kind.data('planKindInitialized')) { return; } $kind.data('planKindInitialized', true); var enslave = function () { var $form = mQuery(this).closest('form'); var $start = $form.find('input[name$="[startDate]"]').first(); var $end = $form.find('input[name$="[endDate]"]').first(); var milestone = 'milestone' === mQuery(this).val(); $end.prop('disabled', milestone); if (milestone) { $end.val($start.val()); // While the milestone is selected, the end follows the start. $start.off('change.planKind').on('change.planKind', function () { $end.val($start.val()); }); } else { $start.off('change.planKind'); } }; $kind.on('change', enslave); enslave.call($kind.get(0)); }); }; Mautic.mt3sMpProgramOnLoad = function (container) { mQuery(container).find('.program-collection').each(function () { Mautic.mt3sMpProgramInitCollection(mQuery(this)); }); mQuery(container).find('.program-tabs').each(function () { Mautic.mt3sMpProgramInitTabs(mQuery(this), mQuery(container)); }); // Detail page: the four tab tables (description items, objectives, allocation items, tasks). mQuery(container).find('table[data-program-tab-table]').each(function () { Mautic.mt3sMpProgramInitTabTable(mQuery(this)); }); // Detail page: long description item texts, clamped to 10 lines with a read-more toggle. mQuery(container).find('.program-read-more').each(function () { Mautic.mt3sMpProgramInitReadMore(mQuery(this)); }); }; /** * A text clamped to a few lines with a "Read more" / "Show less" toggle. The clamping is done by CSS * (line-clamp, see the include template); a toggle appears only when the text really overflows, because * a button under a short text would only state the obvious. * * The overflow is measured when the tab pane is visible: a tab pane that is hidden on page load * (Mautic reopens the tab last used) has no layout, so its scrollHeight is 0. The wrapper is rechecked * on every tab activation until the measurement could run. */ Mautic.mt3sMpProgramInitReadMore = function ($wrapper) { if ($wrapper.data('planReadMoreInitialized')) { return; } $wrapper.data('planReadMoreInitialized', true); var $text = $wrapper.find('.program-read-more-text').first(); var $toggle = $wrapper.find('.program-read-more-toggle').first(); var clampLines = 10; var measure = function () { if (!$text[0] || !$wrapper.is(':visible')) { return false; // Still hidden: try again on the next tab activation. } var lineHeight = parseFloat($text.css('line-height')) || 20; var clampHeight = clampLines * lineHeight; if ($text[0].scrollHeight > clampHeight + 2) { $toggle.show(); } return true; }; $toggle.on('click', function () { var open = 'false' === $toggle.attr('aria-expanded'); $toggle.attr('aria-expanded', open ? 'true' : 'false'); $text.css('-webkit-line-clamp', open ? 'none' : clampLines); // The texts come from the button's data attributes (rendered translated by Twig): the JS // translation catalog only holds core strings, not the plugin's. $toggle.html( (open ? $toggle.attr('data-less-text') : $toggle.attr('data-more-text')) + ' ' ); }); if (!measure()) { // The pane holding this text is hidden: recheck each time a tab of this page is shown. mQuery(document).on('shown.bs.tab.program-read-more', function () { if (measure()) { mQuery(document).off('shown.bs.tab.program-read-more'); } }); } }; /** * A program detail page tab table: one filter box filtering every column (case-insensitive substring), * and every column header sorting the rows asc/desc on click. Filtering and sorting are done on the * client, instantly: these tables hold one plan's items, never many. * * Rows keep their order attribute, so the drag handles and the tree indentation of tasks stay right. * Sorting the tree by another column can interleave a sub-task before its parent; the depth marker * stays attached to its own row, so it just indents wherever it lands. */ Mautic.mt3sMpProgramInitTabTable = function ($table) { if ($table.data('planTabTableInitialized')) { return; } $table.data('planTabTableInitialized', true); var $panes = $table.closest('.tab-pane'); var $filterBox = $panes.find('.program-tab-table-filter').first(); var $input = $filterBox.find('.program-tab-table-filter-input'); var $noMatch = $panes.find('.program-tab-table-nomatch').first(); var $rows = $table.find('tbody tr'); var apply = function () { var needle = mQuery.trim($input.val() || '').toLowerCase(); var visible = 0; $rows.each(function () { var text = mQuery(this).text().toLowerCase().replace(/\s+/g, ' '); var show = '' === needle || -1 !== text.indexOf(needle); mQuery(this).toggleClass('hide', !show); if (show) { visible++; } }); $noMatch.toggleClass('hide', visible > 0); }; $input.on('input', apply); $input.on('change', apply); // Click-to-sort: first click ascending, then toggling. The footer row (allocation total) stays put. $table.find('thead th').each(function (index) { var $th = mQuery(this); $th.css('cursor', 'pointer').attr('title', $table.attr('data-sort-hint') || ''); $th.on('click', function () { var direction = $th.hasClass('program-sorted-asc') ? -1 : 1; var cells = []; $rows.each(function () { cells.push({ text: mQuery(this).find('td').eq(index).text().replace(/\s+/g, ' ').trim(), $row: mQuery(this) }); }); cells.sort(function (a, b) { var va = a.text.toLowerCase(); var vb = b.text.toLowerCase(); var result; // Amounts like "18,500.00 EUR" and numbers sort numerically when both hold one. var na = Mautic.planParseNumber(va); var nb = Mautic.planParseNumber(vb); if (null !== na && null !== nb) { result = na - nb; } else { result = va < vb ? -1 : (va > vb ? 1 : 0); } return result * direction; }); mQuery.each(cells, function () { this.$row.appendTo($table.find('tbody')); }); $table.find('thead th').removeClass('program-sorted-asc program-sorted-desc'); $th.addClass(direction > 0 ? 'program-sorted-asc' : 'program-sorted-desc'); $rows = $table.find('tbody tr'); }); }); }; /** * A cell "18,500.00 EUR" or "12" as a number for the tab table sort, or null when it holds no number * (text, dates as words). The last number in the cell wins, so a name like "Q1 2027" sorts by 2027. */ Mautic.planParseNumber = function (text) { var match = String(text).match(/-?\d[\d,\s]*\.?\d*/g); if (!match) { return null; } var value = parseFloat(match[match.length - 1].replace(/[\s,]/g, '')); return isNaN(value) ? null : value; }; /** * Plan tabs (detail page and form): * - the tab last used for a plan is remembered for the browser session, and reopened when the program's * detail page or form loads, so that Edit, Save and Cancel keep the same tab; * - after a failed save, the tabs whose pane has an error get an icon, and the first of them is * opened instead. * * Core also reopens the last tab used on a page (Mautic.rememberActiveTabs in 12.tabs.js), on document * ready and after every Ajax request, so the order with this code varies (full page load or Ajax). The * chosen tab is therefore applied twice: in a deferred call, and right after core's restore (see the * wrapper below). It stays pending for a second only, so that a later background request can't switch * the tab under the user. The plan's tab panes have no "fade" class: with fade, two tab('show') calls * within the 150 ms transition left two panes visible. */ Mautic.mt3sMpProgramInitTabs = function ($tabs, $container) { var programId = $tabs.attr('data-program-id'); var storageKey = programId ? 'mt3s.marketing_program.tab.' + programId : null; var $target = null; $tabs.find('a[data-toggle="tab"]').each(function () { var $link = mQuery(this); var hasError = Mautic.planPaneHasError($container.find($link.attr('href'))); $link.toggleClass('text-danger', hasError); $link.find('.program-tab-error').remove(); if (hasError) { $link.append(' '); if ($target === null) { $target = $link; } } }); if ($target === null && storageKey) { try { var remembered = window.sessionStorage.getItem(storageKey); if (remembered) { var $remembered = $tabs.find('a[data-toggle="tab"][href="' + remembered + '"]'); $target = $remembered.length ? $remembered : null; } } catch (e) { // sessionStorage unavailable: keep the default tab. } } if (storageKey) { $tabs.find('a[data-toggle="tab"]').on('shown.bs.tab', function (event) { try { window.sessionStorage.setItem(storageKey, mQuery(event.target).attr('href')); } catch (e) { // sessionStorage unavailable: nothing to remember. } }); } if ($target !== null) { Mautic.programPendingTab = $target; setTimeout(Mautic.planApplyPendingTab, 0); setTimeout(function () { if (Mautic.programPendingTab === $target) { Mautic.programPendingTab = null; } }, 1000); } }; Mautic.programPendingTab = null; Mautic.planApplyPendingTab = function () { var $target = Mautic.programPendingTab; if ($target && mQuery.contains(document, $target[0])) { $target.tab('show'); } }; // Run after core's tab restore, so that the program's choice wins (see Mautic.mt3sMpProgramInitTabs). if (typeof Mautic.rememberActiveTabs === 'function' && !Mautic.rememberActiveTabs.planWrapped) { (function (coreRememberActiveTabs) { Mautic.rememberActiveTabs = function () { var result = coreRememberActiveTabs.apply(this, arguments); Mautic.planApplyPendingTab(); return result; }; Mautic.rememberActiveTabs.planWrapped = true; })(Mautic.rememberActiveTabs); } /** * True when a tab pane holds a real validation error, that is an error message as rendered by Mautic's * form_errors block: div.help-block > .alert-danger (one error) or li.alert-danger (several). * * The "has-error" class isn't used: Mautic's checkbox_row theme adds it to every checkbox (it tests * "errors|length is not empty", and 0 isn't empty in Twig), so each objective's "Achieved" checkbox * would mark the Objectives tab. */ Mautic.planPaneHasError = function ($pane) { return $pane.find('.help-block .alert-danger').filter(function () { return mQuery.trim(mQuery(this).text()) !== ''; }).length > 0; }; /** * An editable list of rows built from a Symfony collection: * .program-collection[data-prototype][data-prototype-name] * .program-collection-empty, .program-collection-list > .program-collection-row, .program-collection-add * in each row: .program-collection-handle (drag), .program-collection-remove, input.program-row-position * * The hidden position of every row is rewritten to its display index after each change, so the saved * order is the displayed order. */ Mautic.mt3sMpProgramInitCollection = function ($editor) { if ($editor.data('mt3sMpProgramInitialized')) { return; } $editor.data('mt3sMpProgramInitialized', true); var $list = $editor.find('.program-collection-list').first(); var prototypeName = new RegExp($editor.attr('data-prototype-name'), 'g'); // New rows get indexes that were never used on this page, so they can't collide with existing ones. var nextIndex = $list.children('.program-collection-row').length; var refresh = function () { var $rows = $list.children('.program-collection-row'); $rows.each(function (index) { mQuery(this).find('input.program-row-position').val(index); }); $editor.find('.program-collection-empty').first().toggleClass('hide', $rows.length > 0); }; $editor.on('click', '.program-collection-add', function (event) { event.preventDefault(); var $row = mQuery($editor.attr('data-prototype').replace(prototypeName, nextIndex++)); $list.append($row); refresh(); // A new row may contain nested collections (a call script outcome's options): give them // their own add/remove/drag handler. $row.find('.program-collection').each(function () { Mautic.mt3sMpProgramInitCollection(mQuery(this)); }); if (typeof Mautic.mt3sMpCallScriptInitOutcomeTypes === 'function') { Mautic.mt3sMpCallScriptInitOutcomeTypes($row); } $row.find('select, input[type="text"], textarea').first().trigger('focus'); }); $editor.on('click', '.program-collection-remove', function (event) { event.preventDefault(); mQuery(this).closest('.program-collection-row').remove(); refresh(); }); // Typed items (description, allocation): show the selected type's description under the dropdown, and // its pictogram next to it. Works within a program-collection row. $editor.on('change', 'select.program-item-type', function () { var $option = mQuery(this).find('option:selected'); var $row = mQuery(this).closest('.program-collection-row'); $row.find('.program-item-type-help').first().text($option.attr('data-description') || ''); $row.find('.program-item-type-icon').first().attr('class', 'program-item-type-icon ' + ($option.attr('data-icon') || '')); }); if (typeof $list.sortable === 'function') { $list.sortable({ items: '> .program-collection-row', handle: '.program-collection-handle', axis: 'y', update: refresh }); } refresh(); }; /** * Call Script form: show the options of an outcome only for the choice types (checkbox, radio, * dropdown); a free text outcome has none. */ Mautic.mt3sMpCallScriptInitOutcomeTypes = function ($container) { mQuery($container).find('select.program-outcome-type').each(function () { var $select = mQuery(this); if ($select.data('mt3sMpCallScriptTypeInitialized')) { return; } $select.data('mt3sMpCallScriptTypeInitialized', true); var toggle = function () { var $row = $select.closest('.program-collection-row'); $row.find('.program-outcome-options').first().toggleClass('hide', 'free_text' === $select.val()); }; $select.on('change', toggle); toggle(); }); }; /** * The Call Script form (Components > Call Scripts): its outcomes and their nested options use the * program collections, and the options block follows the chosen data type. */ Mautic.mt3sMpCallScriptOnLoad = function (container) { mQuery(container).find('.program-collection').each(function () { Mautic.mt3sMpProgramInitCollection(mQuery(this)); }); Mautic.mt3sMpCallScriptInitOutcomeTypes(mQuery(container)); }; /** * Settings > Configuration > Plan: add and remove item types (description and allocation). */ (function () { var refreshTypes = function ($editor) { var count = $editor.find('.program-config-type').length; $editor.find('.program-config-types-empty').toggleClass('hide', count > 0); }; mQuery(document).on('click', '.program-config-types .program-config-type-add', function (event) { event.preventDefault(); var $editor = mQuery(this).closest('.program-config-types'); // Never reuse an index: count every index ever added on this page. var index = ($editor.data('programNextIndex') || $editor.find('.program-config-type').length + 1000); $editor.data('programNextIndex', index + 1); var $row = mQuery($editor.attr('data-prototype').replace(/__type__/g, index)); $editor.find('.program-config-types-list').append($row); refreshTypes($editor); $row.find('input[type="text"]').first().trigger('focus'); }); // Pictogram of an item type (description, allocation): preview the chosen icon. mQuery(document).on('change', '.program-config-types select.program-type-icon-select', function () { var icon = mQuery(this).find('option:selected').attr('data-icon') || ''; mQuery(this).closest('.program-config-type').find('.program-type-icon-preview').first().attr('class', 'program-type-icon-preview ' + icon); }); mQuery(document).on('click', '.program-config-types .program-config-type-remove', function (event) { event.preventDefault(); var $editor = mQuery(this).closest('.program-config-types'); mQuery(this).closest('.program-config-type').remove(); refreshTypes($editor); }); })();