audio.js 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. CKEDITOR.dialog.add( 'audio', function ( editor )
  2. {
  3. var lang = editor.lang.audio;
  4. function commitValue( audioNode, extraStyles )
  5. {
  6. var value=this.getValue();
  7. if ( !value && this.id=='id' )
  8. value = generateId();
  9. audioNode.setAttribute( this.id, value);
  10. if ( !value )
  11. return;
  12. switch( this.id )
  13. {
  14. case 'poster':
  15. extraStyles.backgroundImage = 'url(' + value + ')';
  16. break;
  17. case 'width':
  18. extraStyles.width = value + 'px';
  19. break;
  20. case 'height':
  21. extraStyles.height = value + 'px';
  22. break;
  23. }
  24. }
  25. function commitSrc( audioNode, extraStyles, audios )
  26. {
  27. var match = this.id.match(/(\w+)(\d)/),
  28. id = match[1],
  29. number = parseInt(match[2], 10);
  30. var audio = audios[number] || (audios[number]={});
  31. audio[id] = this.getValue();
  32. }
  33. function loadValue( audioNode )
  34. {
  35. if ( audioNode )
  36. this.setValue( audioNode.getAttribute( this.id ) );
  37. else
  38. {
  39. if ( this.id == 'id')
  40. this.setValue( generateId() );
  41. }
  42. }
  43. function loadSrc( audioNode, audios )
  44. {
  45. var match = this.id.match(/(\w+)(\d)/),
  46. id = match[1],
  47. number = parseInt(match[2], 10);
  48. var audio = audios[number];
  49. if (!audio)
  50. return;
  51. this.setValue( audio[ id ] );
  52. }
  53. function generateId()
  54. {
  55. var now = new Date();
  56. return 'audio' + now.getFullYear() + now.getMonth() + now.getDate() + now.getHours() + now.getMinutes() + now.getSeconds();
  57. }
  58. // To automatically get the dimensions of the poster image
  59. var onImgLoadEvent = function()
  60. {
  61. // Image is ready.
  62. var preview = this.previewImage;
  63. preview.removeListener( 'load', onImgLoadEvent );
  64. preview.removeListener( 'error', onImgLoadErrorEvent );
  65. preview.removeListener( 'abort', onImgLoadErrorEvent );
  66. this.setValueOf( 'info', 'width', preview.$.width );
  67. this.setValueOf( 'info', 'height', preview.$.height );
  68. };
  69. var onImgLoadErrorEvent = function()
  70. {
  71. // Error. Image is not loaded.
  72. var preview = this.previewImage;
  73. preview.removeListener( 'load', onImgLoadEvent );
  74. preview.removeListener( 'error', onImgLoadErrorEvent );
  75. preview.removeListener( 'abort', onImgLoadErrorEvent );
  76. };
  77. return {
  78. title : lang.dialogTitle,
  79. minWidth : 'auto',
  80. minHeight : 20,
  81. onShow : function()
  82. {
  83. // Clear previously saved elements.
  84. this.fakeImage = this.audioNode = null;
  85. // To get dimensions of poster image
  86. this.previewImage = editor.document.createElement( 'img' );
  87. var fakeImage = this.getSelectedElement();
  88. if ( fakeImage && fakeImage.data( 'cke-real-element-type' ) && fakeImage.data( 'cke-real-element-type' ) == 'audio' )
  89. {
  90. this.fakeImage = fakeImage;
  91. var audioNode = editor.restoreRealElement( fakeImage ),
  92. audios = [],
  93. sourceList = audioNode.getElementsByTag( 'source', '' );
  94. if (sourceList.count()==0)
  95. sourceList = audioNode.getElementsByTag( 'source', 'cke' );
  96. for ( var i = 0, length = sourceList.count() ; i < length ; i++ )
  97. {
  98. var item = sourceList.getItem( i );
  99. audios.push( {src : item.getAttribute( 'src' ), type: item.getAttribute( 'type' )} );
  100. }
  101. this.audioNode = audioNode;
  102. this.setupContent( audioNode, audios );
  103. }
  104. else
  105. this.setupContent( null, [] );
  106. },
  107. onOk : function()
  108. {
  109. // If there's no selected element create one. Otherwise, reuse it
  110. var audioNode = null;
  111. var extraStyles = {}, audios = [];
  112. if ( !this.fakeImage )
  113. {
  114. audioNode = CKEDITOR.dom.element.createFromHtml( '<cke:a></cke:a>', editor.document );
  115. this.commitContent( audioNode, extraStyles, audios );
  116. audioNode.setAttributes(
  117. {
  118. href : audios[0].src
  119. } );
  120. }
  121. else
  122. {
  123. audioNode = this.audioNode;
  124. }
  125. var innerHtml = '', links = '',
  126. link = lang.linkTemplate || '',
  127. fallbackTemplate = lang.fallbackTemplate || '';
  128. for(var i=0; i<audios.length; i++)
  129. {
  130. var audio = audios[i];
  131. if ( !audio || !audio.src )
  132. continue;
  133. innerHtml += '<cke:source src="' + audio.src + '" type="' + audio.type + '" />';
  134. links += link.replace('%src%', audio.src).replace('%type%', audio.type);
  135. }
  136. audioNode.setHtml( '<div>..</div>' );
  137. // Refresh the fake image.
  138. var newFakeImage = editor.createFakeElement( audioNode, 'cke_audio', 'audio', false );
  139. newFakeImage.setStyles( extraStyles );
  140. if ( this.fakeImage )
  141. {
  142. newFakeImage.replace( this.fakeImage );
  143. editor.getSelection().selectElement( newFakeImage );
  144. }
  145. else
  146. {
  147. // editor.getSelection().selectElement( newFakeImage );
  148. // Insert it in a div
  149. // var div = new CKEDITOR.dom.element( 'DIV', editor.document );
  150. editor.insertElement( newFakeImage );
  151. // div.append( newFakeImage );
  152. }
  153. },
  154. onHide : function()
  155. {
  156. if ( this.previewImage )
  157. {
  158. this.previewImage.removeListener( 'load', onImgLoadEvent );
  159. this.previewImage.removeListener( 'error', onImgLoadErrorEvent );
  160. this.previewImage.removeListener( 'abort', onImgLoadErrorEvent );
  161. this.previewImage.remove();
  162. this.previewImage = null; // Dialog is closed.
  163. }
  164. },
  165. contents :
  166. [
  167. {
  168. id : 'info',
  169. elements :
  170. [
  171. {
  172. type : 'hbox',
  173. widths: [ '', '100px'],
  174. children : [
  175. {
  176. type : 'text',
  177. id : 'poster',
  178. label : lang.poster,
  179. commit : commitValue,
  180. setup : loadValue,
  181. onChange : function()
  182. {
  183. var dialog = this.getDialog(),
  184. newUrl = this.getValue();
  185. //Update preview image
  186. if ( newUrl.length > 0 ) //Prevent from load before onShow
  187. {
  188. dialog = this.getDialog();
  189. var preview = dialog.previewImage;
  190. preview.on( 'load', onImgLoadEvent, dialog );
  191. preview.on( 'error', onImgLoadErrorEvent, dialog );
  192. preview.on( 'abort', onImgLoadErrorEvent, dialog );
  193. preview.setAttribute( 'src', newUrl );
  194. }
  195. }
  196. },
  197. {
  198. type : 'button',
  199. id : 'browse',
  200. hidden : 'true',
  201. style : 'display:inline-block;margin-top:10px;',
  202. filebrowser :
  203. {
  204. action : 'Browse',
  205. target: 'info:poster',
  206. url: editor.config.filebrowserImageBrowseUrl || editor.config.filebrowserBrowseUrl
  207. },
  208. label : editor.lang.common.browseServer
  209. }]
  210. },
  211. {
  212. type : 'hbox',
  213. widths: [ '33%', '33%', '33%'],
  214. children : [
  215. {
  216. type : 'text',
  217. id : 'width',
  218. label : editor.lang.common.width,
  219. 'default' : 400,
  220. validate : CKEDITOR.dialog.validate.notEmpty( lang.widthRequired ),
  221. commit : commitValue,
  222. setup : loadValue
  223. },
  224. {
  225. type : 'text',
  226. id : 'height',
  227. label : editor.lang.common.height,
  228. 'default' : 300,
  229. validate : CKEDITOR.dialog.validate.notEmpty(lang.heightRequired ),
  230. commit : commitValue,
  231. setup : loadValue
  232. },
  233. {
  234. type : 'text',
  235. id : 'id',
  236. label : 'Id',
  237. commit : commitValue,
  238. setup : loadValue
  239. }
  240. ]
  241. },
  242. {
  243. type : 'hbox',
  244. widths: [ '', '100px', '75px'],
  245. children : [
  246. {
  247. type : 'text',
  248. id : 'src0',
  249. label : lang.sourceaudio,
  250. commit : commitSrc,
  251. setup : loadSrc
  252. },
  253. {
  254. type : 'button',
  255. id : 'browse',
  256. hidden : 'true',
  257. style : 'display:inline-block;margin-top:10px;',
  258. filebrowser :
  259. {
  260. action : 'Browse',
  261. target: 'info:src0',
  262. url: editor.config.filebrowserAudioBrowseUrl || editor.config.filebrowserBrowseUrl
  263. },
  264. label : editor.lang.common.browseServer
  265. },
  266. {
  267. id : 'type0',
  268. label : lang.sourceType,
  269. type : 'select',
  270. 'default' : 'audio/mp3',
  271. items :
  272. [
  273. [ 'MP3', 'audio/mp3' ],
  274. [ 'WAV', 'audio/wav' ]
  275. ],
  276. commit : commitSrc,
  277. setup : loadSrc
  278. }]
  279. },
  280. {
  281. type : 'hbox',
  282. widths: [ '', '100px', '75px'],
  283. children : [
  284. {
  285. type : 'text',
  286. id : 'src1',
  287. label : lang.sourceaudio,
  288. commit : commitSrc,
  289. setup : loadSrc
  290. },
  291. {
  292. type : 'button',
  293. id : 'browse',
  294. hidden : 'true',
  295. style : 'display:inline-block;margin-top:10px;',
  296. filebrowser :
  297. {
  298. action : 'Browse',
  299. target: 'info:src1',
  300. url: editor.config.filebrowserAudioBrowseUrl || editor.config.filebrowserBrowseUrl
  301. },
  302. label : editor.lang.common.browseServer
  303. },
  304. {
  305. id : 'type1',
  306. label : lang.sourceType,
  307. type : 'select',
  308. 'default':'audio/wav',
  309. items :
  310. [
  311. [ 'MP3', 'audio/mp3' ],
  312. [ 'WAV', 'audio/wav' ]
  313. ],
  314. commit : commitSrc,
  315. setup : loadSrc
  316. }]
  317. }
  318. ]
  319. }
  320. ]
  321. };
  322. } );