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 3b71d6a..2a0e050 100644 (file)
@@ -1,10 +1,18 @@
-<script src="/intranet-tmpl/lib/codemirror/codemirror-compressed.js"></script>
-<script src="/intranet-tmpl/lib/filesaver.js"></script>
-<script src="/intranet-tmpl/lib/koha/cateditor/marc-mode.js"></script>
-<script src="/intranet-tmpl/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: '/intranet-tmpl/lib/koha/cateditor/',
+    baseUrl: '[% interface %]/lib/koha/cateditor/',
     config: {
         resources: {
             marcflavour: '[% marcflavour %]',
@@ -40,16 +48,16 @@ require( [ 'koha-backend', 'search', 'macros', 'marc-editor', 'marc-record', 'pr
 
     // The columns that should show up in a search, in order, and keyed by the corresponding <metadata> tag in the XSL and Pazpar2 config
     var z3950Labels = [
-               [ "local_number", _("Local number") ],
-               [ "title", _("Title") ],
-               [ "series", _("Series title") ],
-               [ "author", _("Author") ],
-               [ "lccn", _("LCCN") ],
-               [ "isbn", _("ISBN") ],
-               [ "issn", _("ISSN") ],
-               [ "medium", _("Medium") ],
-               [ "edition", _("Edition") ],
-               [ "notes", _("Notes") ],
+        [ "local_number", _("Local number") ],
+        [ "title", _("Title") ],
+        [ "series", _("Series title") ],
+        [ "author", _("Author") ],
+        [ "lccn", _("LCCN") ],
+        [ "isbn", _("ISBN") ],
+        [ "issn", _("ISSN") ],
+        [ "medium", _("Medium") ],
+        [ "edition", _("Edition") ],
+        [ "notes", _("Notes") ],
     ];
 
     var state = {
@@ -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>');
@@ -437,9 +456,24 @@ require( [ 'koha-backend', 'search', 'macros', 'marc-editor', 'marc-record', 'pr
 
         $('#searchresults thead tr').append('<th>' + _("Tools") + '</th>');
 
+        var bibnumMap = KohaBackend.GetSubfieldForKohaField('biblio.biblionumber');
         $.each( data.hits, function( undef, hit ) {
             backends.search.records[ hit.server + ':' + hit.index ] = hit.record;
-            hit.id = 'search/' + hit.server + ':' + hit.index;
+
+            switch ( hit.server ) {
+                case 'koha:biblioserver':
+                    var bibnumField = hit.record.field( bibnumMap[0] );
+
+                    if ( bibnumField && bibnumField.hasSubfield( bibnumMap[1] ) ) {
+                        hit.id = 'catalog/' + bibnumField.subfield( bibnumMap[1] );
+                        break;
+                    }
+
+                    // Otherwise, fallthrough
+
+                default:
+                    hit.id = 'search/' + hit.server + ':' + hit.index;
+            }
 
             var result = '<tr>';
             result += '<td class="sourcecol">' + z3950Servers[ hit.server ].name + '</td>';
@@ -455,7 +489,7 @@ require( [ 'koha-backend', 'search', 'macros', 'marc-editor', 'marc-record', 'pr
             } );
 
             result += '<td class="toolscol"><ul><li><a href="#" class="marc-link">' + _("View MARC") + '</a></li>';
-            result += '<li><a href="#" class="open-link">' + _("Import") + '</a></li>';
+            result += '<li><a href="#" class="open-link">' + ( hit.server == 'koha:biblioserver' ? _("Edit") : _("Import") ) + '</a></li>';
             if ( state.canSave ) result += '<li><a href="#" class="substitute-link" title="' + _("Replace the current record's contents") + '">' + _("Substitute") + '</a></li>';
             result += '</ul></td></tr>';
 
@@ -501,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++ ) {
@@ -513,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>' );
         }
 
-        if ( pages.length > 1 ) $( '#search-top-pages, #search-bottom-pages' ).find( '.pagination' ).html( '<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"));
@@ -544,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' } );
         }
     }
 
@@ -586,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);
             } );
         }
@@ -611,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;
         }
@@ -637,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 );
     }
