Exemple #1
0
        public XrmDateEditor(EditorArguments args) : base(args)
        {
            XrmDateEditor self = this;

            _container = jQuery.FromHtml("<div ><table class='inline-edit-container' cellspacing='0' cellpadding='0'><tr>" +
                                         "<td><INPUT type=text class='sparkle-input-inline' /></td>" +
                                         "<td class='lookup-button-td'><input type=button class='sparkle-imagestrip-inlineedit_calendar_icon' /></td></tr></table></div>");
            _container.AppendTo(_args.Container);

            _input = _container.Find(".sparkle-input-inline");
            _input.Bind("keydown.nav", delegate(jQueryEvent e)
            {
                if (!_calendarOpen && (e.Which == 38 || e.Which == 40) && e.CtrlKey) // Ctrl-Up/Down shows date picker
                {
                    _input.Plugin <DatePickerPlugIn>().DatePicker(DatePickerMethod2.Show);
                    e.StopImmediatePropagation();
                }
                else if (_calendarOpen && e.Which == 13)
                {
                    e.PreventDefault();
                }
            });
            jQueryObject selectButton = _container.Find(".sparkle-imagestrip-inlineedit_calendar_icon");

            _input.Focus().Select();
            DatePickerOptions2 options2 = new DatePickerOptions2();

            options2.ShowOtherMonths = true;
            options2.ShowOn          = ""; // Date Pickers in CRM do not show when they are focused - you click the button
            options2.FirstDay        = OrganizationServiceProxy.OrganizationSettings != null ? OrganizationServiceProxy.OrganizationSettings.WeekStartDayCode.Value.Value : 0;
            options2.BeforeShow      = delegate()
            {
                this._calendarOpen = true;
            };

            options2.OnClose = delegate()
            {
                this._calendarOpen = false;
                _selectedValue     = GetSelectedValue();
            };

            options2.OnSelect = delegate(string dateString, object instance)
            {
                // Select the date text field when selecting a date
                Focus();
            };

            if (OrganizationServiceProxy.UserSettings != null)
            {
                _dateFormat = OrganizationServiceProxy.UserSettings.DateFormatString;
            }
            options2.DateFormat = _dateFormat;
            _input.Plugin <DatePickerPlugIn>().DatePicker(options2);

            // Wire up the date picker button
            selectButton.Click(delegate(jQueryEvent e){
                _input.Plugin <DatePickerPlugIn>().DatePicker(DatePickerMethod2.Show);
                Focus();
            });
        }
        public XrmTimeEditor(EditorArguments args) : base(args)
        {
            if (OrganizationServiceProxy.UserSettings != null)
            {
                _formatString = OrganizationServiceProxy.UserSettings.TimeFormatString;
            }


            _container = jQuery.FromHtml("<div ><table class='inline-edit-container' cellspacing='0' cellpadding='0'><tr><td><INPUT type=text class='sparkle-input-inline' /></td><td class='lookup-button-td'><input type=button class='autocompleteButton' /></td></tr></table></div>");

            _container.AppendTo(_args.Container);

            jQueryObject inputField = _container.Find(".sparkle-input-inline");

            _input = inputField;
            _input.Focus().Select();
            string timeFormatString     = _formatString;
            AutoCompleteOptions options = GetTimePickerAutoCompleteOptions(timeFormatString);

            options.Select = delegate(jQueryEvent e, AutoCompleteSelectEvent uiEvent)
            {
                // Note we assume that the binding has added an array of string items
                //string value = ((Dictionary)uiEvent.Item)["value"].ToString();
            };

            inputField = inputField.Plugin <AutoCompleteObject>().AutoComplete(options);
            jQueryObject selectButton = _container.Find(".autocompleteButton");

            // Add the click binding to show the drop down
            selectButton.Click(delegate(jQueryEvent e)
            {
                inputField.Plugin <AutoCompleteObject>().AutoComplete(AutoCompleteMethod.Search, "");
            });
        }
Exemple #3
0
        private jQueryObject AddEmptyRow(bool popupField)
        {
            jQueryObject emptyRow = FindEmptyRow();

            if (emptyRow != null)
            {
                emptyRow.Find("input.field-select").Select2("focus");

                if (popupField)
                {
                    emptyRow.Find("input.field-select").Select2("open");
                }

                return(emptyRow);
            }

            bool isLastRowOr = this.rowsDiv.Children().Last().Children("a.andor").HasClass("or");

            jQueryObject row = J(RowTemplate).AppendTo(this.rowsDiv);

            jQueryObject parenDiv = row.Children("div.l").Hide();

            parenDiv.Children("a.leftparen, a.rightparen").Click(LeftRightParenClick);

            jQueryObject andor = parenDiv.Children("a.andor").Attribute("title", Q.Text("Controls.FilterPanel.ChangeAndOr"));

            if (isLastRowOr)
            {
                andor.AddClass("or").Text(Q.Text("Controls.FilterPanel.Or"));
            }
            else
            {
                andor.Text(Q.Text("Controls.FilterPanel.And"));
            }

            andor.Click(AndOrClick);

            row.Children("a.delete").Attribute("title", Q.Text("Controls.FilterPanel.RemoveField")).Click(DeleteRowClick);

            var fieldSel = new FieldSelect(row.Children("div.f").Children("input"), this.Store.Fields);

            fieldSel.ChangeSelect2(OnRowFieldChange);

            UpdateParens();
            UpdateButtons();

            row.Find("input.field-select").Select2("focus");

            if (popupField)
            {
                row.Find("input.field-select").Select2("open");
            }

            return(row);
        }
        /// <summary>Constructor.</summary>
        /// <param name="listTree">The list tree under control.</param>
        /// <param name="backButton">The back button.</param>
        /// <param name="backMask">The mask which causes the back-button to look like a back button.</param>
        public ListTreeBackController(ListTreeView listTree, jQueryObject backButton, jQueryObject backMask)
        {
            // Store values.
            this.listTree = listTree;
            this.backButton = backButton;
            this.backMask = backMask;

            // Wire up events.
            listTree.PropertyChanged += OnPropertyChanged;
            backButton.Click(OnBackClick);
            backMask.Click(OnBackClick);
        }
Exemple #5
0
        public override void Init(System.Html.Element element, Func <object> valueAccessor, Func <System.Collections.Dictionary> allBindingsAccessor, object viewModel, object context)
        {
            string       formatString = GetFormatString();
            jQueryObject container    = jQuery.FromElement(element);

            jQueryObject inputField   = container.Find(".sparkle-input-timeofday-part");
            jQueryObject selectButton = container.Find(".sparkle-input-timeofday-button-part");

            AutoCompleteOptions options = XrmTimeEditor.GetTimePickerAutoCompleteOptions(formatString);

            options.Position = new Dictionary <string, object>("collision", "fit");
            options.Select   = delegate(jQueryEvent e, AutoCompleteSelectEvent uiEvent)
            {
                // Note we assume that the binding has added an array of string items
                string value = ((Dictionary)uiEvent.Item)["value"].ToString();

                TrySetObservable(valueAccessor, inputField, value);
            };

            inputField = inputField.Plugin <AutoCompleteObject>().AutoComplete(options);


            // Add the click binding to show the drop down
            selectButton.Click(delegate(jQueryEvent e)
            {
                inputField.Plugin <AutoCompleteObject>().AutoComplete(AutoCompleteMethod.Search, ""); // Give "" to show all items
            });
            //// Set initial value
            //Observable<DateTime> dateValueAccessor = (Observable<DateTime>)valueAccessor();
            //DateTime intialValue = dateValueAccessor.GetValue();
            //FormatterUpdate(inputField, intialValue);

            //handle the field changing
            KnockoutUtils.RegisterEventHandler(inputField.GetElement(0), "change", delegate(object sender, EventArgs e)
            {
                string value = inputField.GetValue();
                TrySetObservable(valueAccessor, inputField, value);
            });

            Action disposeCallBack = delegate()
            {
                Script.Literal("$({0}).autocomplete(\"destroy\")", element);
            };

            //handle disposal (if KO removes by the template binding)
            Script.Literal("ko.utils.domNodeDisposal.addDisposeCallback({0}, {1})", element, (object)disposeCallBack);

            // Note: Because the time picker is always part of the date picker - we don't need to display validation messages
            //Knockout.BindingHandlers["validationCore"].Init(element, valueAccessor, allBindingsAccessor, null, null);
        }
