1<!DOCTYPE html>
2<!--
3Copyright (c) 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/base/guid.html">
9<link rel="import" href="/tracing/model/event_set.html">
10<link rel="import" href="/tracing/model/selection_state.html">
11
12<script>
13'use strict';
14
15tr.exportTo('tr.ui.b', function() {
16  var EventSet = tr.model.EventSet;
17  var SelectionState = tr.model.SelectionState;
18
19  function BrushingState() {
20    this.guid_ = tr.b.GUID.allocate();
21    this.selection_ = new EventSet();
22    this.findMatches_ = new EventSet();
23    this.analysisViewRelatedEvents_ = new EventSet();
24    this.analysisLinkHoveredEvents_ = new EventSet();
25    this.appliedToModel_ = undefined;
26    this.viewSpecificBrushingStates_ = {};
27  }
28  BrushingState.prototype = {
29    get guid() {
30      return this.guid_;
31    },
32
33    clone: function() {
34      var that = new BrushingState();
35      that.selection_ = this.selection_;
36      that.findMatches_ = this.findMatches_;
37      that.analysisViewRelatedEvents_ = this.analysisViewRelatedEvents_;
38      that.analysisLinkHoveredEvents_ = this.analysisLinkHoveredEvents_;
39      that.viewSpecificBrushingStates_ = this.viewSpecificBrushingStates_;
40
41      return that;
42    },
43
44    equals: function(that) {
45      if (!this.selection_.equals(that.selection_))
46        return false;
47      if (!this.findMatches_.equals(that.findMatches_))
48        return false;
49      if (!this.analysisViewRelatedEvents_.equals(
50          that.analysisViewRelatedEvents_)) {
51        return false;
52      }
53      if (!this.analysisLinkHoveredEvents_.equals(
54          that.analysisLinkHoveredEvents_)) {
55        return false;
56      }
57      // We currently do not take the view-specific brushing states into
58      // account. If we did, every change of the view-specific brushing state
59      // of any view would cause a redraw of the whole UI (see the
60      // BrushingStateController.currentBrushingState setter).
61      return true;
62    },
63
64    get selectionOfInterest() {
65      if (this.selection_.length)
66        return this.selection_;
67
68      if (this.highlight_.length)
69        return this.highlight_;
70
71      if (this.analysisViewRelatedEvents_.length)
72        return this.analysisViewRelatedEvents_;
73
74      if (this.analysisLinkHoveredEvents_.length)
75        return this.analysisLinkHoveredEvents_;
76
77      return this.selection_;
78    },
79
80    get selection() {
81      return this.selection_;
82    },
83
84    set selection(selection) {
85      if (this.appliedToModel_)
86        throw new Error('Cannot mutate this state right now');
87      if (selection === undefined)
88        selection = new EventSet();
89      this.selection_ = selection;
90    },
91
92    get findMatches() {
93      return this.findMatches_;
94    },
95
96    set findMatches(findMatches) {
97      if (this.appliedToModel_)
98        throw new Error('Cannot mutate this state right now');
99      if (findMatches === undefined)
100        findMatches = new EventSet();
101      this.findMatches_ = findMatches;
102    },
103
104    get analysisViewRelatedEvents() {
105      return this.analysisViewRelatedEvents_;
106    },
107
108    set analysisViewRelatedEvents(analysisViewRelatedEvents) {
109      if (this.appliedToModel_)
110        throw new Error('Cannot mutate this state right now');
111      if (analysisViewRelatedEvents === undefined)
112        analysisViewRelatedEvents = new EventSet();
113      this.analysisViewRelatedEvents_ = analysisViewRelatedEvents;
114    },
115
116    get analysisLinkHoveredEvents() {
117      return this.analysisLinkHoveredEvents_;
118    },
119
120    set analysisLinkHoveredEvents(analysisLinkHoveredEvents) {
121      if (this.appliedToModel_)
122        throw new Error('Cannot mutate this state right now');
123      if (analysisLinkHoveredEvents === undefined)
124        analysisLinkHoveredEvents = new EventSet();
125      this.analysisLinkHoveredEvents_ = analysisLinkHoveredEvents;
126    },
127
128    get isAppliedToModel() {
129      return this.appliedToModel_ !== undefined;
130    },
131
132    get viewSpecificBrushingStates() {
133      return this.viewSpecificBrushingStates_;
134    },
135
136    set viewSpecificBrushingStates(viewSpecificBrushingStates) {
137      this.viewSpecificBrushingStates_ = viewSpecificBrushingStates;
138    },
139
140    get causesDimming_() {
141      return this.findMatches_.length > 0 ||
142          this.analysisViewRelatedEvents_.length > 0;
143    },
144
145    get brightenedEvents_() {
146      var brightenedEvents = new EventSet();
147      brightenedEvents.addEventSet(this.selection_);
148      brightenedEvents.addEventSet(this.analysisLinkHoveredEvents_);
149      return brightenedEvents;
150    },
151
152    applyToModelSelectionState: function(model) {
153      this.appliedToModel_ = model;
154
155      if (!this.causesDimming_) {
156        this.brightenedEvents_.forEach(function(e) {
157          var score;
158          score = 0;
159          if (this.selection_.contains(e))
160            score++;
161          if (this.analysisLinkHoveredEvents_.contains(e))
162            score++;
163          e.selectionState = SelectionState.getFromBrighteningLevel(score);
164        }, this);
165        return;
166      }
167
168      var brightenedEvents = this.brightenedEvents_;
169      model.iterateAllEvents(function(e) {
170        var score;
171        if (brightenedEvents.contains(e)) {
172          score = 0;
173          if (this.selection_.contains(e))
174            score++;
175          if (this.analysisLinkHoveredEvents_.contains(e))
176            score++;
177          e.selectionState = SelectionState.getFromBrighteningLevel(score);
178        } else {
179          score = 0;
180          if (this.findMatches_.contains(e))
181            score++;
182          if (this.analysisViewRelatedEvents_.contains(e))
183            score++;
184          e.selectionState = SelectionState.getFromDimmingLevel(score);
185        }
186      }.bind(this));
187    },
188
189    transferModelOwnershipToClone: function(that) {
190      if (!this.appliedToModel_)
191        throw new Error('Not applied');
192      // Assumes this.equals(that).
193      that.appliedToModel_ = this.appliedToModel_;
194      this.appliedToModel_ = undefined;
195    },
196
197    unapplyFromModelSelectionState: function() {
198      if (!this.appliedToModel_)
199        throw new Error('Not applied');
200      var model = this.appliedToModel_;
201      this.appliedToModel_ = undefined;
202
203      if (!this.causesDimming_) {
204        this.brightenedEvents_.forEach(function(e) {
205          e.selectionState = SelectionState.NONE;
206        });
207        return;
208      }
209
210      model.iterateAllEvents(function(e) {
211        e.selectionState = SelectionState.NONE;
212      });
213    }
214  };
215
216  return {
217    BrushingState: BrushingState
218  };
219});
220</script>
221