wisemapping-frontend/packages/mindplot/src/components/ImageIcon.js

171 lines
6.4 KiB
JavaScript
Raw Normal View History

2021-07-16 16:41:58 +02:00
/*
2021-12-25 23:39:34 +01:00
* Copyright [2021] [wisemapping]
2021-07-16 16:41:58 +02:00
*
* Licensed under WiseMapping Public License, Version 1.0 (the "License").
* It is basically the Apache License, Version 2.0 (the "License") plus the
* "powered by wisemapping" text requirement on every single page;
* you may not use this file except in compliance with the License.
* You may obtain a copy of the license at
*
* http://www.wisemapping.org/license
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
2021-12-05 00:39:20 +01:00
import { $assert } from '@wisemapping/core-js';
import Icon from './Icon';
import ActionDispatcher from './ActionDispatcher';
2021-07-16 16:41:58 +02:00
function importAll(r) {
const images = {};
r.keys().forEach((item) => { images[item.replace('./', '')] = r(item); });
return images;
}
const images = importAll(require.context('../../assets/icons', false, /\.png$/));
2021-12-05 00:39:20 +01:00
class ImageIcon extends Icon {
constructor(topic, iconModel, readOnly) {
2021-10-05 02:05:34 +02:00
$assert(iconModel, 'iconModel can not be null');
$assert(topic, 'topic can not be null');
// Build graph image representation ...
const iconType = iconModel.getIconType();
2021-12-05 00:39:20 +01:00
const imgUrl = ImageIcon._getImageUrl(iconType);
super(imgUrl);
2021-10-05 02:05:34 +02:00
2021-12-05 00:39:20 +01:00
this._topicId = topic.getId();
this._featureModel = iconModel;
2021-12-19 17:06:42 +01:00
2021-10-05 02:05:34 +02:00
if (!readOnly) {
// Icon
const image = this.getImage();
const me = this;
image.addEvent('click', () => {
const iconTypeClick = iconModel.getIconType();
const newIconType = ImageIcon._getNextFamilyIconId(iconTypeClick);
2021-10-05 02:05:34 +02:00
iconModel.setIconType(newIconType);
me._image.setHref(ImageIcon._getImageUrl(newIconType));
2021-10-05 02:05:34 +02:00
});
this._image.setCursor('pointer');
}
2021-12-05 00:39:20 +01:00
}
2021-10-05 02:05:34 +02:00
2021-12-05 00:39:20 +01:00
static _getImageUrl(iconId) {
return images[`${iconId}.png`];
2021-12-05 00:39:20 +01:00
}
2021-10-05 02:05:34 +02:00
getModel() {
return this._featureModel;
2021-12-05 00:39:20 +01:00
}
2021-10-05 02:05:34 +02:00
2021-12-05 00:39:20 +01:00
static _getNextFamilyIconId(iconId) {
const familyIcons = ImageIcon._getFamilyIcons(iconId);
2021-10-05 02:05:34 +02:00
$assert(familyIcons != null, 'Family Icon not found!');
let result = null;
for (let i = 0; i < familyIcons.length && result == null; i++) {
2021-12-05 00:39:20 +01:00
if (familyIcons[i] === iconId) {
2021-10-05 02:05:34 +02:00
// Is last one?
2021-12-05 00:39:20 +01:00
if (i === (familyIcons.length - 1)) {
[result] = familyIcons;
2021-10-05 02:05:34 +02:00
} else {
result = familyIcons[i + 1];
2021-07-16 16:41:58 +02:00
}
2021-10-05 02:05:34 +02:00
break;
}
}
2021-07-16 16:41:58 +02:00
2021-10-05 02:05:34 +02:00
return result;
2021-12-05 00:39:20 +01:00
}
2021-07-16 16:41:58 +02:00
2021-12-05 00:39:20 +01:00
static _getFamilyIcons(iconId) {
2021-10-05 02:05:34 +02:00
$assert(iconId != null, 'id must not be null');
2021-12-05 00:39:20 +01:00
$assert(iconId.indexOf('_') !== -1, "Invalid icon id (it must contain '_')");
2021-07-16 16:41:58 +02:00
2021-10-05 02:05:34 +02:00
let result = null;
for (let i = 0; i < ImageIcon.prototype.ICON_FAMILIES.length; i++) {
const family = ImageIcon.prototype.ICON_FAMILIES[i];
const iconFamilyId = iconId.substr(0, iconId.indexOf('_'));
2021-07-16 16:41:58 +02:00
2021-12-05 00:39:20 +01:00
if (family.id === iconFamilyId) {
2021-10-05 02:05:34 +02:00
result = family.icons;
break;
}
2021-07-16 16:41:58 +02:00
}
2021-10-05 02:05:34 +02:00
return result;
2021-12-05 00:39:20 +01:00
}
2021-10-05 02:05:34 +02:00
remove() {
const actionDispatcher = ActionDispatcher.getInstance();
const featureId = this._featureModel.getId();
const topicId = this._topicId;
actionDispatcher.removeFeatureFromTopic(topicId, featureId);
2021-12-05 00:39:20 +01:00
}
}
2021-07-16 16:41:58 +02:00
ImageIcon.prototype.ICON_FAMILIES = [
2021-10-05 02:05:34 +02:00
{ id: 'face', icons: ['face_plain', 'face_sad', 'face_crying', 'face_smile', 'face_surprise', 'face_wink'] },
{ id: 'funy', icons: ['funy_angel', 'funy_devilish', 'funy_glasses', 'funy_grin', 'funy_kiss', 'funy_monkey'] },
{ id: 'conn', icons: ['conn_connect', 'conn_disconnect'] },
{
id: 'sport',
icons: ['sport_basketball', 'sport_football', 'sport_golf', 'sport_raquet', 'sport_shuttlecock', 'sport_soccer', 'sport_tennis'],
},
{ id: 'bulb', icons: ['bulb_light_on', 'bulb_light_off'] },
{ id: 'thumb', icons: ['thumb_thumb_up', 'thumb_thumb_down'] },
{ id: 'tick', icons: ['tick_tick', 'tick_cross'] },
{
id: 'onoff',
icons: ['onoff_clock', 'onoff_clock_red', 'onoff_add', 'onoff_delete', 'onoff_status_offline', 'onoff_status_online'],
},
{
id: 'money',
icons: ['money_money', 'money_dollar', 'money_euro', 'money_pound', 'money_yen', 'money_coins', 'money_ruby'],
},
{ id: 'time', icons: ['time_calendar', 'time_clock', 'time_hourglass'] },
{
id: 'number',
icons: ['number_1', 'number_2', 'number_3', 'number_4', 'number_5', 'number_6', 'number_7', 'number_8', 'number_9'],
},
{ id: 'chart', icons: ['chart_bar', 'chart_line', 'chart_curve', 'chart_pie', 'chart_organisation'] },
{ id: 'sign', icons: ['sign_warning', 'sign_info', 'sign_stop', 'sign_help', 'sign_cancel'] },
{
id: 'hard',
icons: ['hard_cd', 'hard_computer', 'hard_controller', 'hard_driver_disk', 'hard_ipod', 'hard_keyboard', 'hard_mouse', 'hard_printer'],
},
{
id: 'soft',
icons: ['soft_bug', 'soft_cursor', 'soft_database_table', 'soft_database', 'soft_feed', 'soft_folder_explore', 'soft_rss', 'soft_penguin'],
},
{ id: 'arrow', icons: ['arrow_up', 'arrow_down', 'arrow_left', 'arrow_right'] },
{
id: 'arrowc',
icons: ['arrowc_rotate_anticlockwise', 'arrowc_rotate_clockwise', 'arrowc_turn_left', 'arrowc_turn_right'],
},
{ id: 'people', icons: ['people_group', 'people_male1', 'people_male2', 'people_female1', 'people_female2'] },
{ id: 'mail', icons: ['mail_envelop', 'mail_mailbox', 'mail_edit', 'mail_list'] },
{ id: 'flag', icons: ['flag_blue', 'flag_green', 'flag_orange', 'flag_pink', 'flag_purple', 'flag_yellow'] },
{
id: 'bullet',
icons: ['bullet_black', 'bullet_blue', 'bullet_green', 'bullet_orange', 'bullet_red', 'bullet_pink', 'bullet_purple'],
},
{ id: 'tag', icons: ['tag_blue', 'tag_green', 'tag_orange', 'tag_red', 'tag_pink', 'tag_yellow'] },
{
id: 'object',
icons: ['object_bell', 'object_clanbomber', 'object_key', 'object_pencil', 'object_phone', 'object_magnifier', 'object_clip',
'object_music', 'object_star', 'object_wizard', 'object_house', 'object_cake', 'object_camera', 'object_palette', 'object_rainbow'],
2021-10-05 02:05:34 +02:00
},
{
id: 'weather',
icons: ['weather_clear-night', 'weather_clear', 'weather_few-clouds-night', 'weather_few-clouds', 'weather_overcast', 'weather_severe-alert', 'weather_showers-scattered', 'weather_showers', 'weather_snow', 'weather_storm'],
},
{ id: 'task', icons: ['task_0', 'task_25', 'task_50', 'task_75', 'task_100'] },
2021-07-16 16:41:58 +02:00
];
export default ImageIcon;