So far we have learned just enough about the Document Object Model (DOM) to facilitate testing our JavaScript problem solver.

We have used the DOM and some basic HTML elements to display the results of JavaScript test calls in simple dynamically created web pages.

Now we will exploit the DOM and Cascading Style Sheets (CSS) to create a full graphical user interface (GUI) for our problem solver, as shown below.

We have already used these aspects of the DOM to display results of testing our JavaScript problem solver code:
While the DOM allows us to access the parts of a displayed web page, CSS (Cascading Style Sheets) provides a language for styling a web page's presentation.

This involves:

CSS strictly separates web page data from its presentation and therefore employs the Model-View-Controller design pattern.
The fundamental component of CSS is a declaration, given by a property/value pair. For example, in this declaration:
	width: 700px
	
the property is "width" and the value is "700px". Multiple declarations can be separated with semicolons:
	width: 700px; height: 600px
	
See Tutorial on the menu to the left for a link to the W3 Schools CSS Tutorial.

Here are the declarations used to style the JavaScript FWGC problem solver that opened this section:

        width: 700px;
        height: 600px;
        border: 5px solid gray;
        padding: 10px;
        margin: 10px;
        background-color: LightBlue;
        position: absolute; 
	
The border property specifies features of any border drawn around an element, in this case width (5px), style (solid), and color (gray).

The padding property specifies the space around elements inside of any border surrounding it.

The margin property specifies the space around elements outside of any border.

This section describes how to use CSS in HTML in three general ways:
First we show a simple HTML file with no CSS styling. The body is composed of a single div element that displays as shown at the right.

A simple way to style HTML is to use the HTML style attribute which expects CSS style declarations.

The following HTML file adds a style attribute to the div element.

We can also define CSS rules specifying styles. A rule looks like this:
selector { declarations }
	
where declarations is a semicolon-separated list of property/value pairs as we have seen, and selector indicates the elements to which the declarations are applied.

Rules can be specified in an HTML document's head element using the style element, as in the example below.

Note that the selector has the form "#name," where name is the value of the "id" attribute of one of the two div elements in the page, so only that div will be given the associated style.

If the selector in a CSS rule does not specify a specific element id, like "#div1," but a general element name, like "div," then all div elements in the page will be given the associated styles, as in the example below.

Note that both div's are styled regardless of their ids.

If the selector in a CSS rule has the form ".name," then name becomes a CSS "class name" for the associated styles.

In the example below, the class is named "outer," which is the value of the class attribute given to one of the two div's in the page, so that div is styled accordingly.

CSS classes are useful because they can be easily applied to elements dynamically created using JavaScript.

Generally useful CSS style rules can be placed into files called external style sheets. The advantages are two-fold: Below the CSS style class outer has been placed in the file styles.css.

In the HTML file, the style element has been replaced by a link element indicating where the styles are to be found. Otherwise the HTML file is the same as the previous one.

styles.css:

To learn about the various CSS properties and their values, see the W3 Schools CSS Tutorial (new window).
In this section we present the development of a constructor function called ProblemPanel and associated CSS styles that ultimately display the FWGC problem solver:

At each step, we show: The test makes use of these facts: The test's display is created with this HTML code:

In the first step, we create the display shown at right. Note:
In the next step, we add a welcome.
In this step, we add the problem's introductory text.
In this step we add the display of the current state of the problem.
In this step we add the display of the move buttons:

The buttons in this version work correctly for valid moves, and the problem can be solved. However, no messages are displayed, and there is no way to reset the problem to the initial state. These are described next.

Finally, we add messages and a reset button:

This completes the development of the ProblemPanel object.

Recall the role of the ProblemGUI class in the Java Problem Solver, shown in the class diagram below.

The ProblemPanel constructor in JavaScript plays an analogous role to ProblemGUI in Java:

In the Java application, multiple problem domains were placed in JavaFX Tabs and collected in a TabPane.

In JavaScript, we will use the DOM to display among ProblemPanel objects using a selector as shown to the right.

We describe a Chooser constructor function that creates an object with the demonstrated behavior using this code:

The top-level HTML for our multiple-domain problem solver is shown in the file solve.html below.

Note that the code to create the chooser is contained in a function given as a value for the onload property of the window object.

The window object represents the browser's window. The onload property specifies a function to run when all the content (including images and multimedia) have been loaded.

solve.html:

First we create a selector without any behavior. See display at right. The listing for Chooser.js and the styles it uses are shown in the menu at left.
When a new selection is made, we must be able to quickly and efficiently change problem panels in the display.

This is facilitated by initially storing the problem panels on a panelMap using the problem names as keys:

To give the selector its behavior, we define its onchange method, which is called whenever a new selection is made.

We use the variable problemPanel, initially null, to refer to the currently displayed problem panel.

When onchange is called,

See below. The full listing is shown in the menu to the left.