示例#1
0
    def get_previous_js_selectors(self):
        lis = []

        parent = self._parent_obj
        if not parent:
            # No parent it's the root tag.
            return lis

        cobj = self
        if isinstance(self, ChoiceElement):
            # Special case for ChoiceElement to get the good object
            cobj = self._value

        for child in parent._full_children:
            # We can delete the child it will continue to exist, it's detached
            child._delete_auto_added()
            if child == cobj:
                break
            parent_ident = prefixes_to_str(parent.prefixes_no_cache +
                                           [child.tagname])
            # NOTE: we use class selector with last in case element is a list.
            # It should be added after the last element of the list.
            lis += [('after', escape_attr('.%s%s' % (
                TREE_PREFIX,
                parent_ident
            )) + ':last')]

        lis.reverse()
        parent_ident = prefixes_to_str(parent.prefixes_no_cache)
        lis += [('inside', escape_attr('#%s%s' % (
            TREE_PREFIX,
            parent_ident
        )))]
        return lis
示例#2
0
    def get_previous_js_selectors(self):
        lis = []

        parent = self._parent_obj
        if not parent:
            # No parent it's the root tag.
            return lis

        cobj = self
        if isinstance(self, ChoiceElement):
            # Special case for ChoiceElement to get the good object
            cobj = self._value

        for child in parent._full_children:
            # We can delete the child it will continue to exist, it's detached
            child._delete_auto_added()
            if child == cobj:
                break
            parent_ident = prefixes_to_str(parent.prefixes_no_cache +
                                           [child.tagname])
            # NOTE: we use class selector with last in case element is a list.
            # It should be added after the last element of the list.
            lis += [
                ('after',
                 escape_attr('.%s%s' % (TREE_PREFIX, parent_ident)) + ':last')
            ]

        lis.reverse()
        parent_ident = prefixes_to_str(parent.prefixes_no_cache)
        lis += [('inside', escape_attr('#%s%s' % (TREE_PREFIX, parent_ident)))]
        return lis
示例#3
0
文件: render.py 项目: LeResKP/xmltool
    def text_element_to_html(self, obj, attrs, value):

        ident = prefixes_to_str(obj.prefixes_no_cache + ['_contenteditable'])

        div_attrs = [
            ('class', 'contenteditable'),
            ('class', 'form-control'),
            ('class', obj.tagname),
            ('contenteditable', 'true'),
            ('spellcheck', 'false'),
            ('id', ident),
        ]
        if self.extra_div_attrs_func:
            div_attrs += self.extra_div_attrs_func(obj)

        return (
            u'<textarea{attrs}>{value}'
            u'</textarea>'
            u'<div{divattrs}>{htmlvalue}</div>'
        ).format(
            attrs=attrs_to_str([('class', 'form-control'),
                                ('class', 'hidden')] + attrs),
            value=value,
            htmlvalue=self.cleanup_value(value),
            divattrs=attrs_to_str(div_attrs),
        )
示例#4
0
    def to_jstree_dict(self):
        data = self._get_jstree_data()
        children = []

        for o in self._children_with_required:
            v = o.to_jstree_dict()
            o._delete_auto_added()
            if v:
                if isinstance(v, list):
                    children += v
                else:
                    children += [v]

        ident = prefixes_to_str(self.prefixes_no_cache)
        return {
            'text': data,
            # NOTE: we put the id on the a to let jstree making the unique id
            # on the node.
            'a_attr': {
                'id': TREE_PREFIX + ident,
            },
            'li_attr': self._get_jstree_attrs(),
            'children': children,
            'state': {
                # To be sure jstree will load the tree recursively
                'opened': True,
            }
        }
示例#5
0
 def _get_html_add_button(self, css_class=None):
     ident = prefixes_to_str(self.prefixes_no_cache)
     css_classes = ['btn-add btn-add-%s' % self.tagname]
     if css_class:
         css_classes += [css_class]
     return '<a class="%s" data-elt-id="%s">Add %s</a>' % (
         ' '.join(css_classes), ident, self.tagname)
