forked from starfishmod/jquery-oembed-all
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjquery.oembed.js
More file actions
116 lines (104 loc) · 5.27 KB
/
jquery.oembed.js
File metadata and controls
116 lines (104 loc) · 5.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
var DS;
(function (DS) {
var OEmbedProvider = (function () {
function OEmbedProvider(apiEndpoint, templateRegex, embedTag) {
this.MaxHeight = 400;
this.MaxWidth = 500;
this.ApiEndpoint = apiEndpoint;
this.TemplateRegex = templateRegex;
this.EmbedTag = embedTag;
}
return OEmbedProvider;
})();
DS.OEmbedProvider = OEmbedProvider;
})(DS || (DS = {}));
var DS;
(function (DS) {
var OEmbedYoutube = (function () {
function OEmbedYoutube(el, options) {
this._defaultOptions = {
MaxWidth: null,
MaxHeight: null,
IsIncludeHandle: true,
IsReplaceLink: true,
OnProviderNotFound: null,
OnBeforeEmbed: null,
OnAfterEmbed: null,
OnEmbed: null
};
this._options = $.extend(true, {}, this._defaultOptions, options);
var resourceUrl = el.attr("href");
var regExp = new RegExp("youtube\\.com/watch.+v=[\\w-]+&?", "i");
if (resourceUrl !== null && resourceUrl !== undefined && resourceUrl.match(regExp) !== null) {
var provider = new DS.OEmbedProvider("https://www.youtube.com/embed/$1?wmode=transparent", /.*(?:v\=|be\/|embed\/)([\w\-]+)&?.*/, { Tag: "iframe", Height: 349, Width: 425 });
provider.MaxWidth = this._options.MaxWidth;
provider.MaxHeight = this._options.MaxHeight;
var code = $("<" + provider.EmbedTag.Tag + "/>").attr("src", resourceUrl.replace(provider.TemplateRegex, provider.ApiEndpoint)).attr("width", provider.EmbedTag.Width ? provider.EmbedTag.Width.toString() : "auto").attr("height", provider.EmbedTag.Height ? provider.EmbedTag.Height.toString() : "auto").attr("allowfullscreen", "true").attr("allowscriptaccess", "always").attr("scrolling", "no").attr("frameborder", "0").css("max-height", this._options.MaxHeight ? this._options.MaxHeight.toString() : "auto").css("max-width", this._options.MaxWidth ? this._options.MaxWidth.toString() : "auto");
if (this._options.OnBeforeEmbed != null) {
this._options.OnBeforeEmbed.call(el, code);
}
if (this._options.OnEmbed != null) {
this._options.OnEmbed.call(el, code);
} else {
if (!code) {
return;
}
el.wrap("<div></div>");
var oembedContainer = el.parent();
if (this._options.IsReplaceLink) {
el.remove();
}
if (this._options.IsIncludeHandle && !this._options.IsReplaceLink) {
$("<span>↓</span>").insertBefore(el).click(function (event) {
var span = $(event.currentTarget);
span.html((window.btoa(span.text()) == "JnVhcnI7") ? "↓" : "↑");
span.parent().children().last().toggle();
});
}
oembedContainer.append("<br/>");
try {
code.clone().appendTo(oembedContainer);
} catch (e) {
oembedContainer.append(code);
}
if (this._options.MaxWidth) {
var postWidth = oembedContainer.parent().width();
if (postWidth < this._options.MaxWidth) {
var iframeWidthOrig = $("iframe", oembedContainer).width();
var iframeHeightOrig = $("iframe", oembedContainer).height();
var ratio = iframeWidthOrig / postWidth;
$("iframe", oembedContainer).width(iframeWidthOrig / ratio);
$("iframe", oembedContainer).height(iframeHeightOrig / ratio);
} else {
if (this._options.MaxWidth) {
$("iframe", oembedContainer).width(this._options.MaxWidth);
}
if (this._options.MaxHeight) {
$("iframe", oembedContainer).height(this._options.MaxHeight);
}
}
}
}
if (this._options.OnAfterEmbed != null) {
this._options.OnAfterEmbed.call(el, code);
}
} else if (this._options.OnProviderNotFound !== null) {
this._options.OnProviderNotFound.call(el, resourceUrl);
}
return this;
}
return OEmbedYoutube;
})();
DS.OEmbedYoutube = OEmbedYoutube;
})(DS || (DS = {}));
(function ($) {
$.fn.OEmbedYoutube = function (options) {
return this.each(function () {
var el = $(this);
if (!el.data("OEmbedYoutube")) {
el.data("OEmbedYoutube", new DS.OEmbedYoutube(el, options));
}
});
};
})(jQuery);
//# sourceMappingURL=jquery.oembed.js.map