SlideShare une entreprise Scribd logo
1  sur  128
Télécharger pour lire hors ligne
YUI for control
    freaks
              Christian Heilmann

 The Ajax Experience, Boston, MA, Autumn 2008
Oh, hello there...
I’m Chris.
I am a lazy person.
I prefer doing things once,
and do them right instead of
 doing them over and over
            again.
For this, I need control.
I like having control.
Remote control
 rubber duck
However, having too much
 control can be an issue.
Limitation is good.
Let’s do a quick rewind.
The Commodore 64
16 preset colours
resolution 160x200 pixels
4 colours per each 8x8 pixel
block
Limitations that inspire
different people in different
            ways.
It is great to have a one size
        fits all solution.
... but it can be as cool to have
    a on-demand set of tools.
YUI is the latter.
It brings order to the chaos.
What chaos?
JavaScript is a part of a larger
            world.
This is not the
JavaScript is a part of a larger
            world.
 copyrighted
photo you are
  looking for
Browser
Interaction with other
technologies (CSS, Markup)
Interaction with other
scripts
Interaction with the
backend
Interaction with the
operating system.
Interaction with the user
(with unknown ability)
Interaction with bad code
(a.k.a. ads)
YUI deals with all of this.
Because it has to – we built it
   for industrial (Yahoo)
          strength.
The first thing we needed to
   get are some sensible
         constraints.
We did this with the Graded
    Browser Support:
http://developer.yahoo.com/
       yui/articles/gbs/
This gave us a defined
playground, and we were able
  to start tackling the other
             issues.
The first thing to tackle before
 you can even hope to build
   interfaces are browser
       differences in CSS.
There is no such thing as an
     “unstyled page”.
There is no such thing as an
     “unstyled page”.
Good luck working around
        that one.
Unless you use reset.css
http://developer.yahoo.com/
          yui/reset/
Starting with a blank canvas
             =
           good.
What about typography?
Make it work across browsers
        with fonts.css
http://developer.yahoo.com/
          yui/fonts/
Even create layouts with
          grids.css
http://developer.yahoo.com/
          yui/grids/
Grids gives you an amazingly
large amount of options and
    layout permutations.
Everybody Duck!
There will be code
Wouldn’t it be cool to not
know when to use which size
 of the grid automatically?
This is where the next YUI gem
         comes in: DOM.
http://developer.yahoo.com/
          yui/dom
Using the DOM component, I
can read out what happens in
         the browser.
I can get the dimensions of the
window, the dimensions of the
 browser, and the dimensions
     of any element in the
  document – regardless of its
          positioning.
Using DOM, I can create a YUI
grid that works with all kind of
   different browsers sizes.
http://yuiblog.com/blog/
  2008/06/25/autogrids
YAHOO.example.autoGrid = function(){
  var container = YAHOO.util.Dom.get('doc') ||
                  YAHOO.util.Dom.get('doc2') ||
                  YAHOO.util.Dom.get('doc4') ||
                  YAHOO.util.Dom.get('doc3') ||
                  YAHOO.util.Dom.get('doc-custom');
  if(container){
    var sidebar = null;
    var classes = container.className;
    if(classes.match(/yui-t[1-3]|yui-left/)){
       var sidebar = 'left';
    }
    if(classes.match(/yui-t[4-6]|yui-right/)){
       var sidebar = 'right';
    }
    function switchGrid(){
      var currentWidth = YAHOO.util.Dom.getViewportWidth();
if(currentWidth > 950){
  container.id = 'doc2';
  if(sidebar){
    container.className = sidebar === 'left'
                          ? 'yui-t3' : 'yui-t6';
  }
}
if(currentWidth < 950){
  container.id = 'doc';
  if(sidebar){
    container.className = sidebar === 'left'
                          ? 'yui-t2' : 'yui-t5';
  }
}
if(currentWidth < 760){
  container.id = 'doc3';
  if(sidebar){
    container.className = sidebar === 'left'
                          ? 'yui-t1' : 'yui-t4';
  }
}
if(currentWidth < 600){
        container.id = 'doc3';
        container.className = '';
      }
    };
    switchGrid();
    function throttle(method, scope) {
       clearTimeout(method._tId);
         method._tId= setTimeout(function(){
         method.call(scope);
       }, 100);
    };
    YAHOO.util.Event.on(window,'resize',function(){
       throttle(YAHOO.example.autoGrid.switchGrid,window);
    });

  };
  return {
     switchGrid:switchGrid
  };
}();
What about monitoring the
   size of an element?
position:fixed is sexy!
It can however also render
your site impossible to use.
var YD = YAHOO.util.Dom;
YAHOO.util.Event.onDOMReady(toggleMenu);
YAHOO.util.Event.on(window,'resize',function(){
  toggleMenu();
});
function toggleMenu(){
  var sidebar = YD.getRegion('sb');
  var browser = YD.getViewportHeight();
  YD.setStyle('sb','position',
     browser < sidebar.bottom ? 'static' : 'fixed'
  );
}
The DOM stepchild: Region
Using Region I can find out the
  dimensions of an element.
