[open-ils-commits] r8208 - in trunk/Open-ILS/xul/staff_client: chrome/content/main chrome/content/util server/cat server/circ

svn at svn.open-ils.org svn at svn.open-ils.org
Thu Dec 13 10:59:52 EST 2007


Author: phasefx
Date: 2007-12-13 10:39:05 -0500 (Thu, 13 Dec 2007)
New Revision: 8208

Modified:
   trunk/Open-ILS/xul/staff_client/chrome/content/main/main.js
   trunk/Open-ILS/xul/staff_client/chrome/content/util/file.js
   trunk/Open-ILS/xul/staff_client/server/cat/copy_editor.js
   trunk/Open-ILS/xul/staff_client/server/circ/copy_status.js
   trunk/Open-ILS/xul/staff_client/server/circ/print_list_template_editor.js
Log:
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

Modified: trunk/Open-ILS/xul/staff_client/chrome/content/main/main.js
===================================================================
--- trunk/Open-ILS/xul/staff_client/chrome/content/main/main.js	2007-12-13 15:36:15 UTC (rev 8207)
+++ trunk/Open-ILS/xul/staff_client/chrome/content/main/main.js	2007-12-13 15:39:05 UTC (rev 8208)
@@ -25,23 +25,6 @@
 	}
 }
 
-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 @@
 			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 @@
 				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() );

Modified: trunk/Open-ILS/xul/staff_client/chrome/content/util/file.js
===================================================================
--- trunk/Open-ILS/xul/staff_client/chrome/content/util/file.js	2007-12-13 15:36:15 UTC (rev 8207)
+++ trunk/Open-ILS/xul/staff_client/chrome/content/util/file.js	2007-12-13 15:39:05 UTC (rev 8208)
@@ -222,6 +222,81 @@
 			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;
+		}
 	}
 
 }

Modified: trunk/Open-ILS/xul/staff_client/server/cat/copy_editor.js
===================================================================
--- trunk/Open-ILS/xul/staff_client/server/cat/copy_editor.js	2007-12-13 15:36:15 UTC (rev 8207)
+++ trunk/Open-ILS/xul/staff_client/server/cat/copy_editor.js	2007-12-13 15:39:05 UTC (rev 8208)
@@ -121,27 +121,6 @@
 }
 
 /******************************************************************************************************/
-/* 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.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.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 @@
 				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 @@
 /******************************************************************************************************/
 /* 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++) {

Modified: trunk/Open-ILS/xul/staff_client/server/circ/copy_status.js
===================================================================
--- trunk/Open-ILS/xul/staff_client/server/circ/copy_status.js	2007-12-13 15:36:15 UTC (rev 8207)
+++ trunk/Open-ILS/xul/staff_client/server/circ/copy_status.js	2007-12-13 15:39:05 UTC (rev 8208)
@@ -288,29 +288,11 @@
 					'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();

Modified: trunk/Open-ILS/xul/staff_client/server/circ/print_list_template_editor.js
===================================================================
--- trunk/Open-ILS/xul/staff_client/server/circ/print_list_template_editor.js	2007-12-13 15:36:15 UTC (rev 8207)
+++ trunk/Open-ILS/xul/staff_client/server/circ/print_list_template_editor.js	2007-12-13 15:39:05 UTC (rev 8208)
@@ -296,27 +296,8 @@
 		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 @@
 		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');



More information about the open-ils-commits mailing list