]> git.llucax.com Git - z.facultad/75.52/sercom.git/blob - sercom/widgets.py
AjaxMultiSelect widget generico.
[z.facultad/75.52/sercom.git] / sercom / widgets.py
1 # Custom useful widget
2 #
3
4 from turbogears import widgets
5
6 class CustomTextField(widgets.TextField):
7     """Un input con un div al lado para ponerle info"""
8     template = '''
9         <span xmlns:py="http://purl.org/kid/ns#">
10         <input  
11             type="text"  
12             name="${name}"  
13             class="${field_class}"  
14             id="${field_id}"  
15             value="${value}"  
16             py:attrs="attrs"  
17          />
18          <span id="${field_id}_info" />
19         </span>
20 '''
21
22 MultiSelectAjax = '''
23
24     function _on_alumno_get_result(lista, loading, results)
25     {
26         load = MochiKit.DOM.getElement(loading);
27         load.style.visibility = 'hidden';
28         if (results.error) {
29             alert(results.msg);
30             return;
31         }
32         MochiKit.DOM.appendChildNodes(lista, OPTION({'value':results.msg.id}, results.msg.value));
33         l = MochiKit.DOM.getElement(lista);
34         for (i=0; i<l.options.length; i++) { 
35             l.options[i].selected = true; 
36         }
37     }
38
39     function _on_alumno_get_error(loading, results)
40     {
41         alert(results)
42         load = MochiKit.DOM.getElement(loading);
43         load.style.visibility = 'hidden';
44     }
45
46     function sacar_de_la_lista(lista)
47     {
48         l = MochiKit.DOM.getElement(lista);
49         if (l.selectedIndex < 0) return;
50         
51         /* caso especial, 1 solo item */
52         if (l.options.length == 1) {
53             l.options.length = 0
54             return;
55         }
56
57         for (i=l.selectedIndex; i<l.options.length-1;i++)
58             l.options[i] = l.options[i+1];
59     }
60 '''
61
62 class AjaxMultiSelect(widgets.MultipleSelectField):
63     template = '''
64     <div style="width:250px" xmlns:py="http://purl.org/kid/ns#">  
65     <div>
66     <input type="text" id="${field_id}_nuevo" size="10" />
67     <img src="/static/images/loading.gif" align="baseline" style="visibility:hidden;" id="${name}_loading" width="16px" height="16px" />
68     <input type="button" id="_agregar" value="Agregar"
69         onClick=" ${on_add}('${field_id}_nuevo', '${field_id}', '${name}_loading'); " />
70     </div>
71     <div>
72     <select  
73         multiple="multiple"  
74         size="${size}"  
75         name="${name}"  
76         class="${field_class}"  
77         id="${field_id}"  
78         py:attrs="attrs"  
79         style="width:250px;"
80     >
81         <optgroup py:for="group, options in grouped_options"  
82             label="${group}"  
83             py:strip="not group"  
84         >  
85         <option py:for="value, desc, attrs in options"  
86             value="${value}"  
87             py:attrs="attrs"  
88             py:content="desc"  
89         />  
90         </optgroup>  
91     </select>
92     </div>
93     <div align="center">
94     <input type="button" id="_sacar" value="Borrar" style="width:100%;"
95         onClick="sacar_de_la_lista('${field_id}'); " />
96     </div>
97     </div>
98     '''
99     javascript = [widgets.JSSource(MultiSelectAjax)]
100     on_add = "alert('Not defined action');"
101
102     def __init__(self, **kw):
103         self.params.append('on_add')
104         self.on_add = "alert('Not defined action');"
105         widgets.MultipleSelectField.__init__(self, **kw)