GrasshopperPebbles.com

  • About
  • Recent Post
  • Comments
  • Tags
  • Links

    About Grasshopperpebbles

    Tweet

    I have been a developer for nearly 20 years and a web developer for about 10 years. Over the years, I have considered creating a blog to share the things I have learned (and am learning) about web design and development, but I never seemed to have the time. When I began learning about web development using Ajax and Ajax Frameworks, I decided to take the time to create this blog.

    My intention for this blog is to focus primarily on Ajax Frameworks, but since web development requires knowledge of many technologies, I will occasionally write about things such as CSS, Javascript, PHP, MySQL, Flash, etc.

    Lately, I have been using JQuery as my primary Ajax tool. Although I have used other Ajax Frameworks in the past (Dojo Toolkit, Yahoo! User Interface Library, Scriptaculous/Prototype), JQuery has thus far been the easiest to learn. In my first few posts I will discuss some of the JQuery Plugins that I have created. Some of these include: a plugin for Google Maps (jquery.imGoogleMaps), Form validation and submission  (jquery.imValidateForm), Page Populater (jquery.imPagePopulate), and a plugin to create lists (jquery.imList).

    While I am still learning about some of the other Ajax Frameworks, I hope that what I have learned will be helpful to others. Just remember, “When you can pull the pebbles from my hand…”

    Share

    Recent Comments

    • Dur Paharer Bosoti on Drupal: Display checkboxes with Images in a Form
    • Hemen on CodeIgniter: Call to a member function on a non-object
    • 20+ Powerful jQuery Flipbook Plugins | Good Favorites on JQuery Plugin: imBookFlip. Page Turning without Flash
    • breaking news on Zend Framework and jQuery
    • Thar on CSS: inline-block IE7 Hack
    • Chec on Using CodeIgniter to get the Last Inserted ID
    • Benjamin Bach on Django: Setting up sub-domain to serve static files
    • Aaron Moore on JQuery Plugin: imBookFlip. Page Turning without Flash
    • rivers on Dojo and CodeIgniter
    • Django: Setting up sub-domain to serve static files | GrasshopperPebbles.com on How to set up a Home page with Django

    Tags

    ActionScript Ajax ajaxally Ajax Framework Animation Apache Aptana Studio AS3 CakePHP class Codeigniter CSS Dialog django Dojo Dojo Toolkit Dojo widgets Drupal Firefox Flash google map gpAS3Library gpUrlLoader Image images Javascript Javascript Framework JQuery jquery plugin JQuery plugins Json MySQL photogallery Photoshop PHP Python Tinymce Ubuntu Widget Wordpress xml Yahoo! User Interface Library YUI YUI widget Zend Framework

    Archives

    • Setting Checkbox State with jQuery Mobile
    • Installing Mezzanine on Webfaction with Apache and WSGI
    • Installing Mezzanine on Ubuntu with Apache and mod_wsgi
    • JQuery Plugin: Mobile Page Switcher
    • Mobile Theme Generator for WordPress and Tumblr
    • WordPress: Sharing database content between primary and sub-domains
    • Drupal: Display checkboxes with Images in a Form
    • Using Multiple Databases in Multiple Environments with Zend Framework
    • Zend Framework: Building forms without decorators
    • jQuery Plugin: gpImageRotate – Create Portfolios, Carousels, Galleries, and more

    My Links

    • CakePHP
    • CodeIgniter
    • Dojo Toolkit
    • JQuery
    • JSon Tester
    • PHP.net
    • Scriptaculous/Prototype
    • SitePoint
    • YUI

Archives

  • May 2013 (6)
  • April 2013 (3)
  • October 2012 (1)
  • June 2012 (7)
  • December 2011 (1)
  • July 2010 (4)
  • June 2010 (8)
  • May 2010 (10)
  • April 2010 (5)
  • March 2010 (2)

Javascript

  • Ajax (58)
  • Dojo (13)
    • dojo-widgets (4)
  • Google Closure (1)
  • Google Maps (5)
  • Javascript (15)
  • JQuery (34)
    • plugins (18)
    • UI Widgets (1)
  • YUI (18)
    • yui-widgets (4)

PHP/MySQL

  • CakePHP (16)
  • Codeigniter (14)
  • Drupal (7)
  • Joomla (3)
  • MySQL (2)
  • PHP (9)
  • Wordpress (2)
  • Zend Framework (4)

Python/Postgresql

  • Django (6)
  • Mezzanine (2)

Design

  • Cool Sites (4)
  • CSS (11)
  • Photoshop (3)

Flash

  • Flash (19)
    • Actionscript (15)

OS

  • mac (3)
  • Ubuntu (10)

Other

  • Apache (4)
  • API's (3)
  • Geek Humor (1)
  • IDE (3)
  • Mobile (2)
  • Pebblet (22)

A YUI Login Widget

Jan13

Tweet

I created YUI 2 login widget, imYUILogin, for an intranet project that I am working on. I originally created it as a Dialog Container, but I recently updated it so that it could be configured as a Panel or a Module. Logging in is done ajaxally, of course.

