This repository was archived by the owner on Oct 3, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathjquery-smartvimeoembed.js
92 lines (76 loc) · 2.35 KB
/
jquery-smartvimeoembed.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
/*
* jQuery Smart Vimeo Embed - v1.1.1
* Author: Warren L. Parsons
* Company: Hanson, Inc.
*
* Licensed under the MIT license
*/
;(function ( $, window, document, undefined ) {
var pluginName = "smartVimeoEmbed",
defaults = {
idSelectorName: 'vimeo-id',
vimeoPatternUrl: 'http://vimeo.com/api/oembed.json?url=http%3A%2F%2Fvimeo.com/',
autoplay: true,
width: 640,
onComplete: function(){},
onError: function(){}
};
function Plugin( element, options ) {
this.element = element;
this.options = $.extend( {}, defaults, options);
this._defaults = defaults;
this._name = pluginName;
this.init();
}
Plugin.prototype = {
init: function() {
var options = this.options;
$(this.element).each(function (i, e) {
var $e = $(e);
var id = $e.data(options.idSelectorName);
// only execute on non-vimeo images
if (id && !/VIMEO/i.test($e.attr('src'))) {
// build Vimeo JSON URL
var url = options.vimeoPatternUrl + id + '&autoplay=' + options.autoplay + '&width=' + options.width + '&callback=?';
// fetch video data from Vimeo
$.ajax({
url: url,
dataType: 'jsonp',
success: function(data){
$('#output').text(JSON.stringify(data));
// add wrapper for play icon positioning
$e.wrap('<div class="vimeo-wrapper" />');
// swap placeholder image with video thumbnail
$e.attr('src', data.thumbnail_url);
// add play icon and click event listener
$e.parent().prepend('<span class="play-icon"/>').on('click', function(){
var $this = $(this);
// only append video once
if ( !$this.find('iframe').length ) {
// append video iframe and hide poster
// image and play icon
$this.append(data.html).find('img, .play-icon').hide();
}
if (options.onComplete && typeof(options.onComplete) === 'function') {
options.onComplete.call(this);
}
});
},
error: function(errorSender, errorMsg){
if (options.onError && typeof(options.onError) === 'function') {
options.onError.call(this);
}
}
});
}
});
}
};
$.fn[pluginName] = function ( options ) {
return this.each(function () {
if (!$.data(this, "plugin_" + pluginName)) {
$.data(this, "plugin_" + pluginName, new Plugin( this, options ));
}
});
};
})( jQuery, window, document );