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