An Introduction to Jquery-UI Widget Development
Total Page:16
File Type:pdf, Size:1020Kb
Piece by piece An introduction to jQuery-UI widget development Jakob Westhoff <[email protected]> Confoo.ca March 2, 2012 Piece by piece 1 / 55 About Me I More than 10 years of professional PHP Working with experience I More than 7 years of Qafoo professional JavaScript passion for software quality experience I Open source enthusiast I Regular speaker at (inter)national conferences I Consultant, Trainer and Author Piece by piece 2 / 55 About Me I More than 10 years of professional PHP Working with experience I More than 7 years of Qafoo professional JavaScript passion for software quality experience We help people to create I Open source enthusiast high quality web I Regular speaker at applications. (inter)national conferences I Consultant, Trainer and Author Piece by piece 2 / 55 About Me I More than 10 years of professional PHP Working with experience I More than 7 years of Qafoo professional JavaScript passion for software quality experience We help people to create I Open source enthusiast high quality web I Regular speaker at applications. (inter)national conferences I Consultant, Trainer and http://qafoo.com Author Piece by piece 2 / 55 Questions answered today 1. What is jQuery? 2. What is jQuery UI? 3. What features and widgets does jQuery UI provide? 4. In which way can jQuery UI be used to write own widgets? 5. How does the jQuery UI Theme generation/usage work? Piece by piece 5 / 55 What comes next? jQuery Piece by piece 6 / 55 jQuery about itself I Fast and concise JavaScript library I HTML document traversing I Event handling I Animation I AJAX Piece by piece 6 / 55 Working with jQuery $(”.tooltip”).addClass(”highlight”).fadeIn(”slow”); I Document centric I Operates on sets accessed using $ or jQuery I $(css selector).operation I jQuery(css selector).operation I Fluent interface paradigm I operation().operation().operation() Piece by piece 7 / 55 Working with jQuery $(”.tooltip”).addClass(”highlight”).fadeIn(”slow”); I Document centric I Operates on sets accessed using $ or jQuery I $(css selector).operation I jQuery(css selector).operation I Fluent interface paradigm I operation().operation().operation() Piece by piece 7 / 55 What comes next? jQuery-UI Piece by piece 8 / 55 jQuery-UI about itself From the jQuery-UI Website: jQuery UI provides abstractions for low-level interaction and animation, advanced effects and high-level, themeable widgets, built on top of the jQuery JavaScript Library, that you can use to build highly interactive web applications. Piece by piece 8 / 55 jQuery-UI - A short overview I A set of high level widgets I Low level framework to create own Widgets, Behaviors and Effects I Compatible with all major Browsers (thanks to jQuery) I Modular code base between 5-220kb I Fully themeable using a graphical tool: ThemeRoller I Highly extensible I Download: http://jqueryui.com Piece by piece 9 / 55 What comes next? Widgets Piece by piece 10 / 55 Widgets included in jQuery UI I Accordion I Autocomplete I Button I Dialog I Slider I Tabs I Datepicker I Progressbar Piece by piece 10 / 55 Live Demo Live Demo - jQuery UI Widgets Live Demo Next section Piece by piece 11 / 55 Accordion Piece by piece 12 / 55 Accordion Piece by piece 12 / 55 Accordion Piece by piece 12 / 55 Autocomplete Piece by piece 13 / 55 Buttons Piece by piece 14 / 55 Dialog Piece by piece 15 / 55 Slider Piece by piece 16 / 55 Tabs Piece by piece 17 / 55 Datepicker Piece by piece 18 / 55 Progressbar Piece by piece 19 / 55 What comes next? Behaviors Piece by piece 20 / 55 Behaviors included in jQuery UI I Draggable I Droppable I Resizable I Selectable I Sortable Piece by piece 20 / 55 What comes next? Effects Piece by piece 21 / 55 Effects included in jQuery UI I Blind I Bounce I Clip I Drop I Explode I Fold I Highlight I Pulsate I ... Piece by piece 21 / 55 What comes next? Animation Enhancements Piece by piece 22 / 55 Animation Enhancements over default jQuery I Color animations I New easing functions I easeInBounce I easeInQuad I ... I CSS class based animation Piece by piece 22 / 55 What comes next? Calling conventions Piece by piece 23 / 55 Calling conventions I For each widget (behavior) one function is registered I The function can be called on any created jQuery set (jQuery.fn) I The widgets name is used as function name I All interaction with the Widget is accomplished through this function Piece by piece 23 / 55 Calling conventions I Widget creation $(”#id”). autocomplete(); $(”#id”). autocomplete( f ... g ); I Invoking a method without arguments $(”#id”). autocomplete(”method”); I Invoking a method with arguments $(”#id”). autocomplete(”method”, arg1, arg2,...); Piece by piece 24 / 55 Calling conventions I Widget creation $(”#id”). autocomplete(); $(”#id”). autocomplete( f ... g ); I Invoking a method without arguments $(”#id”). autocomplete(”method”); I Invoking a method with arguments $(”#id”). autocomplete(”method”, arg1, arg2,...); Piece by piece 24 / 55 Calling conventions I Widget creation $(”#id”). autocomplete(); $(”#id”). autocomplete( f ... g ); I Invoking a method without arguments $(”#id”). autocomplete(”method”); I Invoking a method with arguments $(”#id”). autocomplete(”method”, arg1, arg2,...); Piece by piece 24 / 55 What comes next? Creating your own Widget Piece by piece 25 / 55 Creating a Widget without jQuery-UI I Widgets usually have a state I Excessive use of .data method I Nested closures and custom objects I Different initialization phases I Handling of default and user options I Multiple public methods I Destruct and remove Widget on request Piece by piece 25 / 55 What comes next? Widget Factory Piece by piece 26 / 55 The Widget factory of jQuery-UI I DOM-Instance based persistent states I ”Magic” methods for different initialization phases I Automatic merging of default and user supplied options I Custom methods without namespace pollution I Distinguish between public and private methods Piece by piece 26 / 55 What comes next? Example Widget Piece by piece 27 / 55 The SparkleBar Widget I A custom made progressbar I Less flexible due to heavy usage of images I More sophisticated graphical design possible Piece by piece 27 / 55 Live Demo Live Demo - SparkleBar Live Demo Piece by piece 28 / 55 Filename conventions I Name of this widget: sparklebar I Namespace of this widget: ui I All widgets should follow a certain filename convention: I jquery.ui.sparklebar.js I jquery.ui.sparklebar.css I jquery.ui.sparklebar/image.png I ... Piece by piece 29 / 55 The widget factory in action I Call $.widget to create a new Widget I First argument: namespace and identifier I Namespaces do not protect against naming conflicts I All default widgets use the ui namespace I Using the ui namespace for your own widgets is just fine Piece by piece 30 / 55 The widget factory in action 1 $.widget( 2 ’ui.sparklebar’, 3 f 4 / ∗ Widget implementation ∗ / 5 g 6 ); Piece by piece 31 / 55 What comes next? Initialization Piece by piece 32 / 55 ”Magic” create method I create function is automatically invoked on widget creation I Only called once for each widget Piece by piece 32 / 55 Access to the targeted element I Targeted element stored in element property I Saved as jQuery set I Other magic properties exist: options, widgetName, widgetEventPrefix Piece by piece 33 / 55 ”Magic” create method 1 $.widget(”ui.sparklebar”, f 2 c r e a t e: function () f 3 4 this .element.empty(); 5 6 $(’ <img /> ’). appendTo( 7 this .element 8 ); 9 10 //... Set needed properties of img... 11 12 g 13 g ); Piece by piece 34 / 55 ”Magic” create method 1 $.widget(”ui.sparklebar”, f 2 c r e a t e: function () f 3 4 this .element.empty(); 5 6 $(’ <img /> ’). appendTo( 7 this .element 8 ); 9 10 //... Set needed properties of img... 11 12 g 13 g ); Piece by piece 34 / 55 ”Magic” create method 1 $.widget(”ui.sparklebar”, f 2 c r e a t e: function () f 3 4 this .element.empty(); 5 6 $(’ <img /> ’). appendTo( 7 this .element 8 ); 9 10 //... Set needed properties of img... 11 12 g 13 g ); Piece by piece 34 / 55 ”Magic” create method 1 $.widget(”ui.sparklebar”, f 2 c r e a t e: function () f 3 4 this .element.empty(); 5 6 $(’ <img /> ’). appendTo( 7 this .element 8 ); 9 10 //... Set needed properties of img... 11 12 g 13 g ); Piece by piece 34 / 55 ”Magic” init method I init is invoked every time the widget function is called without a method name I $("#id").sparklebar() I $("#id").sparklebar(f...g) I Little brother of the create function I Called after create has been called Piece by piece 35 / 55 ”Magic” init method 1 $.widget(”ui.sparklebar”, f 2 i n i t: function () f 3 4 // Set DOM properties based on provided options 5 // Initialize state of the widget 6 7 g 8 g ); Piece by piece 36 / 55 ”Magic” init method 1 $.widget(”ui.sparklebar”, f 2 i n i t: function () f 3 4 // Set DOM properties based on provided options 5 // Initialize state of the widget 6 7 g 8 g ); Piece by piece 36 / 55 ”Magic” init method 1 $.widget(”ui.sparklebar”, f 2 i n i t: function () f 3 4 // Set DOM properties based on provided options 5 // Initialize state of the widget 6 7 g 8 g ); Piece by piece 36 / 55 init and create I Use create to prepare the DOM for the widget I Use init to initialize widget based on options Piece by piece 37 / 55 What comes next? Methods / Properties Piece by piece 38 / 55 Widget methods and properties I Use custom properties and methods to structure your code I Both are defined in the Widget object 1 $.widget( 2 ’ui.sparklebar’, 3 f 4 ’currentValue’: 42 5 ’value’: function () f ..