diff --git a/assets/js/src/__init.coffee b/assets/js/src/__init.coffee new file mode 100644 index 0000000..4f622d5 --- /dev/null +++ b/assets/js/src/__init.coffee @@ -0,0 +1,23 @@ +'use strict' + +window.Itch = Itch = + version: '0.0.0' + is: (property, value) -> this.app.dataset[property] is value + app: do -> document.body + +$ -> + Itch.context = -> + # get the context from the first class name of body + # https://github.com/TryGhost/Ghost/wiki/Context-aware-Filters-and-Helpers + className = document.body.className.split(' ')[0].split('-')[0] + if className is '' then 'error' else className + + Itch.device = -> + w = window.innerWidth + h = window.innerHeight + return 'mobile' if (w <= 480) + return 'tablet' if (w <= 1024) + 'desktop' + +# jQuery.smartresize +`!function(n,r){var t;t=function(n,r,t){var i;return i=void 0,function(){var e,u,a;a=this,e=arguments,u=function(){t||n.apply(a,e),i=null},i?clearTimeout(i):t&&n.apply(a,e),i=setTimeout(u,r||100)}},jQuery.fn[r]=function(n){return n?this.bind("resize",t(n)):this.trigger(r)}}(jQuery,"smartresize");` diff --git a/assets/js/src/main.coffee b/assets/js/src/main.coffee new file mode 100644 index 0000000..f18fd11 --- /dev/null +++ b/assets/js/src/main.coffee @@ -0,0 +1,29 @@ +'use strict' + +$(window).load -> + + casperFullImg = do -> + $('img').each -> + contentWidth = $('.post-content').outerWidth() + # Width of the content + imageWidth = $(this)[0].naturalWidth + # Original image resolution + method = if imageWidth >= contentWidth then 'addClass' else 'removeClass' + $(this)[method] 'full-img' + + $(window).smartresize casperFullImg + +$ -> + el = Itch.app + el.dataset.page = Itch.context() + el.dataset.device = Itch.device() + + $('.post-content').fitVids() + + if Itch.is 'device', 'desktop' + $('a').not('[href*="mailto:"]').click -> + if this.href.indexOf(location.hostname) is -1 + window.open $(this).attr 'href' + false + else + FastClick.attach el diff --git a/assets/js/src/main.js b/assets/js/src/main.js deleted file mode 100644 index 1c89477..0000000 --- a/assets/js/src/main.js +++ /dev/null @@ -1,62 +0,0 @@ -/*globals jQuery, document */ -(function($) { - "use strict"; - - $(document).ready(function() { - $(".post-content").fitVids(); - }); - - // When all content is loaded, resize dem images - $(window).load(function() { - - function casperFullImg() { - $("img").each(function() { - var contentWidth = $(".post-content").outerWidth(); // Width of the content - var imageWidth = $(this)[0].naturalWidth; // Original image resolution - - if (imageWidth >= contentWidth) { - $(this).addClass('full-img'); - } else { - $(this).removeClass('full-img'); - } - }); - }; - - casperFullImg(); - $(window).smartresize(casperFullImg); - }); - - -}(jQuery)); - -(function($, sr) { - - // debouncing function from John Hann - // http://unscriptable.com/index.php/2009/03/20/debouncing-javascript-methods/ - var debounce = function(func, threshold, execAsap) { - var timeout; - - return function debounced() { - var obj = this, - args = arguments; - - function delayed() { - if (!execAsap) - func.apply(obj, args); - timeout = null; - }; - - if (timeout) - clearTimeout(timeout); - else if (execAsap) - func.apply(obj, args); - - timeout = setTimeout(delayed, threshold || 100); - }; - } - // smartresize - jQuery.fn[sr] = function(fn) { - return fn ? this.bind('resize', debounce(fn)) : this.trigger(sr); - }; - -})(jQuery, 'smartresize');