From a711575947abde0ab09b26a38018a523237a0aad Mon Sep 17 00:00:00 2001
From: Arpit Jalan <arpit@techapj.com>
Date: Tue, 22 Jul 2014 08:09:32 +0530
Subject: [PATCH] FEATURE: ship lazyYT plugin by default

---
 .gitignore                                    |  1 +
 plugins/lazyYT/README.md                      |  3 +
 .../javascripts/initializers/lazyYT.js.es6    | 13 ++++
 plugins/lazyYT/assets/javascripts/lazyYT.js   | 59 +++++++++++++++++++
 plugins/lazyYT/assets/stylesheets/lazyYT.css  | 34 +++++++++++
 plugins/lazyYT/plugin.rb                      | 29 +++++++++
 6 files changed, 139 insertions(+)
 create mode 100644 plugins/lazyYT/README.md
 create mode 100644 plugins/lazyYT/assets/javascripts/initializers/lazyYT.js.es6
 create mode 100644 plugins/lazyYT/assets/javascripts/lazyYT.js
 create mode 100644 plugins/lazyYT/assets/stylesheets/lazyYT.css
 create mode 100644 plugins/lazyYT/plugin.rb

diff --git a/.gitignore b/.gitignore
index aa9d88e86..c619451c8 100644
--- a/.gitignore
+++ b/.gitignore
@@ -44,6 +44,7 @@ log/
 # Ignore plugins except for the bundled ones.
 /plugins/*
 !/plugins/emoji/
+!/plugins/lazyYT/
 !/plugins/poll/
 /plugins/*/auto_generated/
 
