Skip to content

Commit

Permalink
Fixed a bug with not enabling plugins that are not rendered in content
Browse files Browse the repository at this point in the history
  • Loading branch information
vxsx committed Jun 21, 2018
1 parent 37d2683 commit 9e1cc32
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 9 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.txt
Expand Up @@ -13,6 +13,8 @@
* Fixed a bug where xframe options were processed by clickjacking middleware
when page was served from cache, rather then get this value from cache
* Fixed a bug where cached page permissions overrides global permissions
* Fixed a bug where plugins that are not rendered in content wouldn't be
editable in structure board


=== 3.5.2 (2018-04-11) ===
Expand Down
2 changes: 1 addition & 1 deletion cms/static/cms/js/dist/3.5.2/bundle.toolbar.min.js

Large diffs are not rendered by default.

56 changes: 48 additions & 8 deletions cms/static/cms/js/modules/cms.structureboard.js
Expand Up @@ -379,18 +379,16 @@ class StructureBoard {
}

_loadStructure() {
var that = this;

// case when structure mode is already loaded
if (CMS.config.settings.mode === 'structure' || this._loadedStructure) {
return Promise.resolve();
}

showLoader();
return that
return this
._requestMode('structure')
.done(function(contentMarkup) {
that._requeststructure = null;
.done(contentMarkup => {
this._requeststructure = null;
hideLoader();

CMS.settings.states = Helpers.getSettings().states;
Expand All @@ -405,6 +403,15 @@ class StructureBoard {

return elem.is('[type="text/cms-template"]'); // cms scripts
});
const pluginIds = this.getIds(body.find('.cms-draggable'));
const pluginDataSource = body.filter('script[data-cms]').toArray()
.map(script => script.textContent || '').join();
const pluginData = StructureBoard._getPluginDataFromMarkup(
pluginDataSource,
pluginIds
);

Plugin._updateRegistry(pluginData.map(([, data]) => data));

CMS.API.Toolbar._refreshMarkup(toolbar);

Expand All @@ -426,11 +433,11 @@ class StructureBoard {
}
});

that.ui.sortables = $('.cms-draggables');
that._drag();
this.ui.sortables = $('.cms-draggables');
this._drag();
StructureBoard._initializeDragItemsStates();

that._loadedStructure = true;
this._loadedStructure = true;
})
.fail(function() {
window.location.href = new URI(window.location.href)
Expand Down Expand Up @@ -1491,6 +1498,39 @@ class StructureBoard {
});
}
}

/**
* Get's plugins data from markup
*
* @method _getPluginDataFromMarkup
* @private
* @param {String} markup
* @param {Array<Number | String>} pluginIds
* @returns {Array<[String, Object]>}
*/
static _getPluginDataFromMarkup(markup, pluginIds) {
return compact(
pluginIds.map(pluginId => {
// oh boy
const regex = new RegExp(`CMS._plugins.push\\((\\["cms\-plugin\-${pluginId}",[\\s\\S]*?\\])\\)`, 'g');
const matches = regex.exec(markup);
let settings;

if (matches) {
try {
settings = JSON.parse(matches[1]);
} catch (e) {
settings = false;
}
} else {
settings = false;
}

return settings;
})
);
}

}

/**
Expand Down
45 changes: 45 additions & 0 deletions cms/tests/frontend/unit/cms.structureboard.test.js
Expand Up @@ -2411,6 +2411,51 @@ describe('CMS.StructureBoard', function() {
});
});

describe('_getPluginDataFromMarkup()', () => {
[
{
args: ['', [1, 2, 3]],
expected: []
},
{
args: ['whatever', []],
expected: []
},
{
args: ['CMS._plugins.push(["cms-plugin-4",{"plugin_id":"4"}]);', [1, 2, 3]],
expected: []
},
{
args: ['CMS._plugins.push(["cms-plugin-4",{"plugin_id":"4"}]);', [1, 2, 4]],
expected: [['cms-plugin-4', { plugin_id: '4' }]]
},
{
args: [
`CMS._plugins.push(["cms-plugin-4",{"plugin_id":"4"}]);
CMS._plugins.push(["cms-plugin-10", { "plugin_id": "meh"}]);`, [1, 2, 10]],
expected: [['cms-plugin-10', { plugin_id: 'meh' }]]
},
{
args: ['CMS._plugins.push(["cms-plugin-4",{plugin_id:"4"}])', [4]],
expected: []
},
{
args: ['CMS._plugins.push(["cms-plugin-4",not a json :(]);', [4]],
expected: []
},
{
args: [`CMS._plugins.push(["cms-plugin-4", {
"something": 1
}])`, [4]],
expected: [['cms-plugin-4', { something: 1 }]]
}
].forEach((test, i) => {
it(`extracts plugin data from markup ${i}`, () => {
expect(StructureBoard._getPluginDataFromMarkup(...test.args)).toEqual(test.expected);
});
});
});

describe('_extractMessages()', () => {
let board;

Expand Down

0 comments on commit 9e1cc32

Please sign in to comment.