REDMINE_EXTENSIONS = {
toggleDiv: function(el_or_id) {
var el;
if (typeof(el_or_id) === 'string') {
el = $('#' + el_or_id);
} else {
el = el_or_id;
}
el.toggleClass('collapsed').slideToggle('fast');
},
toggleDivAndChangeOpen: function(toggleElementId, changeOpenElement) {
REDMINE_EXTENSIONS.toggleDiv(toggleElementId);
$(changeOpenElement).toggleClass('open');
},
toggleFilterButtons: function(elButtonsID, elFilter1ID, elFilter2ID)
{
var elButtons = $('#' + elButtonsID);
var elFilter1 = $('#' + elFilter1ID);
var elFilter2 = $('#' + elFilter2ID);
if (elFilter1.hasClass('collapsed') && elFilter2.hasClass('collapsed')) {
elButtons.slideUp('slow');
} else {
elButtons.slideDown('slow');
}
}
};
// TODO delete - it will be added to extensions
(function($, undefined) {
var plugin = 'easygrouploader';
if( $.fn[plugin] )
return;
var defaults = {
next_button_cols: 1,
load_opened: false,
load_limit: 25,
texts: {
'next': 'Next',
}
};
$.fn[plugin] = function(options, methodParams) {
$.each($(this), function(idx) {
var instance = $(this).data('plugin_' + plugin);
if (!instance) {
instance = new EasyGroupLoader(this, options);
$(this).data('plugin_' + plugin, instance);
} else if (typeof options === 'string') {
switch (options) {
case 'load_groups':
if (instance.options.load_opened)
instance.load_all_groups();
}
}
});
return $(this);
};
function EasyGroupLoader(elem, options) {
this.groupsContainer = $(elem);
this.options = $.extend({}, defaults, options);
this.loadUrl = options.loadUrl || elem.data('url');
this.callback = options.callback;
this.texts = this.options.texts;
this.init();
}
EasyGroupLoader.prototype.init = function()
{
var self = this;
this.groupsContainer.on('click', '.group .expander', function(evt) {
var $row = $(this).closest('tr.group');
var group = $row.data('group') || new Group(self, $row);
if (!group.loaded) {
if (!group.header.hasClass('group-loaded')) {
group.load();
group.toggle();
}
} else {
group.toggle();
if(typeof(self.callback) === 'function')
self.callback();
}
});
if (this.options.load_opened)
this.load_all_groups();
};
EasyGroupLoader.prototype.initInlineEdit = function()
{
$('.multieditable-container:not(.multieditable-initialized)', this.groupsContainer).each(function() {
initInlineEditForContainer(this);
});
initProjectEdit();
initEasyAutocomplete();
};
EasyGroupLoader.prototype.load_all_groups = function()
{
var group;
var self = this;
var groups_to_load = [];
var entity_count = 0;
$('.group', this.groupsContainer).not('.group-loaded').each(function() {
group = $(this).data('group') || new Group(self, $(this));
if (!group.loaded) {
groups_to_load.push(group);
entity_count += group.count;
}
if (entity_count >= self.options.load_limit) {
self.load_groups(groups_to_load);
entity_count = 0;
groups_to_load = [];
}
});
if (groups_to_load.length > 0) {
this.load_groups(groups_to_load);
}
};
EasyGroupLoader.prototype.load_groups = function(groups_to_load) {
var self = this;
var group_names = groups_to_load.map(function(group) {
return group.group_value;
});
// var url = EPExtensions.setAttrToUrl(, 'group_to_load', group_names);
$.ajax(this.loadUrl, {
method: 'GET',
data: { group_to_load: group_names },
success: function(data, textStatus, request) {
var parsed = (typeof data === 'object') ? data : $.parseJSON(data);
$.each(groups_to_load, function(idx, group) {
group.parseData(parsed[group.group_name]);
group.toggle();
});
self.initInlineEdit();
}
});
};
function Group(loader, header)
{
this.loader = loader;
this.header = header;
this.header.data('group', this);
this.group_name = this.header.data('group-name');
this.group_value = this.group_name;
if( $.isArray(this.group_name) ) {
// potencialne nebezpecne - TODO: vymyslet spravny oddelovac
this.group_name = '["' + this.group_name.join('", "') + '"]';
}
this.count = parseInt(this.header.data('entity-count'));
this.pages = this.header.data('pages') || 1;
this.loaded = this.header.hasClass('preloaded');
}
Group.prototype.toggle = function() {
EPExtensions.issuesToggleRowGroup(this.header);
};
Group.prototype.load = function() {
var $hrow = this.header;
var self = this;
if (!$hrow.hasClass('group-loaded')) {
$hrow.addClass('group-loaded');
$.ajax(this.loader.loadUrl, {
method: 'GET',
data: {
group_to_load: this.group_value
},
success: function(data, textStatus, request) {
self.parseData(data);
self.loader.initInlineEdit();
if(typeof(self.loader.callback) === 'function')
self.loader.callback();
}
});
}
};
Group.prototype.parseData = function(data) {
var $hrow = this.header;
this.rows = $(data);
$hrow.after(this.rows);
$hrow.data('group-page', 1);
this.loaded = true;
if (this.pages > 1) {
this.createNextButton();
// .find doesn't work on this set
this.rows.filter("tr:last").after(this.next_button);
}
};
Group.prototype.loadNext = function() {
var $hrow = this.header;
var page = $hrow.data('group-page') + 1;
var self = this;
if (page <= this.pages) {
$.ajax(this.loader.loadUrl, {
method: 'GET',
data: {
page: page,
group_to_load: this.group_value
},
success: function(data, textStatus, request) {
self.next_button.before(data);
self.loader.initInlineEdit();
$hrow.data('group-page', page);
if (self.pages === page) {
self.next_button.remove();
}
}
});
}
};
Group.prototype.createNextButton = function() {
//var next_link_url = EPExtensions.setAttrToUrl(this.loader.loadUrl, 'group_to_load', this.group_value);
var next_link_url = this.loader.loadUrl + ( this.loader.loadUrl.indexOf('?') >= 0 ? '&' : '?' ) + $.param({group_to_load: this.group_value});
this.next_link = $('', {href: next_link_url, 'class': 'button'}).text(this.loader.texts['next']).append($("", {"class": "icon-arrow"}));
this.next_button = $('', {'class': 'easy-next-button'}).html($('