Created
March 12, 2013 18:15
-
-
Save MaxMorais/5145407 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# This widget requires jQuery-iMask to run | |
#https://github.com/cwolves/jQuery-iMask | |
def numeric_widget_factory(): | |
import locale, string, random | |
from gluon.sqlhtml import StringWidget | |
from gluon import current | |
from gluon.http import HTTP | |
T = current.T | |
locale.setlocale(locale.LC_ALL, '') | |
loc = locale.localeconv() | |
DECIMAL_PLACES = loc.get('frac_digits', None) or 2 | |
DECIMAL_SEPARATOR = loc.get('decimal_point', None) or '.' | |
THOUSAND_PLACES = loc.get('grouping', [3,0])[0] | |
THOUSAND_SEPARATOR = loc.get('thousands_sep', None) or ',' | |
lib = URL(c='static', f='js/jquery-imask.js'); | |
basescripts = [""" | |
;/* Plugin jQuery para o agendamento da execucao de uma determinada acao | |
no instante em que o objeto passar a existir */ | |
$.fn.onAvailable = function(fn){ | |
var self = this, sel = this.selector, timer; | |
if (this.length > 0) { | |
fn.call(this); | |
} else { | |
timer = setInterval(function(){ | |
if ($(sel).length > 0) { | |
fn.call(self); | |
clearInterval(timer); | |
} | |
}, 50); | |
} | |
};""", | |
""" | |
;function maskNumberThis(uid){ | |
$('input[uid="' + uid + '"]').onAvailable(function(){ | |
var element = $('input[uid="' + uid + '"]'); | |
if (!element.data('masked')){ | |
element.iMask({ | |
'type': 'number', | |
'decDigits': parseFloat(element.data('decimal_places')), | |
'groupSymbol': element.data('thousand_separator'), | |
'decSymbol': element.data('decimal_separator'), | |
'groupDigits': parseFloat(element.data('thousand_places')) | |
}); | |
} | |
}); | |
}; | |
""" | |
] | |
if not lib in response.files: | |
response.files.append(lib) | |
response.js = (response.js or '') | |
for basescript in basescripts: | |
if not basescript in response.js: | |
response.js += basescript.strip() | |
# Cria uma string randomica - útil para identificadores | |
UID = lambda length=8: ''.join([random.choice(string.ascii_letters+string.digits) for x in range(length)]) | |
class NumericWidget(StringWidget): | |
_class = 'number' | |
@classmethod | |
def widget(cls, field, value, **attributes): | |
#remove todos os caracters que não compõem um decimal | |
fin = lambda data: str(data).replace(THOUSAND_SEPARATOR, '').replace(DECIMAL_SEPARATOR, '.').strip() or '0.00' if data else None | |
if request.vars: | |
if request.vars[field.name]: | |
request.vars[field.name] = fin(request.vars[field.name]) | |
uid = UID() | |
defaults = { | |
'_data-DECIMAL_PLACES': DECIMAL_PLACES, | |
'_data-DECIMAL_SEPARATOR': DECIMAL_SEPARATOR, | |
'_data-THOUSAND_PLACES': THOUSAND_PLACES, | |
'_data-THOUSAND_SEPARATOR': THOUSAND_SEPARATOR, | |
'_uid': uid, | |
} | |
attr = cls._attributes(field, defaults, **attributes) | |
script = ";maskNumberThis(%s);"%`uid` | |
if not script in response.js: | |
response.js += script | |
return StringWidget.widget(field, fin(value), **attr) | |
return NumericWidget.widget | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment