Go Back   MarcomCentral (PTI) and FusionPro User Communities > Software-Related Talk > The JavaScript Library > Getting Started, Learning, Etc.

Notices

Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old August 31st, 2016, 08:28 AM
stephsal stephsal is offline
Forum n00b
 
Join Date: Jul 2016
Posts: 2
Default If else statement based on the length of a text field?

I've been using simple rules for internal client templates and so far this has been straightforward but right now, I'm at a loss to what to for a more complex rule. Disclaimer is I'm not a developer and more of a HTML/CSS person so I've been going in blind for a lot of the more complex (to me) JavaScript (I have been using the W3 School site to try to learn it) - So please bear with me.

Currently I'm working on a project on Fusion pro that requires the street address and city of one of our clients. And the format is basically this:

<Street Address><Ste or Office number> | <City>

However, some of our client addresses are incredibly long and fill the whole line and that causes the line to break and <City> to go to another line. The problem is that the "|" also breaks to the next line and aesthetically it's not the greatest. So what I'm trying to figure out is if the combined address field is longer than the textbox and forces <City> to break to the next line, I'd like to suppress the "|" and have the break as is so it would effectively be:

<Street Address><Ste or Office number>
<City>

I looked through the forums and I saw the threads about using Text Measure, and I've used simple suppression rules before - I'm just at a bit of a loss at how to effectively combine the two so that the suppression rule is triggered when the address fields spans the whole text box length.

Thanks!
Reply With Quote
Old August 31st, 2016, 10:43 AM
tou
This message has been deleted by tou.
  #2  
Old August 31st, 2016, 11:29 AM
step's Avatar
step step is offline
Senior Contributor
 
Join Date: Jan 2010
Location: Charlotte, NC
Posts: 955
Default Re: If else statement based on the length of a text field?

You can give this a shot:
Code:
function AllowPipe(input){
  var tm = new FusionProTextMeasure;
  tm.font = 'Helvetica';
  tm.pointSize = '12 pt';
  tm.maxWidth = FusionPro.inValidation ? 7200 : 
    GetSettableTextWidth(FindTextFrame(FusionPro.Composition.CurrentFlow.name));
  tm.CalculateTextExtent(input);
  return tm.textLines == 1;
}
var address = [Field("Street Address"), Field("Ste or Office number"), Field("City")].filter(String).join(' ');
if (AllowPipe(address) && Field("City"))
  address = address.replace(' ' + Field("City"), ' | ' + Field("City"));
return address;
Make sure that you give the text frame that will be returning the rule a unique name. Otherwise, you'll have to manually specify the width of the text frame (in 1/100th's of a point). Then in the rule editor, make sure that you've checked "Re-evaluate this rule for every text flow."
__________________
Ste Pennell
FusionPro VDP Creator 9.3.15
Adobe Acrobat X 10.1.1
Mac OS X 10.12

LinkedIn
Reply With Quote
  #3  
Old August 31st, 2016, 01:59 PM
stephsal stephsal is offline
Forum n00b
 
Join Date: Jul 2016
Posts: 2
Default Re: If else statement based on the length of a text field?

Thanks for the response! There's additional functions I'm trying to add to this as well and while the validate button says the expression is OK, it's appearing as {Rule_Address_Format} in my template preview and I'm not sure what I'm doing wrong now.

Here's the code. In addition to the Pipe suppression, I made an abbreviation rule and a secondary address suppression (Actually that isn't working either now) if the client doesn't have a Suite or office number.

Code:
var fieldaddress = Field("address"),
    fieldaddress2 = Field("address2"),
    tm = new FusionProTextMeasure,
    address = [Field("address"), Field("address2"), Field("city")].filter(String).join(' ');

//Street Abbreviations
function AddressRules(){
        fieldaddress = fieldaddress.replace("Boulevard", "Blvd").replace("Street", "St").replace("Terrace","Ter").replace("Avenue","Ave").replace("Road","Rd");
        return fieldaddress;
    }
    
//Secondary Address Suppression        
function SuppressAddr2(){
        if (fieldaddress2 == "")
            return "";
        else
            return ', '+fieldaddress2;
    }

//Allow Pipe Rule
function AllowPipe(input){
  tm.font = 'Lato Bold';
  tm.pointSize = '87 pt';
  tm.maxWidth = FusionPro.inValidation ? 7200 : 
    GetSettableTextWidth(FindTextFrame(FusionPro.Composition.CurrentFlow.FormatAddress));
  tm.CalculateTextExtent(input);
  return tm.textLines == 1;}

