Fix LP949249, Edit, then Create button creates a new item in all cases
[transitory.git] / Open-ILS / xul / staff_client / server / cat / copy_editor.js
index b6b5d93..e430b8e 100644 (file)
@@ -3,13 +3,13 @@ var g = { 'disabled' : false };
 g.map_acn = {};
 
 function $(id) { return document.getElementById(id); }
+function $_(x) { return $('catStrings').getString(x); }
 
 function my_init() {
     try {
         /******************************************************************************************************/
         /* setup JSAN and some initial libraries */
 
-        netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
         if (typeof JSAN == 'undefined') {
             throw( $('commonStrings').getString('common.jsan.missing') );
         }
@@ -26,13 +26,13 @@ function my_init() {
             $('non_unified_buttons').hidden = true;
         }
 
-        g.docid = xul_param('docid',{'modal_xulG':true});
-        g.handle_update = xul_param('handle_update',{'modal_xulG':true});
+        g.docid = xul_param('docid');
+        g.handle_update = xul_param('handle_update');
 
         /******************************************************************************************************/
         /* Get the copy ids from various sources and flesh them */
 
-        var copy_ids = xul_param('copy_ids',{'concat':true,'JSON2js_if_cgi':true,'JSON2js_if_xulG':true,'JSON2js_if_xpcom':true,'stash_name':'temp_copy_ids','clear_xpcom':true,'modal_xulG':true});
+        var copy_ids = xul_param('copy_ids',{'concat':true,'JSON2js_if_cgi':true,'JSON2js_if_xulG':true,'JSON2js_if_xpcom':true,'stash_name':'temp_copy_ids','clear_xpcom':true});
         if (!copy_ids) copy_ids = [];
 
         if (copy_ids.length > 0) g.copies = g.network.simple_request(
@@ -44,14 +44,23 @@ function my_init() {
         /* And other fleshed copies if any */
 
         if (!g.copies) g.copies = [];
-        var c = xul_param('copies',{'concat':true,'JSON2js_if_cgi':true,'JSON2js_if_xpcom':true,'stash_name':'temp_copies','clear_xpcom':true,'modal_xulG':true})
+        var c = xul_param('copies',{'concat':true,'JSON2js_if_cgi':true,'JSON2js_if_xpcom':true,'stash_name':'temp_copies','clear_xpcom':true})
         if (c) g.copies = g.copies.concat(c);
 
         /******************************************************************************************************/
         /* We try to retrieve callnumbers for existing copies, but for new copies, we rely on this */
 
-        g.callnumbers = xul_param('callnumbers',{'concat':true,'JSON2js_if_cgi':true,'JSON2js_if_xpcom':true,'stash_name':'temp_callnumbers','clear_xpcom':true,'modal_xulG':true});
+        g.callnumbers = xul_param('callnumbers',{'concat':true,'JSON2js_if_cgi':true,'JSON2js_if_xpcom':true,'stash_name':'temp_callnumbers','clear_xpcom':true});
 
+        /******************************************************************************************************/
+        /* Get preference (if it exists) for copy location label order */
+
+        g.cl_first = false; // Default to legacy OU first mode
+        var prefs = Components.classes['@mozilla.org/preferences-service;1']
+            .getService(Components.interfaces['nsIPrefBranch']);
+        try {
+            g.cl_first = prefs.getBoolPref('oils.copy_editor.copy_location_name_first');
+        } catch(E) { }
 
         /******************************************************************************************************/
         /* Quick fix, this was defined inline in the global scope but now needs g.error and g.copies from my_init */
@@ -63,7 +72,7 @@ function my_init() {
         /******************************************************************************************************/
         /* Is the interface an editor or a viewer, single or multi copy, existing copies or new copies? */
 
-        if (xul_param('edit',{'modal_xulG':true}) == '1') { 
+        if (xul_param('edit') == '1') { 
 
             g.edit = false;
 
@@ -215,6 +224,9 @@ function my_init() {
             g.hide_copy_notes_button();
         }
 
+        JSAN.use('util.hide');
+        util.hide.generate_css('ui.hide_copy_editor_fields');
+
     } catch(E) {
         var err_msg = $("commonStrings").getFormattedString('common.exception', ['cat/copy_editor.js', E]);
         try { g.error.sdump('D_ERROR',err_msg); } catch(E) { dump(err_msg); dump(js2JSON(E)); }
@@ -263,6 +275,7 @@ g.retrieve_templates = function() {
                 xulG.update_unified_template_list(list);
                 // functions the unified wrapper should use to let the item attribute editor do the heavy lifting for templates
                 xulG.update_item_editor_template_selection = function(new_value) {
+                    g.template_menu.setAttribute('value', new_value);
                     g.template_menu.value = new_value;
                     g.copy_editor_prefs[ 'template_menu' ] = { 'value' : g.template_menu.value };
                     g.save_attributes();
@@ -311,6 +324,19 @@ g._apply_template = function(name,apply_volume_editor_template_changes) {
         if (g.templates[ name ] != 'undefined') {
             var template = g.templates[ name ];
             for (var i in template) {
+                if (g.is_field_hidden(i)) {
+                    alert($('catStrings').getFormattedString(
+                        'staff.cat.copy_editor.apply_unsafe_field',
+                        [i]
+                    ));
+                    continue;
+                }
+                if (template[i].field == 'status') {
+                    if (!g.safe_to_edit_copy_status()) {
+                        alert($('catStrings').getFormattedString('staff.cat.copy_editor.apply_unsafe_field',[i]));
+                        continue;
+                    }
+                }
                 g.changed[ i ] = template[ i ];
                 switch( template[i].type ) {
                     case 'attribute' :
@@ -405,7 +431,6 @@ g.delete_template = function() {
 
 g.export_templates = function() {
     try {
-        netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
         JSAN.use('util.file'); var f = new util.file('');
         f.export_file( { 'title' : $('catStrings').getString('staff.cat.copy_editor.export_templates.title'), 'data' : g.templates } );
     } catch(E) {
@@ -418,7 +443,6 @@ g.export_templates = function() {
 
 g.import_templates = function() {
     try {
-        netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
         JSAN.use('util.file'); var f = new util.file('');
         var temp = f.import_file( { 'title' : $('catStrings').getString('staff.cat.copy_editor.import_templates.title') } );
         if (temp) {
@@ -640,6 +664,23 @@ g.safe_to_edit_copy_status = function() {
 }
 
 /******************************************************************************************************/
+/* This returns true if the field has been hidden via util.hide */
+
+g.is_field_hidden = function(field) {
+    try {
+        g.data.stash_retrieve();
+        if (g.data.hash.aous['ui.hide_copy_editor_fields']
+            && g.data.hash.aous['ui.hide_copy_editor_fields'].indexOf(field) > -1) {
+            return true;
+        }
+    } catch(E) {
+        g.error.standard_unexpected_error_alert('is_field_hidden?',E);
+        return false;
+    }
+}
+
+
+/******************************************************************************************************/
 /* This concats and uniques all the alert messages for use as the default value for a new alert message */
 
 g.populate_alert_message_input = function(tb) {
@@ -888,6 +929,13 @@ g.panes_and_field_names = {
         $('catStrings').getString('staff.cat.copy_editor.field.barcode.label'),
         {
             render: 'fm.barcode();',
+            input:
+                  'c = function (v) {'
+                +     'g.apply("barcode", v);'
+                +     'if (typeof post_c === "function") post_c(v);'
+                + '};'
+                + 'x = document.createElement("textbox");',
+            attr: { 'class': 'disabled' },
         }
     ], 
     [
@@ -929,7 +977,7 @@ g.panes_and_field_names = {
         $('catStrings').getString('staff.cat.copy_editor.field.location.label'),
         { 
             render: 'typeof fm.location() == "object" ? fm.location().name() : g.data.lookup("acpl",fm.location()).name()', 
-            input: 'c = function(v){ g.apply("location",v); if (typeof post_c == "function") post_c(v); }; x = util.widgets.make_menulist( util.functional.map_list( g.get_acpl_list(), function(obj) { return [ g.data.hash.aou[ obj.owning_lib() ].shortname() + " : " + obj.name(), obj.id() ]; }).sort()); x.addEventListener("apply",function(f){ return function(ev) { f(ev.target.value); } }(c), false);',
+            input: 'c = function(v){ g.apply("location",v); if (typeof post_c == "function") post_c(v); }; x = util.widgets.make_menulist( util.functional.map_list( g.get_acpl_list(), function(obj) { return [ ' + (g.cl_first ? 'obj.name() + " : " + g.data.hash.aou[ obj.owning_lib() ].shortname()' : 'g.data.hash.aou[ obj.owning_lib() ].shortname() + " : " + obj.name()') + ', obj.id() ]; }).sort()); x.addEventListener("apply",function(f){ return function(ev) { f(ev.target.value); } }(c), false);',
 
         }
     ],
@@ -1199,6 +1247,7 @@ g.render = function() {
             try {
                 var f = g.panes_and_field_names[h][i]; var fn = f[0]; var attr = f[1].attr;
                 groupbox = document.createElement('groupbox'); document.getElementById(h).appendChild(groupbox);
+                groupbox.setAttribute('hideable',fn);
                 if (attr) {
                     for (var a in attr) {
                         groupbox.setAttribute(a,attr[a]);
@@ -1207,6 +1256,7 @@ g.render = function() {
                 caption = document.createElement('caption'); groupbox.appendChild(caption);
                 caption.setAttribute('label',fn);
                 caption.setAttribute('id','caption_'+fn); // used for focus/keyboard navigation
+                caption.setAttribute('hideable',fn);
                 vbox = document.createElement('vbox'); groupbox.appendChild(vbox); // main display widget goes here
                 if (typeof g.changed[fn] != 'undefined') { addCSSClass(vbox,'copy_editor_field_changed'); }
                 if (typeof g.required[fn] != 'undefined') { addCSSClass(vbox,'copy_editor_field_required'); }
@@ -1269,8 +1319,16 @@ g.render = function() {
             } catch(E) { alert(E); }
         }
     }
-    if (g.template_menu) g.template_menu.value = g.template_menu.getAttribute('value');
+    if (g.template_menu) {
+        g.template_menu.value = g.template_menu.getAttribute('value');
+        if (xulG.unified_interface) {
+            if (typeof xulG.update_unified_template_selection == 'function') {
+                xulG.update_unified_template_selection(g.template_menu.value);
+            }
+        }
+    }
 
+    util.hide.generate_css('ui.hide_copy_editor_fields');
 }
 
 /******************************************************************************************************/
@@ -1299,16 +1357,12 @@ g.render_input = function(node,blob) {
             groupbox.setAttribute('style','');
         }
 
-        vbox.addEventListener('mouseover',on_mouseover,false);
-        vbox.addEventListener('mouseout',on_mouseout,false);
         groupbox.addEventListener('mouseover',on_mouseover,false);
         groupbox.addEventListener('mouseout',on_mouseout,false);
-        groupbox.firstChild.addEventListener('mouseover',on_mouseover,false);
-        groupbox.firstChild.addEventListener('mouseout',on_mouseout,false);
 
         function on_click(ev){
             try {
-                if (block || g.disabled || !g.edit) {
+                if (block || g.disabled || !g.edit || ev.currentTarget.classList.contains('disabled')) {
                     return;
                 }
                 block = true;
@@ -1375,10 +1429,8 @@ g.render_input = function(node,blob) {
                 g.error.standard_unexpected_error_alert('render_input',E);
             }
         }
-        vbox.addEventListener('click',on_click, false);
-        hbox.addEventListener('click',on_click, false);
-        caption.addEventListener('click',on_click, false);
-        caption.addEventListener('keypress',function(ev) {
+        groupbox.addEventListener('click',on_click, false);
+        groupbox.addEventListener('keypress',function(ev) {
             if (ev.keyCode == 13 /* enter */ || ev.keyCode == 77 /* mac enter */) on_click();
         }, false);
         caption.setAttribute('style','-moz-user-focus: normal');
@@ -1394,17 +1446,24 @@ g.render_input = function(node,blob) {
 /* store the copies in the global xpcom stash */
 
 g.stash_and_close = function() {
+    var r = {textcode: ''};
     try {
         oils_unlock_page();
 
         if (g.handle_update) {
             try {
-                var r = g.network.request(
+                r = g.network.request(
                     api.FM_ACP_FLESHED_BATCH_UPDATE.app,
                     api.FM_ACP_FLESHED_BATCH_UPDATE.method,
                     [ ses(), g.copies, true ]
                 );
-                if (typeof r.ilsevent != 'undefined') {
+                if (r.textcode === 'ITEM_BARCODE_EXISTS') {
+                    alert('error with item update: ' + r.desc);
+                    var barcode = $($_('staff.cat.copy_editor.field.barcode.label'));
+                    barcode.parentNode.classList.remove('disabled');
+                    barcode.click();
+                }
+                else if (typeof r.ilsevent !== 'undefined') {
                     g.error.standard_unexpected_error_alert('copy update',r);
                 }
                 /* FIXME -- revisit the return value here */
@@ -1415,9 +1474,10 @@ g.stash_and_close = function() {
         //g.data.temp_copies = js2JSON( g.copies );
         //g.data.stash('temp_copies');
         xulG.copies = g.copies;
-        update_modal_xulG(xulG);
-        JSAN.use('util.widgets');
-        util.widgets.dispatch('close',window);
+        if (r.textcode !== 'ITEM_BARCODE_EXISTS') {
+            JSAN.use('util.widgets');
+            util.widgets.dispatch('close',window);
+        }
     } catch(E) {
         alert('Error in copy_editor.js, g.stash_and_close(): '+E);
     }