// this file contains all the scripts used in the link edit page function new_tag_remove_tag() { var id = jQuery(this).attr('id'); var num = id.substr(10); var current_tags = jQuery('#tags-input').val().split(','); delete current_tags[num]; var new_tags = []; jQuery.each(current_tags, function(key, val) { if (val && !val.match(/^\s+$/) && '' != val) { new_tags = new_tags.concat(val); } }); jQuery('#tags-input').val( new_tags.join(',').replace(/\s*,+\s*/, ',').replace(/,+/, ',') .replace(/,+\s+,+/, ',').replace(/,+\s*$/, '').replace( /^\s*,+/, '')); tag_update_quickclicks(); jQuery('#newtag').focus(); return false; } function tag_update_quickclicks() { if (jQuery('#tags-input').length == 0) return; var current_tags = jQuery('#tags-input').val().split(','); jQuery('#tagchecklist').empty(); shown = false; // jQuery.merge( current_tags, current_tags ); // this doesn't work anymore, // need something to array_unique jQuery.each(current_tags, function(key, val) { val = val.replace(/^\s+/, '').replace(/\s+$/, ''); // trim if (!val.match(/^\s+$/) && '' != val) { txt = 'X ' + val + ' '; jQuery('#tagchecklist').append(txt); jQuery('#tag-check-' + key).click(new_tag_remove_tag); shown = true; } }); if (shown) jQuery('#tagchecklist').prepend( '' + linkL10n.tagsUsed + '
'); } function tag_flush_to_text(e, a) { a = a || false; var text = a ? jQuery(a).text() : jQuery('#newtag').val(); var newtags = jQuery('#tags-input').val(); var t = text.replace(/\s*([^,]+).*/, '$1,'); newtags += ',' if (newtags.indexOf(t) != -1) return false; newtags += text; // massage newtags = newtags.replace(/\s+,+\s*/g, ',').replace(/,+/g, ',').replace( /,+\s+,+/g, ',').replace(/,+\s*$/g, '').replace(/^\s*,+/g, ''); jQuery('#tags-input').val(newtags); tag_update_quickclicks(); if (!a) { jQuery('#newtag').val(''); jQuery('#newtag').focus(); } return false; } function tag_save_on_publish() { if (jQuery('#newtag').val() != linkL10n.addTag) tag_flush_to_text(); } function tag_press_key(e) { if (13 == e.keyCode) { tag_flush_to_text(); return false; } }; function wordCount(txt) { var tc = 0; txt = txt.replace( /<.[^<>]*?>/g, ' ' ).replace( / /gi, ' ' ); txt = txt.replace( /[0-9.(),;:!?%#$¿'"_+=\\/-]*/g, ''); txt.replace( /\S\s+/g, function(){tc++;} ); return tc; } jQuery(document).ready( function(e) { // add owner on the fly jQuery('#owner_add_toggle').click(function() { jQuery('#owner_add').toggle(); return false; }); jQuery('#owner_add_email').one('focus',function() { jQuery('#owner_add_email').val('');}); jQuery('#owner_add_submit').click(function(e) { var owner_add_email = jQuery('#owner_add_email').val(); if(!owner_add_email.length) return false; jQuery.ajax({ type: 'POST', url: linkL10n.ajax_submit, data: { action : 'instant_create_owner', _ajax_nonce : jQuery('#owner_add_nonce').val(), email : owner_add_email, link_id : jQuery('#link_id').val() }, success: function(response) { jQuery('#linkownerdiv .inside').html(response); }, error: function(response) { if(response && response.responseText) jQuery('#owner_add_response').html(response.responseText); } }); return false; }); jQuery('#tagcloud-link').click(function() { jQuery.ajax({ type: 'POST', url: 'admin-ajax.php', data: { action : 'get-linktagcloud', }, dataType: 'json', success: function(response) { if(response.ok) { html = '

' + response.data + '

'; jQuery('#tagcloud-link').after(html); jQuery('#the-tagcloud a').click( function() { tag_flush_to_text(0, this); return false; }); } }, error: function(reponse) { if(response.responseText) jQuery('#tagcloud-link').after('

' + response.data + '

