0
Adding Interactivity to
   your Plone Site
    http://bit.ly/plone-interactivity
Me
Me
• Eric Steele
Me
• Eric Steele
 • Programmer @ WebLion
Me
• Eric Steele
 • Programmer @ WebLion
 • Plone Products
Me
• Eric Steele
 • Programmer @ WebLion
 • Plone Products
   • Faculty/Staff Directory
Me
• Eric Steele
 • Programmer @ WebLion
 • Plone Products
   • Faculty/Staff Directory
   • GloWorm
Me
• Eric Steele
 • Programmer @ WebLion
 • Plone Products
   • Faculty/Staff Directory
   • GloWorm
 • Plone 4 release ma...
Adding Interactivity to
   your Plone Site
or...
I Suck at JavaScript...
   and So Can You
I Suck at JavaScript...
   and So Can You
          ~
I Suck at JavaScript...
   and So Can You
               ~
 A half-assed guide to interactive
            JavaScript
The Stuff
The Stuff
• The Boring Stuff
  • Resources
The Stuff
• The Boring Stuff   • The Fun Stuff
  • Resources          • KSS
                       • jQuery
Resources
Resources

• JavaScript
• CSS
• KSS
Including JavaScripts
<tal:block metal:fill-slot=quot;javascript_head_slotquot;>
  <script type=quot;text/javascriptquot; s...
Including JavaScripts
<tal:block metal:fill-slot=quot;javascript_head_slotquot;>
  <script type=quot;text/javascriptquot; s...
Including JavaScripts
<tal:block metal:fill-slot=quot;javascript_head_slotquot;>
  <script type=quot;text/javascriptquot; s...
Resource Registries
Resource Registries
Resource Registries

• Control inclusion, order
Resource Registries

• Control inclusion, order
• Automatic merging, compression
Resource Registries

• Control inclusion, order
• Automatic merging, compression
• Improved cacheability
Generic Setup
                             jsregistry.xml
<?xml version=quot;1.0quot;?>
<object name=quot;portal_javascrip...
External Scripts
External Scripts
<javascript ... id=quot;http://ajax.googleapis.com/
ajax/libs/jquery/1.2.6/jquery.min.jsquot; ... />
External Scripts
<javascript ... id=quot;http://ajax.googleapis.com/
ajax/libs/jquery/1.2.6/jquery.min.jsquot; ... />


• ...
External Scripts
<javascript ... id=quot;http://ajax.googleapis.com/
ajax/libs/jquery/1.2.6/jquery.min.jsquot; ... />


• ...
Google AJAX Libraries
Google AJAX Libraries
 http://code.google.com/apis/ajaxlibs/
Google AJAX Libraries
     http://code.google.com/apis/ajaxlibs/

•   jQuery                 •   Dojo

•   jQuery UI      ...
Browser Resources
•   Zope 3 concept

•
Browser Resources
Browser Resources
  <browser:resource
      name=quot;myscript.jsquot;
      image=quot;myscript.jsquot;
      />
Browser Resources
       <browser:resource
           name=quot;myscript.jsquot;
           image=quot;myscript.jsquot;
  ...
Browser Resources
<?xml version=quot;1.0quot;?>
                             jsregistry.xml
<object name=quot;portal_javas...
Browser Resources
Browser Resources
<browser:resource
    name=quot;myscript.jsquot;
    image=quot;myscript.jsquot;
    for=quot;..interfac...
Browser Resources
<browser:resource
    name=quot;myscript.jsquot;
    image=quot;myscript.jsquot;
    for=quot;..interfac...
Browser Resources
Browser Resources
<browser:resourceDirectory
      name=quot;myResourcesquot;
     directory=quot;stuffquot; />
Browser Resources
       <browser:resourceDirectory
             name=quot;myResourcesquot;
            directory=quot;stu...
JavaScript Libraries
A JavaScript library is a library of
pre-written JavaScript controls which
allow for easier development of
JavaScript-base...
Why?
Why?

• Save time
Why?

• Save time
• Reuse existing code
Why?

• Save time
• Reuse existing code
• You don’t need to be an expert
Why?

• Save time
• Reuse existing code
• You don’t need to be an expert
• Browser “quirks”
KSS
KSS

• “Kinetic Style Sheets”
• JavaScript/AJAX framework
• Declare behaviors using CSS format
• Built into Plone 3.0+
• A...
KSS Rules

#mykssbutton:click {
  action-client: alert;
  alert-message: ‘Hello World!’;
}
KSS Rules
CSS Selector
    #mykssbutton:click {
      action-client: alert;
      alert-message: ‘Hello World!’;
    }
KSS Rules
Action Type
   #mykssbutton:click {
     action-client: alert;
     alert-message: ‘Hello World!’;
   }
KSS Rules
             Action

#mykssbutton:click {
  action-client: alert;
  alert-message: ‘Hello World!’;
}
KSS Rules
          Action Parameter
#mykssbutton:click {
  action-client: alert;
  alert-message: ‘Hello World!’;
}      ...
KSS Rules

evt-click-preventdefault: True;
KSS Rules
        #mykssbutton:click {
          evt-click-preventdefault: True;
          action-client: alert;
         ...
