Labs/Personas/Previewing Code: Difference between revisions

From MozillaWiki
< Labs‎ | Personas
Jump to navigation Jump to search
(→‎HTML: add info about data-browsertheme)
 
(4 intermediate revisions by 3 users not shown)
Line 1: Line 1:
== HTML  ==
== HTML  ==


The Personas JavaScript binds to whatever elements you pass it, but it expects a 'personas' attribute on those elements in the following format (copied from getpersonas.com):  
The Personas JavaScript binds to whatever elements you pass it, but it expects a <code>persona</code> attribute on those elements in the following format (copied from getpersonas.com):  


  <img class="preview persona"  
  <img class="preview persona"  
  src="/static/9/5/44495/preview_featured.jpg"  
  src="/static/9/5/44495/preview_featured.jpg"  
  persona="{&quot;id&quot;:&quot;44495&quot;,
  '''persona'''="{&quot;id&quot;:&quot;44495&quot;,
  &quot;name&quot;:&quot;Generation Fame&quot;,
  &quot;name&quot;:&quot;Generation Fame&quot;,
  &quot;accentcolor&quot;:&quot;#663300&quot;,
  &quot;accentcolor&quot;:&quot;#663300&quot;,
Line 21: Line 21:
  }">
  }">


You can copy the JSON blob from any persona on getpersonas.com
You can copy the JSON blob from any persona on getpersonas.com.  Note that HTML special characters have to be escaped in the JSON record when it is placed inside the <code>persona</code> attribute, although this is not shown in the example above.  For example, the quotation mark, which appears many times in the example above, needs to be escaped to the character entity <code>&amp;quot;</code>. Note that the attribute name is <code>data-browsertheme</code> for the built-in lightweight theme behavior in Firefox 3.6+.


== JavaScript ==
== JavaScript ==
Line 32: Line 32:
To just preview personas, all you need to do in your page is this:
To just preview personas, all you need to do in your page is this:
  <script type="text/javascript">
  <script type="text/javascript">
  $(document).ready(function() {
  $(function() {
     $("img.persona").previewPersona();
     $("img.persona").previewPersona();
  });
  });
Line 38: Line 38:
(assuming all images with class="persona" have a persona attribute)
(assuming all images with class="persona" have a persona attribute)
This will preview personas on mouseover/out.
This will preview personas on mouseover/out.
==Installing Personas==
Since this probably won't be used for most sites, it's easiest to do it this way:
<script type="text/javascript">
$("img.persona").click(function(event) {
                dispatchPersonaEvent('SelectPersona', event.currentTarget);
                return false;
            });
</script>
If you need detection if the user has personas installed, etc, check $.fn.personasButton in http://www.getpersonas.com/static/js/script.js

Latest revision as of 03:20, 7 September 2011

HTML

The Personas JavaScript binds to whatever elements you pass it, but it expects a persona attribute on those elements in the following format (copied from getpersonas.com):

<img class="preview persona" 
src="/static/9/5/44495/preview_featured.jpg" 
persona="{"id":"44495",
"name":"Generation Fame",
"accentcolor":"#663300",
"textcolor":"#fce8c3",
"header":"9\/5\/44495\/Persona_Header_fame-final.png",
"footer":"9\/5\/44495\/fame_Persona_footer-final.png",
"category":"Film and TV",
"description":"A dynamic reinvention of the Oscar\u00ae-winning film and globally successful TV series, FAME captures the visceral excitement and explosive energy of young artists and performers struggling to prove their talent and find their own\u2028voice. Caught up in the intense atmosphere of a performing arts high school\u2028in New York, they confront both the tantalizing promise of success, and the\u2028challenges of thriving in a highly competitive environment.\r\nhttp:\/\/www.generationfame.com",
"author":"MGM",
"detailURL":"http:\/\/www.getpersonas.com\/persona\/44495",
"headerURL":"http:\/\/www.getpersonas.com\/static\/9\/5\/44495\/Persona_Header_fame-final.png",
"footerURL":"http:\/\/www.getpersonas.com\/static\/9\/5\/44495\/fame_Persona_footer-final.png",
"previewURL":"http:\/\/www.getpersonas.com\/static\/9\/5\/44495\/preview.jpg",
"iconURL":"http:\/\/www.getpersonas.com\/static\/9\/5\/44495\/preview_small.jpg"
}">

You can copy the JSON blob from any persona on getpersonas.com. Note that HTML special characters have to be escaped in the JSON record when it is placed inside the persona attribute, although this is not shown in the example above. For example, the quotation mark, which appears many times in the example above, needs to be escaped to the character entity &quot;. Note that the attribute name is data-browsertheme for the built-in lightweight theme behavior in Firefox 3.6+.

JavaScript

jQuery required

Grab http://people.mozilla.org/~rdoherty/personasfunctions.js and add it to your JS file.


To just preview personas, all you need to do in your page is this:

<script type="text/javascript">
$(function() {
    $("img.persona").previewPersona();
});
</script>

(assuming all images with class="persona" have a persona attribute) This will preview personas on mouseover/out.

Installing Personas

Since this probably won't be used for most sites, it's easiest to do it this way:

<script type="text/javascript">
$("img.persona").click(function(event) {
                dispatchPersonaEvent('SelectPersona', event.currentTarget);
                return false;
            });
</script>

If you need detection if the user has personas installed, etc, check $.fn.personasButton in http://www.getpersonas.com/static/js/script.js