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