From 0915d5808e3357b6beaf641a89d57e639e68a14c Mon Sep 17 00:00:00 2001
From: Owen Leonard <oleonard@myacpl.org>
Date: Fri, 1 Dec 2017 13:53:16 +0000
Subject: [PATCH] Bug 19726 - Move admin templates JavaScript to the footer:
 Preferences

This patch modifies the staff client's system preferences templates so
that JavaScript is included in the footer instead of the
header.

To test, apply the patch and test the JavaScript-driven features of
both the standard and local use system preferences:

 - WYSIWYG editor
 - Confirmation messages
 - Table sorting
 - Multiple selects (CoceProviders for example)
---
 .../prog/en/modules/admin/preferences.tt           |  93 +++++++------
 .../prog/en/modules/admin/systempreferences.tt     | 152 +++++++++++----------
 2 files changed, 123 insertions(+), 122 deletions(-)

diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/preferences.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/admin/preferences.tt
index 7d884a7..2c6d6b9 100644
--- a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/preferences.tt
+++ b/koha-tmpl/intranet-tmpl/prog/en/modules/admin/preferences.tt
@@ -1,58 +1,12 @@
 [% USE Koha %]
+[% SET footerjs = 1 %]
 [% INCLUDE 'doc-head-open.inc' %]
 <title>Koha &rsaquo; Administration &rsaquo; System preferences</title>
 [% INCLUDE 'doc-head-close.inc' %]
 <link rel="stylesheet" type="text/css" href="[% interface %]/[% theme %]/css/datatables.css" />
-[% INCLUDE 'datatables.inc' %]
 <link rel="stylesheet" type="text/css" href="[% interface %]/[% theme %]/css/preferences.css" />
-[% IF ( bidi ) %]
-   <link rel="stylesheet" type="text/css" href="[% interface %]/[% theme %]/css/right-to-left.css" />
-[% END %]
-<script type="text/javascript" src="[% interface %]/lib/jquery/plugins/jquery.fixFloat.js"></script>
-<script type="text/javascript" src="[% interface %]/lib/jquery/plugins/multiple-select/jquery.multiple.select.js"></script>
 <link rel="stylesheet" type="text/css" href="[% interface %]/lib/jquery/plugins/multiple-select/multiple-select.css" />
-<script type="text/javascript">
-//<![CDATA[
-    [% UNLESS ( searchfield ) %]$(document).ready(function(){
-            $('#toolbar').fixFloat();
-        });[% END %]
-
-    $(document).ready(function(){
-        $("select[multiple='multiple']").multipleSelect( {
-            placeholder: _("Please select ..."),
-            selectAllText: _("Select all"),
-            allSelected: _("All selected"),
-            countSelected: _("# of % selected"),
-            noMatchesFound: _("No matches found")
-        } );
-        $(".force_reload").on("click",function(e){
-            e.preventDefault();
-            window.location.reload(true);
-        });
-    });
-    // This is here because of its dependence on template variables, everything else should go in js/pages/preferences.js - jpw
-    var to_highlight = "[% searchfield |replace("'", "\'") |replace('"', '\"') |replace('\n', '\\n') |replace('\r', '\\r') |html %]";
-    var search_jumped = [% IF ( search_jumped ) %]true[% ELSE %]false[% END %];
-    var MSG_NOTHING_TO_SAVE = _("Nothing to save");
-    var MSG_SAVING = _("Saving...");
-    var MSG_SAVED_PREFERENCE = _("Saved preference %s");
-    var MSG_MODIFIED = _("modified");
-    var MSG_MADE_CHANGES = _("You have made changes to system preferences.");
-    var MSG_CLICK_TO_EXPAND = _("Click to expand this section");
-    var MSG_CLICK_TO_COLLAPSE = _("Click to collapse this section");
-    var MSG_INTERNAL_SERVER_ERROR = _( "Internal Server Error, please reload the page" );
-    var MSG_SESSION_TIMED_OUT = _( "You need to log in again, your session has timed out" );
-    var MSG_DATA_NOT_SAVED = _( "Error; your data might not have been saved" );
-    var MSG_LOADING = _( "Loading..." );
-
-//]]>
-</script>
 <link rel="stylesheet" type="text/css" href="[% interface %]/[% theme %]/css/humanmsg.css" />
