Ejemplo n.º 1
0
        private static void TrySetObservable(Func <object> valueAccessor, jQueryObject inputField, string value)
        {
            Observable <int?> observable = (Observable <int?>)valueAccessor();
            bool isValid = true;

            bool isEmpty = (value == null) || (value.Length == 0);

            // Get the value as minutes decimal
            // ([0-9]*) ((h(our)?[s]?)|(m(inute)?[s]?)|(d(ay)?[s]?))
            string            pattern = @"/([0-9]*)[ ]?((h(our)?[s]?)|(m(inute)?[s]?)|(d(ay)?[s]?))/g";
            RegularExpression regex   = RegularExpression.Parse(pattern);

            string[] match = regex.Exec(value);
            if (isEmpty)
            {
                observable.SetValue(null);
            }
            else if (match != null && match.Length > 0)
            {
                // Get value
                decimal durationNumber = decimal.Parse(match[1]);
                switch (match[2].Substr(0, 1).ToLowerCase())
                {
                case "d":
                    durationNumber = durationNumber * 60 * 24;
                    break;

                case "h":
                    durationNumber = durationNumber * 60;
                    break;
                }

                observable.SetValue((int?)durationNumber);

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

                if (isValid)
                {
                    inputField.Blur();
                }
            }
            else
            {
                Script.Alert("Invalid Duration Format");
                int?   currentValue   = observable.GetValue();
                string durationString = formatDuration(currentValue);

                inputField.Value(durationString);
                inputField.Focus();
            }
        }
Ejemplo n.º 2
0
        private static void TrySetObservable(Func <object> valueAccessor, jQueryObject inputField, EntityReference value)
        {
            Observable <EntityReference> observable = (Observable <EntityReference>)valueAccessor();
            bool isValid = true;

            observable.SetValue(value);

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

            if (isValid)
            {
                inputField.Blur();
            }
        }
Ejemplo n.º 3
0
        private static void TrySetObservable(Func<object> valueAccessor, jQueryObject inputField, string value)
        {
           
            Observable<string> observable = (Observable<string>)valueAccessor();
            bool isValid = true;
            observable.SetValue(value);

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

            if (isValid)
            {
                inputField.Blur();
                
            }
            
        }
Ejemplo n.º 4
0
        private static void TrySetObservable(Func <object> valueAccessor, jQueryObject inputField, string value, NumberFormatInfo format)
        {
            Observable <Money> observable = (Observable <Money>)valueAccessor();
            bool isValid = true;


            Number numericValue = NumberEx.Parse(value, format);

            if (!Number.IsNaN(numericValue) && numericValue >= format.MinValue && numericValue <= format.MaxValue)
            {
                Money newValue = null;
                if (numericValue != null) // Issue #46
                {
                    // Set to precision
                    numericValue = NumberEx.Round(numericValue, format.Precision);
                    newValue     = new Money((decimal)numericValue);
                }

                observable.SetValue(newValue);

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

                if (isValid)
                {
                    string formattedNumber = FormatNumber(newValue, format);
                    inputField.Value(formattedNumber);
                    inputField.Blur();
                }
            }
            else
            {
                Script.Alert(String.Format("You must enter a number between {0} and {1}", format.MinValue, format.MaxValue));
                Money currentValue = observable.GetValue();

                string formattedNumber = FormatNumber(currentValue, format);
                inputField.Value(formattedNumber);
                inputField.Focus();
            }
        }
Ejemplo n.º 5
0
        private static void TrySetObservable(Func <object> valueAccessor, jQueryObject inputField, string value)
        {
            Observable <DateTime> observable = (Observable <DateTime>)valueAccessor();
            bool isValid = true;

            // Test the format
            DateTime testDate = DateTimeEx.AddTimeToDate(observable.GetValue(), value);

            // Check if the value is different
            string newValue      = (testDate == null) ? "" : testDate.ToString();
            string originalValue = (observable.GetValue() == null) ? "" : observable.GetValue().ToString();

            if (newValue == originalValue)
            {
                return;
            }

            if (testDate == null)
            {
                // Invalid
                Script.Alert("Invalid Time");
                inputField.Focus();

                DateTime currentValue = observable.GetValue();
                FormatterUpdate(inputField, currentValue);
            }
            else
            {
                observable.SetValue(testDate);

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

                if (isValid)
                {
                    inputField.Blur();
                }
            }
        }
