plugin.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  1. /**
  2. * oEmbed Plugin plugin
  3. * Licensed under the MIT license
  4. * jQuery Embed Plugin: http://code.google.com/p/jquery-oembed/ (MIT License)
  5. * Plugin for: http://ckeditor.com/license (GPL/LGPL/MPL: http://ckeditor.com/license)
  6. */
  7. (function() {
  8. CKEDITOR.plugins.add('oembed', {
  9. icons: 'oembed',
  10. hidpi: true,
  11. requires: 'widget,dialog',
  12. lang: ['de', 'en', 'fr', 'nl', 'pl', 'pt-br', 'ru'],
  13. afterInit: function(editor) {
  14. /*var dataProcessor = editor.dataProcessor,
  15. dataFilter = dataProcessor && dataProcessor.dataFilter;
  16. if (editor.config.oembed_ShowIframePreview) {
  17. if (dataFilter.elementsRules.iframe) {
  18. delete dataFilter.elementsRules.iframe;
  19. }
  20. return;
  21. }
  22. if (dataFilter && !dataFilter.elementsRules.iframe) {
  23. dataFilter.addRules({
  24. elements: {
  25. iframe: function(element) {
  26. return editor.createFakeParserElement(element, 'cke_iframe', 'iframe', true);
  27. }
  28. }
  29. });
  30. }*/
  31. },
  32. init: function(editor) {
  33. if (editor.config.oembed_ShowIframePreview == null || editor.config.oembed_ShowIframePreview == 'undefined') {
  34. editor.config.oembed_ShowIframePreview = false;
  35. }
  36. if (!editor.plugins.iframe && !editor.config.oembed_ShowIframePreview) {
  37. CKEDITOR.addCss('img.cke_iframe' +
  38. '{' +
  39. 'background-image: url(' + CKEDITOR.getUrl(CKEDITOR.plugins.getPath('oembed') + 'images/placeholder.png') + ');' +
  40. 'background-position: center center;' +
  41. 'background-repeat: no-repeat;' +
  42. 'border: 1px solid #a9a9a9;' +
  43. 'width: 80px;' +
  44. 'height: 80px;' +
  45. '}'
  46. );
  47. }
  48. // Load jquery?
  49. loadjQueryLibaries();
  50. CKEDITOR.tools.extend(CKEDITOR.editor.prototype, {
  51. oEmbed: function(url, maxWidth, maxHeight, responsiveResize) {
  52. if (url.length < 1 || url.indexOf('http') < 0) {
  53. alert(editor.lang.oembed.invalidUrl);
  54. return false;
  55. }
  56. if (typeof(jQuery.fn.oembed) === 'undefined') {
  57. CKEDITOR.scriptLoader.load(CKEDITOR.getUrl(CKEDITOR.plugins.getPath('oembed') + 'libs/jquery.oembed.min.js'), function() {
  58. embed();
  59. });
  60. } else {
  61. embed();
  62. }
  63. function embed() {
  64. if (maxWidth == null || maxWidth == 'undefined') {
  65. maxWidth = null;
  66. }
  67. if (maxHeight == null || maxHeight == 'undefined') {
  68. maxHeight = null;
  69. }
  70. if (responsiveResize == null || responsiveResize == 'undefined') {
  71. responsiveResize = false;
  72. }
  73. embedCode(url, editor, false, maxWidth, maxHeight, responsiveResize);
  74. }
  75. return true;
  76. }
  77. });
  78. editor.widgets.add('oembed', {
  79. mask: true,
  80. dialog: 'oembed',
  81. button: editor.lang.oembed.button,
  82. allowedContent: 'div(!' + (editor.config.oembed_WrapperClass != null ? editor.config.oembed_WrapperClass : "embeddedContent") + ');div iframe[*]',
  83. template:
  84. '<div class="' + (editor.config.oembed_WrapperClass != null ? editor.config.oembed_WrapperClass : "embeddedContent") + '">' +
  85. '</div>',
  86. upcast: function(element) {
  87. return element.name == 'div' && element.hasClass(editor.config.oembed_WrapperClass != null ? editor.config.oembed_WrapperClass : "embeddedContent");
  88. },
  89. });
  90. var resizeTypeChanged = function() {
  91. var dialog = this.getDialog(),
  92. resizetype = this.getValue(),
  93. maxSizeBox = dialog.getContentElement('general', 'maxSizeBox').getElement(),
  94. sizeBox = dialog.getContentElement('general', 'sizeBox').getElement();
  95. if (resizetype == 'noresize') {
  96. maxSizeBox.hide();
  97. sizeBox.hide();
  98. } else if (resizetype == "custom") {
  99. maxSizeBox.hide();
  100. sizeBox.show();
  101. } else {
  102. maxSizeBox.show();
  103. sizeBox.hide();
  104. }
  105. };
  106. String.prototype.beginsWith = function(string) {
  107. return (this.indexOf(string) === 0);
  108. };
  109. function loadjQueryLibaries() {
  110. if (typeof(jQuery) === 'undefined') {
  111. CKEDITOR.scriptLoader.load('http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js', function() {
  112. if (typeof(jQuery.fn.oembed) === 'undefined') {
  113. CKEDITOR.scriptLoader.load(
  114. CKEDITOR.getUrl(CKEDITOR.plugins.getPath('oembed') + 'libs/jquery.oembed.min.js')
  115. );
  116. }
  117. });
  118. } else if (typeof(jQuery.fn.oembed) === 'undefined') {
  119. CKEDITOR.scriptLoader.load(CKEDITOR.getUrl(CKEDITOR.plugins.getPath('oembed') + 'libs/jquery.oembed.min.js'));
  120. }
  121. }
  122. function embedCode(url, instance, closeDialog, maxWidth, maxHeight, responsiveResize, widget) {
  123. jQuery('body').oembed(url, {
  124. onEmbed: function(e) {
  125. var codeElement,
  126. codeIframe,
  127. elementAdded = false;
  128. if (typeof e.code === 'string') {
  129. codeElement = CKEDITOR.dom.element.createFromHtml(e.code);
  130. if (widget.element.$.firstChild) {
  131. widget.element.$.removeChild(widget.element.$.firstChild);
  132. }
  133. /*if (codeElement.$.tagName == "IFRAME" && editor.config.oembed_ShowIframePreview === false) {
  134. codeIframe = editor.createFakeElement(codeElement, 'cke_iframe', 'iframe', true);
  135. widget.element.appendHtml(codeIframe.$.outerHTML);
  136. } else {
  137. widget.element.appendHtml(e.code);
  138. }*/
  139. widget.element.appendHtml(e.code);
  140. elementAdded = true;
  141. } else if (typeof e.code[0].outerHTML === 'string') {
  142. codeElement = CKEDITOR.dom.element.createFromHtml(e.code[0].outerHTML);
  143. if (widget.element.$.firstChild) {
  144. widget.element.$.removeChild(widget.element.$.firstChild);
  145. }
  146. /*if (codeElement.$.tagName == "IFRAME" && editor.config.oembed_ShowIframePreview === false) {
  147. codeIframe = editor.createFakeElement(codeElement, 'cke_iframe', 'iframe', true);
  148. widget.element.appendHtml(codeIframe.$.outerHTML);
  149. } else {
  150. widget.element.appendHtml(e.code[0].outerHTML);
  151. }*/
  152. widget.element.appendHtml(e.code[0].outerHTML);
  153. elementAdded = true;
  154. } else {
  155. alert(editor.lang.oembed.noEmbedCode);
  156. }
  157. if (elementAdded) {
  158. if (closeDialog) {
  159. CKEDITOR.dialog.getCurrent().hide();
  160. }
  161. }
  162. },
  163. onError: function(externalUrl) {
  164. if (externalUrl.indexOf("vimeo.com") > 0) {
  165. alert(editor.lang.oembed.noVimeo);
  166. } else {
  167. alert(editor.lang.oembed.Error);
  168. }
  169. },
  170. maxHeight: maxHeight,
  171. maxWidth: maxWidth,
  172. useResponsiveResize: responsiveResize,
  173. embedMethod: 'editor'
  174. });
  175. }
  176. CKEDITOR.dialog.add('oembed', function(editor) {
  177. return {
  178. title: editor.lang.oembed.title,
  179. minWidth: CKEDITOR.env.ie && CKEDITOR.env.quirks ? 568 : 550,
  180. minHeight: 155,
  181. onShow: function() {
  182. var resizetype = this.getContentElement('general', 'resizeType').getValue(),
  183. maxSizeBox = this.getContentElement('general', 'maxSizeBox').getElement(),
  184. sizeBox = this.getContentElement('general', 'sizeBox').getElement();
  185. if (resizetype == 'noresize') {
  186. maxSizeBox.hide();
  187. sizeBox.hide();
  188. } else if (resizetype == "custom") {
  189. maxSizeBox.hide();
  190. sizeBox.show();
  191. } else {
  192. maxSizeBox.show();
  193. sizeBox.hide();
  194. }
  195. },
  196. onOk: function() {
  197. },
  198. contents: [{
  199. label: editor.lang.common.generalTab,
  200. id: 'general',
  201. elements: [{
  202. type: 'html',
  203. id: 'oembedHeader',
  204. html: '<div style="white-space:normal;width:500px;padding-bottom:10px">' + editor.lang.oembed.pasteUrl + '</div>'
  205. }, {
  206. type: 'text',
  207. id: 'embedCode',
  208. focus: function() {
  209. this.getElement().focus();
  210. },
  211. label: editor.lang.oembed.url,
  212. title: editor.lang.oembed.pasteUrl,
  213. setup: function(widget) {
  214. if (widget.data.oembed) {
  215. this.setValue(widget.data.oembed);
  216. }
  217. },
  218. commit: function(widget) {
  219. var inputCode = CKEDITOR.dialog.getCurrent().getValueOf('general', 'embedCode'),
  220. resizetype = CKEDITOR.dialog.getCurrent().getContentElement('general', 'resizeType').
  221. getValue(),
  222. maxWidth = null,
  223. maxHeight = null,
  224. responsiveResize = false,
  225. editorInstance = CKEDITOR.dialog.getCurrent().getParentEditor(),
  226. closeDialog = CKEDITOR.dialog.getCurrent().getContentElement('general', 'autoCloseDialog').
  227. getValue();
  228. if (inputCode.length < 1 || inputCode.indexOf('http') < 0) {
  229. alert(editor.lang.oembed.invalidUrl);
  230. return false;
  231. }
  232. if (resizetype == "noresize") {
  233. responsiveResize = false;
  234. } else {
  235. if (resizetype == "responsive") {
  236. maxWidth = this.getContentElement('general', 'maxWidth').
  237. getInputElement().
  238. getValue();
  239. maxHeight = this.getContentElement('general', 'maxHeight').
  240. getInputElement().
  241. getValue();
  242. responsiveResize = true;
  243. } else if (resizetype == "custom") {
  244. maxWidth = this.getContentElement('general', 'width').
  245. getInputElement().
  246. getValue();
  247. maxHeight = this.getContentElement('general', 'height').
  248. getInputElement().
  249. getValue();
  250. responsiveResize = false;
  251. }
  252. }
  253. // support for multiple urls
  254. if (inputCode.indexOf(";") > 0) {
  255. var urls = inputCode.split(";");
  256. for (var i = 0; i < urls.length; i++) {
  257. var url = urls[i];
  258. if (url.length > 1 && url.beginsWith('http')) {
  259. embedCode(url, editorInstance, false, maxWidth, maxHeight, responsiveResize, widget);
  260. }
  261. // close after last
  262. if (i == urls.length - 1) {
  263. CKEDITOR.dialog.getCurrent().hide();
  264. }
  265. }
  266. } else {
  267. // single url
  268. embedCode(inputCode, editorInstance, closeDialog, maxWidth, maxHeight, responsiveResize, widget);
  269. }
  270. widget.setData('oembed', this.getValue());
  271. }
  272. }, {
  273. type: 'hbox',
  274. widths: ['50%', '50%'],
  275. children: [{
  276. id: 'resizeType',
  277. type: 'select',
  278. label: editor.lang.oembed.resizeType,
  279. 'default': 'noresize',
  280. items: [
  281. [editor.lang.oembed.noresize, 'noresize'],
  282. [editor.lang.oembed.responsive, 'responsive'],
  283. [editor.lang.oembed.custom, 'custom']
  284. ],
  285. onChange: resizeTypeChanged
  286. }, {
  287. type: 'hbox',
  288. id: 'maxSizeBox',
  289. widths: ['120px', '120px'],
  290. style: 'float:left;position:absolute;left:58%;width:200px',
  291. children: [{
  292. type: 'text',
  293. width: '100px',
  294. id: 'maxWidth',
  295. 'default': editor.config.oembed_maxWidth != null ? editor.config.oembed_maxWidth : '560',
  296. label: editor.lang.oembed.maxWidth,
  297. title: editor.lang.oembed.maxWidthTitle
  298. }, {
  299. type: 'text',
  300. id: 'maxHeight',
  301. width: '120px',
  302. 'default': editor.config.oembed_maxHeight != null ? editor.config.oembed_maxHeight : '315',
  303. label: editor.lang.oembed.maxHeight,
  304. title: editor.lang.oembed.maxHeightTitle
  305. }]
  306. }, {
  307. type: 'hbox',
  308. id: 'sizeBox',
  309. widths: ['120px', '120px'],
  310. style: 'float:left;position:absolute;left:58%;width:200px',
  311. children: [{
  312. type: 'text',
  313. id: 'width',
  314. width: '100px',
  315. 'default': editor.config.oembed_maxWidth != null ? editor.config.oembed_maxWidth : '560',
  316. label: editor.lang.oembed.width,
  317. title: editor.lang.oembed.widthTitle
  318. }, {
  319. type: 'text',
  320. id: 'height',
  321. width: '120px',
  322. 'default': editor.config.oembed_maxHeight != null ? editor.config.oembed_maxHeight : '315',
  323. label: editor.lang.oembed.height,
  324. title: editor.lang.oembed.heightTitle
  325. }]
  326. }]
  327. }, {
  328. type: 'checkbox',
  329. id: 'autoCloseDialog',
  330. 'default': 'checked',
  331. label: editor.lang.oembed.autoClose,
  332. title: editor.lang.oembed.autoClose
  333. }]
  334. }]
  335. };
  336. });
  337. }//
  338. });
  339. }
  340. )();