'); } }); return false; }); function doSuccess(element) { jQuery('#' + element + '_response').html(''); jQuery('#' + element + '_response').addClass('success'); jQuery('#' + element + '_response').append(''); jQuery('#' + element + '_response').show(); } function doError(element) { jQuery('#' + element + '_response').html(''); jQuery('#' + element + '_response').addClass('error'); jQuery('#' + element + '_response').append(''); jQuery('#' + element + '_response').show(); } function doLoading(element) { jQuery('#' + element + '_response').html(''); jQuery('#' + element + '_response').removeClass('success'); jQuery('#' + element + '_response').removeClass('error'); jQuery('#' + element + '_response').append(''); jQuery('#' + element + '_response').show(); } // close postboxes that should be closed jQuery('.if-js-closed').removeClass('if-js-closed').addClass( 'closed'); jQuery('#link_name').focus(); // postboxes postboxes.add_postbox_toggles('link'); // Ajax Cat var newCat = jQuery('#newcat').one('focus', function() { jQuery(this).val('').removeClass('form-input-tip') }); jQuery('#category-add-submit').click( function() { newCat.focus(); }); var noSyncChecks = false; // prophylactic. necessary? var syncChecks = function() { if (noSyncChecks) return; noSyncChecks = true; var th = jQuery(this); var c = th.is(':checked'); var id = th.val().toString(); jQuery('#in-category-' + id + ', #in-popular-category-'+ id).attr('checked', c); noSyncChecks = false; }; var catAddAfter = function(r, s) { jQuery(s.what + ' response_data', r).each( function() { var t = jQuery(jQuery(this).text()); t.find('label').each( function() { var th = jQuery(this); var val = th.find('input').val(); var id = th.find('input')[0].id jQuery('#' + id).change(syncChecks); var name = jQuery.trim(th.text()); var o = jQuery('').text(name); }); }); }; jQuery('#categorychecklist').wpList( { alt : '', what : 'zelist-link-category', response : 'category-ajax-response', addAfter : catAddAfter }); jQuery('a[href="#categories-all"]').click( function() { deleteUserSetting('cats'); }); jQuery('a[href="#categories-pop"]').click( function() { setUserSetting('cats', 'pop'); }); if ('pop' == getUserSetting('cats')) jQuery('a[href="#categories-pop"]').click(); // category tabs var categoryTabs = jQuery('#category-tabs').tabs(); jQuery('#category-add-toggle').click( function() { jQuery(this).parents('div:first').toggleClass('wp-hidden-children'); //categoryTabs.tabsClick(1); return false; }); jQuery('.categorychecklist :checkbox').change(syncChecks).filter(':checked').change(); // status inline edit function updateText() { jQuery('#link-status-display').html(jQuery('#link_status :selected').text()); /* * * if (jQuery('#hidden_link_status').val() == 'draft') { if * (jQuery('#link_status option[value=publish]').length != 0) { * jQuery('#link_status option[value=publish]').remove(); * jQuery('#link_status').val(jQuery('#hidden_link_status').val()); } } * else { jQuery('#link_status * option[value=publish]').html(linkL10n.published); } * jQuery('.edit-link-status').show(); * * if (jQuery('#link_status :selected').val() == 'private' || * jQuery('#link_status :selected').val() == 'publish') { * jQuery('#save-link-status').hide(); } else { * jQuery('#save-link-status').show(); if (jQuery('#link_status * :selected').val() == 'pending') { * jQuery('#save-link-status').show().val(linkL10n.savePending); } else { * jQuery('#save-link-status').show().val(linkL10n.saveDraft); } } */ } jQuery('.edit-link-status').click( function() { if (jQuery('#link-status-select').is(":hidden")) { jQuery('#link-status-select').slideDown("normal"); jQuery(this).hide(); } return false; }); jQuery('.save-link-status').click( function() { jQuery('#link-status-select').slideUp("normal"); jQuery('.edit-link-status').show(); updateText(); return false; }); jQuery('.cancel-link-status').click( function() { jQuery('#link-status-select').slideUp("normal"); jQuery('#link_status').val(jQuery('#hidden_link_status').val()); jQuery('.edit-link-status').show(); updateText(); return false; }); // show/hide ban reasons jQuery('#link-banreason').hide(); jQuery('select[name="link_status"]').change(function(e) { var status = jQuery(this).val(); if(status == 'deny') jQuery('#link-banreason').show(); else jQuery('#link-banreason').hide(); }); // tags jQuery('#tags-input').hide(); tag_update_quickclicks(); // add the quickadd form jQuery('#jaxtag') .prepend( '' + linkL10n.separate + ''); jQuery('#tagadd').click(tag_flush_to_text); jQuery('#newtag').focus( function() { if (this.value == linkL10n.addTag) jQuery(this).val('').removeClass('form-input-tip'); }); jQuery('#newtag').blur( function() { if (this.value == '') jQuery(this).val(linkL10n.addTag).addClass( 'form-input-tip'); }); // auto-save tags on link save/publish jQuery('#publish').click(tag_save_on_publish); jQuery('#save-link').click(tag_save_on_publish); jQuery('#newtag').suggest( 'admin-ajax.php?action=ajax-link-tag-search', { delay : 500, minchars : 2, multiple : true, multipleSep : ", " }); jQuery('#newtag').keypress(tag_press_key); // image upload jQuery('#link_image_iframe').hide(); jQuery('#add_link_image').click(function(e) { jQuery('#link_image_iframe').toggle(); return false; }); jQuery('.response').hide(); jQuery("#link_url").focus( function(e) { jQuery('#link_url_response').hide(); }); jQuery("#link_name").focus( function(e) { jQuery('#link_name_response').hide(); }); jQuery("#link_rss").focus( function(e) { jQuery('#link_rss_response').hide(); }); // URL checking jQuery(".check_url").blur( function(e) { //if(jQuery('#editlink').length) return false; // no check on edit page var skip_existing = false; if(jQuery('#editlink').length) skip_existing = true; var input_id = jQuery(this).attr('id'); var input = jQuery(this); if(!input.val().length) return false; var nonce = jQuery('#check_url').val(); var type = 'url'; if(jQuery(this).hasClass('rss')) type = 'rss'; doLoading(input_id); jQuery('#'+ input_id + '_response').show(); jQuery(input).attr('disabled', 'disabled'); jQuery.ajax({ type: 'POST', url: linkL10n.ajax_submit, data: { action : 'check_url', _ajax_nonce : nonce, url : input.val(), type : type, skip_existing : skip_existing }, dataType: 'json', success: function(response) { if (response.ok) doSuccess(input_id); else doError(input_id); if(response.url) input.val(response.url); if(response.data) jQuery('#' + input_id + '_response').html(response.data); }, error: function(response) { if(response && response.responseText) jQuery('#' + input_id + '_response').html(response.responseText); } }); jQuery(input).attr('disabled', ''); }); // pre submit check var form_id = 'addlink'; if(jQuery('#editlink')) form_id = 'editlink'; jQuery('#' + form_id).submit(function() { if(jQuery('#force_submit').attr('checked')) { return true; } if(jQuery('#link_status').val() != 'publish') { // only check when publishing return true; } jQuery('#publish_response').html(''); var form_ok = true; var error_string = ''; jQuery('.response').hide(); if(!jQuery('#link_url').val()) { form_ok = false; doError('link_url'); jQuery('#link_url_response').html(linkL10n.pre_submit_no_url); } if(!jQuery('#link_name').val()) { form_ok = false; doError('link_name'); jQuery('#link_name_response').html(linkL10n.pre_submit_no_name); } if(!jQuery('[name="link_category[]"]').val()) { form_ok = false; doError('link_category'); jQuery('#link_category_response').html(linkL10n.pre_submit_no_category); } if(jQuery('#content').val()) { tc = wordCount(jQuery('#content').val()); if(linkWordCountL10n && (tc < linkWordCountL10n.min_length || tc > linkWordCountL10n.max_length)) { error_string = error_string + '

' + linkL10n.pre_submit_check_description + '

'; form_ok = false; doError('content'); } } else { error_string = error_string + '

' + linkL10n.pre_submit_check_description + '

'; form_ok = false; doError('content'); jQuery('#link_content_response').html(linkL10n.pre_submit_check_description) } if(!form_ok) { //error_string = error_string + '

' + linkL10n.pre_submit_wrong + '

'; error_string += '  ' + linkL10n.force_submit; jQuery('#publish_response').html(error_string); jQuery('#publish_response').show(); } return form_ok; }); });