Column Prototype

In this example below you will see how to do a Column Prototype with some HTML / CSS and Javascript

Thumbnail
This awesome code was written by d2kagw, you can see more from this user in the personal repository.
You can find the original code on Codepen.io
Copyright d2kagw ©
  • HTML
  • CSS
  • JavaScript
      <header>
    Lexer Column Test
    <a data-action='scroll-home'>Home</a>
  </header>

  <main>

    <!-- Pivot Column -->
    <div class='app-col app-col-pivot'>
      <div class='app-col-container'>
        <button class='btn btn-default'>
          <img src='http://lorempixel.com/30/30?id=1' /> Inbox
        </button>
        <button class='btn btn-default'>
          <img src='http://lorempixel.com/30/30?id=2' /> Outbox
        </button>
      </div>
    </div>

    <!-- Dive Column -->
    <div class='app-col app-col-dive'>
      <div class='app-col-container'>
        <div class='app-col-when-collapsed'>
          <div class='panel-heading'>
            <button class='btn btn-default' data-action='collapse'>x</button>
            Deep Dive
          </div>
        </div>
        <div class='app-col-when-open'>
          <div class='panel panel-default'>
            <div class='panel-heading'>
              Deep Dive
              <button class='btn btn-default' data-action='collapse'>x</button>
            </div>
            <div class='panel-body'>
              <p>
                Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
                tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
                quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
                consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
                cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non
                proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
              </p>
            </div>

            <table class='table'> <thead> <tr> <th>#</th> <th>First Name</th> <th>Last Name</th> <th>Username</th> </tr> </thead> <tbody> <tr> <th scope='row'>1</th> <td>Mark</td> <td>Otto</td> <td>@mdo</td> </tr> <tr> <th scope='row'>2</th> <td>Jacob</td> <td>Thornton</td> <td>@fat</td> </tr> <tr> <th scope='row'>3</th> <td>Larry</td> <td>the Bird</td> <td>@twitter</td> </tr> </tbody> </table>
          </div>
        </div>
      </div>
    </div>

    <!-- Inbox Column -->
    <div class='app-col app-col-narrow'>
      <div class='app-col-container'>
        <h2>Inbox</h2>
        <button class='btn btn-default' data-action='collapse'>x</button>
      </div>
    </div>

    <!-- Object Column -->
    <div class='app-col app-col-main'>
      <div class='app-col-container'>
        <h2>Object</h2>
        <div class='panel panel-default'>
          <div class='panel-body'>
            Basic panel example
          </div>
        </div>
      </div>
    </div>

    <!-- Action Column -->
    <div class='app-col app-col-actions'>
      <div class='app-col-container'>
        <button class='btn btn-default' data-action='show-context' data-value='app-col-action-context'>
          <img src='http://lorempixel.com/30/30?id=3' /> +
        </button>
        <button class='btn btn-default' data-action='show-context' data-value='app-col-action-context'>
          <img src='http://lorempixel.com/30/30?id=4' /> +
        </button>
      </div>
    </div>

  </main>

/*Downloaded from https://www.codeseek.co/d2kagw/column-prototype-adqMbJ */
    html,
body {
  height: 100%;
  width: 100%;
  padding: 0;
  margin: 0;

  min-height: 600px;

  background: #CDD4D3;
}

body {
  display: flex;
  flex-direction: column;
}

header {
  background: #00beb7;
  height: 50px;

  margin-bottom: 10px;
}

main {
  display: flex;
  flex-direction: row;
  flex: auto;
  overflow-x: scroll;
}

/* ------------- */

.app-col {
  overflow: hidden;
  flex-grow: 0;
  flex-shrink: 0;
  flex-basis: 0px;

  transition: flex 0.2s, flex-grow 0.2s, flex-shrink 0.2s, flex-basis 0.2s, min-width 0.2s;

  margin-right: 10px;
  margin-bottom: 10px;
  border-radius: 5px;

  background: #333;
  color: #f00;
}
  .app-col:first-child {
    border-top-left-radius: 0px;
    border-bottom-left-radius: 0px;
  }

  .app-col:last-child {
    border-top-right-radius: 0px;
    border-bottom-right-radius: 0px;
    margin-right: 0px;
  }

  .app-col .app-col-container {
    overflow: hidden;
  }

