Bug 19502: (follow-up) Pass parameters to avoid making templates depend on search...
[koha.git] / koha-tmpl / intranet-tmpl / prog / en / includes / cateditor-ui.inc
index b5c2c23..2a0e050 100644 (file)
@@ -1,8 +1,16 @@
-<script src="[% interface %]/lib/codemirror/codemirror-compressed.js"></script>
-<script src="[% interface %]/lib/filesaver.js"></script>
-<script src="[% interface %]/lib/koha/cateditor/marc-mode.js"></script>
-<script src="[% interface %]/lib/require.js"></script>
+[% Asset.js("lib/codemirror/codemirror-compressed.js") %]
+[% Asset.js("lib/filesaver.js") %]
+[% Asset.js("lib/koha/cateditor/marc-mode.js") %]
+[% Asset.js("lib/require.js") %]
 <script>
+    var authInfo = {
+        [%- FOREACH authtag = authtags -%]
+            [% authtag.tagfield %]: {
+                subfield: '[% authtag.tagsubfield %]',
+                authtypecode: '[% authtag.authtypecode %]',
+                },
+        [%- END -%]
+    };
 require.config( {
     baseUrl: '[% interface %]/lib/koha/cateditor/',
     config: {
@@ -77,12 +85,21 @@ require( [ 'koha-backend', 'search', 'macros', 'marc-editor', 'marc-record', 'pr
                         return $result[0];
                     },
                     postCreate: function() {
-                        this.setText( defaultvalue );
+                        var value = defaultvalue;
+                        var widget = this;
+
+                        $.each( authvals, function() {
+                            if ( this.value == widget.text ) {
+                                value = this.value;
+                            }
+                        } );
+
+                        this.setText( value );
 
                         $( '<select></select>' ).appendTo( this.node );
                         var $node = $( this.node ).find( 'select' );
                         $.each( authvals, function( undef, authval ) {
-                            $node.append( '<option value="' + authval.value + '"' + (authval.value == defaultvalue ? ' selected="selected"' : '') + '>' + authval.lib + '</option>' );
+                            $node.append( '<option value="' + authval.value + '"' + (authval.value == value ? ' selected="selected"' : '') + '>' + authval.lib + '</option>' );
                         } );
                         $node.val( this.text );
 
@@ -163,6 +180,8 @@ require( [ 'koha-backend', 'search', 'macros', 'marc-editor', 'marc-record', 'pr
         [% IF ( marcflavour == 'MARC21' ) %]
             if ( tag == '000' ) {
                 return "http://www.loc.gov/marc/bibliographic/bdleader.html";
+            } else if ( tag >= '090' && tag < '100' ) {
+                return "http://www.loc.gov/marc/bibliographic/bd09x.html";
             } else if ( tag < '900' ) {
                 return "http://www.loc.gov/marc/bibliographic/bd" + tag + ".html";
             } else {
@@ -376,7 +395,7 @@ require( [ 'koha-backend', 'search', 'macros', 'marc-editor', 'marc-record', 'pr
     }
 
     function showResultsBox(data) {
-        $('#search-top-pages, #search-bottom-pages').find('.pagination').empty();
+        $('#search-top-pages, #search-bottom-pages').find('nav').empty();
         $('#searchresults thead tr').empty();
         $('#searchresults tbody').empty();
         $('#search-serversinfo').empty().append('<li>' + _("Loading...") + '</li>');
@@ -516,7 +535,7 @@ require( [ 'koha-backend', 'search', 'macros', 'marc-editor', 'marc-record', 'pr
         var max_page = Math.ceil( data.total_fetched / data.page_size ) - 1;
 
         if ( cur_page != 0 ) {
-            pages.push( '<li><a class="search-nav" href="#" data-offset="' + (data.offset - data.page_size) + '">&laquo; ' + _("Previous") + '</a></li>' );
+            pages.push( '<li><a class="search-nav" href="#" data-offset="' + (data.offset - data.page_size) + '"><span aria-hidden="true">&laquo;</span> ' + _("Previous") + '</a></li>' );
         }
 
         for ( var page = Math.max( 0, cur_page - 9 ); page <= Math.min( max_page, cur_page + 9 ); page++ ) {
@@ -528,10 +547,10 @@ require( [ 'koha-backend', 'search', 'macros', 'marc-editor', 'marc-record', 'pr
         }
 
         if ( cur_page < max_page ) {
-            pages.push( ' <li><a class="search-nav" href="#" data-offset="' + (data.offset + data.page_size) + '">' + _("Next") + ' &raquo;</a></li>' );
+            pages.push( ' <li><a class="search-nav" href="#" data-offset="' + (data.offset + data.page_size) + '">' + _("Next") + ' <span aria-hidden="true">&raquo;</span></a></li>' );
         }
 
-        $( '#search-top-pages, #search-bottom-pages' ).find( '.pagination' ).html( pages.length > 1 ? ( '<ul>' + pages.join( '' ) + '</ul>' ) : '' );
+        $( '#search-top-pages, #search-bottom-pages' ).find( 'nav' ).html( pages.length > 1 ? ( '<ul class="pagination pagination-sm">' + pages.join( '' ) + '</ul>' ) : '' );
 
         var $overlay = $('#search-overlay');
         $overlay.find('span').text(_("Loading"));
@@ -559,7 +578,7 @@ require( [ 'koha-backend', 'search', 'macros', 'marc-editor', 'marc-record', 'pr
             invalidateSearchResults();
             Search.Reconnect();
         } else {
-            humanMsg.displayMsg( _("<h3>Internal search error</h3>") + '<p>' + error + '</p>' + _("<p>Please <b>refresh</b> the page and try again."), { className: 'humanError' } );
+            humanMsg.displayMsg( '<h3>' + _("Internal search error") + '</h3><p>' + error + '</p><p>' + _("Please refresh the page and try again.") + '</p>', { className: 'humanError' } );
         }
     }
 
@@ -601,7 +620,7 @@ require( [ 'koha-backend', 'search', 'macros', 'marc-editor', 'marc-record', 'pr
             $( sel ).on( event, function (e) {
                 e.preventDefault();
                 handler( e, Preferences.user[pref] );
-                Preferences.Save( [% USER_INFO.0.borrowernumber %] );
+                Preferences.Save( [% USER_INFO.borrowernumber %] );
                 showPreference(pref);
             } );
         }
@@ -626,7 +645,7 @@ require( [ 'koha-backend', 'search', 'macros', 'marc-editor', 'marc-record', 'pr
                 $( document ).on( 'change', 'input.search-toggle-server', function() {
                     var server_id = $( this ).closest('li').data('server-id');
                     Preferences.user.selected_search_targets[server_id] = this.checked;
-                    Preferences.Save( [% USER_INFO.0.borrowernumber %] );
+                    Preferences.Save( [% USER_INFO.borrowernumber %] );
                 } );
                 break;
         }
@@ -652,6 +671,7 @@ require( [ 'koha-backend', 'search', 'macros', 'marc-editor', 'marc-record', 'pr
         $( '#macro-list li[data-name="' + name + '"]' ).addClass( 'active' );
         var macro = Preferences.user.macros[name];
         macroEditor.setValue( macro.contents );
+        macroEditor.setOption( 'readOnly', false );
         $( '#macro-format' ).val( macro.format || 'its' );
         if ( macro.history ) macroEditor.setHistory( macro.history );
     }
@@ -663,7 +683,7 @@ require( [ 'koha-backend', 'search', 'macros', 'marc-editor', 'marc-record', 'pr
             delete Preferences.user.macros[name];
         }
 
-        Preferences.Save( [% USER_INFO.0.borrowernumber %] );
+        Preferences.Save( [% USER_INFO.borrowernumber %] );
     }
 
     function showSavedMacros( macros ) {
@@ -752,7 +772,7 @@ require( [ 'koha-backend', 'search', 'macros', 'marc-editor', 'marc-record', 'pr
 
         // Automatically detect resizes and change the height of the editor and position of modals.
         var resizeTimer = null;
-        $( window ).resize( function() {
+        function onResize() {
             if ( resizeTimer == null ) resizeTimer = setTimeout( function() {
                 resizeTimer = null;
 
@@ -763,16 +783,9 @@ require( [ 'koha-backend', 'search', 'macros', 'marc-editor', 'marc-record', 'pr
                     $(this).height( $(window).height() * .8 - $(this).prevAll('.modal-header').height() );
                 } );
             }, 100);
+        }
 
-            $("#advanced-search-ui, #search-results-ui, #macro-ui").css( {
-                marginLeft: function() {
-                    return -($(this).width() / 2);
-                }
-            } );
-
-        } ).resize();
-
-        $( '#macro-ui' ).on( 'shown', function() {
+        $( '#macro-ui' ).on( 'shown.bs.modal', function() {
             if ( macroEditor ) return;
 
             macroEditor = CodeMirror(
@@ -787,6 +800,7 @@ require( [ 'koha-backend', 'search', 'macros', 'marc-editor', 'marc-record', 'pr
                     },
                     mode: 'null',
                     lineNumbers: true,
+                    readOnly: true,
                 }
             );
             var saveTimeout;
@@ -826,7 +840,7 @@ require( [ 'koha-backend', 'search', 'macros', 'marc-editor', 'marc-record', 'pr
 
         // Click bindings
         $( '#save-record, #save-dropdown a' ).click( function() {
-            $( '#save-record' ).find('i').attr( 'class', 'icon-loading' ).siblings( 'span' ).text( _("Saving...") );
+            $( '#save-record' ).find('i').attr( 'class', 'fa fa-spinner' ).siblings( 'span' ).text( _("Saving...") );
 
             function finishCb(result) {
                 if ( result.error == 'syntax' ) {
@@ -870,7 +884,7 @@ require( [ 'koha-backend', 'search', 'macros', 'marc-editor', 'marc-record', 'pr
                     }
                 } );
 
-                $( '#save-record' ).find('i').attr( 'class', 'icon-hdd' );
+                $( '#save-record' ).find('i').attr( 'class', 'fa fa-hdd-o' );
 
                 if ( result.error ) {
                     // Reset backend info
@@ -932,6 +946,7 @@ require( [ 'koha-backend', 'search', 'macros', 'marc-editor', 'marc-record', 'pr
 
             if ( !result.errors.length ) {
                 $('#macro-ui').modal('hide');
+                editor.focus(); //Return cursor to editor after macro run
                 return false;
             }
 
@@ -947,7 +962,7 @@ require( [ 'koha-backend', 'search', 'macros', 'marc-editor', 'marc-record', 'pr
                 errors.push(error);
             } );
 
-            humanMsg.displayMsg( _("<h3>Failed to run macro:</h3>") + '<ul><li>' + errors.join('</li><li>') + '</li></ul>', { className: 'humanError' } );
+            humanMsg.displayMsg( '<h3>' + _("Failed to run macro:") + '</h3><ul><li>' + errors.join('</li><li>') + '</li></ul>', { className: 'humanError' } );
 
             return false;
         } );
@@ -965,7 +980,7 @@ require( [ 'koha-backend', 'search', 'macros', 'marc-editor', 'marc-record', 'pr
         $( '#switch-editor' ).click( function() {
             if ( !confirm( _("Any changes will not be saved. Continue?") ) ) return;
 
-            $.cookie( 'catalogue_editor_[% USER_INFO.0.borrowernumber %]', 'basic', { expires: 365, path: '/' } );
+            $.cookie( 'catalogue_editor_[% USER_INFO.borrowernumber %]', 'basic', { expires: 365, path: '/' } );
 
             if ( state.backend == 'catalog' ) {
                 window.location = '/cgi-bin/koha/cataloguing/addbiblio.pl?biblionumber=' + state.recordID;
@@ -1070,8 +1085,15 @@ require( [ 'koha-backend', 'search', 'macros', 'marc-editor', 'marc-record', 'pr
             return false;
         } );
 
+        window.onbeforeunload = function() {
+            if(editor.modified )
+                { return 1; }
+            else
+                { return undef; }
+        };
+
         // Start editor
-        Preferences.Load( [% USER_INFO.0.borrowernumber || 0 %] );
+        Preferences.Load( [% USER_INFO.borrowernumber || 0 %] );
         displayPreferences(editor);
         makeAuthorisedValueWidgets( '' );
         Search.Init( {
@@ -1085,6 +1107,7 @@ require( [ 'koha-backend', 'search', 'macros', 'marc-editor', 'marc-record', 'pr
 
             Resources.GetAll().done( function() {
                 $("#loading").hide();
+                $( window ).resize( onResize ).resize();
                 editor.focus();
             } );
         }