Exemple #6
0
        public ConfirmedMatches(Element element)
            : base(element, "ConfirmedMatches")
        {
            jQueryObject cancelObject      = jQuery.Select(".confirmedMatch .cancelConfirmedMatch");
            jQueryObject reportScoreObject = jQuery.Select(".confirmedMatch .inputScore");

            if (cancelObject != null)
            {
                cancelObject.Click(CancelMatch);
            }
            if (reportScoreObject != null)
            {
                reportScoreObject.Click(ReportScore);
            }
        }
        private jQueryObject AddEmptyRow()
        {
            jQueryObject emptyRow = FindEmptyRow();

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

            bool isLastRowOr = this.rowsDiv.Children().Last().Children("a.andor").HasClass("or");

            jQueryObject row = J(RowTemplate).AppendTo(this.rowsDiv);

            jQueryObject parenDiv = row.Children("div.l").Hide();

            parenDiv.Children("a.leftparen, a.rightparen").Click(LeftRightParenClick);

            jQueryObject andor = parenDiv.Children("a.andor").Attribute("title", Q.Text("Controls.FilterPanel.ChangeAndOr"));

            if (isLastRowOr)
            {
                andor.AddClass("or").Text(Q.Text("Controls.FilterPanel.Or"));
            }
            else
            {
                andor.Text(Q.Text("Controls.FilterPanel.And"));
            }

            andor.Click(AndOrClick);

            row.Children("a.delete").Attribute("title", Q.Text("Controls.FilterPanel.RemoveField")).Click(DeleteRowClick);

            jQueryObject fieldSel = row.Children("div.f").Children("select");

            fieldSel.Change(OnRowFieldChange);

            PopulateFieldList(fieldSel);

            UpdateParens();
            UpdateButtons();
            OnHeightChange();

            fieldSel.Focus();

            return(row);
        }
Exemple #8
0
        public override void Init(System.Html.Element element, Func <object> valueAccessor, Func <System.Collections.Dictionary> allBindingsAccessor, object viewModel, object context)
        {
            jQueryObject        container    = jQuery.FromElement(element);
            jQueryObject        inputField   = container.Find(".sparkle-input-duration-part");
            jQueryObject        selectButton = container.Find(".sparkle-input-duration-button-part");
            AutoCompleteOptions options      = new AutoCompleteOptions();

            options.Position  = new Dictionary <string, object>("collision", "fit");
            options.Source    = new string[] { "1 m", "2 m", "1 h", "2 h", "1 d" };
            options.Delay     = 0;
            options.MinLength = 0;

            options.Select = delegate(jQueryEvent e, AutoCompleteSelectEvent uiEvent)
            {
                // Note we assume that the binding has added an array of string items
                string value = ((Dictionary)uiEvent.Item)["value"].ToString();

                TrySetObservable(valueAccessor, inputField, value);
            };

            inputField = inputField.Plugin <AutoCompleteObject>().AutoComplete(options);

            // Add the click binding to show the drop down
            selectButton.Click(delegate(jQueryEvent e)
            {
                inputField.Plugin <AutoCompleteObject>().AutoComplete(AutoCompleteMethod.Search, "");// Give "" to show all items
            });

            //handle the field changing
            KnockoutUtils.RegisterEventHandler(element, "change", delegate(object sender, EventArgs e)
            {
                string value = inputField.GetValue();
                TrySetObservable(valueAccessor, inputField, value);
            });

            Action disposeCallBack = delegate()
            {
                Script.Literal("$({0}).autocomplete(\"destroy\")", element);
            };

            //handle disposal (if KO removes by the template binding)
            Script.Literal("ko.utils.domNodeDisposal.addDisposeCallback({0}, {1})", element, (object)disposeCallBack);
            //Knockout.BindingHandlers["validationCore"].Init(element, valueAccessor, allBindingsAccessor, null, null);
        }
        public XrmDateEditor(EditorArguments args) : base(args)
        {
            _container = jQuery.FromHtml("<div ><table class='inline-edit-container' cellspacing='0' cellpadding='0'><tr>" +
                                         "<td><INPUT type=text class='sparkle-input-inline' /></td>" +
                                         "<td class='lookup-button-td'><input type=button class='sparkle-imagestrip-inlineedit_calendar_icon' /></td></tr></table></div>");
            _container.AppendTo(_args.Container);

            _input = _container.Find(".sparkle-input-inline");
            jQueryObject selectButton = _container.Find(".sparkle-imagestrip-inlineedit_calendar_icon");


            _input.Focus().Select();
            DatePickerOptions  options  = new DatePickerOptions();
            DatePickerOptions2 options2 = (DatePickerOptions2)(object)options;

            options2.BeforeShow = delegate()
            {
                this._calendarOpen = true;
            };

            options2.OnClose = delegate()
            {
                this._calendarOpen = false;
                _selectedValue     = GetSelectedValue();
            };

            if (OrganizationServiceProxy.UserSettings != null)
            {
                _dateFormat = OrganizationServiceProxy.UserSettings.DateFormatString;
            }

            options.DateFormat = _dateFormat;

            _input.Plugin <DatePickerObject>().DatePicker(options);

            // Wire up the date picker button
            selectButton.Click(delegate(jQueryEvent e){
                _input.Plugin <DatePickerPlugIn>().DatePicker(DatePickerMethod2.Show);
            });

            //_input.Width(_input.GetWidth() - 24);
        }
