[TYPO] Set the right category for the Point Of Sale
[odoo/odoo.git] / doc / list-view.rst
1 List View
2 =========
3
4 Style Hooks
5 -----------
6
7 The list view provides a few style hook classes for re-styling of list views in
8 various situations:
9
10 ``.oe_list``
11
12     The root element of the list view, styling rules should be rooted
13     on that class.
14
15 ``table.oe_list_content``
16
17     The root table for the listview, accessory components may be
18     generated or added outside this section, this is the list view
19     "proper".
20
21 ``.oe_list_buttons``
22
23     The action buttons array for the list view, with its sub-elements
24
25     ``.oe_list_add``
26
27         The default "Create"/"Add" button of the list view
28
29     ``.oe_alternative``
30
31         The "alternative choice" for the list view, by default text
32         along the lines of "or import" with a link.
33
34 ``.oe_list_field_cell``
35
36     The cell (``td``) for a given field of the list view, cells which
37     are *not* fields (e.g. name of a group, or number of items in a
38     group) will not have this class. The field cell can be further
39     specified:
40
41     ``.oe_number``
42
43         Numeric cell types (integer and float)
44
45     ``.oe_button``
46
47         Action button (``button`` tag in the view) inside the cell
48
49     ``.oe_readonly``
50
51         Readonly field cell
52
53     ``.oe_list_field_$type``
54
55         Additional class for the precise type of the cell, ``$type``
56         is the field's @widget if there is one, otherwise it's the
57         field's type.
58
59 ``.oe_list_record_selector``
60
61     Selector cells
62
63 Editable list view
64 ++++++++++++++++++
65
66 The editable list view module adds a few supplementary style hook
67 classes, for edition situations:
68
69 ``.oe_editing``
70
71     Added to both ``.oe_list`` and ``.oe_list_button`` (as the
72     buttons may be outside of the list view) when a row of the list is
73     currently being edited.
74
75 ``tr.oe_edition``
76
77     Class set on the row being edited itself. Note that the edition
78     form is *not* contained within the row, this allows for styling or
79     modifying the row while it's being edited separately. Mostly for
80     fields which can not be edited (e.g. read-only fields).
81
82
83 Editable list view
84 ------------------
85
86 List view edition is an extension to the base listview providing the
87 capability of inline record edition by delegating to an embedded form
88 view.
89
90 Editability status
91 ++++++++++++++++++
92
93 The editability status of a list view can be queried through the
94 :js:func:`~openerp.web.ListView.editable` method, will return a falsy
95 value if the listview is not currently editable.
96
97 The editability status is based on three flags:
98
99 ``tree/@editable``
100
101     If present, can be either ``"top"`` or ``"bottom"``. Either will
102     make the list view editable, with new records being respectively
103     created at the top or at the bottom of the view.
104
105 ``context.set_editable``
106
107     Boolean flag extracted from a search context (during the
108     :js:func:`~openerp.web.ListView.do_search`` handler), ``true``
109     will make the view editable (from the top), ``false`` or the
110     absence of the flag is a noop.
111
112 ``defaults.editable``
113
114     Like ``tree/@editable``, one of absent (``null``)), ``"top"`` or
115     ``"bottom"``, fallback for the list view if none of the previous
116     two flags are set.
117
118 These three flags can only *make* a listview editable, they can *not*
119 override a previously set flag. To do that, a listview user should
120 instead cancel :ref:`the edit:before event <listview-edit-before>`.
121
122 The editable list view module adds a number of methods to the list
123 view, on top of implementing the :js:class:`EditorDelegate` protocol:
124
125 Interaction Methods
126 +++++++++++++++++++
127
128 .. js:function:: openerp.web.ListView.ensure_saved
129
130     Attempts to resolve the pending edition, if any, by saving the
131     edited row's current state.
132
133     :returns: delegate resolving to all editions having been saved, or
134               rejected if a pending edition could not be saved
135               (e.g. validation failure)
136
137 .. js:function:: openerp.web.ListView.start_edition([record][, options])
138
139     Starts editing the provided record inline, through an overlay form
140     view of editable fields in the record.
141
142     If no record is provided, creates a new one according to the
143     editability configuration of the list view.
144
145     This method resolves any pending edition when invoked, before
146     starting a new edition.
147
148     :param record: record to edit, or null to create a new record
149     :type record: :js:class:`~openerp.web.list.Record`
150     :param EditOptions options:
151     :returns: delegate to the form used for the edition
152
153 .. js:function:: openerp.web.ListView.save_edition
154
155     Resolves the pending edition.
156
157     :returns: delegate to the save being completed, resolves to an
158               object with two attributes ``created`` (flag indicating
159               whether the saved record was just created or was
160               updated) and ``record`` the reloaded record having been
161               edited.
162
163 .. js:function:: openerp.web.ListView.cancel_edition
164
165     Cancels pending edition, cleans up the list view in case of
166     creation (removes the empty record being created).
167
168 Utility Methods
169 +++++++++++++++
170
171 .. js:function:: openerp.web.ListView.get_cells_for(row)
172
173     Extracts the cells from a listview row, and puts them in a
174     {fieldname: cell} mapping for analysis and manipulation.
175
176     :param jQuery row:
177     :rtype: Object
178
179 .. js:function:: openerp.web.ListView.with_event(event_name, event, action[, args][, trigger_params])
180
181     Executes ``action`` in the context of the view's editor,
182     bracketing it with cancellable event signals.
183
184     :param String event_name: base name for the bracketing event, will
185                               be postfixed by ``:before`` and
186                               ``:after`` before being called
187                               (respectively before and after
188                               ``action`` is executed)
189     :param Object event: object passed to the ``:before`` event
190                          handlers.
191     :param Function action: function called with the view's editor as
192                             its ``this``. May return a deferred.
193     :param Array args: arguments passed to ``action``
194     :param Array trigger_params: arguments passed to the ``:after``
195                                  event handler alongside the results
196                                  of ``action``
197
198 Behavioral Customizations
199 +++++++++++++++++++++++++
200
201 .. js:function:: openerp.web.ListView.handle_onwrite(record)
202
203     Implements the handling of the ``onwrite`` listview attribute:
204     calls the RPC methods specified by ``@onwrite``, and if that
205     method returns an array of ids loads or reloads the records
206     corresponding to those ids.
207
208     :param record: record being written having triggered the
209                    ``onwrite`` callback
210     :type record: openerp.web.list.Record
211     :returns: deferred to all reloadings being done
212
213 Events
214 ++++++
215
216 For simpler interactions by/with external users of the listview, the
217 view provides a number of dedicated events to its lifecycle.
218
219 .. note:: if an event is defined as *cancellable*, it means its first
220           parameter is an object on which the ``cancel`` attribute can
221           be set. If the ``cancel`` attribute is set, the view will
222           abort its current behavior as soon as possible, and rollback
223           any state modification.
224
225           Generally speaking, an event should only be cancelled (by
226           setting the ``cancel`` flag to ``true``), uncancelling an
227           event is undefined as event handlers are executed on a
228           first-come-first-serve basis and later handlers may
229           re-cancel an uncancelled event.
230
231 .. _listview-edit-before:
232
233 ``edit:before`` *cancellable*
234
235     Invoked before the list view starts editing a record.
236
237     Provided with an event object with a single property ``record``,
238     holding the attributes of the record being edited (``record`` is
239     empty *but not null* for a new record)
240
241 ``edit:after``
242
243     Invoked after the list view has gone into an edition state,
244     provided with the attributes of the record being edited (see
245     ``edit:before``) as first parameter and the form used for the
246     edition as second parameter.
247
248 ``save:before`` *cancellable*
249
250     Invoked right before saving a pending edition, provided with an
251     event object holding the listview's editor (``editor``) and the
252     edition form (``form``)
253
254 ``save:after``
255
256     Invoked after a save has been completed
257
258 ``cancel:before`` *cancellable*
259
260     Invoked before cancelling a pending edition, provided with the
261     same information as ``save:before``.
262
263 ``cancel:after``
264
265     Invoked after a pending edition has been cancelled.
266
267 DOM events
268 ++++++++++
269
270 The list view has grown hooks for the ``keyup`` event on its edition
271 form (during edition): any such event bubbling out of the edition form
272 will be forwarded to a method ``keyup_EVENTNAME``, where ``EVENTNAME``
273 is the name of the key in ``$.ui.keyCode``.
274
275 The method will also get the event object (originally passed to the
276 ``keyup`` handler) as its sole parameter.
277
278 The base editable list view has handlers for the ``ENTER`` and
279 ``ESCAPE`` keys.
280
281 Editor
282 ------
283
284 The list-edition modules does not generally interact with the embedded
285 formview, delegating instead to its
286 :js:class:`~openerp.web.list.Editor`.
287
288 .. js:class:: openerp.web.list.Editor(parent[, options])
289
290     The editor object provides a more convenient interface to form
291     views, and simplifies the usage of form views for semi-arbitrary
292     edition of stuff.
293
294     However, the editor does *not* task itself with being internally
295     consistent at this point: calling
296     e.g. :js:func:`~openerp.web.list.Editor.edit` multiple times in a
297     row without saving or cancelling each edit is undefined.
298
299     :param parent:
300     :type parent: :js:class:`~openerp.web.Widget`
301     :param EditorOptions options:
302
303     .. js:function:: openerp.web.list.Editor.is_editing([record_state])
304
305         Indicates whether the editor is currently in the process of
306         providing edition for a record.
307
308         Can be filtered by the state of the record being edited
309         (whether it's a record being *created* or a record being
310         *altered*), in which case it asserts both that an edition is
311         underway and that the record being edited respectively does
312         not yet exist in the database or already exists there.
313
314         :param record_state: state of the record being edited.
315                              Either ``"new"`` or ``"edit"``.
316         :type record_state: String
317         :rtype: Boolean
318
319     .. js:function:: openerp.web.list.Editor.edit(record, configureField[, options])
320
321         Loads the provided record into the internal form view and
322         displays the form view.
323
324         Will also attempt to focus the first visible field of the form
325         view.
326
327         :param Object record: record to load into the form view
328                               (key:value mapping similar to the result
329                               of a ``read``)
330         :param configureField: function called with each field of the
331                                form view right after the form is
332                                displayed, lets whoever called this
333                                method do some last-minute
334                                configuration of form fields.
335         :type configureField: Function<String, openerp.web.form.Field>
336         :param EditOptions options:
337         :returns: jQuery delegate to the form object
338
339     .. js:function:: openerp.web.list.Editor.save
340
341         Attempts to save the internal form, then hide it
342
343         :returns: delegate to the record under edition (with ``id``
344                   added for a creation). The record is not updated
345                   from when it was passed in, aside from the ``id``
346                   attribute.
347
348     .. js:function:: openerp.web.list.Editor.cancel
349
350         Attemps to cancel the edition of the internal form, then hide
351         the form
352
353         :returns: delegate to the record under edition
354
355 .. js:class:: EditorOptions
356
357     .. js:attribute:: EditorOptions.formView
358
359         Form view (sub)-class to instantiate and delegate edition to.
360
361         By default, :js:class:`~openerp.web.FormView`
362
363     .. js:attribute:: EditorOptions.delegate
364
365         Object used to get various bits of information about how to
366         display stuff.
367
368         By default, uses the editor's parent widget. See
369         :js:class:`EditorDelegate` for the methods and attributes to
370         provide.
371
372 .. js:class:: EditorDelegate
373
374     Informal protocol defining the methods and attributes expected of
375     the :js:class:`~openerp.web.list.Editor`'s delegate.
376
377     .. js:attribute:: EditorDelegate.dataset
378
379         The dataset passed to the form view to synchronize the form
380         view and the outer widget.
381
382     .. js:function:: EditorDelegate.edition_view(editor)
383
384         Called by the :js:class:`~openerp.web.list.Editor` object to
385         get a form view (JSON) to pass along to the form view it
386         created.
387
388         The result should be a valid form view, see :doc:`Form Notes
389         <form-notes>` for various peculiarities of the form view
390         format.
391
392         :param editor: editor object asking for the view
393         :type editor: :js:class:`~openerp.web.list.Editor`
394         :returns: form view
395         :rtype: Object
396
397     .. js:function:: EditorDelegate.prepends_on_create
398
399         By default, the :js:class:`~openerp.web.list.Editor` will
400         append the ids of newly created records to the
401         :js:attr:`EditorDelegate.dataset`. If this method returns
402         ``true``, it will prepend these ids instead.
403
404         :returns: whether new records should be prepended to the
405                   dataset (instead of appended)
406         :rtype: Boolean
407
408
409 .. js:class:: EditOptions
410
411     Options object optionally passed into a method starting an edition
412     to configure its setup and behavior
413
414     .. js:attribute:: focus_field
415
416         Name of the field to set focus on after setting up the edition
417         of the record.
418
419         If this option is not provided, or the requested field can not
420         be focused (invisible, readonly or not in the view), the first
421         visible non-readonly field is focused.
422
423 Changes from 6.1
424 ----------------
425
426 * The editable listview behavior has been rewritten pretty much from
427   scratch, any code touching on editability will have to be modified
428
429   * The overloading of :js:class:`~openerp.web.ListView.Groups` and
430     :js:class:`~openerp.web.ListView.List` for editability has been
431     drastically simplified, and most of the behavior has been moved to
432     the list view itself. Only
433     :js:func:`~openerp.web.ListView.List.row_clicked` is still
434     overridden.
435
436   * A new method ``get_row_for(record) -> jQuery(tr) | null`` has been
437     added to both ListView.List and ListView.Group, it can be called
438     from the list view to get the table row matching a record (if such
439     a row exists).
440
441 * :js:func:`~openerp.web.ListView.do_button_action`'s core behavior
442   has been split away to
443   :js:func:`~openerp.web.ListView.handle_button`. This allows bypassing
444   overrides of :js:func:`~openerp.web.ListView.do_button_action` in a
445   parent class.
446
447   Ideally, :js:func:`~openerp.web.ListView.handle_button` should not be
448   overridden.
449
450 * Modifiers handling has been improved (all modifiers information
451   should now be available through :js:func:`~Column.modifiers_for`,
452   not just ``invisible``)
453
454 * Changed some handling of the list view's record: a record may now
455   have no id, and the listview will handle that correctly (for new
456   records being created) as well as correctly handle the ``id`` being
457   set.
458
459 * Extended the internal collections structure of the list view with
460   `#find`_, `#succ`_ and `#pred`_.
461
462 .. _#find: http://underscorejs.org/#find
463
464 .. _#succ: http://hackage.haskell.org/packages/archive/base/latest/doc/html/Prelude.html#v:succ
465
466 .. _#pred: http://hackage.haskell.org/packages/archive/base/latest/doc/html/Prelude.html#v:pred