!(function(root, factory) {
if (typeof define === 'function' && define.amd) {
define(['jquery'], factory);
} else if (typeof exports === 'object') {
factory(require('jquery'));
} else {
factory(root.jQuery);
}
})(this, function($) {
'use strict';
/**
* Name of the plugin
* @private
* @const
* @type {String}
*/
var PLUGIN_NAME = 'vide';
/**
* Default settings
* @private
* @const
* @type {Object}
*/
var DEFAULTS = {
volume: 1,
playbackRate: 1,
muted: true,
loop: true,
autoplay: true,
position: '50% 50%',
posterType: 'none',
resizing: true,
bgColor: 'transparent',
className: ''
};
/**
* Not implemented error message
* @private
* @const
* @type {String}
*/
var NOT_IMPLEMENTED_MSG = 'Not implemented';
/**
* Parse a string with options
* @private
* @param {String} str
* @returns {Object|String}
*/
function parseOptions(str) {
var obj = {};
var delimiterIndex;
var option;
var prop;
var val;
var arr;
var len;
var i;
// Remove spaces around delimiters and split
arr = str.replace(/\s*:\s*/g, ':').replace(/\s*,\s*/g, ',').split(',');
// Parse a string
for (i = 0, len = arr.length; i < len; i++) {
option = arr[i];
// Ignore urls and a string without colon delimiters
if (
option.search(/^(http|https|ftp):\/\//) !== -1 ||
option.search(':') === -1
) {
break;
}
delimiterIndex = option.indexOf(':');
prop = option.substring(0, delimiterIndex);
val = option.substring(delimiterIndex + 1);
// If val is an empty string, make it undefined
if (!val) {
val = undefined;
}
// Convert a string value if it is like a boolean
if (typeof val === 'string') {
val = val === 'true' || (val === 'false' ? false : val);
}
// Convert a string value if it is like a number
if (typeof val === 'string') {
val = !isNaN(val) ? +val : val;
}
obj[prop] = val;
}
// If nothing is parsed
if (prop == null && val == null) {
return str;
}
return obj;
}
/**
* Parse a position option
* @private
* @param {String} str
* @returns {Object}
*/
function parsePosition(str) {
str = '' + str;
// Default value is a center
var args = str.split(/\s+/);
var x = '50%';
var y = '50%';
var len;
var arg;
var i;
for (i = 0, len = args.length; i < len; i++) {
arg = args[i];
// Convert values
if (arg === 'left') {
x = '0%';
} else if (arg === 'right') {
x = '100%';
} else if (arg === 'top') {
y = '0%';
} else if (arg === 'bottom') {
y = '100%';
} else if (arg === 'center') {
if (i === 0) {
x = '50%';
} else {
y = '50%';
}
} else {
if (i === 0) {
x = arg;
} else {
y = arg;
}
}
}
return { x: x, y: y };
}
/**
* Search a poster
* @private
* @param {String} path
* @param {Function} callback
*/
function findPoster(path, callback) {
var onLoad = function() {
callback(this.src);
};
$('').load(onLoad);
$('
').load(onLoad);
$('
').load(onLoad);
$('
').load(onLoad);
}
/**
* Vide constructor
* @param {HTMLElement} element
* @param {Object|String} path
* @param {Object|String} options
* @constructor
*/
function Vide(element, path, options) {
this.$element = $(element);
// Parse path
if (typeof path === 'string') {
path = parseOptions(path);
}
// Parse options
if (!options) {
options = {};
} else if (typeof options === 'string') {
options = parseOptions(options);
}
// Remove an extension
if (typeof path === 'string') {
path = path.replace(/\.\w*$/, '');
} else if (typeof path === 'object') {
for (var i in path) {
if (path.hasOwnProperty(i)) {
path[i] = path[i].replace(/\.\w*$/, '');
}
}
}
this.settings = $.extend({}, DEFAULTS, options);
this.path = path;
// https://github.com/VodkaBears/Vide/issues/110
try {
this.init();
} catch (e) {
if (e.message !== NOT_IMPLEMENTED_MSG) {
throw e;
}
}
}
/**
* Initialization
* @public
*/
Vide.prototype.init = function() {
var vide = this;
var path = vide.path;
var poster = path;
var sources = '';
var $element = vide.$element;
var settings = vide.settings;
var position = parsePosition(settings.position);
var posterType = settings.posterType;
var $video;
var $wrapper;
// Set styles of a video wrapper
$wrapper = vide.$wrapper = $('