Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <div>
    By: <a href="http://ericeastwood.com/">Eric Eastwood</a>
</div>

<div>
    Row:<input type="range" id="row-count" min="1" max="50" step="1" value="3" /><label for="row-count"></label>
    <br />
    Column:<input type="range" id="column-count" min="1" max="50" step="1" value="3" /><label for="column-count"></label>
</div>
    
<div class="inventory-table" id="personal-inventory">
    <div class="inventory-row">
        <div class="inventory-cell">
            <div class="inventory-item grape"></div>
        </div>
        <div class="inventory-cell">
            <div class="inventory-item sword1" data-item-type="weapon sword"></div>
        </div>
        <div class="inventory-cell">
            <div class="inventory-item carrot"></div>
        </div>
        <div class="inventory-cell">
        </div>
        <div class="inventory-cell">
        </div>
    </div>
    <div class="inventory-row">
        <div class="inventory-cell">
            <div class="inventory-item carrot" data-item-type="food"></div>
        </div>
        <div class="inventory-cell">
            <div class="inventory-item meat1" data-item-type="food cookable"></div>
        </div>
        <div class="inventory-cell">
            <div class="inventory-item meat1" data-item-type="food cookable"></div>
        </div>
        <div class="inventory-cell"></div>
        <div class="inventory-cell"></div>
    </div>
    <div class="inventory-row">
        <div class="inventory-cell">
            <div class="inventory-item axe1" data-item-type="weapon axe"></div>    
        </div>
        <div class="inventory-cell">
        </div>
        <div class="inventory-cell">
            <div class="inventory-item grape" data-item-type="food"></div>
        </div>
        <div class="inventory-cell"></div>
        <div class="inventory-cell"></div>
    </div>
</div>

<br />

Chest:
<div class="inventory-table">
    <div class="inventory-row">
        <div class="inventory-cell">
            <div class="inventory-item grape" data-item-type="food"></div>
        </div>
        <div class="inventory-cell">
            <div class="inventory-item sword1" data-item-type="weapon sword"></div>
        </div>
        <div class="inventory-cell">
            <div class="inventory-item carrot" data-item-type="food"></div>
        </div>
        <div class="inventory-cell">
            <div class="inventory-item fire-sword1" data-item-type="weapon sword enchanted"></div>
        </div>
        <div class="inventory-cell">
            <div class="inventory-item fire-bow1" data-item-type="weapon bow enchanted"></div>
        </div>
    </div>
    <div class="inventory-row">
        <div class="inventory-cell">
            <div class="inventory-item carrot" data-item-type="food"></div>
        </div>
        <div class="inventory-cell">
            <div class="inventory-item fire-axe1" data-item-type="weapon axe enchanted"></div>    
        </div>
        <div class="inventory-cell"></div>
        <div class="inventory-cell"></div>
        <div class="inventory-cell"></div>
    </div>
</div>



<br />

Furnace:
<div class="inventory-table" data-item-filter-whitelist="cookable">
    <div class="inventory-row">
        <div class="inventory-cell">
        </div>
        <div class="inventory-cell">
        </div>
    </div>
    <div class="inventory-row">
        <div class="inventory-cell">
        </div>
        <div class="inventory-cell">
        </div>
    </div>
</div>

<br />

Only enchanted weapons that are not an axe:
<div class="inventory-table" data-item-filter-whitelist="weapon+enchanted" data-item-filter-blacklist="axe">
    <div class="inventory-row">
        <div class="inventory-cell">
            <div class="inventory-item fire-bow1" data-item-type="weapon bow enchanted"></div>
        </div>
        <div class="inventory-cell">
        </div>
    </div>
    <div class="inventory-row">
        <div class="inventory-cell">
        </div>
        <div class="inventory-cell">
        </div>
    </div>
</div>

              
            
!

CSS

              
                html {
  box-sizing: border-box;
}
*, *:before, *:after {
    box-sizing: inherit;
}

body
{
    background: #332f2c;
  
    color: #dddddd;
    font-family: Arial;
}

