forked from GumbyFramework/Gumby
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gumby.min.js
1 lines (1 loc) · 19.3 KB
/
gumby.min.js
1
!function(t){"use strict";function i(){this.$dom=t(document),this.$html=this.$dom.find("html"),this.isOldie=!!this.$html.hasClass("oldie"),this.click="click",this.onReady=this.onOldie=this.onTouch=!1,this.autoInit="false"===t("script[gumby-init]").attr("gumby-init")?!1:!0,this.debugMode=Boolean(t("script[gumby-debug]").length),this.touchDevice=!!(Modernizr.touch||window.navigator.userAgent.indexOf("Windows Phone")>0),this.gumbyTouch=!1,this.touchEvents="js/libs",this.breakpoint=Number(t("script[gumby-breakpoint]").attr("gumby-breakpoint"))||768,this.touchEventsLoaded=!1,this.uiModulesReady=!1,this.uiModules={},this.inits={};var i=t("script[gumby-touch]").attr("gumby-touch"),e=t("script[gumby-path]").attr("gumby-path");"false"===i?this.touchEvents=!1:i?this.touchEvents=i:e&&(this.touchEvents=e),this.touchDevice&&(this.click+=" tap"),this.touchDevice&&t(window).width()<this.breakpoint?(this.$html.addClass("gumby-touch"),this.gumbyTouch=!0):this.$html.addClass("gumby-no-touch"),this.debugMode&&this.debug("Gumby is in debug mode")}i.prototype.init=function(t){var i=this,e=t?t:{};return this.$dom.ready(function(){e.debug&&(i.debugMode=!0),i.debug("Initializing Gumby");var t=e.uiModules?e.uiModules:!1;i.initUIModules(t),i.onReady&&i.onReady(),i.isOldie&&i.onOldie&&i.onOldie(),Modernizr.touch&&i.onTouch&&i.onTouch()}),this},i.prototype.helpers=function(){this.onReady&&this.onReady(),this.isOldie&&this.onOldie&&this.onOldie(),Modernizr.touch&&this.onTouch&&this.onTouch()},i.prototype.ready=function(t){return t&&"function"==typeof t&&(this.onReady=t),this},i.prototype.oldie=function(t){return t&&"function"==typeof t&&(this.onOldie=t),this},i.prototype.touch=function(t){return t&&"function"==typeof t&&(this.onTouch=t),this},i.prototype.console=function(t,i){this.debugMode&&window.console&&console[console[t]?t:"log"](i.length>1?Array.prototype.slice.call(i):i[0])},i.prototype.log=function(){this.console("log",arguments)},i.prototype.debug=function(){this.console("debug",arguments)},i.prototype.warn=function(){this.console("warn",arguments)},i.prototype.error=function(){this.console("error",arguments)},i.prototype.dump=function(){return{$dom:this.$dom,isOldie:this.isOldie,touchEvents:this.touchEvents,debugMode:this.debugMode,autoInit:this.autoInit,uiModules:this.uiModules,click:this.click}},i.prototype.selectAttr=function(){for(var t=0;t<arguments.length;t++){var i=arguments[t],e="data-"+arguments[t],n="gumby-"+arguments[t];if(this.is("["+e+"]"))return this.attr(e)?this.attr(e):!0;if(this.is("["+n+"]"))return this.attr(n)?this.attr(n):!0;if(this.is("["+i+"]"))return this.attr(i)?this.attr(i):!0}return!1},i.prototype.addInitalisation=function(t,i){this.inits[t]=i},i.prototype.initialize=function(t,i){if("object"==typeof t){var e=0;for(e;e<t.length;e++)this.inits[t[e]]&&"function"==typeof this.inits[t[e]]?this.inits[t[e]](i):this.error("Error initializing module: "+t[e])}else this.inits[t]&&"function"==typeof this.inits[t]?this.inits[t](i):this.error("Error initializing module: "+t);return this},i.prototype.UIModule=function(t){var i=t.module;this.uiModules[i]=t},i.prototype.initUIModules=function(t){var i,e,n=this.uiModules;t&&(n=t);for(i in n)e=t?n[i]:i,this.uiModules[e].init()},window.Gumby=new i}(jQuery),!function(t){"use strict";function i(t){Gumby.debug("Initializing Checkbox",t),this.$el=t,this.$input=this.$el.find("input[type=checkbox]");var i=this;this.$el.on(Gumby.click,function(t){t.preventDefault(),i.$input.is("[disabled]")||(i.$el.hasClass("checked")?i.update(!1):i.update(!0))}).on("gumby.check",function(){Gumby.debug("Check event triggered",i.$el),i.update(!0)}).on("gumby.uncheck",function(){Gumby.debug("Uncheck event triggered",i.$el),i.update(!1)}),(this.$input.prop("checked")||this.$el.hasClass("checked"))&&i.update(!0)}i.prototype.update=function(t){var i=this.$el.find("span");t?(Gumby.debug("Checking Checkbox",this.$el),i.append('<i class="icon-check" />'),this.$input.prop("checked",!0),Gumby.debug("Triggering onCheck event",this.$el),Gumby.debug("Triggering onChange event",this.$el),this.$el.addClass("checked").trigger("gumby.onCheck").trigger("gumby.onChange")):(Gumby.debug("Unchecking Checkbox",this.$el),this.$input.prop("checked",!1),i.find("i").remove(),Gumby.debug("Triggering onUncheck event",this.$el),Gumby.debug("Triggering onChange event",this.$el),this.$el.removeClass("checked").trigger("gumby.onUncheck").trigger("gumby.onChange"))},Gumby.addInitalisation("checkbox",function(){t(".checkbox").each(function(){var e=t(this);return e.data("isCheckbox")?!0:(e.data("isCheckbox",!0),new i(e),void 0)})}),Gumby.UIModule({module:"checkbox",events:["onCheck","onUncheck","onChange","check","uncheck"],init:function(){Gumby.initialize("checkbox")}})}(jQuery),!function(t){"use strict";function i(i){Gumby.debug("Initializing Fixed Position",i),this.$el=i,this.fixedPoint="",this.pinPoint=!1,this.offset=0,this.pinOffset=0,this.top=0,this.constrainEl=!0,this.state=!1,this.measurements={left:0,width:0},this.setup();var e=this;t(window).on("scroll load",function(){e.monitorScroll()}),this.$el.on("gumby.initialize",function(){Gumby.debug("Re-initializing Fixed Position",i),e.setup(),e.monitorScroll()})}i.prototype.setup=function(){var i=this;this.fixedPoint=this.parseAttrValue(Gumby.selectAttr.apply(this.$el,["fixed"])),this.pinPoint=Gumby.selectAttr.apply(this.$el,["pin"])||!1,this.offset=Number(Gumby.selectAttr.apply(this.$el,["offset"]))||0,this.pinOffset=Number(Gumby.selectAttr.apply(this.$el,["pinoffset"]))||0,this.top=Number(Gumby.selectAttr.apply(this.$el,["top"]))||0,this.constrainEl=Gumby.selectAttr.apply(this.$el,["constrain"])||!0,"false"===this.constrainEl&&(this.constrainEl=!1),this.$parent=this.$el.parents(".columns, .column, .row"),this.$parent=this.$parent.length?this.$parent.first():!1,this.parentRow=this.$parent?!!this.$parent.hasClass("row"):!1,this.pinPoint&&(this.pinPoint=this.parseAttrValue(this.pinPoint)),this.$parent&&this.constrainEl&&(this.measure(),t(window).resize(function(){i.state&&(i.measure(),i.constrain())}))},i.prototype.monitorScroll=function(){var i=t(window).scrollTop(),e=this.fixedPoint instanceof jQuery?this.fixedPoint.offset().top:this.fixedPoint,n=!1;this.pinPoint&&(n=this.pinPoint instanceof jQuery?this.pinPoint.offset().top:this.pinPoint),this.offset&&(e-=this.offset),this.pinOffset&&(n-=this.pinOffset),i>=e&&"fixed"!==this.state?(!n||n>i)&&this.fix():e>i&&"fixed"===this.state?this.unfix():n&&i>=n&&"pinned"!==this.state&&this.pin()},i.prototype.fix=function(){Gumby.debug("Element has been fixed",this.$el),Gumby.debug("Triggering onFixed event",this.$el),this.state="fixed",this.$el.css({top:0+this.top}).addClass("fixed").removeClass("unfixed pinned").trigger("gumby.onFixed"),this.$parent&&this.constrain()},i.prototype.unfix=function(){Gumby.debug("Element has been unfixed",this.$el),Gumby.debug("Triggering onUnfixed event",this.$el),this.state="unfixed",this.$el.addClass("unfixed").removeClass("fixed pinned").trigger("gumby.onUnfixed")},i.prototype.pin=function(){Gumby.debug("Element has been pinned",this.$el),Gumby.debug("Triggering onPinned event",this.$el),this.state="pinned",this.$el.css({top:this.$el.offset().top}).addClass("pinned fixed").removeClass("unfixed").trigger("gumby.onPinned")},i.prototype.constrain=function(){Gumby.debug("Constraining element",this.$el),this.$el.css({left:this.measurements.left,width:this.measurements.width})},i.prototype.measure=function(){var t,i=this.$parent.offset();this.measurements.left=i.left,this.measurements.width=this.$parent.width(),this.parentRow&&(t=Number(this.$parent.css("paddingLeft").replace(/px/,"")),t&&(this.measurements.left+=t))},i.prototype.parseAttrValue=function(i){if(t.isNumeric(i))return Number(i);if("top"===i)return this.$el.offset().top;var e=t(i);return e.length?e:(Gumby.error("Cannot find Fixed target: "+i),!1)},Gumby.addInitalisation("fixed",function(e){t("[data-fixed],[gumby-fixed],[fixed]").each(function(){var n=t(this);return n.data("isFixed")&&!e?!0:n.data("isFixed")&&e?(n.trigger("gumby.initialize"),!0):(n.data("isFixed",!0),new i(n),void 0)})}),Gumby.UIModule({module:"fixed",events:["initialize","onFixed","onUnfixed"],init:function(){Gumby.initialize("fixed")}})}(jQuery),!function(t){"use strict";function i(t){Gumby.debug("Initializing Navbar",t),this.$el=t,this.$dropDowns=this.$el.find("li:has(.dropdown)"),this.$dropDowns.on(Gumby.click,this.toggleDropdown).on("swiperight",this.openLink),"#"!==this.$dropDowns.children("a").attr("href")&&this.$dropDowns.children("a").append('<i class="icon-popup"></i>').children("i").on(Gumby.click,this.openLink),this.$dropDowns.find(".dropdown li:not(:has(.dropdown)) a[href]").on(Gumby.click,this.openLink)}Gumby.gumbyTouch&&(i.prototype.toggleDropdown=function(i){i.preventDefault();var e=t(this);e.hasClass("active")?e.removeClass("active"):e.addClass("active")},i.prototype.openLink=function(i){i.preventDefault();var e,n=t(this),s=n;n.is("i")?s=n.parent("a"):n.is("li")&&(s=n.children("a")),e=s.attr("href"),"blank"==s.attr("target")?window.open(e):window.location=e},Gumby.addInitalisation("navbar",function(){t(".navbar").each(function(){var e=t(this);return e.data("isNavbar")?!0:(e.data("isNavbar",!0),new i(e),void 0)})}),Gumby.UIModule({module:"navbar",events:[],init:function(){Gumby.initialize("navbar")}}))}(jQuery),!function(t){"use strict";function i(t){Gumby.debug("Initializing Radio Button",t),this.$el=t,this.$input=this.$el.find("input[type=radio]");var i=this;this.$el.on(Gumby.click,function(t){t.preventDefault(),i.$input.is("[disabled]")||i.update()}).on("gumby.check",function(){Gumby.debug("Check event triggered",i.$el),i.update()}),(this.$input.prop("checked")||this.$el.hasClass("checked"))&&i.update(!0)}i.prototype.update=function(){if(!(this.$el.hasClass("checked")&&this.$input.prop("checked")&&this.$el.find("i.icon-dot").length)){Gumby.debug("Updating Radio Button group",this.$el);var i=this.$el.find("span"),e='input[name="'+this.$input.attr("name")+'"]';t(".radio").has(e).removeClass("checked").find("input").prop("checked",!1).end().find("i").remove(),this.$input.prop("checked",!0),i.append('<i class="icon-dot" />'),Gumby.debug("Triggering onCheck event",this.$el),this.$el.addClass("checked").trigger("gumby.onCheck")}},Gumby.addInitalisation("radiobtn",function(){t(".radio").each(function(){var e=t(this);return e.data("isRadioBtn")?!0:(e.data("isRadioBtn",!0),new i(e),void 0)})}),Gumby.UIModule({module:"radiobtn",events:["onCheck","check"],init:function(){Gumby.initialize("radiobtn")}})}(jQuery),!function(t){"use strict";function i(i){Gumby.debug("Initializing Retina",i),this.$el=i,this.imageSrc=this.$el.attr("src"),this.retinaSrc=this.fetchRetinaImage(),this.$retinaImg=t(new Image);var e=this;return this.retinaSrc?(this.$retinaImg.attr("src",this.retinaSrc).load(function(){e.retinaImageLoaded()}).error(function(){Gumby.error("Couln't load retina image: "+e.retinaSrc)}),void 0):!1}i.prototype.fetchRetinaImage=function(){var t=this.imageSrc,i=this.imageSrc.search(/(\.|\/)(gif|jpe?g|png)$/i);return 0>i?!1:t.substr(0,i)+"@2x"+t.substr(i,t.length)},i.prototype.retinaImageLoaded=function(){Gumby.debug("Swapping image for retina version",this.$el),Gumby.debug("Triggering onRetina event",this.$el),this.$el.attr("src",this.$retinaImg.attr("src")).trigger("gumby.onRetina")},Gumby.addInitalisation("retina",function(){!window.devicePixelRatio||window.devicePixelRatio<=1||t("img[data-retina],img[gumby-retina],img[retina]").each(function(){var e=t(this);return e.data("isRetina")?!0:(e.data("isRetina",!0),new i(e),void 0)})}),Gumby.UIModule({module:"retina",events:["onRetina"],init:function(){Gumby.initialize("retina")}})}(jQuery),!function(t){"use strict";function i(t){Gumby.debug("Initializing Skiplink",t),this.$el=t,this.targetPos=0,this.duration=0,this.offset=!1,this.easing="",this.update=!1,this.setup();var i=this;this.$el.on(Gumby.click+" gumby.skip",function(t){t.preventDefault(),"skip"===t.namespace&&Gumby.debug("Skip event triggered",i.$el),i.update?i.calculateTarget(i.skipTo):i.skipTo()}).on("gumby.initialize",function(){Gumby.debug("Re-initializing Skiplink",i.$el),i.setup()})}i.prototype.setup=function(){this.duration=Number(Gumby.selectAttr.apply(this.$el,["duration"]))||200,this.offset=Gumby.selectAttr.apply(this.$el,["offset"])||!1,this.easing=Gumby.selectAttr.apply(this.$el,["easing"])||"swing",this.update=Gumby.selectAttr.apply(this.$el,["update"])?!0:!1,this.calculateTarget()},i.prototype.calculateTarget=function(i){var e,n=Gumby.selectAttr.apply(this.$el,["goto"]);if("top"==n)this.targetPos=0;else if(t.isNumeric(n))this.targetPos=Number(n);else{if(e=t(n),!e.length)return Gumby.error("Cannot find skiplink target: "+n),!1;this.targetPos=e.offset().top}i&&i.apply(this)},i.prototype.skipTo=function(){Gumby.debug("Skipping to target",this.$el);var i=this;t("html,body").animate({scrollTop:this.calculateOffset()},this.duration,this.easing).promise().done(function(){Gumby.debug("Triggering onComplete event",i.$el),i.$el.trigger("gumby.onComplete")})},i.prototype.calculateOffset=function(){if(!this.offset)return this.targetPos;var t=this.offset.substr(0,1),i=Number(this.offset.substr(1,this.offset.length));return"-"===t?this.targetPos-i:"+"===t?this.targetPos+i:void 0},Gumby.addInitalisation("skiplink",function(e){t(".skiplink > a, .skip").each(function(){var n=t(this);return n.data("isSkipLink")&&!e?!0:n.data("isSkipLink")&&e?(n.trigger("gumby.initialize"),!0):(n.data("isSkipLink",!0),new i(n),void 0)})}),Gumby.UIModule({module:"skiplink",events:["initialize","onComplete","skip"],init:function(){Gumby.initialize("skiplink")}})}(jQuery),!function(t){"use strict";function i(i){Gumby.debug("Initializing Tabs",i),this.$el=i,this.$nav=this.$el.find("ul.tab-nav > li"),this.$content=this.$el.find(".tab-content");var e=this;this.$nav.children("a").on(Gumby.click,function(i){i.preventDefault(),e.click(t(this))}),this.$el.on("gumby.set",function(t,i){Gumby.debug("Set event triggered",e.$el),e.set(t,i)})}i.prototype.click=function(t){var i=t.parent().index();this.$nav.eq(i).add(this.$content.eq(i)).hasClass("active")||(Gumby.debug("Setting active tab to "+i,this.$el),this.$nav.add(this.$content).removeClass("active"),this.$nav.eq(i).add(this.$content.eq(i)).addClass("active"),Gumby.debug("Triggering onChange event",this.$el),this.$el.trigger("gumby.onChange",i))},i.prototype.set=function(t,i){this.$nav.eq(i).find("a").trigger(Gumby.click)},Gumby.addInitalisation("tabs",function(){t(".tabs").each(function(){var e=t(this);return e.data("isTabs")?!0:(e.data("isTabs",!0),new i(e),void 0)})}),Gumby.UIModule({module:"tabs",events:["onChange","set"],init:function(){Gumby.initialize("tabs")}})}(jQuery),!function(t){"use strict";function i(i){this.$el=t(i),this.targets=[],this.on="",this.className="",this.self=!1,this.$el.length&&(Gumby.debug("Initializing Toggle",i),this.init())}function e(i){this.$el=t(i),this.targets=[],this.on="",this.className="",this.self=!1,this.$el.length&&(Gumby.debug("Initializing Switch",i),this.init())}i.prototype.init=function(){var t=this;this.setup(),this.$el.on(this.on,function(i){i.preventDefault(),t.trigger(t.triggered)}).on("gumby.trigger",function(){Gumby.debug("Trigger event triggered",t.$el),t.trigger(t.triggered)}).on("gumby.initialize",function(){Gumby.debug("Re-initializing "+t.constructor,$el),t.setup()})},i.prototype.setup=function(){this.targets=this.parseTargets(),this.on=Gumby.selectAttr.apply(this.$el,["on"])||Gumby.click,this.className=Gumby.selectAttr.apply(this.$el,["classname"])||"active",this.self="false"===Gumby.selectAttr.apply(this.$el,["self"])},i.prototype.parseTargets=function(){var i=Gumby.selectAttr.apply(this.$el,["trigger"]),e=0,n=[];return i?(e=i.indexOf("|"),-1===e?this.checkTargets([i])?[t(i)]:!1:(n=i.split("|"),this.checkTargets(n)?n.length>1?[t(n[0]),t(n[1])]:[t(n[0])]:!1)):!1},i.prototype.checkTargets=function(i){var e=0;for(e;e<i.length;e++)if(i[e]&&!t(i[e]).length)return Gumby.error("Cannot find "+this.constructor.name+" target: "+i[e]),!1;return!0},i.prototype.triggered=function(){Gumby.debug("Triggering onTrigger event",this.$el),this.$el.trigger("gumby.onTrigger",[this.$el.hasClass(this.className)])},e.prototype=new i,e.prototype.constructor=e,i.prototype.trigger=function(t){Gumby.debug("Triggering Toggle",this.$el);var i;this.targets?1==this.targets.length?this.$el.add(this.targets[0]).toggleClass(this.className):this.targets.length>1&&(this.targets[0].hasClass(this.className)?(i=this.targets[0],this.self||(i=i.add(this.$el)),i.removeClass(this.className),this.targets[1].addClass(this.className)):(i=this.targets[0],this.self||(i=i.add(this.$el)),i.addClass(this.className),this.targets[1].removeClass(this.className))):this.$el.toggleClass(this.className),t&&"function"==typeof t&&t.apply(this)},e.prototype.trigger=function(t){Gumby.debug("Triggering Switch",this.$el);var i;this.targets?1==this.targets.length?(i=this.targets[0],this.self||(i=i.add(this.$el)),i.addClass(this.className)):this.targets.length>1&&(i=this.targets[0],this.self||(i=i.add(this.$el)),i.addClass(this.className),this.targets[1].removeClass(this.className)):this.$el.addClass(this.className),t&&"function"==typeof t&&t.apply(this)},Gumby.addInitalisation("toggles",function(e){t(".toggle").each(function(){var n=t(this);return n.data("isToggle")&&!e?!0:(n.data("isToggle")&&e&&n.trigger("gumby.initialize"),n.data("isToggle",!0),new i(n),void 0)})}),Gumby.addInitalisation("switches",function(i){t(".switch").each(function(){var n=t(this);return n.data("isSwitch")&&!i?!0:n.data("isSwitch")&&i?(n.trigger("gumby.initialize"),!0):(n.data("isSwitch",!0),new e(n),void 0)})}),Gumby.UIModule({module:"toggleswitch",events:["initialize","trigger","onTrigger"],init:function(){Gumby.initialize("switches"),Gumby.initialize("toggles")}})}(jQuery),!function(t){"use strict";function i(t,i){Gumby&&Gumby.debug("Initializing Validation",t),this.$this=t,this.$field=this.$this.parents(".field"),this.req=i||function(){return!!this.$this.val().length};var e=this;this.$this.is("[type=checkbox], [type=radio]")?(this.$field=this.$this.parent("label"),this.$field.on("gumby.onChange",function(){e.validate()})):this.$this.is("select")?(this.$field=this.$this.parents(".picker"),this.$field.on("change",function(){e.validate()})):this.$this.on("blur",function(t){9!==t.which&&e.validate()})}i.prototype.validate=function(){var t=this.req(this.$this);return t?this.$field.removeClass("danger").addClass("success"):this.$field.removeClass("success").addClass("danger"),t},t.fn.validation=function(e){var n=t.extend({submit:!1,fail:!1,required:[]},e),s=[];return this.each(function(){if(!n.required.length)return!1;var e,o=t(this),a=n.required.length;for(e=0;a>e;e++)s.push(new i(o.find('[name="'+n.required[e].name+'"]'),n.required[e].validate||!1));o.on("submit",function(t){var i=!1;if(!o.data("passed")){t.preventDefault();var e,a=s.length;for(e=0;a>e;e++)s[e].validate()||(i=!0);if(i){if(n.fail&&"function"==typeof n.fail)return n.fail(),void 0}else{if(n.submit&&"function"==typeof n.submit)return n.submit(o.serializeArray()),void 0;o.data("passed",!0).submit()}}})})}}(jQuery),!function(t){"use strict";Gumby.touchDevice&&Gumby.touchEvents||!Gumby.autoInit?Gumby.touchEvents&&Gumby.touchDevice&&(Gumby.debug("Loading jQuery mobile touch events"),yepnope.errorTimeout=2e3,Modernizr.load({test:Modernizr.touch,yep:Gumby.touchEvents+"/jquery.mobile.custom.min.js",complete:function(){t.mobile||Gumby.error("Error loading jQuery mobile touch events"),Gumby.touchEventsLoaded=!0,Gumby.autoInit?window.Gumby.init():Gumby.uiModulesReady&&Gumby.helpers()}})):window.Gumby.init(),"function"==typeof define&&define.amd&&define(window.Gumby)}(jQuery);