plugin.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /*
  2. Copyright (c) 2003-2013, CKSource - Frederico Knabben. All rights reserved.
  3. For licensing, see LICENSE.html or http://ckeditor.com/license
  4. */
  5. /**
  6. * @file Plugin for inserting Drupal embeded media
  7. */
  8. ( function() {
  9. var numberRegex = /^\d+(?:\.\d+)?$/;
  10. var cssifyLength = function( length )
  11. {
  12. if ( numberRegex.test( length ) )
  13. return length + 'px';
  14. return length;
  15. }
  16. CKEDITOR.plugins.add( 'mediaembed',
  17. {
  18. requires : [ 'dialog', 'fakeobjects' ],
  19. init: function( editor )
  20. {
  21. var addCssObj = CKEDITOR;
  22. if (Drupal.ckeditor_ver == 3) {
  23. addCssObj = editor;
  24. }
  25. addCssObj.addCss(
  26. 'img.cke_mediaembed' +
  27. '{' +
  28. 'background-image: url(' + CKEDITOR.getUrl( this.path + 'images/placeholder.gif' ) + ');' +
  29. 'background-position: center center;' +
  30. 'background-repeat: no-repeat;' +
  31. 'border: 1px solid #a9a9a9;' +
  32. 'width: 80px;' +
  33. 'height: 80px;' +
  34. '}'
  35. );
  36. editor.addCommand( 'mediaembedDialog', new CKEDITOR.dialogCommand( 'mediaembedDialog', { allowedContent : 'div(media_embed);iframe[*](*)' } ) );
  37. editor.ui.addButton( 'MediaEmbed',
  38. {
  39. label: 'Embed Media',
  40. command: 'mediaembedDialog',
  41. icon: this.path + 'images/icon.png'
  42. } );
  43. CKEDITOR.dialog.add( 'mediaembedDialog', this.path + 'dialogs/mediaembed.js' );
  44. },
  45. afterInit : function( editor )
  46. {
  47. var dataProcessor = editor.dataProcessor,
  48. dataFilter = dataProcessor && dataProcessor.dataFilter,
  49. htmlFilter = dataProcessor && dataProcessor.htmlFilter;
  50. if ( htmlFilter )
  51. {
  52. htmlFilter.addRules({
  53. elements :
  54. {
  55. 'div' : function ( element ) {
  56. if( element.attributes['class'] == 'media_embed' ) {
  57. for (var x in element.children) {
  58. if (typeof(element.children[x].attributes) != 'undefined') {
  59. if (typeof(element.children[x].attributes.width) != undefined) {
  60. element.children[x].attributes.width = element.attributes.width;
  61. }
  62. if (typeof(element.children[x].attributes.height) != undefined) {
  63. element.children[x].attributes.height = element.attributes.height;
  64. }
  65. }
  66. }
  67. }
  68. }
  69. }
  70. });
  71. }
  72. if ( dataFilter )
  73. {
  74. dataFilter.addRules(
  75. {
  76. elements :
  77. {
  78. 'div' : function( element )
  79. {
  80. var attributes = element.attributes,
  81. classId = attributes.classid && String( attributes.classid ).toLowerCase();
  82. if (element.attributes[ 'class' ] == 'media_embed') {
  83. var fakeElement = editor.createFakeParserElement(element, 'cke_mediaembed', 'div', true);
  84. var fakeStyle = fakeElement.attributes.style || '';
  85. if (element.children[0] && typeof(element.children[0].attributes) != 'undefined') {
  86. var height = element.children[0].attributes.height,
  87. width = element.children[0].attributes.width;
  88. }
  89. if ( typeof width != 'undefined' )
  90. fakeStyle = fakeElement.attributes.style = fakeStyle + 'width:' + cssifyLength( width ) + ';';
  91. if ( typeof height != 'undefined' )
  92. fakeStyle = fakeElement.attributes.style = fakeStyle + 'height:' + cssifyLength( height ) + ';';
  93. return fakeElement;
  94. }
  95. return element;
  96. }
  97. }
  98. },
  99. 5);
  100. }
  101. }
  102. } );
  103. } )();