|
| 1 | +/* |
| 2 | + jQuery UI Sortable plugin wrapper |
| 3 | +
|
| 4 | + @param [ui-sortable] {object} Options to pass to $.fn.sortable() merged onto ui.config |
| 5 | +*/ |
| 6 | +angular.module('ui.sortable', []).value('uiSortableConfig',{}).directive('uiSortable', [ |
| 7 | + 'uiSortableConfig', function(uiSortableConfig) { |
| 8 | + return { |
| 9 | + require: '?ngModel', |
| 10 | + link: function(scope, element, attrs, ngModel) { |
| 11 | + var onReceive, onRemove, onStart, onUpdate, opts; |
| 12 | + |
| 13 | + opts = angular.extend({}, uiSortableConfig, scope.$eval(attrs.uiSortable)); |
| 14 | + |
| 15 | + if (ngModel) { |
| 16 | + |
| 17 | + ngModel.$render = function() { |
| 18 | + element.sortable( "refresh" ); |
| 19 | + }; |
| 20 | + |
| 21 | + onStart = function(e, ui) { |
| 22 | + // Save position of dragged item |
| 23 | + ui.item.sortable = { index: ui.item.index() }; |
| 24 | + }; |
| 25 | + |
| 26 | + onUpdate = function(e, ui) { |
| 27 | + // For some reason the reference to ngModel in stop() is wrong |
| 28 | + ui.item.sortable.resort = ngModel; |
| 29 | + }; |
| 30 | + |
| 31 | + onReceive = function(e, ui) { |
| 32 | + ui.item.sortable.relocate = true; |
| 33 | + // added item to array into correct position and set up flag |
| 34 | + ngModel.$modelValue.splice(ui.item.index(), 0, ui.item.sortable.moved); |
| 35 | + }; |
| 36 | + |
| 37 | + onRemove = function(e, ui) { |
| 38 | + // copy data into item |
| 39 | + if (ngModel.$modelValue.length === 1) { |
| 40 | + ui.item.sortable.moved = ngModel.$modelValue.splice(0, 1)[0]; |
| 41 | + } else { |
| 42 | + ui.item.sortable.moved = ngModel.$modelValue.splice(ui.item.sortable.index, 1)[0]; |
| 43 | + } |
| 44 | + }; |
| 45 | + |
| 46 | + onStop = function(e, ui) { |
| 47 | + // digest all prepared changes |
| 48 | + if (ui.item.sortable.resort && !ui.item.sortable.relocate) { |
| 49 | + |
| 50 | + // Fetch saved and current position of dropped element |
| 51 | + var end, start; |
| 52 | + start = ui.item.sortable.index; |
| 53 | + end = ui.item.index(); |
| 54 | + |
| 55 | + // Reorder array and apply change to scope |
| 56 | + ui.item.sortable.resort.$modelValue.splice(end, 0, ui.item.sortable.resort.$modelValue.splice(start, 1)[0]); |
| 57 | + |
| 58 | + } |
| 59 | + if (ui.item.sortable.resort || ui.item.sortable.relocate) { |
| 60 | + scope.$apply(); |
| 61 | + } |
| 62 | + }; |
| 63 | + |
| 64 | + // If user provided 'start' callback compose it with onStart function |
| 65 | + opts.start = (function(_start){ |
| 66 | + return function(e, ui) { |
| 67 | + onStart(e, ui); |
| 68 | + if (typeof _start === "function") |
| 69 | + _start(e, ui); |
| 70 | + } |
| 71 | + })(opts.start); |
| 72 | + |
| 73 | + // If user provided 'start' callback compose it with onStart function |
| 74 | + opts.stop = (function(_stop){ |
| 75 | + return function(e, ui) { |
| 76 | + onStop(e, ui); |
| 77 | + if (typeof _stop === "function") |
| 78 | + _stop(e, ui); |
| 79 | + } |
| 80 | + })(opts.stop); |
| 81 | + |
| 82 | + // If user provided 'update' callback compose it with onUpdate function |
| 83 | + opts.update = (function(_update){ |
| 84 | + return function(e, ui) { |
| 85 | + onUpdate(e, ui); |
| 86 | + if (typeof _update === "function") |
| 87 | + _update(e, ui); |
| 88 | + } |
| 89 | + })(opts.update); |
| 90 | + |
| 91 | + // If user provided 'receive' callback compose it with onReceive function |
| 92 | + opts.receive = (function(_receive){ |
| 93 | + return function(e, ui) { |
| 94 | + onReceive(e, ui); |
| 95 | + if (typeof _receive === "function") |
| 96 | + _receive(e, ui); |
| 97 | + } |
| 98 | + })(opts.receive); |
| 99 | + |
| 100 | + // If user provided 'remove' callback compose it with onRemove function |
| 101 | + opts.remove = (function(_remove){ |
| 102 | + return function(e, ui) { |
| 103 | + onRemove(e, ui); |
| 104 | + if (typeof _remove === "function") |
| 105 | + _remove(e, ui); |
| 106 | + }; |
| 107 | + })(opts.remove); |
| 108 | + } |
| 109 | + |
| 110 | + // Create sortable |
| 111 | + element.sortable(opts); |
| 112 | + } |
| 113 | + }; |
| 114 | + } |
| 115 | +]); |
0 commit comments