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,
|
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');
|
|
|
|
if (iconId) {
|
|
|
|
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;
|
|
|
|
var url = this.getIconUrl();
|
|
|
|
var img = document.createElement('img');
|
|
|
|
img.src = url;
|
|
|
|
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;
|
|
|
|
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;
|