
cherokee at cherokee-project
Jan 29, 2010, 3:17 PM
Post #1 of 1
(132 views)
Permalink
|
|
[4206] CTK/trunk: CTK.TextField can be 'optional' now ( optionally filled out)
|
|
Revision: 4206 http://svn.cherokee-project.com/changeset/4206 Author: alo Date: 2010-01-30 00:17:58 +0100 (Sat, 30 Jan 2010) Log Message: ----------- CTK.TextField can be 'optional' now (optionally filled out) Modified Paths: -------------- CTK/trunk/CTK/TextField.py Added Paths: ----------- CTK/trunk/static/js/jquery.form-defaults.js Modified: CTK/trunk/CTK/TextField.py =================================================================== --- CTK/trunk/CTK/TextField.py 2010-01-29 22:34:51 UTC (rev 4205) +++ CTK/trunk/CTK/TextField.py 2010-01-29 23:17:58 UTC (rev 4206) @@ -25,6 +25,11 @@ from Widget import Widget, RenderResponse from Server import cfg +HEADER = [ + '<script type="text/javascript" src="/CTK/js/jquery.form-defaults.js"></script>' +] + + class TextField (Widget): def __init__ (self, props=None): Widget.__init__ (self) @@ -40,7 +45,6 @@ def __get_input_props (self): render = '' - for prop in self._props: render += " %s" %(prop) value = self._props[prop] @@ -61,16 +65,22 @@ # Render the error reporting field html += '<div class="error"%s></div>' %(self.__get_error_div_props()) - return RenderResponse (html) + # Watermark + js = '' + if self._props.get('optional'): + js += "$('#%s').DefaultValue('optional','%s');" %( + self._props.get('id'), _("optional")) + return RenderResponse (html, js, headers=HEADER) + class TextFieldPassword (TextField): def __init__ (self, *a, **kw): TextField.__init__ (self, *a, **kw) self.type = "password" class TextCfg (TextField): - def __init__ (self, key, props=None): + def __init__ (self, key, optional=False, props=None): if not props: props = {} @@ -79,6 +89,9 @@ if val: props['value'] = val + if optional: + props['optional'] = True + # Other properties props['name'] = key Added: CTK/trunk/static/js/jquery.form-defaults.js =================================================================== --- CTK/trunk/static/js/jquery.form-defaults.js (rev 0) +++ CTK/trunk/static/js/jquery.form-defaults.js 2010-01-29 23:17:58 UTC (rev 4206) @@ -0,0 +1,45 @@ +jQuery.fn.DefaultValue = function(klass, text){ + return this.each(function(){ + //Make sure we're dealing with text-based form fields + if (this.type != 'text' && this.type != 'password' && this.type != 'textarea') + return; + + //Store field reference + var fld_current = this; + + //Set value initially if none are specified + if($(this).val() == '') { + $(this).val(text); + } else { + //Other value exists - ignore + return; + } + + //Remove values on focus + $(this).focus(function() { + if($(this).val() == text || $(this).val() == '') { + $(this).removeClass(klass); + $(this).val(''); + } + }); + + //Place values back on blur + $(this).blur(function() { + if($(this).val() == text || $(this).val() == '') { + $(this).addClass(klass); + $(this).val(text); + } + }); + + //Capture parent form submission + //Remove field values that are still default + $(this).parents("form").each(function() { + //Bind parent form submit + $(this).submit(function() { + if($(fld_current).val() == text) { + $(fld_current).val(''); + } + }); + }); + }); +}; \ No newline at end of file
|