mirror of
https://github.com/Hopiu/wagtail.git
synced 2026-05-09 16:04:45 +00:00
refactor code for initialising stream menu buttons into a StreamBlockMenu helper object
This commit is contained in:
parent
f9a442b28c
commit
fea640fbf3
1 changed files with 38 additions and 18 deletions
|
|
@ -1,4 +1,26 @@
|
|||
(function($) {
|
||||
var StreamBlockMenu = function(opts) {
|
||||
/*
|
||||
Helper object to handle the menu of available block types.
|
||||
Options:
|
||||
childBlocks: list of block definitions (same as passed to StreamBlock)
|
||||
menuItemPrefix: ID prefix of all menu items ('-' + childBlock.name is appended to each one)
|
||||
onChooseBlock: callback fired when a block type is chosen -
|
||||
the corresponding childBlock is passed as a parameter
|
||||
*/
|
||||
var self = {};
|
||||
|
||||
/* set up button behaviour */
|
||||
$.each(opts.childBlocks, function(i, childBlock) {
|
||||
var button = $('#' + opts.menuItemPrefix + '-' + childBlock.name);
|
||||
button.click(function() {
|
||||
if (opts.onChooseBlock) opts.onChooseBlock(childBlock);
|
||||
});
|
||||
});
|
||||
|
||||
return self;
|
||||
};
|
||||
|
||||
window.StreamBlock = function(opts) {
|
||||
/* Fetch the HTML template strings to be used when adding a new block of each type.
|
||||
Also reorganise the opts.childBlocks list into a lookup by name
|
||||
|
|
@ -29,29 +51,27 @@
|
|||
sequenceMember.delete();
|
||||
});
|
||||
|
||||
/* initialize 'prepend new block' buttons */
|
||||
function initializeAppendButton(childBlock) {
|
||||
var template = listMemberTemplates[childBlock.name];
|
||||
$('#' + sequenceMember.prefix + '-add-' + childBlock.name).click(function() {
|
||||
/* Set up the 'append a block' menu that appears after the block */
|
||||
StreamBlockMenu({
|
||||
'childBlocks': opts.childBlocks,
|
||||
'menuItemPrefix': sequenceMember.prefix + '-add',
|
||||
'onChooseBlock': function(childBlock) {
|
||||
var template = listMemberTemplates[childBlock.name];
|
||||
sequenceMember.appendMember(template);
|
||||
});
|
||||
}
|
||||
for (var i = 0; i < opts.childBlocks.length; i++) {
|
||||
initializeAppendButton(opts.childBlocks[i]);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
/* initialize header menu */
|
||||
function initializePrependButton(childBlock) {
|
||||
var template = listMemberTemplates[childBlock.name];
|
||||
$('#' + elementPrefix + '-before-add-' + childBlock.name).click(function() {
|
||||
/* Set up the 'prepend a block' menu that appears above the first block in the sequence */
|
||||
StreamBlockMenu({
|
||||
'childBlocks': opts.childBlocks,
|
||||
'menuItemPrefix': elementPrefix + '-before-add',
|
||||
'onChooseBlock': function(childBlock) {
|
||||
var template = listMemberTemplates[childBlock.name];
|
||||
sequence.insertMemberAtStart(template);
|
||||
});
|
||||
}
|
||||
for (var i = 0; i < opts.childBlocks.length; i++) {
|
||||
initializePrependButton(opts.childBlocks[i]);
|
||||
}
|
||||
}
|
||||
});
|
||||
};
|
||||
};
|
||||
})(jQuery);
|
||||
|
|
|
|||
Loading…
Reference in a new issue