1 /* global KOHA CodeMirror to_highlight search_jumped humanMsg dataTablesDefaults themelang */
2 // We can assume 'KOHA' exists, as we depend on KOHA.AJAX
5 Save: function ( form ) {
6 if ( ! $(form).valid() ) {
7 humanMsg.displayAlert( __("Error: presence of invalid data prevent saving. Please make the corrections and try again.") );
11 modified_prefs = $( form ).find( '.modified' );
12 // $.serialize removes empty value, we need to keep them.
13 // If a multiple select has all its entries unselected
14 var unserialized = new Array();
15 $(modified_prefs).each(function(){
16 if ( $(this).attr('multiple') && $(this).val() == null ) {
17 unserialized.push($(this));
20 data = modified_prefs.serialize();
21 $(unserialized).each(function(){
22 data += '&' + $(this).attr('name') + '=';
25 humanMsg.displayAlert( __("Nothing to save") );
28 KOHA.AJAX.MarkRunning($(form).find('.save-all'), __("Saving...") );
31 url: '/cgi-bin/koha/svc/config/systempreferences/',
32 success: function ( data ) { KOHA.Preferences.Success( form ) },
33 complete: function () { KOHA.AJAX.MarkDone( $( form ).find( '.save-all' ) ) }
36 Success: function ( form ) {
38 modified_prefs.each(function(){
39 var modified_pref = $(this).attr("id");
40 modified_pref = modified_pref.replace("pref_","");
41 msg += "<strong>" + __("Saved preference %s").format(modified_pref) + "</strong>\n";
43 humanMsg.displayAlert(msg);
46 .find( '.modified-warning' ).remove().end()
47 .find( '.modified' ).removeClass('modified');
48 KOHA.Preferences.Modified = false;
52 function mark_modified() {
53 $( this.form ).find( '.save-all' ).prop('disabled', false);
54 $( this ).addClass( 'modified' );
55 var name_cell = $( this ).parents( '.name-row' ).find( '.name-cell' );
56 if ( !name_cell.find( '.modified-warning' ).length )
57 name_cell.append('<em class="modified-warning">(' + __("modified") + ')</em>');
58 KOHA.Preferences.Modified = true;
61 window.onbeforeunload = function () {
62 if ( KOHA.Preferences.Modified ) {
63 return __("You have made changes to system preferences.");
67 $( document ).ready( function () {
69 $("table.preferences").dataTable($.extend(true, {}, dataTablesDefaults, {
72 { "aTargets": [ -1 ], "bSortable": false, "bSearchable": false }
78 .find( 'input.preference, textarea.preference' ).on('input', function () {
79 if ( this.defaultValue === undefined || this.value != this.defaultValue ) mark_modified.call( this );
81 .find( 'select.preference' ).change( mark_modified );
82 $('.preference-checkbox').change( function () {
83 $('.preference-checkbox').addClass('modified');
84 mark_modified.call(this);
87 $(".set_syspref").click(function() {
88 var s = $(this).attr('data-syspref');
89 var v = $(this).attr('data-value');
90 // populate the input with the value in data-value
92 // pass the DOM element to trigger "modified" to enable submit button
93 mark_modified.call($("#pref_"+s)[0]);
97 $(".sortable").sortable();
98 $(".sortable").on( "sortchange", function( event, ui ) {
99 // This is not exact but we just need to trigger a change
100 $(ui.item.find('input:first')).change();
103 $( '.prefs-tab .action .cancel' ).click( function () { KOHA.Preferences.Modified = false } );
105 $( '.prefs-tab .save-all' ).prop('disabled', true).click( function () {
106 KOHA.Preferences.Save( this.form );
110 $( ".expand-textarea" ).on("click", function(e){
113 var target = $(this).data("target");
114 var syntax = $(this).data("syntax");
115 $("#collapse_" + target ).show();
117 var editor = CodeMirror.fromTextArea( document.getElementById( "pref_" + target ), {
121 viewportMargin: Infinity,
122 gutters: ["CodeMirror-lint-markers"],
125 editor.on("change", function(){
126 mark_modified.call( $("#pref_" + target )[0]);
128 editor.on("blur", function(){
132 $("#pref_" + target ).show();
136 $( ".collapse-textarea" ).on("click", function(e){
139 var target = $(this).data("target");
140 var syntax = $(this).data("syntax");
141 $("#expand_" + target ).show();
143 var editor = $("#pref_" + target ).next(".CodeMirror")[0].CodeMirror;
146 $("#pref_" + target ).hide();
149 $("h3").attr("class", "expanded").attr("title", __("Click to collapse this section"));
150 var collapsible = $(".collapsed,.expanded");
152 $(collapsible).on("click",function(){
153 var h3Id = $(this).attr("id");
154 var panel = $("#collapse_" + h3Id);
155 if(panel.is(":visible")){
156 $(this).addClass("collapsed").removeClass("expanded").attr("title", __("Click to expand this section") );
159 $(this).addClass("expanded").removeClass("collapsed").attr("title", __("Click to collapse this section") );
164 $(".pref_sublink").on("click", function(){
165 /* If the user clicks a sub-menu link in the sidebar,
166 check to see if it is collapsed. If so, expand it */
167 var href = $(this).attr("href");
168 href = href.replace("#","");
169 var panel = $("#collapse_" + href );
170 if( panel.is(":hidden") ){
171 $("#" + href).addClass("expanded").removeClass("collapsed").attr("title", __("Click to collapse this section") );
176 if ( to_highlight ) {
177 var words = to_highlight.split( ' ' );
178 $( '.prefs-tab table' ).find( 'td, th' ).not( '.name-cell' ).each( function ( i, td ) {
179 $.each( words, function ( i, word ) { $( td ).highlight( word ) } );
180 } ).find( 'option, textarea' ).removeHighlight();
183 if ( search_jumped ) {
184 document.location.hash = "jumped";
187 $("#pref_UpdateItemLocationOnCheckin").change(function(){
188 var the_text = $(this).val();
190 if (the_text.indexOf('_ALL_:') != -1) alert_text = __("Note: _ALL_ value will override all other values") + '\n';
191 var split_text =the_text.split("\n");
192 var alert_issues = '';
194 var reg_check = /.*:\s.*/;
195 for (var i=0; i < split_text.length; i++){
196 if ( !split_text[i].match(reg_check) && split_text[i].length ) {
197 alert_issues+=split_text[i]+"\n";
201 if (issue_count) alert_text += "\n" + __("The following values are not formatted correctly:") + "\n" + alert_issues;
202 if ( alert_text.length ) alert(alert_text);
205 $(".prefs-tab form").each(function () {
208 errorPlacement: function(error, element) {
209 var placement = $(element).parent();
211 $(placement).append(error)
213 error.insertAfter(element);
219 $(".preference-email").each(function() {
220 $(this).rules("add", {
226 $(".modalselect").on("click", function(){
227 var datasource = $(this).data("source");
228 var exclusions = $(this).data("exclusions").split('|');
229 var required = $(this).data("required").split('|');
230 var pref_name = this.id.replace(/pref_/, '');
231 var pref_value = this.value;
232 var prefs = pref_value.split("|");
234 let data = db_columns[datasource];
240 $.each( Object.keys(data).sort(), function( i, key ){
241 if( prefs.indexOf( key ) >= 0 ){
242 checked = ' checked="checked" ';
246 if( required.indexOf( key ) >= 0 ){
248 checked = ' checked="checked" ';
249 } else if( exclusions.indexOf( key ) >= 0 ){
251 disabled = ' disabled="disabled" ';
257 items.push('<label class="' + style +'"><input class="dbcolumn_selection" type="checkbox" id="' + key + '"' + checked + disabled + ' name="pref" value="' + key + '" /> ' + data[key]+ ' (' + key + ')</label>');
260 "class": "columns-2",
262 }).appendTo("#prefModalForm");
264 $("#saveModalPrefs").data("target", this.id );
265 $("#prefModalLabel").text( pref_name );
266 $("#prefModal").modal("show");
269 $("#saveModalPrefs").on("click", function(){
270 var formfieldid = $("#" + $(this).data("target") );
272 $("#prefModal input[type='checkbox']").each(function(){
273 if( $(this).prop("checked") ){
274 prefs.push( this.value );
278 formfieldid.val( prefs.join("|") )
279 .addClass("modified");
280 mark_modified.call( formfieldid );
281 KOHA.Preferences.Save( formfieldid.closest("form") );
282 $("#prefModal").modal("hide");
285 $("#prefModal").on("hide.bs.modal", function(){
286 $("#prefModalLabel,#prefModalForm").html("");
287 $("#saveModalPrefs").data("target", "" );
290 $("#select_all").on("click",function(e){
292 $("label:not(.required) .dbcolumn_selection:not(:disabled)").prop("checked", true);
294 $("#clear_all").on("click",function(e){
296 $("label:not(.required) .dbcolumn_selection").prop("checked", false);
299 $("body").on("click", "label.required input.dbcolumn_selection", function(e){