1 line
5.0 KiB
JavaScript
1 line
5.0 KiB
JavaScript
!function(a,t,e,i){"use strict";function n(n,s){function r(){k("onInit"),o(),s.hideBlankYears===!1&&l(),f(),u(),d(),y(),m(),v(),p(),h(),c()}function o(){C.detach().sort(function(t,e){return"asc"===s.order?a(t).data("year")-a(e).data("year"):a(e).data("year")-a(t).data("year")}),x.append(C)}function c(){x.addClass("loaded");var a=x.find("."+s.className+"-timeblock.active").first().attr("data-year");k("afterLoad",[a])}function l(){var a=parseInt(C.first().attr("data-year")),t=parseInt(C.last().attr("data-year"));if("asc"===s.order)for(var e=a-s.boundaries;e<t+s.boundaries+1;e++)C.parent().find("[data-year="+e+"]").length<=0&&(e>a-s.boundaries?C.parent().find("[data-year="+(e-1)+"]").after('<div data-year="'+e+'" class="inactive"></div>'):C.first().before('<div data-year="'+e+'" class="inactive"></div>'));else for(var e=a+s.boundaries;e>=t-s.boundaries;e--)C.parent().find("[data-year="+e+"]").length<=0&&(e<a+s.boundaries?C.parent().find("[data-year="+(e+1)+"]").after('<div data-year="'+e+'" class="inactive"></div>'):C.first().before('<div data-year="'+e+'" class="inactive"></div>'));C=x.children()}function f(){return x.addClass(s.className).children().wrapAll(s.wrapper).wrapAll('<div class="'+s.className+'-timeline"></div>')}function d(){var a=.8*x.css("padding-top").replace("px","");x.find("."+s.className+"-timeline").css("width",""+C.length*a+"px"),x.find("."+s.className+"-timeliny-timeblock").css("width",""+a+"px")}function u(){C.each(function(t){var e=a(this).html(),i=a(this).attr("data-year"),n='<a href="#'+i+'" class="'+s.className+'-dot" data-year="'+i+'" data-text="'+e+'"></a>';a(this).addClass(""+s.className+"-timeblock").html(n)})}function m(){x.append('<div class="'+s.className+'-vertical-line"></div>')}function v(a){var t,e=x.find("."+s.className+"-vertical-line").position().left,i=x.find("."+s.className+"-timeblock.active").position().left,n=x.find("."+s.className+"-timeblock.active ."+s.className+"-dot").width()/2,r=i-e;t=r>0?"-"+(Math.abs(r)+n+1):"+"+(Math.abs(r)-n-1),x.find("."+s.className+"-timeline").animate({left:t},s.animationSpeed,function(){if("undefined"!=typeof a)if("click"===a){var t=x.find("."+s.className+"-timeblock.active").first().attr("data-year");k("afterChange",[t])}else"resize"===a&&k("afterResize")})}function y(){C.parent().find("."+s.className+"-timeblock:not(.inactive) ."+s.className+"-dot").on("click",function(t){t.preventDefault();var e=a(this).parent().parent().find("."+s.className+"-timeblock.active").attr("data-year"),i=a(this).attr("data-year");return e!=i&&(k("onLeave",[e,i]),C.removeClass("active"),a(this).closest("."+s.className+"-timeblock").addClass("active")),v("click"),!1})}function p(){function e(a,t){var e;return function(){var i=arguments,n=this;clearTimeout(e),e=setTimeout(function(){a.apply(n,i)},t)}}a(t).on("resize.timeliny",e(function(){v("resize")},350))}function h(){function i(a){o=a,l=c-o.offsetLeft}function n(a){c=e.all?t.event.clientX:a.pageX,null!==o&&(o.style.left=c-l+"px")}function r(){if(o){var t=x.find("."+s.className+"-vertical-line").offset().left,e=null,i=1e23;C.parent().find("."+s.className+"-timeblock:not(.inactive) ."+s.className+"-dot").each(function(n){var s=a(this).offset().left,r=Math.abs(s-t);r<i&&(console.log(a(this).attr("data-year")),e=a(this).attr("data-year"),i=r)}),x.find("."+s.className+"-dot[data-year="+e+"]").trigger("click"),o=null}}var o=null,c=0,l=0;x.first().on("mousedown",function(){return i(x.find("."+s.className+"-timeline")[0]),!1}),a(e).on("mousemove.timeliny",function(a){n(a)}),a(e).on("mouseup.timeliny",function(){r()})}function N(a){var t=x.find("."+s.className+"-timeblock[data-year="+a+"]:not(.inactive) ."+s.className+"-dot").first();t.length>0&&t.trigger("click")}function b(a,t){return t?void(s[a]=t):s[a]}function g(){x.each(function(){var i=a(this);a(t).off("resize.timeliny"),i.find("."+s.className+"-timeblock:not(.inactive) ."+s.className+"-dot").off("click"),a(e).off("mousemove.timeliny"),a(e).off("mouseup.timeliny"),i.first().off("mousedown"),i.remove(),k("onDestroy"),i.removeData("plugin_timeliny")})}function k(a,t){s[a]!==i&&s[a].apply(w,t)}var w=n,x=a(n),C=x.children();return s=a.extend({},a.fn.timeliny.defaults,s),r(),{option:b,destroy:g,goToYear:N}}a.fn.timeliny=function(t){if(console.log(t),"string"==typeof arguments[0]){var e,s=arguments[0],r=Array.prototype.slice.call(arguments,1);return this.each(function(){if(!a.data(this,"plugin_timeliny")||"function"!=typeof a.data(this,"plugin_timeliny")[s])throw new Error("Method "+s+" does not exist on jQuery.timeliny");e=a.data(this,"plugin_timeliny")[s].apply(this,r)}),e!==i?e:this}if("object"==typeof t||!t)return this.each(function(){a.data(this,"plugin_timeliny")||a.data(this,"plugin_timeliny",new n(this,t))})},a.fn.timeliny.defaults={order:"asc",className:"timeliny",wrapper:'<div class="timeliny-wrapper"></div>',boundaries:2,animationSpeed:250,hideBlankYears:!1,onInit:function(){},onDestroy:function(){},afterLoad:function(a){},onLeave:function(a,t){},afterChange:function(a){},afterResize:function(){}}}(jQuery,window,document); |