Exemple #10
0
        public override void Init(System.Html.Element element, Func <object> valueAccessor, Func <System.Collections.Dictionary> allBindingsAccessor, object viewModel, object context)
        {
            jQueryObject        inputField = jQuery.FromElement(element);
            AutoCompleteOptions options    = (AutoCompleteOptions)((object)allBindingsAccessor()["autocompleteOptions"]);

            options.Select = delegate(jQueryEvent e, AutoCompleteSelectEvent uiEvent)
            {
                // Note we assume that the binding has added an array of string items
                string value = ((Dictionary)uiEvent.Item)["value"].ToString();

                TrySetObservable(valueAccessor, inputField, value);
            };

            inputField = inputField.Plugin <AutoCompleteObject>().AutoComplete(options);
            jQueryObject selectButton = inputField.Siblings(".timeSelectButton");

            // Add the click binding to show the drop down
            selectButton.Click(delegate(jQueryEvent e)
            {
                inputField.Plugin <AutoCompleteObject>().AutoComplete(AutoCompleteMethod.Search);
            });

            //handle the field changing
            KnockoutUtils.RegisterEventHandler(element, "change", delegate(object sender, EventArgs e)
            {
                string value = inputField.GetValue();
                TrySetObservable(valueAccessor, inputField, value);
            });

            Action disposeCallBack = delegate()
            {
                Script.Literal("$({0}).autocomplete(\"destroy\")", element);
            };

            //handle disposal (if KO removes by the template binding)
            Script.Literal("ko.utils.domNodeDisposal.addDisposeCallback({0}, {1})", element, (object)disposeCallBack);
            Knockout.BindingHandlers["validationCore"].Init(element, valueAccessor, allBindingsAccessor, null, null);
        }
        public PopupMenuButton(jQueryObject div, PopupMenuButtonOptions opt)
            : base(div, opt)
        {
            var self = this;

            div.AddClass("s-PopupMenuButton");

            div.Click((jQueryEvent e) =>
            {
                e.PreventDefault();
                e.StopPropagation();

                if (options.OnPopup != null)
                {
                    options.OnPopup();
                }

                var menu = self.options.Menu;

                menu.Show().As <dynamic>().position(new
                {
                    my = options.PositionMy ?? "left top",
                    at = options.PositionAt ?? "left bottom",
                    of = self.element
                });

                var uq = self.uniqueName;

                jQuery.Document.One("click." + uq, x =>
                {
                    menu.Hide();
                });
            });

            self.options.Menu.Hide().AppendTo(Document.Body).AddClass("s-PopupMenu")
            .As <dynamic>().menu();
        }
        public void constructPagerUI()
        {
            _container.Empty();

            jQueryObject pager = jQuery.FromHtml("<table cellspacing='0' cellpadding='0' class='sparkle-grid-status'><tbody><tr>" +
                                                 "<td class='sparkle-grid-status-label'>1 - 1 of 1&nbsp;(0 selected)</td>" +
                                                 "<td class='sparkle-grid-status-paging'>" +
                                                 "<img src='../../sparkle_/css/images/transparent_spacer.gif' class='sparkle-grid-paging-first'>" +
                                                 "<img src='../../sparkle_/css/images/transparent_spacer.gif' class='sparkle-grid-paging-back'>" +
                                                 "<span class='sparkle-grid-status-paging-page'>Page 1</span>" +
                                                 "<img src='../../sparkle_/css/images/transparent_spacer.gif' class='sparkle-grid-paging-next'>" +
                                                 "&nbsp;</td></tr></tbody></table>");

            jQueryObject firstButton = pager.Find(".sparkle-grid-paging-first");
            jQueryObject backButton  = pager.Find(".sparkle-grid-paging-back");
            jQueryObject nextButton  = pager.Find(".sparkle-grid-paging-next");
            jQueryObject label       = pager.Find(".sparkle-grid-status-label");
            jQueryObject pageInfo    = pager.Find(".sparkle-grid-status-paging-page");

            _container.Append(pager);
            firstButton.Click(gotoFirst);
            backButton.Click(gotoPrev);
            nextButton.Click(gotoNext);
        }
        private void LinkFn(FloatingWindowScope scope, jQueryObject element, dynamic attr)
        {
            myElement = element;
            myScope = scope;
            items.Add(element, scope);

            element.Click((elem, @event) => Focus());

            scope.Parent.SwingAway = (a, b, c) => { SwingAway(a, b, element, c); };
            scope.Parent.SwingBack = (c) => { SwingBack(scope, element, c); };
            scope.Parent.Minimize = () =>
            {
                scope.Parent.Minimized = true;
                scope.Minimize();
            };
            scope.Parent.DestroyWindow = () =>
            {
                scope.Destroy();
                element.Remove();
            };


            scope.PositionStyles = new FloatingWindowPosition() { Left = scope.Left, Top = scope.Top, Display = "block" };
            scope.PositionStyles.ZIndex = 10000;

            if (scope.Left.IndexOf("%") != -1)
            {
                scope.PositionStyles.MarginLeft = (-(int.Parse(scope.Width.Replace("px", "")) / 2)) + "px";
            }
            if (scope.Top.IndexOf("%") != -1)
            {
                scope.PositionStyles.MarginTop = (-(int.Parse(scope.Height.Replace("px", "")) / 2)) + "px";
            }


            scope.SizeStyle = new Size() { Width = scope.Width, Height = scope.Height, };
            scope.Maximize = () =>
            {
                if (!scope.IsMaximized)
                {
                    scope.LastPositionStyles = scope.PositionStyles;
                    scope.LastSizeStyle = scope.SizeStyle;
                    scope.PositionStyles = new FloatingWindowPosition()
                    {
                        Left = "0",
                        Top = "0",
                        Display = "block"
                    };
                    scope.SizeStyle = new Size() { Width = "100%", Height = "100%", };
                }
                else
                {
                    scope.PositionStyles = scope.LastPositionStyles;
                    scope.SizeStyle = scope.LastSizeStyle;
                    scope.LastPositionStyles = null;
                    scope.LastSizeStyle = null;
                }

                scope.IsMaximized = !scope.IsMaximized;
            };
            scope.Close = () =>
            {
                if (scope.OnClose != null)
                {
                    scope.OnClose();
                }
                if (scope.Parent.OnClose != null)
                {
                    scope.Parent.OnClose();
                }
                //todo destroy
                scope.PositionStyles.Display = "none";
            };
            scope.Minimize = () =>
            {
//                myUIManagerService.OnMinimize(scope);
                scope.Parent.SwingAway(SwingDirection.Bottom,
                    false,
                    () => { scope.PositionStyles.Display = "none"; });
            };
            scope.Restore = () =>
            {
                scope.Parent.SwingBack(null);
                scope.PositionStyles.Display = "block";
            };
            Focus();

            if (scope.Parent.OnReady != null)
                scope.Parent.OnReady();
        }
        public override void Init(System.Html.Element element, Func <object> valueAccessor, Func <System.Collections.Dictionary> allBindingsAccessor, object viewModel, object context)
        {
            jQueryObject        container    = jQuery.FromElement(element);
            jQueryObject        inputField   = container.Find(".sparkle-input-lookup-part");
            jQueryObject        selectButton = container.Find(".sparkle-input-lookup-button-part");
            EntityReference     _value       = new EntityReference(null, null, null);
            AutoCompleteOptions options      = new AutoCompleteOptions();

            options.MinLength = 100000; // Don't enable type down - use search button (or return)
            options.Delay     = 0;
            options.Position  = new Dictionary <string, object>("collision", "fit");

            bool justSelected = false;

            // Set the value when selected
            options.Select = delegate(jQueryEvent e, AutoCompleteSelectEvent uiEvent)
            {
                // Note we assume that the binding has added an array of string items
                AutoCompleteItem item = (AutoCompleteItem)uiEvent.Item;
                if (_value == null)
                {
                    _value = new EntityReference(null, null, null);
                }
                string value = item.Label;
                inputField.Value(value);
                _value.Id          = ((Guid)item.Value);
                _value.Name        = item.Label;
                _value.LogicalName = (string)item.Data;
                justSelected       = true;
                TrySetObservable(valueAccessor, inputField, _value);
                Script.Literal("return false;");
            };


            // Get the query command
            Action <string, Action <EntityCollection> > queryCommand = (Action <string, Action <EntityCollection> >)((object)allBindingsAccessor()["queryCommand"]);
            string nameAttribute     = ((string)allBindingsAccessor()["nameAttribute"]);
            string idAttribute       = ((string)allBindingsAccessor()["idAttribute"]);
            string typeCodeAttribute = ((string)allBindingsAccessor()["typeCodeAttribute"]);

            // wire up source to CRM search
            Action <AutoCompleteRequest, Action <AutoCompleteItem[]> > queryDelegate = delegate(AutoCompleteRequest request, Action <AutoCompleteItem[]> response)
            {
                Action <EntityCollection> queryCallBack = delegate(EntityCollection fetchResult)
                {
                    AutoCompleteItem[] results = new AutoCompleteItem[fetchResult.Entities.Count];

                    for (int i = 0; i < fetchResult.Entities.Count; i++)
                    {
                        results[i]       = new AutoCompleteItem();
                        results[i].Label = (string)fetchResult.Entities[i].GetAttributeValue(nameAttribute);
                        results[i].Value = fetchResult.Entities[i].GetAttributeValue(idAttribute);
                        results[i].Data  = fetchResult.Entities[i].LogicalName;
                        string typeCodeName = fetchResult.Entities[i].LogicalName;
                        // Get the type code from the name to find the icon
                        if (!string.IsNullOrEmpty(typeCodeAttribute))
                        {
                            typeCodeName = fetchResult.Entities[i].GetAttributeValue(typeCodeAttribute).ToString();
                        }

                        results[i].Image = MetadataCache.GetSmallIconUrl(typeCodeName);
                    }


                    response(results);

                    // Disable it now so typing doesn't trigger a search
                    AutoCompleteOptions disableOption = new AutoCompleteOptions();
                    disableOption.MinLength = 100000;
                    inputField.Plugin <AutoCompleteObject>().AutoComplete(disableOption);
                };

                // Call the function with the correct 'this' context
                Script.Literal("{0}.call({1}.$parent,{2},{3})", queryCommand, context, request.Term, queryCallBack);
            };

            options.Source = queryDelegate;
            options.Focus  = delegate(jQueryEvent e, AutoCompleteFocusEvent uiEvent)
            {
                // Prevent the value being updated in the text box we scroll through the results
                Script.Literal("return false;");
            };
            inputField = inputField.Plugin <AutoCompleteObject>().AutoComplete(options);

            // Set render template
            ((RenderItemDelegate)Script.Literal("{0}.data('ui-autocomplete')", inputField))._renderItem = delegate(object ul, AutoCompleteItem item)
            {
                return((object)jQuery.Select("<li>").Append("<a class='sparkle-menu-item'><span class='sparkle-menu-item-img'><img src='" + item.Image + "'/></span><span class='sparkle-menu-item-label'>" + item.Label + "</span></a>").AppendTo((jQueryObject)ul));
            };

            // Add the click binding to show the drop down
            selectButton.Click(delegate(jQueryEvent e)
            {
                AutoCompleteOptions enableOption = new AutoCompleteOptions();
                enableOption.MinLength           = 0;
                inputField.Focus();
                inputField.Plugin <AutoCompleteObject>().AutoComplete(enableOption);

                inputField.Plugin <AutoCompleteObject>().AutoComplete(AutoCompleteMethod.Search);
            });

            // handle the field changing
            inputField.Change(delegate(jQueryEvent e)
            {
                if (inputField.GetValue() != _value.Name)
                {
                    TrySetObservable(valueAccessor, inputField, null);
                }
            });

            Action disposeCallBack = delegate()
            {
                if ((bool)Script.Literal("$({0}).data('ui-autocomplete')!=undefined", inputField))
                {
                    //inputField.Plugin<AutoCompleteObject>().AutoComplete(AutoCompleteMethod.Destroy);
                    Script.Literal("$({0}).autocomplete(\"destroy\")", inputField);
                }
            };

            //handle disposal (if KO removes by the template binding)
            Script.Literal("ko.utils.domNodeDisposal.addDisposeCallback({0}, {1})", element, (object)disposeCallBack);
            Knockout.BindingHandlers["validationCore"].Init(element, valueAccessor, allBindingsAccessor, null, null);



            // Bind return to searching
            inputField.Keydown(delegate(jQueryEvent e)
            {
                if (e.Which == 13 && !justSelected) // Return pressed - but we want to do a search not move to the next cell
                {
                    selectButton.Click();
                }
                else if (e.Which == 13)
                {
                    return;
                }
                switch (e.Which)
                {
                case 13:     // Return
                case 38:     // Up - don't navigate - but use the dropdown to select search results
                case 40:     // Down - don't navigate - but use the dropdown to select search results
                    e.PreventDefault();
                    e.StopPropagation();
                    break;
                }
                justSelected = false;
            });

            //Script.Literal("return { controlsDescendantBindings: true };");
        }
        private void LinkFn(FloatingWindowScope scope, jQueryObject element, dynamic attr)
        {
            myElement = element;
            myScope   = scope;
            items.Add(element, scope);

            element.Click((elem, @event) => Focus());

            scope.Parent.SwingAway = (a, b, c) => { SwingAway(a, b, element, c); };
            scope.Parent.SwingBack = (c) => { SwingBack(scope, element, c); };
            scope.Parent.Minimize  = () =>
            {
                scope.Parent.Minimized = true;
                scope.Minimize();
            };
            scope.Parent.DestroyWindow = () =>
            {
                scope.Destroy();
                element.Remove();
            };


            scope.PositionStyles = new FloatingWindowPosition()
            {
                Left = scope.Left, Top = scope.Top, Display = "block"
            };
            scope.PositionStyles.ZIndex = 10000;

            if (scope.Left.IndexOf("%") != -1)
            {
                scope.PositionStyles.MarginLeft = (-(int.Parse(scope.Width.Replace("px", "")) / 2)) + "px";
            }
            if (scope.Top.IndexOf("%") != -1)
            {
                scope.PositionStyles.MarginTop = (-(int.Parse(scope.Height.Replace("px", "")) / 2)) + "px";
            }


            scope.SizeStyle = new Size()
            {
                Width = scope.Width, Height = scope.Height,
            };
            scope.Maximize = () =>
            {
                if (!scope.IsMaximized)
                {
                    scope.LastPositionStyles = scope.PositionStyles;
                    scope.LastSizeStyle      = scope.SizeStyle;
                    scope.PositionStyles     = new FloatingWindowPosition()
                    {
                        Left    = "0",
                        Top     = "0",
                        Display = "block"
                    };
                    scope.SizeStyle = new Size()
                    {
                        Width = "100%", Height = "100%",
                    };
                }
                else
                {
                    scope.PositionStyles     = scope.LastPositionStyles;
                    scope.SizeStyle          = scope.LastSizeStyle;
                    scope.LastPositionStyles = null;
                    scope.LastSizeStyle      = null;
                }

                scope.IsMaximized = !scope.IsMaximized;
            };
            scope.Close = () =>
            {
                if (scope.OnClose != null)
                {
                    scope.OnClose();
                }
                if (scope.Parent.OnClose != null)
                {
                    scope.Parent.OnClose();
                }
                //todo destroy
                scope.PositionStyles.Display = "none";
            };
            scope.Minimize = () =>
            {
//                myUIManagerService.OnMinimize(scope);
                scope.Parent.SwingAway(SwingDirection.Bottom,
                                       false,
                                       () => { scope.PositionStyles.Display = "none"; });
            };
            scope.Restore = () =>
            {
                scope.Parent.SwingBack(null);
                scope.PositionStyles.Display = "block";
            };
            Focus();

            if (scope.Parent.OnReady != null)
            {
                scope.Parent.OnReady();
            }
        }
