yui for control freaks - a presentation at the ajax experience

128
YUI for control freaks Christian Heilmann The Ajax Experience, Boston, MA, Autumn 2008

Upload: christian-heilmann

Post on 15-Jan-2015

6.961 views

Category:

Technology


0 download

DESCRIPTION

 

TRANSCRIPT

Page 1: YUI for control freaks - a presentation at The Ajax Experience

YUI for control freaks

Christian Heilmann

The Ajax Experience, Boston, MA, Autumn 2008

Page 2: YUI for control freaks - a presentation at The Ajax Experience

Oh, hello there...

Page 3: YUI for control freaks - a presentation at The Ajax Experience

I’m Chris.

Page 4: YUI for control freaks - a presentation at The Ajax Experience

I am a lazy person.

Page 5: YUI for control freaks - a presentation at The Ajax Experience

I prefer doing things once, and do them right instead of

doing them over and over again.

Page 6: YUI for control freaks - a presentation at The Ajax Experience

For this, I need control.

Page 7: YUI for control freaks - a presentation at The Ajax Experience

I like having control.

Page 8: YUI for control freaks - a presentation at The Ajax Experience

Remote control

rubber duck

Page 9: YUI for control freaks - a presentation at The Ajax Experience

However, having too much control can be an issue.

Page 10: YUI for control freaks - a presentation at The Ajax Experience
Page 11: YUI for control freaks - a presentation at The Ajax Experience

Limitation is good.

Page 12: YUI for control freaks - a presentation at The Ajax Experience

Let’s do a quick rewind.

Page 13: YUI for control freaks - a presentation at The Ajax Experience

The Commodore 64

Page 14: YUI for control freaks - a presentation at The Ajax Experience

16 preset colours

resolution 160x200 pixels

4 colours per each 8x8 pixel block

Page 15: YUI for control freaks - a presentation at The Ajax Experience

Limitations that inspire different people in different

ways.

Page 16: YUI for control freaks - a presentation at The Ajax Experience
Page 17: YUI for control freaks - a presentation at The Ajax Experience
Page 18: YUI for control freaks - a presentation at The Ajax Experience
Page 19: YUI for control freaks - a presentation at The Ajax Experience
Page 20: YUI for control freaks - a presentation at The Ajax Experience

It is great to have a one size fits all solution.

Page 21: YUI for control freaks - a presentation at The Ajax Experience

... but it can be as cool to have a on-demand set of tools.

Page 22: YUI for control freaks - a presentation at The Ajax Experience

YUI is the latter.

Page 23: YUI for control freaks - a presentation at The Ajax Experience

It brings order to the chaos.

Page 24: YUI for control freaks - a presentation at The Ajax Experience

What chaos?

Page 25: YUI for control freaks - a presentation at The Ajax Experience

JavaScript is a part of a larger world.

Page 26: YUI for control freaks - a presentation at The Ajax Experience

JavaScript is a part of a larger world.

This is not the copyrighted

photo you are looking for

Page 27: YUI for control freaks - a presentation at The Ajax Experience

Browser

Interaction with other technologies (CSS, Markup)

Interaction with other scripts

Interaction with the backend

Page 28: YUI for control freaks - a presentation at The Ajax Experience

Interaction with the operating system.

Interaction with the user (with unknown ability)

Interaction with bad code (a.k.a. ads)

Page 29: YUI for control freaks - a presentation at The Ajax Experience

YUI deals with all of this.

Page 30: YUI for control freaks - a presentation at The Ajax Experience

Because it has to – we built it for industrial (Yahoo)

strength.

Page 31: YUI for control freaks - a presentation at The Ajax Experience

The first thing we needed to get are some sensible

constraints.

Page 33: YUI for control freaks - a presentation at The Ajax Experience
Page 34: YUI for control freaks - a presentation at The Ajax Experience

This gave us a defined playground, and we were able

to start tackling the other issues.

Page 35: YUI for control freaks - a presentation at The Ajax Experience

The first thing to tackle before you can even hope to build

interfaces are browser differences in CSS.

Page 36: YUI for control freaks - a presentation at The Ajax Experience

There is no such thing as an “unstyled page”.

Page 37: YUI for control freaks - a presentation at The Ajax Experience

There is no such thing as an “unstyled page”.

Page 38: YUI for control freaks - a presentation at The Ajax Experience
Page 39: YUI for control freaks - a presentation at The Ajax Experience
Page 40: YUI for control freaks - a presentation at The Ajax Experience
Page 41: YUI for control freaks - a presentation at The Ajax Experience
Page 42: YUI for control freaks - a presentation at The Ajax Experience

Good luck working around that one.

Page 43: YUI for control freaks - a presentation at The Ajax Experience

