X-Git-Url: https://git.mdrn.pl/wolnelektury.git/blobdiff_plain/019f463953a09e7b2fe4ba8d4fa344bbf8c67164..0d0fa7a4cd45b477e40740a8d971ec814bc6a4a9:/apps/sponsors/static/js/ordered_select_multiple.js
diff --git a/apps/sponsors/static/js/ordered_select_multiple.js b/apps/sponsors/static/js/ordered_select_multiple.js
new file mode 100644
index 000000000..e4fd74daa
--- /dev/null
+++ b/apps/sponsors/static/js/ordered_select_multiple.js
@@ -0,0 +1,63 @@
+(function($) {
+ $.fn.orderedSelectMultiple = function(options) {
+ var settings = {
+ choices: []
+ };
+ $.extend(settings, options);
+
+ var input = $(this).hide();
+ var values = input.val().split(',');
+
+ var container = $('
').insertAfter($(this));
+ var choicesList = $('
').appendTo(container).css({
+ width: 200, float: 'left', minHeight: 200, backgroundColor: '#eee', margin: 0, padding: 0
+ });
+ var valuesList = $('
').appendTo(container).css({
+ width: 200, float: 'left', minHeight: 200, backgroundColor: '#eee', margin: 0, padding: 0
+ });
+ var choiceIds = [];
+ $.each(settings.choices, function() {
+ choiceIds.push('' + this.id);
+ });
+
+ function createItem(hash) {
+ return $('' + hash.name + '').css({
+ backgroundColor: '#cff',
+ display: 'block',
+ border: '1px solid #cdd',
+ padding: 2,
+ margin: 0
+ }).data('obj-id', hash.id);
+ }
+
+ $.each(settings.choices, function() {
+ if ($.inArray('' + this.id, values) == -1) {
+ choicesList.append(createItem(this));
+ }
+ });
+
+ $.each(values, function() {
+ var index = $.inArray('' + this, choiceIds); // Why this[0]?
+ if (index != -1) {
+ valuesList.append(createItem(settings.choices[index]));
+ }
+ });
+
+ choicesList.sortable({
+ connectWith: '.connectedSortable'
+ }).disableSelection();
+
+ valuesList.sortable({
+ connectWith: '.connectedSortable',
+ update: function() {
+ values = [];
+ $('li', valuesList).each(function(index) {
+ values.push($(this).data('obj-id'));
+ console.log($(this).data('obj-id'));
+ });
+ console.log('update', values.join(','), input);
+ input.val(values.join(','));
+ }
+ }).disableSelection();
+ };
+})(jQuery);