/* Item atlas: 
 *  - https://i.imgur.com/ngGK5MF.png
 *  - https://s3-us-west-2.amazonaws.com/s.cdpn.io/8709/item-atlas.png
 * Table/Inventory atlas:
 *  - https://i.imgur.com/nOqKSbE.png
 *  - https://s3-us-west-2.amazonaws.com/s.cdpn.io/8709/inventory-atlas.png
*/

a, a:link, a:visited { 
  color: #c00;
  text-decoration: none;
  
  -webkit-transition: all 0.3s ease;  
  transition: all 0.3s ease;
}
a:hover { 
  color: #ff5c19;
  text-decoration: underline;
}  
a:active { 
  color: #6aff19;
}

input[type="range"]
{
    width: 400px;
}

.inventory-table
{

}

.inventory-row
{
   display: flex;
	flex-direction: row;
	justify-content: flex-start; /* align items in Main Axis */
	align-items: flex-start; /* align items in Cross Axis */
	align-content: flex-start; /* Extra space in Cross Axis */
}


.inventory-cell
{
    width: 36px;
    height: 36px;
    
    flex-shrink: 0;
    
    display: flex;
    flex-direction: row;
    justify-content: center; /* align items in Main Axis */
    align-items: center; /* align items in Cross Axis */
    align-content: center; /* Extra space in Cross Axis */
    
    background-image: url('https://s3-us-west-2.amazonaws.com/s.cdpn.io/8709/inventory-atlas.png');
    background-position: -229px -331px;
}

/* left */
.inventory-cell:first-child
{
    width: 38px;
    padding-left: 2px;
    
    background-position: -190px -331px;
}

/* right */
.inventory-cell:last-child
{
    width: 38px;
    padding-right: 2px;
    
    background-position: -266px -331px;
}
    

/* top ROW */
.inventory-row:first-child .inventory-cell
{
    height: 38px;
    padding-top: 2px;
    
    background-position: -229px -292px;
}

/* bottom ROW */
.inventory-row:last-child .inventory-cell
{
    height: 38px;
    padding-bottom: 2px;
    
    background-position: -229px -368px;
}

/* top left */
.inventory-row:first-child .inventory-cell:first-child
{
    width: 38px;
    height: 38px;
    
    background-position: -190px -292px;
}

/* top right */
.inventory-row:first-child .inventory-cell:last-child
{
    width: 38px;
    height: 38px;
    
    background-position: -266px -292px;
}

/* bottom left */
.inventory-row:last-child .inventory-cell:first-child
{
    width: 38px;
    height: 38px;
    
    background-position: -190px -368px;
}

/* bottom right */
.inventory-row:last-child .inventory-cell:last-child
{
    width: 38px;
    height: 38px;
    
    background-position: -266px -368px;
}


/* single horizontal */
.inventory-row:only-child .inventory-cell
{
    height: 40px;
    
    background-position: -613px -286px;
}
/* single horizontal left */
.inventory-row:only-child .inventory-cell:first-child
{
    width: 38px;
    height: 40px;
    
    background-position: -574px -286px;
}
/* single horizontal right */
.inventory-row:only-child .inventory-cell:last-child
{
    width: 38px;
    height: 40px;
    
    background-position: -650px -286px;
}

/* single vertical */
.inventory-row .inventory-cell:only-child
{
    width: 40px;
    
    background-position: -563px -463px;
}
/* single vertical top */
.inventory-row:first-child .inventory-cell:only-child
{
    width: 40px;
    height: 38px;
    
    background-position: -563px -424px;
}
/* single vertical bottom */
.inventory-row:last-child .inventory-cell:only-child
{
    width: 40px;
    height: 38px;
    
    background-position: -563px -500px;
}

/* single by singel */
.inventory-row:only-child .inventory-cell:only-child
{
    width: 40px;
    height: 40px;
    
    background-position: -30px -288px;
}



/* ITEMS */

.inventory-item
{
    width: 34px;
    height: 34px;
    background-image: url('https://s3-us-west-2.amazonaws.com/s.cdpn.io/8709/item-atlas.png');
    background-repeat: no-repeat;
    
    cursor: hand;
    cursor: pointer;
    
    -webkit-transition: opacity 0.3s ease;
    transition: opacity 0.3s ease;
}

