polymer 3 create element runtime and inject html inside slot - polymer

I am trying to create a polymer element in runtime and injecting HTML
inside of it
here is my code:
model-dialog.html
class ModelDialog extends PolymerElement {
static get template() {
return html`
<paper-dialog class="size-position" id="modelDialogId" entry-animation="slide-from-right-animation" exit-animation="slide-right-animation" with-backdrop="{{backdrop}}">
<slot></slot>
<paper-dialog>
`;
}
static get properties() {
return {
header: {
type: String,
value: '',
},
backdrop:{
type: Boolean,
value: false
}
};
}
showModel()
{
this.$.modelDialogId.open();
}
}
customElements.define('model-dialog', ModelDialog);
In the index.html
i am trying to create this element at runtime and trying to append the content to slot
<dom-bind>
<template is="dom-bind">
<h3>Basic model-dialog demo</h3>
<button on-click="_onPrevClick">show model</button>
</template>
</dom-bind>
<script type="module">
domBind._onPrevClick = function()
{
var model1 = document.createElement('model-dialog');
model1.innerHTML = ` <h2>Dialog Title</h2>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna
aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
Duis aute
irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat
cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>`;
model1.showModel();
}
</script>
It is not rendering the HTML in the paper-dialog element which I had created
actually, the HTML need to render inside slot element which I created inside paper-dialog
Maybe I missed something, or I did it in the wrong way?
kindly help me to solve this.
Thanks in advance .

Related

How to leave button selected in each row of a table

I have a doubt, I think it's even simple. I have a table that receives data from a JSON, each row of that table is a JSON field. On each line I added two buttons (Like/Dislike) to check the search quality. However, when I click on the like, for example, and I click on the like of the second line, it does not keep the like of the first line selected.
The method should work like this, after returning the data, the user will go through line by line evaluating the search, choosing between like or dislike. After that, we will take this assessment and save it.
However, it is not keeping the evaluation option selected in each line.
The code already has integration with VUE
Follow File HTML
<template>
<div v-for="regList in myJson" :key="regList" class="container" >
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<table>
<thead>
<tr>
<th>Documento</th>
<th colspan="2">Avalie a Busca</th>
</tr>
</thead>
<tbody>
<tr v-for="countryList in regList[2]" :key="countryList">
<td style="visibility: visible">
{{countryList}}
</td>
<td>
<button class="btn btn-1" type="button"><i class="fa fa-thumbs-up"></i></button>
</td>
<td>
<button class="btn btn-2" type="button"><i class="fa fa-thumbs-down"></i></button>
</td>
</tr>
</tbody>
</table>
</div>
</template>
EDIT 1
Nikola Pavicevic's solution seems to be adequate, but I needed to detail my code a little more. I have a Vue file running the code. The project is for the user to type a term, send it to the backend, which will return a Json, with an autocomplete phrase and also information from another API. In the frontend I separate this sentence from the rest of the JSON and present the autocomplete with the JSON inside the Table. The data I show in the table is just a description. I'll also leave a model of how the JSON goes to the table.
Part. Vue:
<script>
export default {
name: 'Autocomplete',
data: function() {
return {
autoComplete: "",
maxChars: 75,
connection: null,
myJson: []
}
},
mounted() {
const url = "ws://localhost:8000/"
this.connection = new WebSocket(url);
this.connection.onopen = () => console.log("connection established");
this.connection.onmessage = this.receiveText;
},
methods: {
sendText() {
const inputText = this.$refs.editbar.textContent;
this.connection.send(inputText);
},
receiveText(event) {
let result = JSON.parse(event.data)
this.autoComplete = result.shift();
this.myJson = result
}
}
}
</script>
Return Json in table:
[
{
"1": 0.471,
"2": {
"82": "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."
}
},
{
"1": 0.47,
"2": {
"686": "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."
}
}
]
EDIT 2 - Problem Soluction OK
Following Nikola's instructions, I ended up solving the problems. I just had to adjust to my situation. Follow code:
The ForEach I had to leave inside the Receive Text, as it is the place where my JSON is supplied with information
receiveText(event) {
let result = JSON.parse(event.data)
this.autoComplete = result.shift();
this.myJson = result
this.selected = []
this.myJson.forEach((m, i) => {return this.selected.push({i: i, state: null})})
}
For the rest, I followed the same guidelines as Nikola, just adjusting the code I already had and implementing the suggestions.
If I understood you correctly maybe like following snippet:
const app = Vue.createApp({
data() {
return {
myJson: [{"1": 0.471, "2": {"82": "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."}}, {"1": 0.47, "2": {"686": "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."}}],
selected: []
};
},
mounted() {
this.myJson.forEach((m, i) => {
return this.selected.push({i: i, state: null})
})
},
methods: {
setState(idx, state) {
this.selected[idx].state = state
}
}
})
app.mount('#demo')
.active {
background: gold !important;
}
.btn {
border-radius: 4px;
border: none;
padding: .5em 1em;
cursor: pointer;
}
.btn-1 {
background: lime;
}
.btn-2 {
background: crimson;
}
.btn:hover {
background: gold;
}
.fa {
color: white;
}
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<script src="https://unpkg.com/vue#3/dist/vue.global.prod.js"></script>
<div id="demo">
<div v-for="(regList, idx) in myJson" :key="regList" class="container" >
<table>
<thead>
<tr>
<th>Documento</th>
<th colspan="2">Avalie a Busca</th>
</tr>
</thead>
<tbody>
<tr v-for="countryList in regList[2]" :key="countryList">
<td style="visibility: visible">
{{countryList}}
</td>
<td>
<button #click="setState(idx, true)" class="btn btn-1" :class="selected[idx]?.state && 'active'">
<i class="fa fa-thumbs-up"></i>
</button>
</td>
<td>
<button #click="setState(idx, false)" class="btn btn-2" :class="selected[idx]?.state === false && 'active'">
<i class="fa fa-thumbs-down"></i>
</button>
</td>
</tr>
</tbody>
</table>
</div>
{{selected}}
</div>

