Bug 1282791 - Order the props, r=Honza

This commit is contained in:
Evan Tseng 2016-07-20 18:17:45 +08:00
parent 623b56a3f1
commit ade553c6cf

View File

@ -63,30 +63,24 @@ define(function (require, exports, module) {
return (
type == "boolean" ||
type == "number" ||
type == "string" ||
type == "object"
(type == "string" && value.length != 0)
);
};
// Object members with non-empty values are preferred since it gives the
// user a better overview of the object.
let props = this.getProps(object, max, isInterestingProp);
if (props.length <= max) {
// There are not enough props yet (or at least, not enough props to
// be able to know whether we should print "more…" or not).
// Let's display also empty members and functions.
props = props.concat(this.getProps(object, max, (t, value) => {
return !isInterestingProp(t, value);
}));
let ownProperties = object.preview ? object.preview.ownProperties : [];
let indexes = this.getPropIndexes(ownProperties, max, isInterestingProp);
if (indexes.length < max && indexes.length < object.ownPropertyLength) {
// There are not enough props yet. Then add uninteresting props to display them.
indexes = indexes.concat(
this.getPropIndexes(ownProperties, max - indexes.length, (t, value) => {
return !isInterestingProp(t, value);
})
);
}
// getProps() can return max+1 properties (it can't return more)
// to indicate that there is more props than allowed. Remove the last
// one and append 'more…' postfix in such case.
if (props.length > max) {
props.pop();
let props = this.getProps(ownProperties, indexes);
if (props.length < object.ownPropertyLength) {
// There are some undisplayed props. Then display "more...".
let objectLink = this.props.objectLink || span;
props.push(Caption({
@ -108,46 +102,73 @@ define(function (require, exports, module) {
return props;
},
getProps: function (object, max, filter) {
/**
* Get props ordered by index.
*
* @param {Object} ownProperties Props object.
* @param {Array} indexes Indexes of props.
* @return {Array} Props.
*/
getProps: function (ownProperties, indexes) {
let props = [];
max = max || 3;
if (!object) {
return props;
}
// Make indexes ordered by ascending.
indexes.sort(function (a, b) {
return a - b;
});
indexes.forEach((i) => {
let name = Object.keys(ownProperties)[i];
let value = ownProperties[name].value;
props.push(PropRep(Object.assign({}, this.props, {
key: name,
mode: "tiny",
name: name,
object: value,
equal: ": ",
delim: ", ",
})));
});
return props;
},
/**
* Get the indexes of props in the object.
*
* @param {Object} ownProperties Props object.
* @param {Number} max The maximum length of indexes array.
* @param {Function} filter Filter the props you want.
* @return {Array} Indexes of interesting props in the object.
*/
getPropIndexes: function (ownProperties, max, filter) {
let indexes = [];
try {
let ownProperties = object.preview ? object.preview.ownProperties : [];
let i = 0;
for (let name in ownProperties) {
if (props.length > max) {
return props;
if (indexes.length >= max) {
return indexes;
}
let prop = ownProperties[name];
let value = prop.value || {};
let value = prop.value;
// Type is specified in grip's "class" field and for primitive
// values use typeof.
let type = (value.class || typeof value);
type = type.toLowerCase();
// Show only interesting properties.
if (filter(type, value)) {
props.push(PropRep(Object.assign({}, this.props, {
key: name,
mode: "tiny",
name: name,
object: value,
equal: ": ",
delim: ", ",
})));
indexes.push(i);
}
i++;
}
} catch (err) {
console.error(err);
}
return props;
return indexes;
},
render: function () {