| 1 |
$(function(){ |
| 2 |
|
| 3 |
var inputCss = $('#input-css') |
| 4 |
, outputCss = $('#output-css') |
| 5 |
, outputContainer = $('#output-container') |
| 6 |
, originalSize = $('#original-size') |
| 7 |
, compressedSize = $('#compressed-size') |
| 8 |
, bytesSaved = $('#bytes-saved') |
| 9 |
, compressionRatio = $('#compression-ratio') |
| 10 |
, compressBtn = $('#compress-btn') |
| 11 |
, lessConsole = $('#less-error-message') |
| 12 |
|
| 13 |
/** |
| 14 |
* Prints LESS compilation errors |
| 15 |
*/ |
| 16 |
, lessError = function(e) { |
| 17 |
var content, errorline |
| 18 |
, template = '<li><label>{line}</label><pre class="{class}">{content}</pre></li>' |
| 19 |
, error = []; |
| 20 |
|
| 21 |
content = '<h3>' + (e.type || "Syntax") + "Error: " + (e.message || 'There is an error in your .less file') + |
| 22 |
'</h3>' + '<p>'; |
| 23 |
|
| 24 |
errorline = function (e, i, classname) { |
| 25 |
if (e.extract[i] != undefined) { |
| 26 |
error.push(template.replace(/\{line\}/, (parseInt(e.line) || 0) + (i - 1)) |
| 27 |
.replace(/\{class\}/, classname) |
| 28 |
.replace(/\{content\}/, e.extract[i])); |
| 29 |
} |
| 30 |
}; |
| 31 |
|
| 32 |
if (e.stack) { |
| 33 |
content += '<br/>' + e.stack.split('\n').slice(1).join('<br/>'); |
| 34 |
} else if (e.extract) { |
| 35 |
errorline(e, 0, ''); |
| 36 |
errorline(e, 1, 'line'); |
| 37 |
errorline(e, 2, ''); |
| 38 |
content += 'on line ' + e.line + ', column ' + (e.column + 1) + ':</p>' + |
| 39 |
'<ul>' + error.join('') + '</ul>'; |
| 40 |
} |
| 41 |
|
| 42 |
lessConsole.html(content).slideDown('fast'); |
| 43 |
} |
| 44 |
|
| 45 |
/** |
| 46 |
* Compresses user's CSS with the PHP port of the YUI compressor |
| 47 |
*/ |
| 48 |
, compress = function(formData) { |
| 49 |
$.post(window.location.href, formData, function(data, textStatus, jqXHR){ |
| 50 |
// Hide LESS error console |
| 51 |
lessConsole.slideUp('fast'); |
| 52 |
|
| 53 |
// Fill output & show |
| 54 |
outputCss.val(data.css); |
| 55 |
originalSize.html(data.originalSize); |
| 56 |
compressedSize.html(data.compressedSize); |
| 57 |
bytesSaved.html(data.bytesSaved); |
| 58 |
compressionRatio.html(data.compressionRatio); |
| 59 |
|
| 60 |
outputContainer.slideDown('fast'); |
| 61 |
|
| 62 |
// Restore button state |
| 63 |
compressBtn.button('reset'); |
| 64 |
}, 'json'); |
| 65 |
}; |
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
/** |
| 70 |
* Controller |
| 71 |
*/ |
| 72 |
$('#options-form').on('submit', function(e){ |
| 73 |
e && e.preventDefault(); |
| 74 |
|
| 75 |
var data = { |
| 76 |
css: inputCss.val(), |
| 77 |
options: $(this).serialize() |
| 78 |
}; |
| 79 |
|
| 80 |
// Change button state |
| 81 |
compressBtn.button('loading'); |
| 82 |
|
| 83 |
// If LESS enabled, precompile CSS with LESS and then compress |
| 84 |
if (!!$('#enable-less:checked').val()) { |
| 85 |
try { |
| 86 |
new(less.Parser)().parse(data.css, function (err, tree) { |
| 87 |
if (err) { |
| 88 |
lessError(err); |
| 89 |
compressBtn.button('reset'); |
| 90 |
} else { |
| 91 |
data.css = tree.toCSS(); |
| 92 |
compress(data); |
| 93 |
} |
| 94 |
}); |
| 95 |
} catch (err) { |
| 96 |
lessError(err); |
| 97 |
compressBtn.button('reset'); |
| 98 |
} |
| 99 |
} else { |
| 100 |
compress(data); |
| 101 |
} |
| 102 |
}); |
| 103 |
|
| 104 |
}); |