.app-col-pivot, .app-col-actions {
  flex-grow: 0;
  flex-shrink: 0;
  flex-basis: 75px;
}
  .app-col-actions {
    border-top-right-radius: 0px;
    border-bottom-right-radius: 0px;
    margin-right: 0px;
  }

  .app-col-actions + .app-col {
    border-top-left-radius: 0px;
    border-bottom-left-radius: 0px;
  }

  .app-col-pivot .app-col-container .btn,
  .app-col-actions .app-col-container .btn {
    height: 75px;
    width: 75px;

    text-align: center;

    background: none;

    border: none;
    border-bottom: 1px solid #ccc;
    border-radius: 0px;

    color: #fff;
  }
    .app-col-pivot .app-col-container .btn img,
    .app-col-actions .app-col-container .btn img {
      display: block;
    }

.app-col-dive {
  /* collapsible */
  flex-grow: 0;
  flex-shrink: 0;
  flex-basis: 300px;
}

.app-col-dive .app-col-container {
  min-width: 290px;
}

.app-col-narrow {
  /* collapsible */
  flex-grow: 0;
  flex-shrink: 0;
  flex-basis: 200px;
}

.app-col-narrow .app-col-container {
  min-width: 190px;
}

.app-col-main, .app-col-action {

  flex-grow: 1;
  flex-shrink: 1;
  flex-basis: 400px;
  min-width: 400px;
}

.app-col-main .app-col-container,
.app-col-action .app-col-container {
  min-width: 390px;
}

@keyframes col-action {
  0% {
    flex-grow: 0;
    flex-shrink: 0;
    flex-basis: 0px;
    min-width: 0px;
  }
  100% {
    flex-grow: 1;
    flex-shrink: 1;
    flex-basis: 400px;
    min-width: 400px;
  }
}

.app-col-action-animate {
  animation: col-action 0.2s ease;
}


/* ------------- */


.app-col-collapsed {
  flex-grow: 0;
  flex-shrink: 0;
  flex-basis: 40px;
}
  .app-col .app-col-when-collapsed {
    display: none;
  }
  .app-col .app-col-when-open {
    display: block;
  }
  .app-col.app-col-collapsed .app-col-when-collapsed {
    display: block;
  }
  .app-col.app-col-collapsed .app-col-when-open {
    display: none;
  }

.app-col-close {
  flex-grow: 0;
  flex-shrink: 0;
  flex-basis: 0px;
  min-width: 0px;
}


/*Downloaded from https://www.codeseek.co/d2kagw/column-prototype-adqMbJ */
    /*
  TODO
  x allow for the interface to expand
  x state management - if it was closed automatically then re-open when it can
  x what happens when you open a pane and it doesn't fit?
  x bit of magic going on with sizing
  x mobile/tablet support
  x scrolling seems to be off in safari (seems min-width doesn't apply properly)
  - jquery.scrollLeft doesn't work in safari on mobile
  - open, open, open, close, close, open results in a pane showing without smooth animation on safari and IE11
  - user collapsing a pane doesn't work (no error either) in IE11
  - 400px is too wide on an iphone, doesn't fit in a single window (980px/2)
*/

var action_bar_open = false;
var shrinkable_columns = ['.app-col-dive', '.app-col-narrow']

function shrinker(required_room) {
  window_width = $(window).width();

  new_width = required_room || 0;
  $('.app-col').each(function() {
    new_width += parseInt($(this).attr('col-basis')||0);
  });

  new_width = new_width - window_width;

  // if greater than - we need to make space
  if (new_width > 0) {
    shrinker_createSpace(new_width);
  } else {
    shrinker_useSpace(new_width);
  }

  setTimeout(function() {
    width = parseInt($('.app-col:not(.app-col-collapsed)').last().attr('col-basis')||0) * 0.5;
    $('.app-col:not(.app-col-collapsed)').each(function() {
      width += parseInt($(this).attr('col-basis')||0);
    });
    window.columns_at_capacity = width > $(window).width();
  }, 500);
};

function shrinker_createSpace(required_width) {
  required_width = Math.abs(required_width);

  selector = shrinkable_columns.join(':not(.app-col-collapsed), ') + ':not(.app-col-collapsed)';
  columns = $(selector);

  for (var i = 0; i < columns.length; i++) {
    required_width -= parseInt($(columns[i]).attr('col-basis')||0);
    $(columns[i]).addClass('app-col-collapsed');
    $(columns[i]).attr('col-state', 'collapsed-automatic');
    $(columns[i]).attr('col-origin', 'shrinker');

    if (required_width <= 0) {
      break;
    };
  };
}