Creating the Form

First we create the form using YUI’s standard Container classes (actually only ‘hd’ and ‘bd’ classes need to be created as ‘ft’ is created by the imYUILogin widget).

?View Code HTML
<div id="imYUILogin">
<div class="hd">Please Login</div>
	<div class="bd">
		<div id="loginResp"></div>
		<form name="UserLoginForm" id="UserLoginForm" method="POST" action="">
		<label for="username">UserName:</label><input type="textbox" name="username" id="username" maxlength="20" />
		<label for="password">Password:</label><input type="password" name="password" id="password" maxlength="20" /> 
		</form>
	</div>
</div>

I have a div container, imYUILogin, to hold the login form. It can be named anything, but the imYUILogin widget is configured with this particular id by default. More on this later.

The loginResp is where the login response will be displayed. Again, it can be named anything you like, but the widget is configured with this id by default.

Notice that I do not have a submit button. This is created by the widget. Also the form, username and password fields must have an id.

Styling

We will create very minimal styling. The most important styling is the form container (in this case, imYUILogin). You should set it to display:none. This is so the form does not display before the widget container is configured. The imYUILogin widget will set the display once it is set up.

?View Code CSS
#imYUILogin {
	display:none;
}
label { 
	display:block;
	float:left;
	width:90px;
	margin-top:5px;
	clear:left; 
}
#loginResp { 
	margin:5px;
	display:none;
	color:#FF0000; 
	text-align:right;
}
input {
	margin-top:5px;
}

Note: You also have to set the body class:

?View Code HTML
<body class="yui-skin-sam">

YUI Dependencies

In order for the imYUILogin widget to display correctly, you need to load the button, container, json, and utilities files. More on loading these later.

Using the imYUILogin Widget

Depending upon how you designed your login form, you may only have to supply the form id, the submit url (the form action), and the success url. The success url option is only needed if you want the user to be redirected to another page upon a successful login.

The widget can be configured as a dialog, panel or module. The dialog option is the default so if you designed your layout (the login form) exactly as it is layed out above and you want to use the dialog option, you can simply:

?View Code JAVASCRIPT
var imYUILogin = new YAHOO.INTRIG.imYUILogin('UserLoginForm', {
	urls: {submit: 'login.php', success: 'index.php'}
});

If you want more control:

?View Code JAVASCRIPT
var imYUILogin = new YAHOO.INTRIG.imYUILogin('UserLoginForm', {
	urls: {submit: 'login.php', success: 'index.php'},
	container: {id: 'imYUILogin', type: 'dialog'},
	container_position: 'fixed',
        container_width: '175px',
	loginResponseID: 'loginResp',
        fields: 'username,password'
});

So the default options are:

container.id = ‘imYUILogin’.
container.type = ‘dialog’.
container_position = ‘fixed’. If set to ‘fixed’, the dialog will be modal and centered on the screen.
container_width = ’175px’. Depending upon the layout of your page, you may want to change this value.
loginResponseID = ‘loginResp’. The div that will display the login response.
fields = ‘username,password’. The id of the username and password fields. You can name them differently, but they must be comma-delimited.

Loading the imYUILogin Widget

First download the widget from Google Code. I save it under the folder assets/js/intrig/imYUILogin/.

To load the widget, I am going to use the YUI Loader Utility. It will load the widget and all my dependencies.

First load the YUI Loader Utility:

?View Code HTML
<script type="text/javascript" src="path to utility/yuiloader/yuiloader-min.js"></script>

Now we can load the imYUILogin widget in an anonymous function:

?View Code JAVASCRIPT
(function(){
	var loader = new YAHOO.util.YUILoader();
 
	loader.addModule ({
		name: 'INTRIG.imYUILogin',
		type: 'js',
		fullpath: 'assets/js/intrig/imYUILogin/imYUILogin.js',
		varName: 'imYUILogin'
	});
 
	loader.require("button","container","json","utilities", "INTRIG.imYUILogin");
	filter: "MIN";
	loader.insert({
		base: 'assets/js/yui/',
		loadOptional: true,
		allowRollup: true, 
		onSuccess: function() {
			var Event = YAHOO.util.Event;
			Event.onDOMReady(function(){
				var imYUILogin = new YAHOO.INTRIG.imYUILogin('UserLoginForm', {
					urls: {submit: 'login.php', success: 'index.php'},
					container: {id: 'imYUILogin', type: 'dialog'},
					container_position: 'fixed',
		                        container_width: '175px',
					loginResponseID: 'loginResp',
		                        fields: "username,password"
				}); 	 	
			});
		}
	});
})();

Json Response

The imYUILogin widget requires a Json response from the server:

?View Code JAVASCRIPT
// if login was successful
{"type": "message", "label": "login success"}
 
// if login failed
{"type": "message", "label": "login failed"}

When a user attempts to logs in, you typically only send a message when the login was unsuccessful, so the imYUILogin widget will also accept:

?View Code JAVASCRIPT
{"type": "continue"}

This will tell the widget that login was successful and the person should be directed to the urls.success option.