I can also find the region that
 is big enough to include two
regions, or the one that is the
    intersection of the two.
region example
YAHOO.util.Event.onDOMReady(function(){
  var YD = YAHOO.util.Dom;
  var r1 = YD.getRegion('region-one');
  var r2 = YD.getRegion('region-two');
  var i = r1.intersect(r2);
  var u = r1.union(r2);
  var intersect = document.createElement('div');
  document.body.appendChild(intersect);
  YD.setStyle(intersect,'position','absolute');
  YD.setStyle(intersect,'background','#c0c');
  YD.setStyle(intersect,'width',i.right-i.left + 'px');
  YD.setStyle(intersect,'height',i.bottom-i.top + 'px');
  YD.setStyle(intersect,'z-index',100);
  YD.setXY(intersect,i);
var union = document.createElement('div');
  document.body.appendChild(union);
  YD.setStyle(union,'position','absolute');
  YD.setStyle(union,'background','#000');
  YD.setStyle(union,'opacity',.5);
  YD.setStyle(union,'width',u.right-u.left + 'px');
  YD.setStyle(union,'height',u.bottom-u.top + 'px');
  YD.setStyle(union,'z-index',90);
  YD.setXY(union,u);
});
This gives me full control to
     avoid any overlap!
What about things the
browser does not tell me?
Wouldn’t it be cool to find out
 when the font is resized?
http://alistapart.com/articles/fontresizing
You can detect the font size in
       several ways:
Include an element with a
known size in ems and read its
     offsetHeight and
offsetWidth in an interval...
...or use an iframe with em
sizing off-screen and subscribe
        to its resize event.
Or use the YUI container
module anywhere on your
         page... :)
YAHOO.namespace('example.container');
YAHOO.util.Event.onDOMReady(function(){
  YAHOO.example.container.module1 = new YAHOO.widget.Panel(
     'module1',
     {
       close:true,
       draggable:true,
       constraintoviewport:true
     }
  );
  YAHOO.example.container.module1.render();
  YAHOO.widget.Module.textResizeEvent.subscribe(
     function(o){
       console.log('Text has been resized!')
     }
  );
});
YAHOO.namespace('example.container');
YAHOO.util.Event.onDOMReady(function(){
  YAHOO.example.container.module1 = new YAHOO.widget.Panel(
     'module1',
     {
       close:true,
       draggable:true,
       constraintoviewport:true
     }
  );
  YAHOO.example.container.module1.render();
  YAHOO.widget.Module.textResizeEvent.subscribe(
     function(o){
       console.log('Text has been resized!')
     }
  );
});
This works with one feature of
YUI event that is very close to
  my heart: Custom Events.
... which is so cool that all the
other big libraries now have it
            aswell :)
Custom Events allow you to
notify an unknown amount of
   listeners about what is
         happening...
... sending information not
necessarily accessible to them
        when it happens.
Every single YUI component
has a lot of Custom Events you
       can subscribe to.
Say for example you want to
make sure to securely chain
  animation sequences...
