Exemplo n.º 1
0
        public static TWidget TryGetWidget <TWidget>(this jQueryObject element) where TWidget : class
        {
            if (element == null)
            {
                throw new Exception("Argument 'element' is null!");
            }

            TWidget widget;

            if (typeof(TWidget).IsAssignableFrom(typeof(Widget)))
            {
                var widgetName = WidgetExtensions.GetWidgetName(typeof(TWidget));

                widget = element.GetDataValue(widgetName) as TWidget;
                if (widget != null)
                {
                    return(widget);
                }
            }

            var data = element.GetData();

            foreach (string key in data.Keys)
            {
                widget = data[key] as TWidget;
                if (widget != null)
                {
                    return(widget);
                }
            }

            return(null);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Creates a widget on given element. Widget gets a unique name like MyNamespace_MyWidget1234.
        /// </summary>
        /// <remarks>
        /// * A data value is added to element with this unique name as key, and the widget object as value.
        /// * Only one widget instance of same class can be created on a element.
        /// * All events attached by this widget class will be unbound when element is removed from document.
        /// * Elements gets a css class like "s-MyWidget" (it has no namespace by default, but this can be customized)
        /// </remarks>
        protected Widget(jQueryObject element)
        {
            this.element    = element;
            this.widgetName = WidgetExtensions.GetWidgetName(this.GetType());
            this.uniqueName = widgetName + (NextWidgetNumber++).ToString();

            if (element.GetDataValue(widgetName) != null)
            {
                throw new Exception(String.Format("The element already has widget '{0}'!", widgetName));
            }

            var self = this;

            element.Bind("remove." + widgetName, (e) => self.Destroy())
            .Data(widgetName, this);

            AddCssClass();

            if (IsAsyncWidget())
            {
                Window.SetTimeout(delegate()
                {
                    if (element != null && asyncPromise == null)
                    {
                        asyncPromise = this.InitializeAsync();
                    }
                }, 0);
            }
        }
Exemplo n.º 3
0
        public static TWidget TryGetWidget <TWidget>(this jQueryObject element) where TWidget : Widget
        {
            if (element == null)
            {
                throw new Exception("Argument 'element' is null!");
            }

            var widgetName = WidgetExtensions.GetWidgetName(typeof(TWidget));

            return(element.GetDataValue(widgetName) as TWidget);
        }
Exemplo n.º 4
0
        /// <summary>
        /// Creates a widget on given element. Widget gets a unique name like MyNamespace_MyWidget1234.
        /// </summary>
        /// <remarks>
        /// * A data value is added to element with this unique name as key, and the widget object as value.
        /// * Only one widget instance of same class can be created on a element.
        /// * All events attached by this widget class will be unbound when element is removed from document.
        /// * Elements gets a css class like "s-MyWidget" (it has no namespace by default, but this can be customized)
        /// </remarks>
        protected Widget(jQueryObject element)
        {
            this.element    = element;
            this.widgetName = WidgetExtensions.GetWidgetName(this.GetType());
            this.uniqueName = widgetName + (NextWidgetNumber++).ToString();

            if (element.GetDataValue(widgetName) != null)
            {
                throw new Exception(String.Format("The element already has widget '{0}'!", widgetName));
            }

            var self = this;

            element.Bind("remove." + widgetName, (e) => self.Destroy())
            .Data(widgetName, this);

            AddCssClass();

            OnInit();
        }
Exemplo n.º 5
0
        public static object TryGetWidget(this jQueryObject element, Type widgetType)
        {
            if (widgetType == null)
            {
                throw new Exception("Argument 'widgetType' is null!");
            }

            if (element == null)
            {
                throw new Exception("Argument 'element' is null!");
            }

            var widgetName = WidgetExtensions.GetWidgetName(widgetType);
            var widget     = element.GetDataValue(widgetName);

            if (widget != null && !widgetType.IsAssignableFrom(widget.GetType()))
            {
                return(null);
            }

            return(widget);
        }
Exemplo n.º 6
0
        static WidgetExtensions()
        {
            typeof(Widget).Prototype["changeSelect2"] =
                new Action <jQueryEventHandler>((handler) =>
            {
                var widget = Script.This.As <Widget>();
                widget.Element.Bind2("change." + widget.UniqueName, (e, x) =>
                {
                    if (e.HasOriginalEvent() || Q.IsFalse(x))
                    {
                        handler(e);
                    }
                });
            }
                                                );

            typeof(Widget).Prototype["change"] =
                new Action <jQueryEventHandler>((handler) =>
            {
                var widget = Script.This.As <Widget>();
                widget.Element.Bind("change." + widget.UniqueName, handler);
            }
                                                );

            typeof(Widget).Prototype["getGridField"] =
                new Func <jQueryObject>(() =>
            {
                return(Script.This.As <Widget>().Element.Closest(".field"));
            }
                                        );

            jQuery.Instance.fn.tryGetWidget = new Func <Type, object>((widgetType) =>
            {
                var element = Script.This.As <jQueryObject>();

                object widget;
                if (typeof(Widget).IsAssignableFrom(widgetType))
                {
                    var widgetName = WidgetExtensions.GetWidgetName(widgetType);

                    widget = element.GetDataValue(widgetName);
                    if (widget != null && !widgetType.IsAssignableFrom(widget.GetType()))
                    {
                        widget = null;
                    }

                    if (widget != null)
                    {
                        return(widget);
                    }
                }

                var data = element.GetData();
                if (data == null)
                {
                    return(null);
                }

                foreach (string key in data.Keys)
                {
                    widget = data[key];
                    if (widget != null && widgetType.IsAssignableFrom(widget.GetType()))
                    {
                        return(widget);
                    }
                }

                return(null);
            });


            jQuery.Instance.fn.getWidget = new Func <Type, object>((widgetType) =>
            {
                var element = Script.This.As <jQueryObject>();

                if (element == null)
                {
                    throw new ArgumentNullException("element");
                }

                if (element.Length == 0)
                {
                    throw new Exception(String.Format("Searching for widget of type '{0}' on a non-existent element! ({1})",
                                                      widgetType.FullName, element.Selector));
                }

                var widget = TryGetWidget(element, widgetType);
                if (widget == null)
                {
                    throw new Exception(String.Format("Element has no widget of type '{0}'!", widgetType.FullName));
                }

                return(widget);
            });
        }
Exemplo n.º 7
0
        static WidgetExtensions()
        {
            typeof(Widget).Prototype["changeSelect2"] =
                new Action <jQueryEventHandler>((handler) =>
            {
                var widget = Script.This.As <Widget>();
                widget.Element.Bind2("change." + widget.UniqueName, (e, x) =>
                {
                    if (e.HasOriginalEvent() || Q.IsFalse(x))
                    {
                        handler(e);
                    }
                });
            }
                                                );

            typeof(Widget).Prototype["change"] =
                new Action <jQueryEventHandler>((handler) =>
            {
                var widget = Script.This.As <Widget>();
                widget.Element.Bind("change." + widget.UniqueName, handler);
            }
                                                );

            typeof(Widget).Prototype["getGridField"] =
                new Func <jQueryObject>(() =>
            {
                return(Script.This.As <Widget>().Element.Closest(".field"));
            }
                                        );

            jQuery.Instance.fn.tryGetWidget = new Func <Type, object>((widgetType) =>
            {
                var element = Script.This.As <jQueryObject>();

                object widget;
                if (typeof(Widget).IsAssignableFrom(widgetType))
                {
                    var widgetName = WidgetExtensions.GetWidgetName(widgetType);

                    widget = element.GetDataValue(widgetName);
                    if (widget != null && !widgetType.IsAssignableFrom(widget.GetType()))
                    {
                        widget = null;
                    }

                    if (widget != null)
                    {
                        return(widget);
                    }
                }

                var data = element.GetData();
                if (data == null)
                {
                    return(null);
                }

                foreach (string key in data.Keys)
                {
                    widget = data[key];
                    if (widget != null && widgetType.IsAssignableFrom(widget.GetType()))
                    {
                        return(widget);
                    }
                }

                return(null);
            });


            jQuery.Instance.fn.getWidget = new Func <Type, object>((widgetType) =>
            {
                var element = Script.This.As <jQueryObject>();

                if (element == null)
                {
                    throw new ArgumentNullException("element");
                }

                if (element.Length == 0)
                {
                    throw new Exception(String.Format("Searching for widget of type '{0}' on a non-existent element! ({1})",
                                                      widgetType.FullName, element.Selector));
                }

                var widget = TryGetWidget(element, widgetType);
                if (widget == null)
                {
                    var message = String.Format("Element has no widget of type '{0}'! If you have recently changed editor type of a property in " +
                                                "a form class, or changed data type in row (which also changes editor type) your script side Form definition might be out of date. " +
                                                "Make sure your project builds successfully and transform T4 templates", widgetType.FullName);

                    Q.NotifyError(message);
                    throw new Exception(message);
                }

                return(widget);
            });
        }