Issue #631Opened December 12, 2017by z1lk1 reactions

How to refresh Classes view for active Component in the Style Manager

Question

I'm using a trait (type=select) to change the class of a component (a "column" component, so classes representing width 1-12). After I have removed previous col-* classes and added the newly selected one (via a function triggered by the select change), the Classes section of the Style Manager still has the old class. If I select a different component, then select the column again, it has the new, correct class. How do I force a refresh on the style manager without de/reselecting the component?

Perhaps I am adding/removing classes incorrectly, if the Style Manager view should update automatically. Is this standard Backbone stuff? If so, I'd love to be pointed in the direction of a concise Backbone primer.

Relevant component creation:

var comps = editor.DomComponents;
var defaultType = comps.getType('default');
var defaultModel = defaultType.model;
var defaultView = defaultType.view;

comps.addType('col', {
  model: defaultModel.extend({
    defaults: Object.assign({}, defaultModel.prototype.defaults, {
      'custom-name': 'Column',
      traits: [{
        type: 'select',
        options: /* map of classes */,
        label: 'Width',
        name: 'col-width',
        changeProp: 1
      }]
    }),
    init() {
      this.listenTo(this, 'change:col-width', this.changeColClass);
    },
    changeColClass() {
      var width = this.get('col-width');
      var classes = this.get('classes');

      // remove existing col class
      var col_classes = /* list of classes */
      classes.forEach(element => {
        if (col_classes.includes(element.id)) {
          classes.remove(element);
        }
      });

      // add new class to selector manager and component class list
      var class_name = 'col-' + width;
      var sm = this.sm.get('SelectorManager');
      var class_obj = sm.add(class_name)
      classes.add(class_obj);
    }
  }, {
    isComponent: function(el) { /* check if contains a col class */ }
  }),
  view: defaultView
});

Answers (2)

lock[bot]September 18, 20190 reactions

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

Related Questions and Answers

Continue research with similar issue discussions.

Paid Plugins That Match This Issue

Curated by issue keywords and label relevance to help you ship faster.

View all plugins

Loading paid plugin recommendations...

Browse Plugin Categories

Jump directly to plugin category pages on the marketplace.