picture_ops_list_view.js revision 66a37686207944273ced825e0e8b6b6375f8c3de
1// Copyright (c) 2013 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
5'use strict';
6
7base.requireStylesheet('cc.picture_ops_list_view');
8
9base.require('cc.constants');
10base.require('cc.selection');
11base.require('ui.list_view');
12base.require('ui.dom_helpers');
13
14base.exportTo('cc', function() {
15  var constants = cc.constants;
16
17  /**
18   * @constructor
19   */
20  var PictureOpsListView = ui.define('picture-ops-list-view');
21
22  PictureOpsListView.prototype = {
23    __proto__: HTMLUnknownElement.prototype,
24
25    decorate: function() {
26      this.opsList_ = new ui.ListView();
27      this.appendChild(this.opsList_);
28
29      this.selectedOp_ = undefined;
30      this.selectedOpIndex_ = undefined;
31      this.opsList_.addEventListener(
32          'selection-changed', this.onSelectionChanged_.bind(this));
33
34      this.picture_ = undefined;
35    },
36
37    get picture() {
38      return this.picture_;
39    },
40
41    set picture(picture) {
42      this.picture_ = picture;
43      this.updateContents_();
44    },
45
46    updateContents_: function() {
47      this.opsList_.clear();
48
49      if (!this.picture_)
50        return;
51
52      var ops = this.picture_.getOps();
53      if (!ops)
54        return;
55
56      for (var i = 0; i < ops.length; i++) {
57        var op = ops[i];
58        var item = document.createElement('div');
59        item.textContent = i + ') ' + op.cmd_string;
60
61        op.info.forEach(function(info) {
62          var infoItem = document.createElement('div');
63          infoItem.textContent = info;
64          item.appendChild(infoItem);
65        });
66
67        this.opsList_.appendChild(item);
68      }
69    },
70
71    onSelectionChanged_: function(e) {
72      var beforeSelectedOp = true;
73
74      // Deselect on re-selection.
75      if (this.opsList_.selectedElement === this.selectedOp_) {
76        this.opsList_.selectedElement = undefined;
77        beforeSelectedOp = false;
78        this.selectedOpIndex_ = undefined;
79      }
80
81      this.selectedOp_ = this.opsList_.selectedElement;
82
83      // Set selection on all previous ops.
84      var ops = this.opsList_.children;
85      for (var i = 0; i < ops.length; i++) {
86        var op = ops[i];
87        if (op === this.selectedOp_) {
88          beforeSelectedOp = false;
89          this.selectedOpIndex_ = i;
90        } else if (beforeSelectedOp) {
91          op.setAttribute('beforeSelection', 'beforeSelection');
92        } else {
93          op.removeAttribute('beforeSelection');
94        }
95      }
96
97      base.dispatchSimpleEvent(this, 'selection-changed', false);
98    },
99
100    get selectedOpIndex() {
101      return this.selectedOpIndex_;
102    }
103  };
104
105  return {
106    PictureOpsListView: PictureOpsListView
107  };
108});
109