Newer
Older
Sonia Zorba
committed
(function ($) {
// Function factory to handle custom communications between the backing bean and JavaScript functions
function eventHandlerFactory(handler, componentId) {
Sonia Zorba
committed
return function (event) {
if (event.status === 'success') {
var srcId = componentId ? componentId : event.source.getAttribute('id');
var jsUpdate = null, htmlUpdate = null;
Sonia Zorba
committed
var jsupdates = event.responseXML.getElementById('jsupdates');
if (jsupdates) {
jsupdates = jsupdates.childNodes;
for (var i = 0; i < jsupdates.length; i++) {
var jsu = jsupdates[i];
if (jsu.getAttribute('src') === srcId) {
jsUpdate = jsu.innerText || jsu.textContent;
break;
}
}
}
var htmlupdates = event.responseXML.getElementById('htmlupdates');
if (htmlupdates) {
htmlupdates = htmlupdates.childNodes;
for (var i = 0; i < htmlupdates.length; i++) {
var hu = htmlupdates[i];
if (hu.getAttribute('src') === srcId) {
htmlUpdate = hu;
break;
Sonia Zorba
committed
}
handler(event.source, jsUpdate, htmlUpdate);
Sonia Zorba
committed
};
}
var COLUMNS_COMPONENT_ID = 'main:columns-list';
window.TSM = {
textInputChanged: eventHandlerFactory(function (srcElement, jsupdate) {
$(srcElement).toggleClass('changed', jsupdate === 'true');
Sonia Zorba
committed
}),
columnChanged: function (event) {
if (event.status === 'success') {
var $li = $(event.source).closest('li');
$li.closest('ul').find('li').removeClass('active');
$li.addClass('active');
Sonia Zorba
committed
},
stopPropagation: function (event) {
event.stopPropagation();
event.preventDefault();
return false;
},
columnRemoved: eventHandlerFactory(function (srcElement, jsupdate, htmlupdate) {
jsupdate = JSON.parse(jsupdate);
Sonia Zorba
committed
var $ul = $(srcElement).closest('ul');
if (jsupdate.hideRemoved) {
Sonia Zorba
committed
$(srcElement).closest('li').remove();
} else {
$(srcElement).closest('a').find('span').addClass('strikeout');
$(srcElement).prop('disabled', true);
}
if (jsupdate.selectedColumn !== undefined) {
Sonia Zorba
committed
$ul.find('li').removeClass('active');
$ul.find('li:nth-child(' + (jsupdate.selectedColumn + 1) + ')').addClass('active');
if (jsupdate.hideRemoved) {
Sonia Zorba
committed
// Restore correct ids
$htmlUpdate = $(htmlupdate);
$htmlUpdate.find('li a');
$htmlUpdate.find('li a input');
$htmlUpdate.find('li').each(function (index, element) {
var $this = $(element);
var $input = $this.find('input');
var $li = $('.columns-selector').find('li:nth-child(' + (index + 1) + ')');
$li.attr('id', $this.attr('id'));
$li.find('a').attr('id', $this.find('a').attr('id'));
$li.find('input').attr('id', $input.attr('id')).attr('name', $input.attr('name'));
Sonia Zorba
committed
}
}, COLUMNS_COMPONENT_ID),
columnRemovalUndo: eventHandlerFactory(function (srcElement, jsupdate) {
var $a = $('#main\\:columns-list\\:' + jsupdate + '\\:column-selector');
Sonia Zorba
committed
$a.find('input').prop('disabled', false);
$a.find('.strikeout').removeClass('strikeout');
}, COLUMNS_COMPONENT_ID),
Sonia Zorba
committed
ucdTextKeyDown: function (event) {
if (event.keyCode === 13) {
$('#ucd_search_form\\:search_UCD_btn').click();
}
},
openAddablesModal: function (event) {
if (event.status === 'success') {
$('#addablesModal').modal('show');
}
},
entitiesAdded: function (event) {
if (event.status === 'success') {
$('#addablesModal').modal('hide');
}
},
openSearchUCDModal: function (event) {
if (event.status === 'success') {
$('#searchUCDModal').modal('show');
}
},
closeSearchUCDModal: function (event) {
if (event.status === 'success') {
Sonia Zorba
committed
}
},
notifyUpdate: function (event) {
if (event.status === 'success' && $(event.responseXML).find('error').length === 0) {
$('#updateSuccessModal').modal('show');
}
Sonia Zorba
committed
}
};
$(document).ready(function () {
jsf.ajax.addOnError(function (error) {
$('#errorModal .errorName').text(error.errorName);
$('#errorModal .errorMessage').text(error.errorMessage);
if (error.errorName.indexOf('NonexistentConversationException') !== -1) {
// If conversation has expired reload the page to display error message
location.reload();
} else {
$('#errorModal').modal('show');
}
});
});
Sonia Zorba
committed
})(jQuery);