function shrinker_useSpace(available_width) {
  available_width = Math.abs(available_width);

  selector = shrinkable_columns.join('.app-col-collapsed[col-origin=shrinker], ') + '.app-col-collapsed[col-origin=shrinker]';
  columns = $(selector);

  for (var i = columns.length - 1; i >= 0 ; i--) {
    available_width -= parseInt($(columns[i]).attr('col-basis')||0);
    $(columns[i]).removeClass('app-col-collapsed');
    $(columns[i]).attr('col-state', 'open');
    $(columns[i]).attr('col-origin', 'shrinker');

    if (available_width <= 0) {
      break;
    };
  };
}

function scrollToColumn(element) {
  left = $(element).offset().left
  left = left < ($(window).width() * 0.50) ? 0 : left

  $('main').animate({
    scrollLeft: left
  }, 350);
}

$(document).ready(function() {

  // set default column states
  $('.app-col').each(function(){
    $(this).attr('col-state', 'open');
    $(this).attr('col-origin', 'shrinker');
    $(this).attr('col-basis', parseInt($(this).css('flex-basis')));
  });

  // allow users to collapse and expand columns
  $(document).on('click', '[data-action=collapse]', function() {
    parent = $(this).parents('.app-col');
    parent.toggleClass('app-col-collapsed');

    state = parent.hasClass('app-col-collapsed') ? 'collapsed' : 'open'
    parent.attr('col-state', state);

    // weird logic - if the user collapses it, then it should remain collapsed
    // once they open it, the control is over to the shrinker
    // meaning a user closing a pane is persisted, but their request to open isn't
    origin = state == 'collapsed' ? 'user' : 'shrinker'
    parent.attr('col-origin', origin);

    return false;
  });

  // allow users to remove action panes
  $(document).on('click', '[data-action=close]', function() {
    closeActionPane($(this).parents('.app-col'));

    return false;
  });

  // allow users to add action panes
  $(document).on('click', '[data-action=show-context]', function() {
    if (window.action_bar_open) {
      $('.app-col-action').each(function(){
        closeActionPane($(this));
      });
      setTimeout(function(){
        el = generateActionPane();
        scrollToColumn(el);
      }, 500);
    } else {
      el = generateActionPane();
      scrollToColumn(el);
    }

    return false;
  });

  // allow users to add action panes to action panes
  $(document).on('click', '[data-action=show-extra-context]', function() {
    el = generateActionPane();
    scrollToColumn(el);

    return false;
  });

  // scroll to the pane clicked
  $(document).on('click', '.app-col, .app-col .panel-heading, [data-action=scroll-home]', function(event) {
    scrollToColumn($(this));
    return false;
  });

  // run shrinker on resize
  var resizeTimer;
  $(window).on('resize', function(e) {
    clearTimeout(resizeTimer);
    resizeTimer = setTimeout(function() {
      shrinker();
    }, 250);
  });
  shrinker();
})


/*
------------- PANE GENERATORS
A little bit of logic here, more HTML generation
*/
function closeActionPane(element) {
  $(element).addClass('app-col-close');
  shrinker($(element).attr('col-basis') * -1);

  setTimeout(function() {
    $(element).remove();
    window.action_bar_open = false;
  }, 250);
}

function generateActionPane() {
  klass  = 'app-col app-col-action'
  klass += window.columns_at_capacity ? '' : ' app-col-action-animate'

  html = '<div class="' + klass + '" style="background:' + randomHex() + ';">'
  html += '  <div class="app-col-container">'
  html += '    <h2>Context View</h2>';
  html += '    <button class="btn btn-default" data-action="close">x</button>';
  html += '    <button class="btn btn-default" data-action="show-extra-context">+</button>';
  html += '  </div>';
  html += '</div>';

  el = $(html);

  // XXX: have to hard code this as the css attrs aren't set until the end of the animation
  el.attr('col-basis', 400);
  el.attr('col-state', 'open');
  el.attr('col-origin', 'shrinker');

  $('main').append(el);
  window.action_bar_open = true;

  shrinker();

  return el;
}


/*
------------- JUNK
Little helpers for helping the help
*/
function randomHex() {
  return '#' + Math.floor(Math.random()*16777215).toString(16);
}

Comments