示例#6
0
    def to_jstree_dict(self):
        data = self._get_jstree_data()
        children = []

        for o in self._children_with_required:
            v = o.to_jstree_dict()
            o._delete_auto_added()
            if v:
                if isinstance(v, list):
                    children += v
                else:
                    children += [v]

        ident = prefixes_to_str(self.prefixes_no_cache)
        return {
            'text': data,
            # NOTE: we put the id on the a to let jstree making the unique id
            # on the node.
            'a_attr': {
                'id': TREE_PREFIX + ident,
            },
            'li_attr': self._get_jstree_attrs(),
            'children': children,
            'state': {
                # To be sure jstree will load the tree recursively
                'opened': True,
            }
        }
示例#7
0
 def _get_jstree_attrs(self):
     parent_ident = prefixes_to_str(self._parent_obj.prefixes_no_cache)
     css_class = TREE_PREFIX + parent_ident
     # TODO: see if we really need this
     # index = self._parent_obj.index(self)
     # css_class += ' ' + TREE_PREFIX + parent_ident + ':%s' % index
     return {
         'class': '%s %s' % (css_class, self.tagname),
     }
示例#8
0
 def _get_html_add_button(self, css_class=None):
     ident = prefixes_to_str(self.prefixes_no_cache)
     css_classes = ['btn-add btn-add-%s' % self.tagname]
     if css_class:
         css_classes += [css_class]
     return '<a class="%s" data-elt-id="%s">Add %s</a>' % (
         ' '.join(css_classes),
         ident,
         self.tagname)
示例#9
0
 def _get_jstree_attrs(self):
     parent_ident = prefixes_to_str(self._parent_obj.prefixes_no_cache)
     css_class = TREE_PREFIX + parent_ident
     # TODO: see if we really need this
     # index = self._parent_obj.index(self)
     # css_class += ' ' + TREE_PREFIX + parent_ident + ':%s' % index
     return {
         'class': '%s %s' % (css_class, self.tagname),
     }
示例#10
0
    def _comment_to_html(self):
        ident = prefixes_to_str(self.prefixes_no_cache + ['_comment'])

        if not self.comment:
            return (u'<a data-comment-name="%s" class="btn-comment" '
                    u'title="Add comment"></a>') % ident
        else:
            return (u'<a data-comment-name="{name}" '
                    u'class="btn-comment has-comment" title="{comment}"></a>'
                    u'<textarea class="_comment" name="{name}">{comment}'
                    '</textarea>').format(name=ident, comment=self.comment)
示例#11
0
    def _get_html_attrs(self, rows):
        """Get the HTML attributes to put on the textarea.

        :return: List of tuple like [('name', 'myname'), ...]
        """
        name = prefixes_to_str(self.prefixes_no_cache + ['_value'])
        attrs = [
            ('name', name),
            ('rows', rows),
            ('class', self.tagname),
        ]
        return attrs
示例#12
0
    def _get_html_attrs(self, rows):
        """Get the HTML attributes to put on the textarea.

        :return: List of tuple like [('name', 'myname'), ...]
        """
        name = prefixes_to_str(self.prefixes_no_cache + ['_value'])
        attrs = [
            ('name', name),
            ('rows', rows),
            ('class', self.tagname),
        ]
        return attrs
示例#13
0
    def _get_html_add_button(self, index):
        assert (index is not None)
        css_classes = [
            'btn-add btn-add-%s btn-list' % (self._children_class.tagname)
        ]

        ident = prefixes_to_str(self.prefixes_no_cache +
                                [str(index), self._children_class.tagname])
        button = ('<a class="%s" '
                  'data-elt-id="%s">New %s</a>') % (' '.join(
                      css_classes), ident, self._children_class.tagname)
        return button
示例#14
0
    def _get_html_add_button(self, index):
        assert (index is not None)
        button = '<select class="btn-add btn-list">'
        options = '/'.join([e.tagname for e in self._choice_classes])
        button += '<option>New %s</option>' % options

        ident = prefixes_to_str(self.prefixes_no_cache + [str(index)])

        for e in self._choice_classes:
            button += (
                '<option class="xt-option-%s" value="%s:%s">%s</option>' %
                (e.tagname, ident, e.tagname, e.tagname))
        button += '</select>'
        return button
