of-medialibrary-uploader.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /*-----------------------------------------------------------------------------------*/
  2. /* WooFramework Media Library-driven AJAX File Uploader Module
  3. /* JavaScript Functions (2010-11-05)
  4. /*
  5. /* The code below is designed to work as a part of the WooFramework Media Library-driven
  6. /* AJAX File Uploader Module. It is included only on screens where this module is used.
  7. /*
  8. /* Used with (very) slight modifications for Options Framework.
  9. /*-----------------------------------------------------------------------------------*/
  10. (function ($) {
  11. optionsframeworkMLU = {
  12. /*-----------------------------------------------------------------------------------*/
  13. /* Remove file when the "remove" button is clicked.
  14. /*-----------------------------------------------------------------------------------*/
  15. removeFile: function () {
  16. $('.mlu_remove').live('click', function(event) {
  17. $(this).hide();
  18. $(this).parents().parents().children('.upload').attr('value', '');
  19. $(this).parents('.screenshot').slideUp();
  20. $(this).parents('.screenshot').siblings('.of-background-properties').hide(); //remove background properties
  21. return false;
  22. });
  23. // Hide the delete button on the first row
  24. $('a.delete-inline', "#option-1").hide();
  25. }, // End removeFile
  26. /*-----------------------------------------------------------------------------------*/
  27. /* Replace the default file upload field with a customised version.
  28. /*-----------------------------------------------------------------------------------*/
  29. recreateFileField: function () {
  30. $('input.file').each(function(){
  31. var uploadbutton = '<input class="upload_file_button" type="button" value="Upload" />';
  32. $(this).wrap('<div class="file_wrap" />');
  33. $(this).addClass('file').css('opacity', 0); //set to invisible
  34. $(this).parent().append($('<div class="fake_file" />').append($('<input type="text" class="upload" />').attr('id',$(this).attr('id')+'_file')).val( $(this).val() ).append(uploadbutton));
  35. $(this).bind('change', function() {
  36. $('#'+$(this).attr('id')+'_file').val($(this).val());
  37. });
  38. $(this).bind('mouseout', function() {
  39. $('#'+$(this).attr('id')+'_file').val($(this).val());
  40. });
  41. });
  42. }, // End recreateFileField
  43. /*-----------------------------------------------------------------------------------*/
  44. /* Use a custom function when working with the Media Uploads popup.
  45. /* Requires jQuery, Media Upload and Thickbox JavaScripts.
  46. /*-----------------------------------------------------------------------------------*/
  47. mediaUpload: function () {
  48. jQuery.noConflict();
  49. $( 'input.upload_button' ).removeAttr('style');
  50. var formfield,
  51. formID,
  52. btnContent = true,
  53. tbframe_interval;
  54. // On Click
  55. $('input.upload_button').live("click", function () {
  56. formfield = $(this).prev('input').attr('id');
  57. formID = $(this).attr('rel');
  58. //Change "insert into post" to "Use this Button"
  59. tbframe_interval = setInterval(function() {jQuery('#TB_iframeContent').contents().find('.savesend .button').val('Use This Image');}, 2000);
  60. // Display a custom title for each Thickbox popup.
  61. var woo_title = '';
  62. if ( $(this).parents('.section').find('.heading') ) { woo_title = $(this).parents('.section').find('.heading').text(); } // End IF Statement
  63. tb_show( woo_title, 'media-upload.php?post_id='+formID+'&TB_iframe=1' );
  64. return false;
  65. });
  66. window.original_send_to_editor = window.send_to_editor;
  67. window.send_to_editor = function(html) {
  68. if (formfield) {
  69. //clear interval for "Use this Button" so button text resets
  70. clearInterval(tbframe_interval);
  71. // itemurl = $(html).attr('href'); // Use the URL to the main image.
  72. if ( $(html).html(html).find('img').length > 0 ) {
  73. itemurl = $(html).html(html).find('img').attr('src'); // Use the URL to the size selected.
  74. } else {
  75. // It's not an image. Get the URL to the file instead.
  76. var htmlBits = html.split("'"); // jQuery seems to strip out XHTML when assigning the string to an object. Use alternate method.
  77. itemurl = htmlBits[1]; // Use the URL to the file.
  78. var itemtitle = htmlBits[2];
  79. itemtitle = itemtitle.replace( '>', '' );
  80. itemtitle = itemtitle.replace( '</a>', '' );
  81. } // End IF Statement
  82. var image = /(^.*\.jpg|jpeg|png|gif|ico*)/gi;
  83. var document = /(^.*\.pdf|doc|docx|ppt|pptx|odt*)/gi;
  84. var audio = /(^.*\.mp3|m4a|ogg|wav*)/gi;
  85. var video = /(^.*\.mp4|m4v|mov|wmv|avi|mpg|ogv|3gp|3g2*)/gi;
  86. if (itemurl.match(image)) {
  87. btnContent = '<img src="'+itemurl+'" alt="" /><a href="#" class="mlu_remove button">Remove Image</a>';
  88. } else {
  89. // No output preview if it's not an image.
  90. // btnContent = '';
  91. // Standard generic output if it's not an image.
  92. html = '<a href="'+itemurl+'" target="_blank" rel="external">View File</a>';
  93. btnContent = '<div class="no_image"><span class="file_link">'+html+'</span><a href="#" class="mlu_remove button">Remove</a></div>';
  94. }
  95. $('#' + formfield).val(itemurl);
  96. // $('#' + formfield).next().next('div').slideDown().html(btnContent);
  97. $('#' + formfield).siblings('.screenshot').slideDown().html(btnContent);
  98. $('#' + formfield).siblings('.of-background-properties').show(); //show background properties
  99. tb_remove();
  100. } else {
  101. window.original_send_to_editor(html);
  102. }
  103. // Clear the formfield value so the other media library popups can work as they are meant to. - 2010-11-11.
  104. formfield = '';
  105. }
  106. } // End mediaUpload
  107. }; // End optionsframeworkMLU Object // Don't remove this, or the sky will fall on your head.
  108. /*-----------------------------------------------------------------------------------*/
  109. /* Execute the above methods in the optionsframeworkMLU object.
  110. /*-----------------------------------------------------------------------------------*/
  111. $(document).ready(function () {
  112. optionsframeworkMLU.removeFile();
  113. optionsframeworkMLU.recreateFileField();
  114. optionsframeworkMLU.mediaUpload();
  115. });
  116. })(jQuery);