2020-02-04 18:41:10 -05:00
|
|
|
import MultiSelectComponent from "select-kit/components/multi-select";
|
|
|
|
import { computed } from "@ember/object";
|
|
|
|
import { ajax } from "discourse/lib/ajax";
|
|
|
|
import { makeArray } from "discourse-common/lib/helpers";
|
2020-06-01 16:33:43 -04:00
|
|
|
import {
|
|
|
|
convertIconClass,
|
|
|
|
disableMissingIconWarning,
|
|
|
|
enableMissingIconWarning,
|
|
|
|
} from "discourse-common/lib/icon-library";
|
|
|
|
import { isDevelopment } from "discourse-common/config/environment";
|
2020-02-04 18:41:10 -05:00
|
|
|
|
|
|
|
export default MultiSelectComponent.extend({
|
|
|
|
pluginApiIdentifiers: ["icon-picker"],
|
|
|
|
classNames: ["icon-picker"],
|
|
|
|
|
2020-02-05 03:22:11 -05:00
|
|
|
init() {
|
|
|
|
this._super(...arguments);
|
|
|
|
|
|
|
|
this._cachedIconsList = null;
|
2020-02-05 11:39:24 -05:00
|
|
|
|
2020-06-01 16:33:43 -04:00
|
|
|
if (isDevelopment()) {
|
|
|
|
disableMissingIconWarning();
|
2020-02-05 11:39:24 -05:00
|
|
|
}
|
2020-02-05 03:22:11 -05:00
|
|
|
},
|
|
|
|
|
2020-02-04 18:41:10 -05:00
|
|
|
content: computed("value.[]", function () {
|
|
|
|
return makeArray(this.value).map(this._processIcon);
|
|
|
|
}),
|
|
|
|
|
|
|
|
search(filter = "") {
|
2020-02-05 03:22:11 -05:00
|
|
|
if (
|
|
|
|
filter === "" &&
|
|
|
|
this._cachedIconsList &&
|
|
|
|
this._cachedIconsList.length
|
|
|
|
) {
|
|
|
|
return this._cachedIconsList;
|
|
|
|
} else {
|
|
|
|
return ajax("/svg-sprite/picker-search", {
|
|
|
|
data: { filter },
|
|
|
|
}).then((icons) => {
|
|
|
|
icons = icons.map(this._processIcon);
|
|
|
|
if (filter === "") {
|
|
|
|
this._cachedIconsList = icons;
|
|
|
|
}
|
|
|
|
return icons;
|
|
|
|
});
|
|
|
|
}
|
2020-02-04 18:41:10 -05: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 10:28:28 -04:00
|
|
|
if ($(`${spriteEl} .${holder}`).length === 0) {
|
2020-02-04 18:41:10 -05:00
|
|
|
$(spriteEl).append(
|
|
|
|
`<div class="${holder}" style='display: none;'></div>`
|
|
|
|
);
|
2020-09-22 10:28:28 -04:00
|
|
|
}
|
2020-02-04 18:41:10 -05: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 03:22:11 -05:00
|
|
|
|
|
|
|
this._cachedIconsList = null;
|
2020-02-05 11:39:24 -05:00
|
|
|
|
2020-06-01 16:33:43 -04:00
|
|
|
if (isDevelopment()) {
|
|
|
|
enableMissingIconWarning();
|
2020-02-05 11:39:24 -05:00
|
|
|
}
|
2020-02-04 18:41:10 -05: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);
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|