No Description

editor-view.js 7.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. /* global tinyMCE, vpEditorView */
  2. ( function ( $, wp, vpEditorView ) {
  3. wp.mce = wp.mce || {};
  4. if ( 'undefined' === typeof wp.mce.views ) {
  5. return;
  6. }
  7. wp.mce.videopress_wp_view_renderer = {
  8. shortcode_string: 'videopress',
  9. shortcode_data: {},
  10. defaults: {
  11. w: '',
  12. at: '',
  13. permalink: true,
  14. hd: false,
  15. loop: false,
  16. freedom: false,
  17. autoplay: false,
  18. flashonly: false,
  19. },
  20. coerce: wp.media.coerce,
  21. template: wp.template( 'videopress_iframe_vnext' ),
  22. getContent: function () {
  23. var urlargs = 'for=' + encodeURIComponent( vpEditorView.home_url_host ),
  24. named = this.shortcode.attrs.named,
  25. options,
  26. key,
  27. width;
  28. for ( key in named ) {
  29. switch ( key ) {
  30. case 'at':
  31. if ( parseInt( named[ key ], 10 ) ) {
  32. urlargs += '&' + key + '=' + parseInt( named[ key ], 10 );
  33. } // Else omit, as it's the default.
  34. break;
  35. case 'permalink':
  36. if ( 'false' === named[ key ] ) {
  37. urlargs += '&' + key + '=0';
  38. } // Else omit, as it's the default.
  39. break;
  40. case 'hd':
  41. case 'loop':
  42. case 'autoplay':
  43. if ( 'true' === named[ key ] ) {
  44. urlargs += '&' + key + '=1';
  45. } // Else omit, as it's the default.
  46. break;
  47. default:
  48. // Unknown parameters? Ditch it!
  49. break;
  50. }
  51. }
  52. options = {
  53. width: vpEditorView.content_width,
  54. height: vpEditorView.content_width * 0.5625,
  55. guid: this.shortcode.attrs.numeric[ 0 ],
  56. urlargs: urlargs,
  57. };
  58. if ( typeof named.w !== 'undefined' ) {
  59. width = parseInt( named.w, 10 );
  60. if ( width >= vpEditorView.min_content_width && width < vpEditorView.content_width ) {
  61. options.width = width;
  62. options.height = parseInt( width * 0.5625, 10 );
  63. }
  64. }
  65. options.ratio = 100 * ( options.height / options.width );
  66. return this.template( options );
  67. },
  68. edit: function ( data ) {
  69. var shortcode_data = wp.shortcode.next( this.shortcode_string, data ),
  70. named = shortcode_data.shortcode.attrs.named,
  71. editor = tinyMCE.activeEditor,
  72. renderer = this,
  73. oldRenderFormItem = tinyMCE.ui.FormItem.prototype.renderHtml;
  74. /**
  75. * Override TextBox renderHtml to support html5 attrs.
  76. * @link https://github.com/tinymce/tinymce/pull/2784
  77. *
  78. * @returns {string}
  79. */
  80. tinyMCE.ui.TextBox.prototype.renderHtml = function () {
  81. var self = this,
  82. settings = self.settings,
  83. element = document.createElement( settings.multiline ? 'textarea' : 'input' ),
  84. extraAttrs = [
  85. 'rows',
  86. 'spellcheck',
  87. 'maxLength',
  88. 'size',
  89. 'readonly',
  90. 'min',
  91. 'max',
  92. 'step',
  93. 'list',
  94. 'pattern',
  95. 'placeholder',
  96. 'required',
  97. 'multiple',
  98. ],
  99. i,
  100. key;
  101. for ( i = 0; i < extraAttrs.length; i++ ) {
  102. key = extraAttrs[ i ];
  103. if ( typeof settings[ key ] !== 'undefined' ) {
  104. element.setAttribute( key, settings[ key ] );
  105. }
  106. }
  107. if ( settings.multiline ) {
  108. element.innerText = self.state.get( 'value' );
  109. } else {
  110. element.setAttribute( 'type', settings.subtype ? settings.subtype : 'text' );
  111. element.setAttribute( 'value', self.state.get( 'value' ) );
  112. }
  113. element.id = self._id;
  114. element.className = self.classes;
  115. element.setAttribute( 'hidefocus', 1 );
  116. if ( self.disabled() ) {
  117. element.disabled = true;
  118. }
  119. return element.outerHTML;
  120. };
  121. tinyMCE.ui.FormItem.prototype.renderHtml = function () {
  122. _.each(
  123. vpEditorView.modal_labels,
  124. function ( value, key ) {
  125. if ( value === this.settings.items.text ) {
  126. this.classes.add( 'videopress-field-' + key );
  127. }
  128. },
  129. this
  130. );
  131. if (
  132. _.contains(
  133. [
  134. vpEditorView.modal_labels.hd,
  135. vpEditorView.modal_labels.permalink,
  136. vpEditorView.modal_labels.autoplay,
  137. vpEditorView.modal_labels.loop,
  138. vpEditorView.modal_labels.freedom,
  139. vpEditorView.modal_labels.flashonly,
  140. ],
  141. this.settings.items.text
  142. )
  143. ) {
  144. this.classes.add( 'videopress-checkbox' );
  145. }
  146. return oldRenderFormItem.call( this );
  147. };
  148. /**
  149. * Populate the defaults.
  150. */
  151. _.each(
  152. this.defaults,
  153. function ( value, key ) {
  154. named[ key ] = this.coerce( named, key );
  155. },
  156. this
  157. );
  158. /**
  159. * Declare the fields that will show in the popup when editing the shortcode.
  160. */
  161. editor.windowManager.open( {
  162. title: vpEditorView.modal_labels.title,
  163. id: 'videopress-shortcode-settings-modal',
  164. width: 520,
  165. height: 240,
  166. body: [
  167. {
  168. type: 'textbox',
  169. disabled: true,
  170. name: 'guid',
  171. label: vpEditorView.modal_labels.guid,
  172. value: shortcode_data.shortcode.attrs.numeric[ 0 ],
  173. },
  174. {
  175. type: 'textbox',
  176. subtype: 'number',
  177. min: vpEditorView.min_content_width, // The `min` may supported be in the future. https://github.com/tinymce/tinymce/pull/2784
  178. name: 'w',
  179. label: vpEditorView.modal_labels.w,
  180. value: named.w,
  181. },
  182. {
  183. type: 'textbox',
  184. subtype: 'number',
  185. min: 0, // The `min` may supported be in the future. https://github.com/tinymce/tinymce/pull/2784
  186. name: 'at',
  187. label: vpEditorView.modal_labels.at,
  188. value: named.at,
  189. },
  190. {
  191. type: 'checkbox',
  192. name: 'hd',
  193. label: vpEditorView.modal_labels.hd,
  194. checked: named.hd,
  195. },
  196. {
  197. type: 'checkbox',
  198. name: 'permalink',
  199. label: vpEditorView.modal_labels.permalink,
  200. checked: named.permalink,
  201. },
  202. {
  203. type: 'checkbox',
  204. name: 'autoplay',
  205. label: vpEditorView.modal_labels.autoplay,
  206. checked: named.autoplay,
  207. },
  208. {
  209. type: 'checkbox',
  210. name: 'loop',
  211. label: vpEditorView.modal_labels.loop,
  212. checked: named.loop,
  213. },
  214. {
  215. type: 'checkbox',
  216. name: 'freedom',
  217. label: vpEditorView.modal_labels.freedom,
  218. checked: named.freedom,
  219. },
  220. {
  221. type: 'checkbox',
  222. name: 'flashonly',
  223. label: vpEditorView.modal_labels.flashonly,
  224. checked: named.flashonly,
  225. },
  226. ],
  227. onsubmit: function ( e ) {
  228. var args = {
  229. tag: renderer.shortcode_string,
  230. type: 'single',
  231. attrs: {
  232. named: _.pick( e.data, _.keys( renderer.defaults ) ),
  233. numeric: [ e.data.guid ],
  234. },
  235. };
  236. if ( '0' === args.attrs.named.at ) {
  237. args.attrs.named.at = '';
  238. }
  239. _.each(
  240. renderer.defaults,
  241. function ( value, key ) {
  242. args.attrs.named[ key ] = this.coerce( args.attrs.named, key );
  243. if ( value === args.attrs.named[ key ] ) {
  244. delete args.attrs.named[ key ];
  245. }
  246. },
  247. renderer
  248. );
  249. editor.insertContent( wp.shortcode.string( args ) );
  250. },
  251. onopen: function ( e ) {
  252. var prefix = 'mce-videopress-field-';
  253. _.each( [ 'w', 'at' ], function ( value ) {
  254. e.target.$el
  255. .find( '.' + prefix + value + ' .mce-container-body' )
  256. .append(
  257. '<span class="' +
  258. prefix +
  259. 'unit ' +
  260. prefix +
  261. 'unit-' +
  262. value +
  263. '">' +
  264. vpEditorView.modal_labels[ value + '_unit' ]
  265. );
  266. } );
  267. $( 'body' ).addClass( 'modal-open' );
  268. },
  269. onclose: function () {
  270. $( 'body' ).removeClass( 'modal-open' );
  271. },
  272. } );
  273. // Set it back to its original renderer.
  274. tinyMCE.ui.FormItem.prototype.renderHtml = oldRenderFormItem;
  275. },
  276. };
  277. wp.mce.views.register( 'videopress', wp.mce.videopress_wp_view_renderer );
  278. // Extend the videopress one to also handle `wpvideo` instances.
  279. wp.mce.wpvideo_wp_view_renderer = _.extend( {}, wp.mce.videopress_wp_view_renderer, {
  280. shortcode_string: 'wpvideo',
  281. } );
  282. wp.mce.views.register( 'wpvideo', wp.mce.wpvideo_wp_view_renderer );
  283. } )( jQuery, wp, vpEditorView );