<commit_before>/****************************************************************************
 * Copyright (C) 2009-2011 GGA Software Services LLC
 *
 * This file may be distributed and/or modified under the terms of the
 * GNU Affero General Public License version 3 as published by the Free
 * Software Foundation and appearing in the file LICENSE.GPL included in
 * the packaging of this file.
 *
 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
 * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 ***************************************************************************/

if (typeof(ui) == 'undefined')
    ui = function () {};

//
// Undo/redo actions
//
ui.Action = function ()
{
    this.operations = new Array();
};

ui.Action.prototype.addOp = function(op) {
    this.operations.push(op);
    return op;
};

ui.Action.prototype.mergeWith = function (action)
{
    this.operations = this.operations.concat(action.operations);
    return this; //rbalabanov: let it be possible to chain actions in code easier
};

// Perform action and return inverted one
ui.Action.prototype.perform = function ()
{
    var action = new ui.Action();
    var idx = 0;

    this.operations.each(function (op) {
        action.addOp(op.perform(ui.editor));
        idx++;
    }, this);

    action.operations.reverse();
    return action;
};

ui.Action.prototype.isDummy = function ()
{
    return this.operations.detect(function(op) {
        return !op.isDummy(ui.editor); // TODO [RB] the condition is always true for ui.Action.Op* operations
    }, this) == null;
};

ui.Action.fromMultipleMove = function (lists, d)
{
    d = new util.Vec2(d);

    var action = new ui.Action();
    var i;

    var R = ui.render;
    var RS = R.ctab;
    var bondlist = [];
    var atomsToInvalidate = util.Set.empty();
    
    if (lists.atoms) {
        var atomSet = util.Set.fromList(lists.atoms);
        RS.bonds.each(function(bid, bond){
            if (util.Set.contains(atomSet, bond.b.begin) && util.Set.contains(atomSet, bond.b.end))
                bondlist.push(bid);
            else if (util.Set.contains(atomSet, bond.b.begin))
                util.Set.add(atomsToInvalidate, bond.b.begin);
            else if (util.Set.contains(atomSet, bond.b.end))
                util.Set.add(atomsToInvalidate, bond.b.end);
        }, this);
        for (i = 0; i < bondlist.length; ++i) {
            action.addOp(new ui.Action.OpBondMove(bondlist[i], d));
        }
        for (i = 0; i < lists.atoms.length; ++i) {
            var aid = lists.atoms[i];
            action.addOp(new ui.Action.OpAtomMove(aid, d, !util.Set.contains(atomsToInvalidate, aid)));
        }
    }    

    if (lists.rxnArrows)
        for (i = 0; i < lists.rxnArrows.length; ++i)
            action.addOp(new ui.Action.OpRxnArrowMove(lists.rxnArrows[i], d, true));

    if (lists.rxnPluses)
        for (i = 0; i < lists.rxnPluses.length; ++i)
            action.addOp(new ui.Action.OpRxnPlusMove(lists.rxnPluses[i], d, true));

    if (lists.sgroupData)
        for (i = 0; i < lists.sgroupData.length; ++i)
            action.addOp(new ui.Action.OpSGroupDataMove(lists.sgroupData[i], d));
    
    if (lists.chiralFlags)
        for (i = 0; i < lists.chiralFlags.length; ++i)
            action.addOp(new ui.Action.OpChiralFlagMove(d));

    return action.perform();
};

ui.Action.fromAtomsAttrs = function(ids, attrs)
{
    var action = new ui.Action();
    new Hash(attrs).each(function(attr) {
        (typeof(ids) == 'array' ? ids : [ids]).each(function(id) {
            if (attr.key == 'label' && attr.value != null) {
                action.addOp(new ui.Action.OpAtomAttr(id, 'atomList', null));
            }
            action.addOp(new ui.Action.OpAtomAttr(id, attr.key, attr.value));
        }, this);
    }, this);
    return action.perform();
};

