Koha/koha-tmpl/intranet-tmpl/prog/js/pages/preferences.js
Owen Leonard 22ff2118a7 Bug 26237: Move translatable strings out of preferences.tt and into JavaScript files
This patch removes the definition of translatable strings out of
templates and into the corresponding JavaScript file, using the new JS
i81n function.

To test (a non-comprehensive list):

- Apply the patch, go to Administration -> System preference
- Modify any system preference. "modified" should appear next to the
  preference name.
- Click the "Save" button. You should get a message, "Saved preference
  <pref name>"
- Hover your mouse over a section heading (e.g. "Features" under
  Accounting). You should see a tooltip, "Click to collapse this
  section."
- Click to collapse the section. The tooltip should change to "Click to
  expand the section."
- Open a new tab for the staff interface and log out.
- In the original tab, modify and try to save a system preference. You
  should get a message, "Error; your data might not have been saved."

TESTING TRANSLATABILITY

- Update a translation, e.g. fr-FR:

  > cd misc/translator
  > perl translate update fr-FR

- Open the corresponding .po file for JavaScript strings, e.g.
  misc/translator/po/fr-FR-messages-js.po
- Locate strings pulled from
  koha-tmpl/intranet-tmpl/prog/js/pages/preferences.js for
  translation, e.g.:

  msgid "Saved preference %s"
  msgstr ""

- Edit the "msgstr" string however you want (it's just for
  testing).
- Install the updated translation:

  > perl translate install fr-FR

- Switch to your newly translated language in the staff
  client and repeat the test plan above. The translated strings
  should appear.

Signed-off-by: David Nind <david@davidnind.com>

Signed-off-by: Katrin Fischer <katrin.fischer.83@web.de>

Signed-off-by: Jonathan Druart <jonathan.druart@bugs.koha-community.org>
2020-09-29 14:28:18 +02:00

283 lines
10 KiB
JavaScript

