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) {
|
2012-03-06 00:33:21 +01:00
|
|
|
var self = this, form = this.closest('form'), data = form.find(':input').serializeArray();
|
2012-02-09 11:17:09 +01:00
|
|
|
if(!ajaxOpts) ajaxOpts = {};
|
|
|
|
if(!ajaxOpts.data) ajaxOpts.data = [];
|
|
|
|
ajaxOpts.data = ajaxOpts.data.concat(data);
|
|
|
|
|
2012-03-02 20:27:13 +01:00
|
|
|
// Include any GET parameters from the current URL, as the view state might depend on it.
|
|
|
|
// For example, a list prefiltered through external search criteria might be passed to GridField.
|
|
|
|
if(window.location.search) {
|
|
|
|
ajaxOpts.data = window.location.search.replace(/^\?/, '') + '&' + $.param(ajaxOpts.data);
|
|
|
|
}
|
|
|
|
|
2012-02-09 11:17:09 +01:00
|
|
|
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-03-01 00:19:22 +01:00
|
|
|
showDetailView: function(url) {
|
|
|
|
window.location.href = url;
|
|
|
|
},
|
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
|
|
|
}
|
|
|
|
});
|
2012-03-01 00:19:22 +01:00
|
|
|
|
|
|
|
$('.ss-gridfield .ss-gridfield-item').entwine({
|
|
|
|
onclick: function(e) {
|
2012-03-06 01:21:34 +01:00
|
|
|
if($(e.target).closest('.action').length) {
|
2012-03-01 00:19:22 +01:00
|
|
|
this._super(e);
|
2012-03-06 01:21:34 +01:00
|
|
|
return false;
|
2012-03-01 00:19:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
var editLink = this.find('.edit-link');
|
|
|
|
if(editLink.length) this.getGridField().showDetailView(editLink.prop('href'));
|
|
|
|
},
|
|
|
|
onmouseover: function() {
|
|
|
|
if(this.find('.edit-link').length) this.css('cursor', 'pointer');
|
|
|
|
},
|
|
|
|
onmouseout: function() {
|
|
|
|
this.css('cursor', 'default');
|
|
|
|
}
|
|
|
|
});
|
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-03-06 01:21:34 +01:00
|
|
|
$('.ss-gridfield .action.gridfield-button-delete').entwine({
|
2012-02-21 21:30:51 +01:00
|
|
|
onclick: function(e){
|
2012-03-06 01:21:34 +01:00
|
|
|
if(!confirm(ss.i18n._t('TABLEFIELD.DELETECONFIRMMESSAGE'))) {
|
|
|
|
e.preventDefault();
|
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
this._super(e);
|
|
|
|
}
|
2012-02-21 21:30:51 +01:00
|
|
|
}
|
|
|
|
});
|
2012-02-23 02:54:03 +01:00
|
|
|
|
|
|
|
$('fieldset.ss-gridfield .new-link').entwine({
|
|
|
|
onclick: function(e) {
|
2012-03-01 00:19:22 +01:00
|
|
|
this.getGridField().showDetailView($(this).prop('href'));
|
2012-02-23 02:54:03 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
$('fieldset.ss-gridfield .edit-link').entwine({
|
|
|
|
onclick: function(e) {
|
2012-03-01 00:19:22 +01:00
|
|
|
this.getGridField().showDetailView($(this).prop('href'));
|
2012-02-23 02:54:03 +01:00
|
|
|
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-03-06 00:33:21 +01:00
|
|
|
|
2012-03-01 12:25:44 +01:00
|
|
|
/**
|
|
|
|
* Catch submission event in filter input fields, and submit the correct button
|
|
|
|
* rather than the whole form.
|
|
|
|
*/
|
|
|
|
$('.ss-gridfield .filter-header :input').entwine({
|
2012-03-06 00:33:21 +01:00
|
|
|
onmatch: function(){
|
|
|
|
filterbtn = this.closest('.fieldgroup').find('.ss-gridfield-button-filter');
|
|
|
|
resetbtn = this.closest('.fieldgroup').find('.ss-gridfield-button-reset');
|
|
|
|
if(this.val()) {
|
|
|
|
filterbtn.addClass('filtered');
|
|
|
|
resetbtn.addClass('filtered');
|
|
|
|
}
|
|
|
|
},
|
2012-03-01 12:25:44 +01:00
|
|
|
onkeydown: function(e) {
|
2012-03-05 22:44:20 +01:00
|
|
|
filterbtn = this.closest('.fieldgroup').find('.ss-gridfield-button-filter');
|
|
|
|
resetbtn = this.closest('.fieldgroup').find('.ss-gridfield-button-reset');
|
2012-03-01 12:25:44 +01:00
|
|
|
if(e.keyCode == '13') {
|
2012-03-05 05:28:42 +01:00
|
|
|
btns = this.closest('.filter-header').find('.ss-gridfield-button-filter');
|
|
|
|
this.getGridField().reload({data: [{name: btns.attr('name'), value: btns.val()}]});
|
2012-03-01 12:25:44 +01:00
|
|
|
return false;
|
2012-03-05 05:28:42 +01:00
|
|
|
}else{
|
2012-03-05 22:44:20 +01:00
|
|
|
filterbtn.addClass('hover-alike');
|
|
|
|
resetbtn.addClass('hover-alike');
|
2012-03-01 12:25:44 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2012-02-27 23:58:10 +01:00
|
|
|
}(jQuery));
|