ui.Action.fromBondAttrs = function (id, attrs, flip)
{
    var action = new ui.Action();
<commit_msg>fixed: KETCHER-334 (Incorrect Undo action for the created List/Not List)<commit_after>/****************************************************************************
 * Copyright (C) 2009-2011 GGA Software Services LLC
 *
 * This file may be distributed and/or modified under the terms of the
 * GNU Affero General Public License version 3 as published by the Free
 * Software Foundation and appearing in the file LICENSE.GPL included in
 * the packaging of this file.
 *
 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
 * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 ***************************************************************************/

if (typeof(ui) == 'undefined')
    ui = function () {};

//
// Undo/redo actions
//
ui.Action = function ()
{
    this.operations = new Array();
};

ui.Action.prototype.addOp = function(op) {
    this.operations.push(op);
    return op;
};

ui.Action.prototype.mergeWith = function (action)
{
    this.operations = this.operations.concat(action.operations);
    return this; //rbalabanov: let it be possible to chain actions in code easier
};

// Perform action and return inverted one
ui.Action.prototype.perform = function ()
{
    var action = new ui.Action();
    var idx = 0;

    this.operations.each(function (op) {
        action.addOp(op.perform(ui.editor));
        idx++;
    }, this);

    action.operations.reverse();
    return action;
};

ui.Action.prototype.isDummy = function ()
{
    return this.operations.detect(function(op) {
        return !op.isDummy(ui.editor); // TODO [RB] the condition is always true for ui.Action.Op* operations
    }, this) == null;
};

ui.Action.fromMultipleMove = function (lists, d)
{
    d = new util.Vec2(d);

    var action = new ui.Action();
    var i;

    var R = ui.render;
    var RS = R.ctab;
    var bondlist = [];
    var atomsToInvalidate = util.Set.empty();
    
    if (lists.atoms) {
        var atomSet = util.Set.fromList(lists.atoms);
        RS.bonds.each(function(bid, bond){
            if (util.Set.contains(atomSet, bond.b.begin) && util.Set.contains(atomSet, bond.b.end))
                bondlist.push(bid);
            else if (util.Set.contains(atomSet, bond.b.begin))
                util.Set.add(atomsToInvalidate, bond.b.begin);
            else if (util.Set.contains(atomSet, bond.b.end))
                util.Set.add(atomsToInvalidate, bond.b.end);
        }, this);
        for (i = 0; i < bondlist.length; ++i) {
            action.addOp(new ui.Action.OpBondMove(bondlist[i], d));
        }
        for (i = 0; i < lists.atoms.length; ++i) {
            var aid = lists.atoms[i];
            action.addOp(new ui.Action.OpAtomMove(aid, d, !util.Set.contains(atomsToInvalidate, aid)));
        }
    }    

    if (lists.rxnArrows)
        for (i = 0; i < lists.rxnArrows.length; ++i)
            action.addOp(new ui.Action.OpRxnArrowMove(lists.rxnArrows[i], d, true));

    if (lists.rxnPluses)
        for (i = 0; i < lists.rxnPluses.length; ++i)
            action.addOp(new ui.Action.OpRxnPlusMove(lists.rxnPluses[i], d, true));

    if (lists.sgroupData)
        for (i = 0; i < lists.sgroupData.length; ++i)
            action.addOp(new ui.Action.OpSGroupDataMove(lists.sgroupData[i], d));
    
    if (lists.chiralFlags)
        for (i = 0; i < lists.chiralFlags.length; ++i)
            action.addOp(new ui.Action.OpChiralFlagMove(d));

    return action.perform();
};

ui.Action.fromAtomsAttrs = function(ids, attrs)
{
    var action = new ui.Action();
    new Hash(attrs).each(function(attr) {
        (typeof(ids) == 'number' ? [ids] : ids).each(function(id) {
            if (attr.key == 'label' && attr.value != null) {
                action.addOp(new ui.Action.OpAtomAttr(id, 'atomList', null));
            }
            action.addOp(new ui.Action.OpAtomAttr(id, attr.key, attr.value));
        }, this);
    }, this);
    return action.perform();
};

ui.Action.fromBondAttrs = function (id, attrs, flip)
{
    var action = new ui.Action();

{'fraction_non_alphanumeric': 0.17402694610778444, 'fraction_numerical': 0.004615768463073852, 'mean_word_length': 2.704713493530499, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1248053', 'n_tokens_mistral': 2595, 'n_tokens_neox': 2519, 'n_words': 621}