Skip to content

Instantly share code, notes, and snippets.

@joebrislin
Created April 11, 2012 19:47
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joebrislin/2361850 to your computer and use it in GitHub Desktop.
Save joebrislin/2361850 to your computer and use it in GitHub Desktop.
Secondary Navigation Generation for ColdFusion and Twitter Bootstrap
<cfoutput>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Bootstrap, from Twitter</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="">
<meta name="author" content="">
<!-- Le styles -->
<link href="../assets/css/bootstrap.css" rel="stylesheet">
<link href="../assets/css/secondaryNav.css" rel="stylesheet">
<style>
body {
padding-top: 60px; /* 60px to make the container go all the way to the bottom of the topbar */
}
</style>
<link href="../assets/css/bootstrap-responsive.css" rel="stylesheet">
<!-- Le HTML5 shim, for IE6-8 support of HTML5 elements -->
<!--[if lt IE 9]>
<script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<!-- Le fav and touch icons -->
<link rel="shortcut icon" href="../assets/ico/favicon.ico">
<link rel="apple-touch-icon-precomposed" sizes="114x114" href="../assets/ico/apple-touch-icon-114-precomposed.png">
<link rel="apple-touch-icon-precomposed" sizes="72x72" href="../assets/ico/apple-touch-icon-72-precomposed.png">
<link rel="apple-touch-icon-precomposed" href="../assets/ico/apple-touch-icon-57-precomposed.png">
</head>
<body>
<div class="navbar navbar-fixed-top">
<div class="navbar-inner">
<div class="container">
<a class="btn btn-navbar" data-toggle="collapse" data-target=".nav-collapse">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</a>
<a class="brand" href="#">Project name</a>
<div class="nav-collapse">
<ul class="nav">
<li class="active"><a href="#">Home</a></li>
<li><a href="#about">About</a></li>
<li><a href="#contact">Contact</a></li>
</ul>
</div><!--/.nav-collapse -->
</div>
</div>
</div>
<div class="container">
<cfscript>
variables.subItems = [];
arrayAppend( variables.subItems, { title="Parent Level 1", link="/level1" });
arrayAppend( variables.subItems, { title="Parent Level 2", link="/level2" });
variables.nestedItems = [];
arrayAppend( variables.nestedItems, { title="Sub Level 1", link="/sublevel1" });
arrayAppend( variables.nestedItems, { title="Sub Level 2", link="/sublevel2" });
arrayAppend( variables.nestedItems, { title="Sub Level 3", link="/sublevel3" });
arrayAppend( variables.subItems, { title="Nested", subItems=variables.nestedItems });
variables.utilityService = createObject("component", "utility");
variables.subNav = variables.utilityService.dspSubNavigation( navItems=variables.subItems, selectedItem = cgi.path_info );
</cfscript>
<div class="subnav subnav-fixed">#variables.subNav#</div>
<h1>Bootstrap starter template</h1>
<p>Use this document as a way to quick start any new project.<br> All you get is this message and a barebones HTML document.</p>
</div> <!-- /container -->
<!-- Le javascript
================================================== -->
<!-- Placed at the end of the document so the pages load faster -->
<script src="../assets/js/jquery.js"></script>
<script src="../assets/js/bootstrap-transition.js"></script>
<script src="../assets/js/bootstrap-alert.js"></script>
<script src="../assets/js/bootstrap-modal.js"></script>
<script src="../assets/js/bootstrap-dropdown.js"></script>
<script src="../assets/js/bootstrap-scrollspy.js"></script>
<script src="../assets/js/bootstrap-tab.js"></script>
<script src="../assets/js/bootstrap-tooltip.js"></script>
<script src="../assets/js/bootstrap-popover.js"></script>
<script src="../assets/js/bootstrap-button.js"></script>
<script src="../assets/js/bootstrap-collapse.js"></script>
<script src="../assets/js/bootstrap-carousel.js"></script>
<script src="../assets/js/bootstrap-typeahead.js"></script>
</body>
</html>
</cfoutput>
/* Subnav */
.subnav {
width: 100%;
height: 36px;
background-color: #eeeeee; /* Old browsers */
background-repeat: repeat-x; /* Repeat the gradient */
background-image: -moz-linear-gradient(top, #f5f5f5 0%, #eeeeee 100%); /* FF3.6+ */
background-image: -webkit-gradient(linear, left top, left bottom, color-stop(0%,#f5f5f5), color-stop(100%,#eeeeee)); /* Chrome,Safari4+ */
background-image: -webkit-linear-gradient(top, #f5f5f5 0%,#eeeeee 100%); /* Chrome 10+,Safari 5.1+ */
background-image: -ms-linear-gradient(top, #f5f5f5 0%,#eeeeee 100%); /* IE10+ */
background-image: -o-linear-gradient(top, #f5f5f5 0%,#eeeeee 100%); /* Opera 11.10+ */
filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#f5f5f5', endColorstr='#eeeeee',GradientType=0 ); /* IE6-9 */
background-image: linear-gradient(top, #f5f5f5 0%,#eeeeee 100%); /* W3C */
border: 1px solid #e5e5e5;
-webkit-border-radius: 4px;
-moz-border-radius: 4px;
border-radius: 4px;
}
.subnav .nav {
margin-bottom: 0;
}
.subnav .nav > li > a {
margin: 0;
padding-top: 11px;
padding-bottom: 11px;
border-left: 1px solid #f5f5f5;
border-right: 1px solid #e5e5e5;
-webkit-border-radius: 0;
-moz-border-radius: 0;
border-radius: 0;
}
.subnav .nav > .active > a,
.subnav .nav > .active > a:hover {
padding-left: 13px;
color: #777;
background-color: #e9e9e9;
border-right-color: #ddd;
border-left: 0;
-webkit-box-shadow: inset 0 3px 5px rgba(0,0,0,.05);
-moz-box-shadow: inset 0 3px 5px rgba(0,0,0,.05);
box-shadow: inset 0 3px 5px rgba(0,0,0,.05);
}
.subnav .nav > .active > a .caret,
.subnav .nav > .active > a:hover .caret {
border-top-color: #777;
}
.subnav .nav > li:first-child > a,
.subnav .nav > li:first-child > a:hover {
border-left: 0;
padding-left: 12px;
-webkit-border-radius: 4px 0 0 4px;
-moz-border-radius: 4px 0 0 4px;
border-radius: 4px 0 0 4px;
}
.subnav .nav > li:last-child > a {
border-right: 0;
}
.subnav .dropdown-menu {
-webkit-border-radius: 0 0 4px 4px;
-moz-border-radius: 0 0 4px 4px;
border-radius: 0 0 4px 4px;
}
/* Fixed subnav on scroll, but only for 980px and up (sorry IE!) */
@media (min-width: 980px) {
.subnav-fixed {
position: fixed;
top: 40px;
left: 0;
right: 0;
z-index: 1020; /* 10 less than .navbar-fixed to prevent any overlap */
border-color: #d5d5d5;
border-width: 0 0 1px; /* drop the border on the fixed edges */
-webkit-border-radius: 0;
-moz-border-radius: 0;
border-radius: 0;
-webkit-box-shadow: inset 0 1px 0 #fff, 0 1px 5px rgba(0,0,0,.1);
-moz-box-shadow: inset 0 1px 0 #fff, 0 1px 5px rgba(0,0,0,.1);
box-shadow: inset 0 1px 0 #fff, 0 1px 5px rgba(0,0,0,.1);
filter: progid:DXImageTransform.Microsoft.gradient(enabled=false); /* IE6-9 */
}
.subnav-fixed .nav {
width: 938px;
margin: 0 auto;
padding: 0 1px;
}
.subnav .nav > li:first-child > a,
.subnav .nav > li:first-child > a:hover {
-webkit-border-radius: 0;
-moz-border-radius: 0;
border-radius: 0;
}
}
@media (min-width: 1210px) {
/* Update subnav container */
.subnav-fixed .nav {
width: 1168px; /* 2px less to account for left/right borders being removed when in fixed mode */
}
}
component{
/*
*
* @author J. Brislin
* @company BlueRidge Interactive
* @hint Generates the display for sub navigation - required to pass in an array of navItems with the following structure
*
* var subItems = [];
*
arrayAppend( subItems, { title="Parent Level 1", link="/level1" });
arrayAppend( subItems, { title="Parent Level 2", link="/level2" });
var nestedItems = [];
arrayAppend( nestedItems, { title="Sub Level 1", link="/sublevel1" });
arrayAppend( nestedItems, { title="Sub Level 2", link="/sublevel2" });
arrayAppend( nestedItems, { title="Sub Level 3", link="/sublevel3" });
arrayAppend( subItems, { title="Nested", subItems=nestedItems });
subNav = variables.utilityService.dspSubNavigation( navItems=subItems, selectedItem = cgi.path_info );
*
*/
public string function dspSubNavigation( required array navItems, numeric level = 1, string mainClass="", string activeClass="active", string DDLClass = "", string DDLLinkClass="", string DDLListClass="", string selectedItem = "none", boolean useBootstrap = true ){
// variable initialization
var cssClass = "";
var navHtml = "";
// if using bootstrap - set default classes
if( arguments.useBootstrap ){
arguments.mainClass = "nav nav-pills";
arguments.DDLClass = "dropdown";
arguments.DDLLinkClass = "dropdown-toggle";
arguments.DDLListClass = "dropdown-menu";
}
if( arguments.level gt 1 ){
arguments.mainClass = ""; // only utilize main class for top level of navigation
if( len( trim( arguments.DDLListClass ) ) ) arguments.mainClass = arguments.DDLListClass; // nested levels use Dropdown Class as main class if present
}
// check to see if css class is needed
if( len( trim( arguments.mainClass )) ){
cssClass = ' class="' & arguments.mainClass & '"';
}
if( arrayLen( arguments.navItems ) ){
navHtml &= '<ul' & cssClass & '>'; // open main ul
for( var i=1; i lte arrayLen(arguments.navItems); i++ ){
var item = arguments.navItems[i];
// check to see if sub items exist
var hasSubItems = false; // variable reset
if( structKeyExists( item, "subItems" ) ) hasSubItems = true;
/* Open Nav Item */
navHtml &= '<li'; // open nav item
if( ( hasSubItems && len( trim( arguments.DDLClass ) ) ) || ( structKeyExists( item, "link" ) && arguments.selectedItem contains item.link ) ){ // add necessary css classes - Active & Dropdown
navHtml &= ' class="';
if( structKeyExists( item, "link" ) && arguments.selectedItem contains item.link ) navHtml &= arguments.activeClass & ' ';
if( hasSubItems && len( trim( arguments.DDLClass ) ) ) navHtml &= arguments.DDLClass & ' '; // append class to generate dropdown if necessary
navHtml &= '"';
}
navHtml &= '>'; // open nav item - closing bracket
/* Nav Item Link */
navHtml &= '<a';
if( hasSubItems && len( trim( arguments.DDLLinkClass ) ) ) navHtml &= ' class="' & arguments.DDLLinkClass & '" data-toggle="dropdown"'; // append class to generate dropdown if necessary
if( structKeyExists( item, "link" ) && len( trim( item.link ) ) ) navHtml &= ' href="' & item.link & '"';
navHtml &= '>' & item.title;
if( hasSubItems) navHtml &= ' <b class="caret"></b>'; // append caret for dropdown navigation if necessary
navHtml &= '</a>';
/* SubItem Navigation - recursively build dropdowns n levels */
if( hasSubItems ){
navHtml &= THIS.dspSubNavigation(
item.subItems,
arguments.level + 1,
"",
arguments.activeClass,
arguments.DDLClass,
arguments.DDLLinkClass,
arguments.DDLListClass,
arguments.selectedItem
);
}
/* Close Nav Item */
navHtml &= '</li>'; // close nav item
}
navHtml &= "</ul>"; // close main ul
}
return navHtml;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment