PageRenderTime 48ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/functions/js/jcd-medialibrary-uploader.js

https://bitbucket.org/jenclarkdesign/clinics-by-design
JavaScript | 222 lines | 118 code | 63 blank | 41 comment | 16 complexity | 440f1a59b968f57de58d060617cd53c8 MD5 | raw file
  1. /*-----------------------------------------------------------------------------------*/
  2. /* Jcd Media Library-driven AJAX File Uploader Module - JavaScript Functions */
  3. /* 2010-11-05. */
  4. /*
  5. /* The code below is designed to work as a part of the Jcd Media Library-driven
  6. /* AJAX File Uploader Module. It is included only on screens where this module is used.
  7. /*-----------------------------------------------------------------------------------*/
  8. (function ($) {
  9. jcdMLU = {
  10. /*-----------------------------------------------------------------------------------*/
  11. /* Remove file when the "remove" button is clicked.
  12. /*-----------------------------------------------------------------------------------*/
  13. removeFile: function () {
  14. $( '.mlu_remove').live( 'click', function(event) {
  15. $(this).hide();
  16. $(this).parents().parents().children( '.upload').attr( 'value', '' );
  17. $(this).parents( '.screenshot').slideUp();
  18. return false;
  19. });
  20. // Hide the delete button on the first row
  21. $( 'a.delete-inline', '#option-1' ).hide();
  22. }, // End removeFile
  23. /*-----------------------------------------------------------------------------------*/
  24. /* Replace the default file upload field with a customised version.
  25. /*-----------------------------------------------------------------------------------*/
  26. recreateFileField: function () {
  27. $( 'input.file').each(function(){
  28. var uploadbutton = '<input class="upload_file_button" type="button" value="Upload" />';
  29. $(this).wrap( '<div class="file_wrap" />' );
  30. $(this).addClass( 'file').css( 'opacity', 0); //set to invisible
  31. $(this).parent().append($( '<div class="fake_file" />').append($( '<input type="text" class="upload" />').attr( 'id',$(this).attr( 'id')+'_file')).val( $(this).val() ).append(uploadbutton));
  32. $(this).bind( 'change', function() {
  33. $( '#'+$(this).attr( 'id')+'_file').val($(this).val());
  34. });
  35. $(this).bind( 'mouseout', function() {
  36. $( '#'+$(this).attr( 'id')+'_file').val($(this).val());
  37. });
  38. });
  39. }, // End recreateFileField
  40. /*-----------------------------------------------------------------------------------*/
  41. /* Use a custom function when working with the Media Uploads popup.
  42. /* Requires jQuery, Media Upload and Thickbox JavaScripts.
  43. /*-----------------------------------------------------------------------------------*/
  44. mediaUpload: function () {
  45. var _self = this;
  46. jQuery.noConflict();
  47. $( 'input.upload_button' ).removeAttr( 'style' );
  48. var formfield,
  49. formID,
  50. btnContent = true;
  51. // On Click
  52. $( 'input.upload_button').live( "click", function () {
  53. formfield = $(this).prev( 'input').attr( 'name' );
  54. formID = $(this).attr( 'rel' );
  55. if( $(this).prev('input').hasClass('choose-id') ) {
  56. formfield = $(this).prev( 'input').attr( 'id' );
  57. }
  58. // Display a custom title for each Thickbox popup.
  59. var jcd_title = '';
  60. if ( $(this).parents( '.section' ).find( '.heading' ) ) { jcd_title = $(this).parents( '.section' ).find( '.heading' ).text(); } // End IF Statement
  61. if ( jcd_title == '' && $(this).parents( '.jcd_metabox_fields' ).prev( 'th' ).find( 'label' ) ) { jcd_title = $(this).parents( '.jcd_metabox_fields' ).prev( 'th' ).find( 'label' ).text(); } // End IF Statement
  62. // For WP 3.5+
  63. if( typeof wp !== "undefined") {
  64. _self.wp_media_uploader.apply( _self, [formfield, formID, jcd_title] );
  65. } else {
  66. tb_show( jcd_title, 'media-upload.php?post_id='+formID+'&amp;title=' + jcd_title + '&amp;is_jcd=yes&amp;TB_iframe=1' );
  67. }
  68. return false;
  69. });
  70. window.original_send_to_editor = window.send_to_editor;
  71. window.send_to_editor = function(html) {
  72. if (formfield) {
  73. // itemurl = $(html).attr( 'href' ); // Use the URL to the main image.
  74. if ( $(html).html(html).find( 'img').length > 0 ) {
  75. itemurl = $(html).html(html).find( 'img').attr( 'src' ); // Use the URL to the size selected.
  76. } else {
  77. // It's not an image. Get the URL to the file instead.
  78. var htmlBits = html.split( "'" ); // jQuery seems to strip out XHTML when assigning the string to an object. Use alternate method.
  79. itemurl = htmlBits[1]; // Use the URL to the file.
  80. var itemtitle = htmlBits[2];
  81. itemtitle = itemtitle.replace( '>', '' );
  82. itemtitle = itemtitle.replace( '</a>', '' );
  83. } // End IF Statement
  84. var image = /(^.*\.jpg|jpeg|png|gif|ico*)/gi;
  85. var document = /(^.*\.pdf|doc|docx|ppt|pptx|odt*)/gi;
  86. var audio = /(^.*\.mp3|m4a|ogg|wav*)/gi;
  87. var video = /(^.*\.mp4|m4v|mov|wmv|avi|mpg|ogv|3gp|3g2*)/gi;
  88. if (itemurl.match(image)) {
  89. btnContent = '<img src="'+itemurl+'" alt="" /><a href="#" class="mlu_remove button">Remove Image</a>';
  90. } else {
  91. // No output preview if it's not an image.
  92. // btnContent = '';
  93. // Standard generic output if it's not an image.
  94. html = '<a href="'+itemurl+'" target="_blank" rel="external">View File</a>';
  95. btnContent = '<div class="no_image"><span class="file_link">'+html+'</span><a href="#" class="mlu_remove button">Remove</a></div>';
  96. }
  97. $( '#' + formfield).val(itemurl);
  98. $( '#' + formfield).siblings( '.screenshot').slideDown().html(btnContent);
  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. /**
  108. * =================================================================
  109. * WordPress 3.5+ Uploader
  110. * =================================================================
  111. */
  112. file_frame: {},
  113. wp_media_uploader: function( formfield, formID, jcd_title ) {
  114. var _self = this,
  115. image = /(^.*\.jpg|jpeg|png|gif|ico*)/gi,
  116. document = /(^.*\.pdf|doc|docx|ppt|pptx|odt*)/gi,
  117. audio = /(^.*\.mp3|m4a|ogg|wav*)/gi,
  118. video = /(^.*\.mp4|m4v|mov|wmv|avi|mpg|ogv|3gp|3g2*)/gi;
  119. // If the media frame already exists, reopen it.
  120. if( _self.file_frame[ formfield ] ) {
  121. _self.file_frame[ formfield ].open();
  122. return;
  123. }
  124. // Create the media frame.
  125. _self.file_frame[formfield] = wp.media.frames[formfield] = wp.media({
  126. title: jcd_title,
  127. button: {
  128. text: 'Use this media'
  129. },
  130. multiple: false
  131. });
  132. // When an image is selected, run a callback
  133. _self.file_frame[formfield].on( 'select', function() {
  134. var attachment = _self.file_frame[formfield].state().get('selection').first().toJSON(),
  135. btnContent, html;
  136. if (attachment.url.match(image)) {
  137. btnContent = '<img src="'+attachment.url+'" alt="" /><a href="#" class="mlu_remove button">Remove Image</a>';
  138. } else {
  139. // No output preview if it's not an image.
  140. // Standard generic output if it's not an image.
  141. html = '<a href="'+attachment.url+'" target="_blank" rel="external">View File</a>';
  142. btnContent = '<div class="no_image"><span class="file_link">'+html+'</span><a href="#" class="mlu_remove button">Remove</a></div>';
  143. }
  144. $( '#' + formfield ).val( attachment.url );
  145. $( '#' + formfield).siblings( '.screenshot').slideDown().html(btnContent);
  146. });
  147. // Open the modal
  148. _self.file_frame[formfield].open();
  149. }
  150. }; // End jcdMLU Object // Don't remove this, or the sky will fall on your head.
  151. /*-----------------------------------------------------------------------------------*/
  152. /* Execute the above methods in the jcdMLU object.
  153. /*-----------------------------------------------------------------------------------*/
  154. $(document).ready(function () {
  155. jcdMLU.removeFile();
  156. jcdMLU.recreateFileField();
  157. jcdMLU.mediaUpload();
  158. });
  159. })(jQuery);