?View Code JAVASCRIPT
urls: {submit: 'login.php', success: 'index.php'}

You can alternatively send the redirection url string in the json record that is returned from the server:

?View Code JAVASCRIPT
{"type": "continue", "label": "index.php"}

This can be advantageous if you want to send query strings to your url that are determined by the person logging in:

?View Code JAVASCRIPT
{"type": "continue", "label": "index.php?uid=23&whatever=true"}

If a label attribute exists in the returning json record, the widget will use this url before it checks the urls.success option.

The Panel Option

With the exception of the container_position, the Panel option can be loaded similarly to the Dialog option:

?View Code JAVASCRIPT
var imYUILogin = new YAHOO.INTRIG.imYUILogin('frmLogin', {
	urls: {submit: 'login.php', success: 'index.php'},
	container: {id: 'imYUILogin', type: 'panel'},
	container_width: '175px',
	loginResponseID: 'loginResp',
        fields: 'username,password'
});

or:

?View Code JAVASCRIPT
var imYUILogin = new YAHOO.INTRIG.imYUILogin('frmLogin', {
	urls: {submit: 'login.php', success: 'index.php'},
	container: {type: 'panel'}
});

The Module Option

I added the Module option to the imYUILogin widget so that you have more control over the styling of the container. This is done primarily via the hd, bd, and ft Container classes.

?View Code CSS
.hd {
	background-color: #00FF00;
	border: solid thin #fff;
}
.bd {
	background-color: #FFFF00;
	border: solid 2px #FF0033;
}
.ft {
	background-color: #0066FF;
	border: solid 3px #FF9900;
}

Again, the Module options are similar to the Dialog and Panel options. This time I will load the dependencies via the Yahoo CDN. You can use the YUI 2 Dependency Configurator to configure the necessary files.

?View Code HTML
<!-- Combo-handled YUI CSS files: --> 
<link rel="stylesheet" type="text/css" href="http://yui.yahooapis.com/combo?2.8.0r4/build/fonts/fonts-min.css&2.8.0r4/build/button/assets/skins/sam/button.css&2.8.0r4/build/container/assets/skins/sam/container.css">
 
<!-- Combo-handled YUI JS files: --> 
<script type="text/javascript" src="http://yui.yahooapis.com/combo?2.8.0r4/build/utilities/utilities.js&2.8.0r4/build/button/button-min.js&2.8.0r4/build/container/container-min.js&2.8.0r4/build/json/json-min.js"></script>
 
<!-- imYUILogin widget file: --> 
<script type="text/javascript" src='assets/js/intrig/imYUILogin/imYUILogin.js'></script>

Now call the widget:

?View Code JAVASCRIPT
(function(){		
	var Event = YAHOO.util.Event;
	Event.onDOMReady(function(){
		var imYUILogin = new YAHOO.INTRIG.imYUILogin('frmLogin', {
			urls: {submit: 'login', success: 'index.php'},
			container: {id: 'imYUILogin', type: 'module'},
	                container_width: '175px',
			loginResponseID: 'loginResp',
	                fields: "username,password"
		}); 	
	});
})();

That’s it. To view demos of the imYUILogin widget, go to the demo section of my site. You can download the widget from Google Code. Enjoy.

Share
This entry was posted in Ajax, YUI, yui-widgets and tagged Ajax, anonymous function, Container, Dialog, imYUILogin, Login, Module, Panel, Yahoo! User Interface Library, YUI, YUI Loader Utility, YUI widget by admin. Bookmark the permalink.
Logging In...

Profile cancel

Sign in with Twitter Sign in with Facebook
or

Not published

  • 7 Replies
  • 5 Comments
  • 0 Tweets
  • 0 Facebook
  • 2 Pingbacks
Last reply was December 11, 2011
  1. Logging In With YUI and CakePHP AuthComponent | GrasshopperPebbles.com
    View January 14, 2010

    [...] A YUI Login Widget [...]

  2. A YUI Login Widget | GrasshopperPebbles.com | Drakz Free Online Service
    View January 15, 2010

    [...] here: A YUI Login Widget | GrasshopperPebbles.com Share and [...]

  3. Stephen Downes
    View June 7, 2010

    Demo does appear to be on the demo page…?

  4. admin
    View June 9, 2010

    I just viewed it. Go to http://grasshopperpebbles.com/demos/index.php/yui/yuilogin/dialog_doc. You have to scroll down the page and click on the (click here) link to view the demo.

  5. John Media @ host server
    View June 10, 2010

    Great post and you also included some codes this is absolutely wonderful as I am a beginner I would likely follow and study the codes you’ve provided and maybe create my own CSS thanks.

  6. William@Forex Bulletproof
    View December 14, 2010

    Hello, many, many thanks for taking the time to share.. It was useful for my team. Thanks for all of your hard work!

  7. Richard
    View December 11, 2011

    Thanks for all your hard work and sharing with the community, very much appreciated!

Web Development Blogs - BlogCatalog Blog Directory

© 2013 GrasshopperPebbles.com
Entries (RSS) and Comments (RSS).