forked from matthiasmullie/minify
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathminifier.js
More file actions
82 lines (72 loc) · 2.38 KB
/
Copy pathminifier.js
File metadata and controls
82 lines (72 loc) · 2.38 KB
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
$(function() {
var selectType = function() {
var content = $(this).val(),
extension = content.split('.').pop();
// check if we have a valid extension (.css of .js), in which case the
// content of the textarea is likely a link to a file, instead of code
if (['js', 'css'].indexOf(extension) === -1) {
// unknown - try to guess from content
content = content.replace(/\/\*.*?\*\//, '');
if (content.match(/(^|\s|;)(function|var|for|do|while|if|else|new|switch|return)(\s|;|\{|\})/)) {
extension = 'js';
} else {
extension = 'css';
}
}
$('#hero input[name=type]').prop('checked', false);
$('#type_' + extension)
.prop('checked', true)
.trigger('change');
},
checkType = function(e) {
e.preventDefault();
var extension = $('#hero input[name=type]:checked').val();
if (extension) {
minify();
} else {
// Show error message when type is not selected
$('#types').addClass('error');
showError('Please select the type (CSS or JavaScript) of your script!');
}
},
minify = function() {
var $form = $('#hero form'),
$submitButton = $('#hero input[type=submit]'),
$spinner = $('<button class="learn-more-btn"><i class="fa fa-spinner fa-spin"></i></button>');
$submitButton.replaceWith($spinner);
$.ajax({
url: $form.attr('action'),
type: $form.attr('method'),
data: $form.serialize()
}).done(function(data, textStatus, jqXHR) {
// replace textarea content with minified script
$('#source').val(data.minified);
// display minifier gains
showSuccess(
'Original script: ' + data.sizes.original + 'b, ' +
'minified script: ' + data.sizes.minified + 'b. ' +
'Gain: ' + (data.sizes.original - data.sizes.minified) + 'b.'
);
$spinner.replaceWith($submitButton);
}).fail(function() {
showError('Something, somewhere, somehow failed! Did you post a link to an unreachable script?');
$spinner.replaceWith($submitButton);
});
},
showError = function(message) {
$('#error').text(message);
},
showSuccess = function(message) {
$('#success').text(message);
},
discardErrorSuccess = function() {
$('#types').removeClass('error');
showError('');
showSuccess('');
};
$('#source').on('change, keyup', selectType);
$('#hero form').on('submit', checkType);
$('#hero input[name=type]').on('change', discardErrorSuccess);
$('#hero #source').on('keyup', discardErrorSuccess);
$('#source').autoExpand();
});