Jump to content

Help with conditional Rule


cmartin

Recommended Posts

I have seen similar things posted but could not get those changed to work for what I need.

 

Trying to create a conditional rule to where bullets are added if there is more than one field or rule on the line. Also, the bullets need to be in a color (Pantone 801) defined in the document.

 

t. 888.999.6666 • f. 888.999.6666 • d. 888.999.6666

t. 888.999.6666 • f. 888.999.6666

t. 888.999.6666

f. 888.999.6666 • d. 888.999.6666

Link to comment
Share on other sites

Take a look at this thread: http://forums.pti.com/showthread.php?t=3554

 

It discusses your scenario almost exactly. The only difference would be the color bullet points. Assuming you've defined "PANTONE 801" in your FP template, you can apply color to a bullet like this:

return '<color name="PANTONE 801"> • </color>';

 

That was one of the threads I was trying follow to change to work.

 

Here is what I am trying and it is not adding the bullet.

 

var office = (Rule("tagPhone1")) + (Rule("Change phone format Rule"))

var fax = (Rule("tagPhone2")) + (Rule("Change fax phone format Rule"))

 

var numbers = [office + fax];

 

return numbers.filter(function(m){return m.match(/.*\s(?=.)/);}).join(" • ");

 

I get:

t. 901.484.4274 f. 281.989.6950

Link to comment
Share on other sites

Got it fix to show the bullet now. How do I add the color change to it?

 

var office = (Rule("tagPhone1")) + (Rule("Change phone format Rule"))

var fax = (Rule("tagPhone2")) + (Rule("Change fax phone format Rule"))

 

var numbers = [office,fax];

 

return numbers.filter(function(m){return m.match(/.*\s(?=.)/);}).join(" • ");

Link to comment
Share on other sites

Got it fix to show the bullet now. How do I add the color change to it?

 

Replace the bullet in the join method with the tag I posted in my first reply:

var office = (Rule("tagPhone1")) + (Rule("Change phone format Rule"))
var fax = (Rule("tagPhone2")) + (Rule("Change fax phone format Rule"))

var numbers = [office,fax];

return numbers.filter(function(m){return m.match(/.*\s(?=.)/);})[color="Red"].join('<color name="PANTONE 801"> • </color>')[/color];

 

Also, there's nothing wrong with putting parentheses around your "Rule" functions, but you don't need them. So, you could save yourself a few keystrokes and define your variables as:

var office = Rule("tagPhone1") + Rule("Change phone format Rule");
var fax = Rule("tagPhone2") + Rule("Change fax phone format Rule");

Link to comment
Share on other sites

Replace the bullet in the join method with the tag I posted in my first reply:

var office = (Rule("tagPhone1")) + (Rule("Change phone format Rule"))
var fax = (Rule("tagPhone2")) + (Rule("Change fax phone format Rule"))

var numbers = [office,fax];

return numbers.filter(function(m){return m.match(/.*\s(?=.)/);})[color="Red"].join('<color name="PANTONE 801"> • </color>')[/color];

 

Also, there's nothing wrong with putting parentheses around your "Rule" functions, but you don't need them. So, you could save yourself a few keystrokes and define your variables as:

var office = Rule("tagPhone1") + Rule("Change phone format Rule");
var fax = Rule("tagPhone2") + Rule("Change fax phone format Rule");

 

Thanks for the help but the color line is not working.

 

var office = Rule("tagPhone1") + Rule("Change phone format Rule")
var fax = Rule("tagPhone2") + Rule("Change fax phone format Rule")

var numbers = [office,fax];

return numbers.filter(function(m){return m.match(/.*\s(?=.)/);}).join('<color name="PANTONE 801"> • </color>');

 

I get:

t. 901.484.4274 <color name="PANTONE 801"> • </color>f. 281.989.6950

 

I have tried different colors (Cyan, Yellow, etc). I have the color defined in Advanced<colors. Does it need to be defined somewhere else?

Link to comment
Share on other sites

Since you're adding color tags to the bullet, you need to check the box beside "Treat returned strings as tagged text" at the top of your rule.

 

Great! That did it.

 

Well, one last question.

 

if (Field("Phone") != "")
   return "t. ";
else
   return "";

 

How do I change the "t." to a different font? Sorry for the basic questions. I have exampels but they usually include point size to.

