Ejemplo n.º 1
0
        /// <summary>
        /// Attaches behaviors associated declaratively with the specified element and optionally
        /// to contained elements.
        /// </summary>
        /// <param name="element">The element to attach behaviors to.</param>
        /// <param name="recursive">Whether to recursively attach behaviors from contained elements.</param>
        public static void SetupBehaviors(Element element, bool recursive)
        {
            Debug.Assert(element != null);

            if (element.HasAttribute(Behavior.BehaviorsAttribute)) {
                AttachBehaviors(element);
            }

            if (recursive) {
                ElementCollection elements = element.QuerySelectorAll(Behavior.BehaviorsSelector);
                int matchingElements = elements.Length;

                if (matchingElements != 0) {
                    for (int i = 0; i < matchingElements; i++) {
                        AttachBehaviors(elements[i]);
                    }
                }
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Deactivates the specified element and its children by disposing any
        /// behaviors or binding instances attached to the elements.
        /// </summary>
        /// <param name="element">The element to deactivate.</param>
        /// <param name="contentOnly">Whether the element should be deactivated, or only its contained content.</param>
        public void DeactivateFragment(Element element, bool contentOnly)
        {
            Debug.Assert(element != null);

            // Detach behaviors associated with the element and the contained elements.
            if ((contentOnly == false) && element.HasAttribute(Application.BehaviorsAttribute)) {
                DetachBehaviors(element);
            }

            ElementCollection elements = element.QuerySelectorAll(Application.BehaviorsSelector);
            int matchingElements = elements.Length;

            if (matchingElements != 0) {
                for (int i = 0; i < matchingElements; i++) {
                    DetachBehaviors(elements[i]);
                }
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Initializes a behavior by attaching it to the specified element.
        /// When overriding this, be sure to call the base functionality before the implementation
        /// in the derived class.
        /// </summary>
        /// <param name="element">The element to attach the behavior to.</param>
        /// <param name="options">Any initialization options that the behavior should consume.</param>
        public virtual void Initialize(Element element, Dictionary<string, object> options)
        {
            Debug.Assert(_element == null, "A behavior should be initialized only once.");
            Debug.Assert(element != null, "Expected a valid element to initialize a behavior.");

            _element = element;

            // Automatically expose the behavior on the DOM element as an expando using
            // the name of the behavior as the key.

            string name = Script.GetField<string>(this.GetType(), Application.BehaviorNameKey);
            Script.SetField(element, name, this);

            // Add a 'behaviors' expando on the element which is a key/value pair
            // list of behavior name -> behavior instance mapping.

            Dictionary<string, Behavior> behaviors = Script.GetField<Dictionary<string, Behavior>>(element, Application.BehaviorsKey);
            if (behaviors == null) {
                behaviors = new Dictionary<string, Behavior>();
                Script.SetField(element, Application.BehaviorsKey, behaviors);
            }

            behaviors[name] = this;

            // Set data-behavior on the element if it is not already set (i.e. if the behavior
            // is being programmatically constructed), so that if DisposeBehaviors is called,
            // then this element is picked up via the CSS query used to find elements that have
            // behaviors.
            if (element.HasAttribute(Application.BehaviorsAttribute) == false) {
                element.SetAttribute(Application.BehaviorsAttribute, String.Empty);
            }
        }
Ejemplo n.º 4
0
        public void ActivateFragment(Element element, bool contentOnly, object model)
        {
            Debug.Assert(element != null);

            if (element == Document.Body) {
                // Perform app-level activation (tied to body initialization)

                // Setup top-level services specified declaratively.
                SetupServices();

                // Create the model declaratively associated with the application.
                if (model == null) {
                    string modelTypeName = (string)Document.Body.GetAttribute(ModelTypeAttribute);
                    if (String.IsNullOrEmpty(modelTypeName) == false) {
                        Type modelType = Type.GetType(modelTypeName);
                        Debug.Assert(modelType != null, "Could not resolve model '" + modelTypeName + "'");

                        model = GetObject(modelType);

                        IInitializable initializableModel = model as IInitializable;
                        if (initializableModel != null) {
                            Dictionary<string, object> modelData = OptionsParser.GetOptions(element, "model");
                            initializableModel.BeginInitialization(modelData);
                            initializableModel.EndInitialization();
                        }
                    }
                }

                _model = model;
            }

            // Create expressions and bindings associated with the specified elements and
            // the contained elements. Do this first, as this allows behaviors to look at
            // values set as a result of bindings when they are attached.
            if ((contentOnly == false) && element.HasAttribute(Application.BindingsAttribute)) {
                SetupBindings(element, model);
            }

            ElementCollection boundElements = element.QuerySelectorAll(Application.BindingsSelector);
            for (int i = 0, boundElementCount = boundElements.Length; i < boundElementCount; i++) {
                SetupBindings(boundElements[i], model);
            }

            // Attach behaviors associated declaratively with the specified element and the
            // contained elements.
            if ((contentOnly == false) && element.HasAttribute(Application.BehaviorsAttribute)) {
                AttachBehaviors(element);
            }

            ElementCollection extendedElements = element.QuerySelectorAll(Application.BehaviorsSelector);
            for (int i = 0, extendedElementCount = extendedElements.Length; i < extendedElementCount; i++) {
                AttachBehaviors(extendedElements[i]);
            }
        }