Issue #584136, add first and last classes in JS rather than PHP to respect reordering by element_sort.