Using HTML fragments with declarative HTML views - sapui5

I am trying to decompose a large HTML view down into smaller, more manageable chunks.
Is it possible to use fragments to do this?
For example, I have a fragment file (view.configurator.Summary.fragment.html) containing the following:
<div data-sap-ui-type="sap.m.Button" data-text="Hello"></div>
In my parent file, I try to include the fragment as follows:
<div data-sap-ui-type="sap.m.VBox" class="summary-panel-content">
<div data-sap-ui-type="sap.ui.core.Fragment"
data-fragment-name="view.configurator.Summary"
data-type="HTML"></div>
</div>
However I get the following Error in the console:
Please provide a fragment name
Any ideas?
Seems like its a bug, but you can workaround by wrapping the fragment in a custom control
sap.ui.core.Control.extend("sap.mic.controls.Fragment", {
metadata: {
properties: {
"name": "string"
}
},
init: function () {
},
renderer: function (renderManager, control) {
var fragmentName = control.getProperty("name"),
fragment = sap.ui.htmlfragment(fragmentName);
renderManager.renderControl(fragment);
}
});
And used like so:
<div data-sap-ui-type="sap.m.Page" data-enable-scrolling="false">
<div data-sap-ui-type="sap.mic.controls.Fragment"
data-name="view.configurator.Summary"></div>
</div>

In XML-View
Include the view with this:
<mvc:XMLView viewName="your.namespace.ViewName" async="true" />
Whereas xmlns:mvc="sap.ui.core.mvc"
In HTML-View
You can include Views like this:
<div data-sap-ui-type="sap.ui.core.mvc.HTMLView" data-view-name="your.namespace.ViewName" data-async="true"></div>

Related

jQuery.on() for children with no particular selector

I have HTML structure like this:
<div class="parent">
<div class="child">
<div class="something">...</div>
</div>
<div class="child">
<div class="something-else">...</div>
</div>
<div class="child">
...
</div>
...
</div>
I catch events (like click) on .child elements like this:
$('.parent').on('click', '.child', function() { ... });
However, I would like to get rid of explicit class specification and base on the fact of direct ancestry itself.
I want to write the code which would not require any particular classes for children elements. Closest thing to this is:
$('.parent').on('click', '*', function() { ... });
But obviously such handler will spread on deeper descendants (.something, .something-else etc.), not only on the first level.
Is there a way to acheive what I look for, being it using something instead of * or some other way?
P.S. I don't want to use direct binding - $('.parent').children().click(function() {...}); - as it is slower and will not work in case of children being dynamically added.
The selector sought for is > *:
$('.parent').on('click', '> *', function() { ... });
(The actual solution was suggested by Josh Crozier in the comments, I just reposted it as an answer.)

Play framework 2 : What is a proper way to render the contents of parent template?

Normally, when a template uses inheritance, it's possible to render the contents of parent template. For example, in Twig:
{% extends "base.html" %}
{% block sidebar %}
{{ parent() }}
...
{% endblock %}
It means that if I sometimes don't need the contents of parent template, I just simply remove the parent() function. How can I achieve this in Scala template?
You can create a HTML block in child view first and then pass it to parent (layout):
#sidebar = {
<div>MySidebar</div>
}
#parent(sidebar){
<div>Main content of the child view</div>
}
So in parent.scala.html layout you will use it just like
#(sidebar: Html = null)(content: Html)
<div id="parent-wrap">
#sidebar
#content
</div>
Of course if you gonna to use the same HTML code for many subpages you'll do better if you use a tag instead of declaring #sidebar block in each view. Remember that tag is nothing more then just a scala view (template) and you can include it as any other view. Just create a normal view in app/views/tags i.e.: sidebar.scala.html with required HTML block, so you can later use it somewhere like:
<div class="span3">#tags.sidebar("content to pass")</div>
or as a block passed to higher level layout, etc :
#parent(tags.sidebar(additionalContent)){
<div>Main content of the child view</div>
}
TBH I never knew what's the difference between including views and tags ;)
Well, views are just functions in play. You cannot inherit them, but you can easily define reusable blocks:
#parent() = {
...
}
And then reuse it anywhere:
<div ....sidebar>
#parent()
</div>
The working example would look like this:
base.scala.html
#(title: String)(content: Html)
<html>
<head><title>#title</title></head>
<body>
<div class=...>#content</div>
</body>
</html>
index.scala.html
#(someParam: String)
#base("index") {
<h1>#someParam</h1>
}
If you don't need your index page to share the base's behaviour - just don't use #base:
index.scala.html
#(someParam: String)
...some other content
#someParam
...more content
Like this:
parent.scala.html
#(title: String)(content: (() => Html) => Html) {
<div class="parent">
<h2>#title</h2>
#content { () =>
<p>This is the parents optional contribution to the sidebar.</p>
}
</div>
child.scala.html
#parent("Some title") { parentSidebar =>
<p>Blah blah blah.</p>
<div class="sidebar">
...
#parentSidebar()
</div>
}
So the parent takes a function that takes a function that returns HTML.

Need help Selecting

