The sortable accordion focuses on a single accordion. In the spirit of giving users freedom within the confines of the application of course, why don't we see if we can support moving an accordion section to a new accordion?
With that in place, let's turn this markup into two accordions. We'll first extend the accordion widget with some fancy drag-and-drop behavior. The intent is to allow the user to drag accordion sections from the first widget to the second. Here is how it's done:
We now have two basic-looking accordion widgets. However, if the user is so inclined, they can drag a section of the first accordion into the second.
This might seem like a lot of code at the first glance, but for relatively little (approximately 130 lines), we're able to drag accordion sections out of one accordion and into another. Let's break this down further.
We're adding two accordion options with this widget extension: target
and accept
. Target allows us to specify the destination of sections of this accordion. In the example, we used the second accordion as the target for the first accordion, meaning that we can drag from target-accordion
and drop into accept-accordion
. But, in order to make that happen, the second accordion needs to be told where to accept sections from; in this case, it is target-accordion
. We're essentially using these two options to establish a drag-and-drop contract between the two widgets.
This example uses two interaction widgets: draggable and sortable. target-accordion
uses draggable. If the target
option was specified, the _createTarget()
method gets called. The _createTarget()
method goes through the accordion sections, wraps them in a div
element, and creates a draggable()
widget. This is how we're able to drag sections out of the first accordion.
If the accept
option was specified, the _createAccept()
method gets called. This follows the same pattern of wrapping each accordion header with its content in a div
element. Except here, we're making the entire accordion widget sortable()
.
This may seem counterintuitive. Why would we make the second accordion that wants to accept new sections into sortable? Would it not make more sense to use droppable? We could go down that route, but it would involve a lot of work where we're utilizing the connectToSortable
option instead. This is a draggable
option specified in _createTarget()
where we say that we would like to drop these draggable items into a sortable widget. In this example, sortable is the second accordion.
This solves the problem of deciding on where exactly to drop the accordion section relative to other sections (the sortable widget knows how to handle that). However, an interesting constraint with this approach is that we must clone the dragged item. That is, the section that ultimately gets dropped into the new accordion is just a clone, not the original. So we must deal with that at drop time.
As part of the sortable options defined in _createAccept()
, we provide a stop
callback. This callback function is fired when we've dropped a new accordion section into the accordion. Actually, this gets fired for any sorting activity, including new sections being dropped. So, we must take care to check what we're actually working with. We do so by checking whether the item has a draggable
class attached to it, and if so, we can assume we're dealing with a new accordion section.
Keep in mind that this newly dropped accordion section is simply a clone of the original, so some interesting things need to happen before we can start inserting it into the accordion. First, this new section has the same ID as the original. Eventually, we're going to remove the original from the first accordion, so we store that ID for later use. Once we have it, we can get rid of the dropped section's ID so as to avoid duplicates.
With that taken care of, we have the new DOM element in place, but the accordion widget knows nothing about it. This is where we reload the headers, including the newly-dropped header. The new accordion section still isn't functional because it doesn't handle events properly, so expanding the new section will not work, for example. To avoid strange behavior, we turn off all event handlers and rebind them. This puts the new accordion in its new context while the events are turned on.
We now have a new section in accept-accordion
. But we can't forget about the original section. It still needs to be removed. Recall that we stored the original section's DOM ID, and we can now safely remove that section and refresh the accordion to adjust the height.