In the attachment.

Thomas

Le mercredi 12 mars 2014 à 10:26 -0400, Richard Vézina a écrit :
> Hmmm...
> 
> 
> Code please!
> 
> 
> Richard
> 
> 
> On Wed, Mar 12, 2014 at 9:56 AM, Thomas Bellembois
> <[email protected]> wrote:
>         Yes, I tried but it did not work either...
>         
>         There is something weird, actually my widget receives an
>         integer for
>         value (actually a table reference).
>         I generate a checkbox <input type="checkbox" value="1234"> and
>         then a
>         span with the database entry corresponding to the value such
>         as:
>         row = db(db.table.id == value).select().first()
>         I use row.label to fill in my span.
>         
>         The problem is, when a form is accepted an "keepvalues = True"
>         the
>         checkbox is well set to 1234 but my span can NOT be filled in
>         because
>         the "value" is to passed to my widget whereas it is for an
>         update (when
>         I use the widget to update a database entry).
>         
>         Thanks for your help.
>         
>         Thomas
>         
>         Le mercredi 12 mars 2014 à 09:11 -0400, Richard Vézina a
>         écrit :
>         > Ok... But did you try my suggestion? The way to set the
>         default value
>         > is the same, you just have to figure how to set it in the
>         class, it
>         > just less obvious some time.
>         >
>         >
>         > Richard
>         >
>         >
>         > On Wed, Mar 12, 2014 at 8:31 AM, Thomas Bellembois
>         > <[email protected]> wrote:
>         >         Actually I need to initialize many parameters so I
>         took model
>         >         on the
>         >         AutocompleteWidget widget.
>         >
>         >
>         >
>         >         Le mardi 11 mars 2014 à 12:54 -0400, Richard Vézina
>         a écrit :
>         >         > Do you really need a class?
>         >         >
>         >         >
>         >         > You can create widget as a simple function as
>         shown in the
>         >         book. I say
>         >         > that because it easier to show you how to set
>         default value
>         >         with a
>         >         > simple function (less code)...
>         >         >
>         >         >
>         >         > def mysupercustomwidget(field, value, ...)
>         >         >     ...
>         >         >     if value is not None and value != '':
>         >         >
>         >         >         field_value = value
>         >         >     INPUT(..., value=field_value)
>         >         >     ...
>         >         >
>         >         >
>         >         > Hope it helps.
>         >         >
>         >         >
>         >         > Richard
>         >         >
>         >         >
>         >         >
>         >         >
>         >         > On Tue, Mar 11, 2014 at 10:14 AM, Thomas
>         Bellembois
>         >         > <[email protected]> wrote:
>         >         >         Dear web2py community,
>         >         >
>         >         >         I have build my own widget like:
>         >         >
>         >         >         class CHIMITHEQUE_MULTIPLE_widget(object):
>         >         >
>         >         >             def __init__(self, ..., **attributes):
>         >         >                ...
>         >         >
>         >         >             def __call__(self, field, value):
>         >         >                 ...
>         >         >                 # build the widget and populate
>         with the
>         >         value
>         >         >         parameter if not
>         >         >         Null
>         >         >                 ...
>         >         >                 return DIV(...)
>         >         >
>         >         >         I have no problems except for the
>         >         validate(keepvalues=True)
>         >         >         feature, my
>         >         >         widget is not populated. Actually no value
>         is passed
>         >         to the
>         >         >         __call__
>         >         >         function.
>         >         >
>         >         >         While updating a database item, the widget
>         populates
>         >         properly.
>         >         >
>         >         >         I do not know how exactly keepvalues
>         works, so I may
>         >         have
>         >         >         missed
>         >         >         something.
>         >         >
>         >         >         Any idea ?
>         >         >
>         >         >         Regards,
>         >         >
>         >         >         Thomas
>         >         >
>         >         >
>         >         >
>         >         >         --
>         >         >         Resources:
>         >         >         - http://web2py.com
>         >         >         - http://web2py.com/book (Documentation)
>         >         >         - http://github.com/web2py/web2py (Source
>         code)
>         >         >         -
>         https://code.google.com/p/web2py/issues/list
>         >         (Report Issues)
>         >         >         ---
>         >         >         You received this message because you are
>         subscribed
>         >         to the
>         >         >         Google Groups "web2py-users" group.
>         >         >         To unsubscribe from this group and stop
>         receiving
>         >         emails from
>         >         >         it, send an email to web2py
>         >         [email protected].
>         >         >         For more options, visit
>         >         https://groups.google.com/d/optout.
>         >         >
>         >         >
>         >         > --
>         >         > Resources:
>         >         > - http://web2py.com
>         >         > - http://web2py.com/book (Documentation)
>         >         > - http://github.com/web2py/web2py (Source code)
>         >         > - https://code.google.com/p/web2py/issues/list
>         (Report
>         >         Issues)
>         >         > ---
>         >         > You received this message because you are
>         subscribed to the
>         >         Google
>         >         > Groups "web2py-users" group.
>         >         > To unsubscribe from this group and stop receiving
>         emails
>         >         from it, send
>         >         > an email to [email protected].
>         >         > For more options, visit
>         https://groups.google.com/d/optout.
>         >
>         >
>         >
>         >         --
>         >         Resources:
>         >         - http://web2py.com
>         >         - http://web2py.com/book (Documentation)
>         >         - http://github.com/web2py/web2py (Source code)
>         >         - https://code.google.com/p/web2py/issues/list
>         (Report Issues)
>         >         ---
>         >         You received this message because you are subscribed
>         to the
>         >         Google Groups "web2py-users" group.
>         >         To unsubscribe from this group and stop receiving
>         emails from
>         >         it, send an email to web2py
>         [email protected].
>         >         For more options, visit
>         https://groups.google.com/d/optout.
>         >
>         >
>         >
>         > --
>         > Resources:
>         > - http://web2py.com
>         > - http://web2py.com/book (Documentation)
>         > - http://github.com/web2py/web2py (Source code)
>         > - https://code.google.com/p/web2py/issues/list (Report
>         Issues)
>         > ---
>         > You received this message because you are subscribed to the
>         Google
>         > Groups "web2py-users" group.
>         > To unsubscribe from this group and stop receiving emails
>         from it, send
>         > an email to [email protected].
>         > For more options, visit https://groups.google.com/d/optout.
>         
>         
>         
>         --
>         Resources:
>         - http://web2py.com
>         - http://web2py.com/book (Documentation)
>         - http://github.com/web2py/web2py (Source code)
>         - https://code.google.com/p/web2py/issues/list (Report Issues)
>         ---
>         You received this message because you are subscribed to the
>         Google Groups "web2py-users" group.
>         To unsubscribe from this group and stop receiving emails from
>         it, send an email to [email protected].
>         For more options, visit https://groups.google.com/d/optout.
>         
> 
> 
> -- 
> Resources:
> - http://web2py.com
> - http://web2py.com/book (Documentation)
> - http://github.com/web2py/web2py (Source code)
> - https://code.google.com/p/web2py/issues/list (Report Issues)
> --- 
> You received this message because you are subscribed to the Google
> Groups "web2py-users" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to [email protected].
> For more options, visit https://groups.google.com/d/optout.


