|
Lines 693-772
require( [ 'koha-backend', 'search', 'macros', 'marc-editor', 'marc-record', 'pr
Link Here
|
| 693 |
} |
693 |
} |
| 694 |
|
694 |
|
| 695 |
//> Macro functions |
695 |
//> Macro functions |
| 696 |
function loadMacro( name ) { |
696 |
var canCreatePublic = "[% CAN_user_editcatalogue_create_shared_macros | html %]"; |
|
|
697 |
var canDeletePublic = "[% CAN_user_editcatalogue_delete_shared_macros | html %]"; |
| 698 |
|
| 699 |
function deleteMacro( id ){ |
| 700 |
$( '#macro-list' ).empty(); |
| 701 |
var shared = macroEditor.activeMacroShared; |
| 702 |
var id = macroEditor.activeMacroId; |
| 703 |
macroEditor.activeMacroId = null; |
| 704 |
api_url = "/api/v1/advancededitormacros/"; |
| 705 |
if( shared ) { api_url += "shared/" } |
| 706 |
let options = { |
| 707 |
url: api_url + id, |
| 708 |
method: "DELETE", |
| 709 |
contentType: "application/json", |
| 710 |
}; |
| 711 |
$.ajax(options) |
| 712 |
.then(function(result) { |
| 713 |
humanMsg.displayAlert( _("Macro successfully deleted") ); |
| 714 |
showSavedMacros(); |
| 715 |
}) |
| 716 |
.fail(function(err) { |
| 717 |
humanMsg.displayAlert( _("Failed to delete macro:") + err.responseText, { className: 'humanError' } ); |
| 718 |
}); |
| 719 |
} |
| 720 |
|
| 721 |
|
| 722 |
function loadMacro( name, id, shared ) { |
| 697 |
$( '#macro-list li' ).removeClass( 'active' ); |
723 |
$( '#macro-list li' ).removeClass( 'active' ); |
|
|
724 |
$(".macro_shared").prop("checked",false).hide(); |
| 725 |
$("#delete-macro").prop("disabled",true); |
| 726 |
macroEditor.setOption( 'readOnly', false ); |
| 698 |
macroEditor.activeMacro = name; |
727 |
macroEditor.activeMacro = name; |
|
|
728 |
macroEditor.activeMacroId = id; |
| 699 |
|
729 |
|
| 700 |
if ( !name ) { |
730 |
if ( !name ) { |
| 701 |
macroEditor.setValue( '' ); |
731 |
macroEditor.setValue( '' ); |
| 702 |
return; |
732 |
return; |
| 703 |
} |
733 |
} |
|
|
734 |
$( '#macro-list li[data-name="' + name + '"][data-id="' + id + '"]' ).addClass( 'active' ); |
| 735 |
api_url = "/api/v1/advancededitormacros/"; |
| 736 |
if( shared ) { api_url += "shared/" } |
| 737 |
let options = { |
| 738 |
url: api_url + id, |
| 739 |
method: "GET", |
| 740 |
contentType: "application/json", |
| 741 |
}; |
| 742 |
$.ajax(options) |
| 743 |
.then(function(result) { |
| 744 |
macroEditor.setValue( result.macro_text ); |
| 745 |
$(".macro_shared").show(); |
| 746 |
if( result.shared ){ |
| 747 |
$(".macro_shared").prop("checked",true); |
| 748 |
if( canCreatePublic ){ |
| 749 |
macroEditor.setOption( 'readOnly', false ); |
| 750 |
} else { |
| 751 |
macroEditor.setOption( 'readOnly', true ); |
| 752 |
} |
| 753 |
if( canDeletePublic ){ |
| 754 |
$("#delete-macro").prop("disabled",false); |
| 755 |
} |
| 756 |
} else { |
| 757 |
macroEditor.setOption( 'readOnly', false ); |
| 758 |
$("#delete-macro").prop("disabled",false); |
| 759 |
} |
| 760 |
macroEditor.activeMacroShared = result.shared; |
| 761 |
}) |
| 762 |
.fail(function(err) { |
| 763 |
humanMsg.displayAlert( _("Failed to load macros:") + err.responseText, { className: 'humanError' } ); |
| 764 |
}); |
| 704 |
|
765 |
|
| 705 |
$( '#macro-list li[data-name="' + name + '"]' ).addClass( 'active' ); |
|
|
| 706 |
var macro = Preferences.user.macros[name]; |
| 707 |
macroEditor.setValue( macro.contents ); |
| 708 |
macroEditor.setOption( 'readOnly', false ); |
| 709 |
if ( macro.history ) macroEditor.setHistory( macro.history ); |
| 710 |
} |
| 711 |
|
| 712 |
function storeMacro( name, macro ) { |
| 713 |
if ( macro ) { |
| 714 |
Preferences.user.macros[name] = macro; |
| 715 |
} else { |
| 716 |
delete Preferences.user.macros[name]; |
| 717 |
} |
| 718 |
|
| 719 |
Preferences.Save( [% logged_in_user.borrowernumber | html %] ); |
| 720 |
} |
766 |
} |
| 721 |
|
767 |
|
| 722 |
function showSavedMacros( macros ) { |
768 |
function convertOldMacros(){ |
| 723 |
var scrollTop = $('#macro-list').scrollTop(); |
769 |
$("#convert-macros").remove(); |
| 724 |
$( '#macro-list' ).empty(); |
|
|
| 725 |
var macro_list = $.map( Preferences.user.macros, function( macro, name ) { |
770 |
var macro_list = $.map( Preferences.user.macros, function( macro, name ) { |
| 726 |
return $.extend( { name: name }, macro ); |
771 |
return $.extend( { name: name }, macro ); |
| 727 |
} ); |
772 |
} ); |
| 728 |
macro_list.sort( function( a, b ) { |
773 |
macro_list.sort( function( a, b ) { |
| 729 |
return a.name.localeCompare(b.name); |
774 |
return a.name.localeCompare(b.name); |
| 730 |
} ); |
775 |
} ); |
| 731 |
$.each( macro_list, function( undef, macro ) { |
776 |
$.each( macro_list, function( index, macro ) { |
| 732 |
var $li = $( '<li data-name="' + macro.name + '"><a href="#">' + macro.name + '</a><ol class="macro-info"></ol></li>' ); |
777 |
let options = { |
| 733 |
$li.click( function() { |
778 |
url: "/api/v1/advancededitormacros/", |
| 734 |
loadMacro(macro.name); |
779 |
method: "POST", |
| 735 |
return false; |
780 |
contentType: "application/json", |
| 736 |
} ); |
781 |
data: JSON.stringify({ |
| 737 |
if ( macro.name == macroEditor.activeMacro ) $li.addClass( 'active' ); |
782 |
name: macro.name, |
| 738 |
var modified = macro.modified && new Date(macro.modified); |
783 |
patron_id: [% logged_in_user.borrowernumber | html %], |
| 739 |
$li.find( '.macro-info' ).append( |
784 |
macro_text: macro.contents, |
| 740 |
'<li><span class="label">' + _("Last changed:") + '</span>' + |
785 |
shared: false |
| 741 |
( modified ? ( modified.toLocaleDateString() + ', ' + modified.toLocaleTimeString() ) : _("never") ) + '</li>' |
786 |
}) |
| 742 |
); |
787 |
}; |
| 743 |
$('#macro-list').append($li); |
788 |
$.ajax(options) |
|
|
789 |
.then(function(undef, result) { |
| 790 |
delete Preferences.user.macros[macro.name]; |
| 791 |
Preferences.Save( [% logged_in_user.borrowernumber | html %] ); |
| 792 |
if( index == macro_list.length -1 ){ |
| 793 |
showSavedMacros(); |
| 794 |
} |
| 795 |
|
| 796 |
}) |
| 797 |
.fail(function(err) { |
| 798 |
humanMsg.displayAlert( _("Failed to create macro:") + err.responseText, { className: 'humanError' } ); |
| 799 |
}); |
| 744 |
} ); |
800 |
} ); |
|
|
801 |
} |
| 802 |
|
| 803 |
function showSavedMacros( macros ) { |
| 804 |
var scrollTop = $('#macro-list').scrollTop(); |
| 805 |
$( '#macro-list' ).empty(); |
| 806 |
$("#convert-macros").remove(); |
| 807 |
if( Object.keys(Preferences.user.macros).length ){ |
| 808 |
$convert = $( '<button class="btn btn-default" id="convert-macros" title="Convert browser storage macros"><i class="fa fa-adjust"></i> Convert old macros</button>' ); |
| 809 |
$convert.click( function(){ |
| 810 |
if( !confirm( _("This will retrieve macros stored in the brower, save them in the database, and delete them from the browser. Proceed?") ) ){ |
| 811 |
return; |
| 812 |
} |
| 813 |
convertOldMacros(); |
| 814 |
}); |
| 815 |
$("#macro-toolbar").prepend($convert); |
| 816 |
} |
| 817 |
let options = { |
| 818 |
url: "/api/v1/advancededitormacros/", |
| 819 |
method: "GET", |
| 820 |
contentType: "application/json", |
| 821 |
}; |
| 822 |
$.ajax(options) |
| 823 |
.then(function(result) { |
| 824 |
$.each(result,function( undef, macro ){ |
| 825 |
var $li = $( '<li data-name="' + macro.name + '" data-id="' + macro.macro_id + '"><a href="#">'+ macro.macro_id + ' - ' + macro.name + '</a><ol class="macro-info"></ol></li>' ); |
| 826 |
if ( macro.macro_id == macroEditor.activeMacroId ) $li.addClass( 'active' ); |
| 827 |
$li.click( function() { |
| 828 |
loadMacro(macro.name, macro.macro_id, macro.shared); |
| 829 |
return false; |
| 830 |
} ); |
| 831 |
$('#macro-list').append($li); |
| 832 |
}); |
| 833 |
}) |
| 834 |
.fail(function(err) { |
| 835 |
humanMsg.displayAlert( _("Failed to load macros:") + err.responseText, { className: 'humanError' } ); |
| 836 |
}); |
| 745 |
var $new_li = $( '<li class="new-macro"><a href="#">' + _("New macro...") + '</a></li>' ); |
837 |
var $new_li = $( '<li class="new-macro"><a href="#">' + _("New macro...") + '</a></li>' ); |
| 746 |
$new_li.click( function() { |
838 |
$new_li.click( function() { |
| 747 |
// TODO: make this a bit less retro |
839 |
// TODO: make this a bit less retro |
| 748 |
var name = prompt(_("Please enter the name for the new macro:")); |
840 |
var name = prompt(_("Please enter the name for the new macro:")); |
| 749 |
if (!name) return; |
841 |
if (!name) return; |
| 750 |
|
842 |
|
| 751 |
if ( !Preferences.user.macros[name] ) storeMacro( name, { format: "rancor", contents: "" } ); |
843 |
// if ( !Preferences.user.macros[name] ) storeMacro( name, { format: "rancor", contents: "" } ); |
| 752 |
showSavedMacros(); |
844 |
let options = { |
| 753 |
loadMacro( name ); |
845 |
url: "/api/v1/advancededitormacros/", |
|
|
846 |
method: "POST", |
| 847 |
contentType: "application/json", |
| 848 |
data: JSON.stringify({ |
| 849 |
name: name, |
| 850 |
patron_id: [% logged_in_user.borrowernumber | html %], |
| 851 |
macro_text: "", |
| 852 |
shared: false |
| 853 |
}) |
| 854 |
}; |
| 855 |
$.ajax(options) |
| 856 |
.then(function(undef, result) { |
| 857 |
showSavedMacros(); |
| 858 |
loadMacro( result.name, result.macro_id ); |
| 859 |
}) |
| 860 |
.fail(function(err) { |
| 861 |
humanMsg.displayAlert( _("Failed to create macro:") + err.responseText, { className: 'humanError' } ); |
| 862 |
}); |
| 754 |
} ); |
863 |
} ); |
| 755 |
$('#macro-list').append($new_li); |
864 |
$('#macro-list').append($new_li); |
| 756 |
$('#macro-list').scrollTop(scrollTop); |
865 |
$('#macro-list').scrollTop(scrollTop); |
| 757 |
} |
866 |
} |
| 758 |
|
867 |
|
| 759 |
function saveMacro() { |
868 |
function saveMacro(shared) { |
| 760 |
var name = macroEditor.activeMacro; |
869 |
var name = macroEditor.activeMacro; |
|
|
870 |
var macro_id = macroEditor.activeMacroId; |
| 871 |
var was_shared = macroEditor.activeMacroShared; |
| 761 |
|
872 |
|
| 762 |
if ( !name || macroEditor.savedGeneration == macroEditor.changeGeneration() ) return; |
873 |
if ( !name || macroEditor.savedGeneration == macroEditor.changeGeneration() && was_shared == shared ) return; |
| 763 |
|
874 |
|
| 764 |
macroEditor.savedGeneration = macroEditor.changeGeneration(); |
875 |
macroEditor.savedGeneration = macroEditor.changeGeneration(); |
| 765 |
storeMacro( name, { contents: macroEditor.getValue(), modified: (new Date()).valueOf(), history: macroEditor.getHistory() } ); |
876 |
api_url = "/api/v1/advancededitormacros/"; |
| 766 |
$('#macro-save-message').text(_("Saved")); |
877 |
if( shared || was_shared ) { api_url += "shared/" } |
| 767 |
showSavedMacros(); |
878 |
|
|
|
879 |
let options = { |
| 880 |
url: api_url + macro_id, |
| 881 |
method: "PUT", |
| 882 |
contentType: "application/json", |
| 883 |
data: JSON.stringify({ |
| 884 |
name: name, |
| 885 |
patron_id: [% logged_in_user.borrowernumber | html %], |
| 886 |
macro_text: macroEditor.getValue(), |
| 887 |
shared: shared |
| 888 |
}) |
| 889 |
}; |
| 890 |
$.ajax(options) |
| 891 |
.then(function(result) { |
| 892 |
$('#macro-save-message').text(_("Saved")); |
| 893 |
macroEditor.activeMacroShared = shared; |
| 894 |
showSavedMacros(); |
| 895 |
}) |
| 896 |
.fail(function(err) { |
| 897 |
humanMsg.displayAlert( _("Failed to save macro:") + err.responseText, { className: 'humanError' } ); |
| 898 |
}); |
| 899 |
>>>>>>> Bug 17268: Use API to store/retrieve values |
| 768 |
} |
900 |
} |
| 769 |
|
901 |
|
|
|
902 |
$(".macro_shared").change(function(){ |
| 903 |
if(this.checked){ |
| 904 |
saveMacro(true); |
| 905 |
} else { |
| 906 |
saveMacro(false); |
| 907 |
} |
| 908 |
}); |
| 909 |
|
| 910 |
// END Macro functions |
| 911 |
|
| 770 |
$(document).ready( function() { |
912 |
$(document).ready( function() { |
| 771 |
// Editor setup |
913 |
// Editor setup |
| 772 |
editor = new MARCEditor( { |
914 |
editor = new MARCEditor( { |
|
Lines 843-849
require( [ 'koha-backend', 'search', 'macros', 'marc-editor', 'marc-record', 'pr
Link Here
|
| 843 |
|
985 |
|
| 844 |
if ( saveTimeout ) clearTimeout( saveTimeout ); |
986 |
if ( saveTimeout ) clearTimeout( saveTimeout ); |
| 845 |
saveTimeout = setTimeout( function() { |
987 |
saveTimeout = setTimeout( function() { |
| 846 |
saveMacro(); |
988 |
saveMacro(macroEditor.activeMacroShared); |
| 847 |
|
989 |
|
| 848 |
saveTimeout = null; |
990 |
saveTimeout = null; |
| 849 |
}, 500 ); |
991 |
}, 500 ); |
|
Lines 994-1002
require( [ 'koha-backend', 'search', 'macros', 'marc-editor', 'marc-record', 'pr
Link Here
|
| 994 |
|
1136 |
|
| 995 |
$('#delete-macro').click( function() { |
1137 |
$('#delete-macro').click( function() { |
| 996 |
if ( !macroEditor.activeMacro || !confirm( _("Are you sure you want to delete this macro?") ) ) return; |
1138 |
if ( !macroEditor.activeMacro || !confirm( _("Are you sure you want to delete this macro?") ) ) return; |
| 997 |
|
1139 |
deleteMacro(); |
| 998 |
storeMacro( macroEditor.activeMacro, undefined ); |
|
|
| 999 |
showSavedMacros(); |
| 1000 |
loadMacro( undefined ); |
1140 |
loadMacro( undefined ); |
| 1001 |
|
1141 |
|
| 1002 |
return false; |
1142 |
return false; |