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

173 lines
4.7 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 ($) {
2017-12-06 05:06:15 +01:00
const VIEW_TYPE_TREE = 'treeview';
const VIEW_TYPE_LIST = 'listview';
// 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');
}
}
});
2017-12-06 05:06:15 +01:00
/**
* Override super's `onadd` to modify url base the previously select view
* type.
*
* See $('.cms .cms-panel-link') in LeftAndMain.js
*/
$('.cms-panel-deferred.cms-content-view').entwine({
onadd: function() {
2017-12-07 04:24:01 +01:00
if(this.data('no-ajax')) {
return;
}
var viewType = localStorage.getItem('ss.pages-view-type') || VIEW_TYPE_TREE;
if(this.closest('.cms-content-tools').length > 0) {
// Always use treeview when in page edit mode
viewType = VIEW_TYPE_TREE;
}
2017-12-06 05:06:15 +01:00
const url = this.data(`url-${viewType}`);
this.data('deferredNoCache', viewType === VIEW_TYPE_LIST);
this.data('url', url);
this._super();
}
});
// Customise tree / list view pjax tab loading
// See $('.cms .cms-panel-link') in LeftAndMain.js
2017-12-06 05:06:15 +01:00
$('.cms .page-view-link').entwine({
onclick: function(e){
2017-12-06 05:06:15 +01:00
e.preventDefault();
const viewType = $(this).data('view');
const $contentView = this.closest('.cms-content-view');
var url = $contentView.data(`url-${viewType}`);
const isContentViewInSidebar = $contentView.closest('.cms-content-tools').length !== 0;
2017-12-06 05:06:15 +01:00
localStorage.setItem('ss.pages-view-type', viewType);
if(isContentViewInSidebar && viewType === VIEW_TYPE_LIST) {
2017-12-07 04:24:01 +01:00
window.location = $contentView.data('url-listviewroot');
2017-12-06 05:06:15 +01:00
return;
}
2017-12-06 05:06:15 +01:00
$contentView.data('url', url);
$contentView.redraw();
}
});
2017-12-07 04:24:01 +01:00
$('.cms .cms-clear-filter').entwine({
onclick: function(e){
e.preventDefault();
const viewType = localStorage.getItem('ss.pages-view-type');
const $contentView = this.closest('.cms-content-view');
var url = $contentView.data(`url-${viewType}`);
$contentView.data('url', url);
$contentView.redraw();
}
});
$('.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);
}
});
});