rep()

in packages/miew/src/ComplexVisual.js [159:218]


  rep(index, rep) {
    // if index is missing then it is the current
    if (!rep && (index === undefined || index instanceof Object)) {
      rep = index;
      index = this.repCurrent();
    }

    // fail if out of bounds
    if (index < 0 || index > this._reprList.length) {
      logger.error(`Rep ${index} does not exist!`);
      return null;
    }

    // a special case of adding just after the end
    if (index === this._reprList.length) {
      const res = this.repAdd(rep);
      logger.warn(`Rep ${index} does not exist! New representation was created.`);
      return { desc: res.desc, index, status: 'created' };
    }

    // gather description
    const target = this._reprList[index];
    const desc = {
      selector: target.selectorString,
      mode: target.mode.identify(),
      colorer: target.colorer.identify(),
      material: target.materialPreset.id,
    };

    // modification is requested
    if (rep) {
      // modify
      const diff = target.change(
        rep,
        this._complex,
        lookupAndCreate(modes, rep.mode),
        lookupAndCreate(colorers, rep.colorer),
      );

      // something was changed
      if (!_.isEmpty(diff)) {
        target.needsRebuild = true;
        for (const key in diff) {
          if (diff.hasOwnProperty(key)) {
            desc[key] = diff[key];
            logger.debug(`rep[${index}].${key} changed to ${diff[key]}`);
          }
        }

        // safety trick: lower resolution for surface modes
        if (diff.mode && target.mode.isSurface
          && (settings.now.resolution === 'ultra' || settings.now.resolution === 'high')) {
          logger.report('Surface resolution was changed to "medium" to avoid hang-ups.');
          settings.set('resolution', 'medium');
        }
        return { desc, index, status: 'changed' };
      }
    }
    return { desc, index, status: '' };
  }