| 1 |
/** |
| 2 |
* Plugin: "drag_drop" (selectize.js) |
| 3 |
* Copyright (c) 2013 Brian Reavis & contributors |
| 4 |
* Copyright (c) 2020-2022 Selectize Team & contributors* |
| 5 |
* |
| 6 |
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this |
| 7 |
* file except in compliance with the License. You may obtain a copy of the License at: |
| 8 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 9 |
* |
| 10 |
* Unless required by applicable law or agreed to in writing, software distributed under |
| 11 |
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF |
| 12 |
* ANY KIND, either express or implied. See the License for the specific language |
| 13 |
* governing permissions and limitations under the License. |
| 14 |
* |
| 15 |
* @author Brian Reavis <brian@thirdroute.com> |
| 16 |
*/ |
| 17 |
|
| 18 |
Selectize.define('drag_drop', function(options) { |
| 19 |
if (!$.fn.sortable) throw new Error('The "drag_drop" plugin requires jQuery UI "sortable".'); |
| 20 |
if (this.settings.mode !== 'multi') return; |
| 21 |
var self = this; |
| 22 |
|
| 23 |
self.lock = (function() { |
| 24 |
var original = self.lock; |
| 25 |
return function() { |
| 26 |
var sortable = self.$control.data('sortable'); |
| 27 |
if (sortable) sortable.disable(); |
| 28 |
return original.apply(self, arguments); |
| 29 |
}; |
| 30 |
})(); |
| 31 |
|
| 32 |
self.unlock = (function() { |
| 33 |
var original = self.unlock; |
| 34 |
return function() { |
| 35 |
var sortable = self.$control.data('sortable'); |
| 36 |
if (sortable) sortable.enable(); |
| 37 |
return original.apply(self, arguments); |
| 38 |
}; |
| 39 |
})(); |
| 40 |
|
| 41 |
self.setup = (function() { |
| 42 |
var original = self.setup; |
| 43 |
return function() { |
| 44 |
original.apply(this, arguments); |
| 45 |
|
| 46 |
var $control = self.$control.sortable({ |
| 47 |
items: '[data-value]', |
| 48 |
forcePlaceholderSize: true, |
| 49 |
disabled: self.isLocked, |
| 50 |
start: function(e, ui) { |
| 51 |
ui.placeholder.css('width', ui.helper.css('width')); |
| 52 |
// $control.css({overflow: 'visible'}); |
| 53 |
$control.addClass('dragging'); |
| 54 |
}, |
| 55 |
stop: function() { |
| 56 |
// $control.css({overflow: 'hidden'}); |
| 57 |
$control.removeClass('dragging'); |
| 58 |
var active = self.$activeItems ? self.$activeItems.slice() : null; |
| 59 |
var values = []; |
| 60 |
$control.children('[data-value]').each(function() { |
| 61 |
values.push($(this).attr('data-value')); |
| 62 |
}); |
| 63 |
self.isFocused = false; |
| 64 |
self.setValue(values); |
| 65 |
self.isFocused = true; |
| 66 |
self.setActiveItem(active); |
| 67 |
self.positionDropdown(); |
| 68 |
} |
| 69 |
}); |
| 70 |
}; |
| 71 |
})(); |
| 72 |
|
| 73 |
}); |
| 74 |
|