Login   Register  
Icontem

File: MonthByValueKeyDown.js

Recommend this page to a friend!
Stumble It! Stumble It! Bookmark in del.icio.us Bookmark in del.icio.us
  Classes of martin barker  >  Month By Value Key Down  >  MonthByValueKeyDown.js  >  Download  
File: MonthByValueKeyDown.js
Role: Class source
Content type: text/plain
Description: The Main JSON Object
Class: Month By Value Key Down
Select the form input option using the number keys
Author: By
Last change:
Date: 2012-07-18 06:55
Size: 3,143 bytes
 

Contents

Class file image Download
MonthByValueKeyDown = {
	KeyCodeMap:{48:"0", 49:"1", 50:"2", 51:"3", 52:"4", 53:"5", 54:"6", 55:"7", 56:"8", 57:"9", 96:"0", 97:"1", 98:"2", 99:"3", 100:"4", 101:"5", 102:"6", 103:"7", 104:"8", 105:"9"},
	TimeBetweenKeys:350,
	keysPressed:[],
	shift:false,
	ctrl:false,
	timeout:null,
	element:null,
	selectItemByValue:function (value){
		for(var i=0; i < MonthByValueKeyDown.element.options.length; i++){
			if(MonthByValueKeyDown.element.options[i].value == value)
			MonthByValueKeyDown.element.selectedIndex = i;
		}
	},	
	isElement:function(obj) {
		try {
			return obj instanceof HTMLElement;
		}
		catch(e){
			return (typeof obj==="object") && (obj.nodeType===1) && (typeof obj.style === "object") && (typeof obj.ownerDocument ==="object");
		}
	},
	timeLimitExpire:function(){
		var str = "";
		for(var index in MonthByValueKeyDown.keysPressed){
			if(index == 0){
				if( MonthByValueKeyDown.keysPressed[index] == "1"){
					str += MonthByValueKeyDown.keysPressed[index]
				}
			}else{
				str += MonthByValueKeyDown.keysPressed[index];
			}
		}
		// select the correct option in dropdown
		MonthByValueKeyDown.keysPressed = [],
		MonthByValueKeyDown.selectItemByValue(str);
	},
	init:function(element){
		if(typeof(element) == "undefined"){ throw "Can't Initilize without an element"; return;}
		if(typeof(element) == "string"){
			if(typeof(jQuery) == "undefined"){ throw "cant Initilize using a String selector with out jQuery!"; return;}
			MonthByValueKeyDown.element = jQuery(element);
			element = jQuery(element);
		}
		if(typeof(element) == "object"){
			if(element.jquery){
				if(element.length > 1){
					throw "Can't use on more than a single element";
				}
				MonthByValueKeyDown.element = jQuery(element)[0];
				element = jQuery(element);
			}else{
				if(MonthByValueKeyDown.isElement(element)){
					MonthByValueKeyDown.element = element;
					element.bind = (element.addEventListener)? element.addEventListener:element.attachEvent;
				}
			}
		}
		element.bind("keydown", function(e){
			evt = (e)? e:window.event;
			if(!MonthByValueKeyDown.shift && !MonthByValueKeyDown.ctrl){
				if(evt.keyCode == 16){ MonthByValueKeyDown.shift = true; return; }
				if(evt.keyCode == 17){ MonthByValueKeyDown.ctrl = true; return; }
				if(typeof(MonthByValueKeyDown.KeyCodeMap[evt.keyCode]) != "undefined"){
					MonthByValueKeyDown.keysPressed[MonthByValueKeyDown.keysPressed.length] = MonthByValueKeyDown.KeyCodeMap[evt.keyCode];
					if(MonthByValueKeyDown.keysPressed.lengh == 2){
						clearTimeout(MonthByValueKeyDown.timeout);
						MonthByValueKeyDown.selectItemByValue(MonthByValueKeyDown.keysPressed[0]+MonthByValueKeyDown.keysPressed[1])
					}else{
						MonthByValueKeyDown.timeout = setTimeout("MonthByValueKeyDown.timeLimitExpire()", MonthByValueKeyDown.TimeBetweenKeys);
					}
				}
			}
			// 49-57
		});
		element.bind("keyup", function(e){
			evt = (e)? e:window.event;
			if(evt.keyCode == 16){ MonthByValueKeyDown.shift = false; return; }
			if(evt.keyCode == 17){ MonthByValueKeyDown.ctrl = false; return; }
		});
	}
}