Allowing sections to work outside components
This commit is contained in:
parent
dd745e3f65
commit
2a5699a5b7
@ -1,8 +1,26 @@
|
|||||||
|
import { Template } from 'meteor/templating';
|
||||||
|
import { Tracker } from 'meteor/tracker';
|
||||||
|
import { ReactiveVar } from 'meteor/reactive-var';
|
||||||
import { OHIF } from 'meteor/ohif:core';
|
import { OHIF } from 'meteor/ohif:core';
|
||||||
|
|
||||||
|
// Get the view that contains the desired section's content and return it
|
||||||
|
const getSection = (view, sectionName) => {
|
||||||
|
let currentView = view;
|
||||||
|
while (!currentView._sectionMap || !currentView._sectionMap.get(sectionName)) {
|
||||||
|
currentView = OHIF.blaze.getParentTemplateView(currentView);
|
||||||
|
if (!currentView) return;
|
||||||
|
}
|
||||||
|
|
||||||
|
return currentView._sectionMap.get(sectionName);
|
||||||
|
};
|
||||||
|
|
||||||
Template.section.onCreated(() => {
|
Template.section.onCreated(() => {
|
||||||
const instance = Template.instance();
|
const instance = Template.instance();
|
||||||
|
|
||||||
|
if (instance.data === 'dialogFooter') {
|
||||||
|
console.warn('>>>>instance', instance);
|
||||||
|
}
|
||||||
|
|
||||||
// Create the render function and section data as reactive objects
|
// Create the render function and section data as reactive objects
|
||||||
instance.renderFunction = new ReactiveVar(null);
|
instance.renderFunction = new ReactiveVar(null);
|
||||||
instance.sectionData = new ReactiveVar(null);
|
instance.sectionData = new ReactiveVar(null);
|
||||||
@ -18,36 +36,28 @@ Template.section.onCreated(() => {
|
|||||||
// Get the content block
|
// Get the content block
|
||||||
const templateContentBlock = instance.view.templateContentBlock;
|
const templateContentBlock = instance.view.templateContentBlock;
|
||||||
|
|
||||||
|
// Get the parent template view of this section block
|
||||||
|
let currentView = OHIF.blaze.getParentTemplateView(instance.view);
|
||||||
|
|
||||||
// Check if it is defining or printing the section content
|
// Check if it is defining or printing the section content
|
||||||
if (templateContentBlock) {
|
if (templateContentBlock) {
|
||||||
// Get the parent component of this section
|
// Define a section map for template's view if none was yet set
|
||||||
const component = OHIF.blaze.getParentComponent(instance.view, '_wrapperComponent');
|
if (!currentView._sectionMap) {
|
||||||
|
currentView._sectionMap = new Map();
|
||||||
// Stop here if this section is not inside a component
|
|
||||||
if (!component) {
|
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Define the content
|
// Define the content
|
||||||
component.templateInstance.sections[sectionName] = {
|
currentView._sectionMap.set(sectionName, {
|
||||||
data: component.templateInstance.data,
|
data: currentView._templateInstance.data,
|
||||||
renderFunction: templateContentBlock.renderFunction
|
renderFunction: templateContentBlock.renderFunction
|
||||||
};
|
});
|
||||||
} else {
|
} else {
|
||||||
// Wait for re-rendering and print the section content
|
// Wait for re-rendering and print the section content
|
||||||
Tracker.afterFlush(() => {
|
Tracker.afterFlush(() => {
|
||||||
// Get the parent component of this section
|
// Get the defined section's content
|
||||||
const component = OHIF.blaze.getParentComponent(instance.view, '_wrapperComponent');
|
const section = getSection(currentView, sectionName);
|
||||||
|
|
||||||
// Stop here if this section is not inside a component
|
// Stop here if the section content is not defined
|
||||||
if (!component) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Get the defined section content
|
|
||||||
const section = component.templateInstance.sections[sectionName];
|
|
||||||
|
|
||||||
// Stop here if the section content is not defined yet
|
|
||||||
if (!section) {
|
if (!section) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -65,7 +65,6 @@ Template.baseComponent.constructView = function(contentFunc, elseFunc) {
|
|||||||
|
|
||||||
// Iterate over all wrappers and assign the component to them
|
// Iterate over all wrappers and assign the component to them
|
||||||
wrapperInstances.forEach(wrapperInstance => {
|
wrapperInstances.forEach(wrapperInstance => {
|
||||||
wrapperInstance.view._wrapperComponent = instance.component;
|
|
||||||
wrapperInstance.component = instance.component;
|
wrapperInstance.component = instance.component;
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@ -40,3 +40,14 @@ OHIF.blaze.getParentComponent = (view, property='_component') => {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// Search for the parent template of the given view
|
||||||
|
OHIF.blaze.getParentTemplateView = view => {
|
||||||
|
let currentView = view;
|
||||||
|
while (currentView) {
|
||||||
|
currentView = currentView.originalParentView || currentView.parentView;
|
||||||
|
if (currentView.name.indexOf('Template.') > -1) {
|
||||||
|
return currentView;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user