Files
discourse-data-explorer/assets/javascripts/discourse/components/explorer-schema.js.es6
T
Roman Rizzi c6a11d9280 DEV: Use the new discourseDebounce function wrapper. (#85)
We recently merged a Discourse core's PR to replace usages of Ember's debounce and discourseDebounce with a new debounce wrapper. The new wrapper works exactly like Ember's debounce but internally calls "run" when called in test mode.

This PR replaces all usages of other debounce functions with the new wrapper and fallbacks to Ember's debounce for backward-compatibility.
2021-01-05 13:09:21 -03:00

143 lines
3.2 KiB
JavaScript

import {
default as computed,
observes,
} from "discourse-common/utils/decorators";
import discourseDebounce from "discourse-common/lib/debounce";
import debounce from "@ember/runloop";
export default Ember.Component.extend({
actions: {
collapseSchema() {
this.set("hideSchema", true);
},
},
@computed("schema")
transformedSchema(schema) {
for (let key in schema) {
if (!schema.hasOwnProperty(key)) {
continue;
}
schema[key].forEach((col) => {
const notes_components = [];
if (col.primary) {
notes_components.push("primary key");
}
if (col.is_nullable) {
notes_components.push("null");
}
if (col.column_default) {
notes_components.push("default " + col.column_default);
}
if (col.fkey_info) {
notes_components.push("fkey " + col.fkey_info);
}
if (col.denormal) {
notes_components.push("denormal " + col.denormal);
}
const notes = notes_components.join(", ");
if (notes) {
col.notes = notes;
}
if (col.enum || col.column_desc) {
col.havepopup = true;
}
col.havetypeinfo = !!(col.notes || col.enum || col.column_desc);
});
}
return schema;
},
@computed("filter")
rfilter(filter) {
if (!Ember.isBlank(filter)) {
return new RegExp(filter);
}
},
filterTables(schema) {
let tables = [];
const filter = this.rfilter,
haveFilter = !!filter;
for (let key in schema) {
if (!schema.hasOwnProperty(key)) {
continue;
}
if (!haveFilter) {
tables.push({
name: key,
columns: schema[key],
open: false,
});
continue;
}
// Check the table name vs the filter
if (filter.source === key || filter.source + "s" === key) {
tables.unshift({
name: key,
columns: schema[key],
open: haveFilter,
});
} else if (filter.test(key)) {
// whole table matches
tables.push({
name: key,
columns: schema[key],
open: haveFilter,
});
} else {
// filter the columns
let filterCols = [];
schema[key].forEach((col) => {
if (filter.source === col.column_name) {
filterCols.unshift(col);
} else if (filter.test(col.column_name)) {
filterCols.push(col);
}
});
if (!Ember.isEmpty(filterCols)) {
tables.push({
name: key,
columns: filterCols,
open: haveFilter,
});
}
}
}
return tables;
},
@observes("filter")
triggerFilter() {
// TODO: Use discouseDebounce after the 2.7 release.
let debounceFunc = discourseDebounce || debounce;
debounceFunc(
this,
function () {
this.set("filteredTables", this.filterTables(this.transformedSchema));
this.set("loading", false);
},
500
);
},
@observes("filter")
setLoading() {
this.set("loading", true);
},
init() {
this._super(...arguments);
this.set("loading", true);
this.triggerFilter();
},
});