This is an exercise to construct a navigation bar in TypeScript using the principles of object-oriented programming (OOP).
The navigation bar itself and the items in the navigation bar are all objects. They share abstract properties and are all extensions of the same abstract class.
For an example implementation of this navigation bar, see OOP Navigation Bar on JSFiddle.
I coded this project as part of a Follow the Pattern course.
To create your navigation bar, follow these steps:
- Create a copy of the file
source/oop-navbar.ts, and open it with your text editor. - At the bottom of the file, delete the example.
- Create a
NavBarobject, specifying the title of the navigation bar in a string-type parameter. - Create a
NavBarItemobject for each item in your navigation bar. EachNavBarItemobject has two parameters. The first parameter is the parent item which must be anotherNavBarItemobject. The second parameter is a string which specifies the text displayed in the navigation bar item. For the first level of items, the parent item is the navigation bar itself (theNavBarobject). - Call the
SetItemsmethod of theNavBarobject, specifying all theNavBarItemobjects that you have created as the parameters. - Call the
Rendermethod of theNavBarobject. This returns the rendered HTML elements. Add the return value to the DOM with a standard JavaScript method likeappend. - Compile the TypeScript code with a tool like Node.js.
- Link the compiled Javascript code in the header of the HTML file where you want to add the navigation bar.
- In the HTML file, load the Foundation framework. For more information, see the Foundation documentation.
As a result, you have created your navigation bar.