Skip to content

Commit 111109e

Browse files
committed
refactor: use jQuery from django instead of global jQuery
1 parent 8781701 commit 111109e

File tree

4 files changed

+35
-34
lines changed

4 files changed

+35
-34
lines changed

adminsortable2/static/adminsortable2/js/inline-sortable.js

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// make tabular- and stacked inlines sortable
2-
jQuery(function($) {
3-
$('div.inline-group.sortable').each(function() {
2+
django.jQuery(function ($) {
3+
$('div.inline-group.sortable').each(function () {
44
var $order_div = $(this).nextUntil('div.default_order_field').next()
55
var default_order_field = $order_div.attr('default_order_field');
66
var default_order_direction = $order_div.attr('default_order_direction');
@@ -14,16 +14,16 @@ jQuery(function($) {
1414
scroll: true,
1515
cursor: 'ns-resize',
1616
containment: $(this).find('tbody'),
17-
stop: function(event, dragged_rows) {
17+
stop: function (event, dragged_rows) {
1818
var $result_list = $(this);
19-
$result_list.find('tbody tr').each(function(index) {
19+
$result_list.find('tbody tr').each(function (index) {
2020
$(this).removeClass('row1 row2').addClass(index % 2 ? 'row2' : 'row1');
2121
});
2222
var originals = $result_list.find('tbody tr.has_original').get()
23-
if(default_order_direction === '-1') {
23+
if (default_order_direction === '-1') {
2424
originals.reverse();
25-
}
26-
$(originals).each(function(index) {
25+
}
26+
$(originals).each(function (index) {
2727
$(this).find(order_input_field).val(index + 1);
2828
});
2929
}
@@ -38,16 +38,17 @@ jQuery(function($) {
3838
scroll: true,
3939
cursor: 'ns-resize',
4040
containment: $(this),
41-
stop: function(event, dragged_rows) {
41+
stop: function (event, dragged_rows) {
4242
var $result_list = $(this);
4343
var originals = $result_list.find('div.inline-related.has_original').get()
44-
if(default_order_direction === '-1') {
44+
if (default_order_direction === '-1') {
4545
originals.reverse();
46-
}
47-
$(originals).each(function(index) {
46+
}
47+
$(originals).each(function (index) {
4848
$(this).find(order_input_field).val(index + 1);
4949
});
5050
}
5151
});
5252
});
53-
});
53+
})(django.jQuery)
54+
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
django.jQuery(function ($) {
2-
$("script.inline-stacked-config").each(function(i, config_element) {
2+
$("script.inline-stacked-config").each(function (i, config_element) {
33
try {
44
var config = JSON.parse(config_element.textContent);
5-
$("#"+ config["prefix"] + "-group .inline-related").stackedFormset(config);
5+
$("#" + config["prefix"] + "-group .inline-related").stackedFormset(config);
66
}
77
catch (parse_error) {
88
console.error("Configuration for a django-adminsortable2 inline-stacked form is invalid.");
99
}
1010
});
11-
});
11+
})(django.jQuery);
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
django.jQuery(function ($) {
2-
$("script.inline-tabular-config").each(function(i, config_element) {
2+
$("script.inline-tabular-config").each(function (i, config_element) {
33
try {
44
var config = JSON.parse(config_element.textContent);
5-
$("#"+ config["prefix"] + "-group .tabular.inline-related tbody tr").tabularFormset(config);
5+
$("#" + config["prefix"] + "-group .tabular.inline-related tbody tr").tabularFormset(config);
66
}
77
catch (parse_error) {
88
console.error("Configuration for a django-adminsortable2 inline-tabular form is invalid.");
99
}
1010
});
11-
});
11+
})(django.jQuery);

adminsortable2/static/adminsortable2/js/list-sortable.js

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
"use strict";
22

3-
// make list view sortable
4-
jQuery(function($) {
3+
django.jQuery(function ($) {
4+
// make list view sortable
55
var startindex, startorder, endindex, endorder;
66
var csrfvalue = $('form').find('input[name="csrfmiddlewaretoken"]').val();
7-
var getQueryParams = function() {
7+
var getQueryParams = function () {
88
var vars = [], hash, i;
99
var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
1010
for (i = 0; i < hashes.length; i++) {
@@ -35,19 +35,19 @@ jQuery(function($) {
3535
cursor: 'ns-resize',
3636
containment: $('#result_list tbody'),
3737
tolerance: 'pointer',
38-
start: function(event, dragged_rows) {
39-
$(this).find('thead tr th').each(function(index) {
38+
start: function (event, dragged_rows) {
39+
$(this).find('thead tr th').each(function (index) {
4040
$(dragged_rows.item.context.childNodes[index]).width($(this).width() - 10);
4141
});
4242
startindex = dragged_rows.item.index();
4343
},
44-
stop: function(event, dragged_rows) {
45-
$(this).find('thead tr th').each(function(index) {
44+
stop: function (event, dragged_rows) {
45+
$(this).find('thead tr th').each(function (index) {
4646
$(dragged_rows.item.context.childNodes[index]).width('auto');
4747
});
48-
48+
4949
var $result_list = $(this);
50-
$result_list.find('tbody tr').each(function(index) {
50+
$result_list.find('tbody tr').each(function (index) {
5151
$(this).removeClass('row1 row2').addClass(index % 2 ? 'row2' : 'row1');
5252
});
5353
endindex = dragged_rows.item.index();
@@ -72,24 +72,24 @@ jQuery(function($) {
7272
endorder: endorder,
7373
csrfmiddlewaretoken: csrfvalue
7474
},
75-
success: function(moved_items) {
76-
$.each(moved_items, function(index, item) {
77-
$result_list.find('tbody tr .js-reorder-' + item.pk).parents('tr').each(function() {
75+
success: function (moved_items) {
76+
$.each(moved_items, function (index, item) {
77+
$result_list.find('tbody tr .js-reorder-' + item.pk).parents('tr').each(function () {
7878
$(this).find('div.drag').attr('order', item.order);
7979
});
8080
});
8181
},
82-
error: function(response) {
82+
error: function (response) {
8383
console.error('The server responded: ' + response.responseText);
8484
}
8585
});
8686
}
8787
});
8888
$('#result_list, tbody, tr, td, th').disableSelection();
89-
});
89+
})(django.jQuery)
9090

9191
// Show and hide the step input field
92-
jQuery(function($) {
92+
django.jQuery(function ($) {
9393
var $step_field = $('#changelist-form-step');
9494
var $page_field = $('#changelist-form-page');
9595

@@ -139,4 +139,4 @@ jQuery(function($) {
139139
}
140140
var $form = $('#changelist-form') || $grp_form;
141141
$form.find('select[name="action"]').change(display_fields);
142-
});
142+
})(django.jQuery);

0 commit comments

Comments
 (0)