Skip to content

Instantly share code, notes, and snippets.

@radmiraal
Created October 13, 2012 10:06
Show Gist options
  • Save radmiraal/3884034 to your computer and use it in GitHub Desktop.
Save radmiraal/3884034 to your computer and use it in GitHub Desktop.
/**
* Register template generation callbacks.
*
* For adding new content elements VIE needs an HTML template. This method registers callback methods
* for generating those templates. The template itself is rendered on the server, and contains the
* rendered output of the requested content type, rendered within the current typoscript path.
*
* @return {Void}
*/
_registerVieContentTypeTemplateCallbacks: function() {
_.each(vie.types.toArray(), function(type) {
var contentType = type.id.substring(1, type.id.length - 1).replace(T3.ContentModule.TYPO3_NAMESPACE, '');
var prefix = vie.namespaces.getPrefix(type.id);
if (prefix === 'typo3') {
vie.service('rdfa').setTemplate('typo3:' + contentType, 'typo3:content-collection', function(entity, callBack, collectionView) {
// This callback function is called whenever we create a content element
var type = entity.get('@type'),
contentType = type.id.substring(1, type.id.length - 1).replace(T3.ContentModule.TYPO3_NAMESPACE, ''),
referenceEntity = null,
lastMatchedEntity = null;
var afterCreationCallback = function(nodePath, template) {
entity.set('@subject', nodePath);
// We also want to load all the other RDFa properties on the entity.
// Else, editing newly created content elements in the Property Inspector
// does not work.
vie.load({element: template}).from('rdfa').execute();
callBack(template);
// When adding nested content elements (like the two-column-element),
// we need to refresh CreateJS to render the content element handles
// for the nested sections.
CreateJS.enableEdit();
}
// Calculate the position to add the new element based on the position in the collection.
// We need this as we create the content element on the server, and render it in place.
// The rendered output of the new element is then used as template.
_.each(collectionView.collection.models, function(matchEntity) {
if (entity === matchEntity && lastMatchedEntity) {
referenceEntity = lastMatchedEntity;
T3.Content.Controller.NodeActions.addBelow(
contentType,
referenceEntity,
afterCreationCallback
);
} else {
lastMatchedEntity = matchEntity;
}
});
if (referenceEntity === null) {
// No reference entity found. This only happens when an element is created into a section
if (collectionView.collection.models.length === 1) {
// The section only contains the new entity and was empty before, so we create the node into the section
T3.Content.Controller.NodeActions.addInside(
contentType,
vie.entities.get($(collectionView.el).attr('about')),
afterCreationCallback
);
} else {
// The section contains other entities, so we create the node before the first entity (index 1 as index 0 is the newly created entity)
T3.Content.Controller.NodeActions.addAbove(
contentType,
collectionView.collection.models[1],
afterCreationCallback
);
}
}
});
}
});
},
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment