discourse-data-explorer/assets/javascripts/discourse/components/param-input.js.es6

146 lines
3.9 KiB
Plaintext
Raw Normal View History

// import Category from 'discourse/models/category';
const Category = Discourse.Category;
2015-07-14 19:01:38 -04:00
const layoutMap = {
2018-10-10 07:56:23 -04:00
int: "int",
bigint: "int",
boolean: "boolean",
string: "generic",
time: "generic",
date: "generic",
datetime: "generic",
double: "string",
user_id: "user_id",
post_id: "string",
topic_id: "generic",
category_id: "generic",
group_id: "generic",
badge_id: "generic",
int_list: "generic",
string_list: "generic",
user_list: "user_list"
2015-07-14 19:01:38 -04:00
};
function allowsInputTypeTime() {
try {
2018-10-10 07:56:23 -04:00
const inp = document.createElement("input");
inp.attributes.type = "time";
inp.attributes.type = "date";
2015-07-14 19:01:38 -04:00
return true;
} catch (e) {
return false;
}
}
export default Ember.Component.extend({
2018-10-10 07:56:23 -04:00
classNameBindings: ["valid:valid:invalid", ":param"],
2015-07-14 19:01:38 -04:00
2018-10-10 07:56:23 -04:00
boolTypes: [
{ name: I18n.t("explorer.types.bool.true"), id: "Y" },
{ name: I18n.t("explorer.types.bool.false"), id: "N" },
{ name: I18n.t("explorer.types.bool.null_"), id: "#null" }
],
2015-07-14 19:01:38 -04:00
2018-10-10 07:56:23 -04:00
value: Ember.computed("params", "info.identifier", {
2016-11-09 11:58:18 -05:00
get() {
2018-10-10 07:56:23 -04:00
return this.get("params")[this.get("info.identifier")];
2016-11-09 11:58:18 -05:00
},
set(key, value) {
2018-10-10 07:56:23 -04:00
this.get("params")[this.get("info.identifier")] = value.toString();
2016-11-09 11:58:18 -05:00
return value;
2015-07-14 19:01:38 -04:00
}
2016-11-09 11:58:18 -05:00
}),
2015-07-15 14:00:31 -04:00
2018-10-10 07:56:23 -04:00
valueBool: Ember.computed("params", "info.identifier", {
2016-11-09 11:58:18 -05:00
get() {
2018-10-10 07:56:23 -04:00
return this.get("params")[this.get("info.identifier")] !== "false";
2016-11-09 11:58:18 -05:00
},
set(key, value) {
value = !!value;
2018-10-10 07:56:23 -04:00
this.get("params")[this.get("info.identifier")] = value.toString();
2016-11-09 11:58:18 -05:00
return value;
2015-07-15 14:00:31 -04:00
}
2016-11-09 11:58:18 -05:00
}),
2015-07-14 19:01:38 -04:00
valid: function() {
2018-10-10 07:56:23 -04:00
const type = this.get("info.type"),
value = this.get("value");
2015-07-14 19:01:38 -04:00
2018-10-10 07:56:23 -04:00
if (Em.isEmpty(this.get("value"))) {
return this.get("info.nullable");
2015-07-14 19:01:38 -04:00
}
const intVal = parseInt(value, 10);
2018-10-10 07:56:23 -04:00
const intValid =
!isNaN(intVal) && intVal < 2147483648 && intVal > -2147483649;
const isPositiveInt = /^\d+$/.test(value);
2015-07-14 19:01:38 -04:00
switch (type) {
2018-10-10 07:56:23 -04:00
case "int":
2015-07-14 19:01:38 -04:00
return /^-?\d+$/.test(value) && intValid;
2018-10-10 07:56:23 -04:00
case "bigint":
2015-07-14 19:01:38 -04:00
return /^-?\d+$/.test(value) && !isNaN(intVal);
2018-10-10 07:56:23 -04:00
case "boolean":
2015-07-14 19:01:38 -04:00
return /^Y|N|#null|true|false/.test(value);
2018-10-10 07:56:23 -04:00
case "double":
return (
!isNaN(parseFloat(value)) ||
/^(-?)Inf(inity)?$/i.test(value) ||
/^(-?)NaN$/i.test(value)
);
case "int_list":
return value.split(",").every(function(i) {
2015-07-14 19:01:38 -04:00
return /^(-?\d+|null)$/.test(i.trim());
});
2018-10-10 07:56:23 -04:00
case "post_id":
return isPositiveInt || /\d+\/\d+(\?u=.*)?$/.test(value);
2018-10-10 07:56:23 -04:00
case "category_id":
if (!isPositiveInt && value !== value.dasherize()) {
2018-10-10 07:56:23 -04:00
this.set("value", value.dasherize());
}
if (isPositiveInt) {
2015-07-15 19:16:07 -04:00
return !!this.site.categories.find(function(c) {
2018-10-10 07:56:23 -04:00
return c.get("id") === intVal;
});
} else if (/\//.test(value)) {
const match = /(.*)\/(.*)/.exec(value);
if (!match) return false;
2018-10-10 07:56:23 -04:00
const result = Category.findBySlug(
match[2].dasherize(),
match[1].dasherize()
);
return !!result;
} else {
return !!Category.findBySlug(value.dasherize());
}
2018-10-10 07:56:23 -04:00
case "group_id":
const groups = this.site.get("groups");
if (isPositiveInt) {
return !!groups.find(function(g) {
return g.id === intVal;
});
} else {
return !!groups.find(function(g) {
return g.name === value;
});
}
2015-07-14 19:01:38 -04:00
}
return true;
2018-10-10 07:56:23 -04:00
}.property("value", "info.type", "info.nullable"),
2015-07-14 19:01:38 -04:00
layoutType: function() {
2018-10-10 07:56:23 -04:00
const type = this.get("info.type");
2015-07-14 19:01:38 -04:00
if ((type === "time" || type === "date") && !allowsInputTypeTime()) {
return "string";
}
if (layoutMap[type]) {
return layoutMap[type];
}
2018-10-10 07:56:23 -04:00
return "generic";
}.property("info.type"),
2015-07-14 19:01:38 -04:00
layoutName: function() {
2018-10-10 07:56:23 -04:00
return "admin/components/q-params/" + this.get("layoutType");
}.property("layoutType")
2015-07-14 19:01:38 -04:00
});