This repository has been archived by the owner on Feb 13, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
jquery.instagram-embed-processor.js
105 lines (82 loc) · 3.06 KB
/
jquery.instagram-embed-processor.js
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
var InstagramEmbedProcessor = function ($element, options) {
this._settings = $.extend({
instagramEmbedScriptUrl: '//platform.instagram.com/en_US/embeds.js'
}, options);
this.$container = $element;
this.$container.data('pluginInstagramEmbedProcessor', this);
this.$container.attr('instagram-processor-version', this._getVersion());
};
// store deferred for getting instagram object, use same deferred accross all instances
InstagramEmbedProcessor.prototype._shared = {instagramLoaded: null};
InstagramEmbedProcessor.prototype._getInstagramEmbedScript = function () {
if (!this._shared.instagramLoaded || this._shared.instagramLoaded.state() === 'rejected') {
this._shared.instagramLoaded = $.Deferred();
var self = this;
$.getScript(this._settings.instagramEmbedScriptUrl)
.done(function () {
self._shared.instagramLoaded.resolve();
})
.fail(function () {
self._shared.instagramLoaded.reject(arguments);
console.error('Unable to load instagram embed script!', arguments);
});
}
return this._shared.instagramLoaded.promise();
};
InstagramEmbedProcessor.prototype._sanitizeHtml = function (unsanitizedHtml) {
var html;
if (typeof(unsanitizedHtml) === 'string') {
html = unsanitizedHtml;
} else {
html = unescape(this.$container.attr('instagram-embed-html-unsanitized'));
this.$container.attr('instagram-embed-html-unsanitized', '');
}
return $(html).not('script').prop('outerHTML');
};
InstagramEmbedProcessor.prototype._getVersion = function () {
return '1.0.0';
};
InstagramEmbedProcessor.prototype.html = function (unescapedHtml) {
if (typeof(unescapedHtml) === 'string') {
this.$container.attr('instagram-embed-html', escape(unescapedHtml));
}
return unescape(this.$container.attr('instagram-embed-html'));
};
InstagramEmbedProcessor.prototype.isRendered = function (val) {
return this.$container.data('instagramEmbedRendered') === true;
};
InstagramEmbedProcessor.prototype.prep = function (embedHtml) {
var sanitized = this._sanitizeHtml(embedHtml);
this.html(sanitized);
};
InstagramEmbedProcessor.prototype.insertUnrenderedHtml = function () {
this.$container.html(this.html());
};
InstagramEmbedProcessor.prototype.render = function () {
var rendered;
if (!this.isRendered()) {
this.insertUnrenderedHtml();
var self = this;
rendered = this._getInstagramEmbedScript().done(function () {
instgrm.Embeds.process();
self.$container.data('instagramEmbedRendered', true);
});
} else {
rendered = this._getInstagramEmbedScript();
}
return rendered;
};
InstagramEmbedProcessor.prototype.clear = function () {
this.$container.empty();
this.$container.data('instagramEmbedRendered', false);
};
var createInstagramEmbedProcessor = function (options) {
this.each(function () {
var $this = $(this);
if (!$this.data('pluginInstagramEmbedProcessor')) {
$this.data('pluginInstagramEmbedProcessor', new InstagramEmbedProcessor($this, options));
}
});
return this;
};
$.fn.instagramEmbedProcessor = createInstagramEmbedProcessor;