示例#15
0
    def _get_html_add_button(self, index):
        assert(index is not None)
        css_classes = ['btn-add btn-add-%s btn-list' % (
            self._children_class.tagname
        )]

        ident = prefixes_to_str(self.prefixes_no_cache + [
            str(index), self._children_class.tagname])
        button = ('<a class="%s" '
                  'data-elt-id="%s">New %s</a>') % (
                      ' '.join(css_classes),
                      ident,
                      self._children_class.tagname)
        return button
示例#16
0
    def _attributes_to_html(self):
        if not self.attributes:
            return ''

        ident = prefixes_to_str(self.prefixes_no_cache)

        html = []
        for k, v in self.attributes.items():
            html += ['<a name="%s=%s"></a>' % (k, v)]
            html += ['<input value="%s" name="%s" id="%s" class="_attrs" />' % (
                v,
                '%s:_attrs:%s' % (ident, k),
                '%s:_attrs:%s' % (ident, k),
            )]
        return ''.join(html)
示例#17
0
    def _attributes_to_html(self):
        if not self.attributes:
            return ''

        ident = prefixes_to_str(self.prefixes_no_cache)

        html = []
        for k, v in self.attributes.items():
            html += ['<a name="%s=%s"></a>' % (k, v)]
            html += [
                '<input value="%s" name="%s" id="%s" class="_attrs" />' % (
                    v,
                    '%s:_attrs:%s' % (ident, k),
                    '%s:_attrs:%s' % (ident, k),
                )
            ]
        return ''.join(html)
示例#18
0
    def _comment_to_html(self):
        ident = prefixes_to_str(self.prefixes_no_cache + ['_comment'])

        if not self.comment:
            return (
                u'<a data-comment-name="%s" class="btn-comment" '
                u'title="Add comment"></a>') % ident
        else:
            return (
                u'<a data-comment-name="{name}" '
                u'class="btn-comment has-comment" title="{comment}"></a>'
                u'<textarea class="_comment" name="{name}">{comment}'
                '</textarea>'
            ).format(
                name=ident,
                comment=self.comment
            )
示例#19
0
    def _get_html_add_button(self, index):
        assert(index is not None)
        button = '<select class="btn-add btn-list">'
        options = '/'.join([e.tagname for e in self._choice_classes])
        button += '<option>New %s</option>' % options

        ident = prefixes_to_str(self.prefixes_no_cache + [
            str(index)])

        for e in self._choice_classes:
            button += (
                '<option class="xt-option-%s" value="%s:%s">%s</option>' % (
                    e.tagname,
                    ident,
                    e.tagname,
                    e.tagname))
        button += '</select>'
        return button
示例#20
0
    def _get_html_add_button(self, css_class=None):
        """
        ..note:: index is not used here since we never have list of this
        element.
        """
        css_classes = ['btn-add']
        if css_class:
            css_classes += [css_class]

        ident = prefixes_to_str(self.prefixes_no_cache)
        button = '<select class="%s">' % ' '.join(css_classes)
        button += '<option>New %s</option>' % '/'.join(
            [e.tagname for e in self._choice_classes])
        for e in self._choice_classes:
            button += (
                '<option class="xt-option-%s" value="%s:%s">%s</option>' %
                (e.tagname, ident, e.tagname, e.tagname))
        button += '</select>'
        return button
示例#21
0
    def to_html(self):
        renderer = self.get_html_renderer()
        add_button = ''
        if self._add_html_add_button():
            add_button = self._get_html_add_button(css_class='hidden')

        delete_button = ''

        if self._add_html_delete_button():
            delete_button = self._get_html_delete_button()

        value = self.text or ''
        cnt = value.count('\n')
        if cnt:
            cnt += 1
        rows = max(cnt, 1)
        attrs = self._get_html_attrs(rows)
        render = self.get_html_renderer()
        textarea = render.text_element_to_html(self, attrs, value)

        comment = ''
        if renderer.add_comment():
            comment = self._comment_to_html()
        ident = prefixes_to_str(self.prefixes_no_cache)
        return (
            u'<div id="{ident}" class="xt-container-{label}"><label>{label}</label>'
            u'<span class="btn-external-editor" '
            u'ng-click="externalEditor(this)"></span>'
            u'{add_button}'
            u'{delete_button}'
            u'{comment}'
            u'{xmlattrs}'
            u'{textarea}'
            u'</div>').format(
            ident=ident,
            label=self.tagname,
            add_button=add_button,
            delete_button=delete_button,
            comment=comment,
            textarea=textarea,
            xmlattrs=self._attributes_to_html(),
        )
