- Before Getting Started
- Working with Objects
- Working with Behaviors
- Sharing Extensions
- Summary
Sharing Extensions
If your object or behavior is helpful to you, maybe it will be helpful to others. Of course, the more people you share with, the more your extension will need to be cleaned up, dressed up, and made reliable and understandable. If you're not sure what this means, take a look at the extensions that ship with Dreamweaver. They're well-documented; the code is bulletproofed; they have standardized, well-designed interfaces; and they're nicely packaged. None of this happens by accident.
Do You Want to Share?
First of all, you need to ask yourself whether you really want to share. If an object or behavior is probably going to be useful only to your personal workflow, client requirements, or current assignment; if your code is lacking many of the niceties that other people will expect; and if you don't have time to spare getting things in shape to share, you can stop reading right here, and just continue using your custom-made extensions yourself.
On the other hand, you might work in a group setting where all the Dreamweaver users in your office would benefit from your custom extension. Or, you might be a public-minded soul who thinks that the world-at-large can benefit from your brilliance, and you want to submit your extension to the Macromedia Exchange. If that's you, read on!
Bulletproofing
Bulletproofing means making sure that your extension will work under a variety of conditions without breaking. The more diverse circumstances your extension will be used in, the more bulletproof it should be.
How do you bulletproof? Read on.
Test the Inserted Code
No object or behavior is better than the code it inserts. Make sure that your code is worth inserting. Create sample Web pages using code that was inserted with your custom extension. Does the code work successfully across platforms? Does it work in different browsers? Macromedia recommends making sure that your code works in all the major version 4+ browsers and "fails gracefully" in version 3 browsers.
Test the Insertion Process
If there's not a dialog box with opportunities for user input, then the code should insert the same every time. If there is a dialog box, however, then consider the following:
What happens if the user leaves all values at their defaults and just clicks OK? Does Dreamweaver crash? Does garbage get inserted into the user's document?
What happens if the user enters unusual or wrong data in the dialog box?
What do you want to have happen in either of these circumstances? You can code the extension so that valid code gets entered no matter what, you can cause an alert message, or you can simply make the extension not insert code at all unless the user input meets certain criteria. It's up to you.
NOTE
Macromedia recommends that you let the user enter whatever input he desires, as long as it's not going to actually "break" the pagein other words, a result is considered acceptable if the browser will simply ignore the incorrect or nonstandard code rather than generating JavaScript errors, crashing, or exhibiting any other noticeable problems. This is recommended because coding standards evolveso what's nonsensical code today might be perfectly valid tomorrowand because users don't like being boxed in by too-rigid requirements for code entry.
A good example of a well-bulletproofed insertion process is Macromedia's own table object. As a learning experience, open Dreamweaver and try using this object with strange dialog box entries. You'll find that the following are true:
Left to its defaults, the object inserts a valid table based on the last time the object was used.
Fields that correspond to optional table parameters can be left blank, with the result that the inserted code simply doesn't include those parameters.
The rows and columns fields, which must have certain values for the table code to function, will not accept invalid entries. Any non-numeric entry, or 0, will be replaced by a 1.
Test the Object/Behavior Itself
You already know that it works on your computer, with your version of Dreamweaver and your operating system. But ask yourself these questions:
Does it work with older versions of Dreamweaver?
Does it work on versions of Dreamweaver that are configured differently than yours?
Does it work on computers that are configured differently than yours?
Does it work on different platforms?
It may be that some of these things don't matter. If your extension needs to work only in your company, and if you only have PCs running Windows 2000 and Dreamweaver 4, then who cares if it runs correctly on Dreamweaver 2 on a Macintosh or Windows 95? Even though you might not need to fix a certain limitation, though, it's a good idea to be aware of it so that you can share intelligently.
TIP
Macromedia requests that all behaviors submitted to the Exchange include, as part of the defined function, the version of Dreamweaver that they are intended to work with. This information should be added as a comment to the defined function, like this:
function resizeBrowserWindow(width,height) { //v3.0
Exercise 22.13 Testing a Custom Behavior
This exercise uses the previously mentioned testing criteria to improve the Resize Window behavior we created earlier in this chapter. (If you don't have the complete code for this object, you can find it on the accompanying CD-ROM.)
Test the inserted code. The inserted code, in this case, is a JavaScript function that resizes the current browser window. How robust is this code?
Adjust the behavior accordingly. You can't do anything about the script's failure to work in Netscape 3. But you can ask yourself if it will "fail gracefully" in that browser.
If you try the script in Netscape 3, you'll see that the browser simply ignores itno harm, no foul. This qualifies as failing gracefully, so there's no adjustment needed.
-
Try the behavior with invalid input. Try the resize behavior and see what it does if you enter non-numeric data, or zeroes, or empty fields, as shown in Figure 22.25. What happens?
Rewrite the behavior to disallow invalid input. Rewrite the applyBehavior() function so that the form fields are each validated before the function call is inserted. Valid data should be positive integers only; you also might want to consider extremely large numbers invalid. If no valid data is present, make the behavior resort to its default values.
Try out the revised behavior. What happens now when you try to break the behavior by entering some bizarre data (or nothing at all) in the dialog box? It should default to 300x300, or whatever default width and height you decided on. Your behavior is now bulletproofor, at least, more bulletproof than it was before.
According to the O'Reilly online JavaScript reference provided with Dreamweaver, this function should work in Netscape 4+ and in Internet Explorer (see Figure 22.24).
Figure 22.24 Dreamweaver's Reference window showing the O'Reilly information for the resizeTo() function.
If you want to be thorough, test this by actually trying the code in as many different browser/platform configurations as you have access to. For purposes of this exercise, assume that the O'Reilly information is correct.
See what happens when the behavior is used with default values. As your behavior is currently written, there are no default values for the width and height arguments. What will happen if the user tries to enter the behavior that way? Try it, and you'll see that the code is inserted incompletely:
<a href="#" onMouseUp="resizeBrowserWindow(,)"> click </a>
The simplest way to avoid this problem is to put default values into the dialog box. Do this by altering the form code so that it looks like this:
<table> <tr valign="baseline"> <td align="right" nowrap>New width:</td> <td align="left"> <input type="text" name="width" size="8" value="300"> </td> </tr> <tr valign="baseline"> <td align="right" nowrap>New height:</td> <td align="left"> <input type="text" name="height" size="8" value="300"> </td> </tr> </table>
Do this and then try out the revised behavior. The dialog box should always come up with the default values in place.
You already know that leaving the fields empty is a dangerous proposition. Entering zeroes is equally dangerous. Entering non-numeric data will generate JavaScript errors in Internet Explorer and is definitely a bad idea. Perhaps you should fix this problem.
Figure 22.25 Non-numeric data, or zeroes, entered into the Resize Window behavior dialog box, resulting in invalid arguments passed to the function in the user's document.
Depending on your scripting style, you may choose to implement this error-checking in any number of ways. Your code may end up looking like this:
function applyBehavior() { var width=document.theForm.width.value; var height=document.theForm.height.value; if (width=="" || width<1 || width>2000 || parseInt(width) != width) { width=300; } if (height=="" || height<1 || height>2000 || parseInt(height) != height) { height=300; } return "resizeBrowserWindow(" + width + "," + height + ")"; }
Design: Testing for Usability
Although you can test for technical errors, the best people to test for design and usability errors are other peoplepreferably people who have no knowledge of your development process and who are not themselves software developers. Beta testers may indeed find ways to break your extensionin which case, you're back to bulletproofing.
More likely, though, they'll find problems in your design. Is the object or behavior's name self-explanatory, as it appears in menu listings and ToolTips? If it's an object, is its icon communicative and intuitive? If it has a dialog box, is the dialog box attractive and intuitive to use? Does the whole interface blend in well with the Dreamweaver main interface? Is the desired purpose of the extension clear? Does the extension do what users think it's going to do? Is it lacking some key functionality? Do they perceive it as potentially useful?
If the answer to any of these questions is a resounding "No!," you have some redesigning to do.
TIP
Macromedia offers a set of UI guidelines to help you create intuitive, functional interfaces that blend in well with the rest of the Dreamweaver interface. To see these guidelines, go to the Macromedia Exchange for Dreamweaver page ( http://www.macromedia.com/exchange/dreamweaver) and click the Site Help topic Macromedia Approved Extensions. The code for this chapter has been written to follow these guidelines as much as possible.
Documenting
So, you think you're going to remember what this extension is supposed to accomplish six months from now or a year from now? Probably not. And if you can't remember it, obviously no one else can, either. Always, always, always document what you're doingfor your benefit and everyone else's.
Commenting
Always comment your code. Always. Macromedia recommends ityou know it's the right thing to do. Commenting will help you troubleshoot and update the object or behavior in the future. It also will help others learn from your process. (The examples shown so far in this chapter haven't been commented so that you could better examine the code for learning purposes. In the real world, they would be full of comment lines.)
Online Help
According to Macromedia, every extension should have some sort of online help, accessible from the extension's dialog box. Dreamweaver is configured to make adding help files easy for you.
Help in the dialog box. If your object or behavior is so simple that a sentence or two is all the explanation that anybody will ever need, you can put that in the dialog box. Macromedia recommends that you add a table cell at the bottom of the layout, with the cell background color set to #D3D3D3. (Figure 22.26 shows an example of this.)
Help in a help file. If your extension needs more explanation than a sentence or two, put the information in an HTML file. Store the HTML file in a new, personalized folder in the Configuration/Shared folder. (Figure 22.27 shows an example of this.) Place a Help button in the extension's dialog box, linked to that file.
Figure 22.26 The Contact Info object, with a brief help statement added to the bottom. The top view, taken from Dreamweaver layout view, shows the additional table cell used for formatting.
Figure 22.27 The Resize Window behavior dialog box, with a Help button. Clicking the button tells the behavior file to call on a help file (ResizeWindowHelp.html) in a custom folder (Development) in the Configuration/Shared folder.
Exercise 22.14 Adding Online Help to Your Behavior
In this exercise, you'll refine your Resize Window behavior by adding a Help button to the dialog box and linking it to an HTML file in the Shared folder.
Create a folder in the Shared folder to store your help documents.
Create the HTML file. Your help page should include information on what the behavior does, every field and what content it can accept, and any other information you think users will find helpful.
Add the Help button to your behavior's dialog box. Open the behavior file ResizeWindow.js in your text editor. Add this framework code for the function:
Link the Help button to your help file.
Using Explorer or the Finder, open the Configuration folder and the Shared folder inside it. Create a new folder in here; call it Development. Figure 22.28 shows how the folder structure should look.
Figure 22.28 The folder structure of the Configuration/Shared folder with the new Development folder created inside.
You can create your own HTML file or use the file ResizeWindowHelp.html, located on the CD. Figure 22.29 shows an example of what a typical help file might look like.
Save the file as ResizeWindowHelp.html in the Configuration/Shared/ Development folder.
function displayHelp() { }
The displayHelp() function is part of the Dreamweaver API; when present, it is called automatically, so you don't need to call it.
When you've done this, reload extensions and try out your revised behavior. When the dialog box comes up, there should be a Help button in place, like the one shown in Figure 22.27. (Of course, because the function is empty so far, clicking the button won't get you anywhere.)
Figure 22.29 The Resize Window behavior's help file, as it will appear when viewed in a browser.
The standard thing you do with Help buttons is link them to help files. This is done with a function that is part of the Dreamweaver API, dw.browseDocument(). This function takes an absolute URL as its argument. If your help file is located on the Webmaybe on your own company Web site so that users have to come to you to get the latest and greatest helpjust enter an absolute Web address as the argument. In this case, the help function would look like this:
function displayHelp() { dw.browseDocument("http://www.mycompany.com/dwHelpFiles/ResizeWindow.html"); }
Because your help file is going to end up on the user's hard drive, the code needs to return an absolute pathname to that file. Luckily, the Dreamweaver API function, dw.getConfigurationPath(), returns the absolute address to the Configuration folder. All you have to do after getting that information is figure out the path to the help file relative to this root and concatenate the two together. So, the code you should enter looks like this:
function displayHelp() { var myURL = dw.getConfigurationPath(); myURL += "/Shared/Development/ResizeWindowHelp.html"; dw.browseDocument(myURL); }
Enter this code. Then, reload extensions and try it out. If the proper help page doesn't load, double-check the code and tweak it until it does. Make sure that you've entered the path from the Configuration folder to your help file exactlydepending on how you've named your files and folders, your path may differ from the one shown here.
NOTE
The two API functions introduced here are both methods of the Dreamweaver object. Methods of this object can be written as dreamweaver.functionName() or dw.functionName(). The second choice offers fewer opportunities for typos.
Distributing
How are you going to get your lovely object or behavior into Configuration folders everywhere? Read on for instructions.
Packaging for the Extension Manager
The Extension Manager is becoming the standard method of painless extension installation. Therefore, this is the most accessible way to share your extensions.
Lucky for us, the Extension Manager not only installs extensions, but it also packages them up neatly into special installation files. The process is even relatively painless. The steps are listed here:
Put all the required files (help files, HTML files, JS files, and GIF icons) in one folder, outside the Configuration folder.
Create an installation file. This is an XML document with the filename extension .mxi that contains all the instructions needed for installation: where the files should be stored, what versions of Dreamweaver and what platforms the extension requires, the author's name, the type of extension, and a description. The formatting required is very exact. The best approach for beginners is to start from the samples included with the Extension Manager. These files include a blank file (blank.mxi) to use as a template and a sample file (sample.mxi) filled in with information for a simple object.
Launch the Extension Manager, and go to File/Package Extension.
See Figure 22.30 for a sample folder containing all the proper files to package the Contact Info file. This last exercise takes you through all the steps to create this folder.
Figure 22.30 The assembled elements of the Contact Info object, all ready for packaging.
Exercise 22.15 Packaging an Extension
In this exercise, you'll pack up the Contact Info object for sharing with the world.
Copy all needed files into one folder. Somewhere on your hard drive, outside the Configuration folder, create a new folder. Name it whatever you like and will remember (something like Contact Info Files, maybe).
Find all the files that make up the behavior, and copy them there. Files that you should include are listed here:
- Contact Info.html
- Contact Info.js
- Contact Info.gif
Open the blank.mxi file to use in creating the installation file. Duplicate it and save it in your collection folder as ContactInfo.mxi.
On your hard drive, find the Extension Manager application folder. Inside that folder, find the Dreamweaver/Samples folder. Inside there, you should see blank.mxi. (Figure 22.31 shows where to find these items.)
Figure 22.31 The Extension Manager application folder structure, showing sample.mxi and blank.mxi.
Afteryou've made the duplicate file, open it in your text editor.
TIP
You can download a PDF file containing detailed instructions for creating installation files from the Macromedia Web site. Go to the Macromedia Exchange for Dreamweaver page ( http://www.macromedia.com/exchange/dreamweaver), and click the Site Help topic Macromedia Approved Extensions.
Fill in the blanks with the information for your object. The blank file has all the framework you need. By examining the sample file, you can get an idea how it should be formatted. For your extension, fill in the blanks until your code looks like that shown in Figure 22.32.
A few tips about filling in the code:
For the author name. Enter your name (John Smith, Web Genius has been entered herethere's no law against being fanciful).
For the filenames. Enter the complete path from the Dreamweaver application folder root, as shown. If you want your extension to create any new folders in existing folders, enter them as part of the path (SmithStuff has been entered here to create a new folder within the Objects folder). If the object included any added folders within the Shared folder, they would have been added in the same way.
For the version number. Your extension, like any other piece of software, gets its own version number. Start with 1.0, and increment the number if you later revise the extension.
Figure 22.32 The complete code for ContactInfo.mxi. Information that has been added to the framework from blank.mxi is highlighted.
Package everything together with the Extension Manager. Launch the Extension Manager. Go to File/Package Extension.
For the name of your extension, choose something descriptive that obeys the standard naming conventions (no empty spaces, no more than 20 characters, no special characters). Make sure that you leave the .mxp extension in place.
When you're asked to choose a file, choose ContactInto.mxi.
If there aren't any problems, the Manager will generate an extension file in the same folder as the .mxi file. If there are problems, you'll get an error report. Most often, these are problems with the .mxi file. If there are, go back to your text editor, fix the reported errors and try again.
Figure 22.33 shows how this process will look in the Extension Manager.
Figure 22.33 The steps through the packaging process, as they appear in the Extension Manager.
Use the Extension Manager to install your new extension. Quit Dreamweaver, if it's running. In the Extension Manager, go to File/Install. When the dialog box comes up, browse to ContactInfo.mxp.
If everything's hunky dory, you should get an alert message telling you that the extension was installed successfully. Your custom extension should also now appear in the Extension Manager window, as shown in Figure 22.34.
Launch Dreamweaver and check that everything installed correctly. If all went as smoothly as reported, a new category should appear in the Objects panel, named SmithStuff or whatever you called your custom folder. Your object should be the only thing in that category. Check the ToolTip; try inserting it. Then pat yourself on the backyou did it!
Figure 22.34 The Extension Manager window, showing the installed Contact Info object.
Submitting to the Macromedia Exchange
The ultimate in sharing is submitting your extension file to the Macromedia Exchange. When you have the .mxp file, the procedure is simple: Go to the Macromedia Exchange Web site and click the Submit button at the top of the page. Then follow the instructions to submit (see Figure 22.35).
Figure 22.35 The Macromedia Exchange home page with the Submit button.
When you have submitted an extension, Macromedia engineers will run it through a series of tests. One of three things will happen:
If it fails, it gets returned to you with comments.
If it passes the basic tests, it gets put on the Web site as a Basic, or unapproved, extension.
If it also passes the more comprehensive tests, it becomes a Macromedia Approved Extension.
To learn more about the testing process and how to get your extensions accepted and approved, visit the Web site and click any one of the Site Help FAQ topics. This will take you to an extensive categorized list of questions and answers.