640 | | wp.mce.views.register( 'embed', { |
641 | | shortcode: 'embed', |
642 | | View: _.extend( {}, wp.media.mixin, { |
643 | | template: media.template( 'editor-embed' ), |
644 | | initialize: function( options ) { |
645 | | this.players = []; |
646 | | this.content = options.content; |
647 | | this.parsed = false; |
648 | | this.shortcode = options.shortcode; |
649 | | _.bindAll( this, 'setHtml', 'setNode', 'fetch' ); |
650 | | $( this ).on( 'ready', this.setNode ); |
651 | | }, |
652 | | unbind: function() { |
653 | | var self = this; |
654 | | _.each( this.players, function ( player ) { |
655 | | player.pause(); |
656 | | self.removePlayer( player ); |
657 | | } ); |
658 | | this.players = []; |
659 | | }, |
660 | | setNode: function ( e, node ) { |
661 | | this.node = node; |
662 | | if ( this.parsed ) { |
663 | | this.parseMediaShortcodes(); |
664 | | } else { |
665 | | this.fetch(); |
666 | | } |
667 | | }, |
668 | | fetch: function () { |
669 | | wp.ajax.send( 'parse-embed', { |
670 | | data: { |
671 | | post_ID: $( '#post_ID' ).val(), |
672 | | content: this.shortcode.string() |
673 | | } |
674 | | } ).done( this.setHtml ); |
675 | | }, |
676 | | setHtml: function ( content ) { |
677 | | this.parsed = content; |
678 | | $( this.node ).html( this.getHtml() ); |
| 640 | wp.mce.embedView = _.extend( {}, wp.media.mixin, { |
| 641 | template: media.template( 'editor-embed' ), |
| 642 | initialize: function( options ) { |
| 643 | this.players = []; |
| 644 | this.content = options.content; |
| 645 | this.parsed = false; |
| 646 | this.original = options.url || options.shortcode.string(); |
| 647 | if ( options.url ) { |
| 648 | this.shortcode = '[embed]' + options.url + '[/embed]'; |
| 649 | } else { |
| 650 | this.shortcode = options.shortcode.string(); |
| 651 | } |
| 652 | _.bindAll( this, 'setHtml', 'setNode', 'fetch' ); |
| 653 | $( this ).on( 'ready', this.setNode ); |
| 654 | }, |
| 655 | unbind: function() { |
| 656 | var self = this; |
| 657 | _.each( this.players, function ( player ) { |
| 658 | player.pause(); |
| 659 | self.removePlayer( player ); |
| 660 | } ); |
| 661 | this.players = []; |
| 662 | }, |
| 663 | setNode: function ( e, node ) { |
| 664 | this.node = node; |
| 665 | if ( this.parsed ) { |
691 | | return this.template({ content: this.parsed }); |
| 677 | } ).done( this.setHtml ); |
| 678 | }, |
| 679 | setHtml: function ( content ) { |
| 680 | var test = '<a href', |
| 681 | dom = tinymce.activeEditor.dom; |
| 682 | |
| 683 | this.parsed = content; |
| 684 | |
| 685 | if ( content.substring( 0, test.length ) === test ) { |
| 686 | dom.replace( dom.create( 'P', null, this.original ), this.node ); |
| 687 | } else { |
| 688 | $( $.trim( this.getHtml() ) ).insertBefore( $( this.node ).find( 'ins' ) ); |
693 | | } ), |
| 690 | |
| 691 | this.parseMediaShortcodes(); |
| 692 | }, |
| 693 | parseMediaShortcodes: function () { |
| 694 | var self = this; |
| 695 | $( '.wp-audio-shortcode, .wp-video-shortcode', this.node ).each( function ( i, element ) { |
| 696 | self.players.push( new MediaElementPlayer( element, self.mejsSettings ) ); |
| 697 | } ); |
| 698 | }, |
| 699 | getHtml: function() { |
| 700 | if ( ! this.parsed ) { |
| 701 | return ''; |
| 702 | } |
| 703 | return this.template( { content: this.parsed } ); |
| 704 | } |
| 705 | } ); |
| 706 | |
| 707 | wp.mce.views.register( 'embed', { |
| 708 | shortcode: 'embed', |
| 709 | View: wp.mce.embedView, |
| 710 | edit: function() {} |
| 711 | } ); |
| 712 | |
| 713 | wp.mce.views.register( 'embedURL', { |
| 714 | shortcode: 'embed', |
| 715 | toView: function( content ) { |
| 716 | var re = /^<p>\s*(https?:\/\/[^\s"]+)\s*<\/p>$/gim, |
| 717 | match = re.exec( content ); |
| 718 | |
| 719 | if ( ! match ) { |
| 720 | return; |
| 721 | } |
| 722 | |
| 723 | return { |
| 724 | index: match.index, |
| 725 | content: match[0], |
| 726 | options: { |
| 727 | url: match[1] |
| 728 | } |
| 729 | }; |
| 730 | }, |
| 731 | View: wp.mce.embedView, |