summaryrefslogtreecommitdiffstats
path: root/devtools/client/shared/components/reps/grip-map.js
blob: df673d00521fa6a66ad6c27c36515aa3fb1d79f3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
/* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- */
/* vim: set ft=javascript ts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

"use strict";
// Make this available to both AMD and CJS environments
define(function (require, exports, module) {
  // Dependencies
  const React = require("devtools/client/shared/vendor/react");
  const { createFactories, isGrip } = require("./rep-utils");
  const { Caption } = createFactories(require("./caption"));
  const { PropRep } = createFactories(require("./prop-rep"));

  // Shortcuts
  const { span } = React.DOM;
  /**
   * Renders an map. A map is represented by a list of its
   * entries enclosed in curly brackets.
   */
  const GripMap = React.createClass({
    displayName: "GripMap",

    propTypes: {
      object: React.PropTypes.object,
      mode: React.PropTypes.string,
    },

    getTitle: function (object) {
      let title = object && object.class ? object.class : "Map";
      if (this.props.objectLink) {
        return this.props.objectLink({
          object: object
        }, title);
      }
      return title;
    },

    safeEntriesIterator: function (object, max) {
      max = (typeof max === "undefined") ? 3 : max;
      try {
        return this.entriesIterator(object, max);
      } catch (err) {
        console.error(err);
      }
      return [];
    },

    entriesIterator: function (object, max) {
      // Entry filter. Show only interesting entries to the user.
      let isInterestingEntry = this.props.isInterestingEntry || ((type, value) => {
        return (
          type == "boolean" ||
          type == "number" ||
          (type == "string" && value.length != 0)
        );
      });

      let mapEntries = object.preview && object.preview.entries
        ? object.preview.entries : [];

      let indexes = this.getEntriesIndexes(mapEntries, max, isInterestingEntry);
      if (indexes.length < max && indexes.length < mapEntries.length) {
        // There are not enough entries yet, so we add uninteresting entries.
        indexes = indexes.concat(
          this.getEntriesIndexes(mapEntries, max - indexes.length, (t, value, name) => {
            return !isInterestingEntry(t, value, name);
          })
        );
      }

      let entries = this.getEntries(mapEntries, indexes);
      if (entries.length < mapEntries.length) {
        // There are some undisplayed entries. Then display "more…".
        let objectLink = this.props.objectLink || span;

        entries.push(Caption({
          key: "more",
          object: objectLink({
            object: object
          }, `${mapEntries.length - max} more…`)
        }));
      }

      return entries;
    },

    /**
     * Get entries ordered by index.
     *
     * @param {Array} entries Entries array.
     * @param {Array} indexes Indexes of entries.
     * @return {Array} Array of PropRep.
     */
    getEntries: function (entries, indexes) {
      // Make indexes ordered by ascending.
      indexes.sort(function (a, b) {
        return a - b;
      });

      return indexes.map((index, i) => {
        let [key, entryValue] = entries[index];
        let value = entryValue.value !== undefined ? entryValue.value : entryValue;

        return PropRep({
          // key,
          name: key,
          equal: ": ",
          object: value,
          // Do not add a trailing comma on the last entry
          // if there won't be a "more..." item.
          delim: (i < indexes.length - 1 || indexes.length < entries.length) ? ", " : "",
          mode: "tiny",
          objectLink: this.props.objectLink,
        });
      });
    },

    /**
     * Get the indexes of entries in the map.
     *
     * @param {Array} entries Entries array.
     * @param {Number} max The maximum length of indexes array.
     * @param {Function} filter Filter the entry you want.
     * @return {Array} Indexes of filtered entries in the map.
     */
    getEntriesIndexes: function (entries, max, filter) {
      return entries
        .reduce((indexes, [key, entry], i) => {
          if (indexes.length < max) {
            let value = (entry && entry.value !== undefined) ? entry.value : entry;
            // Type is specified in grip's "class" field and for primitive
            // values use typeof.
            let type = (value && value.class ? value.class : typeof value).toLowerCase();

            if (filter(type, value, key)) {
              indexes.push(i);
            }
          }

          return indexes;
        }, []);
    },

    render: function () {
      let object = this.props.object;
      let props = this.safeEntriesIterator(object,
        (this.props.mode == "long") ? 100 : 3);

      let objectLink = this.props.objectLink || span;
      if (this.props.mode == "tiny") {
        return (
          span({className: "objectBox objectBox-object"},
            this.getTitle(object),
            objectLink({
              className: "objectLeftBrace",
              object: object
            }, "")
          )
        );
      }

      return (
        span({className: "objectBox objectBox-object"},
          this.getTitle(object),
          objectLink({
            className: "objectLeftBrace",
            object: object
          }, " { "),
          props,
          objectLink({
            className: "objectRightBrace",
            object: object
          }, " }")
        )
      );
    },
  });

  function supportsObject(grip, type) {
    if (!isGrip(grip)) {
      return false;
    }
    return (grip.preview && grip.preview.kind == "MapLike");
  }

  // Exports from this module
  exports.GripMap = {
    rep: GripMap,
    supportsObject: supportsObject
  };
});