1
0
mirror of https://github.com/moparisthebest/kaiwa synced 2024-08-13 17:03:51 -04:00
kaiwa/clientapp/helpers/embedIt.js

62 lines
2.2 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-02 05:48:21 -05:00
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
});
});
$.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;
2014-01-02 05:48:21 -05:00
var maxTbWidth = 100;
2014-01-01 19:24:11 -05:00
data = _.filter(data, function (item, i) {
item.original = urls[i].value;
item.el = urls[i].el;
2014-01-02 05:48:21 -05:00
return item.type === 'video' || item.type === 'photo' || item.type === 'link' || item.type === 'rich';
2014-01-01 19:24:11 -05:00
});
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);
}
2014-01-02 05:48:21 -05:00
if (item.thumbnail_width && item.thumbnail_height && item.thumbnail_width > maxTbWidth) {
var tbratio = maxTbWidth / item.thumbnail_width;
item.thumbnail_width = maxTbWidth;
item.thumbnail_height = parseInt(item.thumbnail_height * tbratio, 10);
}
2014-01-01 19:24:11 -05:00
$(item.el).replaceWith(templates.includes.embeds(item));
});
}
});
2014-01-02 05:48:21 -05:00
}, 5);
2014-01-01 19:24:11 -05:00
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
});
};