from zope.component import getMultiAdapter
from zope.app.form.interfaces import IInputWidget
from zope.interface import implements
from zope.app.form.browser.widget import SimpleInputWidget
from zope.app.pagetemplate.viewpagetemplatefile import ViewPageTemplateFile

class DynamicListWidget(SimpleInputWidget):

    template = ViewPageTemplateFile('widget.pt')

    def __call__(self):
        self.request.debug = 0
        return self.template()

    def _getFormInput(self):
        value = super(DynamicListWidget, self)._getFormInput()
        # Make sure that we always retrieve a list object from the
        # request, even if only a single item or nothing has been
        # entered
        if value is None:
            return ()
        elif isinstance(value, tuple):
            return value
        elif isinstance(value, list):
            return tuple(value)
        else:
            return (value,)

    def hasInput(self):
        return (self.name + '.marker') in self.request.form

    def hidden(self):
        s = ''
        for value in self._getFormValue():
            widget = getMultiAdapter(
                (self.context.value_type, self.request), IInputWidget)
            widget.name = self.name
            widget.setRenderedValue(value)
            s += widget.hidden()
        return s

    def resource_url(self):
        return '/'.join((self.context.context.absolute_url(),
                         '++resource++listoperations.js'))



