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/side_panel/side_panel.html">
9<link rel="import" href="/tracing/model/comment_box_annotation.html">
10
11<link rel="import" href="/tracing/ui/extras/drive/comments_side_panel.html">
12
13<script>
14'use strict';
15
16(function() {
17  function addDriveCommentWithUIState_(text, uiState) {
18    gapi.client.load('drive', 'v2', function() {
19      var request = gapi.client.drive.revisions.get({
20        'fileId': tr.ui.e.drive.getDriveFileId(),
21        'revisionId': 'head'
22      });
23      request.execute(function(resp) {
24        var anchorObject = {};
25        anchorObject[tr.ui.e.drive.constants.ANCHOR_NAME] = uiState;
26        var anchor = {
27          'r': resp.id,
28          'a': [anchorObject]
29        };
30        anchor = JSON.stringify(anchor);
31        gapi.client.load('drive', 'v2', function() {
32          var request = gapi.client.drive.comments.insert({
33            'fileId': tr.ui.e.drive.getDriveFileId(),
34            'resource': {'content': text, 'anchor': anchor}
35          });
36          request.execute();
37        });
38      });
39    });
40  };
41
42  function onCommentWithUIState(e) {
43    addDriveCommentWithUIState_(e.detail.name, e.detail.location);
44  };
45
46  document.addEventListener('commentWithUIState',
47                            onCommentWithUIState.bind(this));
48}());
49
50tr.exportTo('tr.ui.e.drive.analysis', function() {
51  function DefaultCommentProvider() { }
52
53  DefaultCommentProvider.prototype = {
54    attachToElement: function(attachedElement) {
55      this.attachedElement_ = attachedElement;
56      this.commentsCheckTimer_ = setTimeout(this.checkForComments_.bind(this),
57                                            5000);
58    },
59
60    detachFromElement: function() {
61      clearTimeout(this.commentsCheckTimer_);
62    },
63
64    checkForComments_: function() {
65      this.updateComments();
66      this.commentsCheckTimer_ = setTimeout(this.checkForComments_.bind(this),
67                                            5000);
68    },
69
70    updateComments: function() {
71      var self = this;
72      gapi.client.load('drive', 'v2', function() {
73        var request = gapi.client.drive.comments.list({
74          'fileId': tr.ui.e.drive.getDriveFileId()
75        });
76        request.execute(function(results) {
77          self.attachedElement_.comments_ = results.items;
78        });
79      });
80    },
81
82    addComment: function(body) {
83      var self = this;
84      gapi.client.load('drive', 'v2', function() {
85        var request = gapi.client.drive.comments.insert({
86          'fileId': tr.ui.e.drive.getDriveFileId(),
87          'resource': {'content': body}
88        });
89        request.execute(function(resp) {
90          self.updateComments();
91        });
92      });
93    }
94  };
95
96  return {
97    DefaultCommentProvider: DefaultCommentProvider
98  };
99});
100
101</script>
102