Exemple #16
0
        public override void Init(System.Html.Element element, Func <object> valueAccessor, Func <System.Collections.Dictionary> allBindingsAccessor, object viewModel, object context)
        {
            XrmLookupEditorButton footerButton = (XrmLookupEditorButton)allBindingsAccessor()["footerButton"];
            bool                showFooter     = (bool)allBindingsAccessor()["showFooter"];
            jQueryObject        container      = jQuery.FromElement(element);
            jQueryObject        inputField     = container.Find(".sparkle-input-lookup-part");
            jQueryObject        selectButton   = container.Find(".sparkle-input-lookup-button-part");
            EntityReference     _value         = new EntityReference(null, null, null);
            AutoCompleteOptions options        = new AutoCompleteOptions();

            options.MinLength = 100000; // Don't enable type down - use search button (or return)
            options.Delay     = 0;
            options.Position  = new Dictionary <string, object>("collision", "fit");
            bool justSelected         = false;
            int  totalRecordsReturned = 0;
            Action <AutoCompleteItem, bool> setValue = delegate(AutoCompleteItem item, bool setFocus)
            {
                if (_value == null)
                {
                    _value = new EntityReference(null, null, null);
                }
                string value = item.Label;
                inputField.Value(value);
                _value.Id          = ((Guid)item.Value);
                _value.Name        = item.Label;
                _value.LogicalName = (string)item.Data;
                justSelected       = true;
                TrySetObservable(valueAccessor, inputField, _value, setFocus);
            };

            // Set the value when selected
            options.Select = delegate(jQueryEvent e, AutoCompleteSelectEvent uiEvent)
            {
                // Note we assume that the binding has added an array of string items
                AutoCompleteItem item = (AutoCompleteItem)uiEvent.Item;
                string           data = ((string)item.Data);
                if (data == "footerlink" || data == null)
                {
                    footerButton.OnClick(item);
                    e.PreventDefault();
                    e.StopImmediatePropagation();
                    Script.Literal("return false;");
                }
                else
                {
                    setValue(item, true);
                    Script.Literal("return false;");
                }
            };

            options.Open = delegate(jQueryEvent e, jQueryObject o)
            {
                if (showFooter && totalRecordsReturned > 0)
                {
                    WidgetObject menu = (WidgetObject)Script.Literal("{0}.autocomplete({1})", inputField, "widget");
                    XrmLookupEditor.AddFooter(menu, totalRecordsReturned);
                }
            };

            options.Close = delegate(jQueryEvent e, jQueryObject o)
            {
                WidgetObject menu   = (WidgetObject)Script.Literal("{0}.autocomplete({1})", inputField, "widget");
                jQueryObject footer = menu.Next();
                if (footer.Length > 0 || footer.HasClass("sparkle-menu-footer"))
                {
                    footer.Hide();
                }
            };
            // Get the query command
            Action <string, Action <EntityCollection> > queryCommand = (Action <string, Action <EntityCollection> >)((object)allBindingsAccessor()["queryCommand"]);
            string nameAttribute     = ((string)allBindingsAccessor()["nameAttribute"]);
            string idAttribute       = ((string)allBindingsAccessor()["idAttribute"]);
            string typeCodeAttribute = ((string)allBindingsAccessor()["typeCodeAttribute"]);

            string[] columnAttributes = null;
            // If there multiple names, add them to the columnAttributes
            string[] columns = nameAttribute.Split(",");

            if (columns.Length > 1)
            {
                columnAttributes = columns;
                nameAttribute    = columnAttributes[0];
            }

            // wire up source to CRM search
            Action <AutoCompleteRequest, Action <AutoCompleteItem[]> > queryDelegate = delegate(AutoCompleteRequest request, Action <AutoCompleteItem[]> response)
            {
                Action <EntityCollection> queryCallBack = delegate(EntityCollection fetchResult)
                {
                    int  recordsFound          = fetchResult.Entities.Count;
                    bool noRecordsFound        = recordsFound == 0;
                    AutoCompleteItem[] results = new AutoCompleteItem[recordsFound + (footerButton != null ? 1 : 0) + (noRecordsFound ? 1 : 0)];

                    for (int i = 0; i < recordsFound; i++)
                    {
                        results[i]       = new AutoCompleteItem();
                        results[i].Label = (string)fetchResult.Entities[i].GetAttributeValue(nameAttribute);
                        results[i].Value = fetchResult.Entities[i].GetAttributeValue(idAttribute);
                        results[i].Data  = fetchResult.Entities[i].LogicalName;
                        GetExtraColumns(columnAttributes, fetchResult, results, i);

                        string typeCodeName = fetchResult.Entities[i].LogicalName;
                        // Get the type code from the name to find the icon
                        if (!string.IsNullOrEmpty(typeCodeAttribute))
                        {
                            typeCodeName = fetchResult.Entities[i].GetAttributeValue(typeCodeAttribute).ToString();
                        }

                        results[i].Image = MetadataCache.GetSmallIconUrl(typeCodeName);
                    }

                    if (fetchResult.TotalRecordCount > fetchResult.Entities.Count)
                    {
                        totalRecordsReturned = fetchResult.TotalRecordCount;
                    }
                    else
                    {
                        totalRecordsReturned = fetchResult.Entities.Count;
                    }
                    int itemsCount = recordsFound;
                    if (noRecordsFound)
                    {
                        AutoCompleteItem noRecordsItem = new AutoCompleteItem();
                        noRecordsItem.Label = SparkleResourceStrings.NoRecordsFound;
                        results[itemsCount] = noRecordsItem;
                        itemsCount++;
                    }
                    if (footerButton != null)
                    {
                        // Add the add new
                        AutoCompleteItem addNewLink = new AutoCompleteItem();
                        addNewLink.Label        = footerButton.Label;
                        addNewLink.Image        = footerButton.Image;
                        addNewLink.ColumnValues = null;
                        addNewLink.Data         = "footerlink";
                        results[itemsCount]     = addNewLink;
                    }
                    response(results);

                    // Disable it now so typing doesn't trigger a search
                    AutoCompleteOptions disableOption = new AutoCompleteOptions();
                    disableOption.MinLength = 100000;
                    inputField.Plugin <AutoCompleteObject>().AutoComplete(disableOption);
                };

                // Call the function with the correct 'this' context
                Script.Literal("{0}.call({1}.$parent,{2},{3})", queryCommand, context, request.Term, queryCallBack);
            };

            options.Source = queryDelegate;
            options.Focus  = delegate(jQueryEvent e, AutoCompleteFocusEvent uiEvent)
            {
                // Prevent the value being updated in the text box we scroll through the results
                Script.Literal("return false;");
            };
            inputField = inputField.Plugin <AutoCompleteObject>().AutoComplete(options);

            // Set render template
            ((RenderItemDelegate)Script.Literal("{0}.data('ui-autocomplete')", inputField))._renderItem = delegate(object ul, AutoCompleteItem item)
            {
                if (item.Data == null)
                {
                    return((object)jQuery.Select("<li class='ui-state-disabled'>" + item.Label + "</li>").AppendTo((jQueryObject)ul));
                }

                string html = "<a class='sparkle-menu-item'><span class='sparkle-menu-item-img'>";
                if (item.Image != null)
                {
                    html += @"<img src='" + item.Image + "'/>";
                }
                html += @"</span><span class='sparkle-menu-item-label'>" + item.Label + "</span><br>";

                if (item.ColumnValues != null && item.ColumnValues.Length > 0)
                {
                    foreach (string value in item.ColumnValues)
                    {
                        html += "<span class='sparkle-menu-item-moreinfo'>" + value + "</span>";
                    }
                }
                html += "</a>";
                return((object)jQuery.Select("<li>").Append(html).AppendTo((jQueryObject)ul));
            };

            // Add the click binding to show the drop down
            selectButton.Click(delegate(jQueryEvent e)
            {
                AutoCompleteOptions enableOption = new AutoCompleteOptions();
                enableOption.MinLength           = 0;
                inputField.Focus();
                inputField.Plugin <AutoCompleteObject>().AutoComplete(enableOption);
                inputField.Plugin <AutoCompleteObject>().AutoComplete(AutoCompleteMethod.Search);
            });

            // handle the field changing
            inputField.Change(delegate(jQueryEvent e)
            {
                string inputValue = inputField.GetValue();
                if (inputValue != _value.Name)
                {
                    // The name is different from the name of the lookup reference
                    // search to see if we can auto resolve it
                    TrySetObservable(valueAccessor, inputField, null, false);
                    AutoCompleteRequest lookup = new AutoCompleteRequest();
                    lookup.Term = inputValue;
                    Action <AutoCompleteItem[]> lookupResults = delegate(AutoCompleteItem[] results)
                    {
                        int selectableItems = 0;
                        // If there is only one, then auto-set
                        if (results != null)
                        {
                            foreach (AutoCompleteItem item in results)
                            {
                                if (isItemSelectable(item))
                                {
                                    selectableItems++;
                                }
                                if (selectableItems > 2)
                                {
                                    break;
                                }
                            }
                        }

                        if (selectableItems == 1)
                        {
                            // There is only a single value so set it now
                            setValue(results[0], false);
                        }
                        else
                        {
                            inputField.Value(String.Empty);
                        }
                    };

                    queryDelegate(lookup, lookupResults);
                }
            });

            Action disposeCallBack = delegate()
            {
                if ((bool)Script.Literal("$({0}).data('ui-autocomplete')!=undefined", inputField))
                {
                    Script.Literal("$({0}).autocomplete(\"destroy\")", inputField);
                }
            };

            //handle disposal (if KO removes by the template binding)
            Script.Literal("ko.utils.domNodeDisposal.addDisposeCallback({0}, {1})", element, (object)disposeCallBack);
            Knockout.BindingHandlers["validationCore"].Init(element, valueAccessor, allBindingsAccessor, null, null);

            // Bind return to searching
            inputField.Keydown(delegate(jQueryEvent e)
            {
                if (e.Which == 13 && !justSelected) // Return pressed - but we want to do a search not move to the next cell
                {
                    selectButton.Click();
                }
                else if (e.Which == 13)
                {
                    return;
                }
                switch (e.Which)
                {
                case 13:     // Return
                case 38:     // Up - don't navigate - but use the dropdown to select search results
                case 40:     // Down - don't navigate - but use the dropdown to select search results
                    e.PreventDefault();
                    e.StopPropagation();
                    break;
                }
                justSelected = false;
            });

            //Script.Literal("return { controlsDescendantBindings: true };");
        }
        public override void  Init(System.Html.Element element, Func <object> valueAccessor, Func <System.Collections.Dictionary> allBindingsAccessor, object viewModel, object context)
        {
            jQueryObject container  = jQuery.FromElement(element);
            jQueryObject dateTime   = container.Find(".sparkle-input-datepicker-part");
            jQueryObject dateButton = container.Find(".sparkle-input-datepicker-button-part");
            // Add Date Picker
            DatePickerOptions2 options = new DatePickerOptions2();

            options.ShowOn          = "";
            options.ButtonImageOnly = true;

            options.FirstDay = OrganizationServiceProxy.OrganizationSettings != null ? OrganizationServiceProxy.OrganizationSettings.WeekStartDayCode.Value.Value : 0;
            //options.ButtonImage = @"../images/btn_off_Cal.gif";

            string dateFormat = "dd/MM/yy";

            if (OrganizationServiceProxy.UserSettings != null)
            {
                dateFormat = OrganizationServiceProxy.UserSettings.DateFormatString;
            }
            options.DateFormat = dateFormat;


            dateTime.Plugin <DatePickerPlugIn>().DatePicker(options);
            //// Get current value
            //Observable<DateTime> dateValueAccessor = (Observable<DateTime>)valueAccessor();
            //DateTime intialValue = dateValueAccessor.GetValue();
            //dateTime.Plugin<DatePickerObject>().DatePicker(DatePickerMethod.SetDate, intialValue);

            dateButton.Click(delegate(jQueryEvent e)
            {
                // Note: This is using a custom plugin definition since the standard didn't include show
                dateTime.Plugin <DatePickerPlugIn>().DatePicker(DatePickerMethod2.Show);
            });



            //handle the field changing
            KnockoutUtils.RegisterEventHandler(dateTime.GetElement(0), "change", delegate(object sender, EventArgs e)
            {
                Observable <DateTime> observable = (Observable <DateTime>)valueAccessor();


                bool isValid = true;

                if (((string)Script.Literal("typeof({0}.IsValid)", observable)) != "undefined")
                {
                    isValid = ((IValidatedObservable)observable).IsValid() == true;
                }

                if (isValid)
                {
                    DateTime selectedDate = (DateTime)dateTime.Plugin <DatePickerObject>().DatePicker(DatePickerMethod.GetDate);
                    // Get Current observable value - we only want to set the date part
                    DateTime currentValue = observable.GetValue();
                    DateTimeEx.SetTime(selectedDate, currentValue);
                    observable.SetValue(selectedDate);
                }
                dateTime.Blur();
            });

            Action disposeCallBack = delegate() {
                Script.Literal("$({0}).datepicker(\"destroy\")", element);
            };

            //handle disposal (if KO removes by the template binding)
            Script.Literal("ko.utils.domNodeDisposal.addDisposeCallback({0}, {1})", element, (object)disposeCallBack);

            //Knockout.BindingHandlers["validationCore"].Init(element, valueAccessor, allBindingsAccessor,null,null);
        }
