Showing posts with label YUI. Show all posts
Showing posts with label YUI. Show all posts

Tuesday, March 1, 2011

YUI Calendar

1. YUI: Using Event.addListener. One Calendar, Multiple Fields

http://grasshopperpebbles.com/ajax/yui-using-eventaddlistener-one-calendar-multiple-fields/


created a variable and set it to null:
var clickEl = null;

use an Event Listener (Event.Listener) using the ids of the input fields:

var ids = ["appoint_date", "interp_date", "service_date", "cancel_date"];
Event.addListener(ids, "click", showCal);


When a user clicks on one of the input fields, the showCal function is called. The showCal function sets the clickEl variable to the id of the “clicked” input field and then shows a YUI Dialog component that contains the YUI Calendar.
function showCal(e) {
clickEl = this.id;
dlgCal.show();
}

When a date is selected, the date value is inserted into the appropriate input field:
var selDate = calCal.getSelectedDates()[0];
var dStr = selDate.getDate();
var mStr = calCal.cfg.getProperty("MONTHS_SHORT")[selDate.getMonth()];
var yStr = selDate.getFullYear();
Dom.get(clickEl).value = yStr + "-"+ mStr + "-" + dStr;
...




Tuesday, May 25, 2010

YUI: Customize YUI

Building Your Own Widget Library with YUI
http://yuiblog.com/blog/2008/06/24/buildingwidgets/

Customizing Existing YUI Component

YAHOO.namespace static function to create an object space for our own library

YAHOO.namespace('SATYAM');  // create a property SATYAM under YAHOO


Define Constructor

YAHOO.SATYAM.LoadingPanel = function(id) {

  YAHOO.SATYAM.LoadingPanel.superclass.constructor.call(this,
    id || YAHOO.util.Dom.generateId() ,
    {
       width: "100px",
       fixedcenter: true,
       constraintoviewport: true,
       underlay: "shadow",
       close: false,
       visible: false,
       draggable: true
    }
  );

  this.setHeader("Loading ...");
  this.setBody('');
  YAHOO.util.Dom.setStyle(this.body, 'textAlign', 'center');
  this.render(document.body);
};


Use YAHOO.lang.extend
superclass is part of the inheritance mechanism provided by YUI’s extend method. After you declare the constructor for your new object, you call extend:

YAHOO.lang.extend(YAHOO.SATYAM.LoadingPanel, YAHOO.widget.Panel);

The constructor itself does not get executed immediately. The extend function does get executed immediately.

We use method call of JavaScript native Function object, which takes the first argument as the execution scope of the function called and passes it the rest of the arguments.

To Use the object we just created

if (!loadingPanel2) {
   loadingPanel2 = new YAHOO.SATYAM.LoadingPanel();
}
loadingPanel2.show();


Puts all the code within an anonymous function, which gets immediately executed (notice the empty parenthesis at the end)

YAHOO.namespace('SATYAM');
(function(){
    var Dom = YAHOO.util.Dom,
        Event = YAHOO.util.Event,
        Panel = YAHOO.widget.Panel;

 // here goes the library contents itself
 // constructor
 // extend component
})();
YAHOO.register('SATYAM.LoadingPanel', YAHOO.SATYAM.LoadingPanel, {version: "0.99", build: '11'});

After we declare the instance of the YUI Loader that we will use, we call method addModule providing the information about our library file

var loader = new YAHOO.util.YUILoader();

loader.addModule({
   name: 'SATYAM.LoadingPanel',
   type: 'js',
   requires: ['container'],
   fullpath: 'LoadingPanel.js'
});

loader.require('reset', 'grids', 'base', 'SATYAM.LoadingPanel');






Wednesday, May 19, 2010

YUI Tips

1. YUI Container
http://developer.yahoo.com/yui/container/

Seven Examples of YUI Panels
http://icant.co.uk/sandbox/yuipanel/

2. YUI Event
http://developer.yahoo.com/yui/event/

YAHOO.util.Event.addListener(el, sType, fn, obj, overrideContext)
el:  id, or a collection of ids
sType: the type of event to append (such as "click" http://www.quirksmode.org/dom/events/)
fn: the method the event invokes
obj: an arbitrary object that will be passed as a parameter to the handler
overrideContect: if true, the obj passed in becomes the execution context of the listener; if an object, this object becomes the execution context

YAHOO.util.Subscriber( fn , obj , overrideContext )
fn: the function to execute
obj: an object to be passed along when the event fires
overrideContext: If true, the obj passed in becomes the execution context of the listener

There are 2 main types of event subscriptions:

//DragDrop
var dd = new YAHOO.util.DD('dd');
dd.on('dragEvent', function() { });

//Panel
var panel = new YAHOO.widget.Panel('panel');
panel.renderEvent.subscribe(function() {});

//Calendar
var cal = new YAHOO.widget.Calendar('cal');
cal.selectEvent.subscribe(function() {});

//Editor
var editor = new YAHOO.widget.Editor('editor', {});
editor.on('afterRender', function() {});




3. Define anonymous function 
Defining an anonymous function in order to keep all variables out of the global scope. Inside the anonymous function, define some shortcuts to utils that will be used frequently (Dom and Event).

(function () {
    var Event = YAHOO.util.Event,
    Dom = YAHOO.util.Dom;
}());


Inside the the anonymous function, use the onDOMReady method of the Event utility to instantiate an Overlay and a Button when the page's DOM is ready to be scripted.

Event.onDOMReady(function () {
    var oCalendarMenu;

    // Create an Overlay instance to house the Calendar instance
    oCalendarMenu = new YAHOO.widget.Overlay("calendarmenu", { visible: false });

    // Create a Button instance of type "menu"

    var oButton = new YAHOO.widget.Button({
       type: "menu",
       id: "calendarpicker",
       label: "Choose A Date",
       menu: oCalendarMenu,
       container: "datefields" });
});


3. YUI Panel
Click outside of a panel to close it
var treePanel = new YAHOO.widget.Panel(...);

function isInsideTreePanel(clicked_element) {
    var current_element = clicked_element;
    while(current_element && current_element != document.body) {
       if (current_element == treePanel.element) {
          return true;
       }
    current_element = current_element.parentNode;
    }
    return false;
}

function onDocumentMouseDown(e) {
    if (treePanel.cfg.getProperty('visible')) {
       if (!isInsideTreePanel(e.target)) {
          treePanel.hide();
          YAHOO.util.Event.removeListener(document, onDocumentMouseDown);
       }
    }
}

treePanel.show();
YAHOO.util.Event.addListener(document, 'mousedown', onDocumentMouseDown, null);


4. YUI namespace
http://www.yuiblog.com/blog/2007/06/12/module-pattern/

YAHOO.namespace("mySwitch");
YAHOO.mySwitch.panelTable = new YAHOO.widget.Panel("panelTable");

Error: missing ; before statement
if trying to create a public method with “var” keyword

YAHOO.namespace("mySwitch");
var YAHOO.mySwitch.panelTable = new YAHOO.widget.Panel("panelTable");



parentheses () at the end of function: cause anonymous function to execute immediately

YAHOO.myProject.myModule = function () {
} ();

Wednesday, February 10, 2010