discourse/app/assets/javascripts/select-box-kit/mixins/utils.js.es6

58 lines
1.3 KiB
Plaintext
Raw Normal View History

2017-11-09 13:57:53 -05:00
const { get, isNone } = Ember;
2017-10-19 15:51:08 -04:00
export default Ember.Mixin.create({
2017-11-09 13:57:53 -05:00
_nameForContent(content) {
if (isNone(content)) {
return null;
}
if (typeof content === "object") {
return get(content, this.get("nameProperty"));
}
return content;
},
2017-10-19 15:51:08 -04:00
_castInteger(value) {
if (this.get("castInteger") === true && Ember.isPresent(value)) {
return parseInt(value, 10);
}
2017-11-09 13:57:53 -05:00
return value;
},
_valueForContent(content) {
switch (typeof content) {
case "string":
case "number":
return content;
default:
return get(content, this.get("valueAttribute"));
}
},
_contentForValue(value) {
return this.get("content").find(c => {
if (this._valueForContent(c) === value) { return true; }
});
},
_computedContentForValue(value) {
const searchedValue = value.toString();
return this.get("computedContent").find(c => {
if (c.value.toString() === searchedValue) { return true; }
});
},
_originalValueForValue(value) {
if (isNone(value)) { return null; }
if (value === this.noneValue) { return this.noneValue; }
const computedContent = this._computedContentForValue(value);
if (isNone(computedContent)) { return value; }
return get(computedContent.originalContent, this.get("valueAttribute"));
},
2017-10-19 15:51:08 -04:00
});