-
Notifications
You must be signed in to change notification settings - Fork 2
/
app.js
153 lines (125 loc) · 4.25 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
var app = {
downloads: [],
api: function(endpoint) {
endpoint = endpoint || '';
endpoint = endpoint.replace('https://api.iconfinder.com/v4/', '');
return 'https://iconfinder-api-auth.herokuapp.com/v4/' + endpoint;
},
consoleLog: function(request, response) {
var template = $('#log-template').html();
var compile = _.template(template);
var data = {
type: request.type,
url: request.url,
response: JSON.stringify(response, null, 2)
};
$('#console .log').html(compile(data));
},
indicateLoading: function(state) {
$('#search button i').toggleClass('fa-circle-o-notch fa-spin', state);
$('#search input[type="text"]').attr('disabled', state);
},
search: function(e) {
e.preventDefault();
if( $('#search #query').val().trim().toString().length > 0 ) {
var query = $('#search').serialize();
app.indicateLoading(true);
$.getJSON(app.api('icons/search?' + query), function(result) {
app.renderResults(result);
app.indicateLoading(false);
app.consoleLog(this, result);
});
}
$('.results').hide().empty();
},
renderResults: function(data) {
var html = '';
var template = $('#result-template').html();
var compile = _.template(template);
if( data.total_count > 0 ) {
$.each(data.icons, function(index, icon) {
var file = _.last(icon.raster_sizes);
html += compile({
url: file.formats[0].preview_url,
icon_id: icon.icon_id
});
});
$('.results').html(html).fadeIn();
this.makeDraggable();
}
},
toggleResults: function() {
var input = $(this).val().trim();
var results = $('.results').children();
if( input.length > 0 && results.length > 0 ) {
$('.results').fadeIn();
}
},
makeDraggable: function() {
$('.results').find('img').draggable({
appendTo: 'main',
containment: 'main',
addClasses: false,
scroll: false,
revert: 'invalid',
cursor: 'move',
revertDuration: 200,
helper: 'clone',
start: function() {
$('.results').fadeOut();
}
});
},
makeDroppable: function() {
$('.diagram .placeholder').droppable({
accept: '.results img',
hoverClass: 'active',
drop: function(event, ui) {
var holder = $(this);
var preview = $(ui.draggable).clone().removeClass('ui-draggable-handle');
var iconId = $(ui.draggable).data('icon-id');
holder.addClass('filled').html(preview);
app.getIcon(iconId, holder);
}
});
},
getIcon: function(id, holder) {
$.getJSON(app.api('icons/' + id), function(result) {
var url = result.vector_sizes[0].formats[0].download_url;
app.download(url, holder);
app.downloads.push(id);
app.downloads = _.uniq(app.downloads);
$('#downloads strong').html(app.downloads.length);
app.consoleLog(this, result);
});
},
download: function(url, holder) {
$.ajax({
url: app.api(url),
type: 'GET',
success: function(data) {
var svg = $(data).find('svg').prop('outerHTML');
holder.find('img').attr('src', 'data:image/svg+xml;utf8,' + encodeURIComponent(svg));
}
});
},
bindEvents: function() {
$('#search').on('submit', app.search);
$('#search input').on('focus', app.toggleResults);
$('#search').on('change', 'input[type="checkbox"]', app.search);
$('.toggle-console').on('click', function() {
$('aside').toggleClass('active');
});
},
init: function() {
this.makeDroppable();
this.bindEvents();
}
};
_.templateSettings = {
interpolate: /\{\{(.+?)\}\}/g,
escape: /\{\{-(.*?)\}\}/g
};
$(function() {
app.init();
});