home *** CD-ROM | disk | FTP | other *** search
/ One Click 21 (Special) / OC021.iso / Interface / it.dig / scripts / __Packages / mx / data / binding / Binding.as next >
Encoding:
Text File  |  2006-07-04  |  14.6 KB  |  441 lines

  1. class mx.data.binding.Binding
  2. {
  3.    var is2way;
  4.    var dest;
  5.    var source;
  6.    var value;
  7.    var format;
  8.    var value2;
  9.    var immediate;
  10.    var binding;
  11.    var queued = false;
  12.    var reverse = false;
  13.    static var counter = 0;
  14.    static var screenRegistry = new Object();
  15.    static var bindingRegistry = new Array();
  16.    function Binding(source, dest, format, is2way)
  17.    {
  18.       mx.events.EventDispatcher.initialize(this);
  19.       var _loc5_ = this;
  20.       _loc5_.source = source;
  21.       _loc5_.dest = dest;
  22.       _loc5_.format = format;
  23.       _loc5_.is2way = is2way;
  24.       mx.data.binding.Binding.registerBinding(this);
  25.       this.calcShortLoc(source);
  26.       this.calcShortLoc(dest);
  27.       _global.__dataLogger.logData(null,"Creating binding " + this.summaryString() + (!is2way ? "" : ", 2-way"),{binding:this});
  28.       _global.__dataLogger.nestLevel = _global.__dataLogger.nestLevel + 1;
  29.       mx.data.binding.ComponentMixins.initComponent(dest.component);
  30.       if(source.component != undefined)
  31.       {
  32.          mx.data.binding.ComponentMixins.initComponent(source.component);
  33.       }
  34.       dest.component.addBinding(this);
  35.       if(source.component != undefined)
  36.       {
  37.          source.component.addBinding(this);
  38.          this.setUpListener(source,false);
  39.          if(this.is2way)
  40.          {
  41.             this.setUpListener(dest,true);
  42.             this.setUpIndexListeners(source,false);
  43.             this.setUpIndexListeners(dest,true);
  44.          }
  45.          else
  46.          {
  47.             this.setUpIndexListeners(source,false);
  48.             this.setUpIndexListeners(dest,false);
  49.          }
  50.       }
  51.       else
  52.       {
  53.          this.execute();
  54.       }
  55.       _global.__dataLogger.nestLevel--;
  56.    }
  57.    function execute(reverse)
  58.    {
  59.       var _loc3_ = undefined;
  60.       var _loc4_ = undefined;
  61.       if(reverse)
  62.       {
  63.          if(!this.is2way)
  64.          {
  65.             _global.__dataLogger.logData(null,"Warning: Can\'t execute binding " + this.summaryString(false) + " in reverse, because it\'s not a 2 way binding",{binding:this},mx.data.binding.Log.BRIEF);
  66.             return ["error"];
  67.          }
  68.          _loc3_ = this.dest;
  69.          _loc4_ = this.source;
  70.       }
  71.       else
  72.       {
  73.          _loc3_ = this.source;
  74.          _loc4_ = this.dest;
  75.       }
  76.       _global.__dataLogger.logData(null,"Executing binding " + this.summaryString(reverse),{binding:this});
  77.       _global.__dataLogger.nestLevel = _global.__dataLogger.nestLevel + 1;
  78.       var _loc10_ = undefined;
  79.       if(_loc3_.constant != undefined)
  80.       {
  81.          _loc10_ = {value:new mx.data.binding.TypedValue(_loc3_.constant,"String"),getAnyTypedValue:function()
  82.          {
  83.             return this.value;
  84.          },getTypedValue:function()
  85.          {
  86.             return this.value;
  87.          },getGettableTypes:function()
  88.          {
  89.             return ["String"];
  90.          }};
  91.       }
  92.       else
  93.       {
  94.          _loc10_ = _loc3_.component.getField(_loc3_.property,_loc3_.location,true);
  95.       }
  96.       var _loc20_ = undefined;
  97.       var _loc16_ = undefined;
  98.       var _loc13_ = "";
  99.       var _loc6_ = _loc4_.component.getField(_loc4_.property,_loc4_.location);
  100.       if(this.format != null)
  101.       {
  102.          var _loc5_ = mx.data.binding.Binding.getRuntimeObject(this.format);
  103.          if(_loc5_ != null)
  104.          {
  105.             if(reverse)
  106.             {
  107.                _loc5_.setupDataAccessor(_loc4_.component,_loc4_.property,_loc4_.location);
  108.                _loc5_.dataAccessor = _loc6_;
  109.                _loc6_ = _loc5_;
  110.             }
  111.             else
  112.             {
  113.                _loc5_.setupDataAccessor(_loc3_.component,_loc3_.property,_loc3_.location);
  114.                _loc5_.dataAccessor = _loc10_;
  115.                _loc10_ = _loc5_;
  116.             }
  117.          }
  118.       }
  119.       var _loc15_ = this.format != null ? null : _loc6_.getSettableTypes();
  120.       var _loc11_ = _loc10_.getAnyTypedValue(_loc15_);
  121.       var _loc7_ = new Object();
  122.       if(_loc6_.type.readonly == true)
  123.       {
  124.          _global.__dataLogger.logData(null,"Not executing binding because the destination is read-only",null,mx.data.binding.Log.BRIEF);
  125.          var _loc8_ = new Object();
  126.          _loc8_.type = "invalid";
  127.          _loc8_.property = _loc4_.property;
  128.          _loc8_.location = _loc4_.location;
  129.          _loc8_.messages = [{message:"Cannot assign to a read-only data field."}];
  130.          _loc4_.component.dispatchEvent(_loc8_);
  131.          _loc7_.event = _loc8_;
  132.       }
  133.       else
  134.       {
  135.          _global.__dataLogger.logData(null,"Assigning new value \'<value>\' (<typeName>) " + _loc13_,{value:_loc11_.value,typeName:_loc11_.typeName,unformattedValue:_loc20_,formatterFrom:_loc16_});
  136.          var _loc14_ = _loc6_.setAnyTypedValue(_loc11_);
  137.          _loc6_.validateAndNotify(_loc7_,false,_loc14_);
  138.          _loc4_.component.dispatchEvent({type:"bindingExecuted",binding:this});
  139.       }
  140.       if(_loc7_.event != null)
  141.       {
  142.          if(_loc3_.component != null)
  143.          {
  144.             var _loc9_ = new Object();
  145.             _loc9_.type = _loc7_.event.type;
  146.             _loc9_.property = _loc3_.property;
  147.             _loc9_.location = _loc3_.location;
  148.             _loc9_.messages = _loc7_.event.messages;
  149.             _loc9_.to = _loc4_.component;
  150.             _loc3_.component.dispatchEvent(_loc9_);
  151.          }
  152.       }
  153.       _global.__dataLogger.nestLevel--;
  154.       return _loc7_.event.messages;
  155.    }
  156.    function queueForExecute(reverse)
  157.    {
  158.       if(!this.queued)
  159.       {
  160.          if(_global.__databind_executeQueue == null)
  161.          {
  162.             _global.__databind_executeQueue = new Array();
  163.          }
  164.          if(_root.__databind_dispatch == undefined)
  165.          {
  166.             _root.createEmptyMovieClip("__databind_dispatch",-8888);
  167.          }
  168.          _global.__databind_executeQueue.push(this);
  169.          this.queued = true;
  170.          this.reverse = reverse;
  171.          _root.__databind_dispatch.onEnterFrame = mx.data.binding.Binding.dispatchEnterFrame;
  172.       }
  173.    }
  174.    static function dispatchEnterFrame()
  175.    {
  176.       _root.__databind_dispatch.onEnterFrame = null;
  177.       var _loc4_ = 0;
  178.       while(_loc4_ < _global.__databind_executeQueue.length)
  179.       {
  180.          var _loc3_ = _global.__databind_executeQueue[_loc4_];
  181.          _loc3_.execute(_loc3_.reverse);
  182.          _loc4_ = _loc4_ + 1;
  183.       }
  184.       var _loc5_ = undefined;
  185.       while((_loc5_ = _global.__databind_executeQueue.pop()) != null)
  186.       {
  187.          _loc5_.queued = false;
  188.          _loc5_.reverse = false;
  189.       }
  190.    }
  191.    function calcShortLoc(endpoint)
  192.    {
  193.       var _loc1_ = endpoint.location;
  194.       if(_loc1_.path != null)
  195.       {
  196.          _loc1_ = _loc1_.path;
  197.       }
  198.       endpoint.loc = !(_loc1_ instanceof Array) ? _loc1_ : _loc1_.join(".");
  199.    }
  200.    function summaryString(reverse)
  201.    {
  202.       var _loc2_ = "<binding.dest.component>:<binding.dest.property>:<binding.dest.loc>";
  203.       var _loc3_ = "<binding.source.component>:<binding.source.property>:<binding.source.loc>";
  204.       if(this.source.constant == null)
  205.       {
  206.          if(reverse == true)
  207.          {
  208.             return "from " + _loc2_ + " to " + _loc3_;
  209.          }
  210.          return "from " + _loc3_ + " to " + _loc2_;
  211.       }
  212.       return "from constant \'<binding.source.constant>\' to " + _loc2_;
  213.    }
  214.    static function getRuntimeObject(info, constructorParameter)
  215.    {
  216.       if(info.cls == undefined)
  217.       {
  218.          info.cls = mx.utils.ClassFinder.findClass(info.className);
  219.       }
  220.       var _loc3_ = new info.cls(constructorParameter);
  221.       if(_loc3_ == null)
  222.       {
  223.          _global.__dataLogger.logData(null,"Could not construct a formatter or validator - new <info.className>(<params>)",{info:info,params:constructorParameter},mx.data.binding.Log.BRIEF);
  224.       }
  225.       for(var _loc4_ in info.settings)
  226.       {
  227.          _loc3_[_loc4_] = info.settings[_loc4_];
  228.       }
  229.       return _loc3_;
  230.    }
  231.    static function refreshFromSources(component, property, bindings)
  232.    {
  233.       var _loc5_ = null;
  234.       var _loc3_ = undefined;
  235.       _loc3_ = 0;
  236.       while(_loc3_ < bindings.length)
  237.       {
  238.          var _loc1_ = bindings[_loc3_];
  239.          var _loc2_ = null;
  240.          if(_loc1_.dest.component == component && (property == null || property == _loc1_.dest.property))
  241.          {
  242.             _loc2_ = _loc1_.execute();
  243.          }
  244.          else if(_loc1_.is2way && _loc1_.source.component == component && (property == null || property == _loc1_.source.property))
  245.          {
  246.             _loc2_ = _loc1_.execute(true);
  247.          }
  248.          if(_loc2_ != null)
  249.          {
  250.             _loc5_ = _loc5_ != null ? _loc5_.concat(_loc2_) : _loc2_;
  251.          }
  252.          _loc3_ = _loc3_ + 1;
  253.       }
  254.       return _loc5_;
  255.    }
  256.    static function refreshDestinations(component, bindings)
  257.    {
  258.       var _loc1_ = undefined;
  259.       _loc1_ = 0;
  260.       while(_loc1_ < bindings.length)
  261.       {
  262.          var _loc2_ = bindings[_loc1_];
  263.          if(_loc2_.source.component == component)
  264.          {
  265.             _loc2_.execute();
  266.          }
  267.          else if(_loc2_.is2way && _loc2_.dest.component == component)
  268.          {
  269.             _loc2_.execute(true);
  270.          }
  271.          _loc1_ = _loc1_ + 1;
  272.       }
  273.       _loc1_ = 0;
  274.       while(_loc1_ < component.__indexBindings.length)
  275.       {
  276.          var _loc3_ = component.__indexBindings[_loc1_];
  277.          _loc3_.binding.execute(_loc3_.reverse);
  278.          _loc1_ = _loc1_ + 1;
  279.       }
  280.    }
  281.    static function okToCallGetterFromSetter()
  282.    {
  283.       function setter(val)
  284.       {
  285.          this.value2 = this.value;
  286.       }
  287.       function getter()
  288.       {
  289.          return 5;
  290.       }
  291.       var _loc2_ = new Object();
  292.       _loc2_.addProperty("value",getter,setter);
  293.       _loc2_.value = 0;
  294.       var _loc3_ = _loc2_.value2 == _loc2_.value;
  295.       return _loc3_;
  296.    }
  297.    function setUpListener(endpoint, reverse)
  298.    {
  299.       var _loc4_ = new Object();
  300.       _loc4_.binding = this;
  301.       _loc4_.property = endpoint.property;
  302.       _loc4_.reverse = reverse;
  303.       _loc4_.immediate = mx.data.binding.Binding.okToCallGetterFromSetter();
  304.       _loc4_.handleEvent = function(event)
  305.       {
  306.          _global.__dataLogger.logData(event.target,"Data of property \'<property>\' has changed. <immediate>.",this);
  307.          if(this.immediate)
  308.          {
  309.             if(this.binding.executing != true)
  310.             {
  311.                this.binding.executing = true;
  312.                this.binding.execute(this.reverse);
  313.                this.binding.executing = false;
  314.             }
  315.          }
  316.          else
  317.          {
  318.             this.binding.queueForExecute(this.reverse);
  319.          }
  320.       };
  321.       if(endpoint.event instanceof Array)
  322.       {
  323.          for(var _loc5_ in endpoint.event)
  324.          {
  325.             endpoint.component.__addHighPrioEventListener(endpoint.event[_loc5_],_loc4_);
  326.          }
  327.       }
  328.       else
  329.       {
  330.          endpoint.component.__addHighPrioEventListener(endpoint.event,_loc4_);
  331.       }
  332.       mx.data.binding.ComponentMixins.initComponent(endpoint.component);
  333.    }
  334.    function setUpIndexListeners(endpoint, reverse)
  335.    {
  336.       if(endpoint.location.indices != undefined)
  337.       {
  338.          var _loc3_ = 0;
  339.          while(_loc3_ < endpoint.location.indices.length)
  340.          {
  341.             var _loc2_ = endpoint.location.indices[_loc3_];
  342.             if(_loc2_.component != undefined)
  343.             {
  344.                this.setUpListener(_loc2_,reverse);
  345.                if(_loc2_.component.__indexBindings == undefined)
  346.                {
  347.                   _loc2_.component.__indexBindings = new Array();
  348.                }
  349.                _loc2_.component.__indexBindings.push({binding:this,reverse:reverse});
  350.             }
  351.             _loc3_ = _loc3_ + 1;
  352.          }
  353.       }
  354.    }
  355.    static function copyBinding(b)
  356.    {
  357.       var _loc1_ = new Object();
  358.       _loc1_.source = mx.data.binding.Binding.copyEndPoint(b.source);
  359.       _loc1_.dest = mx.data.binding.Binding.copyEndPoint(b.dest);
  360.       _loc1_.format = b.format;
  361.       _loc1_.is2way = b.is2way;
  362.       return _loc1_;
  363.    }
  364.    static function copyEndPoint(e)
  365.    {
  366.       var _loc1_ = new Object();
  367.       _loc1_.constant = e.constant;
  368.       _loc1_.component = String(e.component);
  369.       _loc1_.event = e.event;
  370.       _loc1_.location = e.location;
  371.       _loc1_.property = e.property;
  372.       return _loc1_;
  373.    }
  374.    static function registerScreen(screen, id)
  375.    {
  376.       var symbol = mx.data.binding.Binding.screenRegistry[id];
  377.       if(symbol == null)
  378.       {
  379.          mx.data.binding.Binding.screenRegistry[id] = {symbolPath:String(screen),bindings:[],id:id};
  380.          return undefined;
  381.       }
  382.       if(symbol.symbolPath == String(screen))
  383.       {
  384.          return undefined;
  385.       }
  386.       var instancePath = String(screen);
  387.       var i = 0;
  388.       while(i < mx.data.binding.Binding.bindingRegistry.length)
  389.       {
  390.          var b = mx.data.binding.Binding.bindingRegistry[i];
  391.          var src = mx.data.binding.Binding.copyEndPoint(b.source);
  392.          var dst = mx.data.binding.Binding.copyEndPoint(b.dest);
  393.          var prefix = symbol.symbolPath + ".";
  394.          var symbolContainsSource = prefix == b.source.component.substr(0,prefix.length);
  395.          var symbolContainsDest = prefix == b.dest.component.substr(0,prefix.length);
  396.          if(symbolContainsSource)
  397.          {
  398.             if(symbolContainsDest)
  399.             {
  400.                src.component = eval(instancePath + src.component.substr(symbol.symbolPath.length));
  401.                dst.component = eval(instancePath + dst.component.substr(symbol.symbolPath.length));
  402.                new mx.data.binding.Binding(src,dst,b.format,b.is2way);
  403.             }
  404.             else
  405.             {
  406.                src.component = eval(instancePath + src.component.substr(symbol.symbolPath.length));
  407.                dst.component = eval(dst.component);
  408.                new mx.data.binding.Binding(src,dst,b.format,b.is2way);
  409.             }
  410.          }
  411.          else if(symbolContainsDest)
  412.          {
  413.             src.component = eval(src.component);
  414.             dst.component = eval(instancePath + dst.component.substr(symbol.symbolPath.length));
  415.             new mx.data.binding.Binding(src,dst,b.format,b.is2way);
  416.          }
  417.          i++;
  418.       }
  419.    }
  420.    static function registerBinding(binding)
  421.    {
  422.       var _loc1_ = mx.data.binding.Binding.copyBinding(binding);
  423.       mx.data.binding.Binding.bindingRegistry.push(_loc1_);
  424.    }
  425.    static function getLocalRoot(clip)
  426.    {
  427.       var _loc2_ = undefined;
  428.       var _loc3_ = clip._url;
  429.       while(clip != null)
  430.       {
  431.          if(clip._url != _loc3_)
  432.          {
  433.             break;
  434.          }
  435.          _loc2_ = clip;
  436.          clip = clip._parent;
  437.       }
  438.       return _loc2_;
  439.    }
  440. }
  441.