July 31, 2015

Angular.js Number Range

Recently, while working on one of the projects at Onevest. I needed a dropdown list of years between 2015 and 1980. Instead of tryping each year out, I decided to create a tiny angular filter that iterates between two numbers.

July 15, 2014

jQuery Touch Event Binding

While I was working on the jQuery prettySocial. One of the biggest hurdles I encountered was dynamically binding touch events to the DOM element. The problem was not actually the binding, but the fact that event was being called when user scrolling the page with their finger. There is an in depth article about this by Brian G.

March 25, 2014

Proper Truncating

Here is a useful PHP function that truncates a block of text without breaking a word. This is useful when creating an excerpt text and making sure that words are not cut off.

September 20, 2013

jQuery downCount - TimeZone Sensitive Countdown Plugin

jQuery countdown plugin that accounts for timezone. Plugin requires you to set your UTC offset. You can find your UTC offset here. Currently the offset is set to NYC -4 UTC.

#Features

  • Timezone sensative
  • Callback function

#Usage

Initlize just like any other plugin.

$('.countdown').downCount({
    date: '08/27/2013 12:00:00',
    offset: -5
}, function() {
    alert('WOOT WOOT, done!');
});

Read more on GitHub…

August 9, 2013

Wordpress WP_Query custom post order

Recently, I had to build a Wordpress Admin widget area which enables a user to select, change order and save a list of posts. Selected post IDs are stored in an array() which can be found in the options table. It looks something like this:

<?php
    $post_list = array( 123, 456, 321 );
?>

So when it comes to rending the posts in the array, I just create a new WP_Query(), like so:

<?php
    $post_list = array( 123, 456, 321 );

    $args = array( 'post__in' => $post_list );

    $my_query = new WP_Query( $args );
?>

Wordpress automatically queries posts in a reverse chronological order (newest post first), or if set - by orderby value. However, what if I need to keep the queried posts in specific order listed in my $post_list array? This sort of condtion cannot be passed into WP_Query() as an argument, but it can be done after query is completed. After some messing around by creating loops and filtering the object, the best solution was to use the usort() PHP function.

<?php
    $post_list = array( 123, 456, 321 );

    $args = array( 'post__in' => $post_list );

    $my_query = new WP_Query( $args );

    usort( $my_query->posts, function( $a, $b ) use( $post_list )
    {
        $apos   = array_search( $a->ID, $post_list );
        $bpos   = array_search( $b->ID, $post_list );

        return ( $apos < $bpos ) ? -1 : 1;
    });
?>

And that’s it.

June 11, 2013

JavaScript Check If Element Has Class

Here is a little snippet If you’re trying to check wether element contains a class, without using jQuery.

function hasClass(element, className) {
    return element.className && new RegExp("(^|\\s)" + className + "(\\s|$)").test(element.className);
}

This accounts for the fact that element might contain multiple class names separated by space.

You can also assign this function to element prototype.

Element.prototype.hasClass = function(className) {
    return this.className && new RegExp("(^|\\s)" + className + "(\\s|$)").test(this.className);
};

And trigger it like this (very similar to jQuery’s .hasClass() function):

document.getElementById('MyDiv').hasClass('active');