UNPKG

js-flex-embed

Version:

jQuery Plugin for responsive intrinsic ratio embeds

6 lines 2.37 kB
/*! js-flex-embed - v1.0.0 - 2016-11-20 jQuery Plugin for responsive intrinsic ratio embeds * https://github.com/vmitsaras/js-flex-embed * Copyright (c) 2016 Vasileios Mitsaras (@vmitsaras) * MIT License */ !function(a,b){"use strict";var c="flex-embed",d=c+"-component",e={flexEmbed:"o-flex-embed",flexEmbedContent:"o-flex-embed__content",flexEmbedImg:"o-flex-embed__crop-item",flexEmbedImgShort:"o-flex-embed__crop-item--short",flexEmbedRatio:"o-flex-embed__ratio"};a.componentNamespace=a.componentNamespace||{};var f=a.componentNamespace.FlexEmbed=function(a,c){if(!a)throw new Error("Element required to initialize object");this.element=a,this.$element=b(a),c=c||{},c.height=c.height||parseInt(this.$element.attr("height"),10)||this.$element.data("height"),c.width=c.width||parseInt(this.$element.attr("width"),10)||this.$element.data("width"),c.aspectRatio=c.aspectRatio||this.$element.data("aspect-ratio")||c.height/c.width,this.options=c};f.prototype.init=function(){this.$element.data(d)||(this.$element.data(d,this),this.$element.trigger("beforecreate."+c),this._create(),this.$element.trigger("create."+c))},f.prototype._create=function(){var a=this,c=a.options;this.$element.is("img")?(this.$flexEmbed=this.$element.addClass(e.flexEmbedImg).wrap('<div class="'+e.flexEmbed+'"><div class="'+e.flexEmbedContent+'"></div></div>').closest("."+e.flexEmbed),this.$element.width()>this.$element.height()&&this.$element.addClass(e.flexEmbedImgShort)):this.$flexEmbed=this.$element.addClass(e.flexEmbedContent).wrap('<div class="'+e.flexEmbed+'"></div>').parent(),this.$flexEmbedRatio=b('<div class="'+e.flexEmbedRatio+'"></div>').prependTo(this.$flexEmbed),this.$flexEmbedRatio.css("padding-bottom",100*c.aspectRatio+"%"),this.$element.removeAttr("height").removeAttr("width")}}(this,jQuery),function(a,b){"use strict";var c="flex-embed",d=".js-"+c;b.fn[c]=function(){return this.each(function(){new a.componentNamespace.FlexEmbed(this).init()})},b(document).on("enhance",function(a){b(b(a.target).is(d)&&a.target).add(d,a.target).filter(d)[c]()})}(this,jQuery),function(a,b){"use strict";var c="has-iframe",d=".js-"+c;b.fn[c]=function(){return this.each(function(){var c=b(this).find("iframe");new a.componentNamespace.FlexEmbed(c[0]).init()})},b(document).on("enhance",function(a){b(b(a.target).is(d)&&a.target).add(d,a.target).filter(d)[c]()})}(this,jQuery);