Unless you use reset.css

http://developer.yahoo.com/yui/reset/

Page 44: YUI for control freaks - a presentation at The Ajax Experience
Page 45: YUI for control freaks - a presentation at The Ajax Experience
Page 46: YUI for control freaks - a presentation at The Ajax Experience
Page 47: YUI for control freaks - a presentation at The Ajax Experience
Page 48: YUI for control freaks - a presentation at The Ajax Experience

Starting with a blank canvas

=

good.

Page 49: YUI for control freaks - a presentation at The Ajax Experience

What about typography?

Page 51: YUI for control freaks - a presentation at The Ajax Experience
Page 53: YUI for control freaks - a presentation at The Ajax Experience
Page 54: YUI for control freaks - a presentation at The Ajax Experience

Grids gives you an amazingly large amount of options and

layout permutations.

Page 55: YUI for control freaks - a presentation at The Ajax Experience

Everybody Duck!

Page 56: YUI for control freaks - a presentation at The Ajax Experience

There will be code

Page 57: YUI for control freaks - a presentation at The Ajax Experience

Wouldn’t it be cool to not know when to use which size

of the grid automatically?

Page 58: YUI for control freaks - a presentation at The Ajax Experience

This is where the next YUI gem comes in: DOM.

http://developer.yahoo.com/yui/dom

Page 59: YUI for control freaks - a presentation at The Ajax Experience

Using the DOM component, I can read out what happens in

the browser.

Page 60: YUI for control freaks - a presentation at The Ajax Experience

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.

Page 61: YUI for control freaks - a presentation at The Ajax Experience

Using DOM, I can create a YUI grid that works with all kind of

different browsers sizes.

Page 63: YUI for control freaks - a presentation at The Ajax Experience

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();

Page 64: YUI for control freaks - a presentation at The Ajax Experience

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'; } }

Page 65: YUI for control freaks - a presentation at The Ajax Experience

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 };}();

Page 66: YUI for control freaks - a presentation at The Ajax Experience

What about monitoring the size of an element?

Page 67: YUI for control freaks - a presentation at The Ajax Experience

position:fixed is sexy!

Page 68: YUI for control freaks - a presentation at The Ajax Experience
Page 69: YUI for control freaks - a presentation at The Ajax Experience

It can however also render your site impossible to use.

Page 70: YUI for control freaks - a presentation at The Ajax Experience
Page 71: YUI for control freaks - a presentation at The Ajax Experience

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' ); }

Page 72: YUI for control freaks - a presentation at The Ajax Experience

The DOM stepchild: Region

Page 73: YUI for control freaks - a presentation at The Ajax Experience

Using Region I can find out the dimensions of an element.

Page 74: YUI for control freaks - a presentation at The Ajax Experience

I can also find the region that is big enough to include two

regions, or the one that is the intersection of the two.

Page 75: YUI for control freaks - a presentation at The Ajax Experience

region example

Page 76: YUI for control freaks - a presentation at The Ajax Experience

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);

Page 77: YUI for control freaks - a presentation at The Ajax Experience

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); });

Page 78: YUI for control freaks - a presentation at The Ajax Experience

This gives me full control to avoid any overlap!

Page 79: YUI for control freaks - a presentation at The Ajax Experience

What about things the browser does not tell me?

Page 80: YUI for control freaks - a presentation at The Ajax Experience

Wouldn’t it be cool to find out when the font is resized?

Page 81: YUI for control freaks - a presentation at The Ajax Experience
Page 83: YUI for control freaks - a presentation at The Ajax Experience

You can detect the font size in several ways:

Page 84: YUI for control freaks - a presentation at The Ajax Experience

Include an element with a known size in ems and read its

offsetHeight and offsetWidth in an interval...

Page 85: YUI for control freaks - a presentation at The Ajax Experience

...or use an iframe with em sizing off-screen and subscribe

to its resize event.

Page 86: YUI for control freaks - a presentation at The Ajax Experience

Or use the YUI container module anywhere on your

page... :)

Page 87: YUI for control freaks - a presentation at The Ajax Experience

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!') } ); });

Page 88: YUI for control freaks - a presentation at The Ajax Experience

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!') } ); });

Page 89: YUI for control freaks - a presentation at The Ajax Experience

This works with one feature of YUI event that is very close to

my heart: Custom Events.

Page 90: YUI for control freaks - a presentation at The Ajax Experience

... which is so cool that all the other big libraries now have it

aswell :)

Page 91: YUI for control freaks - a presentation at The Ajax Experience

Custom Events allow you to notify an unknown amount of

listeners about what is happening...

Page 92: YUI for control freaks - a presentation at The Ajax Experience

... sending information not necessarily accessible to them

