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

95 lines
3.2 KiB
JavaScript
Raw Normal View History

2015-10-17 23:49:24 +02:00
'use strict';
var Backbone = require('backbone'),
2015-10-31 20:09:32 +01:00
IconMap = require('../const/icon-map');
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
},
render: function() {
this.renderTemplate({
sel: this.model.iconId,
2015-11-21 18:04:04 +01:00
icons: IconMap,
canDownloadFavicon: !!this.model.url
2015-10-17 23:49:24 +02:00
}, true);
return this;
},
iconClick: function(e) {
var iconId = +$(e.target).data('val');
if (typeof iconId === 'number' && !isNaN(iconId)) {
this.trigger('select', iconId);
}
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;
var url = this.getIconUrl();
var img = document.createElement('img');
img.src = url;
img.width = 16;
img.height = 16;
img.onload = function() {
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;
};
img.onerror = function(e) {
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;
};
},
getIconUrl: function() {
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;
}
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');
img.src = e.target.result;
img.width = 16;
img.height = 16;
that.$el.find('.icon-select__icon-select img').remove();
that.$el.find('.icon-select__icon-select').addClass('icon-select__icon--custom-selected').append(img);
};
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-10-17 23:49:24 +02:00
}
});
2015-10-31 20:09:32 +01:00
module.exports = IconSelectView;