keeweb/app/scripts/views/icon-select-view.js

145 lines
4.9 KiB
JavaScript
Raw Normal View History

2019-09-16 20:42:33 +02:00
import { View } from 'framework/views/view';
2019-09-15 14:16:32 +02:00
import { IconMap } from 'const/icon-map';
import { Logger } from 'util/logger';
2019-09-15 20:09:28 +02:00
import template from 'templates/icon-select.hbs';
2015-12-12 09:53:50 +01:00
2017-01-31 07:50:28 +01:00
const logger = new Logger('icon-select-view');
2015-10-17 23:49:24 +02:00
2019-09-15 20:09:28 +02:00
class IconSelectView extends View {
template = template;
2015-10-17 23:49:24 +02:00
2019-09-15 20:09:28 +02:00
events = {
2015-11-21 18:04:04 +01:00
'click .icon-select__icon': 'iconClick',
'click .icon-select__icon-download': 'downloadIcon',
'click .icon-select__icon-select': 'selectIcon',
'change .icon-select__file-input': 'iconSelected'
2019-09-15 20:09:28 +02:00
};
2015-10-17 23:49:24 +02:00
2019-09-15 20:09:28 +02:00
special = {
select: null,
download: null
};
2015-11-22 09:21:12 +01:00
2019-08-18 10:17:09 +02:00
render() {
2019-09-15 20:09:28 +02:00
super.render({
sel: this.model.iconId,
icons: IconMap,
canDownloadFavicon: !!this.model.url,
customIcons: this.model.file.getCustomIcons()
});
}
2015-10-17 23:49:24 +02:00
2019-08-18 10:17:09 +02:00
iconClick(e) {
2017-01-31 07:50:28 +01:00
const target = $(e.target).closest('.icon-select__icon');
const iconId = target[0].getAttribute('data-val');
2015-11-22 09:21:12 +01:00
if (iconId === 'special') {
2017-01-31 07:50:28 +01:00
const iconData = this.special[target.data('special')];
2015-11-22 09:21:12 +01:00
if (iconData) {
2017-01-31 07:50:28 +01:00
const id = this.model.file.addCustomIcon(iconData.data);
2019-09-15 20:09:28 +02:00
this.emit('select', { id, custom: true });
2015-11-22 09:21:12 +01:00
e.preventDefault();
e.stopImmediatePropagation();
}
} else if (iconId) {
2017-01-31 07:50:28 +01:00
const isCustomIcon = target.hasClass('icon-select__icon-custom');
2019-09-15 20:09:28 +02:00
this.emit('select', { id: iconId, custom: isCustomIcon });
2015-10-17 23:49:24 +02:00
}
2019-09-15 20:09:28 +02:00
}
2015-11-21 18:04:04 +01:00
2019-08-18 10:17:09 +02:00
downloadIcon() {
2015-11-21 18:04:04 +01:00
if (this.downloadingFavicon) {
return;
}
this.downloadingFavicon = true;
this.$el.find('.icon-select__icon-download>i').addClass('fa-spinner fa-spin');
2019-08-18 08:05:38 +02:00
this.$el
.find('.icon-select__icon-download')
.removeClass('icon-select__icon--download-error');
const url = this.getIconUrl(true);
2017-01-31 07:50:28 +01:00
const img = document.createElement('img');
2015-11-22 09:21:12 +01:00
img.crossOrigin = 'Anonymous';
2015-11-21 18:04:04 +01:00
img.src = url;
2016-07-17 13:30:38 +02:00
img.onload = () => {
this.setSpecialImage(img, 'download');
this.$el.find('.icon-select__icon-download img').remove();
this.$el.find('.icon-select__icon-download>i').removeClass('fa-spinner fa-spin');
2019-08-16 23:05:39 +02:00
this.$el
.find('.icon-select__icon-download')
.addClass('icon-select__icon--custom-selected')
.append(img);
2016-07-17 13:30:38 +02:00
this.downloadingFavicon = false;
2015-11-21 18:04:04 +01:00
};
2020-06-01 16:53:51 +02:00
img.onerror = (e) => {
2015-12-12 09:53:50 +01:00
logger.error('Favicon download error: ' + url, e);
2016-07-17 13:30:38 +02:00
this.$el.find('.icon-select__icon-download>i').removeClass('fa-spinner fa-spin');
2019-08-16 23:05:39 +02:00
this.$el
.find('.icon-select__icon-download')
.removeClass('icon-select__icon--custom-selected')
.addClass('icon-select__icon--download-error');
2016-07-17 13:30:38 +02:00
this.downloadingFavicon = false;
2015-11-21 18:04:04 +01:00
};
2019-09-15 20:09:28 +02:00
}
2015-11-21 18:04:04 +01:00
2019-08-18 10:17:09 +02:00
getIconUrl(useService) {
2015-11-21 18:04:04 +01:00
if (!this.model.url) {
return null;
}
2019-08-18 08:05:38 +02:00
let url = this.model.url.replace(
/([^\/:]\/.*)?$/,
2020-06-01 16:53:51 +02:00
(match) => (match && match[0]) + '/favicon.ico'
2019-08-18 08:05:38 +02:00
);
2016-03-05 19:22:00 +01:00
if (url.indexOf('://') < 0) {
2015-11-21 18:04:04 +01:00
url = 'http://' + url;
}
2015-12-19 16:47:53 +01:00
if (useService) {
2020-03-24 07:55:28 +01:00
return (
'https://services.keeweb.info/favicon/' +
url.replace(/^.*:\/+/, '').replace(/\/.*/, '')
);
2015-11-22 11:59:13 +01:00
}
2015-11-21 18:04:04 +01:00
return url;
2019-09-15 20:09:28 +02:00
}
2015-11-21 18:04:04 +01:00
2019-08-18 10:17:09 +02:00
selectIcon() {
2015-11-21 18:04:04 +01:00
this.$el.find('.icon-select__file-input').click();
2019-09-15 20:09:28 +02:00
}
2015-11-21 18:04:04 +01:00
2019-08-18 10:17:09 +02:00
iconSelected(e) {
2017-01-31 07:50:28 +01:00
const file = e.target.files[0];
2015-11-21 18:04:04 +01:00
if (file) {
2017-01-31 07:50:28 +01:00
const reader = new FileReader();
2020-06-01 16:53:51 +02:00
reader.onload = (e) => {
2017-01-31 07:50:28 +01:00
const img = document.createElement('img');
2016-07-17 13:30:38 +02:00
img.onload = () => {
this.setSpecialImage(img, 'select');
this.$el.find('.icon-select__icon-select img').remove();
2019-08-16 23:05:39 +02:00
this.$el
.find('.icon-select__icon-select')
.addClass('icon-select__icon--custom-selected')
.append(img);
2015-11-22 09:21:12 +01:00
};
2015-11-21 18:04:04 +01:00
img.src = e.target.result;
};
reader.readAsDataURL(file);
} else {
2016-07-17 13:30:38 +02:00
this.$el.find('.icon-select__icon-select img').remove();
2019-08-18 08:05:38 +02:00
this.$el
.find('.icon-select__icon-select')
.removeClass('icon-select__icon--custom-selected');
2015-11-21 18:04:04 +01:00
}
2019-09-15 20:09:28 +02:00
}
2015-11-22 09:21:12 +01:00
2019-08-18 10:17:09 +02:00
setSpecialImage(img, name) {
2017-01-31 07:50:28 +01:00
const size = Math.min(img.width, 32);
const canvas = document.createElement('canvas');
const ctx = canvas.getContext('2d');
2015-11-22 09:21:12 +01:00
canvas.width = size;
canvas.height = size;
ctx.drawImage(img, 0, 0, size, size);
2017-01-31 07:50:28 +01:00
const data = canvas.toDataURL().replace(/^.*,/, '');
2019-08-18 10:17:09 +02:00
this.special[name] = { width: img.width, height: img.height, data };
2015-10-17 23:49:24 +02:00
}
2019-09-15 20:09:28 +02:00
}
2015-10-17 23:49:24 +02:00
2019-09-15 14:16:32 +02:00
export { IconSelectView };