mirror of
https://github.com/DarrenOfficial/dpaste.git
synced 2024-11-15 16:12:51 +11:00
Update and enhance javascript code
* Use shorthand notation for `$(document).ready`. This isn't
just shorter but also slightly faster as it uses a cached
instance or $(document) instead of re-instantiating jQuery.
* Use .prop('checked') instead of .attr('checked').
Form fields reflect live state in properties, attributes are
only used when parsing the HTML to set the initial values of
the DOM properties. In the past (upto jQuery 1.6) jQuery
would cover you by secretly calling .prop() for common mistakes
but this has been deprecated and removed.
* Use .toggleClass instead of `if hasClass/addClass/removeClass`.
* Cancel previous AJAX request before starting another one.
This fixes the bug where using the "Guess lexer" or "Diff" button
twice in a row can sometimes result in the second push being
ignored. Since the requests are asynchronous, one can arrive
before or after the other and the callback called in a different
order. It also saves a bit on the network I guess.
* DiffAjax: Moved location.hash assignment to before the request.
It is asynchronous anyway, so the assignment happened before
the request already. But now it is more obvious that this is
the case.
* Simplified various selectors to just an ID instead of element
with ID. IDs are unique. If you have more than one of them
with a different element, there's something wrong. I suppose
in a way it also saves potential maintenance in the future if
a minor detail changes (e.g. from <div> to <p>).
* Removed weird variable conflict that was incorrectly
copy-pasted in 1f863b345f
.
`var a;` when `a` already exists does nothing. It doesn't clear
or reset it. It just weird syntax that should throw an error
but is instead silently ignored. And `curLine` is already set
and continues to be there.
This commit is contained in:
parent
b6ac14cd1a
commit
fee4bd1354
2 changed files with 48 additions and 37 deletions
|
@ -31,19 +31,25 @@
|
||||||
<script src="//ajax.googleapis.com/ajax/libs/jquery/1/jquery.js"></script>
|
<script src="//ajax.googleapis.com/ajax/libs/jquery/1/jquery.js"></script>
|
||||||
<script src="{% static "dpaste/bootstrap/js/bootstrap.min.js" %}"></script>
|
<script src="{% static "dpaste/bootstrap/js/bootstrap.min.js" %}"></script>
|
||||||
<script>
|
<script>
|
||||||
jQuery(document).ready(function($) {
|
jQuery(function($) {
|
||||||
|
var lexerReq;
|
||||||
$('#guess_lexer_btn').click(function() {
|
$('#guess_lexer_btn').click(function() {
|
||||||
$.getJSON('{% url "snippet_guess_lexer" %}',
|
// Cancel previous request if it is still pending
|
||||||
{
|
if (lexerReq) {
|
||||||
|
lexerReq.abort();
|
||||||
|
}
|
||||||
|
|
||||||
|
lexerReq = $.getJSON('{% url "snippet_guess_lexer" %}', {
|
||||||
codestring: $('#id_content').val()
|
codestring: $('#id_content').val()
|
||||||
},
|
}).done(function(data) {
|
||||||
function(data) {
|
|
||||||
if (data.lexer === 'unknown') {
|
if (data.lexer === 'unknown') {
|
||||||
$('#guess_lexer_btn').css('color', 'red');
|
$('#guess_lexer_btn').css('color', 'red');
|
||||||
} else {
|
} else {
|
||||||
$('#id_lexer').val(data.lexer);
|
$('#id_lexer').val(data.lexer);
|
||||||
$('#guess_lexer_btn').css('color', 'inherit');
|
$('#guess_lexer_btn').css('color', 'inherit');
|
||||||
}
|
}
|
||||||
|
}).complete(function() {
|
||||||
|
lexerReq = null;
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
|
@ -78,7 +78,9 @@
|
||||||
{% block script_footer %}
|
{% block script_footer %}
|
||||||
{{ block.super }}
|
{{ block.super }}
|
||||||
<script>
|
<script>
|
||||||
jQuery(document).ready(function($){
|
jQuery(function($) {
|
||||||
|
var diffReq;
|
||||||
|
|
||||||
$('.snippet-reply-hidden').click(function(e) {
|
$('.snippet-reply-hidden').click(function(e) {
|
||||||
$(this).removeClass('snippet-reply-hidden');
|
$(this).removeClass('snippet-reply-hidden');
|
||||||
});
|
});
|
||||||
|
@ -93,17 +95,26 @@ jQuery(document).ready(function($){
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
$('form#diffform').submit(function() {
|
$('#diffform').submit(function() {
|
||||||
var a = $('input[name="a"]:checked').val(),
|
var a = $('input[name="a"]:checked').val(),
|
||||||
b = $('input[name="b"]:checked').val(),
|
b = $('input[name="b"]:checked').val();
|
||||||
hash = 'D' + a + ',' + b;
|
|
||||||
$.get("{% url "snippet_diff" %}", {
|
window.location.hash = 'D' + a + ',' + b;
|
||||||
|
|
||||||
|
// Cancel previous request if it is still pending
|
||||||
|
if (diffReq) {
|
||||||
|
diffReq.abort();
|
||||||
|
}
|
||||||
|
|
||||||
|
diffReq = $.get("{% url "snippet_diff" %}", {
|
||||||
a: a,
|
a: a,
|
||||||
b: b
|
b: b
|
||||||
}, function(data){
|
}).done(function(data) {
|
||||||
$('#diff').html(data).slideDown('fast');
|
$('#diff').html(data).slideDown('fast');
|
||||||
|
}).complete(function() {
|
||||||
|
diffReq = null;
|
||||||
});
|
});
|
||||||
window.location.hash = hash;
|
|
||||||
return false;
|
return false;
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -112,10 +123,10 @@ jQuery(document).ready(function($){
|
||||||
|
|
||||||
if (curLine.substring(0, 2) === '#D') {
|
if (curLine.substring(0, 2) === '#D') {
|
||||||
hashlist = curLine.substring(2).split(',');
|
hashlist = curLine.substring(2).split(',');
|
||||||
if (hashlist.length == 2) {
|
if (hashlist.length === 2) {
|
||||||
console.log(hashlist);
|
console.log(hashlist);
|
||||||
$('form#diffform input[name="a"][value="'+hashlist[0]+'"]').attr('checked', true);
|
$('#diffform input[name="a"][value="' + hashlist[0] + '"]').prop('checked', true);
|
||||||
$('form#diffform input[name="b"][value="'+hashlist[1]+'"]').attr('checked', true);
|
$('#diffform input[name="b"][value="' + hashlist[1] + '"]').prop('checked', true);
|
||||||
$('#snippet-diff').slideDown('fast');
|
$('#snippet-diff').slideDown('fast');
|
||||||
$('#snippet-diff form').submit();
|
$('#snippet-diff form').submit();
|
||||||
}
|
}
|
||||||
|
@ -124,10 +135,8 @@ jQuery(document).ready(function($){
|
||||||
/**
|
/**
|
||||||
* Line Highlighting
|
* Line Highlighting
|
||||||
*/
|
*/
|
||||||
var curLine = document.location.hash,
|
|
||||||
hashlist;
|
|
||||||
|
|
||||||
if (curLine.substring(0, 2) == '#L'){
|
if (curLine.substring(0, 2) === '#L') {
|
||||||
hashlist = curLine.substring(2).split(',');
|
hashlist = curLine.substring(2).split(',');
|
||||||
if (hashlist.length > 0 && hashlist[0] !== '') {
|
if (hashlist.length > 0 && hashlist[0] !== '') {
|
||||||
$.each(hashlist, function(index, elem){
|
$.each(hashlist, function(index, elem){
|
||||||
|
@ -140,11 +149,7 @@ jQuery(document).ready(function($){
|
||||||
var line = $(this),
|
var line = $(this),
|
||||||
hash = 'L';
|
hash = 'L';
|
||||||
|
|
||||||
if (line.hasClass('marked')) {
|
line.toggleClass('marked');
|
||||||
line.removeClass('marked');
|
|
||||||
} else {
|
|
||||||
line.addClass('marked');
|
|
||||||
}
|
|
||||||
|
|
||||||
$('.linenums li.marked').each(function (index, elem) {
|
$('.linenums li.marked').each(function (index, elem) {
|
||||||
if (hash !== 'L') hash += ',';
|
if (hash !== 'L') hash += ',';
|
||||||
|
|
Loading…
Reference in a new issue