|MySQL Conference and Expo April 14-17, 2008, Santa Clara, CA|
Extending Dreamweaver: Let Dreamweaver Create Your Menus07/06/2001
Welcome to the third part of my Dreamweaver series. While part one was an introduction to Dreamweaver's menu configuration and commands, we created our own first command in the second part by introducing the Dreamweaver DOM basics.
Today's menu is a bit more sophisticated; we'll be using various features and API functions to build a new object that will have Dreamweaver create a tree-style menu for your site's navigation in a single click.
So, what's this all about? In many cases, when you're building your site, you'll organize the files in such a manner that your directory structure can be directly modeled into a navigation system. For hierarchical structures, a tree-style menu has proven to be very useful, since it's a natural way to display this kind of structure.
The idea is actually simple: use a bunch of Dreamweaver API functions, which will scan your complete site or a subfolder of it to build a tree-style hierarchical menu widget automatically. The menu items are being created from the file names -- an enhanced version could also scan the files for their titles. See the examples page for samples of the menus being created.
Working with Files
Dreamweaver defines dozens of functions to work with files that are divided into different APIs. The File I/O API contains 10 different functions that form a basic set to work with files; for example, you can create, read, or delete files and folders or their attributes. The methods of the File I/O API can be accessed via the
You're limited to working with
The file manipulation functions are responsible for opening, browsing, and saving documents and style sheet files inside the editor environment. The largest function group, containing about 50 functions, handles the site files and site map. Among other things, these functions allow you to check links, make and retrieve selections, access current sites, define new sites, check out files. These functions are invoked as methods of the
Step 1: Get the Selection
OK, this is where we need to start. We must access the selection and make sure that it's only a single folder; it will later act as the menu structure's root node (I'll only excerpt the interesting parts of the source code -- you can either browse the code directly or download it from our library):
But things are a little more complicated than that. What happens if this folder contains one or more sub-folders?
The solution comes in the form of one of the most powerful programming techniques: recursion.