2020-06-02 04:33:43 +08:00
|
|
|
import {
|
|
|
|
convertIconClass,
|
|
|
|
disableMissingIconWarning,
|
|
|
|
enableMissingIconWarning,
|
|
|
|
} from "discourse-common/lib/icon-library";
|
2020-02-05 07:41:10 +08:00
|
|
|
import MultiSelectComponent from "select-kit/components/multi-select";
|
|
|
|
import { computed } from "@ember/object";
|
2020-06-02 04:33:43 +08:00
|
|
|
import { isDevelopment } from "discourse-common/config/environment";
|
2020-02-05 07:41:10 +08:00
|
|
|
import { makeArray } from "discourse-common/lib/helpers";
|
2022-06-17 20:50:21 +08:00
|
|
|
import { ajax } from "discourse/lib/ajax";
|
2020-02-05 07:41:10 +08:00
|
|
|
|
|
|
|
export default MultiSelectComponent.extend({
|
|
|
|
pluginApiIdentifiers: ["icon-picker"],
|
|
|
|
classNames: ["icon-picker"],
|
|
|
|
|
2020-02-05 16:22:11 +08:00
|
|
|
init() {
|
|
|
|
this._super(...arguments);
|
|
|
|
|
|
|
|
this._cachedIconsList = null;
|
2020-02-06 00:39:24 +08:00
|
|
|
|
2020-06-02 04:33:43 +08:00
|
|
|
if (isDevelopment()) {
|
|
|
|
disableMissingIconWarning();
|
2020-02-06 00:39:24 +08:00
|
|
|
}
|
2020-02-05 16:22:11 +08:00
|
|
|
},
|
|
|
|
|
2020-02-05 07:41:10 +08:00
|
|
|
content: computed("value.[]", function () {
|
|
|
|
return makeArray(this.value).map(this._processIcon);
|
|
|
|
}),
|
|
|
|
|
|
|
|
search(filter = "") {
|
2020-02-05 16:22:11 +08:00
|
|
|
if (
|
|
|
|
filter === "" &&
|
|
|
|
this._cachedIconsList &&
|
|
|
|
this._cachedIconsList.length
|
|
|
|
) {
|
|
|
|
return this._cachedIconsList;
|
|
|
|
} else {
|
|
|
|
return ajax("/svg-sprite/picker-search", {
|
2023-02-13 06:24:47 +08:00
|
|
|
data: {
|
|
|
|
filter,
|
|
|
|
only_available: this.onlyAvailable,
|
|
|
|
},
|
2020-02-05 16:22:11 +08:00
|
|
|
}).then((icons) => {
|
|
|
|
icons = icons.map(this._processIcon);
|
|
|
|
if (filter === "") {
|
|
|
|
this._cachedIconsList = icons;
|
|
|
|
}
|
|
|
|
return icons;
|
|
|
|
});
|
|
|
|
}
|
2020-02-05 07:41:10 +08:00
|
|
|
},
|
|
|
|
|
|
|
|
_processIcon(icon) {
|
|
|
|
const iconName = typeof icon === "object" ? icon.id : icon,
|
|
|
|
strippedIconName = convertIconClass(iconName);
|
|
|
|
|
|
|
|
const spriteEl = "#svg-sprites",
|
|
|
|
holder = "ajax-icon-holder";
|
|
|
|
|
|
|
|
if (typeof icon === "object") {
|
2020-09-22 22:28:28 +08:00
|
|
|
if ($(`${spriteEl} .${holder}`).length === 0) {
|
2020-02-05 07:41:10 +08:00
|
|
|
$(spriteEl).append(
|
|
|
|
`<div class="${holder}" style='display: none;'></div>`
|
|
|
|
);
|
2020-09-22 22:28:28 +08:00
|
|
|
}
|
2020-02-05 07:41:10 +08:00
|
|
|
|
|
|
|
if (!$(`${spriteEl} symbol#${strippedIconName}`).length) {
|
|
|
|
$(`${spriteEl} .${holder}`).append(
|
|
|
|
`<svg xmlns='http://www.w3.org/2000/svg'>${icon.symbol}</svg>`
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
id: iconName,
|
|
|
|
name: iconName,
|
|
|
|
icon: strippedIconName,
|
|
|
|
};
|
|
|
|
},
|
|
|
|
|
|
|
|
willDestroyElement() {
|
|
|
|
$("#svg-sprites .ajax-icon-holder").remove();
|
|
|
|
this._super(...arguments);
|
2020-02-05 16:22:11 +08:00
|
|
|
|
|
|
|
this._cachedIconsList = null;
|
2020-02-06 00:39:24 +08:00
|
|
|
|
2020-06-02 04:33:43 +08:00
|
|
|
if (isDevelopment()) {
|
|
|
|
enableMissingIconWarning();
|
2020-02-06 00:39:24 +08:00
|
|
|
}
|
2020-02-05 07:41:10 +08:00
|
|
|
},
|
|
|
|
|
|
|
|
actions: {
|
|
|
|
onChange(value, item) {
|
|
|
|
if (this.selectKit.options.maximum === 1) {
|
|
|
|
value = value.length ? value[0] : null;
|
|
|
|
item = item.length ? item[0] : null;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.attrs.onChange && this.attrs.onChange(value, item);
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|