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

129 lines
4.7 KiB
JavaScript
Raw Normal View History

2015-10-17 23:49:24 +02:00
'use strict';
var Backbone = require('backbone'),
2015-11-22 11:59:13 +01:00
IconMap = require('../const/icon-map'),
Launcher = require('../comp/launcher');
2015-10-17 23:49:24 +02:00
2015-10-31 20:09:32 +01:00
var IconSelectView = Backbone.View.extend({
template: require('templates/icon-select.html'),
2015-10-17 23:49:24 +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'
2015-10-17 23:49:24 +02:00
},
2015-11-22 09:21:12 +01:00
initialize: function() {
this.special = {
select: null,
download: null
};
},
2015-10-17 23:49:24 +02:00
render: function() {
this.renderTemplate({
sel: this.model.iconId,
2015-11-21 18:04:04 +01:00
icons: IconMap,
2015-11-21 23:15:51 +01:00
canDownloadFavicon: !!this.model.url,
customIcons: this.model.file.getCustomIcons()
2015-10-17 23:49:24 +02:00
}, true);
return this;
},
iconClick: function(e) {
2015-11-21 23:15:51 +01:00
var target = $(e.target).closest('.icon-select__icon');
var iconId = target[0].getAttribute('data-val');
2015-11-22 09:21:12 +01:00
if (iconId === 'special') {
var iconData = this.special[target.data('special')];
if (iconData) {
var id = this.model.file.addCustomIcon(iconData.data);
this.trigger('select', { id: id, custom: true });
e.preventDefault();
e.stopImmediatePropagation();
}
} else if (iconId) {
2015-11-21 23:15:51 +01:00
var isCustomIcon = target.hasClass('icon-select__icon-custom');
this.trigger('select', { id: iconId, custom: isCustomIcon });
2015-10-17 23:49:24 +02:00
}
2015-11-21 18:04:04 +01:00
},
downloadIcon: function() {
if (this.downloadingFavicon) {
return;
}
this.downloadingFavicon = true;
this.$el.find('.icon-select__icon-download>i').addClass('fa-spinner fa-spin');
var that = this;
2015-11-22 11:59:13 +01:00
var url = this.getIconUrl(!Launcher); // inside launcher we can load images without CORS
2015-11-21 18:04:04 +01:00
var 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;
2015-11-22 11:59:13 +01:00
img.onload = function () {
2015-11-22 09:21:12 +01:00
that.setSpecialImage(img, 'download');
2015-11-21 18:04:04 +01:00
that.$el.find('.icon-select__icon-download img').remove();
that.$el.find('.icon-select__icon-download>i').removeClass('fa-spinner fa-spin');
that.$el.find('.icon-select__icon-download').addClass('icon-select__icon--custom-selected').append(img);
that.downloadingFavicon = false;
};
2015-11-22 11:59:13 +01:00
img.onerror = function (e) {
2015-11-21 18:04:04 +01:00
console.error('Favicon download error: ' + url, e);
that.$el.find('.icon-select__icon-download>i').removeClass('fa-spinner fa-spin');
that.$el.find('.icon-select__icon-download').removeClass('icon-select__icon--custom-selected');
that.downloadingFavicon = false;
};
},
2015-11-22 11:59:13 +01:00
getIconUrl: function(useGoogle) {
2015-11-21 18:04:04 +01:00
if (!this.model.url) {
return null;
}
var url = this.model.url.replace(/([^\/:]\/.*)?$/, function(match) { return (match && match[0]) + '/favicon.ico'; });
if (url.indexOf('://') < 0) {
url = 'http://' + url;
}
2015-11-22 11:59:13 +01:00
if (useGoogle) {
return 'http://www.google.com/s2/favicons?domain_url=' + encodeURIComponent(url.replace('/favicon.ico', '/'));
}
2015-11-21 18:04:04 +01:00
return url;
},
selectIcon: function() {
this.$el.find('.icon-select__file-input').click();
},
iconSelected: function(e) {
var that = this;
var file = e.target.files[0];
if (file) {
var reader = new FileReader();
reader.onload = function(e) {
var img = document.createElement('img');
2015-11-22 09:21:12 +01:00
img.onload = function() {
that.setSpecialImage(img, 'select');
that.$el.find('.icon-select__icon-select img').remove();
that.$el.find('.icon-select__icon-select').addClass('icon-select__icon--custom-selected').append(img);
};
2015-11-21 18:04:04 +01:00
img.src = e.target.result;
};
reader.readAsDataURL(file);
} else {
that.$el.find('.icon-select__icon-select img').remove();
that.$el.find('.icon-select__icon-select').removeClass('icon-select__icon--custom-selected');
}
2015-11-22 09:21:12 +01:00
},
setSpecialImage: function(img, name) {
var size = Math.min(img.width, 32);
var canvas = document.createElement('canvas');
var ctx = canvas.getContext('2d');
canvas.width = size;
canvas.height = size;
ctx.drawImage(img, 0, 0, size, size);
var data = canvas.toDataURL().replace(/^.*,/, '');
this.special[name] = { width: img.width, height: img.height, data: data };
2015-10-17 23:49:24 +02:00
}
});
2015-10-31 20:09:32 +01:00
module.exports = IconSelectView;