Skip to content

Commit

Permalink
Fixed alias plugin preventing operations on the original plugin
Browse files Browse the repository at this point in the history
When alias plugin is initialised it also initialises it's children.
If the alias plugin is on the same page as the plugin that is being
aliased this leads to the event handlers bound twice. So if you
try to toggle context menu or nested plugins in structure mode the
operation would be ran twice and would technically fail. This
commit stops initialisation of the plugin that already has been
initialised.

Essentially this is a backport of changes from django-cms#5429
Ref: django-cms#5382
  • Loading branch information
vxsx committed Jul 4, 2016
1 parent 6957537 commit 17214e1
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 4 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.txt
Expand Up @@ -34,6 +34,8 @@
page tree dropdowns.
* Fixed a bug where Django Compressor could not be used on the sekizai ``js``
block.
* Fixed an issue when plugin that is aliased on the same page couldn't be
operated upon in the structure mode.


=== 3.3.0 (2016-05-26) ===
Expand Down
8 changes: 4 additions & 4 deletions cms/static/cms/js/dist/bundle.toolbar.min.js

Large diffs are not rendered by default.

6 changes: 6 additions & 0 deletions cms/static/cms/js/modules/cms.plugins.js
Expand Up @@ -67,6 +67,9 @@ var Plugin = new Class({

// bind data element to the container
this.ui.container.data('settings', this.options);
if (Plugin.aliasPluginDuplicatesMap[this.options.plugin_id]) {
return;
}
Plugin._initializeDragItemsStates();

// determine type of plugin
Expand All @@ -76,6 +79,7 @@ var Plugin = new Class({
this._collapsables();
break;
case 'plugin': // handler for all plugins
Plugin.aliasPluginDuplicatesMap[this.options.plugin_id] = true;
this._setPlugin();
this._collapsables();
break;
Expand Down Expand Up @@ -1645,6 +1649,8 @@ Plugin._initializeDragItemsStates = Helpers.once(function _initializeDragItemsSt
});
});

Plugin.aliasPluginDuplicatesMap = {};

// shorthand for jQuery(document).ready();
$(Plugin._initializeGlobalHandlers);

Expand Down
31 changes: 31 additions & 0 deletions cms/tests/frontend/unit/cms.plugins.test.js
Expand Up @@ -39,6 +39,10 @@ describe('CMS.Plugin', function () {
});
});

afterEach(function () {
Plugin.aliasPluginDuplicatesMap = {};
});

describe('instance', function () {
var plugin1;
var plugin2;
Expand Down Expand Up @@ -244,8 +248,35 @@ describe('CMS.Plugin', function () {
expect(generic.ui.container.data('settings')).toEqual(generic.options);
});

it('doesnt reset the ui if the same plugin is initialized twice (alias case)', function () {
spyOn(Plugin.prototype, '_setPlugin');
spyOn(Plugin.prototype, '_setPlaceholder');
spyOn(Plugin.prototype, '_setGeneric');
expect(Plugin.aliasPluginDuplicatesMap[plugin1.options.plugin_id]).toEqual(true);

new CMS.Plugin('cms-plugin-1', {
type: 'plugin',
plugin_id: 1,
plugin_type: 'TextPlugin',
placeholder_id: 1,
urls: {
add_plugin: '/en/admin/cms/page/add-plugin/',
edit_plugin: '/en/admin/cms/page/edit-plugin/1/',
move_plugin: '/en/admin/cms/page/move-plugin/',
delete_plugin: '/en/admin/cms/page/delete-plugin/1/',
copy_plugin: '/en/admin/cms/page/copy-plugins/'
}
});

expect(Plugin.aliasPluginDuplicatesMap[plugin1.options.plugin_id]).toEqual(true);
expect(Plugin.prototype._setPlugin).not.toHaveBeenCalled();
expect(Plugin.prototype._setPlaceholder).not.toHaveBeenCalled();
expect(Plugin.prototype._setGeneric).not.toHaveBeenCalled();
});

it('checks if pasting into this plugin is allowed', function () {
spyOn(CMS.Plugin.prototype, '_checkIfPasteAllowed');
Plugin.aliasPluginDuplicatesMap = {};

plugin1 = new CMS.Plugin('cms-plugin-1', {
type: 'plugin',
Expand Down

0 comments on commit 17214e1

Please sign in to comment.