Jump to content

How to keep var elements together on a line


Kim

Recommended Posts

Hi,

I've got a card that will have different phone number fields and an email field.

 

Usually there will be office, direct, cell and fax phone number fields plus an email field. But sometimes there is just one or two phone number fields and the email field.

 

Right now I've got two separate text frames that return a group of phone numbers on the first line and the fax number (if present) and email on the second line.

 

I've got two rules for these lines:

 

var phone =

{

O: Field("Office"),

DD: Field("Direct Dial"),

C: Field("Cell"),

};

 

var result = [];

for (var p in phone)

if (phone[p])

result.push(p + ": " + phone[p]);

 

return result.join(" | ");

 

and

 

var email =

{

F: Field("Fax"),

E: Field("Email Address"),

};

 

var result = [];

for (var e in email)

if (email[e])

result.push(e + ": " + email[e]);

 

return result.join(" | ");

 

It all works great when I've got the multiple phone numbers for the first line.

But when I only have one or two phone numbers and email

I'd like to be able to combine and have all of them on one line or the phone numbers on line 1 and email on line 2 when the email address is too long to fit.

 

I've been trying to figure out how to use a non-breaking space so that I don't wind up with the E: on the first line and the email address wrapping down on a second line but I can't get it to work.

 

Example:

I get this with my current two rule setup:

 

O: 123-456-7890

F: 123-456-7890 | E:personsemailaddress@somewhere.com

 

I'd like to have:

O: 123-456-7890 | F: 123-456-7890

E:personsemailaddress@somewhere.com

 

But when I put everything into one rule, I'm sometimes winding up with:

O: 123-456-7890 | F: 123-456-7890 | E:

personsemailaddress@somewhere.com

 

I thought if I could get a   to work for the E: I could keep the email part together and get the entire email section to go onto line 2 when it's too long to fit on the first line with a phone number.

 

I tried:

var phone =

{

O: Field("Office"),

DD: Field("Direct Dial"),

C: Field("Cell"),

F: Field("Fax"),

E: Field("Email Address"),

};

 

var result = [];

for (var p in phone)

if (phone[p])

result.push(p + ":" + RawTextFromTagged(" ") + phone[p]);

 

return result.join(" | ");

 

but it doesn't work. It still wraps the email with the E: on line 1.

 

I guess I'm also going to have a problem with a | left on the end of line one anyway too, right? So maybe this isn't really do-able?

 

Any ideas? (Dan are you out there?)

Link to comment
Share on other sites

I'm thinking this would be a good use of the TextMeasure() method where you would determine whether or not the additional data would fit on one line and if not, add a break tag plus the additional data. I know there are other threads with examples of the method's use if you want to give it a go. :)
Link to comment
Share on other sites

So a fresh look and I appear to have been over thinking it.

 

result.push(p + ": " + phone[p]);

 

seems to give me what I want to keep the email address section together and going to the 2nd line when needed.

 

So now all I need to figure out is if it's possible to determine when the | would be the last element on line 1 and not show it?

 

Eric -- thanks for the suggestion, I'll see what I can find about TextMeasure.

Link to comment
Share on other sites

Eric -- having trouble wrapping my head around how I'd accomplish this with TextMeasure. Do you have a specific example you could point me to? Not sure how I'd work out a break with the way the rule combines the fields.
Link to comment
Share on other sites

Here's my (admittedly dirty) solution:

var phone = ["O : 555-123-4567","DD: 555-123-6789","C: 555-123-2345","F: 555-123-3456","E: someone@somewhere.com"];

var result = "";
var test = "";

var tm = new FusionProTextMeasure;
var frameWidth = 3;
tm.pointSize = "10 pt";
tm.font = "Helvetica";
tm.useTags = true;

for (var p=0; p<phone.length; p++) {
   if (phone[p] != "") {
       test += phone[p] + " | ";
       tm.CalculateTextExtent(test);
       if (tm.textWidth < frameWidth * 7200) result += phone[p] + " | ";
       else {
           test = phone[p] + " | ";
           result += "<br />" + phone[p] + " | ";
       }
   }
}
result = result.replace(/\|\s<br\s\/>/g,"<br />")
return Left(result,result.length-2);

Link to comment
Share on other sites

Thanks for the quick response, Eric.

I copied your code but didn't get any value returned -- I'll play around with it.

 

I was trying to keep things simple and tried this variation on my previous code:

 

var phone =

{

O: Field("Office"),

DD: Field("Direct Dial"),

C: Field("Cell"),

F: Field("Fax"),

};

 

var result = [];

for (var p in phone)

if (phone[p])

result.push(p + ": " + phone[p]);

 

if ((result.push(p + ": " + phone[p])) == 1)

 

return result.join(" | ") + "E: " + Field("Email Address");

else

return result.join(" | ") + "<br>" + "E: " + Field("Email Address");

Link to comment
Share on other sites

I revised my code to use an associative array like you originally had, but not witten as you originally had it. Still, I think it should be fairly easy to adapt to your situation:

var phone = [];
phone["O"]="555-123-4567"; // your office number
phone["DD"]=""; // your direct dial number
phone["C"]="555-123-2345"; // your cell number
phone["F"]=""; // your fax number
phone["E"]="someone@where.com"; // your email address

var result = "";
var test = "";

var tm = new FusionProTextMeasure;
var frameWidth = 4; // width of your text frame in inches
tm.pointSize = "10 pt"; // your point size here
tm.font = "Helvetica"; // your font here
tm.useTags = true;

for (var p in phone) {
   if (phone[p] != "") {
       test += p + ": " + phone[p] + " | ";
       tm.CalculateTextExtent(test);
       if (tm.textWidth < frameWidth * 7200) result += p + ": " + phone[p] + " | ";
       else {
           test = p + ": " + phone[p] + " | ";
           result += "<br />" + p + ": " + phone[p] + " | ";
       }
   }
}
result = result.replace(/\|\s<br\s\/>/g,"<br />");
return Left(result,result.length-2);

Edited by esmith
fixed leading data prefix after break tag caught by Kim below.
Link to comment
Share on other sites

ah, I always forget that when I copy code in here it truncates the rest of my message.

Following the code was my heartfelt thanks for your help.

I truly appreciate you taking the time to work this out for me.

I'm used to Dan usually swooping in to the rescue so it's great to know there is another javascript god on this forum. You are my hero. Thanks so much.

Edited by Kim
Link to comment
Share on other sites

ah, I always forget that when I copy code in here it truncates the rest of my message.

Following the code was my heartfelt thanks at your help.

I truly appreciate you taking the time to work this out for me.

I'm used to Dan usually swooping in to the rescue so it's great to know there is another javascript god on this forum. You are my hero. Thanks so much.

That's great that Eric could help out. That's the way the User Community is supposed to work. Eric is my hero too!

 

Just a couple of suggestions: When you post, click the "Go Advanced" button. Then, when you paste in code, you can select it and click the # button to make it show up in a code block, like in my and Eric's posts. Also, you can check the the "Disable smilies in text" box, to prevent vBulletin from helpfully replacing certain combinations of letters and punctuation (like in E:personsemailaddress@somewhere.com) with emoticons.

Link to comment
Share on other sites

Quick question, is the Text Measure Function available to versions before FP8?

Yes, the FusionProTextMeasure object has been available in every version of FusionPro. (It actually used to be called DL100TextMeasure in the ancient times before FusionPro, and that syntax will still work.)

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...