Monthly Archives: December 2014

AngularJS Custom Dropdown Time Filter

I’ve created this filter to allow hour selection using HTML dropdown.

The filter accepts as input three paramers: the starting hour, the ending hour, and the minute interval.

var app = angular.module('APP', ['APP.filters']);

app.controller('MainController', ['$scope', function($scope) {
 
}]);

angular.module('APP.filters', []).filter('time', function() {
    return function(input, from, to, interval) {
        from     = parseInt(from, 10);
        to       = parseInt(to, 10);
        interval = parseInt(interval, 10);

        for(var i=from, y=0; i<=to; ++i, y+=interval) {
            for(var y=0; y<60; y+=interval) {
                input.push(((i % 12) || 12)+ ":" + (y===0?'00':y) +" " + (i>12?'pm':'am'));
            }
        }
    
        return input;
    };
});

Usage:

<div ng-app="APP" ng-controller="MainController">
    <select>
        <option ng-repeat="h_m in [] | time:8:20:60">{{h_m}}</option>
    </select>
</div>

The above filter time:8:20:60 will output all the hours starting with 8 AM – 8 PM

The starting/ending hour must be between 0-24.

The interval can be between 1-60.

 

The filter can be used for showing multiple minutes in an hour.

For example time:10:14:15 will output 10:00 am, 10:15 am, 10:30 am, … , 13:45 pm, 14:00 pm.

 

AngularJS Cookie Service

In case you’re searching for a cookie service that can be used in AngularJS, here it is:

 

'use strict'

angular
.module('APP.services', [])
.factory('Cookie', function() {
	var self = this;

	self.set = function(name, value, days) {
		var expires = "";

		if(days) {
			var date = new Date();
			date.setTime(date.getTime()+(days*24*60*60*1000));
			expires = "; expires="+date.toGMTString();
		} else {
			expires = "";
		}

		document.cookie = name + "=" + value + expires + "; path=/";
	};

	self.get = function(name) {
		var nameEQ	= name + "=",
			ca		= document.cookie.split(';');

		for(var i=0; i<ca.length; ++i) {
			var c = ca[i];
			while(c.charAt(0)==' ') {
				c = c.substring(1,c.length);
			}

			if(c.indexOf(nameEQ) === 0) {
				return c.substring(nameEQ.length,c.length);
			}
		}

		return null;
	};

	self.remove = function(name) {
		self.put(name, "", -1);
	};

	return {
		set:	self.set,
		get:	self.get,
		remove:	self.remove
	};
})

;

Usage:

'use strict'

angular
.module('APP.home', ['APP.services'])
.controller('TestController', ['Cookie', function(Cookie) {

    // Set cookie
    Cookie.set('cookie_name', 'VALUE', 1);

    // Get cookie
    if(Cookie.get('cookie_name')) {
        console.log(Cookie.get('cookie_name'));
    }

    // Remove cookie
    Cookie.remove('cookie_name');

}])

;

 

Useful Linux Commands

List processes sorted by memory usage:

ps -e -orss=,args= | sort -b -k1,1n | pr -TW$COLUMNS

Check which process uses an input port:

sudo netstat -tulpn | grep :input_port

Output the md5 of each file to the defined filename:

find * -type f -exec /usr/bin/md5sum {} + > output_filename

View opened file descriptors by pid:

lsof || /usr/sbin/lsof -P -n -p input_pid

 

 

Load Google Maps Library on the fly

Define the service:

'use strict';

angular
.module('APP.services')
.factory('GMAPS', ['$window', function($window) {
    var self        = this,
        callback    = null;

    $window.gmaps_loaded = function() {
        if(callback) {
            callback();
        }
    };

    self.load = function(cb) {
        if($window.google) {
            cb();
            return;
        }

        callback = cb;

        var gmaps_script = $window.document.createElement('script');

        gmaps_script.type = 'text/javascript';
        gmaps_script.src  = 'https://maps.googleapis.com/maps/api/js?v=3.exp&callback=gmaps_loaded&client=your_client_id';
        $window.document.body.appendChild(gmaps_script);
    };

    return {
        'load': self.load
    };
}])

;

Usage:

'use strict';

angular
.module('APP.home', ['APP.services'])
.controller('TestController', ['GMAPS', function(GMAPS) {

    // Load Google library and initiate it
    GMAPS.load(function() {
        var map = new google.maps.Map(element_id, options);
    });

}])

;