This example demonstrates how to set up and instantiate Y.Panel to take advantage of its nested modality and header/footer button support. In this example, we create a simple datatable with some basic information that is updated and removed through a modal form with some custom buttons.
Creating a modal form using Panels
Setting Up The YUI Instance
To create an instance of a Panel on your page, the only module you need to request is the panel
module. The panel
module will pull in the widget
, widget-stack
, widget-position
, widget-position-align
, widget-position-constrain
, widget-stdmod
, widget-buttons
, widget-modality
and widget-autohide
extensions it uses.
For this example, we also use the YUI3 Datatable, and the Drag plugin to make the panels draggable. This requires us to also request the datatable-base
and dd-plugin
modules in our use statement.
YUI({...}).use("panel", "datatable-base", "dd-plugin", function(Y) { // We'll write example code here where we have Y.Datatable, Y.Plugin.Drag and Y.Panel available });
Note, using the panel
module, will also pull down the default CSS required for panel. The CSS that styles the Panel requires you to have the class yui3-skin-sam
on a parent element, commonly the <body>
tag.
Creating a Panel From Markup
For this example, we'll need two panel instances. The first will be created through markup, while the second will be created through JavaScript (just to illustrate the differences). The code snippet below is the markup for our modal form. It consists of a fieldset with a couple of <input>
boxes. The yui3-widget-bd
class is not required, but tells the Panel that this content goes in the body of the widget.
<div id="panelContent"> <div class="yui3-widget-bd"> <form> <fieldset> <p><label for="id">ID</label><br/> <input type="text" name="id" id="productId" placeholder=""></p> <p><label for="name">Name</label><br/> <input type="text" name="name" id="name" value="" placeholder=""></p> <p><label for="password">Price</label><br/> <input type="text" name="price" id="price" value="" placeholder="$"></p> </fieldset> </form> </div>
The container DIV with id="panelContent" is specified as the contentBox for the Panel instance, and during instantiation, the panel will look for DIV's marked with the yui3-widget-hd, yui3-widget-bd, yui3-widget-ft
classes to setup the Overlay's header, body and footer content attributes.
Instantiating the Parent Panel
To create a panel instance, we use the panel constructor Y.Panel
. We can pass in some additional configuration attributes such as modal
, headerContent
, and centered
. We can make the panel draggable by adding the Y.Plugin.Drag
plugin.
After creating the panel instance, we invoke panel.render()
to update the DOM to reflect the current state of the panel.
var panel = new Y.Panel({ srcNode: "#panelContent", width: 250, centered: true, visible: false, modal:true, headerContent: "Add A New Product", plugins: [Y.Plugin.Drag] }); panel.render();
Adding Footer buttons to the Panel
The parent panel has two buttons in the footer, "Add Item" and "Remove All Items". We add these buttons through the addButton()
method present on the Y.Panel instance. For each button, we specify an action
function, which will be called when the button is clicked, and a section
property that specifies whether it should get rendered in the header or the footer.
panel.addButton( { value: "Add Item", action: function(e) { e.preventDefault(); addItem(); //this method adds a record to the datatable }, section: Y.WidgetStdMod.FOOTER } ); panel.addButton( { value: "Remove All Items", action: function(e) { e.preventDefault(); removeAllItemsConfirm(); }, section: Y.WidgetStdMod.FOOTER } );
Creating the nested Panel through JavaScript
In the example, clicking the "Remove all items" button renders a nested confirmation panel. Since Y.Panel implements the Y.WidgetStack
and Y.WidgetModality
extensions, creating nested panels are easy to do for the developer. The nested panel code is as follows:
var nestedPanel = new Y.Panel({ bodyContent: "Are you sure you want to remove all items?", zIndex: 5, //We set a z-index higher than the parent's z-index centered:true, width:400, modal:true, //We add the "Yes" and "No" buttons buttons: [ { value: "Yes", action : function(e) { e.preventDefault(); nestedPanel.hide(); panel.hide(); removeItems(); //this method removes all data from the datatable }, section: Y.WidgetStdMod.FOOTER }, { value: "No", action: function(e) { e.preventDefault(); nestedPanel.hide(); }, section: Y.WidgetStdMod.FOOTER } ] }); nestedPanel.render('#nestedPanel');
In this case, we pass in an array of buttons to the buttons
attribute. As a result, the nested panel does not have the close button in the top-right corner.
CSS: Panel Look/Feel
The panel.css Sam Skin file (build/panel/assets/skins/sam/panel.css) provides the default functional CSS for the panel. In addition, an image file (build/panel/assets/skins/sam/sprite_icons.gif) provides the icons for the "close" button.
NOTE: As discussed on the Widget landing page, all widgets are enclosed in 2 containing elements - the boundingBox is the outer(most) element, and the contentBox is the inner element into which the widget's content is added. It is advised to apply any look/feel CSS for the widget to the content box and it's children. This leaves the bounding box without padding/borders, allowing for consistent positioning/sizing across box models.
Complete Example Source
<style type="text/css"> #desc { margin-bottom:20px; border-bottom:1px dotted #333; } #desc span { background:#a3350d; padding:2px; color:#f27243; } .yui3-panel { outline:none; } .yui3-panel-content .yui3-widget-hd { font-weight:bold; } .yui3-panel-content .yui3-widget-bd { padding:15px; } .yui3-panel-content label { margin-right:30px; } .yui3-panel-content fieldset { border:none; padding:0; } .yui3-panel-content input[type="text"] { border:none; border:1px solid #ccc; padding: 3px 7px; -webkit-border-radius:2px; -moz-border-radius:2px; border-radius:2px; font-size:100%; width:200px; } #addRow { margin-top:10px; } </style> <h2>Using a panel to show a modal form</h2> <div class="yui3-u-1"> <div id="dt"></div> <p><input type="button" id="addRow" value="Add"></p> <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur non felis dolor. Fusce rutrum velit quis sem luctus ultrices. Vivamus bibendum mollis enim, vel auctor massa convallis accumsan. Curabitur laoreet nunc vel leo laoreet sed feugiat elit tempor. Sed fermentum ligula ut nisi lobortis pretium. Donec ut est at leo rhoncus ultricies eu at nunc. Phasellus semper, lacus ac pulvinar dictum, orci orci iaculis nulla, non condimentum nibh justo eu felis. Nam sed orci a ligula vehicula rutrum. Donec sodales euismod laoreet. Mauris ut augue purus. Nulla porta vehicula ligula, id viverra lacus hendrerit ut. Donec eu est vitae orci ullamcorper pellentesque. Morbi molestie placerat aliquet. Aliquam aliquet consectetur porttitor. Mauris semper tincidunt nisi, in dignissim turpis auctor ac. Sed at enim ligula. Aenean quis dignissim augue.</p> <p>Nunc quis sem tortor. Quisque lorem quam, auctor sit amet porttitor pretium, accumsan quis arcu. Mauris blandit, enim nec fermentum faucibus, massa lectus posuere massa, eget consequat leo risus in risus. Sed ornare euismod orci sit amet commodo. Suspendisse ultrices dui ut mi venenatis vitae tincidunt dolor pulvinar. Proin at nibh sed libero molestie facilisis. Maecenas magna purus, lacinia eu tempus in, elementum a est. Morbi eget magna sed justo dignissim pulvinar nec vitae justo. Aliquam tincidunt arcu eget orci tempus ornare ullamcorper dolor aliquet. Vestibulum congue posuere porttitor. Pellentesque magna erat, dapibus nec tristique at, posuere sed nisl. In pretium, risus at volutpat pretium, augue nunc commodo metus, vitae ullamcorper risus quam sagittis turpis. Proin eget cursus quam. Sed elit tortor, tempus pharetra lacinia vel, ultrices nec est. Praesent nibh risus, vulputate nec tincidunt eget, lacinia sed eros. Vestibulum vel velit massa. In hac habitasse platea dictumst. Etiam eu magna ligula.</p> <p>Vivamus vel dui at velit laoreet accumsan. Pellentesque posuere est et urna euismod elementum. Fusce a nibh nisl, vitae iaculis magna. Nulla sit amet odio in elit posuere pellentesque. Nulla sit amet eros eu odio tempus feugiat at vel purus. In vehicula feugiat purus eu ultricies. Aliquam vitae sapien quis augue gravida pretium. Morbi non lectus eu nisi varius mollis. Maecenas eget nisl sit amet turpis cursus gravida at quis odio. Cras viverra eros placerat erat ultricies ultricies.</p> <p>Aenean malesuada erat vel ipsum iaculis sollicitudin. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Sed lectus metus, accumsan in molestie vitae, luctus non nibh. Vestibulum rutrum, nulla vel tristique varius, metus nibh tincidunt erat, at mattis turpis justo quis velit. Donec ac lobortis mauris. Nam nulla tellus, placerat sit amet tempus non, consequat sed nibh. Curabitur eget ligula a sem dictum fringilla. Cras fermentum blandit nulla ut vulputate. Nullam iaculis venenatis orci, et tincidunt lorem pellentesque eget. Morbi sit amet nibh id sapien rhoncus mollis. Vestibulum quis neque massa, eget interdum dolor. Donec rhoncus, metus non dignissim imperdiet, nulla orci eleifend sapien, at interdum augue lacus ac quam. Duis ullamcorper, augue eget semper varius, mi nisi rutrum mi, non sagittis neque quam nec ipsum. Curabitur in mauris lacus. Ut porta porttitor nunc, id elementum quam mattis quis. Donec quis libero eros, at malesuada lectus. Cras lectus tellus, pharetra ut tempor ut, fringilla in turpis.</p> <p>Quisque tempor turpis non ligula ornare cursus. Vivamus tempus lobortis urna sed vestibulum. Duis id ligula eu dolor feugiat laoreet sit amet in enim. Integer ullamcorper erat at sem mattis quis tempor metus ullamcorper. Praesent sed diam elit. Donec vel lorem libero. Suspendisse nec arcu ac purus interdum mollis congue imperdiet erat. Suspendisse eu tristique enim. Quisque volutpat, leo sit amet iaculis luctus, velit neque suscipit nisi, vitae placerat felis diam laoreet metus. Suspendisse consectetur pulvinar commodo. Nulla magna quam, scelerisque blandit pellentesque sed, euismod nec nulla. Curabitur vitae est quis sem condimentum dictum. Aenean tincidunt dolor ac orci consectetur id pulvinar justo aliquam. Proin ante nulla, ullamcorper sit amet auctor in, pulvinar volutpat quam. Sed vitae dolor dui, sed tincidunt nunc. Phasellus euismod consequat fringilla. Quisque semper dolor eget tellus sagittis porta sit amet quis libero.</p> <div id="panelContent"> <div class="yui3-widget-bd"> <form> <fieldset> <p><label for="id">ID</label><br/> <input type="text" name="id" id="productId" placeholder=""></p> <p><label for="name">Name</label><br/> <input type="text" name="name" id="name" value="" placeholder=""></p> <p><label for="password">Price</label><br/> <input type="text" name="price" id="price" value="" placeholder="$"></p> </fieldset> </form> </div> </div> <div id="nestedPanel"></div> </div> <script type="text/javascript"> YUI().use("datatable-base", "panel", "dd-plugin", function(Y) { //Create the datatable with some gadget information var cols = ["id","name","price"], data = [ {id:"ga-3475", name:"gadget", price:"$6.99"}, {id:"sp-9980", name:"sprocket", price:"$3.75"}, {id:"wi-0650", name:"widget", price:"$4.25"} ], dt = new Y.DataTable.Base({ columnset: cols, recordset: data, summary: "Price sheet for inventory parts", caption: "Price sheet for inventory parts" }), //Create the main modal form panel = new Y.Panel({ srcNode: "#panelContent", width: 250, centered: true, visible: false, modal:true, zIndex:5, headerContent: "Add A New Product", plugins: [Y.Plugin.Drag] }), addRowBtn = Y.one("#addRow"), nestedPanel; //Render The Datatable dt.render("#dt"); //Add the footer buttons to the modal form panel.addButton( { value: "Add Item", action: function(e) { e.preventDefault(); addItem(); }, section: Y.WidgetStdMod.FOOTER } ); panel.addButton( { value: "Remove All Items", action: function(e) { e.preventDefault(); removeAllItemsConfirm(); }, section: Y.WidgetStdMod.FOOTER } ); //Render the modal form panel.render(); //When the addRowBtn is pressed, show the modal form addRowBtn.on('click', function(e) { panel.show(); }); //Define the addItem function - this will be called //when "Add Item" is pressed on the modal form var addItem = function() { var o = {}, id = Y.one('#productId'), name = Y.one('#name'), price = Y.one('#price'); o.id = id.get("value"); o.name = name.get("value"); o.price = price.get("value"); id.set("value", ""); name.set("value", ""); price.set("value", ""); data.push(o); dt.set('recordset', data).render(); dt.render(); panel.hide(); }; //Define the removeItems function - this will be called //when "Remove All Items" is pressed on the modal form //and is confirmed "yes" by the nested panel. var removeItems = function() { data = []; dt.set('recordset', data).render(); dt.render(); panel.hide(); }; //Instantiate the nested panel if it doesn't exist, otherwise just show it. var removeAllItemsConfirm = function() { if (!nestedPanel) { nestedPanel = new Y.Panel({ bodyContent: "Are you sure you want to remove all items?", zIndex: 6, centered:true, width:400, modal:true, buttons: [ { value: "Yes", action : function(e) { e.preventDefault(); nestedPanel.hide(); panel.hide(); removeItems(); }, section: Y.WidgetStdMod.FOOTER }, { value: "No", action: function(e) { e.preventDefault(); nestedPanel.hide(); }, section: Y.WidgetStdMod.FOOTER } ] }).render('#nestedPanel'); } else { nestedPanel.show(); } } }); </script>