|
14 | 14 | class MartorWidget(forms.Textarea): |
15 | 15 |
|
16 | 16 | def render(self, name, value, attrs=None, renderer=None, **kwargs): |
17 | | - if attrs is None: |
18 | | - attrs = {} |
19 | | - elif 'class' in attrs: |
| 17 | + # Make the settings the default attributes to pass |
| 18 | + attributes_to_pass = { |
| 19 | + 'data-enable-configs': MARTOR_ENABLE_CONFIGS, |
| 20 | + 'data-upload-url': MARTOR_UPLOAD_URL, |
| 21 | + 'data-markdownfy-url': MARTOR_MARKDOWNIFY_URL, |
| 22 | + 'data-search-users-url': MARTOR_SEARCH_USERS_URL, |
| 23 | + 'data-base-emoji-url': MARTOR_MARKDOWN_BASE_EMOJI_URL |
| 24 | + } |
| 25 | + |
| 26 | + # Make sure that the martor value is in the class attr passed in |
| 27 | + if 'class' in attrs: |
20 | 28 | attrs['class'] += ' martor' |
21 | 29 | else: |
22 | | - attrs.update({'class': 'martor'}) |
| 30 | + attrs['class'] = 'martor' |
| 31 | + |
| 32 | + # Update and overwrite with the attributes passed in |
| 33 | + attributes_to_pass.update(attrs) |
23 | 34 |
|
24 | | - attrs['data-enable-configs'] = MARTOR_ENABLE_CONFIGS |
25 | | - attrs['data-upload-url'] = MARTOR_UPLOAD_URL |
26 | | - attrs['data-markdownfy-url'] = MARTOR_MARKDOWNIFY_URL |
27 | | - attrs['data-search-users-url'] = MARTOR_SEARCH_USERS_URL |
28 | | - attrs['data-base-emoji-url'] = MARTOR_MARKDOWN_BASE_EMOJI_URL |
| 35 | + # Update and overwrite with any attributes that are on the widget |
| 36 | + # itself. This is also the only way we can push something in without |
| 37 | + # being part of the render chain. |
| 38 | + attributes_to_pass.update(self.attrs) |
29 | 39 |
|
30 | | - widget = super(MartorWidget, self).render(name, value, attrs) |
| 40 | + widget = super(MartorWidget, self).render(name, |
| 41 | + value, |
| 42 | + attributes_to_pass) |
31 | 43 |
|
32 | 44 | template = get_template('martor/editor.html') |
33 | 45 |
|
|
0 commit comments