charts-framework
5 days ago
colorpicker
5 days ago
select2
5 days ago
tel
5 days ago
contextmenu.js
5 days ago
currency.js
5 days ago
index.html
5 days ago
jquery-ui.draggable.min.js
5 days ago
jquery-ui.min.js
5 days ago
jquery-ui.sortable.min.js
5 days ago
jquery.fancybox.js
5 days ago
jquery.min.js
5 days ago
sortablelist.js
5 days ago
statuscodes.js
5 days ago
toast.js
5 days ago
utils.js
5 days ago
vap-emparea.js
5 days ago
vikappointments.js
5 days ago
sortablelist.js
156 lines
| 1 | (function($) { |
| 2 | 'use strict'; |
| 3 | |
| 4 | $.fn.viksortablelist = function(data) { |
| 5 | // create default object |
| 6 | data = $.extend({ |
| 7 | customDragClass: 'vik-sortable-placeholder', |
| 8 | handleSelector: '.sortable-handler', |
| 9 | handleCursor: 'move', |
| 10 | direction: 'asc', |
| 11 | }, data); |
| 12 | |
| 13 | // define dragging handler |
| 14 | var handle = $(this).find(data.handleSelector).length ? data.handleSelector : ''; |
| 15 | |
| 16 | let items = 'tbody tr'; |
| 17 | |
| 18 | if (!$(this).is('table')) { |
| 19 | items = '.tr'; |
| 20 | } |
| 21 | |
| 22 | // avoid selection of the table rows |
| 23 | $(this).find(items).disableSelection(); |
| 24 | |
| 25 | var root = this; |
| 26 | |
| 27 | // make list sortable |
| 28 | $(this).sortable({ |
| 29 | // properties |
| 30 | axis: 'y', |
| 31 | cursor: data.handleCursor, |
| 32 | handle: handle, |
| 33 | items: items, |
| 34 | revert: false, |
| 35 | // methods |
| 36 | helper: function(e, ui) { |
| 37 | // copy columns width from table in order to |
| 38 | // display them in full-size |
| 39 | ui.children().each(function () { |
| 40 | $(this).width($(this).width()); |
| 41 | }); |
| 42 | |
| 43 | // add class to stylize rows when dragged |
| 44 | $(ui).addClass(data.customDragClass); |
| 45 | |
| 46 | return ui; |
| 47 | }, |
| 48 | start: function(e, ui) { |
| 49 | // register the current position of the element |
| 50 | root.originalElementIndex = $(root).find(items).index($(ui.item)); |
| 51 | }, |
| 52 | stop: function(e, ui) { |
| 53 | $(ui.item).removeClass(data.customDragClass); |
| 54 | |
| 55 | if (!data.inputSelector) { |
| 56 | // do not go ahead as the ordering inputs are not available |
| 57 | return; |
| 58 | } |
| 59 | |
| 60 | // get list of ordering inputs |
| 61 | var list = $(data.form).find(data.inputSelector); |
| 62 | // fetch the new position of the moved element |
| 63 | var index = $(root).find(items).index($(ui.item)); |
| 64 | |
| 65 | if (index == root.originalElementIndex) { |
| 66 | // same position, do nothing |
| 67 | return; |
| 68 | } |
| 69 | |
| 70 | var delta; |
| 71 | |
| 72 | // rearrange ordering input fields |
| 73 | if (index > root.originalElementIndex) { |
| 74 | // Fetch delta to choose whether to increase or |
| 75 | // decrease the ordering when rearranging the records. |
| 76 | // In case of ASCENDING direction, ordering have to be |
| 77 | // decreased when moved down. |
| 78 | delta = data.direction == 'asc' ? 1 : -1; |
| 79 | |
| 80 | // get ordering to use |
| 81 | var ordering = parseInt($(list[index - 1]).val()); |
| 82 | |
| 83 | $(list[index]).val(ordering); |
| 84 | |
| 85 | // the element was moved down, we should switch the ordering |
| 86 | // with all the previous elements |
| 87 | for (var i = index - 1; i >= root.originalElementIndex; i--) { |
| 88 | if ($(list[i]).val() == ordering) { |
| 89 | // Decrease ordering by one as long as the elements are contiguous. |
| 90 | // In case of DESCENDING ordering, the amount will be increased instead. |
| 91 | ordering -= delta; |
| 92 | |
| 93 | $(list[i]).val(ordering); |
| 94 | } else { |
| 95 | // decrease the value as there is one or |
| 96 | // more breaks between the elements |
| 97 | $(list[i]).val(parseInt($(list[i]).val()) - delta); |
| 98 | } |
| 99 | } |
| 100 | } else { |
| 101 | // Fetch delta to choose whether to increase or |
| 102 | // decrease the ordering when rearranging the records. |
| 103 | // In case of ASCENDING direction, ordering have to be |
| 104 | // increased when moved up. |
| 105 | delta = data.direction == 'asc' ? -1 : 1; |
| 106 | |
| 107 | // get ordering to use |
| 108 | var ordering = parseInt($(list[index + 1]).val()); |
| 109 | $(list[index]).val(ordering); |
| 110 | |
| 111 | // the element was moved up, we should switch the ordering |
| 112 | // with all the next elements |
| 113 | for (var i = index + 1; i <= root.originalElementIndex; i++) { |
| 114 | if ($(list[i]).val() == ordering) { |
| 115 | // Increase ordering by one as long as the elements are contiguous. |
| 116 | // In case of DESCENDING ordering, the amount will be decreased instead. |
| 117 | ordering -= delta; |
| 118 | |
| 119 | $(list[i]).val(ordering); |
| 120 | } else { |
| 121 | // increase the value as there is one or |
| 122 | // more breaks between the elements |
| 123 | $(list[i]).val(parseInt($(list[i]).val()) - delta); |
| 124 | } |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | if (!data.saveUrl) { |
| 129 | // do not go ahead as there is no end-point to save the ordering |
| 130 | return; |
| 131 | } |
| 132 | |
| 133 | // fetch ID => ordering map |
| 134 | var order = {}; |
| 135 | |
| 136 | $(root).find(items).each(function() { |
| 137 | var id = $(this).find('input[name="cid[]"]').val(); |
| 138 | var value = $(this).find('input[name="order[]"]').val(); |
| 139 | |
| 140 | order[id] = value; |
| 141 | }); |
| 142 | |
| 143 | // create post data |
| 144 | var post = { |
| 145 | order: order, |
| 146 | // associative array of filters |
| 147 | filters: data.filters, |
| 148 | }; |
| 149 | |
| 150 | // make request to save ordering |
| 151 | UIAjax.do(data.saveUrl, post); |
| 152 | }, |
| 153 | }); |
| 154 | } |
| 155 | })(jQuery); |
| 156 |