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/model/location.html">
9
10<script>
11'use strict';
12
13tr.exportTo('tr.ui.b', function() {
14  var Location = tr.model.Location;
15
16  /**
17   * UIState is a class that represents the current state of the timeline by
18   * the Location of the point of interest and the current scaleX of the
19   * timeline.
20   *
21   * @constructor
22   */
23  function UIState(location, scaleX) {
24    this.location_ = location;
25    this.scaleX_ = scaleX;
26  };
27
28  /**
29   * Accepts a UIState string in the format of (timestamp)@(stableID)x(scaleX)
30   * Returns undefined if string is not in this format, or throws an Error if
31   * variables in a syntactically-correct stateString does not produce a valid
32   * UIState. Otherwise returns a constructed UIState instance.
33   */
34  UIState.fromUserFriendlyString = function(model, viewport, stateString) {
35    var navByFinderPattern = /^(-?\d+(\.\d+)?)@(.+)x(\d+(\.\d+)?)$/g;
36    var match = navByFinderPattern.exec(stateString);
37    if (!match)
38      return;
39
40    var timestamp = parseFloat(match[1]);
41    var stableId = match[3];
42    var scaleX = parseFloat(match[4]);
43
44    if (scaleX <= 0)
45      throw new Error('Invalid ScaleX value in UI State string.');
46
47    if (!viewport.containerToTrackMap.getTrackByStableId(stableId))
48      throw new Error('Invalid StableID given in UI State String.');
49
50    var loc = tr.model.Location.fromStableIdAndTimestamp(
51        viewport, stableId, timestamp);
52    return new UIState(loc, scaleX);
53  }
54
55  UIState.prototype = {
56
57    get location() {
58      return this.location_;
59    },
60
61    get scaleX() {
62      return this.scaleX_;
63    },
64
65    toUserFriendlyString: function(viewport) {
66      var timestamp = this.location_.xWorld;
67      var stableId =
68          this.location_.getContainingTrack(viewport).eventContainer.stableId;
69      var scaleX = this.scaleX_;
70      return timestamp.toFixed(5) + '@' + stableId + 'x' + scaleX.toFixed(5);
71    },
72
73    toDict: function() {
74      return {
75        location: this.location_.toDict(),
76        scaleX: this.scaleX_
77      };
78    }
79  };
80
81  return {
82    UIState: UIState
83  };
84});
85</script>
86