I have HTML similar to this :
<div class="MainForm">
<form name="FromName">
<button name="Button1"></button>
...
...
</form>
<Div class="blackBox" style="visibility:hidden;"></div>
<Div class="SubFotm" style="visibility:hidden;"></div>
</div>
Now I can properly find the trigger for my button click in my script, but I'm not able to target only the closet blackbox to turn it visible.
Currently I'm doing :
if (PButtonName=="Fermer") {
$(this).closest("div .ProfileForm").remove(); // Closing Profile Form
} else if (PButtonName=="plusAdresse") {
alert('In');
$(this).closest("div .BlackBox").css("visibility","visible");
}
I can get the alert "In" to show, but not the BlackBox
If I change the
$(this).closest("div .BlackBox").css("visibility","visible");
for :
$("div .FormBlackBox").css("visibility","visible");
It will show, but will also show all the black box in the document.
If you are using the above HTML, or something similar, I would do it using a reference to the parents.
instead of:
$(".MainForm").closest("div .BlackBox").css('visibility','visible');
use
$(this).parents('.MainForm').children('.BlackBox').css('visibility','visible');
This is assuming you have more than one MainForm div and they all have a single child with the BlackBox class.
here is an example.
Instead of what you have done just add styles display:none; to your divs and then show them whenever you want.So you can do this as below:
<div class="MainForm">
<form name="FromName">
<button name="Button1"></button>
...
...
</form>
<div class="blackBox" style="display:none;"></div>
<div class="SubFotm" style="display:none;"></div>
</div>
and then in your script
if (PButtonName=="Fermer")
{
$(".MainForm").closest("div .ProfileForm").remove(); // Closing Profile Form
}
else if (PButtonName=="plusAdresse")
{
alert('In');
$(".MainForm").closest("div .BlackBox").show();
}
And I will recommend you using Switch case instead of loops at this place.

Meteorjs - select DOM of a template from another template

I have the following
<div id="header">
{{> header}}
</div>
<div class="hidden content_box">
{{> content}}
</div>
"content_box" is hidden at the start.
template "header" has a single button.
<template name="header">
<button id="display_content">Click to display content</button>
</template>
template "content" is just a simple div
<template name="content">
It's me, content
</template>
When I click on the button in the header, I want to "show" the content_box.
How do I achieve this? - or better yet, what is the best way to achieve this type of effect where you need to access the DOM of a template from an event of another template?
Template.header.events "click button#display_content": (e) ->
Template.content.show() ?????
I don't know if it is the best way to do it, but in similar situations what I've done before is to use a session parameter to store the show/hide status of the div. In your click event, you then only need to change the value of the session flag. In the template of the div you want to show/hide, you just return the class name.
Example in JS:
Template.header.events({
"click button#display_content": function () {
Session.set('contentShow', true);
}
});
Template.content.className = function (input) {
return Session.equals('contentShow', true) ? '' : 'hidden';
};
Html
<template name="content">
<div class="{{className}} content_box">
It's me, content
</div>
</template>
You'd need to initialise the session flag to false in Meteor.startup() for example Session.set('contentShow', false);. As session is reactive, the div class name will be re-evaluated automatically when you change the session flag.

How to use "this" and not "this" selectors in jQuery

I have 4 divs with content like below:
<div class="prodNav-Info-Panel">content</div>
<div class="prodNav-Usage-Panel">content</div>
<div class="prodNav-Guarantee-Panel">content</div>
<div class="prodNav-FAQ-Panel">content</div>
And a navigation list like this:
<div id="nav">
<ul id="navigation">
<li><a class="prodNav-Info" ></a></li>
<li><a class="prodNav-Usage" ></a></li>
<li><a class="prodNav-Guarantee"></a></li>
<li><a class="prodNav-FAQ" ></a></li>
</ul>
</div>
When the page is first displayed I show all the content by executing this:
$('div.prodNav-Usage-Panel').fadeIn('slow');
$('div.prodNav-Guarantee-Panel').fadeIn('slow');
$('div.prodNav-FAQ-Panel').fadeIn('slow');
$('div.prodNav-Info-Panel').fadeIn('slow');
Now, when you click the navigation list item it reveals the clicked content and hides the others, like this:
$('.prodNav-Info').click( function() {
$('div.prodNav-Info-Panel').fadeIn('slow');
$('div.prodNav-Usage-Panel').fadeOut('slow');
$('div.prodNav-Guarantee-Panel').fadeOut('slow');
$('div.prodNav-FAQ-Panel').fadeOut('slow');
});
So what I have is 4 separate functions because I do not know which content is currently displayed. I know this is inefficient and can be done with a couple of lines of code. It seems like there is a way of saying: when this is clicked, hide the rest.
Can I do this with something like $(this) and $(not this)?
Thanks,
Erik
In your particular case you maybe able to use the .sibilings() method something like this:
$(this).fadeIn().sibilings().fadeOut()
Otherwise, lets say that you have a set of elements stored somewhere that points to all of your elements:
// contains 5 elements:
var $hiders = $(".prodNavPanel");
// somewhere later:
$hiders.not("#someElement").fadeOut();
$("#someElement").fadeIn();
Also, I would suggest changing the classes for your <div> and <a> to something more like:
<div class="prodNavPanel" id="panel-Info">content</div>
....
<a class="prodNavLink" href="#panel-Info">info</a>
This gives you a few advantages over your HTML. First: the links will have useful hrefs. Second: You can easily select all your <div>/<a> tags. Then you can do this with jQuery:
$(function() {
var $panels = $(".prodNavPanel");
$(".prodNavLink").click(function() {
var m = this.href.match(/(#panel.*)$/);
if (m) {
var panelId = m[1];
$panels.not(panelId).fadeOut();
$(panelId).fadeIn();
return false; // prevents browser from "moving" the page
}
});
});