PageRenderTime 48ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/plugins/attachments/deprecated/js/attachments.js

https://bitbucket.org/codemen_iftekhar/codemen
JavaScript | 146 lines | 109 code | 24 blank | 13 comment | 12 complexity | cc4e1c20f7dd8c142acca042a40a323f MD5 | raw file
  1. jQuery(document).ready(function($){
  2. var context = false;
  3. var thickbox_modder;
  4. // init sortability
  5. if($('div#attachments-list ul:data(sortable)').length==0&&$('div#attachments-list ul li').length>0){
  6. $('div#attachments-list ul').sortable({
  7. containment: 'parent',
  8. stop: function(e, ui) {
  9. $('#attachments-list ul li').each(function(i, id) {
  10. $(this).find('input.attachment_order').val(i+1);
  11. });
  12. }
  13. });
  14. }
  15. // delete link handler
  16. function attachments_hook_delete_links(theparent){
  17. attachment_parent = theparent.parent().parent().parent();
  18. attachment_parent.slideUp(function() {
  19. attachment_parent.remove();
  20. $('#attachments-list ul li').each(function(i, id) {
  21. $(this).find('input.attachment_order').val(i+1);
  22. });
  23. if($('div#attachments-list li').length == 0) {
  24. $('#attachments-list').slideUp(function() {
  25. $('#attachments-list').hide();
  26. });
  27. }
  28. });
  29. }
  30. // Hook our delete links
  31. if(parseFloat($.fn.jquery)>=1.7){
  32. // 'live' is deprecated
  33. $(document).on("click", "span.attachment-delete a", function(event){
  34. theparent = $(this);
  35. attachments_hook_delete_links(theparent);
  36. return false;
  37. });
  38. }else{
  39. $('span.attachment-delete a').live('click',function(event){
  40. theparent = $(this);
  41. attachments_hook_delete_links(theparent);
  42. return false;
  43. });
  44. }
  45. // thickbox handler
  46. if(parseFloat($.fn.jquery)>=1.7){
  47. // 'live' is deprecated
  48. $(document).on("click", "a#attachments-thickbox", function(event){
  49. event.preventDefault();
  50. theparent = $(this);
  51. attachments_handle_thickbox(event,theparent);
  52. return false;
  53. });
  54. }else{
  55. $('a#attachments-thickbox').live('click',function(event){
  56. event.preventDefault();
  57. theparent = $(this);
  58. attachments_handle_thickbox(event,theparent);
  59. return false;
  60. });
  61. }
  62. function attachments_handle_thickbox(event,theparent){
  63. var href = theparent.attr('href'), width = jQuery(window).width(), H = jQuery(window).height(), W = ( 720 < width ) ? 720 : width;
  64. if ( ! href ) return;
  65. href = href.replace(/&width=[0-9]+/g, '');
  66. href = href.replace(/&height=[0-9]+/g, '');
  67. theparent.attr( 'href', href + '&width=' + ( W - 80 ) + '&height=' + ( H - 85 ) );
  68. context = true;
  69. thickbox_modder = setInterval( function(){
  70. if( context ){
  71. modify_thickbox();
  72. }
  73. }, 500 );
  74. tb_show('Attach a file', event.target.href, false);
  75. return false;
  76. }
  77. // handle the attachment process
  78. function handle_attach(title,caption,id,thumb){
  79. var source = $('#attachment-template').html();
  80. var template = Handlebars.compile(source);
  81. var order = $('#attachments-list > ul > li').length + 1;
  82. $('div#attachments-list ul', top.document).append(template({name:title,title:title,caption:caption,id:id,thumb:thumb,order:order}));
  83. $('#attachments-list > ul > li').each(function(i, id) {
  84. $(this).find('input.attachment_order').val(i+1);
  85. });
  86. return false;
  87. }
  88. function modify_thickbox(){
  89. the_thickbox = jQuery('#TB_iframeContent').contents();
  90. // our new click handler for the attach button
  91. the_thickbox.find('td.savesend input').unbind('click').click(function(e){
  92. jQuery(this).after('<span class="attachments-attached">Attached!</span>');
  93. // grab our meta as per the Media library
  94. var wp_media_meta = $(this).parent().parent().parent();
  95. var wp_media_title = wp_media_meta.find('tr.post_title td.field input').val();
  96. var wp_media_caption = wp_media_meta.find('tr.post_excerpt td.field input').val();
  97. var wp_media_id = wp_media_meta.find('td.imgedit-response').attr('id').replace('imgedit-response-','');
  98. var wp_media_thumb = wp_media_meta.parent().find('img.thumbnail').attr('src');
  99. handle_attach(wp_media_title,wp_media_caption,wp_media_id,wp_media_thumb);
  100. the_thickbox.find('span.attachments-attached').delay(1000).fadeOut('fast');
  101. return false;
  102. });
  103. // update button
  104. if(the_thickbox.find('.media-item .savesend input[type=submit], #insertonlybutton').length){
  105. the_thickbox.find('.media-item .savesend input[type=submit], #insertonlybutton').val('Attach');
  106. }
  107. if(the_thickbox.find('#tab-type_url').length){
  108. the_thickbox.find('#tab-type_url').hide();
  109. }
  110. if(the_thickbox.find('tr.post_title').length){
  111. // we need to ALWAYS get the fullsize since we're retrieving the guid
  112. // if the user inserts an image somewhere else and chooses another size, everything breaks
  113. the_thickbox.find('tr.image-size input[value="full"]').prop('checked', true);
  114. the_thickbox.find('tr.post_title,tr.image_alt,tr.post_excerpt,tr.image-size,tr.post_content,tr.url,tr.align,tr.submit>td>a.del-link').hide();
  115. }
  116. // was the thickbox closed?
  117. if(the_thickbox.length==0 && context){
  118. clearInterval(thickbox_modder);
  119. context = false;
  120. }
  121. }
  122. });