1<!DOCTYPE html>
2<!--
3Copyright 2015 The Chromium Authors. All rights reserved.
4Use of this source code is governed by a BSD-style license that can be
5found in the LICENSE file.
6-->
7
8<link rel="import" href="/tracing/ui/brushing_state_controller.html">
9
10<!--
11This element handles storing and retrieving the brushing state of arbitrary
12views (e.g. analysis sub-views). An element can use it by instantiating it and
13appending it to itself:
14
15  <div id="some-view-with-specific-brushing-state">
16    <tr-ui-b-view-specific-brushing-state view-id="unique-view-identifier">
17    </tr-ui-b-view-specific-brushing-state>
18    ... other child elements ...
19  </div>
20
21The state can then be retrieved from and pushed to the state element as
22follows:
23
24  newStateElement.set(state);
25  state = newStateElement.get();
26
27Under the hood, the state element searches the DOM tree for an ancestor element
28with a brushingStateController field to persist the state (see the
29tr.c.BrushingStateController and tr.ui.b.BrushingState classes for more
30details).
31-->
32<polymer-element name="tr-ui-b-view-specific-brushing-state">
33  <script>
34  'use strict';
35
36  Polymer({
37    /** Compulsory unique identifier of the associated view. */
38    get viewId() {
39      return this.getAttribute('view-id');
40    },
41
42    set viewId(viewId) {
43      this.setAttribute('view-id', viewId);
44    },
45
46    /**
47     * Retrieve the persisted state of the associated view. The returned object
48     * (or any of its fields) must not be modified by the caller (unless the
49     * object/field is treated as a reference).
50     *
51     * If no state has been persisted yet or there is no ancestor element with
52     * a brushingStateController field, this method returns undefined.
53     */
54    get: function() {
55      var viewId = this.viewId;
56      if (!viewId)
57        throw new Error('Element must have a view-id attribute!');
58
59      var brushingStateController =
60          tr.c.BrushingStateController.getControllerForElement(this);
61      if (!brushingStateController)
62        return undefined;
63
64      return brushingStateController.getViewSpecificBrushingState(viewId);
65    },
66
67    /**
68     * Persist the provided state of the associated view. The provided object
69     * (or any of its fields) must not be modified afterwards (unless the
70     * object/field is treated as a reference).
71     *
72     * If there is no ancestor element with a brushingStateController field,
73     * this method does nothing.
74     */
75    set: function(state) {
76      var viewId = this.viewId;
77      if (!viewId)
78        throw new Error('Element must have a view-id attribute!');
79
80      var brushingStateController =
81          tr.c.BrushingStateController.getControllerForElement(this);
82      if (!brushingStateController)
83        return;
84
85      brushingStateController.changeViewSpecificBrushingState(viewId, state);
86    }
87  });
88  </script>
89</polymer-element>
90