How to use COMMENTS channel without getting a warning - warnings

I read the answer to Why am I getting an error when assigning tokens to a channel?.
The answer is that this is not an error, but a warning.
Well that's all very well, but the Eclipse IDE, https://github.com/jknack/antlr4ide, will not generate code when this warning is present.
Is there a way to not get a warning, when using a COMMENT channel?

You could trick the compiler by defining COMMENT in a tokens{} block instead of in the #members{} block. This would result in a constant value getting automatically assigned to it.
If you are also using the HIDDEN channel, I would include something like this to be safe:
#members {
{
if (HIDDEN == COMMENT) {
throw new IllegalStateException(
"Expected the HIDDEN and COMMENT constants to have different values.");
}
}
}

Related

How do I find the name of this control? "Cannot read property 'focus' of null"

I am trying to focus an element of web.whatsapp.com.
I use "Inspect" in Chrome to find its name.
As I read it, the name is
div.Er7QU.copyable-text selectable-text"
However, when I call ".focus()" on it...
document.querySelector("div.Er7QU.copyable-text selectable-text").focus();
I receive the error
Cannot read property 'focus' of null.
What am I doing wrong?
In Chrome, it looks like this:
You must use document.querySelector("div.Er7QU.copyable-text.selectable-text").focus();
You need to concatenate all class names via the . (also, this is exactly what appears on your screenshot. There are no spaces between the classes)
Also, as #Rick mentioned, make sure to protect the code by using a conditional statement that will check whether the selector returned null before running any methods on the element selected.
document.querySelector("div.Er7QU.copyable-text selectable-text") is returning null. code it like this to avoid errors, but something else is wrong with your code:
var el = document.querySelector("div.Er7QU.copyable-text selectable-text");
if (el)
{
el.focus();
}
else
{
alert('Element not found.');
}

Angular: Routing between pages using condition

I am trying to route between pages using basic if condition in Angular.
GoToHome() {
if(this.router.url=='/chat'){
console.log(this.router.url)
this.router.navigate(['login']);
} else {
this.router.navigate(['people']);
}
}
The problem is that the route chat isn't really correct, there are many pages in chat (chat\x , chat\y and many others) I want that it will work for all the pages in chat, but right now it doesn't work. If I write a specific route like chat\x it does work, but only for x. Is there a way to do it for all?
you can read and check Guards. Read about CanActivate method, maybe it will help you?
RouteGuards might do a better job of handling the redirects as per your requirement.
But a quick workaround would be to do a split() on the URL and compare for the chat part. Try the following
if(((this.router.url).split('/')[1]) === 'chat') {
// proceed
}
As other had said, best solution is to use Angular Guard https://medium.com/#ryanchenkie_40935/angular-authentication-using-route-guards-bf7a4ca13ae3.
Anyway to resolve your problem you can use startsWith() function which determines whether a string begins with the characters of a specified string.
GoToHome() {
if((this.router.url).startsWith('/chat'){
console.log(this.router.url)
this.router.navigate(['login']);
} else {
this.router.navigate(['people']);
}
}

How can I customize the filter function for SelectOneMenu

I tried to find on Primefaces Documentation but I have not found how can I customize the filter function for SelectOneMenu.
I add filterMatchMode="custom" filterFunction="#{mainRandevuBean.ilFilter()}"
But I don't know how can I write bean filterFunction.
The filter is a javascript (client-side) function. It all IS in the PrimeFaces documentation, which you should always look into first, carefully, thouroughly.
So use filterFunction="myFilter"
and create a javascript function like
function myFilter(itemLabel, filterValue) {
// return true if this label matches, false otherwise
}
Just as a sidenote: primefaces documentation doesn't say anything semantically about the parameters. It also does not mention where the label comes from (in fact, the docs mention "the item value" which is not very clear).
In fact I used the JavaScript function to debug this in order to figure out what was provided by default as a label.
function filterList(label, filter){
alert("label="+label+" and filter="+filter);
return false;
}
At first I thought it would be anything like the text inside the HTML generated for each list item. But when debugging it I saw the alert said that the label was something like my.package.SomeValueObject#123456 (which is obvously the Java object toString on each item in the list).
You need to define the itemLabel property on the selectItems which is inside the selectManyMenu to generate a proper text value used by the standard filtering mechanisme. As far as I could figure out that is the only reason why you have to put itemLabel there. In the documentation itemLabel is specified before explaining filtering which is confusing.
And as far as I know the itemValue defaults anyhow to just the object value, so I believe following from the documentation is redundant.
itemValue="#{player}"
Hope it helps anyone :.)
I resolve this problem with autocomplete component. Primefaces autocomplete component with dropdown="true" property works like one menu.

If statement is not evaluated correctly

I am trying to evaluate a property of a model to see if it is empty before attempting to render a HiddenFor HTML element, but the code block of the if statement is entered, even if the statement is false.
#{
if(Model.BoatImage.UploadedImagePath != string.Empty)
{
#Html.HiddenFor(model => model.BoatImage.UploadedImagePath)
}
}
In the code above, UploadedImagePath is initialized to string.Empty in BoatImage's constructor. If I break at the if statement, Intellisense shows that UploadedImagePath is empty, and the Immediate window evaluates the if statement to be false. However, the program still attempts to render the HiddenFor element.
EDIT: The debugger incorrectly noted that the code above was causing the exception, when actually is was a couple of lines below the code above. Once I corrected the line that was causing the issue, the code above works correctly.
in razor you should write like this:
#if(!string.IsNullOrEmpty(Model.BoatImage.UploadedImagePath))
{
#Html.HiddenFor(model => model.BoatImage.UploadedImagePath)
}
Please note that I also changed your string evaluation method

how to skip undefined properties in JSON?

When I parse JSON fields coming from google maps, etc., it is a mess. Because they are not made specifically for my script I have to verify many details, epecially because the addresses are different in every country.
Short question: when the script finds a undefined property the script breaks...error..
How can I verify the property is defined?
if(data.Placemark[i].AddressDetails.Country
.AdministrativeArea.SubAdministrativeArea.Locality != null) {
/***do something***/
}
Something like that doesn't seem to solve the problem. Why?
In JavaScript, accessing a property of an object that does not exist returns undefined, not null - heck, you said it in the title.
So, assuming that all the previous properties do actually exist, you can check that the Locality property exists using typeof, like this:
if(typeof (data.
Placemark[i].
AddressDetails.
Country.
AdministrativeArea.
SubAdministrativeArea.
Locality) !== 'undefined') {
/***do something***/
}
Or, (I think) you can use hasOwnProperty():
if (data.
Placemark[i].
AddressDetails.
Country.
AdministrativeArea.
SubAdministrativeArea.hasOwnProperty('Locality'))
{
/*** do something ***/
}
First, In javascript you can use "try / catch" like java or other programming language, this can let your code continue running if something goes wrong...
for your issue, you can test that :
if (typeof(data.Placemark[i].AddressDetails.Country
.AdministrativeArea.SubAdministrativeArea.Locality)
&&
data.Placemark[i].AddressDetails.Country
.AdministrativeArea.SubAdministrativeArea.Locality.length>0) {
}