Get rid of one-letter word at the end of each line

I'm looking for a HTML/CSS solution to get rid of one-letter words in each line in paragraph. In my home country there is grammar rule not to leave one-letter words but unfortunately I haven't found any CSS property to deal with it.
Example of incorrect paragraph:
My name is John Doe and I
look for a solution
Example of grammaticaly correct paragraph:
My name is John Doe and
I look for a solution'
I don't think that's possible with HTML and CSS. But if it must be done you can use <br/> to create it. Like this:
<p>
My name is John Doe and
<br/>
I look for a solution
</p>
Try Google next time. I found something similar as your question: How can I avoid one word on the last line with CSS?
Ok, so I did a little bit more reasearch and found that HTML allows for non-breaking space (&nbsp) so basically I had to insert "&nbsp" into HTML e.x. :
My name is John Doe and&nbspI look for a solution
To avoid inserting it manually into HTML (which is quite large) I wrote JS function for that:
function nbspInsert (sentence) {
let inputArray = sentence.split(" ");
let output = "";
for (let i = 0; i < inputArray.length; i++) {
if (i == inputArray.length) {
output += inputArray[i];
}
if (inputArray[i].length > 2) {
output += inputArray[i];
output += " ";
}
else {
output += inputArray[i];
output += "&nbsp";
}
}
return output;
}
Posting this answer to help anyone who will have same question in the future.
Cheers!
JavaScript Solution:
const setup = () => {
const pList = document.querySelectorAll('p');
pList.forEach(p => noMoreLonelyWords(p));
};
const clearWordBreaks = (target) => target.textContent = target.textContent.replace(/\u00a0/g, ' ');
const noMoreLonelyWords = (target) => {
let textArray = target.textContent.split(' ');
let newTextArray = [];
textArray.forEach((word, i, list) => {
let textEntry = '';
if(word.length === 1)
textEntry = word + '\xa0';
else
textEntry = word + ' ';
newTextArray.push(textEntry);
});
target.textContent = newTextArray.join('');
};
const updateWordBreaks = (target) => {
clearWordBreaks(target);
noMoreLonelyWords(target);
};
//load
window.addEventListener('load', setup);
<p>Lorem ipsum dolor sit amet, consectetur adipiscing I elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor
in reprehenderit in voluptate velit esse cillum dolore eu fugiat I nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor
in reprehenderit in voluptate velit esse cill I um dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor
in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
Good luck

React - put html tags inside json content

I'm trying to make multilingual website with redux, I have followed this tutorial > http://www.bebetterdeveloper.com/coding/getting-started-react-redux.html and adapt it to my project however I have a problem. I put my whole content inside json file. The thing is I wish to make some particular words bolded. Is it possible to add html tags inside the Json content? I found some way in the google but it doesn't work... Will be glad for any suggestions, That's my code:
[
{
"lang": "en",
"page": {
"menu": {
"home": "Home",
"brand": "Brand",
"contact": "Contact"
},
"home": {
"header": "Lorem Ipsum",
"paragraphOne": "Neque porro quisquam est qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit...",
"paragraphTwo": "Lorem <span className=\"bold-me\"> ipsum <\/span> dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."
}
}
}
}
]
You can use dangerouslySetInnerHTML if you want to parse the HTML in a string. You could then style the bold-me class to have bolder text, or simply use the b tag.
Example
const content = [
{
lang: "en",
page: {
menu: {
home: "Home",
brand: "Brand",
contact: "Contact"
},
home: {
header: "Lorem Ipsum",
paragraphOne:
"Neque porro quisquam est qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit...",
paragraphTwo:
"Lorem <b> ipsum </b> dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."
}
}
}
];
function App() {
return (
<div
dangerouslySetInnerHTML={{ __html: content[0].page.home.paragraphTwo }}
/>
);
}
ReactDOM.render(<App />, document.getElementById("root"));
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>
<div id="root"></div>