示例#22
0
    def to_html(self):
        renderer = self.get_html_renderer()
        add_button = ''
        if self._add_html_add_button():
            add_button = self._get_html_add_button(css_class='hidden')

        delete_button = ''

        if self._add_html_delete_button():
            delete_button = self._get_html_delete_button()

        value = self.text or ''
        cnt = value.count('\n')
        if cnt:
            cnt += 1
        rows = max(cnt, 1)
        attrs = self._get_html_attrs(rows)
        render = self.get_html_renderer()
        textarea = render.text_element_to_html(self, attrs, value)

        comment = ''
        if renderer.add_comment():
            comment = self._comment_to_html()
        ident = prefixes_to_str(self.prefixes_no_cache)
        return (
            u'<div id="{ident}" class="xt-container-{label}"><label>{label}</label>'
            u'<span class="btn-external-editor" '
            u'ng-click="externalEditor(this)"></span>'
            u'{add_button}'
            u'{delete_button}'
            u'{comment}'
            u'{xmlattrs}'
            u'{textarea}'
            u'</div>').format(
                ident=ident,
                label=self.tagname,
                add_button=add_button,
                delete_button=delete_button,
                comment=comment,
                textarea=textarea,
                xmlattrs=self._attributes_to_html(),
            )
示例#23
0
    def get_previous_js_selectors(self):
        lis = []
        index = self._parent_obj.index(self)
        if index > 0:
            index -= 1
            tagnames = [self.tagname]
            if isinstance(self._parent_obj, ChoiceListElement):
                # Since we don't know which element is the previous we add all
                # the possibilities
                tagnames = [
                    c.tagname for c in self._parent_obj._choice_classes
                ]
            for tagname in tagnames:
                parent_ident = prefixes_to_str(
                    self._parent_obj.prefixes_no_cache + [str(index), tagname])
                lis += [('after',
                         escape_attr('#%s%s' % (TREE_PREFIX, parent_ident)))]
            return lis

        return self._parent_obj.get_previous_js_selectors()
示例#24
0
    def _get_html_add_button(self, css_class=None):
        """
        ..note:: index is not used here since we never have list of this
        element.
        """
        css_classes = ['btn-add']
        if css_class:
            css_classes += [css_class]

        ident = prefixes_to_str(self.prefixes_no_cache)
        button = '<select class="%s">' % ' '.join(css_classes)
        button += '<option>New %s</option>' % '/'.join([e.tagname for e in self._choice_classes])
        for e in self._choice_classes:
            button += (
                '<option class="xt-option-%s" value="%s:%s">%s</option>' % (
                    e.tagname,
                    ident,
                    e.tagname,
                    e.tagname))
        button += '</select>'
        return button
示例#25
0
    def to_html(self):
        renderer = self.get_html_renderer()
        sub_html = [self._attributes_to_html()]

        for obj in self._full_children:
            tmp = obj._to_html()
            obj._delete_auto_added()
            if tmp:
                sub_html += [tmp]

        legend = self.tagname

        ident = prefixes_to_str(self.prefixes_no_cache)
        if self._parent_obj:
            # The root element is not deletable
            if self._add_html_add_button():
                legend += self._get_html_add_button(css_class='hidden')

            if self._add_html_delete_button():
                legend += self._get_html_delete_button()

        if renderer.add_comment():
            legend += self._comment_to_html()

        html = [(
            u'<div class="panel panel-default {css_class}" id="{ident}">'
            u'<div class="panel-heading">'
            u'<span data-toggle="collapse" '
            u'href="#collapse-{escaped_id}">{legend}</span>'
            u'</div>'
            u'<div class="panel-body panel-collapse collapse in" '
            u'id="collapse-{ident}">').format(
                css_class=self.tagname,
                ident=ident,
                legend=legend,
                escaped_id=escape_attr(ident),
            )]
        html.extend(sub_html)
        html += ['</div></div>']
        return ''.join(html)
