]> git.immae.eu Git - perso/Immae/Projets/packagist/ludivine-ckeditor-component.git/blame - sources/plugins/html5audio/plugin.js
Change html5 audio player
[perso/Immae/Projets/packagist/ludivine-ckeditor-component.git] / sources / plugins / html5audio / plugin.js
CommitLineData
d1844275
IB
1CKEDITOR.plugins.add( 'html5audio', {\r
2 requires: 'widget',\r
3 lang: 'de,en,eu,es,fr,ru,uk,uz',\r
4 icons: 'html5audio',\r
5 init: function( editor ) {\r
6 editor.widgets.add( 'html5audio', {\r
7 button: editor.lang.html5audio.button,\r
8 template: '<div class="ckeditor-html5-audio"></div>', // We add the audio element when needed in the data function, to avoid having an undefined src attribute.\r
9 // See issue #9 on github: https://github.com/iametza/ckeditor-html5-audio/issues/9\r
10 editables: {},\r
11 /*\r
12 * Allowed content rules (http://docs.ckeditor.com/#!/guide/dev_allowed_content_rules):\r
13 * - div-s with text-align,float,margin-left,margin-right inline style rules and required ckeditor-html5-audio class.\r
14 * - audio tags with src and controls attributes.\r
15 */\r
16 allowedContent: 'div(!ckeditor-html5-audio){text-align,float,margin-left,margin-right}; audio[src,controls,autoplay];',\r
17 requiredContent: 'div(ckeditor-html5-audio); audio[src,controls];',\r
18 upcast: function( element ) {\r
19 return element.name === 'div' && element.hasClass( 'ckeditor-html5-audio' );\r
20 },\r
21 dialog: 'html5audio',\r
22 init: function() {\r
23 var src = '';\r
24 var autoplay = '';\r
25 var align = this.element.getStyle( 'text-align' );\r
26\r
27 // If there's a child (the audio element)\r
28 if ( this.element.getChild( 0 ) ) {\r
29 // get it's attributes.\r
30 src = this.element.getChild( 0 ).getAttribute( 'src' );\r
31 autoplay = this.element.getChild( 0 ).getAttribute( 'autoplay' );\r
32 }\r
33\r
34 if ( src ) {\r
35 this.setData( 'src', src );\r
36\r
37 if ( align ) {\r
38 this.setData( 'align', align );\r
39 } else {\r
40 this.setData( 'align', 'none' );\r
41 }\r
42\r
43 if ( autoplay ) {\r
44 this.setData( 'autoplay', 'yes' );\r
45 }\r
46 }\r
47 },\r
48 data: function() {\r
49 // If there is an audio source\r
50 if ( this.data.src ) {\r
51 // and there isn't a child (the audio element)\r
52 if ( !this.element.getChild( 0 ) ) {\r
53 // Create a new <audio> element.\r
54 var audioElement = new CKEDITOR.dom.element( 'audio' );\r
55 // Set the controls attribute.\r
56 audioElement.setAttribute( 'controls', 'controls' );\r
57 // Append it to the container of the plugin.\r
58 this.element.append( audioElement );\r
59 }\r
60 this.element.getChild( 0 ).setAttribute( 'src', this.data.src );\r
61 }\r
62\r
63 this.element.removeStyle( 'float' );\r
64 this.element.removeStyle( 'margin-left' );\r
65 this.element.removeStyle( 'margin-right' );\r
66\r
67 if ( this.data.align === 'none' ) {\r
68 this.element.removeStyle( 'text-align' );\r
69 } else {\r
70 this.element.setStyle( 'text-align', this.data.align );\r
71 }\r
72\r
73 if ( this.data.align === 'left' ) {\r
74 this.element.setStyle( 'float', this.data.align );\r
75 this.element.setStyle( 'margin-right', '10px' );\r
76 } else if ( this.data.align === 'right' ) {\r
77 this.element.setStyle( 'float', this.data.align );\r
78 this.element.setStyle( 'margin-left', '10px' );\r
79 }\r
80\r
81 if ( this.element.getChild( 0 ) ) {\r
82 if ( this.data.autoplay === 'yes' ) {\r
83 this.element.getChild( 0 ).setAttribute( 'autoplay', 'autoplay' );\r
84 } else {\r
85 this.element.getChild( 0 ).removeAttribute( 'autoplay' );\r
86 }\r
87 }\r
88 }\r
89 } );\r
90\r
91 if ( editor.contextMenu ) {\r
92 editor.addMenuGroup( 'html5audioGroup' );\r
93 editor.addMenuItem( 'html5audioPropertiesItem', {\r
94 label: editor.lang.html5audio.audioProperties,\r
95 icon: 'html5audio',\r
96 command: 'html5audio',\r
97 group: 'html5audioGroup'\r
98 });\r
99\r
100 editor.contextMenu.addListener( function( element ) {\r
101 if ( element &&\r
102 element.getChild( 0 ) &&\r
103 element.getChild( 0 ).hasClass &&\r
104 element.getChild( 0 ).hasClass( 'ckeditor-html5-audio' ) ) {\r
105 return { html5audioPropertiesItem: CKEDITOR.TRISTATE_OFF };\r
106 }\r
107 });\r
108 }\r
109\r
110 CKEDITOR.dialog.add( 'html5audio', this.path + 'dialogs/html5audio.js' );\r
111 }\r
112} );\r