`
Client Actions
Client Actions
• alert
Client Actions
• alert
• addClass / removeClass / toggleClass
Client Actions
• alert
• addClass / removeClass / toggleClass
• setAttribute
Client Actions
• alert
• addClass / removeClass / toggleClass
• setAttribute
• setStyle
Client Actions
• alert
• addClass / removeClass / toggleClass
• setAttribute
• setStyle
• replaceInnerHTML
Client Actions
• alert
• addClass / removeClass / toggleClass
• setAttribute
• setStyle
• replaceInnerHTML
• log
Parameter Providers
Parameter Providers

• nodeAttr
Parameter Providers

• nodeAttr
• nodeContent
Parameter Providers

• nodeAttr
• nodeContent
• currentFormVar
Parameter Providers

• nodeAttr
• nodeContent
• currentFormVar
• formVar
Tying Them Together
Tying Them Together
<form id=quot;myFormquot;>
 <input name=quot;colorquot; />
 <input type=quot;submitquot; class=quot;su...
Tying Them Together
<form id=quot;myFormquot;>
 <input name=quot;colorquot; />
 <input type=quot;submitquot; class=quot;su...
Tying Them Together
<form id=quot;myFormquot;>
 <input name=quot;colorquot; />
 <input type=quot;submitquot; class=quot;su...
Fallback
Server Actions
Server Actions
#myForm .submit:click {
  evt-click-preventdefault: True;
  action-server: transformColor;
  transformColor...
Server Actions
#myForm .submit:click {
  evt-click-preventdefault: True;
  action-server: transformColor;
  transformColor...
Server Actions
#myForm .submit:click {
  evt-click-preventdefault: True;
  action-server: transformColor;
  transformColor...
Command Sets
•   Core                 •   Zope

    •   getCssSelector       •   refreshProvider

    •   addClass        ...
Registering KSS


• portal_kss registry
kssregistry.xml
<?xml version=quot;1.0quot;?>
   <object name=quot;portal_kssquot; meta_type=quot;KSS Registryquot;>
     ...
Debugging KSS
Debugging KSS

• Firebug
Debugging KSS

• Firebug
• Firebug
Debugging KSS

• Firebug
• Firebug
• Firebug
Debugging KSS

• Firebug
• Firebug
• Firebug
 • Or Firebug Lite (getfirebug.com/lite.html)
Debugging KSS

• KSS fails silently
• Turn on debugging in portal_javascript
• Messages will appear in Firebug console
Debugging KSS
Debugging KSS
• Firekiss
  http://kssproject.org/download/firekiss.xpi/
KSS Plugins
•   kss.plugin.cacheabilty   •   kss.plugin.jsmath

•   kss.plugin.cns           •   kss.plugin.timer

•   kss...
Documentation


• http://kssproject.org/
• http://plone.org/events/regional/plone-
  symposium-2008/skinning-with-plone-3
KSS Recap


• Simple, CSS-style syntax
• JavaScript, without the JavaScript
But here’s the thing...
Plone 4 won’t include
        KSS *




        * probably...
Dude.
WTF?
• It’s huge
• It’s huge
• Lack of adoption
• It’s huge
• Lack of adoption
• Sparse documentation
See http://dev.plone.org/plone/ticket/8809
           for more information.
This asshole
just wasted half
 an hour of my
      life...
Repeat after me...
“KSS is not going away.”
“It’s just becoming an
   add-on Product.”
“I can still use KSS as
   much as I’d like.”
“By including it in my buildout.”




        Buildout
“Or as a product dependency.”




       Product
“Really.”
jQuery
jQuery
jQuery

• DOM-focused
jQuery

• DOM-focused
• Simple syntax
jQuery

• DOM-focused
• Simple syntax
• Extensible
jQuery

• DOM-focused
• Simple syntax
• Extensible
• 19K
jQuery

• DOM-focused
• Simple syntax
• Extensible
• 19K
• Well-documented
Factory
Factory

• jQuery()
• $()
• jq()
Selectors
Selectors

• CSS
Selectors

• CSS
 • Tag name: jq(‘p’)
Selectors

• CSS
 • Tag name: jq(‘p’)
 • Id: jq(‘#itemId’)
Selectors

• CSS
 • Tag name: jq(‘p’)
 • Id: jq(‘#itemId’)
 • Class: jq(‘.someClass’)
Selectors

• CSS
 • Tag name: jq(‘p’)
 • Id: jq(‘#itemId’)
 • Class: jq(‘.someClass’)
• XPath: jq(‘a[title]’)
Pseudo-Selectors
Pseudo-Selectors
• :first and :last
Pseudo-Selectors
• :first and :last
• :not
  jq(‘#portal-column-content p:not(.discreet)’)
Pseudo-Selectors
• :first and :last
• :not
  jq(‘#portal-column-content p:not(.discreet)’)

• :first-child, :nth-child, and ...
Pseudo-Selectors
• :first and :last
• :not
  jq(‘#portal-column-content p:not(.discreet)’)

• :first-child, :nth-child, and ...
Sets
Sets

• “Implicit iteration”
Sets

• “Implicit iteration”
 • jq(‘#myDiv’).hide()
Sets

• “Implicit iteration”
 • jq(‘#myDiv’).hide()
 • jq(‘.someDivs’).hide()
Chaining
Chaining

• Operations always return a result
Chaining

• Operations always return a result
• So instead of...
  obj.doSomething()
  obj.doSomethingElse(‘blah’)
Chaining

• Operations always return a result
• So instead of...
  obj.doSomething()
  obj.doSomethingElse(‘blah’)

• We c...
Chaining
Chaining

• One one line...
  jq(‘#faq’).find(‘dd’).hide().end().find(‘dt’).click(function(){...});
Chaining

• One one line...
  jq(‘#faq’).find(‘dd’).hide().end().find(‘dt’).click(function(){...});


• Or several...
  jq('...
Events
Events
function foo(){
  alert(‘foo’);
}
window.onload = foo;
Events
function foo(){
  alert(‘foo’);
}
window.onload = foo;
function bar(){
  alert(‘bar’);
}
window.onload = bar;
Events
function foo(){
  alert(‘foo’);
}
window.onload = foo;
function bar(){
  alert(‘bar’);
}
window.onload = bar;
Events
Events


jq(document).ready(function) {
  alert(‘bar’);
});
Events
jq(document).ready(function) {
  alert(‘foo’);
});
    jq(document).ready(function) {
      alert(‘bar’);
    });
Events
jq(document).ready(function) {
  alert(‘foo’);
            jq(document).ready(function) {
});           alert(‘baz’...
Events
jq(document).ready(function) {
  alert(‘foo’);
            jq(document).ready(function) {
});           alert(‘baz’...
Events
jq(document).ready(function) {
  alert(‘foo’);
             jq(document).ready(function) {
});            alert(‘ba...
Events
jq(document).ready(function) {
  alert(‘foo’);
             jq(document).ready(function) {
});            alert(‘ba...
Events
         #mykssbutton:click {
           evt-click-preventdefault: True;
           action-client: alert;
         ...
Events
      jq(document).ready(function) {
        jq(‘#mykssbutton’).click(function(){
          alert(‘Hello World!);
 ...
Another Example

jq(document).ready(function() {
   jq(‘a[@href$=”.pdf”]’).addclass(‘pdflink’);
});

a.pdflink { background:...
AJAX Calls
AJAX Calls
#myForm .submit:click {
  evt-click-preventdefault: True;
  action-server: transformColor;
  transformColor-col...
AJAX Calls
#myForm .submit:click {
  evt-click-preventdefault: True;
  action-server: transformColor;
  transformColor-col...
AJAX Calls
jq_transformColor
  return color.upper()




jq(document).ready(function(){
    jq('#myForm').submit(function()...
jQuery in Plone

• Built into Plone 3.1+
• ‘jq’ instead of ‘$’
• Now use ‘jq’ method instead of ‘cssQuery’
  or ‘registerP...
Integrating jQuery
               Plugins
http://plugins.jquery.com/
Integrating jQuery
      Plugins
Integrating jQuery
          Plugins
• If the plugin uses ‘$’:
Integrating jQuery
          Plugins
• If the plugin uses ‘$’:
 • Place it between jquery.js and jquery-
    integration.j...
Integrating jQuery
          Plugins
• If the plugin uses ‘$’:
 • Place it between jquery.js and jquery-
    integration.j...
Existing Behaviors
Collapsible Sections
<dl class=”collapsible”>
  <dt class=”collapsibleHeader”>
    A Title
  </dt>
  <dd class=”collapsibl...
Collapsible Sections
<dl class=”collapsible”>
  <dt class=”collapsibleHeader”>
    A Title
  </dt>
  <dd class=”collapsibl...
Dropdowns
<dl class=”actionMenu” id=”aUniqueId”>
  <dt class=”actionMenuHeader”>
      <a href=”some_destination”>A Title<...
Dropdowns
<dl class=”actionMenu” id=”aUniqueId”>
  <dt class=”actionMenuHeader”>
      <a href=”some_destination”>A Title<...
Sortable Tables
<table class=”listing” id=”someId”>
  <thead>
     <tr>
        <th>Table header</th>
     </tr>
  </thead...
Sortable Tables
<table class=”listing” id=”someId”>
  <thead>
     <tr>
        <th>Table header</th>
     </tr>
  </thead...
Sortable Tables

<th class=”nosort”>
  Unsortable Column
</th>
Sortable Tables

<th class=”nosort”>
  Unsortable Column
</th>
Products using jQuery
collective.flowplayer
                Turns code like:
<a class=quot;autoFlowPlayerquot; href=quot;path/to/video-file.flvquot...
Deco
Deco
Others

• Carousel
• Plone Slimbox
• FancyZoomView
• FadingNavigation
jQuery Documentation

http://docs.jquery.com/
http://www.packtpub.com/jQuery/
Other Libraries

• Yahoo User Interface (YUI)
• extjs
• MochiKit
• MooTools
• Dojo
Conclusions
• JavaScript novice? – Use KSS
• JavaScript novice? – Use KSS
• Otherwise, use JQuery
Special thanks to...

•   David Glick
    http://wglick.org/jstalk.html

•   Joel Burton
    http://bit.ly/z1nLH
http://bit.ly/plone-interactivity
Questions?
http://bit.ly/plone-interactivity
Plone Interactivity
Plone Interactivity
Plone Interactivity
Upcoming SlideShare
Loading in...5
×

Plone Interactivity

1,477

Published on

A look at using KSS and jQuery in your Plone site.

Published in: Technology
0 Comments
5 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
1,477
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
46
Comments
0
Likes
5
Embeds 0
No embeds

No notes for slide












  • So what kind of stuff are we going to talk about?

    We&#x2019;ll get the boring stuff out of the way first

    and then we&#x2019;ll move on to more interesting things like KSS and jQuery.

    There&#x2019;s a lot to cover, we&#x2019;ll hold questions until the end. And any of these slides that I don&#x2019;t get to, I&#x2019;ll just cut out of Plone 4.


  • So what kind of stuff are we going to talk about?

    We&#x2019;ll get the boring stuff out of the way first

    and then we&#x2019;ll move on to more interesting things like KSS and jQuery.

    There&#x2019;s a lot to cover, we&#x2019;ll hold questions until the end. And any of these slides that I don&#x2019;t get to, I&#x2019;ll just cut out of Plone 4.




  • You might use a block of code like this to include a script in your Plone template if

    you only need it on one page or one template

    or performance isn&#x2019;t really something you&#x2019;re concerned about.
  • You might use a block of code like this to include a script in your Plone template if

    you only need it on one page or one template

    or performance isn&#x2019;t really something you&#x2019;re concerned about.
  • Plone, however, provides Resource Registries for keeping track of things like CSS, JavaScript, and KSS.
  • So what makes resource registries useful?

    Control which scripts are included on a page and in what order they&#x2019;ll render.

    It gives you automatic merging and compression of your scripts.

    And makes JavaScript more cacheable.
  • So what makes resource registries useful?

    Control which scripts are included on a page and in what order they&#x2019;ll render.

    It gives you automatic merging and compression of your scripts.

    And makes JavaScript more cacheable.
  • So what makes resource registries useful?

    Control which scripts are included on a page and in what order they&#x2019;ll render.

    It gives you automatic merging and compression of your scripts.

    And makes JavaScript more cacheable.
  • And its easy for third-party products to add scripts to portal_javascript using Generic Setup.

    We can control all of the settings from here and even specify where it should wind up in the ordering.
  • And now, in Plone 3.3, we can use portal_javascript to include JavaScript being hosted on external servers.
  • And now, in Plone 3.3, we can use portal_javascript to include JavaScript being hosted on external servers.
  • And now, in Plone 3.3, we can use portal_javascript to include JavaScript being hosted on external servers.
  • And that&#x2019;s handy because of sites like Google&#x2019;s AJAX libraries API.

    Google hosts the libraries, you pull them into your site as needed. They&#x2019;ve got a number of the more-popular JavaScript libraries.
  • And that&#x2019;s handy because of sites like Google&#x2019;s AJAX libraries API.

    Google hosts the libraries, you pull them into your site as needed. They&#x2019;ve got a number of the more-popular JavaScript libraries.



  • These can be registered with the resource registries in the same manner as before. This time we&#x2019;ll use that ++resource++ path to point to the file we&#x2019;re after.
  • We can also register these to be available only for specific content types or browser layers so that they&#x2019;ll only appear in certain portions of a site.
  • We can also register these to be available only for specific content types or browser layers so that they&#x2019;ll only appear in certain portions of a site.
  • There&#x2019;s also a resourceDirectory ZCML directive that will allow you to register an entire folder of files, rather than registering each one individually.

    The url would look like this.
  • There&#x2019;s also a resourceDirectory ZCML directive that will allow you to register an entire folder of files, rather than registering each one individually.

    The url would look like this.
  • So, what are Javascript libraries?

  • So why might we use one?

    It&#x2019;s faster. You&#x2019;ve got better things to do with your time.

    Chances are someone&#x2019;s already done what you want to do, and done it better.

    It removes the need to know everything there is to know about writing JavaScript.

    And you can largely ignore browser compatibility issues.



  • So why might we use one?

    It&#x2019;s faster. You&#x2019;ve got better things to do with your time.

    Chances are someone&#x2019;s already done what you want to do, and done it better.

    It removes the need to know everything there is to know about writing JavaScript.

    And you can largely ignore browser compatibility issues.



  • So why might we use one?

    It&#x2019;s faster. You&#x2019;ve got better things to do with your time.

    Chances are someone&#x2019;s already done what you want to do, and done it better.

    It removes the need to know everything there is to know about writing JavaScript.

    And you can largely ignore browser compatibility issues.



  • So why might we use one?

    It&#x2019;s faster. You&#x2019;ve got better things to do with your time.

    Chances are someone&#x2019;s already done what you want to do, and done it better.

    It removes the need to know everything there is to know about writing JavaScript.

    And you can largely ignore browser compatibility issues.



  • So let&#x2019;s start with Plone&#x2019;s built-in library, KSS.

  • This is what a typical KSS rule might look like. Note that it looks very much like something we&#x2019;d see in a stylesheet.




  • One of the things you&#x2019;ll use most when writing KSS rules will be this bit.

    Essentially, we&#x2019;re telling KSS that we want to ignore the default click event of whatever element the rule applies to.
  • This means that if our rule applies to this link, we&#x2019;ll ignore the default action &#x2013;&#xA0;actually going to the front-page &#x2013;&#xA0;and instead apply our own actions, which in this case would be popping up a &#x201C;Hello World!&#x201D; alert message.
  • And that&#x2019;d look something like this.
  • So in that example, we used the &#x201C;alert&#x201D; client action. There are others we can use too.
  • So in that example, we used the &#x201C;alert&#x201D; client action. There are others we can use too.
  • So in that example, we used the &#x201C;alert&#x201D; client action. There are others we can use too.
  • So in that example, we used the &#x201C;alert&#x201D; client action. There are others we can use too.
  • So in that example, we used the &#x201C;alert&#x201D; client action. There are others we can use too.
  • So in that example, we used the &#x201C;alert&#x201D; client action. There are others we can use too.
  • Parameter providers let us pull data out of the current page so that we can use it in an action.

    NodeAttr lets you pull out the value of an HTML attribute of the selected element.

    NodeContent will return the text content of the selected element.

    currentFormVar and formVar pull values out of form fields.
  • Parameter providers let us pull data out of the current page so that we can use it in an action.

    NodeAttr lets you pull out the value of an HTML attribute of the selected element.

    NodeContent will return the text content of the selected element.

    currentFormVar and formVar pull values out of form fields.
  • Parameter providers let us pull data out of the current page so that we can use it in an action.

    NodeAttr lets you pull out the value of an HTML attribute of the selected element.

    NodeContent will return the text content of the selected element.

    currentFormVar and formVar pull values out of form fields.
  • Parameter providers let us pull data out of the current page so that we can use it in an action.

    NodeAttr lets you pull out the value of an HTML attribute of the selected element.

    NodeContent will return the text content of the selected element.

    currentFormVar and formVar pull values out of form fields.
  • So let&#x2019;s tie all of these concepts together.

    I&#x2019;ve created a simple html page with a form that accepts a color name and has a submit button. There&#x2019;s also a div that will tell the user what they&#x2019;ve just typed in. Complex stuff, I know.

    The KSS is rule says that when the user clicks on the submit button in our form, it&#x2019;ll ignore the default action (which would be to submit the form) and instead replace the content of the page element with the id #selectedColor with the value of the color field.

    Let&#x2019;s see this in action.
  • So let&#x2019;s tie all of these concepts together.

    I&#x2019;ve created a simple html page with a form that accepts a color name and has a submit button. There&#x2019;s also a div that will tell the user what they&#x2019;ve just typed in. Complex stuff, I know.

    The KSS is rule says that when the user clicks on the submit button in our form, it&#x2019;ll ignore the default action (which would be to submit the form) and instead replace the content of the page element with the id #selectedColor with the value of the color field.

    Let&#x2019;s see this in action.
  • So let&#x2019;s tie all of these concepts together.

    I&#x2019;ve created a simple html page with a form that accepts a color name and has a submit button. There&#x2019;s also a div that will tell the user what they&#x2019;ve just typed in. Complex stuff, I know.

    The KSS is rule says that when the user clicks on the submit button in our form, it&#x2019;ll ignore the default action (which would be to submit the form) and instead replace the content of the page element with the id #selectedColor with the value of the color field.

    Let&#x2019;s see this in action.
  • So what happens if JavaScript is disabled for whatever reason?

    KSS makes sure that if that&#x2019;s the case, then the click event override we&#x2019;ve created doesn&#x2019;t get registered and the page will fall back to its default functionality.

    So disabling javascript on this page, reloading and trying again, we get the same result.

    KSS makes this very easy, with only a minimal amount of planning on my part.
  • Alright, now let&#x2019;s get into some actual AJAX. Doing things on the client side is fine, but what if we want to do something a bit more involved with the data.

    So in this example, I&#x2019;m going to switch to using action-server. We&#x2019;ll call a python script I&#x2019;ve created. This could also be a browser view if we were creating this as a filesystem product.

    The script is expecting a parameter named &#x201C;color&#x201D;, so we&#x2019;ll need to pass that along as well.

    Looking at the script itself, there&#x2019;s a lot of boilerplate that you don&#x2019;t really need to be concerned about. Just trust that it&#x2019;s there for a reason and move on with your life. The important bits are the remaining three lines. First of all, we&#x2019;ll do a simple bit of Python and change the color name the script has received into uppercase. Next we&#x2019;ll grab the core kss commandset. Core essentially holds all of those action-client methods we could call directly from KSS, and provides them to Python. So in this case, we&#x2019;re going to replace the inner HTML of the element with the selectedColor id with the capitalized color name.

    Looking at this in action, we see that providing &#x2018;blue&#x2019; displays it in the page in uppercase, all without reloading the page.
  • Alright, now let&#x2019;s get into some actual AJAX. Doing things on the client side is fine, but what if we want to do something a bit more involved with the data.

    So in this example, I&#x2019;m going to switch to using action-server. We&#x2019;ll call a python script I&#x2019;ve created. This could also be a browser view if we were creating this as a filesystem product.

    The script is expecting a parameter named &#x201C;color&#x201D;, so we&#x2019;ll need to pass that along as well.

    Looking at the script itself, there&#x2019;s a lot of boilerplate that you don&#x2019;t really need to be concerned about. Just trust that it&#x2019;s there for a reason and move on with your life. The important bits are the remaining three lines. First of all, we&#x2019;ll do a simple bit of Python and change the color name the script has received into uppercase. Next we&#x2019;ll grab the core kss commandset. Core essentially holds all of those action-client methods we could call directly from KSS, and provides them to Python. So in this case, we&#x2019;re going to replace the inner HTML of the element with the selectedColor id with the capitalized color name.

    Looking at this in action, we see that providing &#x2018;blue&#x2019; displays it in the page in uppercase, all without reloading the page.
  • Alright, now let&#x2019;s get into some actual AJAX. Doing things on the client side is fine, but what if we want to do something a bit more involved with the data.

    So in this example, I&#x2019;m going to switch to using action-server. We&#x2019;ll call a python script I&#x2019;ve created. This could also be a browser view if we were creating this as a filesystem product.

    The script is expecting a parameter named &#x201C;color&#x201D;, so we&#x2019;ll need to pass that along as well.

    Looking at the script itself, there&#x2019;s a lot of boilerplate that you don&#x2019;t really need to be concerned about. Just trust that it&#x2019;s there for a reason and move on with your life. The important bits are the remaining three lines. First of all, we&#x2019;ll do a simple bit of Python and change the color name the script has received into uppercase. Next we&#x2019;ll grab the core kss commandset. Core essentially holds all of those action-client methods we could call directly from KSS, and provides them to Python. So in this case, we&#x2019;re going to replace the inner HTML of the element with the selectedColor id with the capitalized color name.

    Looking at this in action, we see that providing &#x2018;blue&#x2019; displays it in the page in uppercase, all without reloading the page.
  • We saw that &#x201C;getCommandSet&#x201D; line... there are 3 different command sets that come with KSS out of the box. I mentioned &#x2018;core&#x2019;, that essentially provides all of the action-client calls available in our KSS rules.

    The second is the Zope command set, which allows us to redraw providers -- things like viewlet managers and portlet managers -- or to redraw individual viewlets.

    Third is the Plone command set, which lets us do things like display portal messages or refresh portlets.

    All of these let us interact with the browser window from Python. So KSS has allowed JavaScript to talk to Python and Python to talk to JavaScript.
  • So how do we register a kss file?

    We&#x2019;re going to use one of the resource registries we talked about earlier -- portal_kss.
  • Typically, that&#x2019;s done from GenericSetup, using an kssregistry.xml file.

    This looks almost exactly like the JavaScript registration we looked at earlier.
  • So how do you go about debugging KSS?

    Firebug. Firebug. Firebug.

    Or, if you&#x2019;re debugging in something that&#x2019;s not Firefox, try Firebug Lite.
  • So how do you go about debugging KSS?

    Firebug. Firebug. Firebug.

    Or, if you&#x2019;re debugging in something that&#x2019;s not Firefox, try Firebug Lite.
  • So how do you go about debugging KSS?

    Firebug. Firebug. Firebug.

    Or, if you&#x2019;re debugging in something that&#x2019;s not Firefox, try Firebug Lite.
  • So how do you go about debugging KSS?

    Firebug. Firebug. Firebug.

    Or, if you&#x2019;re debugging in something that&#x2019;s not Firefox, try Firebug Lite.
  • A side-effect of KSS&#x2019;s no-javascript failover is that if there&#x2019;s an error, your page&#x2019;s KSS will fail to load. Which means that it will typically fail silently. So, essentially, all of the KSS functionality on that page will fail to work if there is a syntax error. I spent a day chasing down what turned out to be a missing semicolon in my ruleset.

    But, if you turn on javascript debugging in portal_javascript, KSS debugging information will appear in Firebug&#x2019;s console window. And there&#x2019;s a lot of it.

  • And this is what that debugging output will look like. Firebug will show all of the rules being processed, any XMLRPC calls being passed around, and detailed error messages that may crop up.
  • There&#x2019;s also &#x201C;Firekiss&#x201D;, a Firebug extension from the KSS folks. It&#x2019;ll show your KSS rules and allow you to change debug mode and log level from within Firebug.
  • There are a number of KSS addons available.

    kss.plugin.cacheability which enables KSS requests to be cached.
    kss.plugin.cns provides a number of new client actions.
    kss.plugin.fadeeffect adds image fading and substitution.
    kss.plugin.history allows you to modify the browser location in an effort to add backward and forward navigation to a javascript application.
    kss.plugin.jsmath uses the jsmath library to display complex mathematical functions.
    kss.plugin.timer displays a timer.
    kss.plugin.yuidnd integrates the Yahoo User Interface drag-and-drop library.

  • Quick recap: What is KSS?

    It uses a simple, css-style syntax.
    Basically, it&#x2019;s Javascript, without the JavaScript, which for someone like me who hates writing Javascript, is really awesome.

  • As of Plone 4, we&#x2019;ll probably no longer be shipping with KSS.

  • Why?
  • So here&#x2019;s why...
    It&#x2019;s huge. The relevant scripts check in at over 100 k.
    It&#x2019;s been slow to catch on, both within and outside of the Plone project.
    And the documentation is somewhat lacking.
  • So here&#x2019;s why...
    It&#x2019;s huge. The relevant scripts check in at over 100 k.
    It&#x2019;s been slow to catch on, both within and outside of the Plone project.
    And the documentation is somewhat lacking.
  • So here&#x2019;s why...
    It&#x2019;s huge. The relevant scripts check in at over 100 k.
    It&#x2019;s been slow to catch on, both within and outside of the Plone project.
    And the documentation is somewhat lacking.
  • And you can read PLIP #8809 for more information about that decision.
  • So right now, you&#x2019;re thinking, &#x201C;This asshole just wasted half an hour of my life.&#x201D;






  • By including it in my buildout.

  • I really like KSS and relied on it heavily to build GloWorm. I think I made it about 90% of the way through to the final release before I actually had to write a line of JavaScript code myself, KSS handled the large majority of what I wanted to do.

    The current direction that Plone core development is taking is to make Plone itself ship as a much leaner product. So just because we&#x2019;re not planning to ship with KSS, that doesn&#x2019;t mean that you can&#x2019;t and shouldn&#x2019;t still use it...
  • So that brings us to jQuery, the JavaScript library with which we&#x2019;ll be replacing KSS functionality in Plone core.
  • So why use jQuery

    It&#x2019;s a DOM-focused library, and by that, I mean that it&#x2019;s less of a &#x201C;make JavaScript a real language&#x201D; library, and more of a &#x201C;get stuff done&#x201D; library.

    It&#x2019;s got a very simple syntax.

    It&#x2019;s easy to add on to the core functionality and there are lots of plugins available that do so.

    It&#x2019;s quite small.

    And there&#x2019;s a large amount of good documentation.

  • So why use jQuery

    It&#x2019;s a DOM-focused library, and by that, I mean that it&#x2019;s less of a &#x201C;make JavaScript a real language&#x201D; library, and more of a &#x201C;get stuff done&#x201D; library.

    It&#x2019;s got a very simple syntax.

    It&#x2019;s easy to add on to the core functionality and there are lots of plugins available that do so.

    It&#x2019;s quite small.

    And there&#x2019;s a large amount of good documentation.

  • So why use jQuery

    It&#x2019;s a DOM-focused library, and by that, I mean that it&#x2019;s less of a &#x201C;make JavaScript a real language&#x201D; library, and more of a &#x201C;get stuff done&#x201D; library.

    It&#x2019;s got a very simple syntax.

    It&#x2019;s easy to add on to the core functionality and there are lots of plugins available that do so.

    It&#x2019;s quite small.

    And there&#x2019;s a large amount of good documentation.

  • So why use jQuery

    It&#x2019;s a DOM-focused library, and by that, I mean that it&#x2019;s less of a &#x201C;make JavaScript a real language&#x201D; library, and more of a &#x201C;get stuff done&#x201D; library.

    It&#x2019;s got a very simple syntax.

    It&#x2019;s easy to add on to the core functionality and there are lots of plugins available that do so.

    It&#x2019;s quite small.

    And there&#x2019;s a large amount of good documentation.

  • So why use jQuery

    It&#x2019;s a DOM-focused library, and by that, I mean that it&#x2019;s less of a &#x201C;make JavaScript a real language&#x201D; library, and more of a &#x201C;get stuff done&#x201D; library.

    It&#x2019;s got a very simple syntax.

    It&#x2019;s easy to add on to the core functionality and there are lots of plugins available that do so.

    It&#x2019;s quite small.

    And there&#x2019;s a large amount of good documentation.

  • The basis of all jQuery calls is known as the jQuery factory function.

    Typically shortened as a dollar sign.

    In Plone, it&#x2019;s aliased as &#x201C;jq&#x201D;. We&#x2019;ll get into that a bit more later, but for now all of my examples will use the &#x201C;jq&#x201D; alias since that&#x2019;s what you can expect to use when writing scripts for Plone.
  • We can pass all sorts of selectors to the factory function. Most of the time, we&#x2019;ll use a CSS selector, like a tag, id, or class.

    But can also use XPath statements to select particular elements of the HTML document we&#x2019;re interacting with.
  • We can pass all sorts of selectors to the factory function. Most of the time, we&#x2019;ll use a CSS selector, like a tag, id, or class.

    But can also use XPath statements to select particular elements of the HTML document we&#x2019;re interacting with.
  • We can pass all sorts of selectors to the factory function. Most of the time, we&#x2019;ll use a CSS selector, like a tag, id, or class.

    But can also use XPath statements to select particular elements of the HTML document we&#x2019;re interacting with.
  • We can pass all sorts of selectors to the factory function. Most of the time, we&#x2019;ll use a CSS selector, like a tag, id, or class.

    But can also use XPath statements to select particular elements of the HTML document we&#x2019;re interacting with.
  • We can pass all sorts of selectors to the factory function. Most of the time, we&#x2019;ll use a CSS selector, like a tag, id, or class.

    But can also use XPath statements to select particular elements of the HTML document we&#x2019;re interacting with.
  • jQuery is fully CSS3 compliant, so we get all sorts of new CSS pseudo-selectors.

    We can select the first or last instance of a selector.

    We can get content that does not meet certain criteria.

    We can get the first, last, or nnth element of a group.

    And also defines some of its own (:even/:odd).
  • jQuery is fully CSS3 compliant, so we get all sorts of new CSS pseudo-selectors.

    We can select the first or last instance of a selector.

    We can get content that does not meet certain criteria.

    We can get the first, last, or nnth element of a group.

    And also defines some of its own (:even/:odd).
  • jQuery is fully CSS3 compliant, so we get all sorts of new CSS pseudo-selectors.

    We can select the first or last instance of a selector.

    We can get content that does not meet certain criteria.

    We can get the first, last, or nnth element of a group.

    And also defines some of its own (:even/:odd).
  • jQuery is fully CSS3 compliant, so we get all sorts of new CSS pseudo-selectors.

    We can select the first or last instance of a selector.

    We can get content that does not meet certain criteria.

    We can get the first, last, or nnth element of a group.

    And also defines some of its own (:even/:odd).
  • Whether our selector finds one, many, or no page elements, jQuery&#x2019;s &#x201C;Implicit Iteration&#x201D; means that methods assume that the selector&#x2019;s result is a set of elements and will run accordingly. That lets us call methods without worrying about the number of results.
  • Whether our selector finds one, many, or no page elements, jQuery&#x2019;s &#x201C;Implicit Iteration&#x201D; means that methods assume that the selector&#x2019;s result is a set of elements and will run accordingly. That lets us call methods without worrying about the number of results.
  • Whether our selector finds one, many, or no page elements, jQuery&#x2019;s &#x201C;Implicit Iteration&#x201D; means that methods assume that the selector&#x2019;s result is a set of elements and will run accordingly. That lets us call methods without worrying about the number of results.
  • Operations on an element will always return a result. That result could be the same element, another element, or a set of elements.

    So instead of calling methods on an object over and over.

    We can chain commands, performing operations on the previous command&#x2019;s result.
  • Operations on an element will always return a result. That result could be the same element, another element, or a set of elements.

    So instead of calling methods on an object over and over.

    We can chain commands, performing operations on the previous command&#x2019;s result.
  • Operations on an element will always return a result. That result could be the same element, another element, or a set of elements.

    So instead of calling methods on an object over and over.

    We can chain commands, performing operations on the previous command&#x2019;s result.
  • We can chain as much as we like, doing it all on one line,

    or several. Splitting it across several lines allows us to provide much more in the way of comments.
  • We can chain as much as we like, doing it all on one line,

    or several. Splitting it across several lines allows us to provide much more in the way of comments.
  • Typically, in JavaScript land, we might define a function that would run once the page has finished loading.

    The problem is that if we do it this way, each new function we add

    would overwrite the others and prevent them from running.
  • Typically, in JavaScript land, we might define a function that would run once the page has finished loading.

    The problem is that if we do it this way, each new function we add

    would overwrite the others and prevent them from running.
  • Typically, in JavaScript land, we might define a function that would run once the page has finished loading.

    The problem is that if we do it this way, each new function we add

    would overwrite the others and prevent them from running.
  • Using jQuery, we could rewrite the this function like this, by assigning a new function to jQuery&#x2019;s document ready event.

    And since jQuery implements an event queue, we can do this

    as many

    times as

    we&#x2019;d like without worrying that registering one will override another.
  • Using jQuery, we could rewrite the this function like this, by assigning a new function to jQuery&#x2019;s document ready event.

    And since jQuery implements an event queue, we can do this

    as many

    times as

    we&#x2019;d like without worrying that registering one will override another.
  • Using jQuery, we could rewrite the this function like this, by assigning a new function to jQuery&#x2019;s document ready event.

    And since jQuery implements an event queue, we can do this

    as many

    times as

    we&#x2019;d like without worrying that registering one will override another.
  • Using jQuery, we could rewrite the this function like this, by assigning a new function to jQuery&#x2019;s document ready event.

    And since jQuery implements an event queue, we can do this

    as many

    times as

    we&#x2019;d like without worrying that registering one will override another.
  • Using jQuery, we could rewrite the this function like this, by assigning a new function to jQuery&#x2019;s document ready event.

    And since jQuery implements an event queue, we can do this

    as many

    times as

    we&#x2019;d like without worrying that registering one will override another.
  • Using jQuery, we could rewrite the this function like this, by assigning a new function to jQuery&#x2019;s document ready event.

    And since jQuery implements an event queue, we can do this

    as many

    times as

    we&#x2019;d like without worrying that registering one will override another.
  • If you remember back to that first KSS action we wrote, we created an action that overrode a link with the id &#x201C;mykssbutton&#x201D; and instead popped up a &#x201C;hello world&#x201D; alert box.

    Let&#x2019;s rewrite that in jQuery....
  • So it&#x2019;ll look like this...

    We&#x2019;ll use the document ready event to signal that a new function should be assigned to click event of the element with an id of #mykssbutton. That new function will present an alert window stating &#x201C;Hello World!&#x201D;


  • Example... create a script that adds a pdf icon to all links on the page pointing to pdfs.


  • And we can do our AJAX calls too.

    This was our KSS AJAX script.
  • And we can do our AJAX calls too.

    This was our KSS AJAX script.
  • And this is how it&#x2019;d look in jQuery.

    Our python script is going to be a lot simpler, now it just returns the data we&#x2019;re after instead of handling the changing of the page content.

    Then, we&#x2019;ll tell the selectedColor element to load the response from our jq_transformColor script.

    Our jQuery will override the submit method of the form. We&#x2019;ll grab the value of the color field.



    The last line is a bit of voodoo... In order to prevent the form from actually submitting, we return a false value. If we returned true, it&#x2019;d go ahead and complete the action.
  • jQuery core is now included with Plone as of version 3.1.

    Plone uses jq instead of the dollar sign shortcut to avoid potential conflicts with other javascript libraries.

    And if you&#x2019;ve been using &#x2018;cssQuery&#x2019; or &#x2018;registerPloneFunction&#x2019;, you should now be using &#x2018;jq&#x2019; instead.
  • There are over a thousand jQuery plugins available to handle things like animation, layout, forms, and AJAX.


  • So how might you integrate one of these into Plone?

    If the plugin uses the &#x2018;$&#x2019; factory alias, you&#x2019;ll need to place it between jquery.js and jquery-integration.js in the portal_javascript registry. Using GenericSetup, that&#x2019;d look something like this -- specifying that the script should be inserted before jquery-integration.js in the ordering.

    Or you can wrap it in a function like this.
  • So how might you integrate one of these into Plone?

    If the plugin uses the &#x2018;$&#x2019; factory alias, you&#x2019;ll need to place it between jquery.js and jquery-integration.js in the portal_javascript registry. Using GenericSetup, that&#x2019;d look something like this -- specifying that the script should be inserted before jquery-integration.js in the ordering.

    Or you can wrap it in a function like this.
  • So how might you integrate one of these into Plone?

    If the plugin uses the &#x2018;$&#x2019; factory alias, you&#x2019;ll need to place it between jquery.js and jquery-integration.js in the portal_javascript registry. Using GenericSetup, that&#x2019;d look something like this -- specifying that the script should be inserted before jquery-integration.js in the ordering.

    Or you can wrap it in a function like this.
  • So let&#x2019;s look at some existing behaviors that Plone provides through jQuery...




  • And there are quite a few Plone products currently making use of jQuery.
  • collective.flowplayer will turn links to flash videos or mp3s into inline players.
  • Deco, which will be the content layout and editing tool at some point in Plone&#x2019;s future makes extensive use of jQuery to handle layout and interaction.

  • jQuery has some excellent documentation available. I highly recommend checking it out.
  • I&#x2019;m a fan of the &#x201C;Learning jQuery&#x201D; book from Packt.
  • And I just want to mention a few other libraries that you can use with Plone. They&#x2019;re each focused on different tasks or approaches to writing interactive applications.

  • If you&#x2019;re not comfortable with JavaScript or are pressed for time, KSS is the way to go.
    Otherwise, your best bet is to use JQuery.
  • If you&#x2019;re not comfortable with JavaScript or are pressed for time, KSS is the way to go.
    Otherwise, your best bet is to use JQuery.


  • Any questions?
  • Transcript of "Plone Interactivity"

    1. 1. Adding Interactivity to your Plone Site http://bit.ly/plone-interactivity
    2. 2. Me
    3. 3. Me • Eric Steele
    4. 4. Me • Eric Steele • Programmer @ WebLion
    5. 5. Me • Eric Steele • Programmer @ WebLion • Plone Products
    6. 6. Me • Eric Steele • Programmer @ WebLion • Plone Products • Faculty/Staff Directory
    7. 7. Me • Eric Steele • Programmer @ WebLion • Plone Products • Faculty/Staff Directory • GloWorm
    8. 8. Me • Eric Steele • Programmer @ WebLion • Plone Products • Faculty/Staff Directory • GloWorm • Plone 4 release manager
    9. 9. Adding Interactivity to your Plone Site
    10. 10. or...
    11. 11. I Suck at JavaScript... and So Can You
    12. 12. I Suck at JavaScript... and So Can You ~
    13. 13. I Suck at JavaScript... and So Can You ~ A half-assed guide to interactive JavaScript
    14. 14. The Stuff
    15. 15. The Stuff • The Boring Stuff • Resources
    16. 16. The Stuff • The Boring Stuff • The Fun Stuff • Resources • KSS • jQuery
    17. 17. Resources
    18. 18. Resources • JavaScript • CSS • KSS
    19. 19. Including JavaScripts <tal:block metal:fill-slot=quot;javascript_head_slotquot;> <script type=quot;text/javascriptquot; src=quot;myscript.jsquot; /> </tal:block>
    20. 20. Including JavaScripts <tal:block metal:fill-slot=quot;javascript_head_slotquot;> <script type=quot;text/javascriptquot; src=quot;myscript.jsquot; /> </tal:block> • Only need script on one page/template
    21. 21. Including JavaScripts <tal:block metal:fill-slot=quot;javascript_head_slotquot;> <script type=quot;text/javascriptquot; src=quot;myscript.jsquot; /> </tal:block> • Only need script on one page/template • Performance isn’t an issue
    22. 22. Resource Registries
    23. 23. Resource Registries
    24. 24. Resource Registries • Control inclusion, order
    25. 25. Resource Registries • Control inclusion, order • Automatic merging, compression
    26. 26. Resource Registries • Control inclusion, order • Automatic merging, compression • Improved cacheability
    27. 27. Generic Setup jsregistry.xml <?xml version=quot;1.0quot;?> <object name=quot;portal_javascriptsquot;> <javascript cacheable=quot;Truequot; compression=quot;safequot; cookable=quot;Truequot; enabled=quot;Truequot; expression=quot;quot; id=quot;myscripts.jsquot; inline=quot;Falsequot; insert-after=quot;dropdown.jsquot;/> </object>
    28. 28. External Scripts
    29. 29. External Scripts <javascript ... id=quot;http://ajax.googleapis.com/ ajax/libs/jquery/1.2.6/jquery.min.jsquot; ... />
    30. 30. External Scripts <javascript ... id=quot;http://ajax.googleapis.com/ ajax/libs/jquery/1.2.6/jquery.min.jsquot; ... /> • New in Plone 3.3
    31. 31. External Scripts <javascript ... id=quot;http://ajax.googleapis.com/ ajax/libs/jquery/1.2.6/jquery.min.jsquot; ... /> • New in Plone 3.3 • Include JavaScript from external sources
    32. 32. Google AJAX Libraries
    33. 33. Google AJAX Libraries http://code.google.com/apis/ajaxlibs/
    34. 34. Google AJAX Libraries http://code.google.com/apis/ajaxlibs/ • jQuery • Dojo • jQuery UI • SWFObjectNew • Prototype • Yahoo! User Interface Library (YUI) • script.aculo.us • MooTools
    35. 35. Browser Resources • Zope 3 concept •
    36. 36. Browser Resources
    37. 37. Browser Resources <browser:resource name=quot;myscript.jsquot; image=quot;myscript.jsquot; />
    38. 38. Browser Resources <browser:resource name=quot;myscript.jsquot; image=quot;myscript.jsquot; /> http://myplonesite/++resource++myscript.js
    39. 39. Browser Resources <?xml version=quot;1.0quot;?> jsregistry.xml <object name=quot;portal_javascriptsquot;> <javascript cacheable=quot;Truequot; compression=quot;safequot; cookable=quot;Truequot; enabled=quot;Truequot; expression=quot;quot; id=quot;++resource++myscript.jsquot; inline=quot;Falsequot; insert-after=quot;dropdown.jsquot;/> </object>
    40. 40. Browser Resources
    41. 41. Browser Resources <browser:resource name=quot;myscript.jsquot; image=quot;myscript.jsquot; for=quot;..interfaces.IMyCustomType” />
    42. 42. Browser Resources <browser:resource name=quot;myscript.jsquot; image=quot;myscript.jsquot; for=quot;..interfaces.IMyCustomType” /> <browser:resource name=quot;myscript.jsquot; image=quot;myscript.jsquot; layer=quot;..interfaces.IMySkinLayer” />
    43. 43. Browser Resources
    44. 44. Browser Resources <browser:resourceDirectory name=quot;myResourcesquot; directory=quot;stuffquot; />
    45. 45. Browser Resources <browser:resourceDirectory name=quot;myResourcesquot; directory=quot;stuffquot; /> http://myplonesite/++resource++myResources/ checkout.png
    46. 46. JavaScript Libraries
    47. 47. A JavaScript library is a library of pre-written JavaScript controls which allow for easier development of JavaScript-based applications, especially for AJAX and other web-centric technologies. http://en.wikipedia.org/wiki/JavaScript_library
    48. 48. Why?
    49. 49. Why? • Save time
    50. 50. Why? • Save time • Reuse existing code
    51. 51. Why? • Save time • Reuse existing code • You don’t need to be an expert
    52. 52. Why? • Save time • Reuse existing code • You don’t need to be an expert • Browser “quirks”
    53. 53. KSS
    54. 54. KSS • “Kinetic Style Sheets” • JavaScript/AJAX framework • Declare behaviors using CSS format • Built into Plone 3.0+ • Also works with Grok, Pylons, Django
    55. 55. KSS Rules #mykssbutton:click { action-client: alert; alert-message: ‘Hello World!’; }
    56. 56. KSS Rules CSS Selector #mykssbutton:click { action-client: alert; alert-message: ‘Hello World!’; }
    57. 57. KSS Rules Action Type #mykssbutton:click { action-client: alert; alert-message: ‘Hello World!’; }
    58. 58. KSS Rules Action #mykssbutton:click { action-client: alert; alert-message: ‘Hello World!’; }
    59. 59. KSS Rules Action Parameter #mykssbutton:click { action-client: alert; alert-message: ‘Hello World!’; } name value
    60. 60. KSS Rules evt-click-preventdefault: True;
    61. 61. KSS Rules #mykssbutton:click { evt-click-preventdefault: True; action-client: alert; alert-message: ‘Hello World!’; } <a href=”front-page” id=”mykssbutton”>Click me! </a>
    62. 62. `
    63. 63. Client Actions
    64. 64. Client Actions • alert
    65. 65. Client Actions • alert • addClass / removeClass / toggleClass
    66. 66. Client Actions • alert • addClass / removeClass / toggleClass • setAttribute
    67. 67. Client Actions • alert • addClass / removeClass / toggleClass • setAttribute • setStyle
    68. 68. Client Actions • alert • addClass / removeClass / toggleClass • setAttribute • setStyle • replaceInnerHTML
    69. 69. Client Actions • alert • addClass / removeClass / toggleClass • setAttribute • setStyle • replaceInnerHTML • log
    70. 70. Parameter Providers
    71. 71. Parameter Providers • nodeAttr
    72. 72. Parameter Providers • nodeAttr • nodeContent
    73. 73. Parameter Providers • nodeAttr • nodeContent • currentFormVar
    74. 74. Parameter Providers • nodeAttr • nodeContent • currentFormVar • formVar
    75. 75. Tying Them Together
    76. 76. Tying Them Together <form id=quot;myFormquot;> <input name=quot;colorquot; /> <input type=quot;submitquot; class=quot;submitquot; /> </form> <div tal:define=quot;color request/form/color|string:nothing;quot;> You chose <span id=quot;selectedColorquot; tal:content=quot;colorquot;> </span>. </div>
    77. 77. Tying Them Together <form id=quot;myFormquot;> <input name=quot;colorquot; /> <input type=quot;submitquot; class=quot;submitquot; /> </form> <div tal:define=quot;color request/form/color|string:nothing;quot;> You chose <span id=quot;selectedColorquot; tal:content=quot;colorquot;> </span>. </div> #myForm .submit:click { evt-click-preventdefault: True; action-client: replaceInnerHTML; replaceInnerHTML-kssSelector: '#selectedColor'; replaceInnerHTML-html: currentFormVar(color); }
    78. 78. Tying Them Together <form id=quot;myFormquot;> <input name=quot;colorquot; /> <input type=quot;submitquot; class=quot;submitquot; /> </form> <div tal:define=quot;color request/form/color|string:nothing;quot;> You chose <span id=quot;selectedColorquot; tal:content=quot;colorquot;> </span>. </div> #myForm .submit:click { evt-click-preventdefault: True; action-client: replaceInnerHTML; replaceInnerHTML-kssSelector: '#selectedColor'; replaceInnerHTML-html: currentFormVar(color); }
    79. 79. Fallback
    80. 80. Server Actions
    81. 81. Server Actions #myForm .submit:click { evt-click-preventdefault: True; action-server: transformColor; transformColor-color: currentFormVar(color); }
    82. 82. Server Actions #myForm .submit:click { evt-click-preventdefault: True; action-server: transformColor; transformColor-color: currentFormVar(color); } transformColor.py from kss.core.ttwapi import (startKSSCommands, getKSSCommandSet, renderKSSCommands) startKSSCommands(context, context.REQUEST) uColor = color.upper() core = getKSSCommandSet('core') core.replaceInnerHTML('#selectedColor', uColor) return renderKSSCommands()
    83. 83. Server Actions #myForm .submit:click { evt-click-preventdefault: True; action-server: transformColor; transformColor-color: currentFormVar(color); } transformColor.py from kss.core.ttwapi import (startKSSCommands, getKSSCommandSet, renderKSSCommands) startKSSCommands(context, context.REQUEST) uColor = color.upper() core = getKSSCommandSet('core') core.replaceInnerHTML('#selectedColor', uColor) return renderKSSCommands()
    84. 84. Command Sets • Core • Zope • getCssSelector • refreshProvider • addClass • refreshViewlet • removeClass • Plone • issuePortalMessage • refreshPortlet
    85. 85. Registering KSS • portal_kss registry
    86. 86. kssregistry.xml <?xml version=quot;1.0quot;?> <object name=quot;portal_kssquot; meta_type=quot;KSS Registryquot;> <kineticstylesheet cacheable=quot;Truequot; compression=quot;safequot; cookable=quot;Truequot; enabled=quot;1quot; expression=quot;quot; id=quot;mykssrules.kssquot;/> </object>
    87. 87. Debugging KSS
    88. 88. Debugging KSS • Firebug
    89. 89. Debugging KSS • Firebug • Firebug
    90. 90. Debugging KSS • Firebug • Firebug • Firebug
    91. 91. Debugging KSS • Firebug • Firebug • Firebug • Or Firebug Lite (getfirebug.com/lite.html)
    92. 92. Debugging KSS • KSS fails silently • Turn on debugging in portal_javascript • Messages will appear in Firebug console
    93. 93. Debugging KSS
    94. 94. Debugging KSS • Firekiss http://kssproject.org/download/firekiss.xpi/
    95. 95. KSS Plugins • kss.plugin.cacheabilty • kss.plugin.jsmath • kss.plugin.cns • kss.plugin.timer • kss.plugin.fadeeffect • kss.plugin.yuidnd • kss.plugin.history
    96. 96. Documentation • http://kssproject.org/ • http://plone.org/events/regional/plone- symposium-2008/skinning-with-plone-3
    97. 97. KSS Recap • Simple, CSS-style syntax • JavaScript, without the JavaScript
    98. 98. But here’s the thing...
    99. 99. Plone 4 won’t include KSS * * probably...
    100. 100. Dude.
    101. 101. WTF?
    102. 102. • It’s huge
    103. 103. • It’s huge • Lack of adoption
    104. 104. • It’s huge • Lack of adoption • Sparse documentation
    105. 105. See http://dev.plone.org/plone/ticket/8809 for more information.
    106. 106. This asshole just wasted half an hour of my life...
    107. 107. Repeat after me...
    108. 108. “KSS is not going away.”
    109. 109. “It’s just becoming an add-on Product.”
    110. 110. “I can still use KSS as much as I’d like.”
    111. 111. “By including it in my buildout.” Buildout
    112. 112. “Or as a product dependency.” Product
    113. 113. “Really.”
    114. 114. jQuery
    115. 115. jQuery
    116. 116. jQuery • DOM-focused
    117. 117. jQuery • DOM-focused • Simple syntax
    118. 118. jQuery • DOM-focused • Simple syntax • Extensible
    119. 119. jQuery • DOM-focused • Simple syntax • Extensible • 19K
    120. 120. jQuery • DOM-focused • Simple syntax • Extensible • 19K • Well-documented
    121. 121. Factory
    122. 122. Factory • jQuery() • $() • jq()
    123. 123. Selectors
    124. 124. Selectors • CSS
    125. 125. Selectors • CSS • Tag name: jq(‘p’)
    126. 126. Selectors • CSS • Tag name: jq(‘p’) • Id: jq(‘#itemId’)
    127. 127. Selectors • CSS • Tag name: jq(‘p’) • Id: jq(‘#itemId’) • Class: jq(‘.someClass’)
    128. 128. Selectors • CSS • Tag name: jq(‘p’) • Id: jq(‘#itemId’) • Class: jq(‘.someClass’) • XPath: jq(‘a[title]’)
    129. 129. Pseudo-Selectors
    130. 130. Pseudo-Selectors • :first and :last
    131. 131. Pseudo-Selectors • :first and :last • :not jq(‘#portal-column-content p:not(.discreet)’)
    132. 132. Pseudo-Selectors • :first and :last • :not jq(‘#portal-column-content p:not(.discreet)’) • :first-child, :nth-child, and :last-child jq(‘ul li:nth-child(2)’)
    133. 133. Pseudo-Selectors • :first and :last • :not jq(‘#portal-column-content p:not(.discreet)’) • :first-child, :nth-child, and :last-child jq(‘ul li:nth-child(2)’) • :even and :odd jq(‘#myTable tr:odd’)
    134. 134. Sets
    135. 135. Sets • “Implicit iteration”
    136. 136. Sets • “Implicit iteration” • jq(‘#myDiv’).hide()
    137. 137. Sets • “Implicit iteration” • jq(‘#myDiv’).hide() • jq(‘.someDivs’).hide()
    138. 138. Chaining
    139. 139. Chaining • Operations always return a result
    140. 140. Chaining • Operations always return a result • So instead of... obj.doSomething() obj.doSomethingElse(‘blah’)
    141. 141. Chaining • Operations always return a result • So instead of... obj.doSomething() obj.doSomethingElse(‘blah’) • We can write... jq(‘#myElement’).parent().addClass(‘blah’)
    142. 142. Chaining
    143. 143. Chaining • One one line... jq(‘#faq’).find(‘dd’).hide().end().find(‘dt’).click(function(){...});
    144. 144. Chaining • One one line... jq(‘#faq’).find(‘dd’).hide().end().find(‘dt’).click(function(){...}); • Or several... jq('#faq') // Find the element with id ‘faq’ .find('dd') // Get all dd elements within .hide() // Hide those elements .end() // Jump back to the ‘faq’ element .find('dt') // Find all dt elements within .click(function() {...}); // And assign an click event.
    145. 145. Events
    146. 146. Events function foo(){ alert(‘foo’); } window.onload = foo;
    147. 147. Events function foo(){ alert(‘foo’); } window.onload = foo; function bar(){ alert(‘bar’); } window.onload = bar;
    148. 148. Events function foo(){ alert(‘foo’); } window.onload = foo; function bar(){ alert(‘bar’); } window.onload = bar;
    149. 149. Events
    150. 150. Events jq(document).ready(function) { alert(‘bar’); });
    151. 151. Events jq(document).ready(function) { alert(‘foo’); }); jq(document).ready(function) { alert(‘bar’); });
    152. 152. Events jq(document).ready(function) { alert(‘foo’); jq(document).ready(function) { }); alert(‘baz’); jq(document).ready(function) { }); alert(‘bar’); });
    153. 153. Events jq(document).ready(function) { alert(‘foo’); jq(document).ready(function) { }); alert(‘baz’); jq(document).ready(function) { }); alert(‘bar’); jq(document).ready(function) { }); alert(‘blort’); });
    154. 154. Events jq(document).ready(function) { alert(‘foo’); jq(document).ready(function) { }); alert(‘baz’); jq(document).ready(function) { jq(document).ready(function) { }); alert(‘bar’); alert(‘smoo’); jq(document).ready(function) { }); }); alert(‘blort’); });
    155. 155. Events jq(document).ready(function) { alert(‘foo’); jq(document).ready(function) { }); alert(‘baz’); jq(document).ready(function) { jq(document).ready(function) { }); alert(‘bar’); alert(‘smoo’); jq(document).ready(function) { jq(document).ready(function) { }); }); alert(‘floob’); alert(‘blort’); }); });
    156. 156. Events #mykssbutton:click { evt-click-preventdefault: True; action-client: alert; alert-message: ‘Hello World!’; } <a href=”front-page” id=”mykssbutton”>Click me! </a>
    157. 157. Events jq(document).ready(function) { jq(‘#mykssbutton’).click(function(){ alert(‘Hello World!); }); }); <a href=”front-page” id=”mykssbutton”>Click me! </a>
    158. 158. Another Example jq(document).ready(function() { jq(‘a[@href$=”.pdf”]’).addclass(‘pdflink’); }); a.pdflink { background:url(‘pdficon.gif’) no-repeat; padding-left:25px; }
    159. 159. AJAX Calls
    160. 160. AJAX Calls #myForm .submit:click { evt-click-preventdefault: True; action-server: transformColor; transformColor-color: currentFormVar(color); }
    161. 161. AJAX Calls #myForm .submit:click { evt-click-preventdefault: True; action-server: transformColor; transformColor-color: currentFormVar(color); } from kss.core.ttwapi import (startKSSCommands, getKSSCommandSet, renderKSSCommands) startKSSCommands(context, context.REQUEST) uColor = color.upper() core = getKSSCommandSet('core') core.replaceInnerHTML('#selectedColor', uColor) return renderKSSCommands()
    162. 162. AJAX Calls jq_transformColor return color.upper() jq(document).ready(function(){ jq('#myForm').submit(function(){ var color = jq('input[name=color]').val(); jq('#selectedColor').load('jq_transformColor?color=' + color); return false; }); });
    163. 163. jQuery in Plone • Built into Plone 3.1+ • ‘jq’ instead of ‘$’ • Now use ‘jq’ method instead of ‘cssQuery’ or ‘registerPloneFunction’
    164. 164. Integrating jQuery Plugins http://plugins.jquery.com/
    165. 165. Integrating jQuery Plugins
    166. 166. Integrating jQuery Plugins • If the plugin uses ‘$’:
    167. 167. Integrating jQuery Plugins • If the plugin uses ‘$’: • Place it between jquery.js and jquery- integration.js <javascript ... insert-before=quot;jquery-integration.jsquot;/>
    168. 168. Integrating jQuery Plugins • If the plugin uses ‘$’: • Place it between jquery.js and jquery- integration.js <javascript ... insert-before=quot;jquery-integration.jsquot;/> • or wrap it { (function($) [your code here] })(jQuery);
    169. 169. Existing Behaviors
    170. 170. Collapsible Sections <dl class=”collapsible”> <dt class=”collapsibleHeader”> A Title </dt> <dd class=”collapsibleContent”> Your hidable content. </dd> </dl>
    171. 171. Collapsible Sections <dl class=”collapsible”> <dt class=”collapsibleHeader”> A Title </dt> <dd class=”collapsibleContent”> Your hidable content. </dd> </dl>
    172. 172. Dropdowns <dl class=”actionMenu” id=”aUniqueId”> <dt class=”actionMenuHeader”> <a href=”some_destination”>A Title</ a> </dt> <dd class=”actionMenuContent”> Your content </dd> </dl>
    173. 173. Dropdowns <dl class=”actionMenu” id=”aUniqueId”> <dt class=”actionMenuHeader”> <a href=”some_destination”>A Title</ a> </dt> <dd class=”actionMenuContent”> Your content </dd> </dl>
    174. 174. Sortable Tables <table class=”listing” id=”someId”> <thead> <tr> <th>Table header</th> </tr> </thead> <tbody> <tr> <td>Table cell 1</td> </tr> <tr> <td>Table cell 2</td> </tr> </tbody> </table>
    175. 175. Sortable Tables <table class=”listing” id=”someId”> <thead> <tr> <th>Table header</th> </tr> </thead> <tbody> <tr> <td>Table cell 1</td> </tr> <tr> <td>Table cell 2</td> </tr> </tbody> </table>
    176. 176. Sortable Tables <th class=”nosort”> Unsortable Column </th>
    177. 177. Sortable Tables <th class=”nosort”> Unsortable Column </th>
    178. 178. Products using jQuery
    179. 179. collective.flowplayer Turns code like: <a class=quot;autoFlowPlayerquot; href=quot;path/to/video-file.flvquot;> <img src=quot;path/to/splashscreen.jpgquot; /> </a> into:
    180. 180. Deco
    181. 181. Deco
    182. 182. Others • Carousel • Plone Slimbox • FancyZoomView • FadingNavigation
    183. 183. jQuery Documentation http://docs.jquery.com/
    184. 184. http://www.packtpub.com/jQuery/
    185. 185. Other Libraries • Yahoo User Interface (YUI) • extjs • MochiKit • MooTools • Dojo
    186. 186. Conclusions
    187. 187. • JavaScript novice? – Use KSS
    188. 188. • JavaScript novice? – Use KSS • Otherwise, use JQuery
    189. 189. Special thanks to... • David Glick http://wglick.org/jstalk.html • Joel Burton http://bit.ly/z1nLH
    190. 190. http://bit.ly/plone-interactivity
    191. 191. Questions? http://bit.ly/plone-interactivity
    1. A particular slide catching your eye?

      Clipping is a handy way to collect important slides you want to go back to later.

    ×