sabox-editor.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. (function( $ ) {
  2. 'use strict';
  3. var SABox = {};
  4. var mediaControl = {
  5. // Initializes a new media manager or returns an existing frame.
  6. // @see wp.media.featuredImage.frame()
  7. selector: null,
  8. size: null,
  9. container: null,
  10. frame: function() {
  11. if ( this._frame ) {
  12. return this._frame;
  13. }
  14. this._frame = wp.media( {
  15. title: 'Media',
  16. button: {
  17. text: 'Update'
  18. },
  19. multiple: false
  20. } );
  21. this._frame.on( 'open', this.updateFrame ).state( 'library' ).on( 'select', this.select );
  22. return this._frame;
  23. },
  24. select: function() {
  25. var context = $( '#sabox-custom-profile-image' ),
  26. input = context.find( '#sabox-custom-image' ),
  27. image = context.find( 'img' ),
  28. attachment = mediaControl.frame().state().get( 'selection' ).first().toJSON();
  29. image.attr( 'src', attachment.url );
  30. input.val( attachment.url );
  31. },
  32. init: function() {
  33. var context = $( '#sabox-custom-profile-image' );
  34. context.on( 'click', '#sabox-add-image', function( e ) {
  35. e.preventDefault();
  36. mediaControl.frame().open();
  37. } );
  38. context.on( 'click', '#sabox-remove-image', function( e ) {
  39. var context = $( '#sabox-custom-profile-image' ),
  40. input = context.find( '#sabox-custom-image' ),
  41. image = context.find( 'img' );
  42. e.preventDefault();
  43. input.val( '' );
  44. image.attr( 'src', image.data( 'default' ) );
  45. } );
  46. }
  47. };
  48. $( document ).ready( function() {
  49. if ( $( '#description' ).length > 0 ) {
  50. wp.editor.initialize( 'description', {
  51. tinymce: {
  52. wpautop: true
  53. },
  54. quicktags: true
  55. } );
  56. }
  57. // Add Social Links
  58. $( '.sabox-add-social-link a' ).click( function( e ) {
  59. e.preventDefault();
  60. if ( undefined === SABox.html ) {
  61. SABox.html = '<tr> <th><span class="sabox-drag"></span><select name="sabox-social-icons[]">';
  62. $.each( SABHerlper.socialIcons, function( key, name ) {
  63. SABox.html = SABox.html + '<option value="' + key + '">' + name + '</option>';
  64. } );
  65. SABox.html = SABox.html + '</select></th><td><input name="sabox-social-links[]" type="text" class="regular-text"><span class="dashicons dashicons-trash"></span><td></tr>';
  66. }
  67. $( '#sabox-social-table' ).append( SABox.html );
  68. } );
  69. // Remove Social Link
  70. $( '#sabox-social-table' ).on( 'click', '.dashicons-trash', function() {
  71. var row = $( this ).parents( 'tr' );
  72. row.fadeOut( 'slow', function() {
  73. row.remove();
  74. } );
  75. } );
  76. mediaControl.init();
  77. } );
  78. })( jQuery );