.inventory-item-sortable-placeholder
{
    display: none;
}

.inventory-item.ui-sortable-helper, .inventory-item.ui-draggable-dragging
{
    opacity: .6;
}

.inventory-item.grape
{
    background-position: 0px 0px;
}
.inventory-item.carrot
{
    background-position: -34px -34px;
}
.inventory-item.sword1
{
    background-position: 0px -170px;
}
.inventory-item.fire-sword1
{
    background-position: -34px -952px;
}
.inventory-item.axe1
{
    background-position: -170px -340px;
}
.inventory-item.fire-axe1
{
    background-position: -306px -340px;
}
.inventory-item.fire-bow1
{
    background-position: -238px -374px;
}
.inventory-item.meat1
{
    background-position: -374px -34px;
}
.inventory-item.meat1-cooked
{
    background-position: -442px -34px;
}

              
            
!

JS

              
                "use strict";

jQuery.fn.extend({
    addRemoveItems: function(targetCount) {
        return this.each(function() {
            var $children = $(this).children();
            var rowCountDifference = targetCount - $children.length;
            //console.log('row count diff: ' + rowCountDifference);
           
            if(rowCountDifference > 0)
            {
                // Add items
                for(var i = 0; i < rowCountDifference; i++)
                {
                    //console.log($rows.first());
                    $children.last().clone().appendTo(this);
                }
            }
            else if(rowCountDifference < 0)
            {
                // remove items
                $children.slice(rowCountDifference).remove();
            }
        });
    },
    // Modified and Updated by MLM
    // Origin: Davy8 (http://stackoverflow.com/a/5212193/796832)
    parentToAnimate: function(newParent, duration) {
        duration = duration || 'slow';
        
        var $element = $(this);
        //console.log($element);
        if($element.length > 0)
        {
            
            newParent = $(newParent); // Allow passing in either a JQuery object or selector
            var oldOffset = $element.offset();
            $(this).appendTo(newParent);
            var newOffset = $element.offset();
            
            
            var temp = $element.clone().appendTo('body');
            
            temp.css({
                'position': 'absolute',
                'left': oldOffset.left,
                'top': oldOffset.top,
                'zIndex': 1000
            });
            
            $element.hide();
                
            temp.animate({
                'top': newOffset.top,
                'left': newOffset.left
            }, duration, function() {
                $element.show();
                temp.remove();
            });
            
            //console.log("parentTo Animate done");
        }
    }
});

$('#row-count').on('input propertychange change', function() {
    var targetRowCount = $(this).val();
    //console.log('target count: ' + targetRowCount);
    $('label[for="'+$(this).attr('id')+'"]').html(targetRowCount);
      
    $('#personal-inventory.inventory-table').addRemoveItems(targetRowCount);
    
    refreshSortableInventoryList();
}).trigger('change');

$('#column-count').on('input propertychange change', function() {
    var targetColumnCount = $(this).val();
    //console.log('target count: ' + targetColumnCount);
    $('label[for="'+$(this).attr('id')+'"]').html(targetColumnCount);
        
    $('#personal-inventory.inventory-table .inventory-row').addRemoveItems(targetColumnCount);
    
    refreshSortableInventoryList();
}).trigger('change');




// Sorting, dragging, dropping, etc