diff --git a/plugins/lazyYT/README.md b/plugins/lazyYT/README.md
new file mode 100644
index 000000000..430a205f7
--- /dev/null
+++ b/plugins/lazyYT/README.md
@@ -0,0 +1,3 @@
+# lazyYT
+
+Lazy load YouTube videos plugin for [Discourse](http://discourse.org), highly inspired by the [lazyYT](https://github.com/tylerpearson/lazyYT) jQuery plugin.
\ No newline at end of file
diff --git a/plugins/lazyYT/assets/javascripts/initializers/lazyYT.js.es6 b/plugins/lazyYT/assets/javascripts/initializers/lazyYT.js.es6
new file mode 100644
index 000000000..0743a30c3
--- /dev/null
+++ b/plugins/lazyYT/assets/javascripts/initializers/lazyYT.js.es6
@@ -0,0 +1,13 @@
+/**
+  Apply lazyYT when the app boots
+**/
+import { decorateCooked } from 'discourse/lib/plugin-api';
+
+export default {
+  name: "apply-lazyYT",
+  initialize: function(container) {
+    decorateCooked(container, function($elem) {
+      $('.lazyYT', $elem).lazyYT();
+    });
+  }
+};
diff --git a/plugins/lazyYT/assets/javascripts/lazyYT.js b/plugins/lazyYT/assets/javascripts/lazyYT.js
new file mode 100644
index 000000000..747ea6922
--- /dev/null
+++ b/plugins/lazyYT/assets/javascripts/lazyYT.js
@@ -0,0 +1,59 @@
+/*! LazyYT (lazy load Youtube videos plugin) - v0.3.4 - 2014-06-30
+* Usage: <div class="lazyYT" data-youtube-id="laknj093n" ratio="16:9" data-parameters="rel=0">loading...</div>
+* Copyright (c) 2014 Tyler Pearson; Licensed MIT */
+
+
+;(function ($) {
+    'use strict';
+
+    function setUp($el) {
+        var width = $el.data('width'),
+            height = $el.data('height'),
+            ratio = $el.data('ratio'),
+            id = $el.data('youtube-id'),
+            aspectRatio = ['16', '9'],
+            paddingTop = 0,
+            youtubeParameters = $el.data('parameters') || '';
+
+        if (typeof width === 'undefined' || typeof height === 'undefined') {
+          height = 0;
+          width = '100%';
+          aspectRatio = (ratio.split(":")[1] / ratio.split(":")[0]) * 100;
+          paddingTop = aspectRatio + '%';
+        }
+
+        $el.css({
+            'position': 'relative',
+            'height': height,
+            'width': width,
+            'padding-top': paddingTop,
+            'background': 'url(http://img.youtube.com/vi/' + id + '/hqdefault.jpg) center center no-repeat',
+            'cursor': 'pointer',
+            'background-size': 'cover'
+        })
+            .html('<p id="lazyYT-title-' + id + '" class="lazyYT-title"></p><div class="lazyYT-button"></div>')
+            .addClass('lazyYT-image-loaded');
+
+        $.getJSON('https://gdata.youtube.com/feeds/api/videos/' + id + '?v=2&alt=json', function (data) {
+            $('#lazyYT-title-' + id).text(data.entry.title.$t);
+        });
+
+        $el.on('click', function (e) {
+            e.preventDefault();
+            if (!$el.hasClass('lazyYT-video-loaded') && $el.hasClass('lazyYT-image-loaded')) {
+                $el.html('<iframe width="' + width + '" height="' + height + '" src="//www.youtube.com/embed/' + id + '?autoplay=1&' + youtubeParameters + '" style="position:absolute; top:0; left:0; width:100%; height:100%;" frameborder="0" allowfullscreen></iframe>')
+                    .removeClass('lazyYT-image-loaded')
+                    .addClass('lazyYT-video-loaded');
+            }
+        });
+
+    }
+
+    $.fn.lazyYT = function () {
+        return this.each(function () {
+            var $el = $(this).css('cursor', 'pointer');
+            setUp($el);
+        });
+    };
+
+}(jQuery));
diff --git a/plugins/lazyYT/assets/stylesheets/lazyYT.css b/plugins/lazyYT/assets/stylesheets/lazyYT.css
new file mode 100644
index 000000000..3887aa6dd
--- /dev/null
+++ b/plugins/lazyYT/assets/stylesheets/lazyYT.css
@@ -0,0 +1,34 @@
+/*!
+* lazyyt
+* v0.3.4 - 2014-06-30
+* Copyright (c) 2014 Tyler Pearson <ty.pearson@gmail.com> (http://tylerp.me); Licensed MIT %>
+*/
+
+.lazyYT-title {
+    z-index: 100!important;
+    color: #fff!important;
+    font-family: sans-serif!important;
+    font-size: 12px!important;
+    top: 10px!important;
+    left: 12px!important;
+    position: absolute!important;
+    margin: 0!important;
+    padding: 0!important;
+    line-height: 1!important;
+    font-style: normal!important;
+    font-weight: normal!important;
+}
+
+.lazyYT-button {
+    margin: 0!important;
+    padding: 0!important;
+    width: 60px!important;
+    height: 41px!important;
+    z-index: 100!important;
+    position: absolute!important;
+    top: 50%!important;
+    margin-top: -22px!important;
+    left: 50%!important;
+    margin-left: -30px!important;
+    background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAADwAAAApCAYAAABp50paAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAABV9JREFUeNrcWk1IK1cUvrmZGBOjJvr6xKe+Slvroi6kK6GrUkSxO12IC6GgUFBcuOlC8GdRulERV3VRQV0IKhRU0NJupK3tpi1dCRaxffWHGjWZPJNnNJlMz9FzX+ZNkzylcxP1wMdMZiYz97vnu+ee+2Njmc0GyANwgANQDCgAuABOwGOAG6AAiuh+MV3Lo+fc9KyN3qGYvnE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z8XGGvrlJIHSbix46lavc60xISVjc2NsK9vb0ukHKZYeIhG00I7WpeOirxQ3xnZwc99w90MaHx8fFAa2trMZAtYcl542wYOhbXoU7xox8BvmLJFTxLCRcWFkbb29tVv9+vrKyseOnj2SL6MqUEHAA+QcJ1gDl2PTcdldCexeKXCEq5GIrizOzvgC5OUTrI/rtua1ncYsl1nlzm7CjpCKcTld3vtaQbZVlY+SJoBR4wUeHIPUyykDBOwP8mZgTYw1pQQy755N2fsGlxurgO+JUmAxyGh/V7VgHmMjspNn0D+IEZZhOwDf/FrheOn7Lkdgexkm43vfB18rF8JuQGvYCD4DSUH69/B/gccJSqgG+z630euJb6Fv3JaSCfjpBm6McTFqWKNvbqPg6eIefXSbYXVAbc8PIH4EfA1+x620NGj2Cf7KMOG7cm4fi0hK5XUw0KiG1MeHQTHBZGVrFjJ0znuEXpjIItdnX7FHRPCeLeITPt4LmtBEWNiz1XYj7MxZJbmPIJpSy5pUlUXDrPxAzKiBBJN53vk9fE/q0okY4ZVBS7jaL+FWAA/y++OTUmOgsAAAAASUVORK5CYII=')!important;
+}
diff --git a/plugins/lazyYT/plugin.rb b/plugins/lazyYT/plugin.rb
new file mode 100644
index 000000000..7293d2242
--- /dev/null
+++ b/plugins/lazyYT/plugin.rb
@@ -0,0 +1,29 @@
+# name: lazyYT
+# about: Uses the lazyYT plugin to lazy load Youtube videos
+# version: 0.1
+# authors: Arpit Jalan
+
+# javascript
+register_asset "javascripts/lazyYT.js"
+register_asset "javascripts/initializers/lazyYT.js.es6"
+
+# stylesheet
+register_asset "stylesheets/lazyYT.css"
+
+# freedom patch YouTube Onebox
+class Onebox::Engine::YoutubeOnebox
+  include Onebox::Engine
+
+  def to_html
+    if video_id
+      # Avoid making HTTP requests if we are able to get the video ID from the
+      # URL.
+      html = "<div class=\"lazyYT\" data-youtube-id=\"#{video_id}\" data-width=\"480\" data-height=\"270\"></div>"
+    else
+      # Fall back to making HTTP requests.
+      html = raw[:html] || ""
+    end
+
+    rewrite_agnostic(append_params(html))
+  end
+end