12.12. Assign-Tickets Component
The ticket-assignment component provides the users of the ticket manager with a quick way to assign unassigned tickets to users in the system. This is done by dragging tickets from a table and dropping them on a box that represents each user. This interface is a good example of the power that fully interactive interfaces give users. A drag-and-drop approach is especially useful for tasks such as sorting or assigning, because they are made up of elements that are mappable and that are often time-consuming with pre-AJAX approaches. The scriptaculous library is used to provide the drag-and-drop logic in this example. The interface of the assign-tickets component is created by Ticket.php (see Listing 12-21) and is shown in Figure 12-15.
Figure 12-15. Assign-tickets component
Listing 12-21. Ticket.php
Listing 12-22. Assign.js
The ticket-assignment component uses both a setup function and an onDisplay function, so it starts by setting those up. The onDisplay function (lines 14) is run each time the user loads the section. In this function, AJAX calls are made to the server to load the users and unassigned tickets in the browser. The ticket information is updated when the section is displayed because you don't want to be in a situation in which you are reassigning tickets that someone else already assigned. This problem can happen if you are working with stale data; updating the data on section display least guarantees you fresh data at the start of the assignment process. The Setup function (lines 69) loads the template row into the app object using cloneNode so that it can be used latereven after the source is overwritten. The Setup function also registers the onDisplay function because that can't be done until the assign DIV is loaded.
The next ten lines take care of setting up the AJAX class. Callbacks for listUnassignedTickets and listUsers are registered. As in the rest of the application, these callbacks do minimal work, leaving the implementation to functions that are registered later in the file.
The rest of Assign.js contains the functions that provide the functionality of the component. MakeDraggable (lines 2437) makes the rows in the ticket table draggable. It does this by looping over the rows in the body of the table and adding a new td on the right side. This td will contain a div, which is the drag handle for the row. The use of the handle DIV makes it clear how you should interact with the table; it works better than directly dragging-and-dropping the table rows because the rows have a variety of bugs in different browsers and they don't always offer a great user experience. The DIV is made draggable on lines 3435 by creating a new scriptaculous Draggable instance; the revert option is set to true so that the DIV stays in place after it is dragged to a user's drop target.
The buildUserDrops function takes a list of users that is returned from the server and dynamically creates a DIV for each user that is used as a drop target for the ticket rows. This process starts by clearing the target DIV (line 40) because drops may already be in place from an earlier view of this section. Then the list of users is iterated over. On lines 4245, a DIVis created, and it's populated with some of the user's information. Included in this is the user's user_id set as a property on the DIV so that it can be accessed by other code when the tickets are dropped. The function finishes by adding the new DIV to the target area (line 47) and making it a drop target (lines 4854). When we make it a drop target, we set an onDrop handler. This handler calls the assignTicket function and gives us a way to tie the drop event from the user interface to the back end.
The assignTicket function runs when a drop is completed. The first parameter, handle, is the DIV from the ticket row; the second parameter, user, is the user's drop target DIV. The function starts by using the handle's parentNode property to get the actual table row (line 59), and then it uses the row to get the title of the ticket (line 60) and the ticket's ID (line 61). With that information, an AJAX call to the server can be made. This call assigns the ticket to the specified users (line 62). The rest of the function provides status feedback to the user; lines 6465 provide a textual message, and lines 6768 update the assigned column in the ticket table and highlight the row to show that it's been updated.