Exemple #18
0
        public XrmTimeEditor(EditorArguments args) : base(args)
        {
            bool justSelected = false;

            XrmTimeEditor self = this;

            if (OrganizationServiceProxy.UserSettings != null)
            {
                _formatString = OrganizationServiceProxy.UserSettings.TimeFormatString;
            }


            _container = jQuery.FromHtml("<div ><table class='inline-edit-container' cellspacing='0' cellpadding='0'><tr><td><INPUT type=text class='sparkle-input-inline' /></td><td class='lookup-button-td'><input type=button class='autocompleteButton' /></td></tr></table></div>");

            _container.AppendTo(_args.Container);

            jQueryObject inputField = _container.Find(".sparkle-input-inline");

            _input = inputField;
            _input.Focus().Select();
            string timeFormatString     = _formatString;
            AutoCompleteOptions options = GetTimePickerAutoCompleteOptions(timeFormatString);

            options.Select = delegate(jQueryEvent e, AutoCompleteSelectEvent uiEvent)
            {
                justSelected = true;
            };
            options.Open = delegate(jQueryEvent e, jQueryObject o)
            {
                self._searchOpen = true;
            };

            options.Close = delegate(jQueryEvent e, jQueryObject o)
            {
                self._searchOpen = false;
            };

            inputField = inputField.Plugin <AutoCompleteObject>().AutoComplete(options);
            jQueryObject selectButton = _container.Find(".autocompleteButton");

            // Add the click binding to show the drop down
            selectButton.Click(delegate(jQueryEvent e)
            {
                inputField.Plugin <AutoCompleteObject>().AutoComplete(AutoCompleteMethod.Search, "");
            });

            // Bind return to searching

            _input.Keydown(delegate(jQueryEvent e)
            {
                if (self._searchOpen)
                {
                    switch (e.Which)
                    {
                    case 13:     // Return
                    case 38:     // Up - don't navigate - but use the dropdown to select search results
                    case 40:     // Down - don't navigate - but use the dropdown to select search results
                        e.PreventDefault();
                        e.StopPropagation();
                        break;
                    }
                }

                justSelected = false;
            });
        }
        public XrmLookupEditor(EditorArguments args) : base(args)
        {
            _args      = args;
            _container = jQuery.FromHtml("<div ><table class='inline-edit-container' cellspacing='0' cellpadding='0'><tr><td><INPUT type=text class='sparkle-input-inline' /></td><td class='lookup-button-td'><input type=button class='sparkle-lookup-button' /></td></tr></table></div>");
            _container.AppendTo(_args.Container);

            jQueryObject inputField   = _container.Find(".sparkle-input-inline");
            jQueryObject selectButton = _container.Find(".sparkle-lookup-button");

            _input = inputField;
            _input.Focus().Select();

            _autoComplete = inputField.Plugin <AutoCompleteObject>();

            AutoCompleteOptions options = new AutoCompleteOptions();

            options.MinLength = 100000;
            options.Delay     = 0; // TODO- set to something that makes sense

            bool justSelected = false;

            options.Select = delegate(jQueryEvent e, AutoCompleteSelectEvent uiEvent)
            {
                if (_value == null)
                {
                    _value = new EntityReference(null, null, null);
                }

                // Note we assume that the binding has added an array of string items
                AutoCompleteItem item  = (AutoCompleteItem)uiEvent.Item;
                string           value = item.Label;
                _input.Value(value);
                _value.Id          = ((EntityReference)item.Value).Id;
                _value.Name        = ((EntityReference)item.Value).Name;
                _value.LogicalName = ((EntityReference)item.Value).LogicalName;
                justSelected       = true;
                Script.Literal("return false;");
            };
            //
            options.Focus = delegate(jQueryEvent e, AutoCompleteFocusEvent uiEvent)
            {
                // Prevent the value being updated in the text box we scroll through the results
                Script.Literal("return false;");
            };

            XrmLookupEditorOptions editorOptions = (XrmLookupEditorOptions)args.Column.Options;

            // wire up source to CRM search
            Action <AutoCompleteRequest, Action <AutoCompleteItem[]> > queryDelegate = delegate(AutoCompleteRequest request, Action <AutoCompleteItem[]> response)
            {
                // Get the option set values
                editorOptions.queryCommand(request.Term, delegate(EntityCollection fetchResult)
                {
                    AutoCompleteItem[] results = new AutoCompleteItem[fetchResult.Entities.Count];

                    for (int i = 0; i < fetchResult.Entities.Count; i++)
                    {
                        results[i]         = new AutoCompleteItem();
                        results[i].Label   = (string)fetchResult.Entities[i].GetAttributeValue(editorOptions.nameAttribute);
                        EntityReference id = new EntityReference(null, null, null);
                        id.Name            = results[i].Label;
                        id.LogicalName     = fetchResult.Entities[i].LogicalName;
                        id.Id            = (Guid)fetchResult.Entities[i].GetAttributeValue(editorOptions.idAttribute);
                        results[i].Value = id;

                        string typeCodeName = fetchResult.Entities[i].LogicalName;

                        // Get the type code from the name to find the icon
                        if (!string.IsNullOrEmpty(editorOptions.typeCodeAttribute))
                        {
                            typeCodeName = fetchResult.Entities[i].GetAttributeValue(editorOptions.typeCodeAttribute).ToString();
                        }

                        results[i].Image = MetadataCache.GetSmallIconUrl(typeCodeName);
                    }

                    response(results);

                    // Disable it now so typing doesn't trigger a search
                    AutoCompleteOptions disableOption = new AutoCompleteOptions();
                    disableOption.MinLength           = 100000;
                    _autoComplete.AutoComplete(disableOption);
                });
            };

            options.Source = queryDelegate;
            inputField     = _autoComplete.AutoComplete(options);
            ((RenderItemDelegate)Script.Literal("{0}.data('ui-autocomplete')", inputField))._renderItem = delegate(object ul, AutoCompleteItem item)
            {
                return((object)jQuery.Select("<li>").Append("<a class='sparkle-menu-item'><span class='sparkle-menu-item-img'><img src='" + item.Image + "'/></span><span class='sparkle-menu-item-label'>" + item.Label + "</span></a>").AppendTo((jQueryObject)ul));
            };

            // Add the click binding to show the drop down
            selectButton.Click(delegate(jQueryEvent e)
            {
                AutoCompleteOptions enableOption = new AutoCompleteOptions();
                enableOption.MinLength           = 0;
                _autoComplete.AutoComplete(enableOption);
                _autoComplete.AutoComplete(AutoCompleteMethod.Search, inputField.GetValue());
            });

            // Bind return to searching
            _input.Keydown(delegate(jQueryEvent e)
            {
                if (e.Which == 13 && !justSelected) // Return pressed - but we want to do a search not move to the next cell
                {
                    if (inputField.GetValue().Length > 0)
                    {
                        selectButton.Click();
                    }
                    else
                    {
                        // Set value to null
                        _value = null;
                        return;
                    }
                }
                else if (e.Which == 13)
                {
                    return;
                }
                switch (e.Which)
                {
                case 13:     // Return
                case 38:     // Up - don't navigate - but use the dropdown to select search results
                case 40:     // Down - don't navigate - but use the dropdown to select search results
                    e.PreventDefault();
                    e.StopPropagation();
                    break;
                }
                justSelected = false;
            });
        }
        public XrmLookupEditor(EditorArguments args) : base(args)
        {
            XrmLookupEditor self = this;

            _args      = args;
            _container = jQuery.FromHtml("<div><table class='inline-edit-container' cellspacing='0' cellpadding='0'><tr><td><INPUT type=text class='sparkle-input-inline' /></td><td class='lookup-button-td'><input type=button class='sparkle-lookup-button' /></td></tr></table></div>");
            _container.AppendTo(_args.Container);

            jQueryObject inputField   = _container.Find(".sparkle-input-inline");
            jQueryObject selectButton = _container.Find(".sparkle-lookup-button");

            _input = inputField;
            _input.Focus().Select();

            _autoComplete = inputField.Plugin <AutoCompleteObject>();

            AutoCompleteOptions options = new AutoCompleteOptions();

            options.Position  = new Dictionary <string, object>("collision", "fit");
            options.MinLength = 100000;
            options.Delay     = 0; // TODO- set to something that makes sense
            XrmLookupEditorOptions editorOptions = (XrmLookupEditorOptions)args.Column.Options;

            bool justSelected = false;

            options.Select = delegate(jQueryEvent e, AutoCompleteSelectEvent uiEvent)
            {
                if (_value == null)
                {
                    _value = new EntityReference(null, null, null);
                }

                // Note we assume that the binding has added an array of string items
                AutoCompleteItem item    = (AutoCompleteItem)uiEvent.Item;
                EntityReference  itemRef = (EntityReference)item.Value;
                if (itemRef.LogicalName == "footerlink")
                {
                    XrmLookupEditorButton button = editorOptions.footerButton;
                    button.OnClick(item);
                }
                else
                {
                    string value = item.Label;
                    _input.Value(value);
                    _value.Id          = itemRef.Id;
                    _value.Name        = itemRef.Name;
                    _value.LogicalName = ((EntityReference)item.Value).LogicalName;
                    justSelected       = true;
                }
                Script.Literal("return false;");
            };

            options.Focus = delegate(jQueryEvent e, AutoCompleteFocusEvent uiEvent)
            {
                // Prevent the value being updated in the text box as we scroll through the results
                Script.Literal("return false;");
            };

            options.Open = delegate(jQueryEvent e, jQueryObject o)
            {
                self._searchOpen = true;
                if (editorOptions.showFooter && totalRecordsReturned > 0)
                {
                    WidgetObject menu = (WidgetObject)Script.Literal("{0}.autocomplete({1})", _input, "widget");
                    AddFooter(menu, totalRecordsReturned);
                }
            };

            options.Close = delegate(jQueryEvent e, jQueryObject o)
            {
                self._searchOpen = false;
                WidgetObject menu   = (WidgetObject)Script.Literal("{0}.autocomplete({1})", _input, "widget");
                jQueryObject footer = menu.Next();
                if (footer.Length > 0 || footer.HasClass("sparkle-menu-footer"))
                {
                    footer.Hide();
                }
            };

            // If there multiple names, add them to the columnAttributes
            string[] columns = editorOptions.nameAttribute.Split(",");

            if (columns.Length > 1)
            {
                editorOptions.columns       = columns;
                editorOptions.nameAttribute = columns[0];
            }

            // wire up source to CRM search
            Action <AutoCompleteRequest, Action <AutoCompleteItem[]> > queryDelegate = delegate(AutoCompleteRequest request, Action <AutoCompleteItem[]> response)
            {
                // Get the option set values
                editorOptions.queryCommand(request.Term, delegate(EntityCollection fetchResult)
                {
                    if (fetchResult.TotalRecordCount > fetchResult.Entities.Count)
                    {
                        totalRecordsReturned = fetchResult.TotalRecordCount;
                    }
                    else
                    {
                        totalRecordsReturned = fetchResult.Entities.Count;
                    }

                    int recordsFound             = fetchResult.Entities.Count;
                    bool noRecordsFound          = recordsFound == 0;
                    XrmLookupEditorButton button = editorOptions.footerButton;
                    bool footerButton            = (button != null);

                    AutoCompleteItem[] results = new AutoCompleteItem[recordsFound + (footerButton ? 1 : 0) + (noRecordsFound ? 1 :0)];

                    for (int i = 0; i < recordsFound; i++)
                    {
                        results[i]         = new AutoCompleteItem();
                        results[i].Label   = (string)fetchResult.Entities[i].GetAttributeValue(editorOptions.nameAttribute);
                        EntityReference id = new EntityReference(null, null, null);
                        id.Name            = results[i].Label;
                        id.LogicalName     = fetchResult.Entities[i].LogicalName;
                        id.Id            = (Guid)fetchResult.Entities[i].GetAttributeValue(editorOptions.idAttribute);
                        results[i].Value = id;
                        XrmLookupBinding.GetExtraColumns(editorOptions.columns, fetchResult, results, i);
                        string typeCodeName = fetchResult.Entities[i].LogicalName;

                        // Get the type code from the name to find the icon
                        if (!string.IsNullOrEmpty(editorOptions.typeCodeAttribute))
                        {
                            typeCodeName = fetchResult.Entities[i].GetAttributeValue(editorOptions.typeCodeAttribute).ToString();
                        }

                        if (editorOptions.showImage)
                        {
                            results[i].Image = MetadataCache.GetSmallIconUrl(typeCodeName);
                        }
                    }

                    int itemsCount = recordsFound;
                    if (noRecordsFound)
                    {
                        AutoCompleteItem noRecordsItem = new AutoCompleteItem();
                        noRecordsItem.Label            = SparkleResourceStrings.NoRecordsFound;
                        results[itemsCount]            = noRecordsItem;
                        itemsCount++;
                    }

                    if (footerButton)
                    {
                        // Add the add new
                        AutoCompleteItem addNewLink = new AutoCompleteItem();
                        addNewLink.Label            = button.Label;
                        addNewLink.Image            = button.Image;
                        addNewLink.ColumnValues     = null;
                        addNewLink.Value            = new Entity("footerlink");
                        results[itemsCount]         = addNewLink;
                    }
                    response(results);

                    // Disable it now so typing doesn't trigger a search
                    AutoCompleteOptions disableOption = new AutoCompleteOptions();
                    disableOption.MinLength           = 100000;
                    _autoComplete.AutoComplete(disableOption);
                });
            };

            options.Source = queryDelegate;
            inputField     = _autoComplete.AutoComplete(options);
            RenderItemDelegate autoCompleteDelegates = ((RenderItemDelegate)Script.Literal("{0}.data('ui-autocomplete')", inputField));

            autoCompleteDelegates._renderItem = delegate(object ul, AutoCompleteItem item)
            {
                if (item.Value == item.Label)
                {
                    return((object)jQuery.Select("<li class='ui-state-disabled'>" + item.Label + "</li>").AppendTo((jQueryObject)ul));
                }

                string itemHtml = "<a class='sparkle-menu-item'>";
                // Allow for no image by passing false to 'ShowImage' on the XrmLookupEditorOptions options
                if (item.Image != null)
                {
                    itemHtml += "<span class='sparkle-menu-item-img'><img src='" + item.Image + "'/></span>";
                }
                itemHtml += "<span class='sparkle-menu-item-label'>" + item.Label + "</span><br/>";
                if (item.ColumnValues != null && item.ColumnValues.Length > 0)
                {
                    foreach (string value in item.ColumnValues)
                    {
                        itemHtml += "<span class='sparkle-menu-item-moreinfo'>" + value + "</span>";
                    }
                }
                itemHtml += "</a>";
                return((object)jQuery.Select("<li>").Append(itemHtml).AppendTo((jQueryObject)ul));
            };

            // Add the click binding to show the drop down
            selectButton.Click(delegate(jQueryEvent e)
            {
                AutoCompleteOptions enableOption = new AutoCompleteOptions();
                enableOption.MinLength           = 0;
                _autoComplete.AutoComplete(enableOption);
                _autoComplete.AutoComplete(AutoCompleteMethod.Search, inputField.GetValue());
            });

            // Bind return to searching
            _input.Keydown(delegate(jQueryEvent e)
            {
                if (e.Which == 13 && !justSelected) // Return pressed - but we want to do a search not move to the next cell
                {
                    if (inputField.GetValue().Length > 0)
                    {
                        selectButton.Click();
                    }
                    else
                    {
                        // Set value to null
                        _value = null;
                        return;
                    }
                }
                else if (e.Which == 13)
                {
                    return;
                }
                if (self._searchOpen)
                {
                    switch (e.Which)
                    {
                    case 9:
                    case 13:     // Return
                    case 38:     // Up - don't navigate - but use the dropdown to select search results
                    case 40:     // Down - don't navigate - but use the dropdown to select search results
                        e.PreventDefault();
                        e.StopPropagation();
                        break;
                    }
                }
                else
                {
                    switch (e.Which)
                    {
                    case 13:     // Return
                        e.PreventDefault();
                        e.StopPropagation();
                        break;
                    }
                }
                justSelected = false;
            });
        }
        private void LinkFn(CanvasTilePieceScope scope, jQueryObject element, dynamic attr)
        {
            element.Width(scope.Width);
            element.Height(scope.Height);
            if (scope.Edit)
            {
                element.Click(ev =>
                              {
                                  TilePieceInfo tilePieceInfo = scope.TilePiece;

                                  if (tilePieceInfo == null) return;
                                  var x = ev.OffsetX;
                                  var y = ev.OffsetY;


                                  var _x = (int)(x / ((double)scope.Width / TileChunk.Size));
                                  var _y = (int)(y / ((double)scope.Height / TileChunk.Size));

                                  //                                  scope.DebugDrawOptions.OutlineTilePiece = scope.TileChunk.GetTilePieceInfo(_x, _y, true);

                              });
            }
            var zero = new Point(0, 0);

            var context = (CanvasRenderingContext2D)((CanvasElement)element[0]).GetContext(CanvasContextId.Render2D);
            Action updateTilePiece = () =>
                                 {

                                     if (scope.TilePiece == null) return;
                                     var tilePiece = scope.TilePiece.GetTilePiece();

                                     context.Canvas.Width = context.Canvas.Width;

                                     context.Me().webkitImageSmoothingEnabled = false;
                                     context.Me().mozImageSmoothingEnabled = false;
                                     context.Me().imageSmoothingEnabled = false;

                                     context.Scale(scope.Width / ((double)TileChunk.TileSideLength), scope.Height / ((double)TileChunk.TileSideLength));
                                     tilePiece.DrawBase(context, zero, ChunkLayer.Low, false, false);
                                     tilePiece.DrawBase(context, zero, ChunkLayer.High, false, false);

                                     for (int index = 0; index < tilePiece.AnimatedPaletteIndexes.Count; index++)
                                     {
                                         var animatedPaletteIndex = tilePiece.AnimatedPaletteIndexes[index];
                                         tilePiece.DrawAnimatedPalette(context, zero, ChunkLayer.Low, false, false, animatedPaletteIndex);
                                         tilePiece.DrawAnimatedPalette(context, zero, ChunkLayer.High, false, false, animatedPaletteIndex);
                                     }


                                     for (int index = 0; index < tilePiece.AnimatedTileIndexes.Count; index++)
                                     {
                                         var animatedTileIndex = tilePiece.AnimatedTileIndexes[index];
                                         tilePiece.DrawAnimatedTile(context, zero, ChunkLayer.Low, false, false, animatedTileIndex);
                                         tilePiece.DrawAnimatedTile(context, zero, ChunkLayer.High, false, false, animatedTileIndex);
                                     }
                                 
                                      

                                 };

            scope.Watch("tilePiece", updateTilePiece);

            Window.SetInterval(() =>
                               {
/*
                                   if (tilePieceInfo == null) return;
                                   if (!tilePieceInfo.ShouldAnimate())
                                   {
                                       if (scope.ShouldAnimate)
                                       {
                                           updateTilePiece();
                                       }
                                       else
                                           scope.Digest();
                                   }
*/
                               }, 1000 / 60);
        }
        private void LinkFn(CanvasTileChunkScope scope, jQueryObject element, dynamic attr)
        {
            element.Width(scope.Width);
            element.Height(scope.Height);
            if (scope.Edit)
            {
                element.Click(ev =>
                              {
                                  if (scope.TileChunk == null) return;
                                  var x = ev.OffsetX;
                                  var y = ev.OffsetY;


                                  var _x = (int)(x / ((double)scope.Width / TileChunk.Size));
                                  var _y = (int)(y / ((double)scope.Height / TileChunk.Size));

                                  scope.DebugDrawOptions.OutlineTilePiece = scope.TileChunk.GetTilePieceInfo(_x, _y, true);

                              });
            }
            var context = (CanvasRenderingContext2D)((CanvasElement)element[0]).GetContext(CanvasContextId.Render2D);
            Action updateTileChunk = () =>
                                 {
                                     if (scope.TileChunk == null) return;
                                     var drawOptions = scope.DrawOptions ?? DefaultDrawOptions;

                                     context.Canvas.Width = context.Canvas.Width;

                                     context.Me().webkitImageSmoothingEnabled = false;
                                     context.Me().mozImageSmoothingEnabled = false;
                                     context.Me().imageSmoothingEnabled = false;

                                     context.Scale(scope.Width / ((double)TileChunk.Size), scope.Height / ((double)TileChunk.Size));
                                     if (drawOptions.ShowLowLayer && !scope.TileChunk.OnlyForeground())
                                         scope.TileChunk.Draw(context, new Point(0, 0), ChunkLayer.Low);
                                     if (drawOptions.ShowHighLayer && !scope.TileChunk.OnlyBackground())
                                         scope.TileChunk.Draw(context, new Point(0, 0), ChunkLayer.High);

                                     if (scope.DebugDrawOptions != null)
                                     {
                                         if (drawOptions.ShowLowLayer && !scope.TileChunk.OnlyForeground())
                                             scope.TileChunk.DrawAnimationDebug(context, new Point(0, 0), ChunkLayer.Low, scope.DebugDrawOptions);
                                         if (drawOptions.ShowHighLayer && !scope.TileChunk.OnlyBackground())
                                             scope.TileChunk.DrawAnimationDebug(context, new Point(0, 0), ChunkLayer.High, scope.DebugDrawOptions);
                                     }
                                 };

            scope.Watch("tileChunk", updateTileChunk);
            scope.Watch("drawOptions", updateTileChunk, true);
            scope.Watch("debugDrawOptions", updateTileChunk, true);
            scope.Watch("tileChunk.currentTileAnimationFrameIndexCache", updateTileChunk, true);
            scope.Watch("tileChunk.currentPaletteAnimationFrameIndexCache", updateTileChunk, true);

            Window.SetInterval(() =>
                               {
                                   if (scope.TileChunk == null) return;
                                   if (!scope.TileChunk.NeverAnimates())
                                   {
                                       if (scope.ShouldAnimate)
                                       {
                                           updateTileChunk();
                                       }
                                       else
                                           scope.Digest();
                                   }
                               }, 1000 / 60);
        }