/** * Add a trim() method to all string objects */ if(typeof(String.prototype.trim) === "undefined") { String.prototype.trim = function() { return String(this).replace(/^\s+|\s+$/g, ''); }; } /** * Take the user input and the selected role and add that user to the list * of users assigned to a post with that role. * Take care to show the div that surrounds the role list of the div was initially hidden. */ function ad_add_to_participants(user_id, user_nicename, role_id, role_name){ var error_message = false; // @todo This check doesn't work all that well if (user_id.length == 0){ error_message = '
' + 'No user selected' + '
'; } var user_role_status = 'pending'; jQuery("#ad-participant-error-message").remove(); // @todo check to see whether use was already assigned in this rold jQuery('input[name="ad-participant-role-' + role_id + '[]"]').each(function(){ if (jQuery(this).val().split('|')[0] == user_id ) { error_message = '
' + user_nicename + ' has already been added as ' + role_name + '
'; } }) // Add it to the existing role wrap if that already exists // Else, create a new one if (jQuery("#ad-user-role-" + role_id + "-wrap").length > 0 && !error_message) { // create a new list item that hold a hidden form element. var field_html = '
  • ' + user_nicename + ' | ' + user_role_status + '
  • '; // Append it to the list jQuery("ul#ad-participants-" + role_id).append(field_html); } else if (!error_message) { jQuery('#ad-no-participants').remove(); var field_html = '
    ' + '
    ' + role_name + '
    ' + '
    '; jQuery("#ad-participants-wrap").append(field_html); } else { jQuery("#ad-assign-form").prepend(error_message); } return false; } /** * When a user clicks the "Assign" button for a contributor, show them a form to select the role. */ function show_participant_assign_form(user_login){ user_login.trim() var participant_assign_form = "" + user_login; participant_assign_form += jQuery('div#ad-hidden-user-role-select').html(); participant_assign_form += 'Assign'; jQuery('#ad_participant_' + user_login).html(participant_assign_form); return false; } jQuery(document).ready(function() { var ad_current_assignment_status = ''; var ad_current_participant_types = []; var ad_current_pitched_by_participant = '' /** * Initialize Co-Author Plus auto-suggest if it exists */ if (coauthor_ajax_suggest_link) { jQuery('#ad-assignee-search').suggest(coauthor_ajax_suggest_link, { onSelect: function() { var vals = this.value.split('|'); var author = {}; author.id = jQuery.trim(vals[0]); author.login = jQuery.trim(vals[1]); author.name = jQuery.trim(vals[2]); jQuery('#ad-assignee-search').val(author.name); } }).keydown(function(e) { if (e.keyCode == 13) { return false; } }); } /** * Toggle post_meta_box subheads */ jQuery('h4.toggle').click(function() { var inner = jQuery(this).parent().find('div.inner').slideToggle(); }); /** * Fires when the user hits the assign button. * Get the user search box, check if its a valid user, and add to the selected role. */ jQuery("a#ad-assign-button").click(function(){ var valid_user = true; var user_id = ''; var user_nicename = ''; if (jQuery('#ad-assignee-search').length > 0 && jQuery('#ad-assignee-search').val().length > 1 ) { var search = jQuery('#ad-assignee-search').val().trim(); var data = { action: 'user_check', q: search }; // Call another JAX function verify the username jQuery.ajax({ url: ajaxurl, data: data, async: false, success: function(response){ // valid username returns user->ID > 0 if(parseInt(response) > 0){ user_id = search; user_nicename = search; jQuery('#ad-assignee-search').val(''); } else { // flag the invalid_user and display an error message valid_user = false; jQuery('#ad-participant-error-message').remove(); error_message = '
    '+ search + ' is not a valid user
    '; jQuery("#ad-assign-form").prepend(error_message); } } }); } else { var user_id = jQuery('#ad-assignee-dropdown option:selected').val(); var user_nicename = jQuery('#ad-assignee-dropdown option:selected').text(); } if(valid_user){ var role_id = jQuery('#ad-user-role-dropdown option:selected').val(); var role_name = jQuery('#ad-user-role-dropdown option:selected').text(); ad_add_to_participants(user_id, user_nicename, role_id, role_name); } return false; }); /* ============================ Assignment Status ============================ */ /** * Manipulate the DOM when the user wants to "Edit" assignment status * In short, save the current status and show the selection tool */ jQuery('#ad-edit-assignment-status').click(function(){ jQuery(this).hide(); ad_current_assignment_status = jQuery('select#ad-assignment-status').val(); jQuery('#ad-assignment-status-select').slideToggle(); return false; }); /** * Manipulate the DOM when the user wants to "Save" their assignment status selection * In short, ... */ jQuery('#ad-save-assignment-status').click(function(){ jQuery('#ad-edit-assignment-status').show(); var text = jQuery('select#ad-assignment-status option:selected').text(); jQuery('#ad-assignment-status-display').html(text); jQuery('#ad-assignment-status-select').slideToggle(); return false; }); /** * Manipulate the DOM when the user wants to "Cancel" their assignment status selection * In short, ... */ jQuery('#ad-cancel-assignment-status').click(function(){ jQuery('#ad-edit-assignment-status').show(); jQuery('select#ad-assignment-status').val(ad_current_assignment_status); jQuery('#ad-assignment-status-select').slideToggle(); return false; }); /* ============================ Participant Types ============================ */ /** * Manipulate the DOM when the user wants to "Edit" participant types * In short, save current checkbox states and then show list of types */ jQuery('#ad-edit-participant-types').click(function(){ jQuery(this).hide(); jQuery("input[name='ad-participant-types[]']").each(function(){ if (jQuery(this).is(':checked')) { ad_current_participant_types[jQuery(this).val()] = 'on'; } else { ad_current_participant_types[jQuery(this).val()] = 'off'; } }); jQuery('#ad-participant-types-select').slideToggle(); return false; }); /** * Manipulate the DOM when the user hits "Save" on participant types * In short, build new field label and then hide list of types */ jQuery('#save-ad-participant-types').click(function(){ jQuery('#ad-participant-types-select').slideToggle(); var ad_all_participant_types = []; var ad_display_participant_types = ''; jQuery("input[name='ad-participant-types[]']").each(function(){ if ( jQuery(this).is(':checked') ) { ad_display_participant_types += jQuery(this).parent().find('label').html() + ', '; ad_all_participant_types[jQuery(this).val()] = 'on'; } else { ad_all_participant_types[jQuery(this).val()] = 'off'; } }); // Hacky way to check if values are in array var joined = '|' + ad_all_participant_types.join('|') + '|'; if (joined.indexOf('on') != -1 && joined.indexOf('off') == -1) { ad_display_participant_types = 'All'; } else if (joined.indexOf('on') == -1 && joined.indexOf('off') != -1) { ad_display_participant_types = 'None'; } else { ad_display_participant_types = ad_display_participant_types.slice(0, ad_display_participant_types.length - 2); } // Update the label for the field because we have new values jQuery('#ad-participant-types-display').html(ad_display_participant_types); jQuery('#ad-edit-participant-types').show(); return false; }); /** * Manipulate the DOM when the user hits "Cancel" on participant types * In short, restore checkbox values and hide the list of options */ jQuery('#cancel-ad-participant-types').click(function(){ jQuery('#ad-participant-types-select').slideToggle(); // Restore checkbox values to what they were previously jQuery("input[name='ad-participant-types[]']").each(function(){ if (ad_current_participant_types[jQuery(this).val()] == 'on') { jQuery(this).attr('checked', 'checked'); } else { jQuery(this).removeAttr('checked'); } }); jQuery('#ad-edit-participant-types').show(); return false; }); jQuery('.ad-remove-participant').each(function(index, link){ jQuery(link).click(function(){ var pieces = this.id.split('-'); // Remove the user from that role. jQuery('#ad-participant-' + pieces[3] + '-' + pieces[4]).remove(); // @todo - roll up the role if no users left // @todo - Call coauthors_remove_author(name) if no roles left for this pitch return false }); }); /* ============================ Pitched By ============================ */ /** * Manipulate the DOM when the user wants to "Edit" the pitched by field. * In short, save the currently selected user and show the selection tool. */ jQuery('#ad-edit-pitched-by-participant').click(function(){ jQuery(this).hide(); ad_current_pitched_by_participant = jQuery('select#ad-pitched-by-participant').val(); jQuery('#ad-pitched-by-participant-select').slideToggle(); return false; }); /** * Manipulate the DOM when the user wants to "Save" the pitched by field. */ jQuery('#ad-save-pitched-by-participant').click(function(){ jQuery('#ad-edit-pitched-by-participant').show(); var text = jQuery('select#ad-pitched-by-participant option:selected').text(); var user_id = jQuery('select#ad-pitched-by-participant option:selected').val(); var link = '' + text + ""; jQuery('#ad-pitched-by-participant-display').html(link); jQuery('#ad-pitched-by-participant-select').slideToggle(); return false; }); /** * Manipulate the DOM when the user hits "Cancel" on pitched by field. * In short, restore field value and hide the pitched by form. */ jQuery('#ad-cancel-pitched-by-participant').click(function(){ jQuery('#ad-pitched-by-participant-select').slideToggle(); jQuery('select#ad-pitched-by-participant').val(ad_current_pitched_by_participant); jQuery('#ad-edit-pitched-by-participant').show(); return false; }); });