| 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 | if ( options.url ) { |
| | 647 | this.shortcode = '[embed]' + options.url + '[/embed]'; |
| | 648 | } else { |
| | 649 | this.shortcode = options.shortcode.string(); |
| | 650 | } |
| | 651 | _.bindAll( this, 'setHtml', 'setNode', 'fetch' ); |
| | 652 | $( this ).on( 'ready', this.setNode ); |
| | 653 | }, |
| | 654 | unbind: function() { |
| | 655 | var self = this; |
| | 656 | _.each( this.players, function ( player ) { |
| | 657 | player.pause(); |
| | 658 | self.removePlayer( player ); |
| | 659 | } ); |
| | 660 | this.players = []; |
| | 661 | }, |
| | 662 | setNode: function ( e, node ) { |
| | 663 | this.node = node; |
| | 664 | if ( this.parsed ) { |
| 691 | | return this.template({ content: this.parsed }); |
| | 676 | } ).done( this.setHtml ); |
| | 677 | }, |
| | 678 | setHtml: function ( content ) { |
| | 679 | var test = '<a href', |
| | 680 | dom = tinymce.activeEditor.dom; |
| | 681 | |
| | 682 | this.parsed = content; |
| | 683 | |
| | 684 | if ( content.substring( 0, test.length ) === test ) { |
| | 685 | dom.replace( dom.create( 'P', null, content ), this.node ); |
| | 686 | } else { |
| | 687 | $( this.node ).html( this.getHtml() ); |
| 693 | | } ), |
| | 689 | |
| | 690 | this.parseMediaShortcodes(); |
| | 691 | }, |
| | 692 | parseMediaShortcodes: function () { |
| | 693 | var self = this; |
| | 694 | $( '.wp-audio-shortcode, .wp-video-shortcode', this.node ).each( function ( i, element ) { |
| | 695 | self.players.push( new MediaElementPlayer( element, self.mejsSettings ) ); |
| | 696 | } ); |
| | 697 | }, |
| | 698 | getHtml: function() { |
| | 699 | if ( ! this.parsed ) { |
| | 700 | return ''; |
| | 701 | } |
| | 702 | return this.template( { content: this.parsed } ); |
| | 703 | } |
| | 704 | } ); |
| | 705 | |
| | 706 | wp.mce.views.register( 'embed', { |
| | 707 | shortcode: 'embed', |
| | 708 | View: wp.mce.embedView, |
| | 709 | edit: function() {} |
| | 710 | } ); |
| | 711 | |
| | 712 | wp.mce.views.register( 'embedURL', { |
| | 713 | shortcode: 'embed', |
| | 714 | toView: function( content ) { |
| | 715 | var re = /^<p>\s*(https?:\/\/[^\s"]+)\s*<\/p>$/gim, |
| | 716 | match = re.exec( content ); |
| | 717 | |
| | 718 | if ( ! match ) { |
| | 719 | return; |
| | 720 | } |
| | 721 | |
| | 722 | return { |
| | 723 | index: match.index, |
| | 724 | content: match[0], |
| | 725 | options: { |
| | 726 | url: match[1] |
| | 727 | } |
| | 728 | }; |
| | 729 | }, |
| | 730 | View: wp.mce.embedView, |