/*! jGrid Plugin - v0.1.0 - 2012-09-03 * https://github.com/dpiatek/jgrid * Copyright (c) 2012 Dominik Piatek; Licensed MIT */ (function($) { var jGrid = { // Todo // - press ctrl-shift-l for jgrid box // - remove button submits and make them more CSS-safe // - add opacity adjustment // - make hide button a bit out of the box, like a pull down // - add position fixed to col-width // - add hide button for menu or maybe go straight to drag&drop ? // - clean-up plugin // - comment code // - make readme // - push to github // - live on / off // - make this library agnostic // Initialize plugin init: function( setup ){ this.self = this; window.updateID = null; this.keylistener(); this.addmenu(); this.updateGrid(); $('#jgrid-setup, #jgrid-overlay, #jgrid-col-width').hide(); }, // Add styles to head and append menu to body addmenu: function () { $('head').append(''); $('body').append('
pxpxHide
'); }, // Listen for Ctrl + Alt + l to toggle grid display keylistener: function ( ) { var isCtrl = false, isShift = false, that = this.self; $('body').on('keyup', function(e) { if (e.which === 16) { isShift = false; } if (e.which === 17) { isCtrl = false; } }); $('body').on('keydown', that , function(e) { if(e.which === 17) { isCtrl = true; } if(e.which === 16) { isShift = true; } if(e.which === 76 && isCtrl && isShift) { window.clearTimeout(that.updateID); // console.log(window.updateID); $('#jgrid-setup, #jgrid-overlay, #jgrid-col-width').toggle(); if ($('#jgrid-setup').css('display') !== 'none') { that.updateGrid(); } } }); }, setupGrid: function( colWidth , setup ) { var cols = '', wrapper = null; if (!($('#jgrid-overlay').length)) { $('body').append('
'); $('#jgrid-wrapper').width( setup.wrapper ); } wrapper = $('#jgrid-wrapper'); wrapper.empty(); for (var i = 0; i < setup.columns; i++) { cols += ''; } wrapper.width( setup.wrapper ); wrapper.append(cols); wrapper.find('span').width(colWidth).css('margin-right', setup.gutter); wrapper.find('span:first-child').css('margin-left', setup.marLeft); wrapper.find('span:last-child').css('margin-right', setup.marRight); }, calculateGrid: function( setup ) { var colWidth = ( setup.wrapper - setup.marLeft - setup.marRight - ( (setup.columns - 1) * setup.gutter ) ) / setup.columns; var checkWidth = (setup.columns * colWidth) + ( (setup.columns - 1) * setup.gutter ) + setup.marLeft + setup.marRight; var percentage = ( Math.round((colWidth / setup.wrapper) * 10000) / 1000 ); this.setupGrid( colWidth , setup ); this.updateColWidth( colWidth , percentage ); }, getDimensions: function () { var setup = {}; setup.wrapper = parseInt($('input[name=wrapper]').val(),10) || 0; setup.columns = parseInt($('input[name=columns]').val(),10) || 1; setup.gutter = parseInt($('input[name=gutter]').val(),10) || 0; setup.marLeft = parseInt($('input[name=marLeft]').val(),10) || 0; setup.marRight = parseInt($('input[name=marRight]').val(),10) || 0; // console.log(setup); this.calculateGrid( setup ); }, updateColWidth: function ( colWidth , percentage ) { var widthDisplay = $('#jgrid-col-width'); if ( !(widthDisplay.length) ) { $('body').append(''); widthDisplay = $('#jgrid-col-width'); } if (!(typeof colWidth === 'number' && (colWidth % 1 === 0) )) { colWidth = Math.round( colWidth*100 ) / 100; widthDisplay.css('color','#F55'); } else { colWidth = colWidth + 'px'; widthDisplay.css('color','#DDD'); } // console.log(isNaN(percentage)); percentage = isNaN(percentage) ? '0%' : percentage + '%'; widthDisplay.text( 'Column width: ' + colWidth ); widthDisplay.append( '
' + percentage ); }, updateGrid: function () { var that = this.self; this.getDimensions(); this.updateID = window.setTimeout(function ( ) { that.updateGrid(); }, 1000, that); } }; jGrid.init(); // return $.initGrid = jGrid.calculateGrid({ wrapper:960, columns:12, gutter:10, marLeft:5, marRight:5 }); }(jQuery));