Attached file.

-- 

--- 
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+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.


#-*- encoding:utf-8 -*-
#    * formModal
from gluon.html import A, DIV, H3, BUTTON, OPTION, TAG, I
from gluon.compileapp import LOAD
from gluon.http import HTTP
from gluon import current


class formModal(object):

    def __init__(self, value, title_btn, title_modal, _crud):
        self.value = value
        self.title_btn = title_btn
        self.title_modal = title_modal
        self.key = "newkey"
        self.crud = _crud

    def btn_show(self):
        #  Button to trigger modal .
        btn_show_modal = A(I(_class="icon-plus-sign"),
                            ' ', self.value,
                            **{"_role": "button",
                               #"_class": "btn btn-link",
                               "_data-toggle": "modal",
                               "_href": "#Modal_%s" % self.key,
                               "_title": self.title_btn})
        return btn_show_modal

    def div_modal(self, content_modal):
        div_modal = DIV(
                        DIV(
                            H3(self.title_modal, _id="myModalLabel"),
                            _class="modal-header"),
                        DIV(content_modal, _class="modal-body"),
                        DIV(
                            BUTTON("Cerrar", **{"_class": "btn",
                                                "_data-dismiss": "modal",
                                                "_aria-hidden": "true"}),
                            _class="modal-footer",
                            ),
                        **{"_id": "Modal_%s" % self.key,
                           "_class": "modal hide face",
                           "_tabindex": "-1",
                           "_role": "dialog",
                           "_aria-hidden": "true",
                           "_aria-labelledby": "myModalLabel"}
                    )
        return div_modal

    def create(self, field):
        request = current.request
        response = current.response
        crud = self.crud

        if not field.type.startswith('reference'):
            raise SyntaxError("S?lo puede usarse con field reference")
        if not hasattr(field.requires, 'options'):
            raise SyntaxError("No puede determinarse las opciones")

        self.key = str(field).replace('.', '_')
        if request.get_vars._ajax_add == str(field):
            def update_select(form):
                options = TAG[''](*[OPTION(v,
                                    _value=k,
                                    _selected=str(form.vars.id) == str(k))
                                    for (k, v) in field.requires.options()])
                _cmd = "jQuery('#%s').html('%s');"
                _cmd += "jQuery('#Modal_%s').modal('hide')"
                command = _cmd % (self.key,
                                  options.xml().replace("'", "\'"),
                                  self.key)
                response.headers['web2py-component-command'] = command
            table = field._db[field.type[10:]]
            raise HTTP(200,
                       crud.create(table,
                                   onaccept=update_select).xml(),
                       **response.headers)

        return TAG[''](
                self.btn_show(),
                self.div_modal(LOAD(request.controller,
                                    request.function,
                                    args=request.args,
                                    vars=dict(_ajax_add=field), ajax=True))
            )

Reply via email to