-- 
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
--- 
You received this message because you are subscribed to the Google Groups 
"web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/d/optout.
# Copyright 2011 - Thomas Bellembois [email protected]
# Cecill licence, see LICENSE
# $Id: chimitheque_multiple_widget.py 140 2014-03-05 13:48:37Z tbellemb $
# -*- coding: utf-8 -*-
from gluon import current
from gluon.html import DIV
from gluon.html import IMG
from gluon.html import INPUT
from gluon.html import SCRIPT
from gluon.html import SPAN
from gluon.html import URL
from gluon.html import XML
from types import ListType
from types import StringType
from uuid import uuid4
import json
from chimitheque_logger import chimitheque_logger
mylogger = chimitheque_logger()

def CHIMITHEQUE_MULTIPLE_widget(field, value,
                                ref_field,
                                 configuration={'*': ''},
                                 minchar=2,
                                 max_item_length=40,
                                 image_insert_url = URL('static','images/cmw_insert.png'),
                                 image_select_url = URL('static','images/cmw_select.png'),
                                 image_delete_url = URL('static','images/cmw_delete.png'),
                                 text_delete = current.T('delete item', lazy=False),
                                 text_no_item_selected = current.T('no item selected', lazy=False),
                                 text_item_already_selected = current.T('item already selected', lazy=False),
                                 text_close_list = current.T('close list', lazy=False),
                                 text_submit = current.T('submit', lazy=False),
                                 **attributes):

    if '_class' not in attributes.keys():
        attributes['_class'] = 'not_required'

    uid = uuid4().hex

    mylogger.debug(message='field._tablename:%s' %(str(field._tablename)))
    mylogger.debug(message='field:%s' %(str(field)))
    mylogger.debug(message='field.name:%s' %(str(field.name)))
    mylogger.debug(message='field.type:%s' %(str(field.type)))
    mylogger.debug(message='field.requires:%s' %(str(field.requires)))
    mylogger.debug(message='type(value):%s' %(str(type(value))))
    mylogger.debug(message='value:%s' %(str(value)))

    if current.request and current.request['function']: function = current.request['function']
    function_configuration = configuration['*'] if configuration.keys().count('*') > 0 else configuration[function] if function in configuration.keys() else ''
    
    # query parameter not used yet...
    if 'query' in function_configuration:
        query = function_configuration['query'].as_json()
    else:
        query = None
    if 'disable_validate' in function_configuration:
        disable_validate = function_configuration['disable_validate']
    else:
        disable_validate = False
    if 'add_in_db' in function_configuration:
        add_in_db = function_configuration['add_in_db']
    else:
        add_in_db = False
    if 'fake_add_in_db' in function_configuration:
        fake_add_in_db = function_configuration['fake_add_in_db']
    else:
        fake_add_in_db = False
    if 'multiple' in function_configuration:
        multiple = function_configuration['multiple']
    else:
        multiple = False
    max_nb_item = 20
    if 'max_nb_item' in function_configuration:
        max_nb_item = function_configuration['max_nb_item']
    func_lambda = ''
    if 'func_lambda' in function_configuration:
        func_lambda = function_configuration['func_lambda']
    
    disabled = '_disabled' in attributes.keys()
    mylogger.debug(message='add_in_db:%s' %(str(add_in_db)))
    mylogger.debug(message='multiple:%s' %(str(multiple)))
    mylogger.debug(message='disabled:%s' %(str(disabled)))
    mylogger.debug(message='max_nb_item:%s' %(str(max_nb_item)))
    
    if (value) and (type(value) is StringType) and (value == '0'): nb_item = 0
    elif (value) and (type(value) is StringType) and (value == '|0|'): nb_item = 0
    elif (value) and (type(value) is ListType) and (value[0] == 0): nb_item = 0
    elif (value) and (type(value) is ListType) and (value[0] == '|0|'): nb_item = 0
    elif (value) and (type(value) is ListType): nb_item = len(value)
    elif value and value != '': nb_item = 1
    else: nb_item = 0
    
    if value and not type(value) is ListType and value != '': value = [value]
    
    mylogger.debug(message='nb_item:%s' %(str(nb_item)))
    
    checkboxes_form = DIV()
    suggestions_form = DIV(_id='%s_suggestions' %(uid), _class='CHIMITHEQUE_MULTIPLE_widget_suggestions')
    MESSAGE_form = DIV(_id='%s_MESSAGE' %(uid), _class='CHIMITHEQUE_MULTIPLE_widget_MESSAGE')
    
    search_input = DIV(INPUT(_name='%s_search' %uid, _type='text', _title='%s_search' %uid),
                          suggestions_form,
                             _id='%s_search' %(uid),
                             _class='search_input')
    if not disabled:
        final_form = DIV(search_input)
    else:
        final_form = DIV()
    
    _ajax_parameters = {
                       'uid': uid,
                       'multiple': multiple,
                       'disable_validate': disable_validate,
                       'add_in_db': add_in_db,
                       'fake_add_in_db': fake_add_in_db,
                       'ref_field_tablename': ref_field._tablename,
                       'ref_field_name': ref_field.name,
                       'max_nb_item': max_nb_item,
                       'max_item_length': max_item_length,
                       'lambda': func_lambda,
                       'query': query,
                       'text_close_list': text_close_list,
                       'text_submit': text_submit,
                       'image_select_url': image_select_url
                       }
    ajax_parameters = json.dumps(_ajax_parameters)
    
    if not disabled:
        if add_in_db:
    
            search_input.append(IMG(_src=image_insert_url,
                                  _alt='submit',
                                  _id='%s_add' %uid,
                                  _title=text_submit,
                                  _style='visibility: hidden;',
                                  _class='CHIMITHEQUE_MULTIPLE_widget_addindb',
                                  _onclick='''
                                        // adding the search parameter to the JSON object
                                        ajax_parameters = %(ajax_parameters)s;
                                        ajax_parameters["search"] = $('input[name=%(uid)s_search]').val();
                                        var ret = $.ajax({
                                                   type: "POST",
                                                   url: "/%(application)s/chimitheque_multiple_widget/item_add",
                                                   data: JSON.stringify(ajax_parameters),
                                                   dataType: "json",
                                                   contentType: "application/json; charset=utf-8",
                                                   async: false
                                                 }).done(function(data) {
                                                        var _action = data['action'];
                                                        var _id = data['id'];
                                                        var _val = data['val'];
                                                        var _encval = data['encval'];
    
                                                        var funcCall = "addReplaceCheckBox%(uid)s" + "('" + _action + "','" + _id + "','" + _val + "','" + _encval + "')";
                                                        eval(funcCall);
    
                                                        $('img#%(uid)s_add').attr('style', 'visibility: hidden;');
                                                    });
                                                ''' %{'uid': uid,
                                                      'application': current.request.application,
                                                      'ajax_parameters': ajax_parameters}))
    
    
    final_form.append(DIV(DIV(checkboxes_form,
                             _id='%s' %(uid),
                             _class='%s_%s' %(ref_field._tablename, ref_field.name)),
                         **attributes))
    
    final_form.append(DIV(MESSAGE_form,
                     _id='%s_%s' %(str(field._tablename), str(field.name))))
    
    if nb_item == 0:
        checkboxes_form.append(SPAN(XML(text_no_item_selected),
                                    _id='%s_span_no_selected' %field.name))
        final_form.append(DIV(INPUT(_name='%s' %field.name,
                        _id='%s_hidden' %field.name,
                        _type='text',
                        _value='',
                        #_style='visibility: hidden; height: 0px;',
                        #value=True,
                        requires=field.requires
                        )))
    else:
        for i in range(0, nb_item):
    
            mylogger.debug(message='i:%i' %(i))
            row = current.db(current.db['%s' %ref_field._tablename]['id'] == (value[i])).select().first()
            mylogger.debug(message='row:%s' %row)
    
            if row:
                if multiple:
                    input = INPUT(_name='%s' %field.name,
                                     #_id='%s' %field.name,
                                     _id="%s_%s" % (field._tablename, field.name),
                                     _type='checkbox',
                                     _class='CHIMITHEQUE_MULTIPLE_widget_selected %s' % field.type,
                                     _encvalue = uid,
                                     _value = (value[i] if value else None),
                                     value = True,
                                     requires=field.requires
                                     )
                else:
                    input = INPUT(_name='%s' %field.name,
                                     #_id='%s' %field.name,
                                     _id="%s_%s" % (field._tablename, field.name),
                                     _type='radio',
                                     _class='CHIMITHEQUE_MULTIPLE_widget_selected %s' % field.type,
                                     _encvalue = uid,
                                     _value = (value[i] if value else None),
                                     value = (value[i] if value else None),
                                     requires=field.requires
                                     )
                if multiple:
                    checkboxes_form.append(DIV(input,
                                SPAN('%s' %current.db['%s' %ref_field._tablename]._format(row)),
                                _class='CHIMITHEQUE_MULTIPLE_widget_selected'))
                elif not disabled:
                    checkboxes_form.append(DIV(
                                IMG(_src=image_delete_url, _alt=text_delete, _title=text_delete, _onclick='deleteItem%s();' %uid, _style='float: left;'),
                                input,
                                SPAN('%s' %current.db['%s' %ref_field._tablename]._format(row)),
                                _class='CHIMITHEQUE_MULTIPLE_widget_selected'))
                else:
                    checkboxes_form.append(DIV(
                                input,
                                SPAN('%s' %current.db['%s' %ref_field._tablename]._format(row)),
                                _class='CHIMITHEQUE_MULTIPLE_widget_selected'))
    
    return DIV(final_form,
               SCRIPT(
                      """
                        function disableAddButton%(uid)s() {
                            $('#%(uid)s_add').attr('style', 'visibility: hidden;');
    
                        }
    
                        function displayMessage%(uid)s(MESSAGE) {
                            $('#%(uid)s_MESSAGE span').remove();
                            $('#%(uid)s_MESSAGE').append('<span class="error">' + MESSAGE + '</span>');
    
                        }
    
                        function deleteItem%(uid)s() {
                            $('#%(uid)s').find('div[class=CHIMITHEQUE_MULTIPLE_widget_selected]').remove();
    
                            console.log($('input[name=%(field_name)s]').length);
                            /* enabling the hidden field if needed */
                            if ($('input[name=%(field_name)s]').length <= 1) {
                                console.log("input name '%(field_name)s' was the last element");
                                $('input[id=%(field_name)s_hidden]').removeAttr('disabled');
                                $('div[id=%(uid)s]').append('<span id="%(field_name)s_span_no_selected">%(no_item_selected)s</span>');
    
                            }
                            else {
                                console.log("input name '%(field_name)s' was not the last element");
                            }
                        }
    
                        function addReplaceCheckBox%(uid)s(action, id, val, encval) {
                                console.log(arguments.callee.name);
                                console.log('action:' + action);
                                console.log('id:' + id);
                                console.log('val:' + val);
                                console.log('encval:' + encval);
    
                                /* disabling the hidden field */
                                $('input[id=%(field_name)s_hidden]').attr('disabled','true');
                                $('span[id=%(field_name)s_span_no_selected]').remove();
    
                                if ($('#%(uid)s').find('input[value='+id+'][encvalue='+encval+']').length != 0) {
                                    alert('%(text_item_already_selected)s');
                                }
                                else {
                                    var newDiv = $('<div class="CHIMITHEQUE_MULTIPLE_widget_selected"/>');
                                    var newDel = $('<img/>').attr({
                                        'src': '%(image_delete_url)s',
                                        'alt': '%(image_delete_alt)s',
                                        'title': '%(image_delete_title)s',
                                        'onclick': 'deleteItem%(uid)s();'
                                    });
                                    var newElem = $('<input/>').attr({
                                        'id': '%(field_name)s',
                                        'type': '%(type)s',
                                        'checked': 'checked',
                                        'name': '%(field_name)s',
                                        'value': id,
                                        'class': 'CHIMITHEQUE_MULTIPLE_widget_selected',
                                        'encvalue': encval,
                                    });
                                    if (action == 'replace') {
                                        newDiv.append(newDel);
                                    }
                                    newDiv.append(newElem);
                                    newDiv.append(val);
                                    if (action == 'replace') {
                                        $('#%(uid)s div').remove();
                                    }
                                    $('#%(uid)s').append(newDiv);
                                }
                                $('input[name=%(uid)s_search]').val('');
    
                        }
    
                        function autocomplete%(uid)s() {
                               $elem = $('input[type=text][name=%(uid)s_search]')
                               var inputLength = $elem.val().length;
                               if (inputLength >= %(minchar)s) {
                                    // adding the search parameter to the JSON object
                                    ajax_parameters = %(ajax_parameters)s;
                                    ajax_parameters["search"] = $elem.val();
                                    var ret = $.ajax({
                                               type: "POST",
                                               url: "/%(application)s/chimitheque_multiple_widget/item_selector",
                                               data: JSON.stringify(ajax_parameters),
                                               dataType: "json",
                                               contentType: "application/json; charset=utf-8",
                                               async: false
                                             }).responseText;
    
                                    $('#%(uid)s_suggestions > *').remove();
                                    $('#%(uid)s_MESSAGE').text('');
                                    if (ret.substr(0, 5) == 'ERROR') {
                                        $('#%(uid)s_MESSAGE').text(ret);
                                        $('#%(uid)s_add').attr('style', 'visibility: hidden;');
                                    }else if (ret.substr(0, 4) == 'INDB'){
                                        $('#%(uid)s_add').attr('style', 'visibility: hidden;');
                                        $('#%(uid)s_suggestions').append(ret);
                                    }else if (ret.substr(0, 4) == 'NONE'){
                                        $('#%(uid)s_add').attr('style', 'visibility: visible;');
                                    }
                                    else {
                                        $('#%(uid)s_add').attr('style', 'visibility: visible;');
                                        $('#%(uid)s_suggestions').append(ret);
                                    }
                               }
                        }
    
                        $(document).ready(function() {
    
                        jQuery('input[type=text][name=%(uid)s_search]').bind('paste', function(e) {
                            setTimeout(function() {
                                autocomplete%(uid)s();
                            }, 0);
                        });
                        jQuery('input[type=text][name=%(uid)s_search]').typing({
                            start:function(event, $elem) {
                            },
                            stop: function(event, $elem) {
                                autocomplete%(uid)s();
                            }
                            ,
                            delay: 500
                        });
    
                    });
                      """ %{'disable_validate': disable_validate,
                            'add_in_db': add_in_db,
                            'multiple': multiple,
                            'uid': uid,
                            'field_tablename': field._tablename,
                            'field_name': field.name,
                            'ref_field_tablename': ref_field._tablename,
                            'ref_field_name': ref_field.name,
                            'minchar': minchar,
                            'image_delete_url': image_delete_url,
                            'image_delete_alt': text_delete,
                            'image_delete_title': text_delete,
                            'type': 'checkbox' if multiple else 'radio',
                            'max_nb_item': max_nb_item,
                            'max_item_length': max_item_length,
                            'lambda': func_lambda,
                            'image_delete_small': image_delete_url,
                            'text_item_already_selected': text_item_already_selected,
                            'no_item_selected': text_no_item_selected,
                            'application': current.request.application,
    
                            'ajax_parameters': ajax_parameters
                            }),
                        _class='CHIMITHEQUE_MULTIPLE_widget'
                      )


