| 1 | 
           efrain | 
           1 | 
           ---
  | 
        
        
            | 
            | 
           2 | 
           layout: docs
  | 
        
        
            | 
            | 
           3 | 
           title: "Sortable list"
  | 
        
        
            | 
            | 
           4 | 
           description: "A reusable list component for sorting"
  | 
        
        
            | 
            | 
           5 | 
           date: 2020-02-04T09:40:32+01:00
  | 
        
        
            | 
            | 
           6 | 
           draft: false
  | 
        
        
            | 
            | 
           7 | 
           tags:
  | 
        
        
            | 
            | 
           8 | 
           - MDL-51803
  | 
        
        
            | 
            | 
           9 | 
           - 3.7
  | 
        
        
            | 
            | 
           10 | 
           ---
  | 
        
        
            | 
            | 
           11 | 
              | 
        
        
            | 
            | 
           12 | 
           ## How it works
  | 
        
        
            | 
            | 
           13 | 
              | 
        
        
            | 
            | 
           14 | 
           The sortable lists can be used for horizontal and vertical ordering of list elements. The key element of using a sortable list is to provide drag handles to list items. These can be included using the core drag_handle template.
  | 
        
        
            | 
            | 
           15 | 
              | 
        
        
            | 
            | 
           16 | 
           ## Source files
  | 
        
        
            | 
            | 
           17 | 
              | 
        
        
            | 
            | 
           18 | 
           * `/lib/amd/src/sortable_lists.js`
  | 
        
        
            | 
            | 
           19 | 
           * `/lib/templates/drag_handle.mustache`
  | 
        
        
            | 
            | 
           20 | 
              | 
        
        
            | 
            | 
           21 | 
           ## Usage
  | 
        
        
            | 
            | 
           22 | 
              | 
        
        
            | 
            | 
           23 | 
           The sortable list AMD module can be loaded in your template or via a amd init call. The list to be sorted is passed as the first argument to
  | 
        
        
            | 
            | 
           24 | 
              | 
        
        
            | 
            | 
           25 | 
           {{< highlight js >}}
  | 
        
        
            | 
            | 
           26 | 
           // Default config, this variable is optional.
  | 
        
        
            | 
            | 
           27 | 
           var config = {
  | 
        
        
            | 
            | 
           28 | 
               targetListSelector: null,
  | 
        
        
            | 
            | 
           29 | 
               moveHandlerSelector: '[data-drag-type=move]',
  | 
        
        
            | 
            | 
           30 | 
               isHorizontal: false,
  | 
        
        
            | 
            | 
           31 | 
               autoScroll: true
  | 
        
        
            | 
            | 
           32 | 
           };
  | 
        
        
            | 
            | 
           33 | 
           new SortableList(domElement, config);
  | 
        
        
            | 
            | 
           34 | 
           {{< /highlight >}}
  | 
        
        
            | 
            | 
           35 | 
              | 
        
        
            | 
            | 
           36 | 
           The events listed below can be used to connect the sortable list to a webserver handling changed positions of your sortable list items.
  | 
        
        
            | 
            | 
           37 | 
              | 
        
        
            | 
            | 
           38 | 
           ## Events
  | 
        
        
            | 
            | 
           39 | 
              | 
        
        
            | 
            | 
           40 | 
           <table class="table">
  | 
        
        
            | 
            | 
           41 | 
             <thead>
  | 
        
        
            | 
            | 
           42 | 
               <tr>
  | 
        
        
            | 
            | 
           43 | 
                 <th style="width: 150px;">Event type</th>
  | 
        
        
            | 
            | 
           44 | 
                 <th>Description</th>
  | 
        
        
            | 
            | 
           45 | 
               </tr>
  | 
        
        
            | 
            | 
           46 | 
             </thead>
  | 
        
        
            | 
            | 
           47 | 
             <tbody>
  | 
        
        
            | 
            | 
           48 | 
               <tr>
  | 
        
        
            | 
            | 
           49 | 
                 <td>SortableList.EVENTS.DRAGSTART</td>
  | 
        
        
            | 
            | 
           50 | 
                 <td>This event fires immediately when user started dragging a list element.</td>
  | 
        
        
            | 
            | 
           51 | 
               </tr>
  | 
        
        
            | 
            | 
           52 | 
               <tr>
  | 
        
        
            | 
            | 
           53 | 
                 <td>SortableList.EVENTS.DRAG</td>
  | 
        
        
            | 
            | 
           54 | 
                 <td>This event is fired when user dragged a list element to a new position.</td>
  | 
        
        
            | 
            | 
           55 | 
               </tr>
  | 
        
        
            | 
            | 
           56 | 
               <tr>
  | 
        
        
            | 
            | 
           57 | 
                 <td>SortableList.EVENTS.DROP</td>
  | 
        
        
            | 
            | 
           58 | 
                 <td>This event is fired when user dropped a list element</td>
  | 
        
        
            | 
            | 
           59 | 
               </tr>
  | 
        
        
            | 
            | 
           60 | 
               <tr>
  | 
        
        
            | 
            | 
           61 | 
                 <td>SortableList.EVENTS.DROPEND</td>
  | 
        
        
            | 
            | 
           62 | 
                 <td>This event is fired when user finished dragging - either fired right after dropping or if "Esc" was pressed during dragging</td>
  | 
        
        
            | 
            | 
           63 | 
               </tr>
  | 
        
        
            | 
            | 
           64 | 
             </tbody>
  | 
        
        
            | 
            | 
           65 | 
           </table>
  | 
        
        
            | 
            | 
           66 | 
              | 
        
        
            | 
            | 
           67 | 
           ## Examples
  | 
        
        
            | 
            | 
           68 | 
              | 
        
        
            | 
            | 
           69 | 
           {{< example>}}
  | 
        
        
            | 
            | 
           70 | 
           <div class="row w-50">
  | 
        
        
            | 
            | 
           71 | 
             <div class="col-md-6">
  | 
        
        
            | 
            | 
           72 | 
               <ul class="list-group my-draggable-list-ex">
  | 
        
        
            | 
            | 
           73 | 
                   <li class="list-group-item">
  | 
        
        
            | 
            | 
           74 | 
                       <span tabindex="0" role="button" aria-haspopup="true" data-drag-type="move" title="1. Cras justo odio">
  | 
        
        
            | 
            | 
           75 | 
                           <i class="fa fa-arrows"></i>
  | 
        
        
            | 
            | 
           76 | 
                       </span>
  | 
        
        
            | 
            | 
           77 | 
                       1. Cras justo odio
  | 
        
        
            | 
            | 
           78 | 
                   </li>
  | 
        
        
            | 
            | 
           79 | 
                   <li class="list-group-item">
  | 
        
        
            | 
            | 
           80 | 
                       <span tabindex="0" role="button" aria-haspopup="true" data-drag-type="move" title="2. Dapibus ac facilisis in">
  | 
        
        
            | 
            | 
           81 | 
                           <i class="fa fa-arrows"></i>
  | 
        
        
            | 
            | 
           82 | 
                       </span>
  | 
        
        
            | 
            | 
           83 | 
                       2. Dapibus ac facilisis in
  | 
        
        
            | 
            | 
           84 | 
                   </li>
  | 
        
        
            | 
            | 
           85 | 
                   <li class="list-group-item">
  | 
        
        
            | 
            | 
           86 | 
                       <span tabindex="0" role="button" aria-haspopup="true" data-drag-type="move" title="3. Morbi leo risus">
  | 
        
        
            | 
            | 
           87 | 
                           <i class="fa fa-arrows"></i>
  | 
        
        
            | 
            | 
           88 | 
                       </span>
  | 
        
        
            | 
            | 
           89 | 
                       3. Morbi leo risus
  | 
        
        
            | 
            | 
           90 | 
                   </li>
  | 
        
        
            | 
            | 
           91 | 
                   <li class="list-group-item">
  | 
        
        
            | 
            | 
           92 | 
                       <span tabindex="0" role="button" aria-haspopup="true" data-drag-type="move" title="4. Porta ac consectetur ac">
  | 
        
        
            | 
            | 
           93 | 
                           <i class="fa fa-arrows"></i>
  | 
        
        
            | 
            | 
           94 | 
                       </span>
  | 
        
        
            | 
            | 
           95 | 
                       4. Porta ac consectetur ac
  | 
        
        
            | 
            | 
           96 | 
                   </li>
  | 
        
        
            | 
            | 
           97 | 
                   <li class="list-group-item">
  | 
        
        
            | 
            | 
           98 | 
                       <span tabindex="0" role="button" aria-haspopup="true" data-drag-type="move" title="5. Vestibulum at eros">
  | 
        
        
            | 
            | 
           99 | 
                           <i class="fa fa-arrows"></i>
  | 
        
        
            | 
            | 
           100 | 
                       </span>
  | 
        
        
            | 
            | 
           101 | 
                       5. Vestibulum at eros
  | 
        
        
            | 
            | 
           102 | 
                   </li>
  | 
        
        
            | 
            | 
           103 | 
               </ul>
  | 
        
        
            | 
            | 
           104 | 
             </div>
  | 
        
        
            | 
            | 
           105 | 
           </div>
  | 
        
        
            | 
            | 
           106 | 
              | 
        
        
            | 
            | 
           107 | 
           {{#js}}
  | 
        
        
            | 
            | 
           108 | 
           require(
  | 
        
        
            | 
            | 
           109 | 
           [
  | 
        
        
            | 
            | 
           110 | 
               'jquery',
  | 
        
        
            | 
            | 
           111 | 
               'core/sortable_list',
  | 
        
        
            | 
            | 
           112 | 
           ],
  | 
        
        
            | 
            | 
           113 | 
           function(
  | 
        
        
            | 
            | 
           114 | 
               $,
  | 
        
        
            | 
            | 
           115 | 
               SortableList
  | 
        
        
            | 
            | 
           116 | 
           ) {
  | 
        
        
            | 
            | 
           117 | 
                new SortableList('ul.my-draggable-list-ex');
  | 
        
        
            | 
            | 
           118 | 
              | 
        
        
            | 
            | 
           119 | 
                $('ul.my-draggable-list-ex > *').on(SortableList.EVENTS.DROP, function(evt, info) {
  | 
        
        
            | 
            | 
           120 | 
                   console.log(info);
  | 
        
        
            | 
            | 
           121 | 
                });
  | 
        
        
            | 
            | 
           122 | 
           });
  | 
        
        
            | 
            | 
           123 | 
           {{/js}}
  | 
        
        
            | 
            | 
           124 | 
           {{< /example >}}
  |