Skip to content
This repository has been archived by the owner on Feb 13, 2019. It is now read-only.

Commit

Permalink
Some cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrew Kos committed Sep 18, 2015
1 parent cdba0ed commit 270bd9a
Showing 1 changed file with 23 additions and 14 deletions.
37 changes: 23 additions & 14 deletions src/jquery.instagram-embed-processor.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,17 @@ var InstagramEmbedProcessor = function ($element, options) {
}, options);
this.$container = $element;
this.$container.data('pluginInstagramEmbedProcessor', this);

this.$container.attr('instagram-processor-version', this._getVersion());
};

// store deferred for getting instagram object
InstagramEmbedProcessor.prototype.shared = {instagramLoaded: null};
// 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') {
if (!this._shared.instagramLoaded || this._shared.instagramLoaded.state() === 'rejected') {

this.shared.instagramLoaded = $.Deferred();
this._shared.instagramLoaded = $.Deferred();

var self = this;
$.getScript(this._settings.instagramEmbedScriptUrl)
Expand All @@ -25,17 +27,25 @@ InstagramEmbedProcessor.prototype._getInstagramEmbedScript = function () {
});
}

return this.shared.instagramLoaded.promise();
return this._shared.instagramLoaded.promise();
};

InstagramEmbedProcessor.prototype.sanitizeHtml = function (html) {
InstagramEmbedProcessor.prototype._sanitizeHtml = function (html) {
return $(html).not('script').prop('outerHTML');
};

InstagramEmbedProcessor.prototype.getVersion = function () {
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;
};
Expand All @@ -49,22 +59,21 @@ InstagramEmbedProcessor.prototype.prep = function (embedHtml) {
html = embedHtml;
}

var sanitized = this.sanitizeHtml(html);
this.$container.attr('instagram-processor-version', this.getVersion());
this.$container.attr('instagram-embed-html', escape(sanitized));
var sanitized = this._sanitizeHtml(html);
this.html(sanitized);
};

InstagramEmbedProcessor.prototype.render = function () {
var rendered;

if (!this.isRendered()) {
var self = this;
rendered = this._getInstagramEmbedScript().done(function () {
var code = this.attr('instagram-embed-html');
this.html(unescape(code));
self.$container.html(self.html());
instgrm.Embeds.process();

this.data('instagramEmbedRendered', true);
}.bind(this.$container));
self.$container.data('instagramEmbedRendered', true);
});
} else {
rendered = this._getInstagramEmbedScript();
}
Expand Down

0 comments on commit 270bd9a

Please sign in to comment.