2012-02-09 11:17:09 +01:00
|
|
|
(function($){
|
2012-02-07 20:56:40 +01:00
|
|
|
|
2012-02-23 15:17:39 +01:00
|
|
|
$('.ss-gridfield').entwine({
|
2012-02-09 11:17:09 +01:00
|
|
|
/**
|
|
|
|
* @param {Object} Additional options for jQuery.ajax() call
|
2012-02-17 00:38:21 +01:00
|
|
|
* @param {successCallback} callback to call after reloading succeeded.
|
2012-02-09 11:17:09 +01:00
|
|
|
*/
|
|
|
|
reload: function(ajaxOpts, successCallback) {
|
|
|
|
var self = this, form = this.closest('form'), data = form.find(':input').serializeArray();
|
|
|
|
if(!ajaxOpts) ajaxOpts = {};
|
|
|
|
if(!ajaxOpts.data) ajaxOpts.data = [];
|
|
|
|
ajaxOpts.data = ajaxOpts.data.concat(data);
|
|
|
|
|
|
|
|
form.addClass('loading');
|
|
|
|
|
|
|
|
$.ajax($.extend({}, {
|
|
|
|
headers: {"X-Get-Fragment" : 'CurrentField'},
|
|
|
|
type: "POST",
|
|
|
|
url: this.data('url'),
|
|
|
|
dataType: 'html',
|
|
|
|
success: function(data) {
|
|
|
|
// Replace the grid field with response, not the form.
|
2012-02-09 17:17:39 +01:00
|
|
|
// TODO Only replaces all its children, to avoid replacing the current scope
|
|
|
|
// of the executing method. Means that it doesn't retrigger the onmatch() on the main container.
|
|
|
|
self.empty().append($(data).children());
|
|
|
|
|
2012-02-09 11:17:09 +01:00
|
|
|
form.removeClass('loading');
|
|
|
|
if(successCallback) successCallback.apply(this, arguments);
|
2012-02-17 00:38:21 +01:00
|
|
|
self.trigger('reload', self);
|
2012-02-09 11:17:09 +01:00
|
|
|
},
|
|
|
|
error: function(e) {
|
2012-02-21 21:30:51 +01:00
|
|
|
alert(ss.i18n._t('GRIDFIELD.ERRORINTRANSACTION'));
|
2012-02-09 11:17:09 +01:00
|
|
|
form.removeClass('loading');
|
|
|
|
}
|
|
|
|
}, ajaxOpts));
|
|
|
|
},
|
2012-02-07 20:56:40 +01:00
|
|
|
getItems: function() {
|
|
|
|
return this.find('.ss-gridfield-item');
|
2012-02-08 21:28:04 +01:00
|
|
|
},
|
|
|
|
/**
|
|
|
|
* @param {String}
|
|
|
|
* @param {Mixed}
|
|
|
|
*/
|
|
|
|
setState: function(k, v) {
|
|
|
|
var state = this.getState();
|
|
|
|
state[k] = v;
|
|
|
|
this.find(':input[name="' + this.data('name') + '[GridState]"]').val(JSON.stringify(state));
|
|
|
|
},
|
|
|
|
/**
|
|
|
|
* @return {Object}
|
|
|
|
*/
|
|
|
|
getState: function() {
|
|
|
|
return JSON.parse(this.find(':input[name="' + this.data('name') + '[GridState]"]').val());
|
2012-02-07 20:56:40 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2012-02-23 15:17:39 +01:00
|
|
|
$('.ss-gridfield *').entwine({
|
2012-02-07 20:56:40 +01:00
|
|
|
getGridField: function() {
|
2012-02-23 15:17:39 +01:00
|
|
|
return this.closest('.ss-gridfield');
|
2012-02-07 20:56:40 +01:00
|
|
|
}
|
|
|
|
});
|
2011-12-06 01:56:24 +01:00
|
|
|
|
2012-02-23 15:17:39 +01:00
|
|
|
$('.ss-gridfield .action').entwine({
|
2011-12-06 01:56:24 +01:00
|
|
|
onclick: function(e){
|
2012-02-09 11:17:09 +01:00
|
|
|
this.getGridField().reload({data: [{name: this.attr('name'), value: this.val()}]});
|
2011-12-06 01:56:24 +01:00
|
|
|
e.preventDefault();
|
|
|
|
}
|
|
|
|
});
|
2012-02-21 21:30:51 +01:00
|
|
|
|
2012-02-23 15:17:39 +01:00
|
|
|
$('.ss-gridfield .action-deleterecord').entwine({
|
2012-02-21 21:30:51 +01:00
|
|
|
onclick: function(e){
|
|
|
|
if(!confirm(ss.i18n._t('TABLEFIELD.DELETECONFIRMMESSAGE'))) return false;
|
|
|
|
else this._super(e);
|
|
|
|
}
|
|
|
|
});
|
2011-12-06 01:56:24 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Upon focusing on a filter <input> element, move "filter" and "reset" buttons and display next to the current <input> element
|
|
|
|
* ToDo ensure filter-button state is maintained after filtering (see resetState param)
|
|
|
|
* ToDo get working in IE 6-7
|
|
|
|
*/
|
2012-02-23 15:17:39 +01:00
|
|
|
$('.ss-gridfield input.ss-gridfield-sort').entwine({
|
2011-12-06 01:56:24 +01:00
|
|
|
onfocusin: function(e) {
|
2012-01-10 00:28:24 +01:00
|
|
|
// Dodgy results in IE <=7 & ignore if only one filter-field
|
2012-02-23 15:17:39 +01:00
|
|
|
countfields = $('.ss-gridfield input.ss-gridfield-sort').length;
|
2012-01-10 00:28:24 +01:00
|
|
|
if(($.browser.msie && $.browser.version <= 7) || countfields == 1) {
|
2011-12-06 01:56:24 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
var eleInput = $(this);
|
2012-02-07 20:56:40 +01:00
|
|
|
|
2011-12-06 01:56:24 +01:00
|
|
|
// Remove existing <div> and <button> elements in-lieu of cloning
|
2012-02-07 20:56:40 +01:00
|
|
|
this.getGridField().find('th > div').each(function(i,v) {$(v).remove();});
|
|
|
|
|
2011-12-06 01:56:24 +01:00
|
|
|
var eleButtonSetFilter = $('#action_filter');
|
|
|
|
var eleButtonResetFilter = $('#action_reset');
|
|
|
|
// Retain current widths to ensure <th>'s don't shift widths
|
|
|
|
var eleButtonWidth = eleButtonSetFilter.width();
|
|
|
|
// Check <th> doesn't already have an (extra) cloned <button> appended, otherwise clone
|
|
|
|
if(eleInput.closest('th').children().length == 1) {
|
|
|
|
var newButtonCss = {
|
|
|
|
'position':'absolute',
|
|
|
|
'top':'-23px',
|
|
|
|
'left':'0',
|
|
|
|
'border':'#EEE solid 1px',
|
|
|
|
'padding':'0',
|
|
|
|
'margin-left':'0'
|
|
|
|
};
|
|
|
|
// Append a <div> element used purely for CSS positioning - table elements on their own are untrustworthy to style in this manner
|
|
|
|
$('<div/>').append(
|
|
|
|
eleButtonSetFilter.clone().css(newButtonCss),
|
|
|
|
eleButtonResetFilter.clone().css(newButtonCss).css('left',(eleButtonWidth+4)+'px')
|
|
|
|
).css({'position':'relative','margin':'0 auto','width':'65%'}).appendTo(eleInput.closest('th'));
|
|
|
|
}
|
|
|
|
}
|
2012-02-23 02:54:03 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
$('fieldset.ss-gridfield .new-link').entwine({
|
|
|
|
onclick: function(e) {
|
|
|
|
$(this).trigger('opennewview', $(this).prop('href'));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
$('fieldset.ss-gridfield .edit-link').entwine({
|
|
|
|
onclick: function(e) {
|
|
|
|
$(this).trigger('openeditview', $(this).prop('href'));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
});
|
2011-12-06 01:56:24 +01:00
|
|
|
|
2012-02-07 20:56:40 +01:00
|
|
|
/**
|
|
|
|
* Allows selection of one or more rows in the grid field.
|
|
|
|
* Purely clientside at the moment.
|
|
|
|
*/
|
2012-02-23 15:17:39 +01:00
|
|
|
$('.ss-gridfield[data-selectable]').entwine({
|
2012-02-07 20:56:40 +01:00
|
|
|
/**
|
|
|
|
* @return {jQuery} Collection
|
|
|
|
*/
|
|
|
|
getSelectedItems: function() {
|
|
|
|
return this.find('.ss-gridfield-item.ui-selected');
|
|
|
|
},
|
|
|
|
/**
|
|
|
|
* @return {Array} Of record IDs
|
|
|
|
*/
|
|
|
|
getSelectedIDs: function() {
|
|
|
|
return $.map(this.getSelectedItems(), function(el) {return $(el).data('id');});
|
|
|
|
}
|
|
|
|
});
|
2012-02-23 15:17:39 +01:00
|
|
|
$('.ss-gridfield[data-selectable] .ss-gridfield-items').entwine({
|
2012-02-07 20:56:40 +01:00
|
|
|
onmatch: function() {
|
|
|
|
this._super();
|
|
|
|
|
|
|
|
// TODO Limit to single selection
|
|
|
|
this.selectable();
|
|
|
|
},
|
|
|
|
onunmatch: function() {
|
|
|
|
this._super();
|
|
|
|
this.selectable('destroy');
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
2012-02-27 23:58:10 +01:00
|
|
|
}(jQuery));
|