silverstripe-cms/client/src/legacy/CMSMain.js

130 lines
3.4 KiB
JavaScript
Raw Normal View History

import $ from 'jquery';
/**
* Behaviour for the CMS Content Toolbar.
* Applies to tools on top-level views i.e. '/admin/pages' and '/admin/assets' and
* their corresponding tools in the SiteTree panel.
* An example is 'bulk actions' on the Pages view.
*/
$.entwine('ss', function ($) {
// Faux three column layout
$('.cms-content-header-info').entwine({
'from .cms-panel': {
// Keep the header info's width synced with the TreeView panel's width.
ontoggle: function (e) {
var $treeViewPanel = this.closest('.cms-content').find(e.target);
if ($treeViewPanel.length === 0) {
return;
}
this.parent()[$treeViewPanel.hasClass('collapsed') ? 'addClass' : 'removeClass']('collapsed');
}
}
});
// Customise tree / list view pjax tab loading
// See $('.cms .cms-panel-link') in LeftAndMain.js
$('.cms .cms-panel-link.page-view-link').entwine({
onclick: function(e){
// Toggle 'active' flag
this.siblings().removeClass('active');
this.addClass('active');
// Toggle 'view' parameter in search form to keep same view type
var viewField = $(".cms-content-filters input[type='hidden'][name='view']");
viewField.val($(this).data('view'));
// Does pjax load
return this._super(e);
}
});
$('.cms-content-toolbar').entwine({
onmatch: function () {
var self = this;
this._super();
// Initialise the buttons
$.each(this.find('.cms-actions-buttons-row .tool-button'), function () {
var $button = $(this),
toolId = $button.data('toolid'),
isActive = $button.hasClass('active');
// We don't care about tools that don't have a related 'action'.
if (toolId !== void 0) {
// Set the tool to its closed state.
$button.data('active', false).removeClass('active');
$('#' + toolId).hide();
self.bindActionButtonEvents($button);
}
});
},
onunmatch: function () {
var self = this;
this._super();
$.each(this.find('.cms-actions-buttons-row .tool-button'), function () {
var $button = $(this);
self.unbindActionButtonEvents($button);
});
},
/**
* @func bindActionButtonEvents
* @param {object} $button
* @desc Add event handlers in the '.cmsContentToolbar' namespace.
*/
bindActionButtonEvents: function ($button) {
var self = this;
$button.on('click.cmsContentToolbar', function (e) {
self.showHideTool($button);
});
},
/**
* @func unbindActionButtonEvents
* @param {object} $button
* @desc Remove all event handlers in the '.cmsContentToolbar' namespace.
*/
unbindActionButtonEvents: function ($button) {
$button.off('.cmsContentToolbar');
},
/**
* @func showTool
* @param {object} $button
* @desc Show a tool in the tools row. Hides all other tools.
*/
showHideTool: function ($button) {
var isActive = $button.data('active'),
toolId = $button.data('toolid'),
$action = $('#' + toolId);
// Hide all tools except the one passed as a param,
// which gets handled separately.
$.each(this.find('.cms-actions-buttons-row .tool-button'), function () {
var $currentButton = $(this),
$currentAction = $('#' + $currentButton.data('toolid'));
if ($currentButton.data('toolid') !== toolId) {
$currentAction.hide();
$currentButton.data('active', false);
}
});
$button[isActive ? 'removeClass' : 'addClass']('active');
$action[isActive ? 'hide' : 'show']();
$button.data('active', !isActive);
}
});
});