Commits

Carl Meyer  committed b2ce1b6

allow auto preview to be overridden per-widget or markup_editor template tag

  • Participants
  • Parent commits f5c1a65

Comments (0)

Files changed (4)

 media somewhere on the page using ``{{ form.media }}``, or the
 MarkItUpWidget will have no effect.
 
+MarkItUpWidget accepts three optional keyword arguments:
+``markitup_set`` and ``markitup_skin`` (see `Choosing a MarkItUp!
+button set and skin`_) and ``auto_preview`` (to override the value of
+the `MARKITUP_AUTO_PREVIEW`_ setting).
+
 To use the widget in the Django admin::
 
     from markitup.widgets import AdminMarkItUpWidget
 You can use ``markitup_editor`` on as many different textareas as you
 like.
 
+``markitup_editor`` accepts an optional second parameter, which can be
+either ``"auto_preview"`` or ``"no_auto_preview"`` to override the
+value of the `MARKITUP_AUTO_PREVIEW`_ setting.
+
 The actual HTML included by these templatetags is defined by the
 contents of the templates ``markitup/include_css.html``,
 ``markitup/include_js.html``, and ``markitup/editor.html``. You can

File markitup/templatetags/markitup_tags.py

     return register._markitup_context
 
 @register.inclusion_tag('markitup/editor.html')
-def markitup_editor(textarea_id):
+def markitup_editor(textarea_id, auto_preview=None):
+    if auto_preview is not None:
+        auto_preview = (auto_preview == 'auto_preview')
+    else:
+        auto_preview = settings.MARKITUP_AUTO_PREVIEW
     return {'textarea_id': textarea_id,
-            'AUTO_PREVIEW': settings.MARKITUP_AUTO_PREVIEW}
+            'AUTO_PREVIEW': auto_preview}

File markitup/widgets.py

 """
 widgets for django-markitup
 
-Time-stamp: <2010-01-06 11:46:37 carljm widgets.py>
+Time-stamp: <2010-01-06 12:31:06 carljm widgets.py>
 
 """
 from django import forms
     """
     def __init__(self, attrs=None,
                  markitup_set=None,
-                 markitup_skin=None):
+                 markitup_skin=None,
+                 auto_preview=None):
         self.miu_set = absolute_url(markitup_set or settings.MARKITUP_SET)
         self.miu_skin = absolute_url(markitup_skin or settings.MARKITUP_SKIN)
+        if auto_preview is None:
+            auto_preview = settings.MARKITUP_AUTO_PREVIEW
+        self.auto_preview = auto_preview
         super(MarkItUpWidget, self).__init__(attrs)
 
     def _media(self):
     def render(self, name, value, attrs=None):
         html = super(MarkItUpWidget, self).render(name, value, attrs)
 
-        if settings.MARKITUP_AUTO_PREVIEW:
+        if self.auto_preview:
             auto_preview = "$('a[title=\"Preview\"]').trigger('mouseup');"
         else: auto_preview = ''
 

File tests/tests.py

 
 
 class MIUTestCase(TestCase):
-    def assertIn(self, needle, haystack):
-        self.failUnless(needle in haystack,
-                        "'%s' not in '%s'" % (needle, haystack))
+    def assertIn(self, needle, haystack, reverse=False):
+        func = reverse and self.failIf or self.failUnless
+        descrip = reverse and 'in' or 'not in'
+        func(needle in haystack,
+             "'%s' %s '%s'" % (needle, descrip, haystack))
 
     def render(self, template_string, context_dict=None):
         """A shortcut for testing template output."""
                       
 class RenderTests(MIUTestCase):
     look_for = '$("#my_id").markItUp(mySettings);'
+    auto_preview_override = True
     
     def test_widget_render(self):
         widget = MarkItUpWidget()
         self.assertIn(self.look_for,
                       self.render(template))
 
-class AutoPreviewRenderTests(RenderTests):
+    def test_per_widget_auto_preview_override(self):
+        widget = MarkItUpWidget(auto_preview=self.auto_preview_override)
+        self.assertIn(AutoPreviewSettingTests.look_for,
+                      widget.render('name', 'value', {'id': 'my_id'}),
+                      reverse=not self.auto_preview_override)
+
+    def test_per_ttag_auto_preview_override(self):
+        if self.auto_preview_override:
+            arg = "auto_preview"
+        else:
+            arg = "no_auto_preview"
+        template = """{%% load markitup_tags %%}{%% markitup_editor "my_id" "%s" %%}""" % (arg,)
+        self.assertIn(AutoPreviewSettingTests.look_for,
+                      self.render(template),
+                      reverse=not self.auto_preview_override)
+
+
+class AutoPreviewSettingTests(RenderTests):
     look_for = "$('a[title=\"Preview\"]').trigger('mouseup');"
+    auto_preview_override = False
     
     def setUp(self):
         self._old_auto = settings.MARKITUP_AUTO_PREVIEW