2019-10-30 16:13:48 -04:00
|
|
|
import Mixin from "@ember/object/mixin";
|
2020-02-05 11:14:42 -05:00
|
|
|
import { get } from "@ember/object";
|
2017-11-09 13:57:53 -05:00
|
|
|
|
2019-10-30 15:03:08 -04:00
|
|
|
export default Mixin.create({
|
2020-02-03 08:22:14 -05:00
|
|
|
defaultItem(value, name) {
|
|
|
|
if (this.selectKit.valueProperty) {
|
|
|
|
const item = {};
|
|
|
|
item[this.selectKit.valueProperty] = value;
|
|
|
|
item[this.selectKit.nameProperty] = name;
|
|
|
|
return item;
|
|
|
|
} else {
|
|
|
|
return name || value;
|
2017-11-21 05:53:09 -05:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2020-02-07 08:12:17 -05:00
|
|
|
itemForValue(value, content) {
|
2020-02-03 08:22:14 -05:00
|
|
|
if (this.selectKit.valueProperty) {
|
2020-02-07 08:12:17 -05:00
|
|
|
return content.findBy(this.selectKit.valueProperty, value);
|
2020-02-03 08:22:14 -05:00
|
|
|
} else {
|
|
|
|
return value;
|
2017-11-09 13:57:53 -05:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2020-02-03 08:22:14 -05:00
|
|
|
getProperty(item, property, options = { definedOnly: true }) {
|
|
|
|
const { definedOnly } = options;
|
2017-11-13 21:51:19 -05:00
|
|
|
|
2020-02-03 08:22:14 -05:00
|
|
|
if (item && typeof property === "string") {
|
|
|
|
const attempt = get(item, property);
|
|
|
|
if (attempt) {
|
|
|
|
return attempt;
|
|
|
|
}
|
|
|
|
}
|
2018-06-26 06:19:14 -04:00
|
|
|
|
2020-02-03 08:22:14 -05:00
|
|
|
property = get(this.selectKit, property);
|
|
|
|
|
|
|
|
if (!item) {
|
|
|
|
return null;
|
2018-06-26 06:19:14 -04:00
|
|
|
}
|
|
|
|
|
2020-02-03 08:22:14 -05:00
|
|
|
if (!property && definedOnly) {
|
|
|
|
return null;
|
|
|
|
} else if (!property) {
|
|
|
|
return item;
|
|
|
|
} else if (typeof property === "string") {
|
|
|
|
return get(item, property);
|
|
|
|
} else {
|
|
|
|
return property(item);
|
|
|
|
}
|
2018-06-26 06:19:14 -04:00
|
|
|
},
|
|
|
|
|
2020-02-03 08:22:14 -05:00
|
|
|
getValue(item) {
|
|
|
|
return this.getProperty(item, "valueProperty", { definedOnly: false });
|
2018-05-24 17:41:39 -04:00
|
|
|
},
|
|
|
|
|
2020-02-03 08:22:14 -05:00
|
|
|
getName(item) {
|
|
|
|
return this.getProperty(item, "nameProperty", { definedOnly: false });
|
2018-05-24 17:41:39 -04:00
|
|
|
},
|
|
|
|
|
2020-02-03 08:22:14 -05:00
|
|
|
findValue(content, item) {
|
2021-01-06 06:57:13 -05:00
|
|
|
return this._findInContent(content, item, "valueProperty", "getValue");
|
|
|
|
},
|
|
|
|
|
|
|
|
findName(content, item) {
|
|
|
|
return this._findInContent(content, item, "nameProperty", "getName");
|
|
|
|
},
|
|
|
|
|
|
|
|
_findInContent(content, item, type, getter) {
|
|
|
|
const property = get(this.selectKit, type);
|
2020-02-03 08:22:14 -05:00
|
|
|
|
|
|
|
if (!property) {
|
|
|
|
if (content.indexOf(item) > -1) {
|
|
|
|
return item;
|
|
|
|
}
|
|
|
|
} else if (typeof property === "string") {
|
2021-01-06 06:57:13 -05:00
|
|
|
return content.findBy(property, this[getter](item));
|
2020-02-03 08:22:14 -05:00
|
|
|
} else {
|
2021-01-06 06:57:13 -05:00
|
|
|
const name = this[getter](item);
|
2020-02-03 08:22:14 -05:00
|
|
|
return content.find((contentItem) => {
|
2021-01-06 06:57:13 -05:00
|
|
|
return this[getter](contentItem) === name;
|
2020-02-03 08:22:14 -05:00
|
|
|
});
|
2017-10-19 15:51:08 -04:00
|
|
|
}
|
2020-02-03 08:22:14 -05:00
|
|
|
},
|
2017-10-19 15:51:08 -04:00
|
|
|
|
2020-02-03 08:22:14 -05:00
|
|
|
_isNumeric(input) {
|
|
|
|
return !isNaN(parseFloat(input)) && isFinite(input);
|
2017-11-09 13:57:53 -05:00
|
|
|
},
|
|
|
|
|
2020-02-03 08:22:14 -05:00
|
|
|
_normalize(input) {
|
|
|
|
if (input) {
|
|
|
|
input = input.toLowerCase();
|
2018-03-22 06:29:55 -04:00
|
|
|
|
2020-02-03 08:22:14 -05:00
|
|
|
if (typeof input.normalize === "function") {
|
|
|
|
input = input.normalize("NFD").replace(/[\u0300-\u036f]/g, "");
|
|
|
|
}
|
2018-03-22 06:29:55 -04:00
|
|
|
}
|
|
|
|
|
2020-02-03 08:22:14 -05:00
|
|
|
return input;
|
2017-11-21 05:53:09 -05:00
|
|
|
},
|
2017-10-19 15:51:08 -04:00
|
|
|
});
|