Disclaimer and License

Opinions expressed here by Tim Tripcony are his own and not representative of his employer.

Creative Commons License
Tip of the Iceberg is licensed under a Creative Commons Attribution 3.0 Unported License.
Based on a work at timtripcony.com.

Unless otherwise explicitly specified, all code samples and downloads are copyright Tim Tripcony and licensed under Apache License 2.0.

Search

What the Quote?

"It's an enzyme that keeps you from farting when you eat chili."

Devin Olson

"I'm tired. And the music is absurd."

Laura Tripcony

"Don't be the monkey that looks at the bone and goes, 'meh'."

Chris Toohey

« Rip and replace? Um... yeah, not so much. | Main| speaking at IAMLUG »

Facebook Like as a custom control

Category xpages

Last week Chris Toohey demonstrated how easy it now is to add a Facebook "Like" button to a site. Here's an implementation of a custom control that allows you to drag and drop this capability onto any XPage:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <xp:view xmlns:xp="http://www.ibm.com/xsp/core">
  3.     <div class="fblike">
  4.         <iframe
  5.             src="#{javascript:var fbUrl:XSPUrl = new XSPUrl(
  6.                 'http://www.facebook.com/plugins/like.php'
  7.             );
  8.             var fbParams = {
  9.                 href:java.net.URLEncoder.encode(
  10.                     compositeData.url
  11.                 ),
  12.                 layout:'standard',
  13.                 show_faces:'false',
  14.                 width:compositeData.width,
  15.                 height:compositeData.height,
  16.                 action:'like',
  17.                 font:'verdana',
  18.                 colorscheme:'light'
  19.             };
  20.             for(var i in fbParams) {
  21.                 fbUrl.setParameter(i, fbParams[i]);
  22.             }
  23.             return fbUrl.toString();}"
  24.             scrolling="no" frameborder="0"
  25.             style="border:none;
  26.                 overflow:hidden;
  27.                 width:#{compositeData.width}px;
  28.                 height:#{compositeData.height}px;"
  29.             allowTransparency="true"></iframe>
  30.     </div>
  31. </xp:view>

NOTE: the carriage returns above were included for the sake of readability in this context; you may want to get rid of some of the above whitespace in your actual source code.

A couple additional implementation caveats:

  • The above code assumes that you have defined custom properties for width, height, and url (string should be sufficient as the type for each).
  • Set the default value for height to 35, width to 450, and url to the following SSJS expression:
    return context.getUrl().toString();
  • Since all the definable properties have defaults, you can override them on any page to which you add the control, but don't need to override any of them; if you don't pass a URL to the control, it will just automatically use the URL of the current page, but if you want to display the button inside a repeat control (or any other context where the object or concept being "liked" is not specific to the current page), you'll want to pass the corresponding "permalink" for each member of the iterated collection.
  • For bonus points, save a screenshot of the Like button and upload it as an image resource called "fblike.jpg", then in the "Design Definition" section of the control properties, add the following XML (when viewing the Design tab of any XPage containing the control, this will cause Designer to display the image resource as a placeholder for the custom control, making it easier to drag to another spot on the page if you decide you want to move it somewhere else):
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <xp:view xmlns:xp="http://www.ibm.com/xsp/core">
  3. <xp:image url="/fblike.jpg"/>
  4. </xp:view>

Comments

Gravatar Image1 - Good stuff Tim. Many thanks for sharing this with us.

Post A Comment

:-D:-o:-p:-x:-(:-):-\:angry::cool::cry::emb::grin::huh::laugh::lips::rolleyes:;-)