//This is the first animation; this one will
	 //fire when the button is clicked.
	 var move = new YAHOO.util.Anim(quot;animatorquot;, {
	 	 left: {from:0, to:75}
	 }, 1);	
	 //This is the second animation; it will fire
	 //when the first animation is complete.
	 var changeColor = new YAHOO.util.ColorAnim(
     quot;animatorquot;, { backgroundColor:
     {from:quot;#003366quot;, to:quot;#ff0000quot;}
	     }, 1);
	 //Here's the chaining glue: We subscribe to the
	 //first animation's onComplete event, and in
	 //our handler we animate the second animation:
	 move.onComplete.subscribe(function() {
	 	 changeColor.animate();
	 });
//Here we set up our YUI Button and subcribe to
	 //its click event. When clicked, it will
	 //animate the first animation:
	 var start = new YAHOO.widget.Button(quot;startAnimquot;);
	 start.subscribe(quot;clickquot;, function() {
	 	 //reset the color value to the start so that
	 	 //the animation can be run multiple times:
	 	 YAHOO.util.Dom.setStyle(quot;animatorquot;,
                             quot;backgroundColorquot;,
                             quot;#003366quot;);
	 	 move.animate();
	 });
//You can also make use of the onStart and onTween
	 //custom events in Animation; here, we'll log all
	 //of changeColor's custom events and peek at their
	 //argument signatures:
	 changeColor.onStart.subscribe(function() {
	 	 YAHOO.log(quot;changeColor animation is starting.quot;,
               quot;infoquot;, quot;examplequot;);
	 });
	 changeColor.onTween.subscribe(function(s, o) {
	 	 YAHOO.log(quot;changeColor onTween firing with these
               arguments: quot; + YAHOO.lang.dump(o),
               quot;infoquot;, quot;examplequot;);
	 });
	 changeColor.onComplete.subscribe(function(s, o) {
	 	 YAHOO.log(quot;changeColor onComplete firing with
               these arguments: quot; + YAHOO.lang.dump(o),
               quot;infoquot;, quot;examplequot;);
	 });
That is a lot of control!
{font resizing example}
Knowledge is power.
This is why YUI comes with a
lot of tools to gain knowledge
   about what is happening
    under the hood of your
          application.
YUI logger gives you a cross-
 browser console to show
          values.
Death to alert()!
All YUI components come as a
   debug version which log
everything that is going on to
          the logger.
You can even include the
 logger on the fly with a
     bookmarklet.
http://blog.rajatpandit.com/sandbox/yuilogger/index.html
If you need even more
  control, there is the YUI
          profiler.
http://developer.yahoo.com/
         yui/profiler/
And the YUI test framework
for test driven development.
http://developer.yahoo.com/
         yui/yuitest/
If you like even more control...
Then you must be Nicholas Zakas or Dean Edwards!
On a code level, YUI comes
   out-of-the-box with
      namespacing.
Which – if used correctly –
keeps large amounts of code
readable and maintainable.
YAHOO.lang also comes with a
 lot of validation methods to
 ensure things are what they
              are.
So how is YUI good for control
           freaks?
Built on agreed standards
Separated into modules
each dealing with one task
Constant reporting of what
is going on
Own Debugging
environment
Here’s another small thing I
     prepared earlier:
Using Event and Dom I can
 control the visible part:
function move(e){
   y = YAHOO.util.Event.getXY(e);
   if(y[1] > size){
     render(y);
   }
};
function render(y){
   var d = YAHOO.util.Dom;
   var real = y[1] - d.getDocumentScrollTop();
   d.setStyle(top,'height',real-size+'px');
   d.setStyle(bottom,'top',real+size+'px');
   var h = d.getViewportHeight() - real + size;
   d.setStyle(bottom,'height',h + 'px');
};
http://yuiblog.com/blog/2008/09/30/reading-blinds/
What does the future hold?
YUI3
http://developer.yahoo.com/
           yui/3/
Include on demand
Multiple sandboxed
instances in a page
Modularity on CSS level (per
element reset)
Every event is a custom
event
THANKS!
http://icanhaz.com/yuicontrol




                     Christian Heilmann

                    http://wait-till-i.com

                 http://scriptingenabled.org

                 http://twitter.com/codepo8

Contenu connexe

Tendances

Introducing AlloyUI DiagramBuilder
Introducing AlloyUI DiagramBuilderIntroducing AlloyUI DiagramBuilder
Introducing AlloyUI DiagramBuilder
Eduardo Lundgren
 

Tendances (20)

Canvas al ajillo
Canvas al ajilloCanvas al ajillo
Canvas al ajillo
 
Introduction to HTML5 Canvas
Introduction to HTML5 CanvasIntroduction to HTML5 Canvas
Introduction to HTML5 Canvas
 
Matching Game In Java
Matching Game In JavaMatching Game In Java
Matching Game In Java
 
Core Animation
Core AnimationCore Animation
Core Animation
 
YUI3 and AlloyUI Introduction for Pernambuco.JS 2012
YUI3 and AlloyUI Introduction for Pernambuco.JS 2012YUI3 and AlloyUI Introduction for Pernambuco.JS 2012
YUI3 and AlloyUI Introduction for Pernambuco.JS 2012
 
Testing Web Applications Through User Interface Constraints (CASCON 2015 Talk)
Testing Web Applications Through User Interface Constraints (CASCON 2015 Talk)Testing Web Applications Through User Interface Constraints (CASCON 2015 Talk)
Testing Web Applications Through User Interface Constraints (CASCON 2015 Talk)
 
SenchaCon 2010: Developing components and extensions for ext js
SenchaCon 2010: Developing components and extensions for ext jsSenchaCon 2010: Developing components and extensions for ext js
SenchaCon 2010: Developing components and extensions for ext js
 
Intro to Game Programming
Intro to Game ProgrammingIntro to Game Programming
Intro to Game Programming
 
HTML5 Canvas
HTML5 CanvasHTML5 Canvas
HTML5 Canvas
 
Introducing AlloyUI DiagramBuilder
Introducing AlloyUI DiagramBuilderIntroducing AlloyUI DiagramBuilder
Introducing AlloyUI DiagramBuilder
 
HTML5 Canvas - Let's Draw!
HTML5 Canvas - Let's Draw!HTML5 Canvas - Let's Draw!
HTML5 Canvas - Let's Draw!
 
Александр Зимин – Анимация как средство самовыражения
Александр Зимин – Анимация как средство самовыраженияАлександр Зимин – Анимация как средство самовыражения
Александр Зимин – Анимация как средство самовыражения
 
ev
evev
ev
 
Introduction to Game Programming Tutorial
Introduction to Game Programming TutorialIntroduction to Game Programming Tutorial
Introduction to Game Programming Tutorial
 
OL4JSF - Latinoware 2010
OL4JSF - Latinoware 2010OL4JSF - Latinoware 2010
OL4JSF - Latinoware 2010
 
Mapping the world with Twitter
Mapping the world with TwitterMapping the world with Twitter
Mapping the world with Twitter
 
Standford 2015 week3: Objective-C Compatibility, Property List, Views
Standford 2015 week3: Objective-C Compatibility, Property List, ViewsStandford 2015 week3: Objective-C Compatibility, Property List, Views
Standford 2015 week3: Objective-C Compatibility, Property List, Views
 
004
004004
004
 
Exploring Canvas
Exploring CanvasExploring Canvas
Exploring Canvas
 
Touchevents
ToucheventsTouchevents
Touchevents
 

En vedette

Fce speaking 2
Fce speaking 2Fce speaking 2
Fce speaking 2
Ceci Sosa
 
SPEAKING, PARTS 2, AND 4. CITY LIFE
SPEAKING, PARTS 2, AND 4. CITY LIFESPEAKING, PARTS 2, AND 4. CITY LIFE
SPEAKING, PARTS 2, AND 4. CITY LIFE
Javier Martos
 

En vedette (20)

Marsella
MarsellaMarsella
Marsella
 
Attracting customers syn neg pos
Attracting customers syn neg posAttracting customers syn neg pos
Attracting customers syn neg pos
 
Tips: FCE Speaking part 1
Tips: FCE Speaking part 1Tips: FCE Speaking part 1
Tips: FCE Speaking part 1
 
Liberate your control freaks
Liberate your control freaksLiberate your control freaks
Liberate your control freaks
 
What Attracts Narcissists
What Attracts NarcissistsWhat Attracts Narcissists
What Attracts Narcissists
 
I Term Fce Expert
I Term Fce ExpertI Term Fce Expert
I Term Fce Expert
 
Fce speaking - parts 1 and 2
Fce   speaking - parts 1 and 2Fce   speaking - parts 1 and 2
Fce speaking - parts 1 and 2
 
FCE Speaking Paper Part 2
FCE Speaking Paper Part 2FCE Speaking Paper Part 2
FCE Speaking Paper Part 2
 
Fce practice tests
Fce practice testsFce practice tests
Fce practice tests
 
Narcissistic Personalities: Identifying them, understanding them, relating to...
Narcissistic Personalities: Identifying them, understanding them, relating to...Narcissistic Personalities: Identifying them, understanding them, relating to...
Narcissistic Personalities: Identifying them, understanding them, relating to...
 
Why Can't I Move On? Narcissistic Abuse: A Complex Trauma. Compiled by Jeni M...
Why Can't I Move On? Narcissistic Abuse: A Complex Trauma. Compiled by Jeni M...Why Can't I Move On? Narcissistic Abuse: A Complex Trauma. Compiled by Jeni M...
Why Can't I Move On? Narcissistic Abuse: A Complex Trauma. Compiled by Jeni M...
 
Fce speaking 2
Fce speaking 2Fce speaking 2
Fce speaking 2
 
Fce part 3 and 4
Fce part 3 and 4Fce part 3 and 4
Fce part 3 and 4
 
IELTS SPEAKING PART 3
IELTS SPEAKING PART 3IELTS SPEAKING PART 3
IELTS SPEAKING PART 3
 
Verbal Abuse and the Narcissist: Communication Tactics Designed to Make you C...
Verbal Abuse and the Narcissist: Communication Tactics Designed to Make you C...Verbal Abuse and the Narcissist: Communication Tactics Designed to Make you C...
Verbal Abuse and the Narcissist: Communication Tactics Designed to Make you C...
 
Speaking part 3
Speaking part 3Speaking part 3
Speaking part 3
 
Speaking part 2 comparing photos vocabulary bank
Speaking part 2 comparing photos vocabulary bankSpeaking part 2 comparing photos vocabulary bank
Speaking part 2 comparing photos vocabulary bank
 
SPEAKING, PARTS 2, AND 4. CITY LIFE
SPEAKING, PARTS 2, AND 4. CITY LIFESPEAKING, PARTS 2, AND 4. CITY LIFE
SPEAKING, PARTS 2, AND 4. CITY LIFE
 
Fce speaking part
Fce speaking partFce speaking part
Fce speaking part
 
Red Flags to Narcissistic Personality Disorder compiled by Jeni Mawter
Red Flags to Narcissistic Personality Disorder compiled by Jeni MawterRed Flags to Narcissistic Personality Disorder compiled by Jeni Mawter
Red Flags to Narcissistic Personality Disorder compiled by Jeni Mawter
 

Similaire à YUI for control freaks - a presentation at The Ajax Experience

YUI introduction to build hack interfaces
YUI introduction to build hack interfacesYUI introduction to build hack interfaces
YUI introduction to build hack interfaces
Christian Heilmann
 
Your Library Sucks, and why you should use it.
Your Library Sucks, and why you should use it.Your Library Sucks, and why you should use it.
Your Library Sucks, and why you should use it.
Peter Higgins
 
Is HTML5 Ready? (workshop)
Is HTML5 Ready? (workshop)Is HTML5 Ready? (workshop)
Is HTML5 Ready? (workshop)
Remy Sharp
 
Is html5-ready-workshop-110727181512-phpapp02
Is html5-ready-workshop-110727181512-phpapp02Is html5-ready-workshop-110727181512-phpapp02
Is html5-ready-workshop-110727181512-phpapp02
PL dream
 

Similaire à YUI for control freaks - a presentation at The Ajax Experience (20)

YUI introduction to build hack interfaces
YUI introduction to build hack interfacesYUI introduction to build hack interfaces
YUI introduction to build hack interfaces
 
A More Flash Like Web?
A More Flash Like Web?A More Flash Like Web?
A More Flash Like Web?
 
HTML5 - Daha Flash bir web?
HTML5 - Daha Flash bir web?HTML5 - Daha Flash bir web?
HTML5 - Daha Flash bir web?
 
YUI 3
YUI 3YUI 3
YUI 3
 
jQuery in the [Aol.] Enterprise
jQuery in the [Aol.] EnterprisejQuery in the [Aol.] Enterprise
jQuery in the [Aol.] Enterprise
 
YUI for your Hacks
YUI for your Hacks YUI for your Hacks
YUI for your Hacks
 
Writing Maintainable JavaScript
Writing Maintainable JavaScriptWriting Maintainable JavaScript
Writing Maintainable JavaScript
 
The Theory Of The Dom
The Theory Of The DomThe Theory Of The Dom
The Theory Of The Dom
 
Prototype UI
Prototype UIPrototype UI
Prototype UI
 
How to develop a Graphical User Interface (GUI) in Scilab
How to develop a Graphical User Interface (GUI) in ScilabHow to develop a Graphical User Interface (GUI) in Scilab
How to develop a Graphical User Interface (GUI) in Scilab
 
Lhy tutorial gui(1)
Lhy tutorial gui(1)Lhy tutorial gui(1)
Lhy tutorial gui(1)
 
Your Library Sucks, and why you should use it.
Your Library Sucks, and why you should use it.Your Library Sucks, and why you should use it.
Your Library Sucks, and why you should use it.
 
Is HTML5 Ready? (workshop)
Is HTML5 Ready? (workshop)Is HTML5 Ready? (workshop)
Is HTML5 Ready? (workshop)
 
Is html5-ready-workshop-110727181512-phpapp02
Is html5-ready-workshop-110727181512-phpapp02Is html5-ready-workshop-110727181512-phpapp02
Is html5-ready-workshop-110727181512-phpapp02
 
Moustamera
MoustameraMoustamera
Moustamera
 
2009 Hackday Taiwan Yui
2009 Hackday Taiwan Yui2009 Hackday Taiwan Yui
2009 Hackday Taiwan Yui
 
Griffon @ Svwjug
Griffon @ SvwjugGriffon @ Svwjug
Griffon @ Svwjug
 
Hack U - YUI - 2012 IIT Kharagpur
Hack U - YUI - 2012 IIT KharagpurHack U - YUI - 2012 IIT Kharagpur
Hack U - YUI - 2012 IIT Kharagpur
 
JavaScript Refactoring
JavaScript RefactoringJavaScript Refactoring
JavaScript Refactoring
 
Leaving Flatland: getting started with WebGL
Leaving Flatland: getting started with WebGLLeaving Flatland: getting started with WebGL
Leaving Flatland: getting started with WebGL
 

Plus de Christian Heilmann

The Soul in The Machine - Developing for Humans (FrankenJS edition)
The Soul in The Machine - Developing for Humans (FrankenJS edition)The Soul in The Machine - Developing for Humans (FrankenJS edition)
The Soul in The Machine - Developing for Humans (FrankenJS edition)
Christian Heilmann
 

Plus de Christian Heilmann (20)

Develop, Debug, Learn? - Dotjs2019
Develop, Debug, Learn? - Dotjs2019Develop, Debug, Learn? - Dotjs2019
Develop, Debug, Learn? - Dotjs2019
 
Hinting at a better web
Hinting at a better webHinting at a better web
Hinting at a better web
 
Taking the "vile" out of privilege
Taking the "vile" out of privilegeTaking the "vile" out of privilege
Taking the "vile" out of privilege
 
Seven ways to be a happier JavaScript developer - NDC Oslo
Seven ways to be a happier JavaScript developer - NDC OsloSeven ways to be a happier JavaScript developer - NDC Oslo
Seven ways to be a happier JavaScript developer - NDC Oslo
 
Artificial intelligence for humans… #AIDC2018 keynote
Artificial intelligence for humans… #AIDC2018 keynoteArtificial intelligence for humans… #AIDC2018 keynote
Artificial intelligence for humans… #AIDC2018 keynote
 
Killing the golden calf of coding - We are Developers keynote
Killing the golden calf of coding - We are Developers keynoteKilling the golden calf of coding - We are Developers keynote
Killing the golden calf of coding - We are Developers keynote
 
Progressive Web Apps - Techdays Finland
Progressive Web Apps - Techdays FinlandProgressive Web Apps - Techdays Finland
Progressive Web Apps - Techdays Finland
 
Taking the "vile" out of privilege
Taking the "vile" out of privilegeTaking the "vile" out of privilege
Taking the "vile" out of privilege
 
Five ways to be a happier JavaScript developer
Five ways to be a happier JavaScript developerFive ways to be a happier JavaScript developer
Five ways to be a happier JavaScript developer
 
Taking the P out of PWA
Taking the P out of PWATaking the P out of PWA
Taking the P out of PWA
 
Sacrificing the golden calf of "coding"
Sacrificing the golden calf of "coding"Sacrificing the golden calf of "coding"
Sacrificing the golden calf of "coding"
 
You learned JavaScript - now what?
You learned JavaScript - now what?You learned JavaScript - now what?
You learned JavaScript - now what?
 
Sacrificing the golden calf of "coding"
Sacrificing the golden calf of "coding"Sacrificing the golden calf of "coding"
Sacrificing the golden calf of "coding"
 
Progressive Web Apps - Covering the best of both worlds - DevReach
Progressive Web Apps - Covering the best of both worlds - DevReachProgressive Web Apps - Covering the best of both worlds - DevReach
Progressive Web Apps - Covering the best of both worlds - DevReach
 
Progressive Web Apps - Covering the best of both worlds
Progressive Web Apps - Covering the best of both worldsProgressive Web Apps - Covering the best of both worlds
Progressive Web Apps - Covering the best of both worlds
 
Non-trivial pursuits: Learning machines and forgetful humans
Non-trivial pursuits: Learning machines and forgetful humansNon-trivial pursuits: Learning machines and forgetful humans
Non-trivial pursuits: Learning machines and forgetful humans
 
Progressive Web Apps - Bringing the web front and center
Progressive Web Apps - Bringing the web front and center Progressive Web Apps - Bringing the web front and center
Progressive Web Apps - Bringing the web front and center
 
CSS vs. JavaScript - Trust vs. Control
CSS vs. JavaScript - Trust vs. ControlCSS vs. JavaScript - Trust vs. Control
CSS vs. JavaScript - Trust vs. Control
 
Leveling up your JavaScipt - DrupalJam 2017
Leveling up your JavaScipt - DrupalJam 2017Leveling up your JavaScipt - DrupalJam 2017
Leveling up your JavaScipt - DrupalJam 2017
 
The Soul in The Machine - Developing for Humans (FrankenJS edition)
The Soul in The Machine - Developing for Humans (FrankenJS edition)The Soul in The Machine - Developing for Humans (FrankenJS edition)
The Soul in The Machine - Developing for Humans (FrankenJS edition)
 

Dernier

Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
panagenda
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
WSO2
 

Dernier (20)

ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challenges
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
 
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbu
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu SubbuApidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbu
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbu
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
 
Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024
 
Ransomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdfRansomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdf
 
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
 
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot ModelNavi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
 
A Beginners Guide to Building a RAG App Using Open Source Milvus
A Beginners Guide to Building a RAG App Using Open Source MilvusA Beginners Guide to Building a RAG App Using Open Source Milvus
A Beginners Guide to Building a RAG App Using Open Source Milvus
 
AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 

YUI for control freaks - a presentation at The Ajax Experience

  • 1. YUI for control freaks Christian Heilmann The Ajax Experience, Boston, MA, Autumn 2008
  • 4. I am a lazy person.
  • 5. I prefer doing things once, and do them right instead of doing them over and over again.
  • 6. For this, I need control.
  • 7. I like having control.
  • 9. However, having too much control can be an issue.
  • 10.
  • 12. Let’s do a quick rewind.
  • 14. 16 preset colours resolution 160x200 pixels 4 colours per each 8x8 pixel block
  • 15. Limitations that inspire different people in different ways.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20. It is great to have a one size fits all solution.
  • 21. ... but it can be as cool to have a on-demand set of tools.
  • 22. YUI is the latter.
  • 23. It brings order to the chaos.
  • 25. JavaScript is a part of a larger world.
  • 26. This is not the JavaScript is a part of a larger world. copyrighted photo you are looking for
  • 27. Browser Interaction with other technologies (CSS, Markup) Interaction with other scripts Interaction with the backend
  • 28. Interaction with the operating system. Interaction with the user (with unknown ability) Interaction with bad code (a.k.a. ads)
  • 29. YUI deals with all of this.
  • 30. Because it has to – we built it for industrial (Yahoo) strength.
  • 31. The first thing we needed to get are some sensible constraints.
  • 32. We did this with the Graded Browser Support: http://developer.yahoo.com/ yui/articles/gbs/
  • 33.
  • 34. This gave us a defined playground, and we were able to start tackling the other issues.
  • 35. The first thing to tackle before you can even hope to build interfaces are browser differences in CSS.
  • 36. There is no such thing as an “unstyled page”.
  • 37. There is no such thing as an “unstyled page”.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42. Good luck working around that one.
  • 43. Unless you use reset.css http://developer.yahoo.com/ yui/reset/
  • 44.
  • 45.
  • 46.
  • 47.
  • 48. Starting with a blank canvas = good.
  • 50. Make it work across browsers with fonts.css http://developer.yahoo.com/ yui/fonts/
  • 51.
  • 52. Even create layouts with grids.css http://developer.yahoo.com/ yui/grids/
  • 53.
  • 54. Grids gives you an amazingly large amount of options and layout permutations.
  • 57. Wouldn’t it be cool to not know when to use which size of the grid automatically?
  • 58. This is where the next YUI gem comes in: DOM. http://developer.yahoo.com/ yui/dom
  • 59. Using the DOM component, I can read out what happens in the browser.
  • 60. I can get the dimensions of the window, the dimensions of the browser, and the dimensions of any element in the document – regardless of its positioning.
  • 61. Using DOM, I can create a YUI grid that works with all kind of different browsers sizes.
  • 63. YAHOO.example.autoGrid = function(){ var container = YAHOO.util.Dom.get('doc') || YAHOO.util.Dom.get('doc2') || YAHOO.util.Dom.get('doc4') || YAHOO.util.Dom.get('doc3') || YAHOO.util.Dom.get('doc-custom'); if(container){ var sidebar = null; var classes = container.className; if(classes.match(/yui-t[1-3]|yui-left/)){ var sidebar = 'left'; } if(classes.match(/yui-t[4-6]|yui-right/)){ var sidebar = 'right'; } function switchGrid(){ var currentWidth = YAHOO.util.Dom.getViewportWidth();
  • 64. if(currentWidth > 950){ container.id = 'doc2'; if(sidebar){ container.className = sidebar === 'left' ? 'yui-t3' : 'yui-t6'; } } if(currentWidth < 950){ container.id = 'doc'; if(sidebar){ container.className = sidebar === 'left' ? 'yui-t2' : 'yui-t5'; } } if(currentWidth < 760){ container.id = 'doc3'; if(sidebar){ container.className = sidebar === 'left' ? 'yui-t1' : 'yui-t4'; } }
  • 65. if(currentWidth < 600){ container.id = 'doc3'; container.className = ''; } }; switchGrid(); function throttle(method, scope) { clearTimeout(method._tId); method._tId= setTimeout(function(){ method.call(scope); }, 100); }; YAHOO.util.Event.on(window,'resize',function(){ throttle(YAHOO.example.autoGrid.switchGrid,window); }); }; return { switchGrid:switchGrid }; }();
  • 66. What about monitoring the size of an element?
  • 68.
  • 69. It can however also render your site impossible to use.
  • 70.
  • 71. var YD = YAHOO.util.Dom; YAHOO.util.Event.onDOMReady(toggleMenu); YAHOO.util.Event.on(window,'resize',function(){ toggleMenu(); }); function toggleMenu(){ var sidebar = YD.getRegion('sb'); var browser = YD.getViewportHeight(); YD.setStyle('sb','position', browser < sidebar.bottom ? 'static' : 'fixed' ); }
  • 73. Using Region I can find out the dimensions of an element.
  • 74. I can also find the region that is big enough to include two regions, or the one that is the intersection of the two.
  • 76. YAHOO.util.Event.onDOMReady(function(){ var YD = YAHOO.util.Dom; var r1 = YD.getRegion('region-one'); var r2 = YD.getRegion('region-two'); var i = r1.intersect(r2); var u = r1.union(r2); var intersect = document.createElement('div'); document.body.appendChild(intersect); YD.setStyle(intersect,'position','absolute'); YD.setStyle(intersect,'background','#c0c'); YD.setStyle(intersect,'width',i.right-i.left + 'px'); YD.setStyle(intersect,'height',i.bottom-i.top + 'px'); YD.setStyle(intersect,'z-index',100); YD.setXY(intersect,i);
  • 77. var union = document.createElement('div'); document.body.appendChild(union); YD.setStyle(union,'position','absolute'); YD.setStyle(union,'background','#000'); YD.setStyle(union,'opacity',.5); YD.setStyle(union,'width',u.right-u.left + 'px'); YD.setStyle(union,'height',u.bottom-u.top + 'px'); YD.setStyle(union,'z-index',90); YD.setXY(union,u); });
  • 78. This gives me full control to avoid any overlap!
  • 79. What about things the browser does not tell me?
  • 80. Wouldn’t it be cool to find out when the font is resized?
  • 81.
  • 83. You can detect the font size in several ways:
  • 84. Include an element with a known size in ems and read its offsetHeight and offsetWidth in an interval...
  • 85. ...or use an iframe with em sizing off-screen and subscribe to its resize event.
  • 86. Or use the YUI container module anywhere on your page... :)
  • 87. YAHOO.namespace('example.container'); YAHOO.util.Event.onDOMReady(function(){ YAHOO.example.container.module1 = new YAHOO.widget.Panel( 'module1', { close:true, draggable:true, constraintoviewport:true } ); YAHOO.example.container.module1.render(); YAHOO.widget.Module.textResizeEvent.subscribe( function(o){ console.log('Text has been resized!') } ); });
  • 88. YAHOO.namespace('example.container'); YAHOO.util.Event.onDOMReady(function(){ YAHOO.example.container.module1 = new YAHOO.widget.Panel( 'module1', { close:true, draggable:true, constraintoviewport:true } ); YAHOO.example.container.module1.render(); YAHOO.widget.Module.textResizeEvent.subscribe( function(o){ console.log('Text has been resized!') } ); });
  • 89. This works with one feature of YUI event that is very close to my heart: Custom Events.
  • 90. ... which is so cool that all the other big libraries now have it aswell :)
  • 91. Custom Events allow you to notify an unknown amount of listeners about what is happening...
  • 92. ... sending information not necessarily accessible to them when it happens.
  • 93. Every single YUI component has a lot of Custom Events you can subscribe to.
  • 94.
  • 95. Say for example you want to make sure to securely chain animation sequences...
  • 96. //This is the first animation; this one will //fire when the button is clicked. var move = new YAHOO.util.Anim(quot;animatorquot;, { left: {from:0, to:75} }, 1); //This is the second animation; it will fire //when the first animation is complete. var changeColor = new YAHOO.util.ColorAnim( quot;animatorquot;, { backgroundColor: {from:quot;#003366quot;, to:quot;#ff0000quot;} }, 1); //Here's the chaining glue: We subscribe to the //first animation's onComplete event, and in //our handler we animate the second animation: move.onComplete.subscribe(function() { changeColor.animate(); });
  • 97. //Here we set up our YUI Button and subcribe to //its click event. When clicked, it will //animate the first animation: var start = new YAHOO.widget.Button(quot;startAnimquot;); start.subscribe(quot;clickquot;, function() { //reset the color value to the start so that //the animation can be run multiple times: YAHOO.util.Dom.setStyle(quot;animatorquot;, quot;backgroundColorquot;, quot;#003366quot;); move.animate(); });
  • 98. //You can also make use of the onStart and onTween //custom events in Animation; here, we'll log all //of changeColor's custom events and peek at their //argument signatures: changeColor.onStart.subscribe(function() { YAHOO.log(quot;changeColor animation is starting.quot;, quot;infoquot;, quot;examplequot;); }); changeColor.onTween.subscribe(function(s, o) { YAHOO.log(quot;changeColor onTween firing with these arguments: quot; + YAHOO.lang.dump(o), quot;infoquot;, quot;examplequot;); }); changeColor.onComplete.subscribe(function(s, o) { YAHOO.log(quot;changeColor onComplete firing with these arguments: quot; + YAHOO.lang.dump(o), quot;infoquot;, quot;examplequot;); });
  • 99. That is a lot of control!
  • 102. This is why YUI comes with a lot of tools to gain knowledge about what is happening under the hood of your application.
  • 103. YUI logger gives you a cross- browser console to show values.
  • 104.
  • 106. All YUI components come as a debug version which log everything that is going on to the logger.
  • 107. You can even include the logger on the fly with a bookmarklet.
  • 109. If you need even more control, there is the YUI profiler. http://developer.yahoo.com/ yui/profiler/
  • 110.
  • 111. And the YUI test framework for test driven development. http://developer.yahoo.com/ yui/yuitest/
  • 112. If you like even more control...
  • 113. Then you must be Nicholas Zakas or Dean Edwards!
  • 114. On a code level, YUI comes out-of-the-box with namespacing.
  • 115. Which – if used correctly – keeps large amounts of code readable and maintainable.
  • 116. YAHOO.lang also comes with a lot of validation methods to ensure things are what they are.
  • 117. So how is YUI good for control freaks?
  • 118. Built on agreed standards Separated into modules each dealing with one task Constant reporting of what is going on Own Debugging environment
  • 119. Here’s another small thing I prepared earlier:
  • 120.
  • 121. Using Event and Dom I can control the visible part:
  • 122. function move(e){ y = YAHOO.util.Event.getXY(e); if(y[1] > size){ render(y); } }; function render(y){ var d = YAHOO.util.Dom; var real = y[1] - d.getDocumentScrollTop(); d.setStyle(top,'height',real-size+'px'); d.setStyle(bottom,'top',real+size+'px'); var h = d.getViewportHeight() - real + size; d.setStyle(bottom,'height',h + 'px'); };
  • 124. What does the future hold?
  • 126.
  • 127. Include on demand Multiple sandboxed instances in a page Modularity on CSS level (per element reset) Every event is a custom event
  • 128. THANKS! http://icanhaz.com/yuicontrol Christian Heilmann http://wait-till-i.com http://scriptingenabled.org http://twitter.com/codepo8