2 $.fn.sponsorsFooter = function(options) {
6 $.extend(settings, options);
8 var input = $(this).hide();
10 var container = $('<div class="sponsors"></div>').appendTo(input.parent());
11 var groups = $.evalJSON(input.val());
13 var unusedDiv = $('<div class="sponsors-sponsor-group sponsors-unused-sponsor-group"></div>')
15 .append('<p class="sponsors-sponsor-group-name sponsors-unused-sponsor-group-name">dostępni sponsorzy</p>');
16 var unusedList = $('<ol class="sponsors-sponsor-group-list sponsors-unused-group-list"></ol>')
19 connectWith: '.sponsors-sponsor-group-list'
22 // Edit group name inline
23 function editNameInline(name) {
24 name.unbind('click.sponsorsFooter');
25 var inlineInput = $('<input></input>').val(name.html());
28 function endEditing() {
29 name.html(inlineInput.val());
31 name.bind('click.sponsorsFooter', function() {
32 editNameInline($(this));
34 input.parents('form').unbind('submit.sponsorsFooter', endEditing);
38 inlineInput.appendTo(name).focus().blur(endEditing);
39 input.parents('form').bind('submit.sponsorsFooter', endEditing);
42 // Remove sponsor with passed id from sponsors array and return it
43 function popSponsor(id) {
44 for (var i=0; i < settings.sponsors.length; i++) {
45 if (settings.sponsors[i].id == id) {
46 var s = settings.sponsors[i];
47 settings.sponsors.splice(i, 1);
54 // Create sponsor group and bind events
55 function createGroup(name, sponsors) {
60 var groupDiv = $('<div class="sponsors-sponsor-group"></div>');
62 $('<a class="sponsors-remove-sponsor-group">X</a>')
64 groupDiv.fadeOut('slow', function() {
65 $('.sponsors-sponsor', groupDiv).hide().appendTo(unusedList).fadeIn();
68 }).appendTo(groupDiv);
70 $('<p class="sponsors-sponsor-group-name">' + name + '</p>')
71 .bind('click.sponsorsFooter', function() {
72 editNameInline($(this));
73 }).appendTo(groupDiv);
75 var groupList = $('<ol class="sponsors-sponsor-group-list"></ol>')
78 connectWith: '.sponsors-sponsor-group-list'
82 for (var i = 0; i < sponsors.length; i++) {
83 $('<li class="sponsors-sponsor">' + sponsors[i].name + '</li>')
84 .data('obj_id', sponsors[i].id)
90 // Create groups from data in input value
91 for (var i = 0; i < groups.length; i++) {
92 var group = groups[i];
95 for (var j = 0; j < group.sponsors.length; j++) {
96 var s = popSponsor(group.sponsors[j]);
101 createGroup(group.name, sponsors).appendTo(container);
104 // Serialize input value before submiting form
105 input.parents('form').submit(function(event) {
107 $('.sponsors-sponsor-group', container).not('.sponsors-unused-sponsor-group').each(function() {
108 var group = {name: $('.sponsors-sponsor-group-name', this).html(), sponsors: []};
109 $('.sponsors-sponsor', this).each(function() {
110 group.sponsors.push($(this).data('obj_id'));
114 input.val($.toJSON(groups));
117 for (i = 0; i < settings.sponsors.length; i++) {
118 $('<li class="sponsors-sponsor">' + settings.sponsors[i].name + '</li>')
119 .data('obj_id', settings.sponsors[i].id)
120 .appendTo(unusedList);
123 $('<button type="button">Dodaj nową grupę</button>')
125 var newGroup = createGroup('').appendTo(container);
126 editNameInline($('.sponsors-sponsor-group-name', newGroup));
127 }).prependTo(input.parent());
129 input.parent().append('<div style="clear: both"></div>');