@@ -648,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 ) {
@@ -670,7 +705,7 @@ require( [ 'koha-backend', 'search', 'macros', 'marc-editor', 'marc-record', 'pr
             var modified = macro.modified && new Date(macro.modified);
             $li.find( '.macro-info' ).append(
                 '<li><span class="label">' + _("Last changed:") + '</span>' +
-                ( modified ? modified.toLocaleFormat() : _("never") ) + '</li>'
+                ( modified ? ( modified.toLocaleDateString() + ', ' + modified.toLocaleTimeString() ) : _("never") ) + '</li>'
             );
             $('#macro-list').append($li);
         } );
@@ -721,7 +756,7 @@ require( [ 'koha-backend', 'search', 'macros', 'marc-editor', 'marc-record', 'pr
                     if ( !subfield ) return;
 
                     var subfieldinfo = taginfo.subfields[ subfield.code ];
-                    $('#status-subfield-info').html( '<strong>$' + subfield.code + ':</strong> ' );
+                    $('#status-subfield-info').html( '<strong>' + subfield.code + ':</strong> ' );
 
                     if ( subfieldinfo ) {
                         $('#status-subfield-info').append( subfieldinfo.lib );
@@ -737,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;
 
@@ -748,23 +783,24 @@ 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(
                 $('#macro-editor')[0],
                 {
+                    extraKeys: {
+                        'Ctrl-D': function( cm ) {
+                            var cur = cm.getCursor();
+
+                            cm.replaceRange( "‡", cur, null );
+                        },
+                    },
                     mode: 'null',
                     lineNumbers: true,
+                    readOnly: true,
                 }
             );
             var saveTimeout;
@@ -804,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' ) {
@@ -833,19 +869,22 @@ require( [ 'koha-backend', 'search', 'macros', 'marc-editor', 'marc-record', 'pr
                             if ( error.subfield == '@' ) {
                                 editor.addError( error.line, _("Missing control field contents") );
                             } else {
-                                editor.addError( error.line, _("Missing mandatory subfield: $") + error.subfield );
+                                editor.addError( error.line, _("Missing mandatory subfield: ") + error.subfield );
                             }
                             break;
                         case 'unrepeatableTag':
                             editor.addError( error.line, _("Tag ") + error.tag + _(" cannot be repeated") );
                             break;
                         case 'unrepeatableSubfield':
-                            editor.addError( error.line, _("Subfield $") + error.subfield + _(" cannot be repeated") );
+                            editor.addError( error.line, _("Subfield ‡") + error.subfield + _(" cannot be repeated") );
+                            break;
+                        case 'itemTagUnsupported':
+                            editor.addError( error.line, _("Item tags cannot currently be saved") );
                             break;
                     }
                 } );
 
-                $( '#save-record' ).find('i').attr( 'class', 'icon-hdd' );
+                $( '#save-record' ).find('i').attr( 'class', 'fa fa-hdd-o' );
 
                 if ( result.error ) {
                     // Reset backend info
@@ -884,6 +923,8 @@ require( [ 'koha-backend', 'search', 'macros', 'marc-editor', 'marc-record', 'pr
                             return;
                         }
 
+                        if (record.marc8_corrupted) humanMsg.displayMsg( '<h3>' + _("Possible record corruption") + '</h3><p>' + _("Record not marked as UTF-8, may be corrupted") + '</p>', { className: 'humanError' } );
+
                         editor.displayRecord( record );
                     };
 
@@ -905,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;
             }
 
@@ -920,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;
         } );
@@ -938,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;
@@ -962,13 +1004,14 @@ require( [ 'koha-backend', 'search', 'macros', 'marc-editor', 'marc-record', 'pr
         } );
 
         $( document ).on( 'click', 'a.search-nav', function() {
-            $("#search-overlay").show();
-            Search.Fetch( { offset: $( this ).data( 'offset' ) } );
+            if ( Search.Fetch( { offset: $( this ).data( 'offset' ) } ) ) {
+                $("#search-overlay").show();
+            }
+
             return false;
         });
 
         $( document ).on( 'click', 'th[data-sort-label]', function() {
-            $("#search-overlay").show();
             var direction;
 
             if ( $( this ).hasClass( 'sorting_asc' ) ) {
@@ -977,9 +1020,12 @@ require( [ 'koha-backend', 'search', 'macros', 'marc-editor', 'marc-record', 'pr
                 direction = 'asc';
             }
 
-            showSearchSorting( $( this ).data( 'sort-label' ), direction );
+            if ( Search.Fetch( { sort_key: $( this ).data( 'sort-label' ), sort_direction: direction } ) ) {
+                showSearchSorting( $( this ).data( 'sort-label' ), direction );
+
+                $("#search-overlay").show();
+            }
 
-            Search.Fetch( { sort_key: $( this ).data( 'sort-label' ), sort_direction: direction } );
             return false;
         });
 
@@ -987,9 +1033,8 @@ require( [ 'koha-backend', 'search', 'macros', 'marc-editor', 'marc-record', 'pr
             var server = z3950Servers[ $( this ).closest('li').data('server-id') ];
             server.checked = this.checked;
 
-            if ( $('#search-results-ui').is( ':visible' ) ) {
+            if ( $('#search-results-ui').is( ':visible' ) && Search.Fetch() ) {
                 $("#search-overlay").show();
-                Search.Fetch();
             }
         } );
 
@@ -1024,13 +1069,31 @@ require( [ 'koha-backend', 'search', 'macros', 'marc-editor', 'marc-record', 'pr
                 return '<div id="alerts-container"><ul>' + prevAlerts.join('') + '</ul></div>';
             },
         });
+
+        $('#show-shortcuts').popover({
+            html: true,
+            placement: 'bottom',
+            content: function() {
+                return '<div id="shortcuts-container">' + $('#shortcuts-contents').html() + '</div>';
+            },
+        });
+
         $('#new-record' ).click( function() {
+            if ( editor.modified && !confirm( _("Are you sure you want to erase your changes?") ) ) return;
+
             openRecord( 'new/', editor );
             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( {
@@ -1044,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();
             } );
         }