Changeset View
Changeset View
Standalone View
Standalone View
lib/app/base-chips/field-types/text.js
"use strict"; | |||||
const Promise = require("bluebird"); | const Promise = require("bluebird"); | ||||
const escape = require("escape-html"); | const escape = require("escape-html"); | ||||
module.exports = { | module.exports = { | ||||
name: "text", | name: "text", | ||||
has_index: function(params) { | has_index(params) { | ||||
if (params.full_text_search || params.include_in_search) { | if (params.full_text_search || params.include_in_search) { | ||||
return { original: "text" }; | return { original: "text" }; | ||||
} else { | |||||
return false; | |||||
} | } | ||||
return false; | |||||
}, | }, | ||||
get_description: function(context, params) { | get_description(context, params) { | ||||
return `Text with maximum length ${params.max_length}`; | return `Text with maximum length ${params.max_length}`; | ||||
}, | }, | ||||
is_proper_value: function(context, params, new_value) { | is_proper_value(context, params, new_value) { | ||||
let checks = []; | let checks = []; | ||||
checks.push(text => { | checks.push(text => { | ||||
if (typeof new_value !== "string") { | if (typeof new_value !== "string") { | ||||
return Promise.reject( | return Promise.reject( | ||||
`Type of ${new_value} is ${typeof new_value}, not string.` | `Type of ${new_value} is ${typeof new_value}, not string.` | ||||
); | ); | ||||
} | } | ||||
Show All 19 Lines | if (params.max_length) { | ||||
); | ); | ||||
} | } | ||||
}); | }); | ||||
} | } | ||||
return Promise.all(checks.map(fn => fn(new_value))).then(() => | return Promise.all(checks.map(fn => fn(new_value))).then(() => | ||||
Promise.resolve() | Promise.resolve() | ||||
); | ); | ||||
}, | }, | ||||
encode: function(context, params, value_in_code) { | encode(context, params, value_in_code) { | ||||
if (typeof value_in_code === "string" && value_in_code !== null) { | if (typeof value_in_code === "string" && value_in_code !== null) { | ||||
const result = { | const result = { | ||||
original: value_in_code, | original: value_in_code, | ||||
safe: escape(value_in_code), | safe: escape(value_in_code), | ||||
valueOf: function() { | valueOf() { | ||||
return value_in_code; | return value_in_code; | ||||
}, | }, | ||||
}; | }; | ||||
return Promise.resolve(result); | return Promise.resolve(result); | ||||
} else return Promise.resolve(null); | } | ||||
return Promise.resolve(null); | |||||
}, | }, | ||||
get_aggregation_stages: function( | get_aggregation_stages( | ||||
context, | context, | ||||
params, | params, | ||||
field_name, | field_name, | ||||
field_value_path, | field_value_path, | ||||
query_params | query_params | ||||
) { | ) { | ||||
let filter_value = | let filter_value = | ||||
query_params.filter && query_params.filter[field_name]; | query_params.filter && query_params.filter[field_name]; | ||||
Show All 18 Lines | return [ | ||||
{ | { | ||||
[`${field_value_path}.safe`]: filter_value, | [`${field_value_path}.safe`]: filter_value, | ||||
}, | }, | ||||
], | ], | ||||
}, | }, | ||||
}, | }, | ||||
]; | ]; | ||||
}, | }, | ||||
format: function(context, params, decoded_value, format) { | format(context, params, decoded_value, format) { | ||||
if (decoded_value === null || decoded_value === undefined) { | if (decoded_value === null || decoded_value === undefined) { | ||||
return Promise.resolve(decoded_value); | return Promise.resolve(decoded_value); | ||||
} | } | ||||
if (format === undefined) { | if (format === undefined) { | ||||
return decoded_value.safe; | return decoded_value.safe; | ||||
} | } | ||||
return decoded_value[format] ? decoded_value[format] : decoded_value; | return decoded_value[format] ? decoded_value[format] : decoded_value; | ||||
}, | }, | ||||
}; | }; |