# class CHIMITHEQUE_MULTIPLE_widget(object):
# 
#     def __init__(self,
#                  ref_field,
#                  configuration={'*': ''},
#                  minchar=2,
#                  max_item_length=40,
#                  image_insert_url = URL('static','images/cmw_insert.png'),
#                  image_select_url = URL('static','images/cmw_select.png'),
#                  image_delete_url = URL('static','images/cmw_delete.png'),
#                  text_delete = current.T('delete item', lazy=False),
#                  text_no_item_selected = current.T('no item selected', lazy=False),
#                  text_item_already_selected = current.T('item already selected', lazy=False),
#                  text_close_list = current.T('close list', lazy=False),
#                  text_submit = current.T('submit', lazy=False),
#                  **attributes):
# 
#         self.ref_field = ref_field
#         self.minchar = minchar
#         self.max_item_length = max_item_length
#         self.configuration = configuration
#         self.uid = uuid4().hex
# 
#         self.image_insert_url = image_insert_url
#         self.image_select_url = image_select_url
#         self.image_delete_url = image_delete_url
# 
#         self.text_submit = text_submit
#         self.text_delete = text_delete
#         self.text_no_item_selected = text_no_item_selected
#         self.text_item_already_selected = text_item_already_selected
#         self.text_close_list = text_close_list
# 
#         self.attributes = attributes
#         if '_class' not in attributes.keys():
#             self.attributes['_class'] = 'not_required'
#         mylogger.debug(message='ref_field._tablename:%s' %(str(ref_field._tablename)))
#         mylogger.debug(message='ref_field.name:%s' %(str(ref_field.name)))
#         mylogger.debug(message='ref_field.type:%s' %(str(ref_field.type)))
# 
# 
#     def __call__(self, field, value):
# 
#         mylogger.debug(message='field._tablename:%s' %(str(field._tablename)))
#         mylogger.debug(message='field:%s' %(str(field)))
#         mylogger.debug(message='field.name:%s' %(str(field.name)))
#         mylogger.debug(message='field.type:%s' %(str(field.type)))
#         mylogger.debug(message='field.requires:%s' %(str(field.requires)))
#         mylogger.debug(message='type(value):%s' %(str(type(value))))
#         mylogger.debug(message='value:%s' %(str(value)))
# 
#         if current.request and current.request['function']: function = current.request['function']
#         function_configuration = self.configuration['*'] if self.configuration.keys().count('*') > 0 else self.configuration[function] if function in self.configuration.keys() else ''
# 
#         # query parameter not used yet...
#         if 'query' in function_configuration:
#             query = function_configuration['query'].as_json()
#         else:
#             query = None
#         if 'disable_validate' in function_configuration:
#             disable_validate = function_configuration['disable_validate']
#         else:
#             disable_validate = False
#         if 'add_in_db' in function_configuration:
#             add_in_db = function_configuration['add_in_db']
#         else:
#             add_in_db = False
#         if 'fake_add_in_db' in function_configuration:
#             fake_add_in_db = function_configuration['fake_add_in_db']
#         else:
#             fake_add_in_db = False
#         if 'multiple' in function_configuration:
#             multiple = function_configuration['multiple']
#         else:
#             multiple = False
#         max_nb_item = 20
#         if 'max_nb_item' in function_configuration:
#             max_nb_item = function_configuration['max_nb_item']
#         func_lambda = ''
#         if 'func_lambda' in function_configuration:
#             func_lambda = function_configuration['func_lambda']
# 
#         disabled = '_disabled' in self.attributes.keys()
#         mylogger.debug(message='add_in_db:%s' %(str(add_in_db)))
#         mylogger.debug(message='multiple:%s' %(str(multiple)))
#         mylogger.debug(message='disabled:%s' %(str(disabled)))
#         mylogger.debug(message='max_nb_item:%s' %(str(max_nb_item)))
# 
#         if (value) and (type(value) is StringType) and (value == '0'): nb_item = 0
#         elif (value) and (type(value) is StringType) and (value == '|0|'): nb_item = 0
#         elif (value) and (type(value) is ListType) and (value[0] == 0): nb_item = 0
#         elif (value) and (type(value) is ListType) and (value[0] == '|0|'): nb_item = 0
#         elif (value) and (type(value) is ListType): nb_item = len(value)
#         elif value and value != '': nb_item = 1
#         else: nb_item = 0
# 
#         if value and not type(value) is ListType and value != '': value = [value]
# 
#         mylogger.debug(message='nb_item:%s' %(str(nb_item)))
# 
#         checkboxes_form = DIV()
#         suggestions_form = DIV(_id='%s_suggestions' %(self.uid), _class='CHIMITHEQUE_MULTIPLE_widget_suggestions')
#         MESSAGE_form = DIV(_id='%s_MESSAGE' %(self.uid), _class='CHIMITHEQUE_MULTIPLE_widget_MESSAGE')
# 
#         search_input = DIV(INPUT(_name='%s_search' %self.uid, _type='text', _title='%s_search' %self.uid),
#                               suggestions_form,
#                                  _id='%s_search' %(self.uid),
#                                  _class='search_input')
#         if not disabled:
#             final_form = DIV(search_input)
#         else:
#             final_form = DIV()
# 
#         _ajax_parameters = {
#                            'uid': self.uid,
#                            'multiple': multiple,
#                            'disable_validate': disable_validate,
#                            'add_in_db': add_in_db,
#                            'fake_add_in_db': fake_add_in_db,
#                            'ref_field_tablename': self.ref_field._tablename,
#                            'ref_field_name': self.ref_field.name,
#                            'max_nb_item': max_nb_item,
#                            'max_item_length': self.max_item_length,
#                            'lambda': func_lambda,
#                            'query': query,
#                            'text_close_list': self.text_close_list,
#                            'text_submit': self.text_submit,
#                            'image_select_url': self.image_select_url
#                            }
#         ajax_parameters = json.dumps(_ajax_parameters)
# 
#         if not disabled:
#             if add_in_db:
# 
#                 search_input.append(IMG(_src=self.image_insert_url,
#                                       _alt='submit',
#                                       _id='%s_add' %self.uid,
#                                       _title=self.text_submit,
#                                       _style='visibility: hidden;',
#                                       _class='CHIMITHEQUE_MULTIPLE_widget_addindb',
#                                       _onclick='''
#                                             // adding the search parameter to the JSON object
#                                             ajax_parameters = %(ajax_parameters)s;
#                                             ajax_parameters["search"] = $('input[name=%(uid)s_search]').val();
#                                             var ret = $.ajax({
#                                                        type: "POST",
#                                                        url: "/%(application)s/chimitheque_multiple_widget/item_add",
#                                                        data: JSON.stringify(ajax_parameters),
#                                                        dataType: "json",
#                                                        contentType: "application/json; charset=utf-8",
#                                                        async: false
#                                                      }).done(function(data) {
#                                                             var _action = data['action'];
#                                                             var _id = data['id'];
#                                                             var _val = data['val'];
#                                                             var _encval = data['encval'];
# 
#                                                             var funcCall = "addReplaceCheckBox%(uid)s" + "('" + _action + "','" + _id + "','" + _val + "','" + _encval + "')";
#                                                             eval(funcCall);
# 
#                                                             $('img#%(uid)s_add').attr('style', 'visibility: hidden;');
#                                                         });
#                                                     ''' %{'uid': self.uid,
#                                                           'application': current.request.application,
#                                                           'ajax_parameters': ajax_parameters}))
# 
# 
#         final_form.append(DIV(DIV(checkboxes_form,
#                                  _id='%s' %(self.uid),
#                                  _class='%s_%s' %(self.ref_field._tablename, self.ref_field.name)),
#                              **self.attributes))
# 
#         final_form.append(DIV(MESSAGE_form,
#                          _id='%s_%s' %(str(field._tablename), str(field.name))))
# 
#         if nb_item == 0:
#             checkboxes_form.append(SPAN(XML(self.text_no_item_selected),
#                                         _id='%s_span_no_selected' %field.name))
#             final_form.append(DIV(INPUT(_name='%s' %field.name,
#                             _id='%s_hidden' %field.name,
#                             _type='hidden',
#                             _value='',
#                             #_style='visibility: hidden; height: 0px;',
#                             #value=True,
#                             requires=field.requires
#                             )))
#         else:
#             for i in range(0, nb_item):
#  
#                 mylogger.debug(message='i:%i' %(i))
#                 row = current.db(current.db['%s' %self.ref_field._tablename]['id'] == (value[i])).select().first()
#                 mylogger.debug(message='row:%s' %row)
#  
#                 if row:
#                     if multiple:
#                         input = INPUT(_name='%s' %field.name,
#                                          #_id='%s' %field.name,
#                                          _id="%s_%s" % (field._tablename, field.name),
#                                          _type='checkbox',
#                                          _class='CHIMITHEQUE_MULTIPLE_widget_selected %s' % field.type,
#                                          _encvalue = self.uid,
#                                          _value = (value[i] if value else None),
#                                          value = True,
#                                          requires=field.requires
#                                          )
#                     else:
#                         input = INPUT(_name='%s' %field.name,
#                                          #_id='%s' %field.name,
#                                          _id="%s_%s" % (field._tablename, field.name),
#                                          _type='radio',
#                                          _class='CHIMITHEQUE_MULTIPLE_widget_selected %s' % field.type,
#                                          _encvalue = self.uid,
#                                          _value = (value[i] if value else None),
#                                          value = (value[i] if value else None),
#                                          requires=field.requires
#                                          )
#                     if multiple:
#                         checkboxes_form.append(DIV(input,
#                                     SPAN('%s' %current.db['%s' %self.ref_field._tablename]._format(row)),
#                                     _class='CHIMITHEQUE_MULTIPLE_widget_selected'))
#                     elif not disabled:
#                         checkboxes_form.append(DIV(
#                                     IMG(_src=self.image_delete_url, _alt=self.text_delete, _title=self.text_delete, _onclick='deleteItem%s();' %self.uid, _style='float: left;'),
#                                     input,
#                                     SPAN('%s' %current.db['%s' %self.ref_field._tablename]._format(row)),
#                                     _class='CHIMITHEQUE_MULTIPLE_widget_selected'))
#                     else:
#                         checkboxes_form.append(DIV(
#                                     input,
#                                     SPAN('%s' %current.db['%s' %self.ref_field._tablename]._format(row)),
#                                     _class='CHIMITHEQUE_MULTIPLE_widget_selected'))
# 
#         return DIV(final_form,
#                    SCRIPT(
#                           """
#                             function disableAddButton%(uid)s() {
#                                 $('#%(uid)s_add').attr('style', 'visibility: hidden;');
# 
#                             }
# 
#                             function displayMessage%(uid)s(MESSAGE) {
#                                 $('#%(uid)s_MESSAGE span').remove();
#                                 $('#%(uid)s_MESSAGE').append('<span class="error">' + MESSAGE + '</span>');
# 
#                             }
# 
#                             function deleteItem%(uid)s() {
#                                 $('#%(uid)s').find('div[class=CHIMITHEQUE_MULTIPLE_widget_selected]').remove();
# 
#                                 console.log($('input[name=%(field_name)s]').length);
#                                 /* enabling the hidden field if needed */
#                                 if ($('input[name=%(field_name)s]').length <= 1) {
#                                     console.log("input name '%(field_name)s' was the last element");
#                                     $('input[id=%(field_name)s_hidden]').removeAttr('disabled');
#                                     $('div[id=%(uid)s]').append('<span id="%(field_name)s_span_no_selected">%(no_item_selected)s</span>');
# 
#                                 }
#                                 else {
#                                     console.log("input name '%(field_name)s' was not the last element");
#                                 }
#                             }
# 
#                             function addReplaceCheckBox%(uid)s(action, id, val, encval) {
#                                     console.log(arguments.callee.name);
#                                     console.log('action:' + action);
#                                     console.log('id:' + id);
#                                     console.log('val:' + val);
#                                     console.log('encval:' + encval);
# 
#                                     /* disabling the hidden field */
#                                     $('input[id=%(field_name)s_hidden]').attr('disabled','true');
#                                     $('span[id=%(field_name)s_span_no_selected]').remove();
# 
#                                     if ($('#%(uid)s').find('input[value='+id+'][encvalue='+encval+']').length != 0) {
#                                         alert('%(text_item_already_selected)s');
#                                     }
#                                     else {
#                                         var newDiv = $('<div class="CHIMITHEQUE_MULTIPLE_widget_selected"/>');
#                                         var newDel = $('<img/>').attr({
#                                             'src': '%(image_delete_url)s',
#                                             'alt': '%(image_delete_alt)s',
#                                             'title': '%(image_delete_title)s',
#                                             'onclick': 'deleteItem%(uid)s();'
#                                         });
#                                         var newElem = $('<input/>').attr({
#                                             'id': '%(field_name)s',
#                                             'type': '%(type)s',
#                                             'checked': 'checked',
#                                             'name': '%(field_name)s',
#                                             'value': id,
#                                             'class': 'CHIMITHEQUE_MULTIPLE_widget_selected',
#                                             'encvalue': encval,
#                                         });
#                                         if (action == 'replace') {
#                                             newDiv.append(newDel);
#                                         }
#                                         newDiv.append(newElem);
#                                         newDiv.append(val);
#                                         if (action == 'replace') {
#                                             $('#%(uid)s div').remove();
#                                         }
#                                         $('#%(uid)s').append(newDiv);
#                                     }
#                                     $('input[name=%(uid)s_search]').val('');
# 
#                             }
# 
#                             function autocomplete%(uid)s() {
#                                    $elem = $('input[type=text][name=%(uid)s_search]')
#                                    var inputLength = $elem.val().length;
#                                    if (inputLength >= %(minchar)s) {
#                                         // adding the search parameter to the JSON object
#                                         ajax_parameters = %(ajax_parameters)s;
#                                         ajax_parameters["search"] = $elem.val();
#                                         var ret = $.ajax({
#                                                    type: "POST",
#                                                    url: "/%(application)s/chimitheque_multiple_widget/item_selector",
#                                                    data: JSON.stringify(ajax_parameters),
#                                                    dataType: "json",
#                                                    contentType: "application/json; charset=utf-8",
#                                                    async: false
#                                                  }).responseText;
# 
#                                         $('#%(uid)s_suggestions > *').remove();
#                                         $('#%(uid)s_MESSAGE').text('');
#                                         if (ret.substr(0, 5) == 'ERROR') {
#                                             $('#%(uid)s_MESSAGE').text(ret);
#                                             $('#%(uid)s_add').attr('style', 'visibility: hidden;');
#                                         }else if (ret.substr(0, 4) == 'INDB'){
#                                             $('#%(uid)s_add').attr('style', 'visibility: hidden;');
#                                             $('#%(uid)s_suggestions').append(ret);
#                                         }else if (ret.substr(0, 4) == 'NONE'){
#                                             $('#%(uid)s_add').attr('style', 'visibility: visible;');
#                                         }
#                                         else {
#                                             $('#%(uid)s_add').attr('style', 'visibility: visible;');
#                                             $('#%(uid)s_suggestions').append(ret);
#                                         }
#                                    }
#                             }
# 
#                             $(document).ready(function() {
# 
#                             jQuery('input[type=text][name=%(uid)s_search]').bind('paste', function(e) {
#                                 setTimeout(function() {
#                                     autocomplete%(uid)s();
#                                 }, 0);
#                             });
#                             jQuery('input[type=text][name=%(uid)s_search]').typing({
#                                 start:function(event, $elem) {
#                                 },
#                                 stop: function(event, $elem) {
#                                     autocomplete%(uid)s();
#                                 }
#                                 ,
#                                 delay: 500
#                             });
# 
#                         });
#                           """ %{'disable_validate': disable_validate,
#                                 'add_in_db': add_in_db,
#                                 'multiple': multiple,
#                                 'uid': self.uid,
#                                 'field_tablename': field._tablename,
#                                 'field_name': field.name,
#                                 'ref_field_tablename': self.ref_field._tablename,
#                                 'ref_field_name': self.ref_field.name,
#                                 'minchar': self.minchar,
#                                 'image_delete_url': self.image_delete_url,
#                                 'image_delete_alt': self.text_delete,
#                                 'image_delete_title': self.text_delete,
#                                 'type': 'checkbox' if multiple else 'radio',
#                                 'max_nb_item': max_nb_item,
#                                 'max_item_length': self.max_item_length,
#                                 'lambda': func_lambda,
#                                 'image_delete_small': self.image_delete_url,
#                                 'text_item_already_selected': self.text_item_already_selected,
#                                 'no_item_selected': self.text_no_item_selected,
#                                 'application': current.request.application,
# 
#                                 'ajax_parameters': ajax_parameters
#                                 }),
#                             _class='CHIMITHEQUE_MULTIPLE_widget'
#                           )

Reply via email to