Ejemplo n.º 6
0
        private static void TrySetObservable(Func<object> valueAccessor, jQueryObject inputField, EntityReference value, bool setFocus)
        {
            Observable<EntityReference> observable = (Observable<EntityReference>)valueAccessor();
            bool isValid = true;
            observable.SetValue(value);

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

            if (isValid && setFocus)
            {
                // Ensure the field is reinitialised
                inputField.Blur();
                inputField.Focus();
            }
        }
Ejemplo n.º 7
0
        private static void TrySetObservable(Func<object> valueAccessor, jQueryObject inputField, string value)
        {

            Observable<DateTime> observable = (Observable<DateTime>)valueAccessor();
            bool isValid = true;

            // Test the format
            DateTime testDate = DateTimeEx.AddTimeToDate(observable.GetValue(), value);
            
            // Check if the value is different
            string newValue = (testDate==null) ? "" : testDate.ToString();
            string originalValue = (observable.GetValue() == null) ? "" : observable.GetValue().ToString();

            if (newValue == originalValue)
                return;

            if (testDate == null)
            {
                // Invalid
                Script.Alert("Invalid Time");
                inputField.Focus();
               
                DateTime currentValue = observable.GetValue();
                FormatterUpdate(inputField, currentValue);
            }
            else
            {
                observable.SetValue(testDate);

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

                if (isValid)
                {
                    inputField.Blur();

                }
            }
        }
Ejemplo n.º 8
0
        private static void TrySetObservable(Func<object> valueAccessor, jQueryObject inputField, string value, NumberFormatInfo format)
        {

            Observable<Money> observable = (Observable<Money>)valueAccessor();
            bool isValid = true;


            Number numericValue = NumberEx.Parse(value, format);

            if (!Number.IsNaN(numericValue) && numericValue>=format.MinValue && numericValue<=format.MaxValue)
            {
                // Set to precision
                numericValue = NumberEx.Round(numericValue, format.Precision);
                Money newValue = new Money((decimal)numericValue);
                
                observable.SetValue(newValue);

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

                if (isValid)
                {
                    string formattedNumber = FormatNumber(newValue, format);
                    inputField.Value(formattedNumber);
                    inputField.Blur();
                }
            }
            else
            {
                Script.Alert(String.Format("You must enter a number between {0} and {1}",format.MinValue,format.MaxValue));
                Money currentValue = observable.GetValue();
              
                string formattedNumber = FormatNumber(currentValue, format);
                inputField.Value(formattedNumber);
                inputField.Focus();
            }

        }
        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);
        }
Ejemplo n.º 10
0
        private static void TrySetObservable(Func<object> valueAccessor, jQueryObject inputField, string value)
        {
            
            Observable<int?> observable = (Observable<int?>)valueAccessor();
            bool isValid = true;

            bool isEmpty = (value==null) || (value.Length==0);
            
            // Get the value as minutes decimal
            // ([0-9]*) ((h(our)?[s]?)|(m(inute)?[s]?)|(d(ay)?[s]?))
            string pattern = @"/([0-9]*)[ ]?((h(our)?[s]?)|(m(inute)?[s]?)|(d(ay)?[s]?))/g";
            RegularExpression regex = RegularExpression.Parse(pattern);
            string[] match = regex.Exec(value);
            if (isEmpty)
            {
                observable.SetValue(null);
            }
            else if (match!=null && match.Length > 0)
            {
                // Get value
                decimal durationNumber = decimal.Parse(match[1]);
                switch (match[2].Substr(0, 1).ToLowerCase())
                {
                    case "d":
                        durationNumber = durationNumber * 60 * 24;
                        break;
                    case "h":
                        durationNumber = durationNumber *60;
                        break;
                }

                observable.SetValue((int?)durationNumber);

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

                if (isValid)
                {
                    inputField.Blur();
                }
            }
            else
            {
                Script.Alert("Invalid Duration Format");
                int? currentValue = observable.GetValue();
                string durationString = formatDuration(currentValue);

                inputField.Value(durationString);
                inputField.Focus();
            }
            
        }