'''
Copyright (c) 2017, Jairus Martin.

Distributed under the terms of the MIT License.

The full license is in the file COPYING.txt, distributed with this software.

Created on Apr 15, 2017

@author: jrm
'''
from atom.api import ForwardInstance, Enum
from enaml.core.declarative import Declarative, d_


class Block(Declarative):
    """ An object which dynamically insert's its children into another block's parent object.

    The 'Block' object is used to cleanly and easily insert it's children
    into the children of another object. The 'Object' instance assigned to the
    'block' property of the 'Block' will be parented with the parent of
    the 'Block'. Creating a 'Block' with no parent is a programming
    error.

    """

    #: The Block to which this blocks children should be inserted into
    block = d_(ForwardInstance(lambda: Block))

    #: If replace, replace all parent's children (except the block of course)
    mode = d_(Enum('replace', 'append'))

    def initialize(self):
        """ A reimplemented initializer.

        This method will add the include objects to the parent of the
        include and ensure that they are initialized.

        """
        super(Block, self).initialize()

        if self.block: #: This block is setting the content of another block
            #: Remove the existing blocks children
            if self.mode == 'replace':
                #: Clear the blocks children
                for c in self.block.children:
                    c.destroy()
            #: Add this blocks children to the other block
            self.block.insert_children(None, self.children)

        else: #: This block is inserting it's children into it's parent
            self.parent.insert_children(self,self.children)

    def _observe_block(self, change):
        """ A change handler for the 'objects' list of the Include.

        If the object is initialized objects which are removed will be
        unparented and objects which are added will be reparented. Old
        objects will be destroyed if the 'destroy_old' flag is True.

        """
        if self.is_initialized:
            if change['type'] == 'update':
                raise NotImplementedError
                old_block = change['oldvalue']
                old_block.parent.remove_children(old_block,self.children)
                new_block = change['value']
                new_block.parent.insert_children(new_block, self.children)

    def _observe__children(self, change):
        if not self.is_initialized:
            return
        if change['type'] == 'update':
            if self.block:
                if self.mode == 'replace':
                    self.block.children = change['value']
                else:
                    for c in change['oldvalue']:
                        self.block.children.remove(c)
                        c.destroy()
                    before = self.block.children[-1] if self.block.children else None
                    self.block.insert_children(before, change['value'])
            else:
                for c in change['oldvalue']:
                    if c not in change['value']:
                        c.destroy()
                self.parent.insert_children(self, change['value'])

@@ -1222,32 +1222,55 @@
 ()%0A%0A        
-if self.
+block = self.block%0A%0A        if 
 block: #: Th
@@ -1478,21 +1478,16 @@
 or c in 
-self.
 block.ch
@@ -1589,37 +1589,32 @@
 ock%0A            
-self.
 block.insert_chi
@@ -1758,16 +1758,17 @@
 en(self,
+ 
 self.chi
@@ -2185,50 +2185,8 @@
 e':%0A
-                raise NotImplementedError%0A
     
@@ -2252,60 +2252,120 @@
 old_
-block.parent.remove_children(old_block,self
+parent = old_block.parent%0A                for c in self.children:%0A                    old_parent
 .child
+_
 re
-n
+moved(c
 )%0A  
@@ -2579,16 +2579,43 @@
  return%0A
+        block = self.block%0A
         
@@ -2656,29 +2656,24 @@
          if 
-self.
 block:%0A     
@@ -2722,37 +2722,32 @@
                 
-self.
 block.children =
@@ -2854,29 +2854,24 @@
             
-self.
 block.childr
@@ -2948,21 +2948,16 @@
 efore = 
-self.
 block.ch
@@ -2970,21 +2970,16 @@
 %5B-1%5D if 
-self.
 block.ch
@@ -3015,21 +3015,16 @@
         
-self.
 block.in

Fix block reloading and speedup by using local variable