示例#26
0
    def to_html(self):
        renderer = self.get_html_renderer()
        sub_html = [self._attributes_to_html()]

        for obj in self._full_children:
            tmp = obj._to_html()
            obj._delete_auto_added()
            if tmp:
                sub_html += [tmp]

        legend = self.tagname

        ident = prefixes_to_str(self.prefixes_no_cache)
        if self._parent_obj:
            # The root element is not deletable
            if self._add_html_add_button():
                legend += self._get_html_add_button(css_class='hidden')

            if self._add_html_delete_button():
                legend += self._get_html_delete_button()

        if renderer.add_comment():
            legend += self._comment_to_html()

        html = [(u'<div class="panel panel-default {css_class}" id="{ident}">'
                 u'<div class="panel-heading">'
                 u'<span data-toggle="collapse" '
                 u'href="#collapse-{escaped_id}">{legend}</span>'
                 u'</div>'
                 u'<div class="panel-body panel-collapse collapse in" '
                 u'id="collapse-{ident}">').format(
                     css_class=self.tagname,
                     ident=ident,
                     legend=legend,
                     escaped_id=escape_attr(ident),
                 )]
        html.extend(sub_html)
        html += ['</div></div>']
        return ''.join(html)
示例#27
0
    def get_previous_js_selectors(self):
        lis = []
        index = self._parent_obj.index(self)
        if index > 0:
            index -= 1
            tagnames = [self.tagname]
            if isinstance(self._parent_obj, ChoiceListElement):
                # Since we don't know which element is the previous we add all
                # the possibilities
                tagnames = [c.tagname
                            for c in self._parent_obj._choice_classes]
            for tagname in tagnames:
                parent_ident = prefixes_to_str(
                    self._parent_obj.prefixes_no_cache +
                    [str(index), tagname])
                lis += [
                    ('after', escape_attr('#%s%s' % (
                        TREE_PREFIX,
                        parent_ident
                    )))]
            return lis

        return self._parent_obj.get_previous_js_selectors()
示例#28
0
    def text_element_to_html(self, obj, attrs, value):

        ident = prefixes_to_str(obj.prefixes_no_cache + ['_contenteditable'])

        div_attrs = [
            ('class', 'contenteditable'),
            ('class', 'form-control'),
            ('class', obj.tagname),
            ('contenteditable', 'true'),
            ('spellcheck', 'false'),
            ('id', ident),
        ]
        if self.extra_div_attrs_func:
            div_attrs += self.extra_div_attrs_func(obj)

        return (u'<textarea{attrs}>{value}'
                u'</textarea>'
                u'<div{divattrs}>{htmlvalue}</div>').format(
                    attrs=attrs_to_str([('class', 'form-control'),
                                        ('class', 'hidden')] + attrs),
                    value=value,
                    htmlvalue=self.cleanup_value(value),
                    divattrs=attrs_to_str(div_attrs),
                )
示例#29
0
 def _prefix_str(self):
     return prefixes_to_str(self.prefixes_no_cache)
示例#30
0
 def _get_jstree_attrs(self):
     ident = prefixes_to_str(self.prefixes_no_cache)
     return {
         'class': '%s %s' % (TREE_PREFIX + ident, self.tagname),
     }
示例#31
0
 def _get_html_delete_button(self):
     ident = prefixes_to_str(self.prefixes_no_cache)
     return ('<a class="btn-delete btn-list" '
             'data-target="#%s" title="Delete"></a>') % ident
示例#32
0
 def _get_html_delete_button(self):
     ident = prefixes_to_str(self.prefixes_no_cache)
     return ('<a class="btn-delete btn-list" '
             'data-target="#%s" title="Delete"></a>') % ident
示例#33
0
 def _prefix_str(self):
     return prefixes_to_str(self.prefixes_no_cache)
示例#34
0
 def _get_jstree_attrs(self):
     ident = prefixes_to_str(self.prefixes_no_cache)
     return {
         'class': '%s %s' % (TREE_PREFIX + ident, self.tagname),
     }