123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388 |
- /*
- * Youtube Embed Plugin
- *
- * @author Jonnas Fonini <contato@fonini.net>
- * @version 2.0.0
- */
- ( function() {
- CKEDITOR.plugins.add( 'youtube',
- {
- lang: [ 'en', 'pt', 'ja', 'hu', 'it', 'fr', 'tr', 'ru', 'de', 'ar', 'nl', 'pl', 'vi', 'zh', 'el'],
- init: function( editor )
- {
- editor.addCommand( 'youtube', new CKEDITOR.dialogCommand( 'youtube', {
- allowedContent: 'iframe[!width,!height,!src,!frameborder,!allowfullscreen]; object param[*]'
- }));
- editor.ui.addButton( 'Youtube',
- {
- label : editor.lang.youtube.button,
- toolbar : 'insert',
- command : 'youtube',
- icon : this.path + 'images/icon.png'
- });
- CKEDITOR.dialog.add( 'youtube', function ( instance )
- {
- var video;
- return {
- title : editor.lang.youtube.title,
- minWidth : 500,
- minHeight : 200,
- contents :
- [{
- id : 'youtubePlugin',
- expand : true,
- elements :
- [{
- id : 'txtEmbed',
- type : 'textarea',
- label : editor.lang.youtube.txtEmbed,
- autofocus : 'autofocus',
- onChange : function ( api )
- {
- handleEmbedChange( this, api );
- },
- onKeyUp : function ( api )
- {
- handleEmbedChange( this, api );
- },
- validate : function ()
- {
- if ( this.isEnabled() )
- {
- if ( !this.getValue() )
- {
- alert( editor.lang.youtube.noCode );
- return false;
- }
- else
- if ( this.getValue().length === 0 || this.getValue().indexOf( '//' ) === -1 )
- {
- alert( editor.lang.youtube.invalidEmbed );
- return false;
- }
- }
- }
- },
- {
- type : 'html',
- html : editor.lang.youtube.or + '<hr>'
- },
- {
- type : 'hbox',
- widths : [ '70%', '15%', '15%' ],
- children :
- [
- {
- id : 'txtUrl',
- type : 'text',
- label : editor.lang.youtube.txtUrl,
- onChange : function ( api )
- {
- handleLinkChange( this, api );
- },
- onKeyUp : function ( api )
- {
- handleLinkChange( this, api );
- },
- validate : function ()
- {
- if ( this.isEnabled() )
- {
- if ( !this.getValue() )
- {
- alert( editor.lang.youtube.noCode );
- return false;
- }
- else{
- video = ytVidId(this.getValue());
- if ( this.getValue().length === 0 || video === false)
- {
- alert( editor.lang.youtube.invalidUrl );
- return false;
- }
- }
- }
- }
- },
- {
- type : 'text',
- id : 'txtWidth',
- width : '60px',
- label : editor.lang.youtube.txtWidth,
- 'default' : editor.config.youtube_width != null ? editor.config.youtube_width : '640',
- validate : function ()
- {
- if ( this.getValue() )
- {
- var width = parseInt ( this.getValue() ) || 0;
- if ( width === 0 )
- {
- alert( editor.lang.youtube.invalidWidth );
- return false;
- }
- }
- else {
- alert( editor.lang.youtube.noWidth );
- return false;
- }
- }
- },
- {
- type : 'text',
- id : 'txtHeight',
- width : '60px',
- label : editor.lang.youtube.txtHeight,
- 'default' : editor.config.youtube_height != null ? editor.config.youtube_height : '360',
- validate : function ()
- {
- if ( this.getValue() )
- {
- var height = parseInt ( this.getValue() ) || 0;
- if ( height === 0 )
- {
- alert( editor.lang.youtube.invalidHeight );
- return false;
- }
- }
- else {
- alert( editor.lang.youtube.noHeight );
- return false;
- }
- }
- }
- ]
- },
- {
- type : 'hbox',
- widths : [ '100%' ],
- children :
- [
- {
- id : 'chkResponsive',
- type : 'checkbox',
- label : editor.lang.youtube.txtResponsive,
- 'default' : editor.config.youtube_responsive != null ? editor.config.youtube_responsive : false
- }
- ]
- },
- {
- type : 'hbox',
- widths : [ '55%', '45%' ],
- children :
- [
- {
- id : 'chkRelated',
- type : 'checkbox',
- 'default' : editor.config.youtube_related != null ? editor.config.youtube_related : true,
- label : editor.lang.youtube.chkRelated
- },
- {
- id : 'chkOlderCode',
- type : 'checkbox',
- 'default' : editor.config.youtube_older != null ? editor.config.youtube_older : false,
- label : editor.lang.youtube.chkOlderCode
- }
- ]
- },
- {
- type : 'hbox',
- widths : [ '55%', '45%' ],
- children :
- [
- {
- id : 'chkPrivacy',
- type : 'checkbox',
- label : editor.lang.youtube.chkPrivacy,
- 'default' : editor.config.youtube_privacy != null ? editor.config.youtube_privacy : false
- },
- {
- id : 'chkAutoplay',
- type : 'checkbox',
- 'default' : editor.config.youtube_autoplay != null ? editor.config.youtube_autoplay : false,
- label : editor.lang.youtube.chkAutoplay
- }
- ]
- },
- {
- type : 'hbox',
- widths : [ '55%', '45%'],
- children :
- [
- {
- id : 'txtStartAt',
- type : 'text',
- label : editor.lang.youtube.txtStartAt,
- validate : function ()
- {
- if ( this.getValue() )
- {
- var str = this.getValue();
- if ( !/^(?:(?:([01]?\d|2[0-3]):)?([0-5]?\d):)?([0-5]?\d)$/i.test( str ) )
- {
- alert( editor.lang.youtube.invalidTime );
- return false;
- }
- }
- }
- },
- {
- id: 'empty',
- type: 'html',
- html: ''
- }
- ]
- }
- ]
- }
- ],
- onOk: function()
- {
- var content = '';
- var responsiveStyle='';
- if ( this.getContentElement( 'youtubePlugin', 'txtEmbed' ).isEnabled() )
- {
- content = this.getValueOf( 'youtubePlugin', 'txtEmbed' );
- }
- else {
- var url = '//', params = [], startSecs;
- var width = this.getValueOf( 'youtubePlugin', 'txtWidth' );
- var height = this.getValueOf( 'youtubePlugin', 'txtHeight' );
- if ( this.getContentElement( 'youtubePlugin', 'chkPrivacy' ).getValue() === true )
- {
- url += 'www.youtube-nocookie.com/';
- }
- else {
- url += 'www.youtube.com/';
- }
- url += 'embed/' + video;
- if ( this.getContentElement( 'youtubePlugin', 'chkRelated' ).getValue() === false )
- {
- params.push('rel=0');
- }
- if ( this.getContentElement( 'youtubePlugin', 'chkAutoplay' ).getValue() === true )
- {
- params.push('autoplay=1');
- }
- startSecs = this.getValueOf( 'youtubePlugin', 'txtStartAt' );
- if ( startSecs ){
- var seconds = hmsToSeconds( startSecs );
- params.push('start=' + seconds);
- }
- if ( params.length > 0 )
- {
- url = url + '?' + params.join( '&' );
- }
- if ( this.getContentElement( 'youtubePlugin', 'chkResponsive').getValue() === true ) {
- content += '<div style="position:relative;padding-bottom:56.25%;padding-top:30px;height:0;overflow:hidden;">';
- responsiveStyle = 'style="position: absolute;top: 0;left: 0;width: 100%;height: 100%;"';
- }
- if ( this.getContentElement( 'youtubePlugin', 'chkOlderCode' ).getValue() === true )
- {
- url = url.replace('embed/', 'v/');
- url = url.replace(/&/g, '&');
- if ( url.indexOf('?') === -1 )
- {
- url += '?';
- }
- else {
- url += '&';
- }
- url += 'hl=' + (this.getParentEditor().config.language ? this.getParentEditor().config.language : 'en') + '&version=3';
- content += '<object width="' + width + '" height="' + height + '" ' + responsiveStyle + '>';
- content += '<param name="movie" value="' + url + '"></param>';
- content += '<param name="allowFullScreen" value="true"></param>';
- content += '<param name="allowscriptaccess" value="always"></param>';
- content += '<embed src="' + url + '" type="application/x-shockwave-flash" ';
- content += 'width="' + width + '" height="' + height + '" '+ responsiveStyle + ' allowscriptaccess="always" ';
- content += 'allowfullscreen="true"></embed>';
- content += '</object>';
- }
- else {
- content += '<iframe width="' + width + '" height="' + height + '" src="' + url + '" ' + responsiveStyle;
- content += 'frameborder="0" allowfullscreen></iframe>';
- }
- if ( this.getContentElement( 'youtubePlugin', 'chkResponsive').getValue() === true ) {
- content += '</div>';
- }
- }
-
- var element = CKEDITOR.dom.element.createFromHtml( content );
- var instance = this.getParentEditor();
- instance.insertElement(element);
- }
- };
- });
- }
- });
- })();
- function handleLinkChange( el, api )
- {
- if ( el.getValue().length > 0 )
- {
- el.getDialog().getContentElement( 'youtubePlugin', 'txtEmbed' ).disable();
- }
- else {
- el.getDialog().getContentElement( 'youtubePlugin', 'txtEmbed' ).enable();
- }
- }
- function handleEmbedChange( el, api )
- {
- if ( el.getValue().length > 0 )
- {
- el.getDialog().getContentElement( 'youtubePlugin', 'txtUrl' ).disable();
- }
- else {
- el.getDialog().getContentElement( 'youtubePlugin', 'txtUrl' ).enable();
- }
- }
- /**
- * JavaScript function to match (and return) the video Id
- * of any valid Youtube Url, given as input string.
- * @author: Stephan Schmitz <eyecatchup@gmail.com>
- * @url: http://stackoverflow.com/a/10315969/624466
- */
- function ytVidId( url )
- {
- var p = /^(?:https?:\/\/)?(?:www\.)?(?:youtu\.be\/|youtube\.com\/(?:embed\/|v\/|watch\?v=|watch\?.+&v=))((\w|-){11})(?:\S+)?$/;
- return ( url.match( p ) ) ? RegExp.$1 : false;
- }
- /**
- * Converts time in hms format to seconds only
- */
- function hmsToSeconds( time )
- {
- var arr = time.split(':'), s = 0, m = 1;
- while (arr.length > 0)
- {
- s += m * parseInt(arr.pop(), 10);
- m *= 60;
- }
- return s;
- }
|