FIX: Don't cloak videos once they begin playing

This commit is contained in:
Robin Ward 2016-02-29 13:56:38 -05:00
parent 01e1bb53f1
commit d538bcbe40
4 changed files with 40 additions and 4 deletions

View File

@ -7,6 +7,7 @@ import { addToolbarCallback } from 'discourse/components/d-editor';
import { addWidgetCleanCallback } from 'discourse/components/mount-widget'; import { addWidgetCleanCallback } from 'discourse/components/mount-widget';
import { decorateWidget, changeSetting } from 'discourse/widgets/widget'; import { decorateWidget, changeSetting } from 'discourse/widgets/widget';
import { onPageChange } from 'discourse/lib/page-tracker'; import { onPageChange } from 'discourse/lib/page-tracker';
import { preventCloak } from 'discourse/widgets/post-stream';
class PluginApi { class PluginApi {
constructor(version, container) { constructor(version, container) {
@ -253,6 +254,20 @@ class PluginApi {
changeWidgetSetting(widgetName, settingName, newValue) { changeWidgetSetting(widgetName, settingName, newValue) {
changeSetting(widgetName, settingName, newValue); changeSetting(widgetName, settingName, newValue);
} }
/**
* Prevents an element in the post stream from being cloaked.
* This is useful if you are using a plugin such as youtube
* and don't want the video removed once it has begun
* playing.
*
* ```javascript
* api.preventCloak(1234);
* ```
**/
preventCloak(postId) {
preventCloak(postId);
}
} }
let _pluginv01; let _pluginv01;

View File

@ -6,10 +6,15 @@ import { addWidgetCleanCallback } from 'discourse/components/mount-widget';
const CLOAKING_ENABLED = !window.inTestEnv; const CLOAKING_ENABLED = !window.inTestEnv;
const DAY = 1000 * 60 * 60 * 24; const DAY = 1000 * 60 * 60 * 24;
const _dontCloak = {};
let _cloaked = {}; let _cloaked = {};
export function preventCloak(postId) {
_dontCloak[postId] = true;
}
export function cloak(post, component) { export function cloak(post, component) {
if (!CLOAKING_ENABLED || _cloaked[post.id]) { return; } if (!CLOAKING_ENABLED || _cloaked[post.id] || _dontCloak[post.id]) { return; }
const $post = $(`#post_${post.post_number}`); const $post = $(`#post_${post.post_number}`);
_cloaked[post.id] = $post.height(); _cloaked[post.id] = $post.height();

View File

@ -4,7 +4,15 @@ export default {
name: "apply-lazyYT", name: "apply-lazyYT",
initialize() { initialize() {
withPluginApi('0.1', api => { withPluginApi('0.1', api => {
api.decorateCooked($elem => $('.lazyYT', $elem).lazyYT()); api.decorateCooked($elem => $('.lazyYT', $elem).lazyYT({
onPlay(e, $el) {
// don't cloak posts that have playing videos in them
const postId = parseInt($el.closest('article').data('post-id'));
if (postId) {
api.preventCloak(postId);
}
}
}));
}); });
} }
}; };

View File

@ -6,9 +6,12 @@
* Contributors: https://github.com/tylerpearson/lazyYT/graphs/contributors || https://github.com/daugilas/lazyYT/graphs/contributors * Contributors: https://github.com/tylerpearson/lazyYT/graphs/contributors || https://github.com/daugilas/lazyYT/graphs/contributors
* *
* Usage: <div class="lazyYT" data-youtube-id="laknj093n" data-parameters="rel=0">loading...</div> * Usage: <div class="lazyYT" data-youtube-id="laknj093n" data-parameters="rel=0">loading...</div>
*
* Note: Discourse has forked this from the original, beware when updating the file.
*
*/ */
;(function ($) { (function ($) {
'use strict'; 'use strict';
function setUp($el, settings) { function setUp($el, settings) {
@ -100,10 +103,15 @@
.addClass('lazyYT-image-loaded') .addClass('lazyYT-image-loaded')
.on('click', function (e) { .on('click', function (e) {
e.preventDefault(); e.preventDefault();
if (!$el.hasClass('lazyYT-video-loaded') && $thumb.hasClass('lazyYT-image-loaded')) { if (!$el.hasClass('lazyYT-video-loaded') && $thumb.hasClass('lazyYT-image-loaded')) {
$el.html('<iframe src="//www.youtube.com/embed/' + id + '?autoplay=1&' + youtube_parameters + '" frameborder="0" allowfullscreen></iframe>') $el.html('<iframe src="//www.youtube.com/embed/' + id + '?autoplay=1&' + youtube_parameters + '" frameborder="0" allowfullscreen></iframe>')
.addClass('lazyYT-video-loaded'); .addClass('lazyYT-video-loaded');
} }
if (settings.onPlay) {
settings.onPlay(e, $el);
}
}); });
} }
@ -122,4 +130,4 @@
}); });
}; };
}(jQuery)); })(jQuery);