-<script src="[% interface %]/lib/jquery/plugins/humanmsg.js" type="text/javascript"></script>
-<script src="[% interface %]/[% theme %]/js/ajax.js" type="text/javascript"></script>
-<script src="[% interface %]/[% theme %]/js/pages/preferences.js" type="text/javascript"></script>
-[%# Add WYSIWYG editor for htmlarea system preferences %]
-[% INCLUDE 'wysiwyg-systempreferences.inc' %]
 </head>
 <body id="admin_preferences" class="admin">
 [% INCLUDE 'header.inc' %]
@@ -195,4 +149,49 @@
 [% INCLUDE 'prefs-menu.inc' %]
 </div>
 </div>
+
+[% MACRO jsinclude BLOCK %]
+    [% INCLUDE 'datatables.inc' %]
+    <script type="text/javascript" src="[% interface %]/lib/jquery/plugins/jquery.fixFloat.js"></script>
+    <script type="text/javascript" src="[% interface %]/lib/jquery/plugins/multiple-select/jquery.multiple.select.js"></script>
+    <script type="text/javascript">
+        [% UNLESS ( searchfield ) %]$(document).ready(function(){
+                $('#toolbar').fixFloat();
+            });[% END %]
+
+        $(document).ready(function(){
+            $("select[multiple='multiple']").multipleSelect( {
+                placeholder: _("Please select ..."),
+                selectAllText: _("Select all"),
+                allSelected: _("All selected"),
+                countSelected: _("# of % selected"),
+                noMatchesFound: _("No matches found")
+            } );
+            $(".force_reload").on("click",function(e){
+                e.preventDefault();
+                window.location.reload(true);
+            });
+        });
+        // This is here because of its dependence on template variables, everything else should go in js/pages/preferences.js - jpw
+        var to_highlight = "[% searchfield |replace("'", "\'") |replace('"', '\"') |replace('\n', '\\n') |replace('\r', '\\r') |html %]";
+        var search_jumped = [% IF ( search_jumped ) %]true[% ELSE %]false[% END %];
+        var MSG_NOTHING_TO_SAVE = _("Nothing to save");
+        var MSG_SAVING = _("Saving...");
+        var MSG_SAVED_PREFERENCE = _("Saved preference %s");
+        var MSG_MODIFIED = _("modified");
+        var MSG_MADE_CHANGES = _("You have made changes to system preferences.");
+        var MSG_CLICK_TO_EXPAND = _("Click to expand this section");
+        var MSG_CLICK_TO_COLLAPSE = _("Click to collapse this section");
+        var MSG_INTERNAL_SERVER_ERROR = _( "Internal Server Error, please reload the page" );
+        var MSG_SESSION_TIMED_OUT = _( "You need to log in again, your session has timed out" );
+        var MSG_DATA_NOT_SAVED = _( "Error; your data might not have been saved" );
+        var MSG_LOADING = _( "Loading..." );
+    </script>
+    <script src="[% interface %]/lib/jquery/plugins/humanmsg.js" type="text/javascript"></script>
+    <script src="[% interface %]/[% theme %]/js/ajax.js" type="text/javascript"></script>
+    <script src="[% interface %]/[% theme %]/js/pages/preferences.js" type="text/javascript"></script>
+    [%# Add WYSIWYG editor for htmlarea system preferences %]
+    [% INCLUDE 'wysiwyg-systempreferences.inc' %]
+[% END %]
+
 [% INCLUDE 'intranet-bottom.inc' %]
diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/systempreferences.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/admin/systempreferences.tt
index 4f8f511..d018044 100644
--- a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/systempreferences.tt
+++ b/koha-tmpl/intranet-tmpl/prog/en/modules/admin/systempreferences.tt
@@ -1,85 +1,12 @@
 [% USE Koha %]
+[% SET footerjs = 1 %]
 [% INCLUDE 'doc-head-open.inc' %]
 <title>Koha &rsaquo; Administration &rsaquo; [% IF ( add_form ) %] System preferences &rsaquo; [% IF ( modify ) %]Modify system preference '[% searchfield %]'[% ELSE %]Add a system preference[% END %][% END %][% IF ( add_validate ) %] System preferences &rsaquo; Data added[% END %]
 [% IF ( delete_confirm ) %] System preferences &rsaquo; [% searchfield %] &rsaquo; Confirm deletion of parameter '[% searchfield %]'[% END %][% IF ( delete_confirmed ) %] System preferences &rsaquo; Parameter deleted[% END %][% IF ( else ) %]System preferences[% END %]</title>
 [% INCLUDE 'doc-head-close.inc' %]
-[%# Add WYSIWYG editor for htmlarea system preferences %]
-[% INCLUDE 'wysiwyg-systempreferences.inc' %]
-[% IF ( else ) %]
 <link rel="stylesheet" type="text/css" href="[% interface %]/[% theme %]/css/datatables.css" />
-[% INCLUDE 'datatables.inc' %]
-<script type="text/javascript">
-    //<![CDATA[
-     $(document).ready(function() {
-        [% IF ( loop ) %]$("#sysprefst").dataTable($.extend(true, {}, dataTablesDefaults, {
-            "sDom": '<"top pager"ilf>t',
-            "aoColumnDefs": [
-                { "aTargets": [ -1, -2, -3 ], "bSortable": false }
-            ],
-            "bPaginate": false
-        }));[% END %]
-     });
-</script>
-[% END %]
-<script type="text/javascript">
-//<![CDATA[
-[% IF ( add_form ) %]
-    $(document).ready(function() {
-        $('.variabletype').css({ color: "#0000CC", cursor: "pointer" });
-        $('.variabletype').click(function() {
-            $('#preftype').attr("value",$(this).attr("id"));
-        });
-    });
-[% END %]
-        // FIXME: many of these js functions appear unused
-        function isNotNull(f,noalert) {
-            if (f.value.length ==0) {
-    return false;
-            }
-            return true;
-        }
-        //
-        function isNum(v,maybenull) {
-        var n = new Number(v.value);
-        if (isNaN(n)) {
-            return false;
-            }
-        if (maybenull==0 && v.value=='') {
-            return false;
-        }
-        return true;
-        }
-        //
-        function Check(f) {
-            var ok=1;
-            var _alertString="";
-            var alertString2;
-            if (f.variable.value.length==0) {
-                _alertString += "\n- " + _("variable missing");
-            }
-            if (f.value.value.length==0) {
-                _alertString += "\n- " + _("value missing");
-            }
-            if (_alertString.length==0) {
-                document.Aform.submit();
-            } else {
-                alertString2  = _("Form not submitted because of the following problem(s)");
-                alertString2 += "\n------------------------------------------------------------------------------------\n";
-                alertString2 += _alertString;
-                alert(alertString2);
-            }
-        }
-        $( function() {
-            $( '#sysprefst .expand-textarea' ).show().click( function () {
-                $( this ).hide().nextAll( 'textarea, input[type=submit]' ).show( 'slow' );
-
-                return false;
-            } ).nextAll( 'textarea, input[type=submit]' ).hide();
-        } );
-
-        //]]>
-</script>
 </head>
+
 <body id="admin_systempreferences" class="admin">
 [% INCLUDE 'header.inc' %]
 [% INCLUDE 'prefs-admin-search.inc' %]
@@ -449,4 +376,79 @@
 [% INCLUDE 'prefs-menu.inc' %]
 </div>
 </div>
+
+[% MACRO jsinclude BLOCK %]
+    [%# Add WYSIWYG editor for htmlarea system preferences %]
+    [% INCLUDE 'wysiwyg-systempreferences.inc' %]
+    [% IF ( else ) %]
+        [% INCLUDE 'datatables.inc' %]
+        <script type="text/javascript">
+             $(document).ready(function() {
+                [% IF ( loop ) %]$("#sysprefst").dataTable($.extend(true, {}, dataTablesDefaults, {
+                    "sDom": '<"top pager"ilf>t',
+                    "aoColumnDefs": [
+                        { "aTargets": [ -1, -2, -3 ], "bSortable": false }
+                    ],
+                    "bPaginate": false
+                }));[% END %]
+             });
+        </script>
+    [% END %]
+    <script type="text/javascript">
+        [% IF ( add_form ) %]
+            $(document).ready(function() {
+                $('.variabletype').css({ color: "#0000CC", cursor: "pointer" });
+                $('.variabletype').click(function() {
+                    $('#preftype').attr("value",$(this).attr("id"));
+                });
+            });
+        [% END %]
+            // FIXME: many of these js functions appear unused
+        function isNotNull(f,noalert) {
+                if (f.value.length ==0) {
+        return false;
+                }
+                return true;
+        }
+
+        function isNum(v,maybenull) {
+            var n = new Number(v.value);
+            if (isNaN(n)) {
+                return false;
+                }
+            if (maybenull==0 && v.value=='') {
+                return false;
+            }
+            return true;
+        }
+
+        function Check(f) {
+            var ok=1;
+            var _alertString="";
+            var alertString2;
+            if (f.variable.value.length==0) {
+                _alertString += "\n- " + _("variable missing");
+            }
+            if (f.value.value.length==0) {
+                _alertString += "\n- " + _("value missing");
+            }
+            if (_alertString.length==0) {
+                document.Aform.submit();
+            } else {
+                alertString2  = _("Form not submitted because of the following problem(s)");
+                alertString2 += "\n------------------------------------------------------------------------------------\n";
+                alertString2 += _alertString;
+                alert(alertString2);
+            }
+        }
+        $( function() {
+            $( '#sysprefst .expand-textarea' ).show().click( function () {
+                $( this ).hide().nextAll( 'textarea, input[type=submit]' ).show( 'slow' );
+
+                return false;
+            } ).nextAll( 'textarea, input[type=submit]' ).hide();
+        });
+    </script>
+[% END %]
+
 [% INCLUDE 'intranet-bottom.inc' %]
-- 
2.1.4