/* global KOHA CodeMirror to_highlight search_jumped humanMsg dataTablesDefaults themelang */
// We can assume 'KOHA' exists, as we depend on KOHA.AJAX
KOHA.Preferences = {
Save: function ( form ) {
if ( ! $(form).valid() ) {
humanMsg.displayAlert( __("Error: presence of invalid data prevent saving. Please make the corrections and try again.") );
return;
}
modified_prefs = $( form ).find( '.modified' );
// $.serialize removes empty value, we need to keep them.
// If a multiple select has all its entries unselected
var unserialized = new Array();
$(modified_prefs).each(function(){
if ( $(this).attr('multiple') && $(this).val() == null ) {
unserialized.push($(this));
}
});
data = modified_prefs.serialize();
$(unserialized).each(function(){
data += '&' + $(this).attr('name') + '=';
});
if ( !data ) {
humanMsg.displayAlert( __("Nothing to save") );
return;
}
KOHA.AJAX.MarkRunning($(form).find('.save-all'), __("Saving...") );
KOHA.AJAX.Submit( {
data: data,
url: '/cgi-bin/koha/svc/config/systempreferences/',
success: function ( data ) { KOHA.Preferences.Success( form ) },
complete: function () { KOHA.AJAX.MarkDone( $( form ).find( '.save-all' ) ) }
} );
},
Success: function ( form ) {
var msg = "";
modified_prefs.each(function(){
var modified_pref = $(this).attr("id");
modified_pref = modified_pref.replace("pref_","");
msg += "<strong>" + __("Saved preference %s").format(modified_pref) + "</strong>\n";
});
humanMsg.displayAlert(msg);
$( form )
.find( '.modified-warning' ).remove().end()
.find( '.modified' ).removeClass('modified');
KOHA.Preferences.Modified = false;
}
};
function mark_modified() {
$( this.form ).find( '.save-all' ).prop('disabled', false);
$( this ).addClass( 'modified' );
var name_cell = $( this ).parents( '.name-row' ).find( '.name-cell' );
if ( !name_cell.find( '.modified-warning' ).length )
name_cell.append('<em class="modified-warning">(' + __("modified") + ')</em>');
KOHA.Preferences.Modified = true;
}
window.onbeforeunload = function () {
if ( KOHA.Preferences.Modified ) {
return __("You have made changes to system preferences.");
}
};
$( document ).ready( function () {
$("table.preferences").dataTable($.extend(true, {}, dataTablesDefaults, {
"sDom": 't',
"aoColumnDefs": [
{ "aTargets": [ -1 ], "bSortable": false, "bSearchable": false }
],
"bPaginate": false
}));
$( '.prefs-tab' )
.find( 'input.preference, textarea.preference' ).on('input', function () {
if ( this.defaultValue === undefined || this.value != this.defaultValue ) mark_modified.call( this );
} ).end()
.find( 'select.preference' ).change( mark_modified );
$('.preference-checkbox').change( function () {
$('.preference-checkbox').addClass('modified');
mark_modified.call(this);
} );
$(".set_syspref").click(function() {
var s = $(this).attr('data-syspref');
var v = $(this).attr('data-value');
// populate the input with the value in data-value
$("#pref_"+s).val(v);
// pass the DOM element to trigger "modified" to enable submit button
mark_modified.call($("#pref_"+s)[0]);
return false;
});
$(".sortable").sortable();
$(".sortable").on( "sortchange", function( event, ui ) {
// This is not exact but we just need to trigger a change
$(ui.item.find('input:first')).change();
} );
$( '.prefs-tab .action .cancel' ).click( function () { KOHA.Preferences.Modified = false } );
$( '.prefs-tab .save-all' ).prop('disabled', true).click( function () {
KOHA.Preferences.Save( this.form );
return false;
});
$( ".expand-textarea" ).on("click", function(e){
e.preventDefault();
$(this).hide();
var target = $(this).data("target");
var syntax = $(this).data("syntax");
$("#collapse_" + target ).show();
if( syntax ){
var editor = CodeMirror.fromTextArea( document.getElementById( "pref_" + target ), {
lineNumbers: true,
mode: syntax,
lineWrapping: true,
viewportMargin: Infinity,
gutters: ["CodeMirror-lint-markers"],
lint: true
});
editor.on("change", function(){
mark_modified.call( $("#pref_" + target )[0]);
});
editor.on("blur", function(){
editor.save();
});
} else {
$("#pref_" + target ).show();
}
});
$( ".collapse-textarea" ).on("click", function(e){
e.preventDefault();
$(this).hide();
var target = $(this).data("target");
var syntax = $(this).data("syntax");
$("#expand_" + target ).show();
if( syntax ){
var editor = $("#pref_" + target ).next(".CodeMirror")[0].CodeMirror;
editor.toTextArea();
}
$("#pref_" + target ).hide();
});
$("h3").attr("class", "expanded").attr("title", __("Click to collapse this section"));
var collapsible = $(".collapsed,.expanded");
$(collapsible).on("click",function(){
var h3Id = $(this).attr("id");
var panel = $("#collapse_" + h3Id);
if(panel.is(":visible")){
$(this).addClass("collapsed").removeClass("expanded").attr("title", __("Click to expand this section") );
panel.hide();
} else {
$(this).addClass("expanded").removeClass("collapsed").attr("title", __("Click to collapse this section") );
panel.show();
}
});
$(".pref_sublink").on("click", function(){
/* If the user clicks a sub-menu link in the sidebar,
check to see if it is collapsed. If so, expand it */
var href = $(this).attr("href");
href = href.replace("#","");
var panel = $("#collapse_" + href );
if( panel.is(":hidden") ){
$("#" + href).addClass("expanded").removeClass("collapsed").attr("title", __("Click to collapse this section") );
panel.show();
}
});
if ( to_highlight ) {
var words = to_highlight.split( ' ' );
$( '.prefs-tab table' ).find( 'td, th' ).not( '.name-cell' ).each( function ( i, td ) {
$.each( words, function ( i, word ) { $( td ).highlight( word ) } );
} ).find( 'option, textarea' ).removeHighlight();
}
if ( search_jumped ) {
document.location.hash = "jumped";
}
$("#pref_UpdateItemLocationOnCheckin").change(function(){
var the_text = $(this).val();
var alert_text = '';
if (the_text.indexOf('_ALL_:') != -1) alert_text = __("Note: _ALL_ value will override all other values") + '\n';
var split_text =the_text.split("\n");
var alert_issues = '';
var issue_count = 0;
var reg_check = /.*:\s.*/;
for (var i=0; i < split_text.length; i++){
if ( !split_text[i].match(reg_check) && split_text[i].length ) {
alert_issues+=split_text[i]+"\n";
issue_count++;
}
}
if (issue_count) alert_text += "\n" + __("The following values are not formatted correctly:") + "\n" + alert_issues;
if ( alert_text.length ) alert(alert_text);
});
$(".prefs-tab form").each(function () {
$(this).validate({
rules: { },
errorPlacement: function(error, element) {
var placement = $(element).parent();
if (placement) {
$(placement).append(error)
} else {
error.insertAfter(element);
}
}
});
});
$(".preference-email").each(function() {
$(this).rules("add", {
email: true
});
});
$(".modalselect").on("click", function(){
var datasource = $(this).data("source");
var pref_name = this.id.replace(/pref_/, '');
var pref_value = this.value;
var prefs = pref_value.split("|");
$.getJSON( themelang + "/modules/admin/preferences/" + datasource + ".json", function( data ){
var items = [];
var checked = "";
$.each( data, function( key, val ){
if( prefs.indexOf( val ) >= 0 ){
checked = ' checked="checked" ';
} else {
checked = "";
}
items.push('<label><input class="dbcolumn_selection" type="checkbox" id="' + key + '"' + checked + ' name="pref" value="' + val + '" /> ' + key + '</label>');
});
$("<div/>", {
"class": "columns-2",
html: items.join("")
}).appendTo("#prefModalForm");
});
$("#saveModalPrefs").data("target", this.id );
$("#prefModalLabel").text( pref_name );
$("#prefModal").modal("show");
});
$("#saveModalPrefs").on("click", function(){
var formfieldid = $("#" + $(this).data("target") );
var prefs = [];
$("#prefModal input[type='checkbox']").each(function(){
if( $(this).prop("checked") ){
prefs.push( this.value );
}
});
formfieldid.val( prefs.join("|") )
.addClass("modified");
mark_modified.call( formfieldid );
KOHA.Preferences.Save( formfieldid.closest("form") );
$("#prefModal").modal("hide");
});
$("#prefModal").on("hide.bs.modal", function(){
$("#prefModalLabel,#prefModalForm").html("");
$("#saveModalPrefs").data("target", "" );
});
$("#select_all").on("click",function(e){
e.preventDefault();
$(".dbcolumn_selection").prop("checked", true);
});
$("#clear_all").on("click",function(e){
e.preventDefault();
$(".dbcolumn_selection").prop("checked", false);
});
} );