Link to comment
Share on other sites

Similar to the "color" tag, there is also a "font" tag. You can find an example of how it works and a better explanation than I could give you on page 47 of the TagsRefGuide PDF that comes with FusionPro.

 

FusionPro > Documentation > Tags Reference

 

return Field("Phone") ? '<f name="Different Font">t.</f> ' : '';

Link to comment
Share on other sites

Thanks for all the help. JavaScript is not my thing but trying to learn. That worked and of course one additional question.

 

I used that on 3 additional sections and it worked on 2 but not the other.

 

Here is the one that failed.

 

var email = Field("Email");
var web = Field("Website");

var numbers = [email,web];

return numbers.filter(function(m){return m.match(/.*\s(?=.)/);}).join('<color name="Pantone 801"> • </color>');

 

It just returns "return value" when validating it.

Link to comment
Share on other sites

You're overthinking it and trying to apply a match rule written for phone numbers to email and web addresses. Not all rules have to be the same. in the above example, it would probably work to just have:

 

var numbers = [Field("Email"),Field("Website")];
return numbers.join('<color name="Pantone 801"> • </color>');

Link to comment
Share on other sites

Thanks but my mistake for not giving all the details.

 

I am trying to do the same same as the phones.

 

They might not put the email or website in so the bullet will be added depending on which they put in. Same as the phone. That is why I was trying to use that one.

Link to comment
Share on other sites

The way the code I wrote works is this:

  1. It creates an array of phone numbers which will be in this format: "f. ###.###.###"
  2. The filter method is applied to the array of numbers to remove the empty ones (more on this below).
  3. The remaining elements of the array are joined by a bullet.

 

The filter method is the part that I think is causing some confusion. If you take a look at its definition I've linked, you'll read that the filter method tests each element of the array against a function that will either return true or false. If the result is true, the element is kept. If it is false, it is removed from the array.

 

So, for the sake of this example, let's assume some values for your fields:

Rule("tagPhone1") = "d. "

Rule("Change phone format Rule") = "555.555.5555"

Rule("tagPhone2") = "f. "

Rule("Change phone format Rule") = ""

 

/*
var office = "d. " + "555.555.5555";
var fax = "f. " + ""; 
*/
var office = Rule("tagPhone1") + Rule("Change phone format Rule");
var fax = Rule("tagPhone2") + Rule("Change fax phone format Rule");

/*
var numbers = ["d. 555.555.5555", "f. "]
*/
var numbers = [office, fax];

The comments in the code above show what happens to the code when a number ("Change fax phone format Rule" in this example) is empty.

 

We know that we want to get rid of the "f. " element since it doesn't have a corresponding number associated with it so we have to write a function for the filter array that will return false when an element doesn't fit the format of: "letter, period, space, phone number".

 

That can be done a number of ways but the way I chose was to remove elements that contained a space but did not have a character following the space. I'll name the function "HasCharacterAfterSpace" and re-write the code like this to better illustrate:

/*
function HasCharacterAfterSpace(element) {
  return element.match(/.*\s(?=.)/);
}

// The below calls: HasCharacterAfterSpace("d. 555.555.5555"); <-- true
// then calls     : HasCharacterAfterSpace("f. "); <-- false
numbers = ["d. 555.555.5555", "f. "].filter(HasCharacterAfterSpace);

// Values that returned false are removed from the array:
numbers = ["d. 555.555.5555"];
*/
numbers = numbers.filter(function(m){return m.match(/.*\s(?=.)/);});

 

The next part is just joining the remaining elements in the array with something. In your case, you're joining them with a bullet wrapped in color tags but it could be anything you like. And as is the case in this example, arrays containing only one element won't be "joined" but will still be converted to a string.

 

It's also worth noting that arrays can contain more than two elements. So you could have all of your phone numbers in one array like this:

var numbers [ 
 "d. 555.555.5555", 
 "f. ",
 "c. 444.444.4444"
];

// returns 'd. 555.555.5555 <color name="PANTONE 801"> • </color>c. 444.444.4444';
return numbers.filter( function(element){ 
 return element.match(/.*\s(?=.)/);
}).join('<color name="PANTONE 801"> • </color>');

 

Hopefully that gives you a better understanding of what the code is doing and how to make the filtering work for an array of elements that likely don't contain any spaces (websites and email address).

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...