From: phasefx Date: Thu, 13 Dec 2007 15:36:15 +0000 (+0000) Subject: refactoring the pick_file/export/import a bit, though chrome/content/main.js is a... X-Git-Url: https://old-git.evergreen-ils.org/?a=commitdiff_plain;h=0df685abe5e9c575708401a6ed0d40629b543bca;p=Evergreen.git refactoring the pick_file/export/import a bit, though chrome/content/main.js is a little more complicated than what I want to tackle right now. Ultimate goal down the road is to abstract things so that we can store data like this on the backend to facilitate sharing/administration git-svn-id: svn://svn.open-ils.org/ILS/branches/rel_1_2@8207 dcc99617-32d9-48b4-a31d-7c20da2025e4 --- diff --git a/Open-ILS/xul/staff_client/chrome/content/main/main.js b/Open-ILS/xul/staff_client/chrome/content/main/main.js index 36e88cade2..eec3f72ef9 100644 --- a/Open-ILS/xul/staff_client/chrome/content/main/main.js +++ b/Open-ILS/xul/staff_client/chrome/content/main/main.js @@ -25,23 +25,6 @@ function clear_the_cache() { } } -function pick_file(mode) { - var nsIFilePicker = Components.interfaces.nsIFilePicker; - var fp = Components.classes["@mozilla.org/filepicker;1"].createInstance( nsIFilePicker ); - fp.init( - window, - mode == 'open' ? "Import Transaction File" : "Save Transaction File As", - mode == 'open' ? nsIFilePicker.modeOpen : nsIFilePicker.modeSave - ); - fp.appendFilters( nsIFilePicker.filterAll ); - var fp_result = fp.show(); - if ( ( fp_result == nsIFilePicker.returnOK || fp_result == nsIFilePicker.returnReplace ) && fp.file ) { - return fp.file; - } else { - return null; - } -} - function main_init() { dump('entering main_init()\n'); try { @@ -138,7 +121,8 @@ function main_init() { try { JSAN.use('util.file'); var file = new util.file('pending_xacts'); if (file._file.exists()) { - var f = pick_file('save'); + var file2 = new util.file(''); + var f = file2.pick_file( { 'mode' : 'save', 'title' : 'Save Transaction File As' } ); if (f) { if (f.exists()) { var r = G.error.yns_alert( @@ -193,7 +177,8 @@ function main_init() { if (file._file.exists()) { alert('There are already outstanding transactions on this staff client. Upload these first.'); } else { - var f = pick_file('open'); + var file2 = new util.file(''); + var f = file2.pick_file( { 'mode' : 'open', 'title' : 'Import Transaction File'} ); if (f && f.exists()) { var i_file = new util.file(''); i_file._file = f; file.write_content( 'truncate', i_file.get_content() ); diff --git a/Open-ILS/xul/staff_client/chrome/content/util/file.js b/Open-ILS/xul/staff_client/chrome/content/util/file.js index 0cdf367d0b..6244546aa9 100644 --- a/Open-ILS/xul/staff_client/chrome/content/util/file.js +++ b/Open-ILS/xul/staff_client/chrome/content/util/file.js @@ -222,6 +222,81 @@ util.file.prototype = { this.error.sdump('D_ERROR',this._file.path + '\nutil.file._create_output_stream(): ' + E); throw(E); } + }, + + 'pick_file' : function(params) { + try { + netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect"); + if (typeof params.mode == 'undefined') params.mode = 'open'; + var nsIFilePicker = Components.interfaces.nsIFilePicker; + var fp = Components.classes["@mozilla.org/filepicker;1"].createInstance( nsIFilePicker ); + fp.init( + window, + typeof params.title == 'undefined' ? params.mode : params.title, + params.mode == 'open' ? nsIFilePicker.modeOpen : nsIFilePicker.modeSave + ); + fp.appendFilters( nsIFilePicker.filterAll ); + var fp_result = fp.show(); + if ( ( fp_result == nsIFilePicker.returnOK || fp_result == nsIFilePicker.returnReplace ) && fp.file ) { + return fp.file; + } else { + return null; + } + } catch(E) { + this.error.standard_unexpected_error_alert('error picking file',E); + } + }, + + 'export_file' : function(params) { + try { + var obj = this; + params.mode = 'save'; + if (typeof params.data == 'undefined') throw('Need a .data field to export'); + netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect"); + var f = obj.pick_file( params ); + if (f) { + obj._file = f; + var temp = params.data; + if (typeof params.not_json == 'undefined') { + temp = js2JSON( temp ); + } + obj.write_content( 'truncate', temp ); + obj.close(); + alert('Exported ' + f.leafName); + return obj._file; + } else { + alert('File not chosen for export.'); + return null; + } + + } catch(E) { + this.error.standard_unexpected_error_alert('Error exporting file',E); + return null; + } + }, + + 'import_file' : function(params) { + try { + var obj = this; + params.mode = 'open'; + netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect"); + var f = obj.pick_file(params); + if (f && f.exists()) { + obj._file = f; + var temp = obj.get_content(); + obj.close(); + if (typeof params.not_json == 'undefined') { + temp = JSON2js( obj.get_content() ); + } + return temp; + } else { + alert('File not chosen for import.'); + return null; + } + } catch(E) { + this.error.standard_unexpected_error_alert('Error importing file',E); + return null; + } } } diff --git a/Open-ILS/xul/staff_client/server/cat/copy_editor.js b/Open-ILS/xul/staff_client/server/cat/copy_editor.js index e9407c3736..5e0c94cdd3 100644 --- a/Open-ILS/xul/staff_client/server/cat/copy_editor.js +++ b/Open-ILS/xul/staff_client/server/cat/copy_editor.js @@ -121,27 +121,6 @@ function my_init() { } /******************************************************************************************************/ -/* File picker for template export/import */ - -function pick_file(mode) { - netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect"); - var nsIFilePicker = Components.interfaces.nsIFilePicker; - var fp = Components.classes["@mozilla.org/filepicker;1"].createInstance( nsIFilePicker ); - fp.init( - window, - mode == 'open' ? "Import Templates File" : "Save Templates File As", - mode == 'open' ? nsIFilePicker.modeOpen : nsIFilePicker.modeSave - ); - fp.appendFilters( nsIFilePicker.filterAll ); - var fp_result = fp.show(); - if ( ( fp_result == nsIFilePicker.returnOK || fp_result == nsIFilePicker.returnReplace ) && fp.file ) { - return fp.file; - } else { - return null; - } -} - -/******************************************************************************************************/ /* Retrieve Templates */ g.retrieve_templates = function() { @@ -265,28 +244,8 @@ g.delete_template = function() { g.export_templates = function() { try { netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect"); - JSAN.use('util.file'); - var f = pick_file('save'); - if (f) { - if (f.exists()) { - var r = G.error.yns_alert( - 'Would you like to overwrite the existing file ' + f.leafName + '?', - 'Templates Export Warning', - 'Yes', - 'No', - null, - 'Check here to confirm this message' - ); - if (r != 0) { file.close(); alert('Not overwriting file.'); return; } - } - var e_file = new util.file(''); e_file._file = f; - e_file.write_content( 'truncate', js2JSON( g.templates ) ); - e_file.close(); - alert('Templates exported as file ' + f.leafName); - } else { - alert('File not chosen for export.'); - } - + JSAN.use('util.file'); var f = new util.file(''); + f.export_file( { 'title' : 'Save Templates File As', 'data' : g.templates } ); } catch(E) { g.error.standard_unexpected_error_alert('Error exporting templates',E); } @@ -298,12 +257,9 @@ g.export_templates = function() { g.import_templates = function() { try { netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect"); - JSAN.use('util.file'); - var f = pick_file('open'); - if (f && f.exists()) { - var i_file = new util.file(''); i_file._file = f; - var temp = JSON2js( i_file.get_content() ); - i_file.close(); + JSAN.use('util.file'); var f = new util.file(''); + var temp = f.import_file( { 'title' : 'Import Templates File' } ); + if (temp) { for (var i in temp) { if (g.templates[i]) { @@ -354,8 +310,6 @@ g.import_templates = function() { alert("Note: These imported templates will get saved along with any new template you try to create, but if that doesn't happen, then these templates will disappear with the next invocation of the item attribute editor."); } - } else { - alert('File not chosen for import.'); } } catch(E) { g.error.standard_unexpected_error_alert('Error importing templates',E); @@ -1211,6 +1165,7 @@ g.copy_notes = function() { /******************************************************************************************************/ /* hides or unhides stat cats based on library stat cat filter menu */ g.toggle_stat_cat_display = function(el) { + if (!el) return; var visible = el.getAttribute('checked'); var nl = document.getElementsByAttribute('sc_lib',el.getAttribute('value')); for (var n = 0; n < nl.length; n++) { diff --git a/Open-ILS/xul/staff_client/server/circ/copy_status.js b/Open-ILS/xul/staff_client/server/circ/copy_status.js index 1d74c7d898..f173c4583a 100644 --- a/Open-ILS/xul/staff_client/server/circ/copy_status.js +++ b/Open-ILS/xul/staff_client/server/circ/copy_status.js @@ -288,29 +288,11 @@ circ.copy_status.prototype = { 'cmd_copy_status_upload_file' : [ ['command'], function() { - function pick_file(mode) { - netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect UniversalBrowserWrite'); - var nsIFilePicker = Components.interfaces.nsIFilePicker; - var fp = Components.classes["@mozilla.org/filepicker;1"].createInstance( nsIFilePicker ); - fp.init( - window, - mode == 'open' ? "Import Barcode File" : "Save Barcode File As", - mode == 'open' ? nsIFilePicker.modeOpen : nsIFilePicker.modeSave - ); - fp.appendFilters( nsIFilePicker.filterAll ); - var fp_result = fp.show(); - if ( ( fp_result == nsIFilePicker.returnOK || fp_result == nsIFilePicker.returnReplace ) && fp.file ) { - return fp.file; - } else { - return null; - } - } netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect UniversalBrowserWrite'); JSAN.use('util.file'); - var f = pick_file('open'); - var i_file = new util.file(''); i_file._file = f; - var content = i_file.get_content(); - i_file.close(); + var f = new util.file(''); + var content = f.import_file( { 'title' : 'Import Barcode File', 'not_json' : true } ); + if (!content) return; var barcodes = content.split(/\s+/); if (barcodes.length > 0) { JSAN.use('util.exec'); var exec = new util.exec(); diff --git a/Open-ILS/xul/staff_client/server/circ/print_list_template_editor.js b/Open-ILS/xul/staff_client/server/circ/print_list_template_editor.js index 632f6092d6..19bc1c8724 100644 --- a/Open-ILS/xul/staff_client/server/circ/print_list_template_editor.js +++ b/Open-ILS/xul/staff_client/server/circ/print_list_template_editor.js @@ -296,27 +296,8 @@ circ.print_list_template_editor.prototype = { try { var obj = this; netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect"); - JSAN.use('util.file'); - var f = obj.pick_file('save'); - if (f) { - if (f.exists()) { - var r = obj.error.yns_alert( - 'Would you like to overwrite the existing file ' + f.leafName + '?', - 'Templates Export Warning', - 'Yes', - 'No', - null, - 'Check here to confirm this message' - ); - if (r != 0) { file.close(); alert('Not overwriting file.'); return; } - } - var e_file = new util.file(''); e_file._file = f; - e_file.write_content( 'truncate', js2JSON( obj.data.print_list_templates ) ); - e_file.close(); - alert('Templates exported as file ' + f.leafName); - } else { - alert('File not chosen for export.'); - } + JSAN.use('util.file'); var f = new util.file(''); + f.export_file( { 'title' : 'Save Templates File As', 'data' : obj.data.print_list_templates } ); } catch(E) { this.error.standard_unexpected_error_alert('Error exporting templates',E); @@ -327,56 +308,28 @@ circ.print_list_template_editor.prototype = { try { var obj = this; netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect"); - JSAN.use('util.file'); - var f = obj.pick_file('open'); - if (f && f.exists()) { - var i_file = new util.file(''); i_file._file = f; - var temp = JSON2js( i_file.get_content() ); - i_file.close(); - var s = ''; - function set_t(k,v) { - obj.data.print_list_templates[k] = v; - if (s) s+= ', '; s += k; - } - for (var i in temp) { set_t(i,temp[i]); } - obj.data.stash('print_list_templates'); - alert('Imported these templates: ' + s); - if (xulG) { - xulG.set_tab(xulG.url_prefix(urls.XUL_PRINT_LIST_TEMPLATE_EDITOR), {}, {}); - } else { - alert('Please reload this interface.'); - } + JSAN.use('util.file'); var f = new util.file(''); + var temp = f.import_file( { 'title' : 'Import Templates File' } ); + if (!temp) return; + var s = ''; + function set_t(k,v) { + obj.data.print_list_templates[k] = v; + if (s) s+= ', '; s += k; + } + for (var i in temp) { set_t(i,temp[i]); } + obj.data.stash('print_list_templates'); + alert('Imported these templates: ' + s); + if (xulG) { + xulG.set_tab(xulG.url_prefix(urls.XUL_PRINT_LIST_TEMPLATE_EDITOR), {}, {}); + } else { + alert('Please reload this interface.'); + } - } else { - alert('File not chosen for import.'); - } } catch(E) { this.error.standard_unexpected_error_alert('Error importing templates',E); } }, - 'pick_file' : function(mode) { - try { - netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect"); - var nsIFilePicker = Components.interfaces.nsIFilePicker; - var fp = Components.classes["@mozilla.org/filepicker;1"].createInstance( nsIFilePicker ); - fp.init( - window, - mode == 'open' ? "Import Templates File" : "Save Templates File As", - mode == 'open' ? nsIFilePicker.modeOpen : nsIFilePicker.modeSave - ); - fp.appendFilters( nsIFilePicker.filterAll ); - var fp_result = fp.show(); - if ( ( fp_result == nsIFilePicker.returnOK || fp_result == nsIFilePicker.returnReplace ) && fp.file ) { - return fp.file; - } else { - return null; - } - } catch(E) { - this.error.standard_unexpected_error_alert('error picking file',E); - } - }, - } dump('exiting print_list_template_editor.js\n');