when it happens.

Page 93: YUI for control freaks - a presentation at The Ajax Experience

Every single YUI component has a lot of Custom Events you

can subscribe to.

Page 94: YUI for control freaks - a presentation at The Ajax Experience
Page 95: YUI for control freaks - a presentation at The Ajax Experience

Say for example you want to make sure to securely chain

animation sequences...

Page 96: YUI for control freaks - a presentation at The Ajax Experience

//This is the first animation; this one will //fire when the button is clicked. var move = new YAHOO.util.Anim("animator", { 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( "animator", { backgroundColor: {from:"#003366", to:"#ff0000"} }, 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(); });

Page 97: YUI for control freaks - a presentation at The Ajax Experience

//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("startAnim"); start.subscribe("click", function() { //reset the color value to the start so that //the animation can be run multiple times: YAHOO.util.Dom.setStyle("animator", "backgroundColor", "#003366"); move.animate(); });

Page 98: YUI for control freaks - a presentation at The Ajax Experience

//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("changeColor animation is starting.", "info", "example"); }); changeColor.onTween.subscribe(function(s, o) { YAHOO.log("changeColor onTween firing with these arguments: " + YAHOO.lang.dump(o), "info", "example"); }); changeColor.onComplete.subscribe(function(s, o) { YAHOO.log("changeColor onComplete firing with these arguments: " + YAHOO.lang.dump(o), "info", "example"); });

Page 99: YUI for control freaks - a presentation at The Ajax Experience

That is a lot of control!

Page 100: YUI for control freaks - a presentation at The Ajax Experience

{font resizing example}

Page 101: YUI for control freaks - a presentation at The Ajax Experience

Knowledge is power.

Page 102: YUI for control freaks - a presentation at The Ajax Experience

This is why YUI comes with a lot of tools to gain knowledge

about what is happening under the hood of your

application.

Page 103: YUI for control freaks - a presentation at The Ajax Experience

YUI logger gives you a cross-browser console to show

values.

Page 104: YUI for control freaks - a presentation at The Ajax Experience
Page 105: YUI for control freaks - a presentation at The Ajax Experience

Death to alert()!

Page 106: YUI for control freaks - a presentation at The Ajax Experience

All YUI components come as a debug version which log

everything that is going on to the logger.

Page 107: YUI for control freaks - a presentation at The Ajax Experience

You can even include the logger on the fly with a

bookmarklet.

Page 109: YUI for control freaks - a presentation at The Ajax Experience

If you need even more control, there is the YUI

profiler.

http://developer.yahoo.com/yui/profiler/

Page 110: YUI for control freaks - a presentation at The Ajax Experience
Page 111: YUI for control freaks - a presentation at The Ajax Experience

And the YUI test framework for test driven development.

http://developer.yahoo.com/yui/yuitest/

Page 112: YUI for control freaks - a presentation at The Ajax Experience

If you like even more control...

Page 113: YUI for control freaks - a presentation at The Ajax Experience

Then you must be Nicholas Zakas or Dean Edwards!

Page 114: YUI for control freaks - a presentation at The Ajax Experience

On a code level, YUI comes out-of-the-box with

namespacing.

Page 115: YUI for control freaks - a presentation at The Ajax Experience

Which – if used correctly – keeps large amounts of code readable and maintainable.

Page 116: YUI for control freaks - a presentation at The Ajax Experience

YAHOO.lang also comes with a lot of validation methods to ensure things are what they

are.

Page 117: YUI for control freaks - a presentation at The Ajax Experience

So how is YUI good for control freaks?

Page 118: YUI for control freaks - a presentation at The Ajax Experience

Built on agreed standards

Separated into modules each dealing with one task

Constant reporting of what is going on

Own Debugging environment

Page 119: YUI for control freaks - a presentation at The Ajax Experience

Here’s another small thing I prepared earlier:

Page 120: YUI for control freaks - a presentation at The Ajax Experience
Page 121: YUI for control freaks - a presentation at The Ajax Experience

Using Event and Dom I can control the visible part:

Page 122: YUI for control freaks - a presentation at The Ajax Experience

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'); };

Page 124: YUI for control freaks - a presentation at The Ajax Experience

What does the future hold?

Page 126: YUI for control freaks - a presentation at The Ajax Experience
Page 127: YUI for control freaks - a presentation at The Ajax Experience

Include on demand

Multiple sandboxed instances in a page

Modularity on CSS level (per element reset)

Every event is a custom event

Page 128: YUI for control freaks - a presentation at The Ajax Experience

Christian Heilmann

http://wait-till-i.com

http://scriptingenabled.org

http://twitter.com/codepo8

THANKS!http://icanhaz.com/yuicontrol