(function(w){ var sw = document.body.clientWidth, //Viewport Width sh = $(document).height(), //Viewport Height minViewportWidth = 240, //Minimum Size for Viewport maxViewportWidth = 2600, //Maxiumum Size for Viewport viewportResizeHandleWidth = 14, //Width of the viewport drag-to-resize handle $sgViewport = $('#sg-viewport'), //Viewport element $sizePx = $('.sg-size-px'), //Px size input element in toolbar $sizeEms = $('.sg-size-em'), //Em size input element in toolbar $bodySize = parseInt($('body').css('font-size')), //Body size of the document $headerHeight = $('.sg-header').height(), discoID = false, discoMode = false, fullMode = true, hayMode = false; $(w).resize(function(){ //Update dimensions on resize sw = document.body.clientWidth; sh = $(document).height(); setAccordionHeight(); if(fullMode === true) { sizeiframe(sw, false); }; }); /* Pattern Lab accordion dropdown */ // Accordion dropdown $('.sg-acc-handle').on("click", function(e){ e.preventDefault(); var $this = $(this), $panel = $this.next('.sg-acc-panel'), subnav = $this.parent().parent().hasClass('sg-acc-panel'); //Close other panels if link isn't a subnavigation item if (!subnav) { $('.sg-acc-handle').not($this).removeClass('active'); $('.sg-acc-panel').not($panel).removeClass('active'); } //Activate selected panel $this.toggleClass('active'); $panel.toggleClass('active'); setAccordionHeight(); }); //Accordion Height function setAccordionHeight() { var $activeAccordion = $('.sg-acc-panel.active').first(), accordionHeight = $activeAccordion.height(), availableHeight = sh-$headerHeight; //Screen height minus the height of the header $activeAccordion.height(availableHeight); //Set height of accordion to the available height } $('.sg-nav-toggle').on("click", function(e){ e.preventDefault(); $('.sg-nav-container').toggleClass('active'); }); //"View (containing clean, code, raw, etc options) Trigger $('#sg-t-toggle').on("click", function(e){ e.preventDefault(); $(this).parents('ul').toggleClass('active'); }); //Size Trigger $('#sg-size-toggle').on("click", function(e){ e.preventDefault(); $(this).parents('ul').toggleClass('active'); }); //Phase View Events $('.sg-size[data-size]').on("click", function(e){ e.preventDefault(); killDisco(); killHay(); fullMode = false; var val = $(this).attr('data-size'); if (val.indexOf('px') > -1) { $bodySize = 1; } val = val.replace(/[^\d.-]/g,'') sizeiframe(Math.floor(val*$bodySize)); }); //Size View Events // handle small button function goSmall() { killDisco(); killHay(); fullMode = false; sizeiframe(getRandom(minViewportWidth,500)); } $('#sg-size-s').on("click", function(e){ e.preventDefault(); goSmall(); }); jwerty.key('ctrl+shift+s', function(e) { goSmall(); return false; }); // handle medium button function goMedium() { killDisco(); killHay(); fullMode = false; sizeiframe(getRandom(500,800)); } $('#sg-size-m').on("click", function(e){ e.preventDefault(); goMedium(); }); jwerty.key('ctrl+shift+m', function(e) { goMedium(); return false; }); // handle large button function goLarge() { killDisco(); killHay(); fullMode = false; sizeiframe(getRandom(800,1200)); } $('#sg-size-l').on("click", function(e){ e.preventDefault(); goLarge(); }); jwerty.key('ctrl+shift+l', function(e) { goLarge(); return false; }); //Click Full Width Button $('#sg-size-full').on("click", function(e){ //Resets e.preventDefault(); killDisco(); killHay(); fullMode = true; sizeiframe(sw); }); //Click Random Size Button $('#sg-size-random').on("click", function(e){ e.preventDefault(); killDisco(); killHay(); sizeiframe(getRandom(minViewportWidth,sw)); }); //Click for Disco Mode, which resizes the viewport randomly $('#sg-size-disco').on("click", function(e){ e.preventDefault(); killHay(); fullMode = false; if (discoMode) { killDisco(); } else { startDisco(); } }); /* Disco Mode */ function disco() { sizeiframe(getRandom(minViewportWidth,sw)); } function killDisco() { discoMode = false; clearInterval(discoID); discoID = false; } function startDisco() { killHay(); discoMode = true; discoID = setInterval(disco, 800); } jwerty.key('ctrl+shift+d', function(e) { if (!discoMode) { startDisco(); } else { killDisco(); } return false; }); //Stephen Hay Mode - "Start with the small screen first, then expand until it looks like shit. Time for a breakpoint!" $('#sg-size-hay').on("click", function(e){ e.preventDefault(); killDisco(); if (hayMode) { killHay(); } else { startHay(); } }); //Stop Hay! Mode function killHay() { var currentWidth = $sgViewport.width(); hayMode = false; $sgViewport.removeClass('hay-mode'); $('#sg-gen-container').removeClass('hay-mode'); sizeiframe(Math.floor(currentWidth)); } // start Hay! mode function startHay() { killDisco(); hayMode = true; $('#sg-gen-container').removeClass("vp-animate").width(minViewportWidth+viewportResizeHandleWidth); $sgViewport.removeClass("vp-animate").width(minViewportWidth); var timeoutID = window.setTimeout(function(){ $('#sg-gen-container').addClass('hay-mode').width(maxViewportWidth+viewportResizeHandleWidth); $sgViewport.addClass('hay-mode').width(maxViewportWidth); setInterval(function(){ var vpSize = $sgViewport.width(); updateSizeReading(vpSize); },100); }, 200); } // start hay from a keyboard shortcut jwerty.key('ctrl+shift+h', function(e) { if (hayMode) { killHay(); } else { startHay(); } return false; }); //Pixel input $sizePx.on('keydown', function(e){ var val = Math.floor($(this).val()); if(e.keyCode === 38) { //If the up arrow key is hit val++; sizeiframe(val,false); } else if(e.keyCode === 40) { //If the down arrow key is hit val--; sizeiframe(val,false); } else if(e.keyCode === 13) { //If the Enter key is hit e.preventDefault(); sizeiframe(val); //Size Iframe to value of text box $(this).blur(); } }); $sizePx.on('keyup', function(){ var val = Math.floor($(this).val()); updateSizeReading(val,'px','updateEmInput'); }); //Em input $sizeEms.on('keydown', function(e){ var val = parseFloat($(this).val()); if(e.keyCode === 38) { //If the up arrow key is hit val++; sizeiframe(Math.floor(val*$bodySize),false); } else if(e.keyCode === 40) { //If the down arrow key is hit val--; sizeiframe(Math.floor(val*$bodySize),false); } else if(e.keyCode === 13) { //If the Enter key is hit e.preventDefault(); sizeiframe(Math.floor(val*$bodySize)); //Size Iframe to value of text box } }); $sizeEms.on('keyup', function(){ var val = parseFloat($(this).val()); updateSizeReading(val,'em','updatePxInput'); }); // set 0 to 320px as a default jwerty.key('ctrl+shift+0', function(e) { e.preventDefault(); sizeiframe(320,true); return false; }); // handle the MQ click var mqs = []; $('#sg-mq a').each(function(i) { mqs.push($(this).html()); // bind the click $(this).on("click", function(i,k) { return function(e) { e.preventDefault(); var val = $(k).html(); var type = (val.indexOf("px") !== -1) ? "px" : "em"; val = val.replace(type,""); var width = (type === "px") ? val*1 : val*$bodySize; sizeiframe(width,true); } }(i,this)); // bind the keyboard shortcut. can't use cmd on a mac because 3 & 4 are for screenshots jwerty.key('ctrl+shift+'+(i+1), function (k) { return function(e) { var val = $(k).html(); var type = (val.indexOf("px") !== -1) ? "px" : "em"; val = val.replace(type,""); var width = (type === "px") ? val*1 : val*$bodySize; sizeiframe(width,true); return false; } }(this)); }); //Resize the viewport //'size' is the target size of the viewport //'animate' is a boolean for switching the CSS animation on or off. 'animate' is true by default, but can be set to false for things like nudging and dragging function sizeiframe(size,animate) { var theSize; if(size>maxViewportWidth) { //If the entered size is larger than the max allowed viewport size, cap value at max vp size theSize = maxViewportWidth; } else if(size