cmsplugin-news / cmsplugin_news / widgets / tinymce_widget.py

from tinymce.widgets import TinyMCE, get_language_config
from django.conf import settings
from django.utils.translation import get_language
from django.template.loader import render_to_string
from django.utils.safestring import mark_safe
from os.path import join
from django.utils.encoding import smart_unicode
import tinymce.settings
from django.utils import simplejson
from django.template.defaultfilters import escape
from django.forms.widgets import flatatt
import cms.plugins.text.settings

class TinyMCEEditor(TinyMCE):
    
    def __init__(self, installed_plugins=None,  **kwargs):
        super(TinyMCEEditor, self).__init__(**kwargs)
        self.installed_plugins = installed_plugins
        
    def render_additions(self, name, value, attrs=None):
        language = get_language()
        context = {
            'name': name,
            'language': language,
            'CMS_MEDIA_URL': settings.CMS_MEDIA_URL,
            'installed_plugins': self.installed_plugins,
        }
        return mark_safe(render_to_string(
            'cmsplugin_news/widgets/tinymce.html', context))
        
    def _media(self):
        media = super(TinyMCEEditor, self)._media()
        media.add_js([join(settings.CMS_MEDIA_URL, path) for path in (
                      'js/lib/jquery.js',
                      )])
        media.add_css({"all":[join(settings.CMS_MEDIA_URL, path) for path in ('css/jquery/cupertino/jquery-ui.css',
                                                                     'css/tinymce_toolbar.css')]})
        
        return media
    
    
    media = property(_media)
    
    def render(self, name, value, attrs=None):
        if value is None: value = ''
        value = smart_unicode(value)
        final_attrs = self.build_attrs(attrs)
        final_attrs['name'] = name
        assert 'id' in final_attrs, "TinyMCE widget attributes must contain 'id'"
        mce_config = cms.plugins.text.settings.TINYMCE_CONFIG.copy()
        mce_config.update(get_language_config(self.content_language))
        if tinymce.settings.USE_FILEBROWSER:
            mce_config['file_browser_callback'] = "djangoFileBrowser"
        mce_config.update(self.mce_attrs)
        mce_config['mode'] = 'exact'
        mce_config['elements'] = final_attrs['id']
        mce_config['strict_loading_mode'] = 1
        """
        plugins = mce_config.get("plugins", "")
        if len(plugins):
            plugins += ","
        plugins += "-cmsplugins"
        mce_config['plugins'] = plugins
        adv2 = mce_config.get('theme_advanced_buttons1', "")
        if len(adv2):
            adv2 = "," + adv2
        adv2 = "cmsplugins,cmspluginsedit" + adv2
        mce_config['theme_advanced_buttons1'] = adv2
        """
        json = simplejson.dumps(mce_config)
        
        html = [u'<textarea%s>%s</textarea>' % (flatatt(final_attrs), escape(value))]
        if tinymce.settings.USE_COMPRESSOR:
            compressor_config = {
                'plugins': mce_config.get('plugins', ''),
                'themes': mce_config.get('theme', 'advanced'),
                'languages': mce_config.get('language', ''),
                'diskcache': True,
                'debug': False,
            }
            c_json = simplejson.dumps(compressor_config)
            html.append(u'<script type="text/javascript">tinyMCE_GZ.init(%s);</script>' % (c_json))
        html.append(u'<script type="text/javascript">%s;\ntinyMCE.init(%s);</script>' % (self.render_additions(name, value, attrs), json))
        return mark_safe(u'\n'.join(html))
    
    
    
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.