mise-en-production
[auf_framonde.git] / project / cmsplugin_contact_plus / inline_ordering / static / inline_ordering.js
1 var InlineOrdering = {
2
3 /**
4 * Get list of elements that can be reordered
5 *
6 * At this point, only already existent records can be reordered (ie. where pk != '')
7 *
8 * @return Array
9 * @todo Check if given record changed, and if so, make it reorderable
10 * @todo Primary key might not be 'id' - better selector needed
11 *
12 */
13 getOrderables: function () {
14 var allInlineRows = InlineOrdering.jQuery('.inline-related'),
15 i = 0,
16 ids = [];
17
18 for (i = 0; i < allInlineRows.length; i = i + 1) {
19 if (InlineOrdering.jQuery('.inline_ordering_position input, .field-inline_ordering_position input', allInlineRows[i]).val()) {
20 ids.push('#' + allInlineRows[i].id);
21 }
22 }
23
24 // this redundant way is required, so that proper order is maintained,
25 // otherwise orderables were returned in more or less random order
26 return InlineOrdering.jQuery(ids.join(', '));
27 },
28
29 /**
30 * Inits the jQuery UI D&D
31 *
32 */
33 init: function (jQuery) {
34 InlineOrdering.jQuery = jQuery;
35 InlineOrdering.jQuery("div.inline-group").sortable({
36 axis: 'y',
37 placeholder: 'ui-state-highlight',
38 forcePlaceholderSize: 'true',
39 items: InlineOrdering.getOrderables(),
40 update: InlineOrdering.update
41 });
42 //jQuery("div.inline-group").disableSelection();
43
44 InlineOrdering.jQuery('div.field-inline_ordering_position').hide();
45 InlineOrdering.jQuery('div.inline_ordering_position').hide();
46 InlineOrdering.jQuery('td.inline_ordering_position input').hide();
47
48 InlineOrdering.jQuery('.add-row a').click(InlineOrdering.update);
49
50 InlineOrdering.getOrderables().css('cursor', 'move');
51
52 InlineOrdering.update();
53 },
54
55 jQuery: null,
56
57 /**
58 * Updates the position field
59 *
60 */
61 update: function () {
62 InlineOrdering.getOrderables().each(function (i) {
63 InlineOrdering.jQuery('input[id$=inline_ordering_position]', this).val(i + 1);
64 InlineOrdering.jQuery(this).find('h3 > span.position').remove();
65 InlineOrdering.jQuery(this).find('h3').append('<span class="position">#' + (i + 1).toFixed() + '</span>');
66 });
67 }
68
69 };
70
71 django.jQuery(function () {
72 InlineOrdering.init(django.jQuery);
73 });