refreshSortableInventoryList();
function refreshSortableInventoryList()
{
    $('.inventory-cell').sortable({
        connectWith: '.inventory-cell',
        placeholder: 'inventory-item-sortable-placeholder',
        receive: function( event, ui ) {
            var attrWhitelist = $(this).closest('.inventory-table').attr('data-item-filter-whitelist');
            var attrBlackList = $(this).closest('.inventory-table').attr('data-item-filter-blacklist');
            var itemFilterWhitelistArray = attrWhitelist ? attrWhitelist.split(/\s+/) : [];
            var itemFilterBlacklistArray = attrBlackList ? attrBlackList.split(/\s+/) : [];
            //console.log(itemFilterWhitelistArray);
            //console.log(itemFilterBlacklistArray);  
            
            var attrTypeList = $(ui.item).attr('data-item-type');
            var itemTypeListArray = attrTypeList ? attrTypeList.split(/\s+/) : [];
            //console.log(itemTypeListArray);
            
            var canMoveIntoSlot = verifyWithWhiteBlackLists(itemTypeListArray, itemFilterWhitelistArray, itemFilterBlacklistArray)
            
            if(!canMoveIntoSlot)
            {
                console.log("Can't move to this slot");
                //$(ui.sender).sortable('cancel');
                $(ui.item).parentToAnimate($(ui.sender), 200);
            }
            else                
            {
            
                // Swap places of items if dragging on top of another
                // Add the items in this list to the list the new item was from
                $(this).children().not(ui.item).parentToAnimate($(ui.sender), 200);
                
                // $(this) is the list the item is being moved into
                // $(ui.sender) is the list the item came from
                // Don't forget the move swap items as well
                
                // $(this).attr('data-slot-position-x');
                // $(this).attr('data-slot-position-y');
                // $(ui.sender).attr('data-slot-position-x');
                // $(ui.sender).attr('data-slot-position-y');
                //console.log("Moving to: (" + $(this).attr('data-slot-position-x') + ", " + $(this).attr('data-slot-position-y') + ") - From: (" + $(ui.sender).attr('data-slot-position-x') + ", " + $(ui.sender).attr('data-slot-position-y') + ")");
            }
        }
    }).each(function() {
        // Setup some nice attributes for everything
        // Makes it easier to update the backend
        $(this).attr('data-slot-position-x', $(this).prevAll('.inventory-cell').length);
        $(this).attr('data-slot-position-y', $(this).closest('.inventory-row').prevAll('.inventory-row').length);
    }).disableSelection();
}

function verifyWithWhiteBlackLists(itemList, whiteList, blackList)
{
    // itemList should contain tags
    // whiteList and blackList can contain tags and tag queries
    
    // If we have a matching tags to some tag query in the whiteList but not in the blackList, then return true
    // Else return false
    
    
    console.group("Lists");
    console.log(itemList);
    console.log(whiteList);
    console.log(blackList);
    console.groupEnd();

    // If white and black lists are empty, return true
    // Save the calculations, no filtering
    if(whiteList.length == 0 && blackList.length == 0)
        return true;
    

    
    // Check if the itemList has an item in the blackList
    var inBlackList = false;
    $.each(blackList, function(index, value) {
        var itemBlack = value;
        var itemBlackAndArray = itemBlack.split(/\+/);
        console.log(itemBlackAndArray);
        
        var andedResult = true;
        for(var i = 0; i < itemBlackAndArray.length; i++)
        {
            if(blackList.length > 0 && $.inArray(itemBlackAndArray[i], itemList) !== -1)
            {
                andedResult = andedResult && true;
            }
            else
            {
                andedResult = andedResult && false;
            }
        }
        
        if(andedResult)
            inBlackList = true;
    });
    
    inBlackList = blackList.length > 0 ? inBlackList : false;
    
    
    // Check if the itemList has an item in the whiteList
    var inWhiteList = false;
    $.each(whiteList, function(index, value) {
        var itemWhite = value;
        var itemWhiteAndArray = itemWhite.split(/\+/);
        //console.log(itemWhiteAndArray);
        
        var andedResult = true;
        for(var i = 0; i < itemWhiteAndArray.length; i++)
        {
            if(whiteList.length > 0 && $.inArray(itemWhiteAndArray[i], itemList) !== -1)
            {
                andedResult = andedResult && true;
            }
            else
            {
                andedResult = andedResult && false;
            }
        }
        //console.log("andedResult: " + andedResult);
        
        if(andedResult)
            inWhiteList = true;
       
    });
    
    inWhiteList = whiteList.length > 0 ? inWhiteList : false;
    
    
    console.log("inWhite: " + inWhiteList + " - inBlack: " + inBlackList);
    
    if((whiteList.length == 0 || inWhiteList) && !inBlackList)
        return true;
    
    return false;
}

              
            
!
999px

Console