From 45f8545086ac612c4d6066d43a6c9352da024117 Mon Sep 17 00:00:00 2001 From: Jason Etheridge Date: Tue, 12 Apr 2011 13:02:11 -0400 Subject: [PATCH] widget_prompt utility function. Simpler/better than the evil fancy_prompt, you shove a node in, and when a user clicks Okay, the node's value gets passed back out. var value = widget_prompt( $('existing_menu_list_we_want_to_reuse').cloneNode(true), { 'title' : 'titlebar text, also used as a key for dialog height/width peristence', 'desc' : 'description to shove above the widget' } ); --- .../chrome/content/OpenILS/global_util.js | 30 +++++++++ .../chrome/content/util/widget_prompt.js | 78 ++++++++++++++++++++++ .../chrome/content/util/widget_prompt.xul | 49 ++++++++++++++ .../chrome/locale/en-US/offline.properties | 4 ++ 4 files changed, 161 insertions(+) create mode 100644 Open-ILS/xul/staff_client/chrome/content/util/widget_prompt.js create mode 100644 Open-ILS/xul/staff_client/chrome/content/util/widget_prompt.xul diff --git a/Open-ILS/xul/staff_client/chrome/content/OpenILS/global_util.js b/Open-ILS/xul/staff_client/chrome/content/OpenILS/global_util.js index befb693aec..8250caedcd 100644 --- a/Open-ILS/xul/staff_client/chrome/content/OpenILS/global_util.js +++ b/Open-ILS/xul/staff_client/chrome/content/OpenILS/global_util.js @@ -595,3 +595,33 @@ return url; } + function widget_prompt(node,args) { + // args is an object that may contain the following keys: title, desc, ok_label, ok_accesskey, cancel_label, cancel_accesskey, access, method + // access may contain 'property' or 'attribute' or 'method' for retrieving the value from the node + // if 'method', then the method key will reference a function that returns the value + try { + if (!node) { return false; } + if (!args) { args = {}; } + args[ 'widget' ] = node; + + var url = location.protocol == 'chrome' + ? 'chrome://open_ils_staff_client/content/util/widget_prompt.xul' + : '/xul/server/util/widget_prompt.xul'; + + JSAN.use('util.window'); var win = new util.window(); + var my_xulG = win.open( + url, + args.title || 'widget_prompt', + 'chrome,modal', + args + ); + + if (my_xulG.status == 'incomplete') { + return false; + } else { + return my_xulG.value; + } + } catch(E) { + alert('Error in global_utils.js, widget_prompt(): ' + E); + } + } diff --git a/Open-ILS/xul/staff_client/chrome/content/util/widget_prompt.js b/Open-ILS/xul/staff_client/chrome/content/util/widget_prompt.js new file mode 100644 index 0000000000..c51689b71b --- /dev/null +++ b/Open-ILS/xul/staff_client/chrome/content/util/widget_prompt.js @@ -0,0 +1,78 @@ +var xulG = {}; +var widget; + +function my_init() { + try { + netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect"); + if (typeof JSAN == 'undefined') { throw( "The JSAN library object is missing."); } + JSAN.errorLevel = "die"; // none, warn, or die + JSAN.addRepository('/xul/server/'); + JSAN.use('util.error'); g.error = new util.error(); + g.error.sdump('D_TRACE','my_init() for widget_prompt.xul'); + + widget = xul_param('widget',{'modal_xulG':true}); + if (widget) { + $('widget_prompt_main').appendChild(widget); + } + + var ok_label = xul_param('ok_label',{'modal_xulG':true}) || offlineStrings.getString('common.ok.label'); + $('ok_btn').setAttribute('label',ok_label); + + var ok_accesskey = xul_param('ok_accesskey',{'modal_xulG':true}) || offlineStrings.getString('common.ok.accesskey'); + $('ok_btn').setAttribute('accesskey',ok_accesskey); + + var cancel_label = xul_param('cancel_label',{'modal_xulG':true}) || offlineStrings.getString('common.cancel.label'); + $('cancel_btn').setAttribute('label',cancel_label); + + var cancel_accesskey = xul_param('cancel_accesskey',{'modal_xulG':true}) || offlineStrings.getString('common.cancel.accesskey'); + $('cancel_btn').setAttribute('accesskey',cancel_accesskey); + + var desc = xul_param('desc',{'modal_xulG':true}); + if (desc) { + $('desc').appendChild( document.createTextNode( desc ) ); + } + + $('ok_btn').addEventListener('command',widget_save,false); + $('cancel_btn').addEventListener('command',function(ev) { window.close(); },false); + + if (xul_param('title',{'modal_xulG':true})) { + try { window.title = xul_param('title',{'modal_xulG':true}); } catch(E) {} + try { document.title = xul_param('title',{'modal_xulG':true}); } catch(E) {} + } + + xulG[ 'status' ] = 'incomplete'; + update_modal_xulG(xulG); + + try { widget.focus(); } catch(E) {} + + } catch(E) { + alert('Error in widget_prompt.js, my_init(): ' + E); + } +} + +function widget_save(ev) { + try { + if (widget) { + switch( xul_param('access',{'modal_xulG':true}) ) { + case 'method' : + xulG[ 'value' ] = xulG[ 'method' ](); + break; + case 'attribute': + xulG[ 'value' ] = widget.getAttribute('value'); + break; + case 'property': + default: + xulG[ 'value' ] = widget.value; + break; + } + } + xulG[ 'status' ] = 'complete'; + + update_modal_xulG(xulG); + + window.close(); + } catch(E) { + alert('Error in widget_prompt.js, widget_save(): ' + E); + } +} + diff --git a/Open-ILS/xul/staff_client/chrome/content/util/widget_prompt.xul b/Open-ILS/xul/staff_client/chrome/content/util/widget_prompt.xul new file mode 100644 index 0000000000..303a4c3e90 --- /dev/null +++ b/Open-ILS/xul/staff_client/chrome/content/util/widget_prompt.xul @@ -0,0 +1,49 @@ + + + + + + + + + + + + + +]> + + + + + + + + + + + + +