=========================================
Editing sort order of address book fields
=========================================

The sort order of the fields of the entities can be changed globally. The
places where fields are displayed in order are changed accordingly.

Set up
======

Create an address book and a browser to access it. Log in as
`Administrator`:

>>> from icemac.addressbook.testing import create_addressbook
>>> from z3c.etestbrowser.wsgi import ExtendedTestBrowser as Browser
>>> ab = create_addressbook()
>>> browser = Browser()
>>> browser.addHeader('Authorization', 'Basic mgr:mgrpw')
>>> browser.open('http://localhost/++skin++AddressBook/ab')

To show that the sort order includes user defined fields, we have to create
one:

>>> from icemac.addressbook.testing import create_field
>>> _ = create_field(
...         ab, 'icemac.addressbook.person.Person', 'Bool', u'grown-up')

The sort order of the fields can be changed in the master data area on each
entity, for example we choose the `person` entity here:

>>> browser.getLink('Master data').click()
>>> browser.getLink('Entities').click()
>>> browser.getLink('Edit fields', index=1).click()
>>> browser.url
'http://localhost/.../ab/++attribute++entities/icemac.addressbook.person.Person'

The default sort order looks the following way (user defined field was
sorted to the end):

>>> print browser.contents
<!DOCTYPE...
    <tbody>
      <tr id="first_name" class="table-even-row">
        <td>first name</td>
        <td></td>
        <td></td>
      </tr>
      <tr id="last_name" class="table-odd-row">
        <td>last name</td>
        <td></td>
        <td></td>
      </tr>
      <tr id="birth_date" class="table-even-row">
        <td>birth date</td>
        <td></td>
        <td></td>
      </tr>
      <tr id="keywords" class="table-odd-row">
        <td>keywords</td>
        <td></td>
        <td></td>
      </tr>
      <tr id="notes" class="table-even-row">
        <td>notes</td>
        <td></td>
        <td></td>
      </tr>
      <tr id="Field-1" class="table-odd-row">
        <td>grown-up</td>
        <td><a href="http://localhost/++skin++AddressBook/ab/++attribute++entities/icemac.addressbook.person.Person/Field-1/@@delete.html">Delete</a></td>
        <td><a href="http://localhost/++skin++AddressBook/ab/++attribute++entities/icemac.addressbook.person.Person/Field-1">Edit</a></td>
      </tr>
    </tbody>
  ...

Changing sort order
===================

The sort order is changed using JavaScript (tested using Selenium). When the
user hits the `Save sortorder` button a URL is generated and sent to the
server:

>>> from icemac.addressbook.testing import get_messages
>>> browser.open(browser.url +
...              '/@@save-sortorder.html?f:list=last_name&f:list=Field-1')
>>> get_messages(browser)
['Saved sortorder.']

The fields are now ordered as they were in the URL (not mentioned fields are
sorted to the end):

>>> print browser.contents
<!DOCTYPE...
    <tbody>
      <tr id="last_name" class="table-even-row">
        <td>last name</td>
        <td></td>
        <td></td>
      </tr>
      <tr id="Field-1" class="table-odd-row">
        <td>grown-up</td>
        <td><a href="http://localhost/++skin++AddressBook/ab/++attribute++entities/icemac.addressbook.person.Person/Field-1/@@delete.html">Delete</a></td>
        <td><a href="http://localhost/++skin++AddressBook/ab/++attribute++entities/icemac.addressbook.person.Person/Field-1">Edit</a></td>
      </tr>
      <tr id="first_name" class="table-even-row">
        <td>first name</td>
        <td></td>
        <td></td>
      </tr>
      <tr id="birth_date" class="table-odd-row">
        <td>birth date</td>
        <td></td>
        <td></td>
      </tr>
      <tr id="keywords" class="table-even-row">
        <td>keywords</td>
        <td></td>
        <td></td>
      </tr>
      <tr id="notes" class="table-odd-row">
        <td>notes</td>
        <td></td>
        <td></td>
      </tr>
    </tbody>
  ...