[ LiB ] |
The simplest kind of Dreamweaver extension to understand and create is the object. An object , in terms of the Dreamweaver API, is an HTML file that contains, or uses JavaScript to construct, a string of HTML code to insert into the user's document. The user clicks an icon in the Insert bar or selects an item from the Insert menu, and the specified code is inserted into the current document, usually at the insertion point.
As you learned in the preceding chapter, object files are stored in subfolders of the Configuration/Objects folder that correspond to object categories in the Insert bar. Most objects consist of from one to three files, all with the same name but different extensions:
An HTML file (such as table.htm) This is the object file itselfthe file that contains or returns the code to be inserted. This is the only file that must be present to constitute an object.
A JavaScript file (such as table.js) This file contains JavaScript instructions for constructing the code to be inserted, in the form of one or more JavaScript functions, and is called on from the HTML file. This file is optional. It is entirely legal to contain the JavaScript functions in the head section of the object file instead of saving it to an external file. As experienced scripters know, it can be easier to keep track of and update JavaScripts if the code is in a separate filebut this isn't necessary.
An image file (such as table.png) This file contains an 18x18-pixel image that Dreamweaver uses to represent the object in the Insert bar. This file is also optional: If there is no image file, Dreamweaver supplies a generic image icon to represent the object in the panel.
Figure 29.1 shows some typical sets of object files.
Dreamweaver MX 2004 relies heavily on XML files to help construct a unified interface from its many files and extensions. The insertbar.xml file, stored in the Configuration/Objects folder, contains a series of nested XML tags that govern what objects appear where in the Insert bar. Examine this file in a text editor, and you'll see that each object category is created from a pair of category tags. Within a category, subcategories are created by a pair of menubutton tags. Each object's presence in the panel is determined by a button tag. The button tag includes attributes specifying what files the object will call on, when it will appear in the panel, and what its unique identifier is. The Table object, which appears inside the Layout objects subcategory of the Common objects category, looks like this:
<category id="DW_Insertbar_Common" MMString:name= "insertbar/category/common" folder="Common"> [etc] <menubutton id="DW_LayoutTools" MMString:name= "insertbar/layoutTools" image="Common\Table.png" folder="Tables"> [etc] <button id="DW_Table" image="Common\Table.png" enabled= "!_VIEW_LAYOUT" MMString:name="insertbar/table" file="Common\Table.htm"/> [etc] </menubutton> [etc] </category>
Some objects, such as the Email Link object, use dialog boxes to collect user information; others, such as the Horizontal Rule object, don't. The overall structure and procedure for both is the same.
Figure 29.2 shows the HTML file for a simple object that doesn't call a dialog box. Note that the body section is empty. Also note that the objectTag() function is not called anywhere in the file. Dreamweaver calls the function automatically. Figure 29.3 shows the file for the same object, with a dialog box. The body contains a form, which will become the contents of the dialog box. Note that the form does not include a Submit button. Dreamweaver supplies this.
Both files contain the following key elements:
Filename This becomes the Insert menu entry for the object.
Page title This becomes the ToolTip that pops up to identify the object in the Insert bar.
objectTag() function This JavaScript function is the most important element of the object file. This function returns the exact code you want the object to insert into your document, enclosed in quotes. The objectTag() function is part of the Dreamweaver API, so it doesn't need to be defined. It also doesn't need to be called; Dreamweaver calls it automatically when the object is chosen .
HTML form (optional) This becomes the dialog box for collecting user input and customizing the code. Dreamweaver supplies the OK and Cancel buttons automatically.
displayHelp() function (optional) If there is a dialog box, and this function is defined, Dreamweaver adds a Help button to the dialog box. When the user clicks that button, the function is executed.
In the example shown in Figure 29.2, the code returned by the objectTag() function is a simple level 1 heading. Everything between the quotation marks, in the return statement, becomes part of the user's document. In the example shown in Figure 29.3, form input is collected and used to construct the return statement.
The API procedure for processing objects determines how and when Dreamweaver uses the file elements previously discussed. The procedure is as follows :
Pretty simple, eh? To create an object extension, all you have to do is decide what code you want inserted and create a file that matches the structure shown in Figures 29.2 and 29.3. Then create an entry for the object in insertbar.xml to make it part of the Dreamweaver interface.
Not all objects are created equal! Some items lurking in the Insert barespecially those representing server-side code for live data pagesare very complex commands in object disguise. They're way beyond the scope of this chapter. |
In this exercise, you'll get your workspace in order and learn about some of the basic extension-developing features available in Dreamweaver. You will create a custom folder within the Objects folder to store your exercise objects and add data to insertbar.xml so that your custom folder becomes a custom Insert bar category.
Never store your object files loose in the Configuration/Objects folder, or Dreamweaver won't recognize them. Objects must be placed in an existing or new subdirectory within that folder. |
<category id="DW_Insertbar_Development" folder="Development"> </category>
The simplest objects are those that don't call up a dialog box for user input, and therefore always return the same code. The simple object you will create in this exercise is a copyright statementjust the sort of thing you might want to put at the bottom of all your web pages.
To create this object, use the text editor of your choice. Save all the exercise files in the Development folder you created in the preceding exercise.
Use Design view to create and format the copyright statement, and then go to Code view. The code will be there, written for you. |
<html> <head> <title> Title Goes Here </title> <script language="JavaScript"> function objectTag() { return ' inserted code goes here '; } </script> </head> <body> </body> </html>
<html> <head> <title> Tom Thumb Copyright Info </title>
Working with Word? As with any JavaScript return statement, no hard returns can occur within the statement. Make sure your code is written in one long line, or it won't work! |
<html> <head> <title> Tom Thumb Copyright Info </title> <script language="JavaScript"> function objectTag() { return ' <p>© 2003, Tom Thumb</p> '; } </script> </head> <body> </body> </html>
You can get more control over how the object name appears in the Insert menu by editing Configuration/Menus/ menus .xml . |
<category id="DW_Insertbar_Development" folder="Development"> <button name="Tom Thumb Copyright Info" image= "Development\Copyright Statement.gif" id="DW_TomThumb- Copyright" file="Development\Tom Thumb copyright.htm" /> </category>
Don't waste your time making custom icon files for objects while they're still in the development phase. Wait until the object is all polished and perfectly functioning, and then dress it up with a custom icon. (Exercise 29.4 shows how to make an icon file.) |
What If It Doesn't Work?If your object doesn't show up in the Insert bar, you either saved it in the wrong place, you didn't append the .htm/.html extension to the filename, or there might be a syntax error in the insertbar.xml file. If your object shows up, but something is wrong with the code, you'll probably get an error message when Dreamweaver tries to execute the objectTag() function. Dreamweaver error messages are fairly specific as far as what went wrong and what needs fixing. Examine the message, and fix your code accordingly . |
Your simple object is fine as far as it goes, but it's not very flexible or useful because it always returns the same code, no matter what. What if you want to assign a copyright to someone besides good old Tom Thumb? A fully functional object would bring up a dialog box that would ask for user input and would enter that information into the code. That's the object you will build in this exercise.
If you want to improve your skills in creating user-friendly forms, pay attention to all the user interface elements in Dreamweaver. The dialog boxes, inspectors, and panels are beautiful examples of clean, transparent interface design, and they're constructed almost entirely from standard form elements. |
<form name="theForm"> <table> <tr valign="baseline"> <td align="right" nowrap>Name:</td> <td align="left"><input type="text" name="copyrightname"> </td> </tr> <tr valign="baseline"> <td align="right" nowrap>Year:</td> <td align="left"><input type="text" name="copyrightyear"> </td> </tr> </table> </form>
If you're building your form in the Dreamweaver visual editor, method and action properties are automatically added to the <form> tag. Your form doesn't need either of these because it won't be processed in the standard way. You can safely remove these properties from your code. |
function objectTag() { return '<p>© '+document.theForm.copyrightyear.value+', '+document.theForm.copyrightname.value+'</p>'; }
If you think the return statement is too unwieldy to read easily, you can collect the form input into variables and use those to construct the final statement. |
<button name="Copyright Statement" id="DW_CopyrightStatement" file="Development\Copyright Statement.htm" />
What If It Doesn't Work?As with the earlier exercise, if there's a problem with your code, Dreamweaver should give you a helpful error message. Read the error message, try to guess what it means, and then go back to your code and look for problems. Compare your code to the code listed in this exercise to see what might be wrong. The most common things that go wrong in this kind of object file are incorrect references to form elements and mismatched single and double quotes in the return statement. |
Professional-looking objects have their own icons. When the development phase of your object is finished, the final touch is to make an icon file to represent it in the Objects panel.
The requirements for an icon file are as follows:
The file must be a GIF, JPEG, or PNG image file, preferably no larger than 18x18 pixels. (Larger images work, but they're squashed into an 18x18-pixel space in the panel.)
It's customary, though not required, to give the image the same name as the object file it goes with. For this exercise, therefore, name the image Copyright Statement.gif .
It's customary, though not required, to store the image file in the same folder as the object file it goes with. For this exercise, the icon file must be stored in the Development folder.
The icon must be called on in insertbar.xml by adding the image attribute to the <button> tag.
Icon files can have any colors you like, and the icon can look like anything you can imagine. You'll quickly discover, however, that designing icons that clearly communicate what they represent, when there are only 324 pixels to play with, is a real art.
Congratulations! You now have the foundation skills for making Dreamweaver objects. How can you make objects work for you? As you have seen, any piece of HTML code that you repeatedly use in web pages is a candidate for an object. The best object candidates, however, are pieces of code that you need to customize and then insertchanging the name and email address, specifying a certain URL to link to, and so forth.
Any time you find yourself going through the same steps repeatedly as you add content to web pages, ask yourself the following questions:
Is the code I'm inserting similar enough each time that I could create an object from it?
Are there differences in the code each time, or is it exactly the same? (If the code is exactly the same each time, requiring no customization, it might be more efficient to use a recorded command or snippet.)
How many more times do I think I'm likely to need to insert this code? Will my need continue after today? After the current assignment? Indefinitely? Creating an object is a time-consuming solution (not smart for a need that is only very temporary).
Do I have some extra time right now to devote to making this object? (Never try a new, challenging solution when your deadline is 45 minutes away.)
Depending on your answers, you'll know whether it's time to crack open Dreamweaver and fit a new custom object inside.
[ LiB ] |