Commits

Luke Plant  committed 7936ba8

Implemented method for restricting styles to certain elements

Schema change - need to do ./manage reset semanticeditor

  • Participants
  • Parent commits 4c60d2a

Comments (0)

Files changed (4)

File semanticeditor/media/semanticeditor/javascript/wymeditor/plugins/semantic/wymeditor.semantic.js

 		help = "(No help available)";
 	    }
 	    help = "<h1>" + escapeHtml(item.verbose_name) + "</h1>" + help;
+            help = help + '<br/><hr/><p>Can be used on these elements:</p><p>' + item.allowed_elements.join(' ') + '</p>';
 	    // Assign an id, because orbitaltooltip
             // doesn't work without it.
 	    $(this).attr('id', 'id_optsbox_label_' + i);
     // Currently selected heading?
     var headingIndex = self.get_heading_index();
     if (headingIndex == null) return;
-    var sect_id = this.active_heading_list[headingIndex].sect_id;
+    var sect = this.active_heading_list[headingIndex];
+    var sect_id = sect.sect_id;
     var styles = this.presentation_info[sect_id];
     if (styles == null) {
 	styles = new Array();
 	function(i, input) {
 	    // Set state
 	    var val = input.value;
+            if (jQuery.inArray(sect.tag.toLowerCase(), self.available_styles[i].allowed_elements) != -1) {
+                input.disabled = false;
+            } else {
+                input.disabled = true;
+            }
 	    jQuery.each(styles,
-		function (i, style) {
+		function (j, style) {
 		    if (flattenPresStyle(style) == val) {
 			input.checked = true;
 		    }
 				    self.headingscontrol.get(0).focus();
 			 });
 	});
-    this.optsbox.find("input").removeAttr("disabled");
 };
 
 PresentationControls.prototype.show_preview = function() {
 PresentationControls.prototype.insert_row_command = function(arr, i, sect_id) {
     arr.splice(i, 0, {level:1,
                       name:"New row",
-                      tag:"newrow",
+                      tag:"row",
                       sect_id:"newrow_" + sect_id
                      });
 };
 PresentationControls.prototype.insert_column_command = function(arr, i, sect_id) {
     arr.splice(i, 0, {level:1,
                       name:"New column",
-                      tag:"newcol",
+                      tag:"column",
                       sect_id:"newcol_" + sect_id
                      });
 };
     if (this.stored_headings.length > 0) {
         // If after this process we don't have newrow and newcol commands
         // at the beginning of the heading list, we add them.
-        if (this.stored_headings[0].tag != 'newrow') {
+        if (this.stored_headings[0].tag != 'row') {
             this.insert_row_command(this.stored_headings, 0, first_sect_id);
         }
-        if (this.stored_headings[1].tag != 'newcol') {
+        if (this.stored_headings[1].tag != 'column') {
             this.insert_column_command(this.stored_headings, 1, first_sect_id);
         }
     }
 		    var spaces = (new Array((item.level - 1)*3)).join("&nbsp;");
                     var caption = "";
                     var tag = item.tag.toLowerCase();
-                    if (tag == 'newrow' || tag == 'newcol') {
+                    if (tag == 'row' || tag == 'column') {
                         caption = "[" + escapeHtml(item.name) + "]";
                     } else {
                         caption = tag + ": " + escapeHtml(item.name);

File semanticeditor/models.py

                                    "when the user hovers over the name in the "
                                    "list of styles.  The data will be interpreted "
                                    "as raw HTML, so it can include an example.")
+    allowed_elements = models.CharField("Allowed HTML elements", max_length=255,
+                                        help_text="A space separated list of HTML "
+                                        "element names.  Use 'row' or 'column' to indicate "
+                                        "it can be applied to layout rows or columns ",
+                                        default="h1 h2 h3 h4 h5 h6 p blockquote ul li row column")
 
     def __unicode__(self):
         return self.verbose_name

File semanticeditor/utils/presentation.py

     """
     Encapsulates a piece of presentation information.
     """
-    def __init__(self, prestype=None, name=None, verbose_name="", description=""):
+    def __init__(self, prestype=None, name=None, verbose_name="", description="", allowed_elements=None):
         self.prestype = prestype
         self.name = name
-        # The verbose_name and description are additional pieces of
-        # information that are only needed when the client is
-        # requesting a list of styles.  In other sitations these
-        # objects may not have these attributes filled in.
+        # verbose_name, description and allowed_elements are additional pieces
+        # of information that are only needed when the client is requesting a
+        # list of styles.  In other sitations these objects may not have these
+        # attributes filled in.
         self.verbose_name = verbose_name
         self.description = description
+        if allowed_elements is None:
+            allowed_elements = []
+        self.allowed_elements = allowed_elements
 
     def __eq__(self, other):
         return self.prestype == other.prestype and self.name == other.name
     def __repr__(self):
         return "PresentationInfo(prestype=\"%s\", name=\"%s\")" % (self.prestype, self.name)
 
-def PresentationClass(name, verbose_name="", description=""):
+def PresentationClass(name, verbose_name="", description="", allowed_elements=None):
     """
     Shortcut for creating CSS classes
     """
     return PresentationInfo(prestype="class",  name=name,
-                            verbose_name=verbose_name, description=description)
+                            verbose_name=verbose_name, description=description,
+                            allowed_elements=allowed_elements)
 
 def PresentationCommand(name, verbose_name="", description=""):
     """
 need to apply a 'New row' command to that section, creating a row with
 just one column in it.</p>
 
-""")
+"""
+                             )
 
 NEWCOL = PresentationCommand('newcol',
                              verbose_name = "New column",

File semanticeditor/views.py

 @json_view
 def retrieve_styles(request):
     retval = [PresentationClass(c.name,
-                              verbose_name=c.verbose_name,
-                              description=c.description)
-            for c in CssClass.objects.all().order_by('verbose_name')]
+                                verbose_name=c.verbose_name,
+                                description=c.description,
+                                allowed_elements=c.allowed_elements.lower().split(' '))
+              for c in CssClass.objects.all().order_by('verbose_name')]
     return success(map(PI_to_dict,retval))
 
 @json_view