1
0
mirror of https://github.com/moparisthebest/kaiwa synced 2024-11-12 12:35:00 -05:00
kaiwa/clientapp/helpers/embedIt.js

56 lines
1.8 KiB
JavaScript
Raw Normal View History

2014-01-01 19:24:11 -05:00
/*global $, app*/
var _ = require('underscore');
var async = require('async');
var templates = require('../templates');
2014-01-02 03:52:26 -05:00
var embedQueue = window.embedQueue = async.cargo(function (embeds, cb) {
2014-01-01 19:24:11 -05:00
var urls = [];
2014-01-02 03:52:26 -05:00
_.each(embeds, function (embed) {
var url = embed.find('a.source')[0].href;
2014-01-01 19:24:11 -05:00
urls.push({
2014-01-02 03:52:26 -05:00
value: url,
el: embed[0]
2014-01-01 19:24:11 -05:00
});
});
2014-01-02 03:52:26 -05:00
cb();
2014-01-01 19:24:11 -05:00
$.ajax({
// We have to massage the data into the URL ourselves because
// jQuery won't let us have unencoded commas between encoded URLs
url: '/oembed?' + $.param({
maxwidth: 500
2014-01-02 03:52:26 -05:00
}) + '&urls=' + _.map(urls, function (item) { return encodeURIComponent(item.value); }).join(','),
2014-01-01 19:24:11 -05:00
dataType: 'jsonp',
success: function (data) {
var maxWidth = 500;
data = _.filter(data, function (item, i) {
item.original = urls[i].value;
item.el = urls[i].el;
return item.type === 'video' || item.type === 'photo';
});
data.forEach(function (item) {
if (item.width && item.height && item.width > maxWidth) {
var ratio = maxWidth / item.width;
item.width = maxWidth;
item.height = parseInt(item.height * ratio, 10);
}
$(item.el).replaceWith(templates.includes.embeds(item));
});
}
});
}, 10);
module.exports = function ($html, cb) {
cb = cb || function () {};
//if (!app.settings.chatEmbeds) return cb();
var $links;
var batches = [];
var allUrls = [];
2014-01-02 03:52:26 -05:00
var embeds = $html.find('.embed');
if (!embeds.length) embeds = $html.filter('.embed');
2014-01-01 19:24:11 -05:00
2014-01-02 03:52:26 -05:00
_.each(embeds, function (embed) {
embedQueue.push(embeds);
2014-01-01 19:24:11 -05:00
});
};