217 lines
8.9 KiB
JavaScript
217 lines
8.9 KiB
JavaScript
/**
|
|
* 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.planOnLoad(), which Mautic calls after
|
|
* it loads any page whose mauticContent is "plan", including pages loaded through Ajax.
|
|
* - Settings > Configuration > Plan: adding and removing item types (delegated handlers,
|
|
* because that page isn't a "plan" page).
|
|
*/
|
|
Mautic.planOnLoad = function (container) {
|
|
mQuery(container).find('.plan-collection').each(function () {
|
|
Mautic.planInitCollection(mQuery(this));
|
|
});
|
|
mQuery(container).find('.plan-tabs').each(function () {
|
|
Mautic.planInitTabs(mQuery(this), mQuery(container));
|
|
});
|
|
};
|
|
|
|
/**
|
|
* Plan tabs (detail page and form):
|
|
* - the tab last used for a plan is remembered for the browser session, and reopened when the plan'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.planInitTabs = function ($tabs, $container) {
|
|
var planId = $tabs.attr('data-plan-id');
|
|
var storageKey = planId ? 'mautic.plan.tab.' + planId : 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('.plan-tab-error').remove();
|
|
if (hasError) {
|
|
$link.append(' <i class="ri-error-warning-line text-danger plan-tab-error"></i>');
|
|
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.planPendingTab = $target;
|
|
setTimeout(Mautic.planApplyPendingTab, 0);
|
|
setTimeout(function () {
|
|
if (Mautic.planPendingTab === $target) {
|
|
Mautic.planPendingTab = null;
|
|
}
|
|
}, 1000);
|
|
}
|
|
};
|
|
|
|
Mautic.planPendingTab = null;
|
|
|
|
Mautic.planApplyPendingTab = function () {
|
|
var $target = Mautic.planPendingTab;
|
|
if ($target && mQuery.contains(document, $target[0])) {
|
|
$target.tab('show');
|
|
}
|
|
};
|
|
|
|
// Run after core's tab restore, so that the plan's choice wins (see Mautic.planInitTabs).
|
|
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:
|
|
* .plan-collection[data-prototype][data-prototype-name]
|
|
* .plan-collection-empty, .plan-collection-list > .plan-collection-row, .plan-collection-add
|
|
* in each row: .plan-collection-handle (drag), .plan-collection-remove, input.plan-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.planInitCollection = function ($editor) {
|
|
if ($editor.data('planInitialized')) {
|
|
return;
|
|
}
|
|
$editor.data('planInitialized', true);
|
|
|
|
var $list = $editor.find('.plan-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('.plan-collection-row').length;
|
|
|
|
var refresh = function () {
|
|
var $rows = $list.children('.plan-collection-row');
|
|
$rows.each(function (index) {
|
|
mQuery(this).find('input.plan-row-position').val(index);
|
|
});
|
|
$editor.find('.plan-collection-empty').first().toggleClass('hide', $rows.length > 0);
|
|
};
|
|
|
|
$editor.on('click', '.plan-collection-add', function (event) {
|
|
event.preventDefault();
|
|
var $row = mQuery($editor.attr('data-prototype').replace(prototypeName, nextIndex++));
|
|
$list.append($row);
|
|
refresh();
|
|
$row.find('select, input[type="text"], textarea').first().trigger('focus');
|
|
});
|
|
|
|
$editor.on('click', '.plan-collection-remove', function (event) {
|
|
event.preventDefault();
|
|
mQuery(this).closest('.plan-collection-row').remove();
|
|
refresh();
|
|
});
|
|
|
|
// Typed items (description, allocation): show the selected type's description under the dropdown, and
|
|
// its pictogram next to it.
|
|
$editor.on('change', 'select.plan-item-type', function () {
|
|
var $option = mQuery(this).find('option:selected');
|
|
var $row = mQuery(this).closest('.plan-collection-row');
|
|
$row.find('.plan-item-type-help').first().text($option.attr('data-description') || '');
|
|
$row.find('.plan-item-type-icon').first().attr('class', 'plan-item-type-icon ' + ($option.attr('data-icon') || ''));
|
|
});
|
|
|
|
if (typeof $list.sortable === 'function') {
|
|
$list.sortable({
|
|
items: '> .plan-collection-row',
|
|
handle: '.plan-collection-handle',
|
|
axis: 'y',
|
|
update: refresh
|
|
});
|
|
}
|
|
|
|
refresh();
|
|
};
|
|
|
|
/**
|
|
* Settings > Configuration > Plan: add and remove item types (description and allocation).
|
|
*/
|
|
(function () {
|
|
var refreshTypes = function ($editor) {
|
|
var count = $editor.find('.plan-config-type').length;
|
|
$editor.find('.plan-config-types-empty').toggleClass('hide', count > 0);
|
|
};
|
|
|
|
mQuery(document).on('click', '.plan-config-types .plan-config-type-add', function (event) {
|
|
event.preventDefault();
|
|
var $editor = mQuery(this).closest('.plan-config-types');
|
|
// Never reuse an index: count every index ever added on this page.
|
|
var index = ($editor.data('planNextIndex') || $editor.find('.plan-config-type').length + 1000);
|
|
$editor.data('planNextIndex', index + 1);
|
|
var $row = mQuery($editor.attr('data-prototype').replace(/__type__/g, index));
|
|
$editor.find('.plan-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', '.plan-config-types select.plan-type-icon-select', function () {
|
|
var icon = mQuery(this).find('option:selected').attr('data-icon') || '';
|
|
mQuery(this).closest('.plan-config-type').find('.plan-type-icon-preview').first().attr('class', 'plan-type-icon-preview ' + icon);
|
|
});
|
|
|
|
mQuery(document).on('click', '.plan-config-types .plan-config-type-remove', function (event) {
|
|
event.preventDefault();
|
|
var $editor = mQuery(this).closest('.plan-config-types');
|
|
mQuery(this).closest('.plan-config-type').remove();
|
|
refreshTypes($editor);
|
|
});
|
|
})();
|