<%! from django.core.urlresolvers import reverse %>
<%! from contentstore.utils import compute_unit_state %>
<%! from xmodule.modulestore.django import loc_mapper %>

<!--
This def will enumerate through a passed in subsection and list all of the units
-->
<%def name="enum_units(subsection, actions=True, selected=None, sortable=True, subsection_units=None)">
<ol ${'class="sortable-unit-list"' if sortable else ''}>
  <%
    if subsection_units is None:
      subsection_units = subsection.get_children()
  %>
  % for unit in subsection_units:
  <%
    unit_update_url = loc_mapper().translate_location(context_course.location.course_id, unit.location, False, True).url_reverse('xblock')
  %>
  <li class="courseware-unit leaf unit is-draggable" data-id="${unit.location}" data-parent-id="${subsection.location.url()}"
      data-update_url="${unit_update_url}" >

    <%include file="_ui-dnd-indicator-before.html" />

    <%
      unit_state = compute_unit_state(unit)
      if unit.location == selected:
        selected_class = 'editing'
      else:
        selected_class = ''
    %>
    <div class="section-item ${selected_class}">
      <a href="${reverse('edit_unit', args=[unit.location])}" class="${unit_state}-item">
        <span class="${unit.scope_ids.block_type}-icon"></span>
        <span class="unit-name">${unit.display_name_with_default}</span>
      </a>
      % if actions:
      <div class="item-actions">
	<a href="#" data-tooltip="Delete this unit" class="delete-button" data-id="${unit.location}"
       data-update_url="${unit_update_url}">
        <span class="delete-icon"></span></a>
	<span data-tooltip="Drag to sort" class="drag-handle unit-drag-handle"></span>
      </div>
      % endif
    </div>

    <%include file="_ui-dnd-indicator-after.html" />
  </li>
  % endfor
  <li>
    <%include file="_ui-dnd-indicator-initial.html" />

    <a href="#" class="new-unit-item" data-category="${new_unit_category}" data-parent="${subsection.location}">
      <span class="new-unit-icon"></span>New Unit
    </a>
  </li>
</ol>
</%def>