if (AllowPipe(address) && Field("city"))
  address = address.replace(' ' + Field("city"), ' | ' + Field("city"));
return address;
Reply With Quote
  #4  
Old August 31st, 2016, 02:46 PM
step's Avatar
step step is offline
Senior Contributor
 
Join Date: Jan 2010
Location: Charlotte, NC
Posts: 955
Default Re: If else statement based on the length of a text field?

Quote:
Originally Posted by stephsal View Post
Thanks for the response! There's additional functions I'm trying to add to this as well and while the validate button says the expression is OK, it's appearing as {Rule_Address_Format} in my template preview and I'm not sure what I'm doing wrong now.

Here's the code. In addition to the Pipe suppression, I made an abbreviation rule and a secondary address suppression (Actually that isn't working either now) if the client doesn't have a Suite or office number.
Code:
var fieldaddress = Field("address"),
    fieldaddress2 = Field("address2"),
    tm = new FusionProTextMeasure,
    address = [Field("address"), Field("address2"), Field("city")].filter(String).join(' ');

//Street Abbreviations
function AddressRules(){
        fieldaddress = fieldaddress.replace("Boulevard", "Blvd").replace("Street", "St").replace("Terrace","Ter").replace("Avenue","Ave").replace("Road","Rd");
        return fieldaddress;
    }
    
//Secondary Address Suppression        
function SuppressAddr2(){
        if (fieldaddress2 == "")
            return "";
        else
            return ', '+fieldaddress2;
    }

//Allow Pipe Rule
function AllowPipe(input){
  tm.font = 'Lato Bold';
  tm.pointSize = '87 pt';
  tm.maxWidth = FusionPro.inValidation ? 7200 : 
    GetSettableTextWidth(FindTextFrame(FusionPro.Composition.CurrentFlow.FormatAddress));
  tm.CalculateTextExtent(input);
  return tm.textLines == 1;}

if (AllowPipe(address) && Field("city"))
  address = address.replace(' ' + Field("city"), ' | ' + Field("city"));
return address;
The reason that it validates correctly and doesn't display the right thing is this:
Code:
  tm.maxWidth = FusionPro.inValidation ? 7200 : 
    GetSettableTextWidth(FindTextFrame(FusionPro.Composition.CurrentFlow.FormatAddress));
That line of code sets the max width to the width of the current text frame. At the point of validation, the width of the current text frame is not known so I've manually set it to 7200 (or 1 inch) for validation purposes. "FormatAddress" is not a property of FusionPro.Composition.CurrentFlow. That should be changed to:
Code:
tm.maxWidth = FusionPro.inValidation ? 7200 : 
    GetSettableTextWidth(FindTextFrame(FusionPro.Composition.CurrentFlow.name));
Or (assuming your text frame is named "FormatAddress"):
Code:
tm.maxWidth = FusionPro.inValidation ? 7200 :
    GetSettableTextWidth(FindTextFrame("FormatAddress"));
Once you fix that, you probably still won't get the results you're looking for because you don't call either of the functions that you created. I think you want to do something like this:
Code:
function AllowPipe(input){
  tm = new FusionProTextMeasure;
  tm.font = 'Lato Bold';
  tm.pointSize = '87 pt';
  tm.maxWidth = FusionPro.inValidation ? 7200 : 
    GetSettableTextWidth(FindTextFrame(FusionPro.Composition.CurrentFlow.name));
  tm.CalculateTextExtent(input);
  return tm.textLines == 1;
}

var replace = { 'Boulevard': 'Blvd', 'Street': 'St', 'Terrace': 'Ter', 'Avenue': 'Ave', 'Road': 'Rd' };
var address = [
  [Field("address"), Field("address2")].filter(String).join(', '),
  Field("city")
].filter(String).join(' | ');

for (var find in replace)
  address = address.replace(new RegExp(find, 'gi'), replace[find]);

if (!AllowPipe(address))
  address = address.replace(' | ', ' ');

return address;
__________________
Ste Pennell
FusionPro VDP Creator 9.3.15
Adobe Acrobat X 10.1.1
Mac OS X 10.12

LinkedIn
Reply With Quote
Reply

Tags
rules, text formatting, textmeasure

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -7. The time now is 12:25 AM.


Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
(c) 2011, PTI Marketing Technologies™, Inc.