vpn.email
Version:
vpn.email client
180 lines (143 loc) • 5.09 kB
JavaScript
$.widget( "metro.keypad" , {
version: "3.0.0",
options: {
target: false,
shuffle: false,
length: false,
keys: ['1', '2', '3', '4', '5', '6', '7', '8', '9', '0'],
size: 32,
onKey: function(key){},
onChange: function(value){}
},
//_keys: ['1', '2', '3', '4', '5', '6', '7', '8', '9', '0'],
_create: function () {
var that = this, element = this.element, o = this.options;
$.each(element.data(), function(key, value){
if (key in o) {
try {
o[key] = $.parseJSON(value);
} catch (e) {
o[key] = value;
}
}
});
if (typeof o.keys === 'string') {
o.keys = o.keys.split(",");
}
if (o.target !== false) {
o.target = $(o.target);
}
this._createKeypad();
element.data('keypad', this);
},
_shuffleKeys: function(){
var that = this, element = this.element, o = this.options;
var keys = o.keys.slice(0);
var keypad = this._keypad;
var keys_length = keys.length + 2;
if (o.shuffle) {
keys = keys.shuffle();
}
keypad.html('').css({
width: keys_length / 4 * o.size + (keys_length / 4 + 1) * 2 + 2
});
keys.map(function(i){
$("<div/>").addClass('key').html(i).data('key', i).appendTo(keypad);
});
$("<div/>").addClass('key').html('←').data('key', '←').appendTo(keypad);
$("<div/>").addClass('key').html('×').data('key', '×').appendTo(keypad);
},
_createKeypad: function(){
var that = this, element = this.element, o = this.options;
var keypad;
if (element.hasClass('input-control')) {
keypad = $("<div/>").addClass('keypad keypad-dropdown').css({
position: 'absolute',
'z-index': 1000,
display: 'none'
}).appendTo(element);
o.target = element.find('input');
element.on('click', function(e){
if (keypad.css('display') === 'none') {
keypad.show();
} else {
keypad.hide();
}
var opened_pads = $(".keypad.keypad-dropdown");
$.each(opened_pads, function(){
if (!$(this).is(keypad)) {
$(this).hide();
}
});
e.stopPropagation();
});
$('html').on('click', function(){
$(".keypad.keypad-dropdown").hide();
});
} else {
keypad = element;
keypad.addClass('keypad');
}
if (o.target !== false) {
$(o.target).attr('readonly', true);
}
if (keypad.parent().data('role') === 'dropdown') {
keypad.parent().css({
top: '100%'
});
}
this._keypad = keypad;
this._shuffleKeys();
keypad.on('click', '.key', function(e){
var key = $(this);
var result;
if (o.target) {
if (key.data('key') !== '←' && key.data('key') !== '×') {
if (o.length && $(o.target).val().length === o.length) {
return false;
}
$(o.target).val($(o.target).val() + '' + key.data('key'));
} else {
if (key.data('key') === '×') {
$(o.target).val('');
}
if (key.data('key') === '←') {
var val = $(o.target).val();
$(o.target).val(val.substring(0, val.length - 1))
}
}
o.target.trigger('change');
}
if (typeof o.onKey === 'function') {
o.onKey(key);
} else {
if (typeof window[o.onKey] === 'function') {
window[o.onKey](key);
} else {
result = eval("(function(){"+o.onKey+"})");
result.call(key);
}
}
if (typeof o.onChange === 'function') {
o.onChange(o.target.val());
} else {
if (typeof window[o.onChange] === 'function') {
window[o.onChange](o.target.val());
} else {
result = eval("(function(){"+o.onChange+"})");
result.call({value: o.target.val()});
}
}
if (o.shuffle) {
that._shuffleKeys();
}
e.preventDefault();
e.stopPropagation();
});
},
_destroy: function () {
},
_setOption: function ( key, value ) {
this._super('_setOption', key, value);
}
});