File:  [LON-CAPA] / modules / damieng / graphical_editor / loncapa_daxe / web / nodes / organic_structure.dart
Revision 1.2: download - view: text, annotated - select for diffs
Tue Feb 28 20:40:59 2017 UTC (7 years, 3 months ago) by damieng
Branches: MAIN
CVS tags: HEAD
updates following changes in Daxe Config API

/*
  This file is part of LONCAPA-Daxe.

  LONCAPA-Daxe is free software: you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation, either version 3 of the License, or
  (at your option) any later version.

  LONCAPA-Daxe is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with Daxe.  If not, see <http://www.gnu.org/licenses/>.
*/

part of loncapa_daxe;

/**
 * Display for organicstructure.
 * Jaxe display type: 'organicstructure'.
 */
class OrganicStructure extends LCDBlock {
  
  OrganicStructure.fromRef(x.Element elementRef) : super.fromRef(elementRef);
  
  OrganicStructure.fromNode(x.Node node, DaxeNode parent) : super.fromNode(node, parent);
  
  @override
  h.TableRowElement attributeHTML(x.Element refAttr) {
    // add another column, use it to add an edit button for certain fields
    // NOTE: this code is similar to the one used in OrganicResponse
    h.TableRowElement tr = super.attributeHTML(refAttr);
    String name = doc.cfg.attributeName(refAttr);
    if (name == 'molecule') {
      h.TableCellElement td = tr.lastChild;
      h.ButtonElement bEdit = new h.ButtonElement();
      bEdit.attributes['type'] = 'button';
      bEdit.text = LCDStrings.get('edit_with_jsme');
      bEdit.onClick.listen((h.MouseEvent event) {
        String jmeString = attributeControls['molecule'].getValue();
        if (jmeString == '')
          jmeString = null;
        JSMEDialog dlg;
        dlg = new JSMEDialog(() {
          String jmeString = dlg.getJMEString();
          doc.doNewEdit(new UndoableEdit.changeAttribute(this, new DaxeAttr('molecule', jmeString)));
        }, jmeString:jmeString);
        dlg.show();
      });
      td.append(bEdit);
      tr.append(td);
    } else if (name == 'options' && (getAttribute('options') == null || !getAttribute('options').contains('\$'))) {
      h.TableCellElement td = tr.lastChild;
      td.remove();
      td = tr.lastChild;
      td.remove();
      td = new h.TableCellElement();
      td.colSpan = 2;
      td.classes.add('expand');
      List<String> optionList = parseOptions();
      List<String> optionNames = ['border', 'reaction'];
      for (String option in optionNames) {
        h.LabelElement label = new h.LabelElement();
        h.CheckboxInputElement cb = new h.CheckboxInputElement();
        cb.checked = optionList.contains(option);
        cb.onChange.listen((h.Event event) {
          List<String> newList = new List.from(parseOptions());
          if (newList.contains(option))
            newList.remove(option);
          else
            newList.add(option);
          String soptions;
          if (newList.length == 0)
            soptions = null;
          else
            soptions = newList.join(',');
          doc.doNewEdit(new UndoableEdit.changeAttribute(this,
            new DaxeAttr('options', soptions), updateDisplay:false));
        });
        label.append(cb);
        label.appendText(' ');
        label.appendText(LCDStrings.get("option_$option"));
        td.append(label);
        td.append(new h.BRElement());
      }
      tr.append(td);
      
    }
    return tr;
  }
  List<String> parseOptions() {
    String options = getAttribute('options');
    if (options == null || options.trim() == '')
      return new List<String>();
    List<String> list = options.split(',');
    for (int i=0; i<list.length; i++) {
      list[i] = list[i].trim();
    }
    return list;
  }
  
  @override
  void updateAttributes() {
    updateHTML(); // TODO: optimize for speed
  }
  
}

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>