editor-view.js 7.6 KB

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