Bernardo Cabezas Serra avatar Bernardo Cabezas Serra committed 4e6cf3e

Changes for django-cms v2.2 on wymeditor widget

Comments (0)

Files changed (2)

cmsplugin_advancednews/templates/cmsplugin_advancednews/widgets/wymeditor.html

 {% load i18n %}
 <script type="text/javascript">
+//<![CDATA]
 
 // Global var, for storing callbacks, see below.
 var editPluginPopupCallbacks = {};
 
 {% include "cms/plugins/widgets/widget_lib.js" %}
 
-
-
-$(document).ready(function(){
+jQuery(document).ready(function ($) {
+	// scroll to top
+	scrollTo(0, 0);
+	
+	// init wysiwyg
     $('#id_{{ name }}').wymeditor({
         lang: '{{ language }}',
         skin: 'django',
-        skinPath: "{{ CMS_MEDIA_URL }}js/wymeditor/skins/django/",
+        skinPath: "{{ STATIC_URL }}cms/js/wymeditor/skins/django/",
         updateSelector: 'input[type=submit],',
         updateEvent: 'click',
 		logoHtml: '',
 			{{ WYM_STYLESHEET }}
 		,
 		{% endif %}
+        postInit: function(wym) {
+			 //wym.resizable({handles: "s", maxHeight: 600});
+            //construct the insertLinkButton html
+            html = get_plugin_html()
+            //add the button to the tools box
+            jQuery(wym._box)
+            .find(wym._options.toolsSelector + wym._options.toolsListSelector)
+            .append(html);
+            // Enable the placeholderbridge plugin, to allow
+            // the placeholder controls to talk to editor
+            wym.placeholderbridge({'name': '{{ name }}'});
+            init_buttons("{{ name }}");
+        },
         //handle click event on dialog's submit button
         postInitDialog: function( wym, wdw ) {
      
         }
     });
+
+    /* onclick for 'Insert object' */
+	function init_buttons(placeholder){
+		$('span.insert-object').click(function(){
+			var select = $(this).parent().children("select");
+			var pluginvalue = select.attr('value');
+			var splits = window.location.href.split("?")[0].split("/");
+			var parent_id = Number(splits[splits.length - 2]);
+			var language = $('#id_language').attr('value');
+			
+			if (pluginvalue == "") {
+				alert("{% filter escapejs %}{% trans "Please select a plugin type." %}{% endfilter %}");
+				return;
+			}
+			
+			var texteditor = get_editor(placeholder);
+			if (texteditor == null || texteditor.insertText == null) {
+				alert("{% filter escapejs %}{% trans "Text editor does not support inserting objects." %}{% endfilter %}");
+				return;
+			}
+			// First create db instance using AJAX post back
+			add_plugin(pluginvalue, parent_id, language)
+			
+		}).css("cursor", "pointer").css("margin", "5px");
+		
+		/* onclick for 'Edit selected object' */
+		$('span.edit-object').click(function(){
+			var texteditor = get_editor(placeholder);
+			if (texteditor == null || texteditor.selectedObject == null) {
+				alert("{% filter escapejs %}{% trans "Text editor does not support editing objects." %}{% endfilter %}");
+				return;
+			}
+			var imgobj = texteditor.selectedObject();
+			if (imgobj == null) {
+				alert("{% filter escapejs %}{% trans "No object selected." %}{% endfilter %}");
+				return;
+			}
+			if (imgobj.id == null || imgobj.id.indexOf("plugin_obj_") != 0) {
+				alert("{% filter escapejs %}{% trans "Not a plugin object" %}{% endfilter %}");
+				return;
+			}
+			var plugin_id = imgobj.id.substr("plugin_obj_".length);
+			edit_plugin(plugin_id);
+		}).css("cursor", "pointer").css("margin","5px");
+	}
 });
-    </script>
+
+function get_plugin_html(){
+	html = '<li class="wym_tools_plugins">'
+        + '<select name="plugins">'
+        + '<option value="" selected="selected">{% filter escapejs %}{% trans "Available Plugins" %}{% endfilter %}</option>'{% for p in installed_plugins %}
+    	+ '<option value="{{ p.value }}">{{ p.name }}</option>'{% endfor %}
+        + '</select>'
+        + '<span class="insert-object addlink">{% filter escapejs %}{% trans "Insert plugin" %}{% endfilter %}</span>'
+        + '<span class="edit-object changelink">{% filter escapejs %}{% trans "Edit selected plugin" %}{% endfilter %}</span>'
+        + '</li>';
+return html;
+}
+//]]>
+</script>

cmsplugin_advancednews/widgets/wymeditor_widget.py

-from os.path import join
-
+from cms.plugins.text import settings as text_settings
+from cms.utils import cms_static_url
+from django.conf import settings
+from django.forms import Textarea
+from django.template.loader import render_to_string
 from django.utils.safestring import mark_safe
-from django.template.loader import render_to_string
-from django.forms import Textarea
-
+from django.utils.translation.trans_real import get_language
 from django.conf import settings
-from cms.plugins.text import settings as text_settings
-from django.utils.translation.trans_real import get_language
 
 class WYMEditor(Textarea):
     class Media:
-        js = [join(settings.CMS_MEDIA_URL, path) for path in (
-            'js/lib/jquery.js',
+        js = ['%sjs/jquery.min.js' % settings.ADMIN_MEDIA_PREFIX] + [cms_static_url(path) for path in (
+            'js/plugins/admincompat.js',
+            'js/libs/jquery.query.js',
+            'js/libs/jquery.ui.core.js',
+            'js/libs/jquery.ui.dialog.js',
             'wymeditor/jquery.wymeditor.js',
             'wymeditor/plugins/resizable/jquery.wymeditor.resizable.js',
+            'js/wymeditor.placeholdereditor.js',
+            'js/libs/jquery.ui.core.js',
+            'js/placeholder_editor_registry.js',
         )]
-              
+        css = {
+            'all': [cms_static_url(path) for path in (
+                        'css/jquery/cupertino/jquery-ui.css',
+                    )],
+        }
+
     def __init__(self, attrs=None, installed_plugins=None):
         """
         Create a widget for editing text + plugins.
             self.attrs.update(attrs)
         super(WYMEditor, self).__init__(attrs)
         self.installed_plugins = installed_plugins
-        
+
     def render_textarea(self, name, value, attrs=None):
         return super(WYMEditor, self).render(name, value, attrs)
 
         context = {
             'name': name,
             'language': language,
-            'CMS_MEDIA_URL': settings.CMS_MEDIA_URL,
+            'STATIC_URL': settings.STATIC_URL,
             'WYM_TOOLS': mark_safe(text_settings.WYM_TOOLS),
             'WYM_CONTAINERS': mark_safe(text_settings.WYM_CONTAINERS),
             'WYM_CLASSES': mark_safe(text_settings.WYM_CLASSES),
         }
         return mark_safe(render_to_string(
             'cmsplugin_advancednews/widgets/wymeditor.html', context))
+            #'cms/plugins/widgets/wymeditor.html', context))
 
     def render(self, name, value, attrs=None):
         return self.render_textarea(name, value, attrs) + \
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.