Beispiel #1
0
            /// <summary>
            /// Run a deletion event.
            /// </summary>
            /// <param name="forward"></param>
            public override void run(bool forward)
            {
                var workspace = Workspace.getById(this.workspaceId);

                if (forward)
                {
                    foreach (var id in this.ids)
                    {
                        var block = (BlockSvg)workspace.getBlockById(id);
                        if (block != null)
                        {
                            block.dispose(false, false);
                        }
                        else if (id == this.blockId)
                        {
                            // Only complain about root-level block.
                            Console.WriteLine("Can't delete non-existant block: " + id);
                        }
                    }
                }
                else
                {
                    var xml = goog.dom.createDom("xml");
                    xml.AppendChild(this.oldXml);
                    Xml.domToWorkspace(xml, workspace);
                }
            }
Beispiel #2
0
        /**
         * Procedure calls cannot exist without the corresponding procedure
         * definition.  Enforce this link whenever an event is fired.
         * @this Blockly.Block
         */
        protected override void onchange(Events.Abstract e)
        {
            if (this.workspace == null || this.workspace.isFlyout)
            {
                // Block is deleted or is in a flyout.
                return;
            }
            if (e.type == Events.CREATE &&
                Array.IndexOf(((Events.Create)e).ids, this.id) != -1)
            {
                // Look for the case where a procedure call was created (usually through
                // paste) and there is no matching definition.  In this case, create
                // an empty definition block with the correct signature.
                var name = this.getProcedureCall();
                var def  = Core.Procedures.getDefinition(name, this.workspace);
                if (def != null && (def.type != this.defType_ ||
                                    JSON.Stringify(def.arguments_.ToArray()) != JSON.Stringify(this.arguments_)))
                {
                    // The signatures don't match.
                    def = null;
                }
                if (def == null)
                {
                    Events.setGroup(e.group);

                    /**
                     * Create matching definition block.
                     * <xml>
                     *   <block type="procedures_defreturn" x="10" y="20">
                     *     <mutation name="test">
                     *       <arg name="x"></arg>
                     *     </mutation>
                     *     <field name="NAME">test</field>
                     *   </block>
                     * </xml>
                     */
                    var xml   = goog.dom.createDom("xml");
                    var block = goog.dom.createDom("block");
                    block.SetAttribute("type", this.defType_);
                    var xy = this.getRelativeToSurfaceXY();
                    var x  = xy.x + Core.SNAP_RADIUS * (this.RTL ? -1 : 1);
                    var y  = xy.y + Core.SNAP_RADIUS * 2;
                    block.SetAttribute("x", x.ToString());
                    block.SetAttribute("y", y.ToString());
                    var mutation = this.mutationToDom();
                    block.AppendChild(mutation);
                    var field = goog.dom.createDom("field");
                    field.SetAttribute("name", "NAME");
                    field.AppendChild(Document.CreateTextNode(this.getProcedureCall()));
                    block.AppendChild(field);
                    xml.AppendChild(block);
                    Xml.domToWorkspace(xml, this.workspace);
                    Events.setGroup(false);
                }
            }
            else if (e.type == Events.DELETE)
            {
                // Look for the case where a procedure definition has been deleted,
                // leaving this block (a procedure call) orphaned.  In this case, delete
                // the orphan.
                var name = this.getProcedureCall();
                var def  = Core.Procedures.getDefinition(name, this.workspace);
                if (def == null)
                {
                    Events.setGroup(e.group);
                    this.dispose(true, false);
                    Events.setGroup(false);
                }
            }
        }