summaryrefslogtreecommitdiffstats
path: root/devtools/client/shared/components/reps/reps/grip-map.js
blob: 918e37ac8c0ba021fafd82fc348ae27e69cb8594 (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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
/* 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 PropTypes = require("devtools/client/shared/vendor/react-prop-types");
  const { span } = require("devtools/client/shared/vendor/react-dom-factories");

  const {
    lengthBubble,
  } = require("devtools/client/shared/components/reps/shared/grip-length-bubble");
  const {
    interleave,
    wrapRender,
    ellipsisElement,
  } = require("devtools/client/shared/components/reps/reps/rep-utils");
  const PropRep = require("devtools/client/shared/components/reps/reps/prop-rep");
  const {
    MODE,
  } = require("devtools/client/shared/components/reps/reps/constants");

  /**
   * Renders an map. A map is represented by a list of its
   * entries enclosed in curly brackets.
   */

  GripMap.propTypes = {
    object: PropTypes.object,
    mode: PropTypes.oneOf(Object.values(MODE)),
    isInterestingEntry: PropTypes.func,
    onDOMNodeMouseOver: PropTypes.func,
    onDOMNodeMouseOut: PropTypes.func,
    onInspectIconClick: PropTypes.func,
    title: PropTypes.string,
    shouldRenderTooltip: PropTypes.bool,
  };

  function GripMap(props) {
    const { mode, object, shouldRenderTooltip } = props;

    const config = {
      "data-link-actor-id": object.actor,
      className: "objectBox objectBox-object",
      title: shouldRenderTooltip ? getTooltip(object, props) : null,
    };

    const title = getTitle(props, object);
    const isEmpty = getLength(object) === 0;

    if (isEmpty || mode === MODE.TINY || mode === MODE.HEADER) {
      return span(config, title);
    }

    const propsArray = safeEntriesIterator(
      props,
      object,
      maxLengthMap.get(mode)
    );

    return span(
      config,
      title,
      span(
        {
          className: "objectLeftBrace",
        },
        " { "
      ),
      ...interleave(propsArray, ", "),
      span(
        {
          className: "objectRightBrace",
        },
        " }"
      )
    );
  }

  function getTitle(props, object) {
    const title =
      props.title || (object && object.class ? object.class : "Map");
    return span(
      {
        className: "objectTitle",
      },
      title,
      lengthBubble({
        object,
        mode: props.mode,
        maxLengthMap,
        getLength,
        showZeroLength: true,
      })
    );
  }

  function getTooltip(object, props) {
    const tooltip =
      props.title || (object && object.class ? object.class : "Map");
    return `${tooltip}(${getLength(object)})`;
  }

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

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

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

    let indexes = 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(
        getEntriesIndexes(
          mapEntries,
          max - indexes.length,
          (t, value, name) => {
            return !isInterestingEntry(t, value, name);
          }
        )
      );
    }

    const entries = getEntries(props, mapEntries, indexes);
    if (entries.length < getLength(object)) {
      // There are some undisplayed entries. Then display "…".
      entries.push(ellipsisElement);
    }

    return entries;
  }

  /**
   * Get entries ordered by index.
   *
   * @param {Object} props Component props.
   * @param {Array} entries Entries array.
   * @param {Array} indexes Indexes of entries.
   * @return {Array} Array of PropRep.
   */
  function getEntries(props, entries, indexes) {
    const { onDOMNodeMouseOver, onDOMNodeMouseOut, onInspectIconClick } = props;

    // Make indexes ordered by ascending.
    indexes.sort(function (a, b) {
      return a - b;
    });

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

      return PropRep({
        name: key && key.getGrip ? key.getGrip() : key,
        equal: " \u2192 ",
        object: value && value.getGrip ? value.getGrip() : value,
        mode: MODE.TINY,
        onDOMNodeMouseOver,
        onDOMNodeMouseOut,
        onInspectIconClick,
      });
    });
  }

  /**
   * 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.
   */
  function getEntriesIndexes(entries, max, filter) {
    return entries.reduce((indexes, [key, entry], i) => {
      if (indexes.length < max) {
        const value = entry && entry.value !== undefined ? entry.value : entry;
        // Type is specified in grip's "class" field and for primitive
        // values use typeof.
        const type = (
          value && value.class ? value.class : typeof value
        ).toLowerCase();

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

      return indexes;
    }, []);
  }

  function getLength(grip) {
    return grip.preview.size || 0;
  }

  function supportsObject(grip) {
    return grip?.preview?.kind == "MapLike";
  }

  const maxLengthMap = new Map();
  maxLengthMap.set(MODE.SHORT, 3);
  maxLengthMap.set(MODE.LONG, 10);

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