list_item.js revision 21d179b334e59e9a3bfcaed4c4430bef1bc5759d
1// Copyright (c) 2010 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5cr.define('cr.ui', function() {
6
7  /**
8   * Creates a new list item element.
9   * @param {string} opt_label The text label for the item.
10   * @constructor
11   * @extends {HTMLLIElement}
12   */
13  var ListItem = cr.ui.define('li');
14
15  ListItem.prototype = {
16    __proto__: HTMLLIElement.prototype,
17
18    /**
19     * Plain text label.
20     * @type {string}
21     */
22    get label() {
23      return this.textContent;
24    },
25    set label(label) {
26      this.textContent = label;
27    },
28
29    /**
30     * The current selection state.
31     * @type {Boolean}
32     */
33    get selected() {
34      return this.hasAttribute('selected');
35    },
36    set selected(selected) {
37      selected = Boolean(selected);
38      var oldSelected = this.selected;
39      if (oldSelected == selected)
40        return;
41
42      if (selected)
43        this.setAttribute('selected', '');
44      else
45        this.removeAttribute('selected');
46
47      cr.dispatchPropertyChange(this, 'selected', selected, oldSelected);
48      this.selectionChanged();
49    },
50
51    /**
52     * Called when an element is decorated as a list item.
53     */
54    decorate: function() {
55    },
56
57    /**
58     * Called when the selection state of this element changes.
59     */
60    selectionChanged: function() {
61    },
62  };
63
64  /**
65   * Whether the item is selected. Setting this does not update the underlying
66   * selection model. This is only used for display purpose.
67   * @type {boolean}
68   */
69  cr.defineProperty(ListItem, 'selected', cr.PropertyKind.BOOL_ATTR);
70
71  /**
72   * Whether the item is the lead in a selection. Setting this does not update
73   * the underlying selection model. This is only used for display purpose.
74   * @type {boolean}
75   */
76  cr.defineProperty(ListItem, 'lead', cr.PropertyKind.BOOL_ATTR);
77
78  return {
79    ListItem: ListItem
80  };
81});
82