Skip to content

Commit 4e932e9

Browse files
committed
feat: callback functions added #10
1 parent fa519c1 commit 4e932e9

6 files changed

Lines changed: 165 additions & 94 deletions

File tree

.jshintrc

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@
33
"jquery": true,
44
"node": true,
55
"esnext": false,
6-
"globals": {},
6+
"globals": {
7+
"hljs":true,
8+
"twttr":true
9+
},
710
"laxcomma": true,
811
"globalstrict": false,
912
"quotmark": "single",

README.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,6 @@ This is the minimal setup in which all features will work in their default confi
9393
Advanced Options
9494
----------------
9595
Only **gdevAuth** option is mandatory. Other options provide you additional control on the plugin.
96-
The below setting shows all the default values.
9796

9897
```html
9998
<script>
@@ -175,6 +174,12 @@ $('#element').embedJS({
175174
onVideoShow:function(){},
176175
//callback on video load (youtube/vimeo)
177176
onVideoLoad:function(){}
177+
//function to execute before embedding services
178+
beforeEmbedJSApply: function () {},
179+
//callback after embedJS is applied
180+
afterEmbedJSLApply: function () {},
181+
//callback after all the twitter widgets are loaded.
182+
onTwitterShow : function () {}
178183
179184
});
180185
</script>

demo/index.html

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,10 @@ <h3>Emoticons and link embed Support</h3>
212212
gdevAuthKey: 'AIzaSyCqFouT8h5DKAbxlrTZmjXEmNBjC69f0ts',
213213
videoWidth : 800,
214214
audioEmbed : true,
215-
excludeEmbed:['twitchTv']
215+
excludeEmbed:['twitchTv','spotify'],
216+
onEmbedJSLoad:function(){
217+
alert('a');
218+
}
216219
});
217220
});
218221
</script>

dist/jquery.embed.js

Lines changed: 75 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -110,25 +110,25 @@
110110

111111
/* VARIABLE DECLARATIONS */
112112
var pluginName = 'embedJS', options = {
113-
embedSelector : 'div', //Refers to the selector inside #element that is to be processed
114-
link : true, //Instructs the library whether or not to embed urls
115-
linkTarget : '_self', //same as the target attribute in html anchor tag . supports all html
116-
// supported target values.
117-
linkExclude : [], //Array of extensions to be excluded from converting into links
118-
pdfEmbed : true, //set true to show a preview of pdf links
119-
imageEmbed : true, //set true to embed images
120-
audioEmbed : false, //set true to embed audio
121-
videoEmbed : true, //set true to show a preview of youtube/vimeo videos with details
122-
basicVideoEmbed : true, //set true to show basic video files like mp4 etc. (supported by html5
123-
// player)
124-
videoWidth : null, //width of the video frame (in pixels)
125-
videoHeight : null, //height of the video frame (in pixels)
126-
gdevAuthKey : null, //( Mandatory ) The authorization key obtained from google's developer
127-
// console for using youtube data api and map embed api
128-
locationEmbed : true,
129-
highlightCode : true, //Instructs the library whether or not to highlight code syntax.
130-
tweetsEmbed : true, //Instructs the library whether or not embed the tweets
131-
tweetOptions : {
113+
embedSelector : 'div', //Refers to the selector inside #element that is to be processed
114+
link : true, //Instructs the library whether or not to embed urls
115+
linkTarget : '_self', //same as the target attribute in html anchor tag . supports all html
116+
// supported target values.
117+
linkExclude : [], //Array of extensions to be excluded from converting into links
118+
pdfEmbed : true, //set true to show a preview of pdf links
119+
imageEmbed : true, //set true to embed images
120+
audioEmbed : false, //set true to embed audio
121+
videoEmbed : true, //set true to show a preview of youtube/vimeo videos with details
122+
basicVideoEmbed : true, //set true to show basic video files like mp4 etc. (supported by html5
123+
// player)
124+
videoWidth : null, //width of the video frame (in pixels)
125+
videoHeight : null, //height of the video frame (in pixels)
126+
gdevAuthKey : null, //( Mandatory ) The authorization key obtained from google's developer
127+
// console for using youtube data api and map embed api
128+
locationEmbed : true,
129+
highlightCode : true, //Instructs the library whether or not to highlight code syntax.
130+
tweetsEmbed : true, //Instructs the library whether or not embed the tweets
131+
tweetOptions : {
132132
maxWidth : 550, //The maximum width of a rendered Tweet in whole pixels. This value must be
133133
// between 220 and 550 inclusive.
134134
hideMedia : false, //When set to true or 1 links in a Tweet are not expanded to photo, video, or
@@ -143,10 +143,11 @@
143143
lang : 'en' //Request returned HTML and a rendered Tweet in the specified
144144
// (https://dev.twitter.com/web/overview/languages)
145145
},
146-
excludeEmbed : [],
147-
codeEmbedHeight : 300,
148-
soundCloudOptions: {
149-
height : 160, themeColor: 'f50000', //Hex Code of the player theme color
146+
excludeEmbed : [],
147+
codeEmbedHeight : 300,
148+
soundCloudOptions : {
149+
height : 160,
150+
themeColor : 'f50000', //Hex Code of the player theme color
150151
autoPlay : false,
151152
hideRelated : false,
152153
showComments: true,
@@ -155,18 +156,25 @@
155156
visual : false, //Show/hide the big preview image
156157
download : false //Show/Hide download buttons
157158
},
158-
vineOptions : {
159+
vineOptions : {
159160
width: 500,
160161
type : 'postcard' //'postcard' or 'simple' embedding
161162
},
162-
beforePdfPreview : function () { //callback before pdf preview
163+
beforePdfPreview : function () { //callback before pdf preview
163164
},
164-
afterPdfPreview : function () { //callback after pdf preview
165+
afterPdfPreview : function () { //callback after pdf preview
165166
},
166-
onVideoShow : function () { // callback on video frame view
167+
onVideoShow : function () { // callback on video frame view
167168
},
168-
onVideoLoad : function () { //callback on video load (youtube/vimeo)
169+
onVideoLoad : function () { //callback on video load (youtube/vimeo)
170+
},
171+
beforeEmbedJSApply: function () { //function to execute before embedding services
172+
},
173+
afterEmbedJSLApply: function () { //callback after embedJS is applied
174+
},
175+
onTwitterShow : function () { //callback after all the twitter widgets are loaded.
169176
}
177+
170178
};
171179
/* ENDS */
172180

@@ -227,11 +235,11 @@
227235
}
228236
};
229237

230-
function urlEmbed(str,opts) {
238+
function urlEmbed(str, opts) {
231239
var urlRegex = /((href|src)=["']|)(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig;
232240
return str.replace(urlRegex, function (match) {
233241
var extension = match.split('.')[match.split('.').length - 1];
234-
if (($.inArray(extension,opts.linkExclude) === -1)) {
242+
if (($.inArray(extension, opts.linkExclude) === -1)) {
235243
return '<a href="' + match + '" target="' + options.linkTarget + '">' + match + '</a>';
236244
}
237245
return match;
@@ -386,7 +394,7 @@
386394
if (matches) {
387395
var i = 0;
388396
while (i < matches.length) {
389-
str = str + '<div class="ejs-video"><iframe src="https://dotsub.com/media/' + matches[i].split('/')[2] + '/embed/" width="' + videoDimensions.width + '" height="' + videoDimensions.height + '"></iframe></div>'
397+
str = str + '<div class="ejs-video"><iframe src="https://dotsub.com/media/' + matches[i].split('/')[2] + '/embed/" width="' + videoDimensions.width + '" height="' + videoDimensions.height + '"></iframe></div>';
390398
i++;
391399
}
392400
}
@@ -400,7 +408,7 @@
400408
if (matches) {
401409
var i = 0;
402410
while (i < matches.length) {
403-
str = str + '<div class="ejs-video"><iframe src="http://www.dailymotion.com/embed/video/' + matches[i].split('/')[2] + '" height="' + videoDimensions.height + '" width="' + videoDimensions.width + '"></iframe></div>'
411+
str = str + '<div class="ejs-video"><iframe src="http://www.dailymotion.com/embed/video/' + matches[i].split('/')[2] + '" height="' + videoDimensions.height + '" width="' + videoDimensions.width + '"></iframe></div>';
404412
i++;
405413
}
406414
}
@@ -413,7 +421,7 @@
413421
if (matches) {
414422
var i = 0;
415423
while (i < matches.length) {
416-
str = str + '<div class="ejs-vine"><iframe src="https://vine.co/v/' + matches[i].split('/')[2] + '/embed/' + opts.vineOptions.type + '" height="' + (opts.vineOptions.type == 'postcard' ? (opts.vineOptions.width + 158) : opts.vineOptions.width) + '" width="' + opts.vineOptions.width + '"></iframe></div>'
424+
str = str + '<div class="ejs-vine"><iframe src="https://vine.co/v/' + matches[i].split('/')[2] + '/embed/' + opts.vineOptions.type + '" height="' + (opts.vineOptions.type == 'postcard' ? (opts.vineOptions.width + 158) : opts.vineOptions.width) + '" width="' + opts.vineOptions.width + '"></iframe></div>';
417425
i++;
418426
}
419427
}
@@ -493,15 +501,14 @@
493501
highlight: function (text) {
494502
if (!window.hljs) {
495503
throw new ReferenceError('hljs is not defined. HighlightJS library is needed to highlight code. Visit https://highlightjs.org/');
496-
return;
497504
}
498505
var that = this;
499506
text = text.replace(/(`+)(\s|[a-z]+)\s*([\s\S]*?[^`])\s*\1(?!`)/gm, function (wholeMatch, m1, m2, m3) {
500507
var c = m3;
501-
c = c.replace(/^([ \t]*)/g, ""); // leading whitespace
502-
c = c.replace(/[ \t]*$/g, ""); // trailing whitespace
508+
c = c.replace(/^([ \t]*)/g, ''); // leading whitespace
509+
c = c.replace(/[ \t]*$/g, ''); // trailing whitespace
503510
c = that.encodeCode(c);
504-
c = c.replace(/:\/\//g, "~P"); // to prevent auto-linking. Not necessary in code
511+
c = c.replace(/:\/\//g, '~P'); // to prevent auto-linking. Not necessary in code
505512
// *blocks*, but in code spans. Will be converted
506513
// back after the auto-linker runs.
507514

@@ -548,7 +555,7 @@
548555
return str;
549556
},
550557

551-
spotifyEmbed: function (rawStr, str, opts) {
558+
spotifyEmbed: function (rawStr, str) {
552559
var spotifyRegex = /spotify.com\/track\/[a-zA-Z0-9_]+/gi;
553560
var matches = rawStr.match(spotifyRegex) ? rawStr.match(spotifyRegex).getUnique() : null;
554561
if (matches) {
@@ -730,10 +737,13 @@
730737
}
731738
return str;
732739
}
733-
}
740+
};
734741

735742
function _driver(elem, settings) {
736-
elem.each(function () {
743+
var len = elem.length;
744+
var deferred = $.Deferred();
745+
elem.each(function (i) {
746+
console.log(i);
737747
var input = $(this).html();
738748
if (input === undefined || input === null) {
739749
return;
@@ -750,7 +760,7 @@
750760
return (($.inArray(serviceName, settings.excludeEmbed) == -1) && (settings.excludeEmbed !== 'all'));
751761
};
752762

753-
input = (settings.link) ? urlEmbed(input,settings) : input;
763+
input = (settings.link) ? urlEmbed(input, settings) : input;
754764
input = emoticonProcess.insertfontSmiley(input);
755765
input = emoticonProcess.insertEmoji(input);
756766
input = (settings.pdfEmbed) ? pdfProcess.embed(rawInput, input) : input;
@@ -770,20 +780,32 @@
770780
input = (ifEmbed('vine')) ? videoProcess.vineEmbed(rawInput, input, settings) : input;
771781
input = (ifEmbed('ted')) ? videoProcess.tedEmbed(rawInput, input, settings) : input;
772782
input = (ifEmbed('liveLeak')) ? videoProcess.liveleakEmbed(rawInput, input, settings) : input;
773-
input = (ifEmbed('spotify')) ? audioProcess.spotifyEmbed(rawInput, input, settings) : input;
783+
input = (ifEmbed('spotify')) ? audioProcess.spotifyEmbed(rawInput, input) : input;
774784
input = (settings.locationEmbed) ? mapProcess.locationEmbed(rawInput, input, settings) : input;
775785

776786
videoProcess.embed(input, settings).then(function (d) {
777-
if (tweetProcess.getMatches(d)) {
787+
if (settings.tweetsEmbed && tweetProcess.getMatches(d)) {
778788
tweetProcess.embed(d, tweetProcess.getMatches(input), settings).then(function (data) {
779789
$(that).html(data);
780790
$(that).css('display', 'block');
781-
if (settings.tweetsEmbed)twttr.widgets.load();
791+
twttr.widgets.load();
792+
twttr.events.bind(
793+
'rendered',
794+
function () {
795+
settings.onTwitterShow();
796+
if (i == len - 1) {
797+
deferred.resolve();
798+
}
799+
}
800+
);
782801
});
783802
}
784803
else {
785804
$(that).html(d);
786805
$(that).css('display', 'block');
806+
if (i == len - 1) {
807+
deferred.resolve();
808+
}
787809
}
788810
});
789811

@@ -792,14 +814,22 @@
792814
videoProcess.play(elem, settings);
793815
pdfProcess.view(elem, settings);
794816

817+
return deferred.promise();
818+
795819
}
796820

797821
/* ENDS */
798822

799823
// Avoid Plugin.prototype conflicts
800824
$.extend(Plugin.prototype, {
801825
init: function (settings, element) {
802-
_driver($(element).find(settings.embedSelector), settings);
826+
//call beforeEmbedJSApply function
827+
settings.beforeEmbedJSApply();
828+
829+
_driver($(element).find(settings.embedSelector), settings).then(function () {
830+
settings.afterEmbedJSLApply();
831+
});
832+
803833
}
804834
});
805835

dist/jquery.embed.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)