Provide border if div has overflow text

What would be the best way to implement a design that want div-containers be provides with a border, if the content of the div causes vertical scroll of the div?
CSS
div {
max-height: 100px;
overflow-y: auto;
// if content exceeds 100 px show border
border: solid 1px;
}
HTML
<div>
// Text ..
</div>
Set border if overflow.
var div = document.querySelector('div');
if (div.scrollHeight > div.clientHeight) {
div.style.border = '1px solid';
}
div {
max-height: 100px;
overflow-y: auto;
}
<div>
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor
in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Lorem ipsum dolor sit amet, consectetur adipiscing elit,
sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore
eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
</div>
I am assuming there is some kind of event that changes or determines the content of that div.
In my humble opinion, you can not achieve that using html/css alone. You would have to use JavaScript.
https://jsfiddle.net/4u2Lurt3/
Here is a simple example of how to do that. You may need to "re-wire" a bit differently to get the exact behavior you wanted
<div id="myDiv">
Hello
</div>
<button onclick="addText()">
Click to change div
</button>
And
function checkWidth() {
var div = document.getElementById('myDiv');
if(div.offsetWidth > 100) {
div.style.border = "1px solid black"
}
}
function addText() {
var div = document.getElementById('myDiv');
div.style.width="250px";
div.innerText = "AAAAAAAAAAAAA VVVVVVEERRRRY LOOOONNNNGGG TEEEEEXXXXXTT";
checkWidth();
}

Link to an element within the current page [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I created a HTML page with a number of tables with headers like this: Content, Main_Page, Document, Expenses, etc.
I want to create a link for the top of the page. When I click that link it should go to the specific section. So I use the below code to map the content. But it's not working for me.
Content Section
You need to create an anchor for the link. The modern way of doing this is to give the appropriate element an id="Content" attribute. The older way of doing this was to use <a name="Content"></a>.
Give the element you want to 'jump' to a clear ID, like so:
<p id="idOfTag">Your content goes here</p>
Then in the link on the top of the page, make it refer to the id of that element (mind the #):
Jump
Full example with multiple links:
<ul>
<li>Content</li>
<li>Main page</li>
<li>Document</li>
<li>Expenses</li>
</ul>
<p id="contentParagraph">
<h4>Content</h4>
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor
in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
</p>
<p id="mainPageParagraph">
<h4>Main page</h4>
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor
in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
</p>
<p id="documentParagraph">
<h4>Document</h4>
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor
in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
</p>
<p id="expensesParagraph">
<h4>Expenses</h4>
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor
in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
</p>
You can use name attribute for your anchor tag to achieve this.
Let say you have a div with id content
<div id="content">This is my div</div>
Next make sure you have a anchor tag with name attribute same as the id of the div content
Click to go to the top
Live Demo.
Scroll down to see the link
Another approach to do this would be
<div id="content">My div</div>
Then your anchor tag's href should be #content
Click to go to top
Live Demo.
Looks like the question has been answered but if you wanted to use a scrolling effect when transitioning to those elements here a little JS snipt.
$(function() {
function filterPath(string) {
return string
.replace(/^\//,'')
.replace(/(index|default).[a-zA-Z]{3,4}$/,'')
.replace(/\/$/,'');
}
var locationPath = filterPath(location.pathname);
var scrollElem = scrollableElement('html', 'body');
// Any links with hash tags in them (can't do ^= because of fully qualified URL potential)
$('a[href*=#]').each(function() {
// Ensure it's a same-page link
var thisPath = filterPath(this.pathname) || locationPath;
if ( locationPath == thisPath
&& (location.hostname == this.hostname || !this.hostname)
&& this.hash.replace(/#/,'') ) {
// Ensure target exists
var $target = $(this.hash), target = this.hash;
if (target) {
// Find location of target
var targetOffset = $target.offset().top;
$(this).click(function(event) {
// Prevent jump-down
event.preventDefault();
// Animate to target
$(scrollElem).animate({scrollTop: targetOffset}, 2000, function() {
// Set hash in URL after animation successful
location.hash = target;
});
});
}
}
});
// Use the first element that is "scrollable" (cross-browser fix?)
function scrollableElement(els) {
for (var i = 0, argLength = arguments.length; i <argLength; i++) {
var el = arguments[i],
$scrollElement = $(el);
if ($scrollElement.scrollTop()> 0) {
return el;
} else {
$scrollElement.scrollTop(1);
var isScrollable = $scrollElement.scrollTop()> 0;
$scrollElement.scrollTop(0);
if (isScrollable) {
return el;
}
}
}
return [];
}
});