Beispiel #1
0
        /// <summary>
        /// Renders the contents inside the control begin and end tags.
        /// </summary>
        protected override void RenderContents(IHtmlWriter writer, IDotvvmRequestContext context)
        {
            if (RenderOnServer)
            {
                // render on server
                foreach (var child in Children.Except(new[] { emptyDataContainer }))
                {
                    child.Render(writer, context);
                }
            }
            else
            {
                // render on client
                var placeholder = new DataItemContainer()
                {
                    DataContext = null
                };
                placeholder.SetValue(Internal.PathFragmentProperty, GetPathFragmentExpression() + "/[$index]");
                //placeholder.SetBinding(DataContextProperty, GetValueBinding(DataSourceProperty).CastTo<ValueBindingExpression>().MakeKoContextIndexer());
                placeholder.SetValue(Internal.ClientIDFragmentProperty, GetValueRaw(Internal.CurrentIndexBindingProperty));
                ItemTemplate.BuildContent(context, placeholder);
                Children.Add(placeholder);

                placeholder.Render(writer, context);
            }
        }
Beispiel #2
0
        /// <summary>
        /// Renders the contents inside the control begin and end tags.
        /// </summary>
        protected override void RenderContents(IHtmlWriter writer, RenderContext context)
        {
            if (RenderOnServer)
            {
                // render on server
                foreach (var child in Children.Except(new[] { emptyDataContainer }))
                {
                    child.Render(writer, context);
                }
            }
            else
            {
                // render on client
                var placeholder = new DataItemContainer()
                {
                    DataContext = null
                };
                placeholder.SetValue(Internal.PathFragmentProperty, JavascriptCompilationHelper.AddIndexerToViewModel(GetPathFragmentExpression(), "$index"));
                placeholder.SetValue(Internal.ClientIDFragmentProperty, "'i' + $index()");
                ItemTemplate.BuildContent(context.RequestContext, placeholder);
                Children.Add(placeholder);

                placeholder.Render(writer, context);
            }
        }
Beispiel #3
0
        private DotvvmControl GetItem(IDotvvmRequestContext context, object item = null, int?index = null, bool allowMemoizationRetrive = false, bool allowMemoizationStore = false)
        {
            if (allowMemoizationRetrive && item != null && childrenCache.TryGetValue(item, out var container2) && container2.Parent == null)
            {
                Debug.Assert(item == container2.GetValueRaw(DataContextProperty));
                SetUpServerItem(context, item, (int)index, container2);
                return(container2);
            }

            var container = new DataItemContainer();

            container.SetDataContextTypeFromDataSource(GetBinding(DataSourceProperty));
            if (item == null && index == null)
            {
                SetUpClientItem(container);
            }
            else
            {
                SetUpServerItem(context, item, (int)index, container);
            }

            ItemTemplate.BuildContent(context, container);

            // write it to the cache after the content is build. If it would be before that, exception could be suppressed
            if (allowMemoizationStore && item != null)
            {
                // this GetValue call adds the value without raising exception when the value is already added...
                childrenCache.GetValue(item, _ => container);
            }

            return(container);
        }
Beispiel #4
0
        private void CreateRowWithCells(Hosting.IDotvvmRequestContext context, DataItemContainer placeholder)
        {
            var isInEditMode = false;

            if (InlineEditing)
            {
                // if gridviewdataset is missing throw exception
                if (!(DataSource is IGridViewDataSet))
                {
                    throw new ArgumentException("You have to use GridViewDataSet with InlineEditing enabled.");
                }

                //checks if row is being edited
                isInEditMode = IsEditedRow(placeholder);
            }

            var row = CreateRow(placeholder, isInEditMode);

            // create cells
            foreach (var column in Columns)
            {
                var cell = new HtmlGenericControl("td");
                SetCellAttributes(column, cell, false);
                row.Children.Add(cell);

                if (isInEditMode && column.IsEditable)
                {
                    column.CreateEditControls(context, cell);
                }
                else
                {
                    column.CreateControls(context, cell);
                }
            }
        }
Beispiel #5
0
        private void DataBind(IDotvvmRequestContext context)
        {
            Children.Clear();
            emptyDataContainer = null;
            head = null;

            var dataSourceBinding = GetDataSourceBinding();
            var dataSource        = DataSource;

            var sortCommand = SortChanged;

            sortCommand ??=
            typeof(ISortableGridViewDataSet).IsAssignableFrom((GetBinding(DataSourceProperty) as IStaticValueBinding)?.ResultType)
                  ? (Action <string?>)SortChangedCommand
                  : null;

            sortCommand ??= s =>
            throw new DotvvmControlException(this, "Cannot sort when DataSource is null.");

            CreateHeaderRow(context, sortCommand);

            var index = 0;

            if (dataSource != null)
            {
                foreach (var item in GetIEnumerableFromDataSource() !)
                {
                    // create row
                    var placeholder = new DataItemContainer {
                        DataItemIndex = index
                    };
                    placeholder.SetDataContextTypeFromDataSource(dataSourceBinding);
                    placeholder.DataContext = item;
                    placeholder.SetValue(Internal.PathFragmentProperty, GetPathFragmentExpression() + "/[" + index + "]");
                    placeholder.ID = index.ToString();
                    Children.Add(placeholder);
                    CreateRowWithCells(context, placeholder);

                    index++;
                }
                numberOfRows = index;
            }
            else
            {
                numberOfRows = 0;
            }

            // add empty item
            if (EmptyDataTemplate != null)
            {
                emptyDataContainer = new EmptyData();
                emptyDataContainer.SetValue(EmptyData.VisibleProperty, GetValueRaw(VisibleProperty));
                emptyDataContainer.SetBinding(DataSourceProperty, dataSourceBinding);
                EmptyDataTemplate.BuildContent(context, emptyDataContainer);
                Children.Add(emptyDataContainer);
            }
        }
Beispiel #6
0
        private bool IsEditedRow(DataItemContainer placeholder)
        {
            var value = ReflectionUtils.GetObjectProperty(placeholder.DataContext, ((IGridViewDataSet)DataSource).PrimaryKeyPropertyName);

            if (value != null)
            {
                if (value.Equals(((IGridViewDataSet)DataSource).EditRowId))
                {
                    return(true);
                }
            }

            return(false);
        }
Beispiel #7
0
        /// <summary>
        /// Performs the data-binding and builds the controls inside the <see cref="Repeater"/>.
        /// </summary>
        private void DataBind(IDotvvmRequestContext context)
        {
            Children.Clear();
            emptyDataContainer = null;

            var dataSourceBinding = GetDataSourceBinding();
            var index             = 0;
            var dataSource        = DataSource;

            if (dataSource != null)
            {
                var itemBinding    = GetItemBinding();
                var bindingService = context.Configuration.ServiceLocator.GetService <BindingCompilationService>();
                foreach (var item in GetIEnumerableFromDataSource())
                {
                    var placeholder = new DataItemContainer {
                        DataItemIndex = index
                    };
                    ItemTemplate.BuildContent(context, placeholder);
                    placeholder.SetBinding(DataContextProperty, ValueBindingExpression.CreateBinding(
                                               bindingService,
                                               j => item,
                                               itemBinding.KnockoutExpression.AssignParameters(p =>
                                                                                               p == JavascriptTranslator.CurrentIndexParameter ? new CodeParameterAssignment(index.ToString(), OperatorPrecedence.Max) :
                                                                                               default(CodeParameterAssignment))));
                    placeholder.SetValue(Internal.PathFragmentProperty, GetPathFragmentExpression() + "/[" + index + "]");
                    placeholder.ID = index.ToString();
                    Children.Add(placeholder);
                    index++;
                }
                numberOfRows = index;
            }
            else
            {
                numberOfRows = 0;
            }

            // add empty item
            if (EmptyDataTemplate != null)
            {
                emptyDataContainer = new EmptyData();
                emptyDataContainer.SetValue(EmptyData.RenderWrapperTagProperty, GetValueRaw(RenderWrapperTagProperty));
                emptyDataContainer.SetValue(EmptyData.WrapperTagNameProperty, GetValueRaw(WrapperTagNameProperty));
                emptyDataContainer.SetBinding(DataSourceProperty, dataSourceBinding);
                EmptyDataTemplate.BuildContent(context, emptyDataContainer);
                Children.Add(emptyDataContainer);
            }
        }
Beispiel #8
0
        private DotvvmControl GetItem(IDotvvmRequestContext context, object item = null, int index = -1, IValueBinding itemBinding = null)
        {
            var container = new DataItemContainer();

            if (item == null)
            {
                SetUpClientItem(container);
            }
            else
            {
                SetUpServerItem(context, item, index, itemBinding, container);
            }

            ItemTemplate.BuildContent(context, container);
            return(container);
        }
Beispiel #9
0
        private bool IsEditedRow(DataItemContainer placeholder)
        {
            var property = ResolvePrimaryKeyProperty();
            var value    = property.GetValue(placeholder.DataContext);

            if (value != null)
            {
                var editRowId = ((IGridViewDataSet)DataSource !).RowEditOptions.EditRowId;
                if (editRowId != null && value.Equals(ReflectionUtils.ConvertValue(editRowId, property.PropertyType)))
                {
                    return(true);
                }
            }

            return(false);
        }
Beispiel #10
0
        private DotvvmControl GetItem(IDotvvmRequestContext context, object item = null, int?index = null)
        {
            var container = new DataItemContainer();

            container.SetDataContextTypeFromDataSource(GetBinding(DataSourceProperty));
            if (item == null && index == null)
            {
                SetUpClientItem(container);
            }
            else
            {
                SetUpServerItem(context, item, (int)index, container);
            }

            ItemTemplate.BuildContent(context, container);
            return(container);
        }
Beispiel #11
0
        private void CreateRow(IDotvvmRequestContext context, DataItemContainer placeholder)
        {
            var row = new HtmlGenericControl("tr");

            DotvvmControl container = row;

            foreach (var decorator in RowDecorators)
            {
                var decoratorInstance = decorator.Clone();
                decoratorInstance.Children.Add(container);
                container = decoratorInstance;
            }
            placeholder.Children.Add(container);

            var isEdit = false;

            if (InlineEditing == true)
            {
                // if gridviewdataset is missing throw exception
                if (!(DataSource is IGridViewDataSet))
                {
                    throw new ArgumentException("You have to use GridViewDataSet with InlineEditing enabled.");
                }
                //checks if row is being edited
                isEdit = IsEditedRow(placeholder);
            }

            // create cells
            foreach (var column in Columns)
            {
                var cell = new HtmlGenericControl("td");
                SetCellAttributes(column, cell, false);
                row.Children.Add(cell);

                if (isEdit && column.IsEditable)
                {
                    column.CreateEditControls(context, cell);
                }
                else
                {
                    column.CreateControls(context, cell);
                }
            }
        }
Beispiel #12
0
        /// <summary>
        /// Performs the data-binding and builds the controls inside the <see cref="Repeater"/>.
        /// </summary>
        private void DataBind(IDotvvmRequestContext context)
        {
            Children.Clear();
            emptyDataContainer = null;

            var dataSourceBinding = GetDataSourceBinding();
            var index             = 0;
            var dataSource        = DataSource;

            if (dataSource != null)
            {
                var items = GetIEnumerableFromDataSource(dataSource).Cast <object>().ToArray();
                var javascriptDataSourceExpression = dataSourceBinding.GetKnockoutBindingExpression();
                foreach (var item in items)
                {
                    var placeholder = new DataItemContainer {
                        DataItemIndex = index
                    };
                    ItemTemplate.BuildContent(context, placeholder);
                    placeholder.SetBinding(DataContextProperty, GetItemBinding((IList)items, javascriptDataSourceExpression, index));
                    placeholder.SetValue(Internal.PathFragmentProperty, JavascriptCompilationHelper.AddIndexerToViewModel(GetPathFragmentExpression(), index));
                    placeholder.ID = index.ToString();
                    Children.Add(placeholder);
                    index++;
                }
                numberOfRows = index;
            }
            else
            {
                numberOfRows = 0;
            }

            // add empty item
            if (EmptyDataTemplate != null)
            {
                emptyDataContainer = new EmptyData();
                emptyDataContainer.SetValue(EmptyData.RenderWrapperTagProperty, GetValueRaw(RenderWrapperTagProperty));
                emptyDataContainer.SetValue(EmptyData.WrapperTagNameProperty, GetValueRaw(WrapperTagNameProperty));
                emptyDataContainer.SetBinding(DataSourceProperty, dataSourceBinding);
                EmptyDataTemplate.BuildContent(context, emptyDataContainer);
                Children.Add(emptyDataContainer);
            }
        }
Beispiel #13
0
        private HtmlGenericControl CreateRow(DataItemContainer placeholder, bool isInEditMode)
        {
            var row = new HtmlGenericControl("tr");

            DotvvmControl container  = row;
            var           decorators = isInEditMode ? EditRowDecorators : RowDecorators;

            if (decorators != null)
            {
                foreach (var decorator in decorators)
                {
                    var decoratorInstance = decorator.Clone();
                    decoratorInstance.Children.Add(container);
                    container = decoratorInstance;
                }
            }
            placeholder.Children.Add(container);
            return(row);
        }
Beispiel #14
0
        private void CreateTemplates(Hosting.IDotvvmRequestContext context, DataItemContainer placeholder, bool isInEditMode = false)
        {
            var row = CreateRow(placeholder, isInEditMode);

            // create cells
            foreach (var column in Columns)
            {
                var cell = new HtmlGenericControl("td");
                row.Children.Add(cell);
                SetCellAttributes(column, cell, false);
                if (isInEditMode && column.IsEditable)
                {
                    column.CreateEditControls(context, cell);
                }
                else
                {
                    column.CreateControls(context, cell);
                }
            }
        }
Beispiel #15
0
        private void CreateTemplates(IDotvvmRequestContext context, DataItemContainer placeholder, bool isInEditMode = false)
        {
            var row = CreateRow(placeholder, isInEditMode);

            // create cells
            foreach (var column in Columns.NotNull("GridView.Columns must be set"))
            {
                var cell = new HtmlGenericControl("td");
                cell.SetValue(Internal.DataContextTypeProperty, column.GetValueRaw(Internal.DataContextTypeProperty));
                row.Children.Add(cell);
                SetCellAttributes(column, cell, false);
                if (isInEditMode && column.IsEditable)
                {
                    column.CreateEditControls(context, cell);
                }
                else
                {
                    column.CreateControls(context, cell);
                }
            }
        }
Beispiel #16
0
        private bool IsEditedRow(DataItemContainer placeholder)
        {
            var primaryKeyPropertyName = ((IGridViewDataSet)DataSource).RowEditOptions.PrimaryKeyPropertyName;

            if (string.IsNullOrEmpty(primaryKeyPropertyName))
            {
                throw new DotvvmControlException(this, $"The {nameof(IGridViewDataSet)} must specify the {nameof(IRowEditOptions.PrimaryKeyPropertyName)} property when inline editing is enabled on the {nameof(GridView)} control!");
            }

            PropertyInfo prop;
            var          value = ReflectionUtils.GetObjectPropertyValue(placeholder.DataContext, primaryKeyPropertyName, out prop);

            if (value != null)
            {
                var editRowId = ((IGridViewDataSet)DataSource).RowEditOptions.EditRowId;
                if (editRowId != null && value.Equals(ReflectionUtils.ConvertValue(editRowId, prop.PropertyType)))
                {
                    return(true);
                }
            }

            return(false);
        }
Beispiel #17
0
        private void CreateTemplates(IDotvvmRequestContext context, DataItemContainer placeholder, bool createEditTemplates = false)
        {
            var row = new HtmlGenericControl("tr");

            DotvvmControl container = row;

            placeholder.Children.Add(container);


            // create cells
            foreach (var column in Columns)
            {
                var cell = new HtmlGenericControl("td");
                row.Children.Add(cell);
                if (createEditTemplates && column.IsEditable)
                {
                    column.CreateEditControls(context, cell);
                }
                else
                {
                    column.CreateControls(context, cell);
                }
            }
        }
Beispiel #18
0
        protected override void RenderContents(IHtmlWriter writer, RenderContext context)
        {
            if (Children.Count == 0) return;

            // render the header
            Children[0].Render(writer, context);

            // render body
            var dataSourceBinding = GetDataSourceBinding();
            if (!RenderOnServer)
            {
                var expression = dataSourceBinding.GetKnockoutBindingExpression();
                writer.AddKnockoutForeachDataBind(expression);
            }
            writer.RenderBeginTag("tbody");

            // render contents
            if (RenderOnServer)
            {
                // render on server
                var index = 0;
                foreach (var child in Children.Skip(1))
                {
                    Children[index].Render(writer, context);
                    index++;
                }
            }
            else
            {
                // render on client
                var placeholder = new DataItemContainer { DataContext = null };
                placeholder.SetValue(Internal.PathFragmentProperty, 
                    JavascriptCompilationHelper.AddIndexerToViewModel(dataSourceBinding.GetKnockoutBindingExpression(), "$index"));
                Children.Add(placeholder);

                CreateRow(context.RequestContext, placeholder);

                placeholder.Render(writer, context);
            }

            writer.RenderEndTag();
        }
Beispiel #19
0
        private void CreateRow(DotvvmRequestContext context, DataItemContainer placeholder)
        {
            var row = new HtmlGenericControl("tr");

            DotvvmControl container = row;
            foreach (var decorator in RowDecorators)
            {
                var decoratorInstance = decorator.Clone();
                decoratorInstance.Children.Add(container);
                container = decoratorInstance;
            }
            placeholder.Children.Add(container);

            // create cells
            foreach (var column in Columns)
            {
                var cell = new HtmlGenericControl("td");
                SetCellAttributes(column, cell, false);
                row.Children.Add(cell);
                column.CreateControls(context, cell);
            }
        }
Beispiel #20
0
        private void SetUpServerItem(IDotvvmRequestContext context, object item, int index, IValueBinding itemBinding, DataItemContainer container)
        {
            container.DataItemIndex = index;
            var bindingService = context.Configuration.ServiceLocator.GetService <BindingCompilationService>();

            container.SetBinding(DataContextProperty, ValueBindingExpression.CreateBinding(
                                     bindingService.WithoutInitialization(),
                                     j => item,
                                     itemBinding.KnockoutExpression.AssignParameters(p =>
                                                                                     p == JavascriptTranslator.CurrentIndexParameter ? new CodeParameterAssignment(index.ToString(), OperatorPrecedence.Max) :
                                                                                     default(CodeParameterAssignment))));
            container.SetValue(Internal.PathFragmentProperty, GetPathFragmentExpression() + "/[" + index + "]");
            container.ID = index.ToString();
        }
Beispiel #21
0
        private void DataBind(DotvvmRequestContext context)
        {
            Children.Clear();

            var dataSourceBinding = GetDataSourceBinding();
            // var dataSourcePath = dataSourceBinding.GetViewModelPathExpression(this, DataSourceProperty);
            var dataSource = DataSource;

            Action<string> sortCommand = null;
            if (dataSource is IGridViewDataSet)
            {
                sortCommand = ((IGridViewDataSet)dataSource).SetSortExpression; // dataSourcePath + ".SetSortExpression";
            }
            else
            {
                var sortCommandBinding = GetCommandBinding(SortChangedProperty);
                if (sortCommandBinding != null)
                {
                    sortCommand = s => sortCommandBinding.Delegate(new []{ s }.Concat(BindingExpression.GetDataContexts(this, true)).ToArray(), null);
                }
            }

            var index = 0;
            if (dataSource != null)
            {
                // create header row
                CreateHeaderRow(context, sortCommand);
                var items = GetIEnumerableFromDataSource(dataSource);
                foreach (var item in items)
                {
                    // create row
                    var placeholder = new DataItemContainer { DataItemIndex = index };
                    placeholder.SetBinding(DataContextProperty, GetItemBinding((IList)items, dataSourceBinding.GetKnockoutBindingExpression(), index));
                    Children.Add(placeholder);

                    CreateRow(context, placeholder);

                    index++;
                }
            }
        }
Beispiel #22
0
 private void CreateTemplates(Hosting.IDotvvmRequestContext context, DataItemContainer placeholder, bool isInEditMode = false)
 {
     var row = CreateRow(placeholder, isInEditMode);
     
     // create cells
     foreach (var column in Columns)
     {
         var cell = new HtmlGenericControl("td");
         row.Children.Add(cell);
         if (isInEditMode && column.IsEditable)
         {
             column.CreateEditControls(context, cell);
         }
         else
         {
             column.CreateControls(context, cell);
         }
     }
 }
Beispiel #23
0
 private void SetUpServerItem(IDotvvmRequestContext context, object item, int index, DataItemContainer container)
 {
     container.DataItemIndex = index;
     container.DataContext   = item;
     container.SetValue(Internal.PathFragmentProperty, GetPathFragmentExpression() + "/[" + index + "]");
     container.ID = index.ToString();
 }
Beispiel #24
0
        /// <summary>
        /// Renders the contents inside the control begin and end tags.
        /// </summary>
        protected override void RenderContents(IHtmlWriter writer, IDotvvmRequestContext context)
        {
            if (RenderOnServer)
            {
                // render on server
                foreach (var child in Children.Except(new[] { emptyDataContainer }))
                {
                    child.Render(writer, context);
                }
            }
            else
            {
                // render on client
                var placeholder = new DataItemContainer() { DataContext = null };
                placeholder.SetValue(Internal.PathFragmentProperty, JavascriptCompilationHelper.AddIndexerToViewModel(GetPathFragmentExpression(), "$index"));
                placeholder.SetValue(Internal.ClientIDFragmentProperty, "$index()");
                ItemTemplate.BuildContent(context, placeholder);
                Children.Add(placeholder);

                placeholder.Render(writer, context);
            }
        }
Beispiel #25
0
        private HtmlGenericControl CreateRow(DataItemContainer placeholder, bool isInEditMode)
        {
            var row = new HtmlGenericControl("tr");

            DotvvmControl container = row;
            var decorators = isInEditMode ? EditRowDecorators : RowDecorators;
            if (decorators != null)
            {
                foreach (var decorator in decorators)
                {
                    var decoratorInstance = decorator.Clone();
                    decoratorInstance.Children.Add(container);
                    container = decoratorInstance;
                }
            }
            placeholder.Children.Add(container);
            return row;
        }
Beispiel #26
0
        protected override void RenderContents(IHtmlWriter writer, IDotvvmRequestContext context)
        {
            // render the header
            head?.Render(writer, context);

            // render body
            if (!RenderOnServer)
            {
                writer.AddKnockoutForeachDataBind("dotvvm.evaluator.getDataSourceItems($gridViewDataSet)");
            }
            writer.RenderBeginTag("tbody");

            // render contents
            if (RenderOnServer)
            {
                // render on server
                var index = 0;
                foreach (var child in Children.Except(new[] { head, emptyDataContainer }))
                {
                    child.Render(writer, context);
                    index++;
                }
            }
            else
            {
                // render on client
                if (InlineEditing)
                {
                    var propertySerialization = context.Services
                                                .GetRequiredService <IPropertySerialization>();
                    var primaryKeyProperty     = ResolvePrimaryKeyProperty();
                    var primaryKeyPropertyName = propertySerialization.ResolveName(primaryKeyProperty);

                    var placeholder = new DataItemContainer {
                        DataContext = null
                    };
                    placeholder.SetDataContextTypeFromDataSource(GetBinding(DataSourceProperty));
                    placeholder.SetValue(Internal.PathFragmentProperty, GetPathFragmentExpression() + "/[$index]");
                    placeholder.SetValue(Internal.ClientIDFragmentProperty, GetValueRaw(Internal.CurrentIndexBindingProperty));
                    writer.WriteKnockoutDataBindComment("if", "ko.unwrap(ko.unwrap($gridViewDataSet).RowEditOptions().EditRowId) " +
                                                        $"!== ko.unwrap($data['{primaryKeyPropertyName}'])");
                    CreateTemplates(context, placeholder);
                    Children.Add(placeholder);
                    placeholder.Render(writer, context);
                    writer.WriteKnockoutDataBindEndComment();

                    var placeholderEdit = new DataItemContainer {
                        DataContext = null
                    };
                    placeholderEdit.SetDataContextTypeFromDataSource(GetBinding(DataSourceProperty));
                    placeholderEdit.SetValue(Internal.PathFragmentProperty, GetPathFragmentExpression() + "/[$index]");
                    placeholderEdit.SetValue(Internal.ClientIDFragmentProperty, GetValueRaw(Internal.CurrentIndexBindingProperty));
                    writer.WriteKnockoutDataBindComment("if", "ko.unwrap(ko.unwrap($gridViewDataSet).RowEditOptions().EditRowId) " +
                                                        $"=== ko.unwrap($data['{primaryKeyPropertyName}'])");
                    CreateTemplates(context, placeholderEdit, true);
                    Children.Add(placeholderEdit);
                    placeholderEdit.Render(writer, context);
                    writer.WriteKnockoutDataBindEndComment();
                }
                else
                {
                    var placeholder = new DataItemContainer {
                        DataContext = null
                    };
                    placeholder.SetDataContextTypeFromDataSource(GetBinding(DataSourceProperty));
                    placeholder.SetValue(Internal.PathFragmentProperty, GetPathFragmentExpression() + "/[$index]");
                    placeholder.SetValue(Internal.ClientIDFragmentProperty, GetValueRaw(Internal.CurrentIndexBindingProperty));
                    Children.Add(placeholder);
                    CreateRowWithCells(context, placeholder);
                    placeholder.Render(writer, context);
                }
            }

            writer.RenderEndTag();
        }
Beispiel #27
0
        /// <summary>
        /// Performs the data-binding and builds the controls inside the <see cref="Repeater"/>.
        /// </summary>
        private void DataBind(Hosting.IDotvvmRequestContext context)
        {
            Children.Clear();
            emptyDataContainer = null;

            var dataSourceBinding = GetDataSourceBinding();
            var index = 0;
            var dataSource = DataSource;
            if (dataSource != null)
            {
                var items = GetIEnumerableFromDataSource(dataSource).Cast<object>().ToArray();
                var javascriptDataSourceExpression = dataSourceBinding.GetKnockoutBindingExpression();
                foreach (var item in items)
                {
                    var placeholder = new DataItemContainer { DataItemIndex = index };
                    ItemTemplate.BuildContent(context, placeholder);
                    placeholder.SetBinding(DataContextProperty, GetItemBinding((IList)items, javascriptDataSourceExpression, index));
                    placeholder.SetValue(Internal.PathFragmentProperty, JavascriptCompilationHelper.AddIndexerToViewModel(GetPathFragmentExpression(), index));
                    placeholder.ID = index.ToString();
                    Children.Add(placeholder);
                    index++;
                }
                numberOfRows = index;
            }
            else
            {
                numberOfRows = 0;
            }

            // add empty item
            if (EmptyDataTemplate != null)
            {
                emptyDataContainer = new EmptyData();
                emptyDataContainer.SetBinding(DataSourceProperty, dataSourceBinding);
                EmptyDataTemplate.BuildContent(context, emptyDataContainer);
                Children.Add(emptyDataContainer);
            }
        }
Beispiel #28
0
        private void DataBind(IDotvvmRequestContext context)
        {
            Children.Clear();
            emptyDataContainer = null;
            head = null;

            var dataSourceBinding = GetDataSourceBinding();
            var dataSource        = DataSource;

            var sortCommand =
                dataSource is ISortableGridViewDataSet sortableSet && sortableSet.SortingOptions is ISortingOptions sortOptions ?
                expr => {
                if (sortOptions.SortExpression == expr)
                {
                    sortOptions.SortDescending ^= true;
                }
                else
                {
                    sortOptions.SortExpression = expr;
                    sortOptions.SortDescending = false;
                }
                (sortableSet as IPageableGridViewDataSet)?.GoToFirstPage();
            }
            :
            SortChanged;

            // WORKAROUND: DataSource is null => don't throw exception
            if (sortCommand == null && dataSource == null)
            {
                sortCommand = s => {
                    throw new DotvvmControlException(this, "Cannot sort when DataSource is null.");
                };
            }

            CreateHeaderRow(context, sortCommand);

            var index = 0;

            if (dataSource != null)
            {
                var itemBinding = GetItemBinding();
                foreach (var item in GetIEnumerableFromDataSource())
                {
                    // create row
                    var placeholder = new DataItemContainer {
                        DataItemIndex = index
                    };
                    placeholder.SetDataContextTypeFromDataSource(dataSourceBinding);
                    placeholder.DataContext = item;
                    placeholder.SetValue(Internal.PathFragmentProperty, GetPathFragmentExpression() + "/[" + index + "]");
                    placeholder.ID = index.ToString();
                    Children.Add(placeholder);
                    CreateRowWithCells(context, placeholder);

                    index++;
                }
                numberOfRows = index;
            }
            else
            {
                numberOfRows = 0;
            }

            // add empty item
            if (EmptyDataTemplate != null)
            {
                emptyDataContainer = new EmptyData();
                emptyDataContainer.SetValue(EmptyData.VisibleProperty, GetValueRaw(VisibleProperty));
                emptyDataContainer.SetBinding(DataSourceProperty, dataSourceBinding);
                EmptyDataTemplate.BuildContent(context, emptyDataContainer);
                Children.Add(emptyDataContainer);
            }
        }
Beispiel #29
0
        protected override void RenderContents(IHtmlWriter writer, RenderContext context)
        {
            // render the header
            head?.Render(writer, context);

            // render body
            if (!RenderOnServer)
            {
                writer.AddKnockoutForeachDataBind("dotvvm.evaluator.getDataSourceItems($data)");
            }
            writer.RenderBeginTag("tbody");


            // render contents
            if (RenderOnServer)
            {
                // render on server
                var index = 0;
                foreach (var child in Children.Except(new[] { head, emptyDataContainer }))
                {
                    child.Render(writer, context);
                    index++;
                }
            }
            else
            {
                // render on client
                if (InlineEditing)
                {
                    var placeholder = new DataItemContainer {
                        DataContext = null
                    };
                    placeholder.SetValue(Internal.PathFragmentProperty, JavascriptCompilationHelper.AddIndexerToViewModel(GetPathFragmentExpression(), "$index"));
                    placeholder.SetValue(Internal.ClientIDFragmentProperty, "'i' + $index()");
                    writer.WriteKnockoutDataBindComment("if", "ko.unwrap($parent.EditRowId) !== ko.unwrap($data[ko.unwrap($parent.PrimaryKeyPropertyName)])");
                    CreateTemplates(context.RequestContext, placeholder);
                    Children.Add(placeholder);
                    placeholder.Render(writer, context);
                    writer.WriteKnockoutDataBindEndComment();

                    var placeholderEdit = new DataItemContainer {
                        DataContext = null
                    };
                    placeholderEdit.SetValue(Internal.PathFragmentProperty, JavascriptCompilationHelper.AddIndexerToViewModel(GetPathFragmentExpression(), "$index"));
                    placeholderEdit.SetValue(Internal.ClientIDFragmentProperty, "'i' + $index()");
                    writer.WriteKnockoutDataBindComment("if", "ko.unwrap($parent.EditRowId) === ko.unwrap($data[ko.unwrap($parent.PrimaryKeyPropertyName)])");
                    CreateTemplates(context.RequestContext, placeholderEdit, true);
                    Children.Add(placeholderEdit);
                    placeholderEdit.Render(writer, context);
                    writer.WriteKnockoutDataBindEndComment();
                }
                else
                {
                    var placeholder = new DataItemContainer {
                        DataContext = null
                    };
                    placeholder.SetValue(Internal.PathFragmentProperty, JavascriptCompilationHelper.AddIndexerToViewModel(GetPathFragmentExpression(), "$index"));
                    placeholder.SetValue(Internal.ClientIDFragmentProperty, "'i' + $index()");
                    CreateRow(context.RequestContext, placeholder);
                    Children.Add(placeholder);
                    placeholder.Render(writer, context);
                }
            }

            writer.RenderEndTag();
        }
Beispiel #30
0
        /// <summary>
        /// Performs the data-binding and builds the controls inside the <see cref="Repeater"/>.
        /// </summary>
        private void DataBind(DotvvmRequestContext context)
        {
            var dataSourceBinding = GetDataSourceBinding();

            var index = 0;
            var dataSource = DataSource;
            if (dataSource != null)
            {
                var items = GetIEnumerableFromDataSource(dataSource).Cast<object>().ToArray();
                if (lastBoundArray != null)
                {
                    if (lastBoundArray.SequenceEqual(items)) return;
                }
                Children.Clear();
                foreach (var item in items)
                {
                    var placeholder = new DataItemContainer { DataItemIndex = index };
                    ItemTemplate.BuildContent(context, placeholder);
                    Children.Add(placeholder);
                    placeholder.SetBinding(DataContextProperty, GetItemBinding((IList)items, dataSourceBinding.GetKnockoutBindingExpression(), index));
                    Debug.Assert(placeholder.properties[DataContextProperty] != null);
                    index++;
                }
            }
        }
Beispiel #31
0
        private void DataBind(Hosting.IDotvvmRequestContext context)
        {
            Children.Clear();
            emptyDataContainer = null;
            head = null;

            var dataSourceBinding = GetDataSourceBinding();
            var dataSource = DataSource;

            Action<string> sortCommand = null;
            var set = dataSource as IGridViewDataSet;
            if (set != null)
            {
                sortCommand = set.SetSortExpression;
            }
            else
            {
                sortCommand = SortChanged;
            }

            // WORKAROUND: DataSource is null => don't throw exception
            if (sortCommand == null && dataSource == null)
            {
                sortCommand = s =>
                {
                    throw new DotvvmControlException(this, "Cannot sort when DataSource is null.");
                };
            }

            CreateHeaderRow(context, sortCommand);
            var index = 0;
            if (dataSource != null)
            {
                // create header row
                var items = GetIEnumerableFromDataSource(dataSource);
                var javascriptDataSourceExpression = dataSourceBinding.GetKnockoutBindingExpression();

                foreach (var item in items)
                {
                    // create row
                    var placeholder = new DataItemContainer { DataItemIndex = index };
                    placeholder.SetBinding(DataContextProperty, GetItemBinding((IList)items, javascriptDataSourceExpression, index));
                    placeholder.SetValue(Internal.PathFragmentProperty, JavascriptCompilationHelper.AddIndexerToViewModel(GetPathFragmentExpression(), index));
                    placeholder.ID = index.ToString();
                    CreateRowWithCells(context, placeholder);
                    Children.Add(placeholder);

                    index++;
                }
                numberOfRows = index;
            }
            else
            {
                numberOfRows = 0;
            }

            // add empty item
            if (EmptyDataTemplate != null)
            {
                emptyDataContainer = new EmptyData();
                emptyDataContainer.SetBinding(DataSourceProperty, dataSourceBinding);
                EmptyDataTemplate.BuildContent(context, emptyDataContainer);
                Children.Add(emptyDataContainer);
            }
        }
Beispiel #32
0
        private void DataBind(IDotvvmRequestContext context)
        {
            Children.Clear();
            emptyDataContainer = null;
            head = null;

            var dataSourceBinding = GetDataSourceBinding();
            var dataSource        = DataSource;


            if (dataSource is IRefreshableGridViewDataSet refreshableDataSet)
            {
                CallGridViewDataSetRefreshRequest(refreshableDataSet);
            }

            var sortCommand =
                dataSource is ISortableGridViewDataSet sortableGridViewDataSet ?
                sortableGridViewDataSet.SetSortExpression :
                SortChanged;

            // WORKAROUND: DataSource is null => don't throw exception
            if (sortCommand == null && dataSource == null)
            {
                sortCommand = s => {
                    throw new DotvvmControlException(this, "Cannot sort when DataSource is null.");
                };
            }

            CreateHeaderRow(context, sortCommand);
            var index = 0;

            if (dataSource != null)
            {
                var itemBinding    = GetItemBinding();
                var bindingService = context.Configuration.ServiceLocator.GetService <BindingCompilationService>();
                foreach (var item in GetIEnumerableFromDataSource())
                {
                    // create row
                    var placeholder = new DataItemContainer {
                        DataItemIndex = index
                    };
                    placeholder.SetBinding(DataContextProperty, ValueBindingExpression.CreateBinding(
                                               bindingService,
                                               j => item,
                                               itemBinding.KnockoutExpression.AssignParameters(p =>
                                                                                               p == JavascriptTranslator.CurrentIndexParameter ? new CodeParameterAssignment(index.ToString(), OperatorPrecedence.Max) :
                                                                                               default(CodeParameterAssignment))));
                    placeholder.SetValue(Internal.PathFragmentProperty, GetPathFragmentExpression() + "/[" + index + "]");
                    placeholder.ID = index.ToString();
                    Children.Add(placeholder);
                    CreateRowWithCells(context, placeholder);

                    index++;
                }
                numberOfRows = index;
            }
            else
            {
                numberOfRows = 0;
            }

            // add empty item
            if (EmptyDataTemplate != null)
            {
                emptyDataContainer = new EmptyData();
                emptyDataContainer.SetBinding(DataSourceProperty, dataSourceBinding);
                EmptyDataTemplate.BuildContent(context, emptyDataContainer);
                Children.Add(emptyDataContainer);
            }
        }
Beispiel #33
0
        private void CreateRowWithCells(Hosting.IDotvvmRequestContext context, DataItemContainer placeholder)
        {
            var isInEditMode = false;
            if (InlineEditing)
            {
                // if gridviewdataset is missing throw exception
                if (!(DataSource is IGridViewDataSet))
                {
                    throw new ArgumentException("You have to use GridViewDataSet with InlineEditing enabled.");
                }

                //checks if row is being edited
                isInEditMode = IsEditedRow(placeholder);
            }

            var row = CreateRow(placeholder, isInEditMode);

            // create cells
            foreach (var column in Columns)
            {
                var cell = new HtmlGenericControl("td");
                SetCellAttributes(column, cell, false);
                row.Children.Add(cell);

                if (isInEditMode && column.IsEditable)
                {
                    column.CreateEditControls(context, cell);
                }
                else
                {
                    column.CreateControls(context, cell);
                }
            }
        }
Beispiel #34
0
 private void SetUpClientItem(DataItemContainer container)
 {
     container.DataContext = null;
     container.SetValue(Internal.PathFragmentProperty, GetPathFragmentExpression() + "/[$index]");
     container.SetValue(Internal.ClientIDFragmentProperty, GetValueRaw(Internal.CurrentIndexBindingProperty));
 }
Beispiel #35
0
        private bool IsEditedRow(DataItemContainer placeholder)
        {
            var primaryKeyPropertyName = ((IGridViewDataSet)DataSource).PrimaryKeyPropertyName;
            if (string.IsNullOrEmpty(primaryKeyPropertyName))
            {
                throw new DotvvmControlException(this, $"The {nameof(IGridViewDataSet)} must specify the {nameof(IGridViewDataSet.PrimaryKeyPropertyName)} property when inline editing is enabled on the {nameof(GridView)} control!");
            }

            PropertyInfo prop;
            var value = ReflectionUtils.GetObjectPropertyValue(placeholder.DataContext, primaryKeyPropertyName, out prop);

            if (value != null)
            {
                var editRowId = ((IGridViewDataSet)DataSource).EditRowId;
                if (editRowId != null && value.Equals(ReflectionUtils.ConvertValue(editRowId, prop.PropertyType)))
                {
                    return true;
                }
            }

            return false;
        }
Beispiel #36
0
        private void DataBind(IDotvvmRequestContext context)
        {
            Children.Clear();
            emptyDataContainer = null;
            head = null;

            var dataSourceBinding = GetDataSourceBinding();
            var dataSource        = DataSource;

            Action <string> sortCommand = null;

            if (dataSource is IGridViewDataSet)
            {
                sortCommand = ((IGridViewDataSet)dataSource).SetSortExpression;
            }
            else
            {
                sortCommand = SortChanged;
            }

            // WORKAROUND: DataSource is null => don't throw exception
            if (sortCommand == null && dataSource == null)
            {
                sortCommand = s => { throw new Exception("can't sort null data source"); };
            }

            CreateHeaderRow(context, sortCommand);
            var index = 0;

            if (dataSource != null)
            {
                // create header row
                var items = GetIEnumerableFromDataSource(dataSource);
                var javascriptDataSourceExpression = dataSourceBinding.GetKnockoutBindingExpression();

                foreach (var item in items)
                {
                    // create row
                    var placeholder = new DataItemContainer {
                        DataItemIndex = index
                    };
                    placeholder.SetBinding(DataContextProperty, GetItemBinding((IList)items, javascriptDataSourceExpression, index));
                    placeholder.SetValue(Internal.PathFragmentProperty, JavascriptCompilationHelper.AddIndexerToViewModel(GetPathFragmentExpression(), index));
                    placeholder.ID = "i" + index;
                    CreateRow(context, placeholder);
                    Children.Add(placeholder);

                    index++;
                }
                numberOfRows = index;
            }
            else
            {
                numberOfRows = 0;
            }

            // add empty item
            if (EmptyDataTemplate != null)
            {
                emptyDataContainer = new EmptyData();
                emptyDataContainer.SetBinding(DataSourceProperty, dataSourceBinding);
                EmptyDataTemplate.BuildContent(context, emptyDataContainer);
                Children.Add(emptyDataContainer);
            }
        }
Beispiel #37
0
        protected override void RenderContents(IHtmlWriter writer, IDotvvmRequestContext context)
        {
            // render the header
            head?.Render(writer, context);

            // render body
            if (!RenderOnServer)
            {
                writer.AddKnockoutForeachDataBind("dotvvm.evaluator.getDataSourceItems($gridViewDataSet)");
            }
            writer.RenderBeginTag("tbody");

            // render contents
            if (RenderOnServer)
            {
                // render on server
                var index = 0;
                foreach (var child in Children.Except(new[] { head, emptyDataContainer }))
                {
                    child.Render(writer, context);
                    index++;
                }
            }
            else
            {
                // render on client
                if (InlineEditing)
                {
                    var placeholder = new DataItemContainer { DataContext = null };
                    placeholder.SetValue(Internal.PathFragmentProperty, JavascriptCompilationHelper.AddIndexerToViewModel(GetPathFragmentExpression(), "$index"));
                    placeholder.SetValue(Internal.ClientIDFragmentProperty, "$index()");
                    writer.WriteKnockoutDataBindComment("if", "ko.unwrap(ko.unwrap($gridViewDataSet).EditRowId) !== ko.unwrap($data[ko.unwrap(ko.unwrap($gridViewDataSet).PrimaryKeyPropertyName)])");
                    CreateTemplates(context, placeholder);
                    Children.Add(placeholder);
                    placeholder.Render(writer, context);
                    writer.WriteKnockoutDataBindEndComment();

                    var placeholderEdit = new DataItemContainer { DataContext = null };
                    placeholderEdit.SetValue(Internal.PathFragmentProperty, JavascriptCompilationHelper.AddIndexerToViewModel(GetPathFragmentExpression(), "$index"));
                    placeholderEdit.SetValue(Internal.ClientIDFragmentProperty, "$index()");
                    writer.WriteKnockoutDataBindComment("if", "ko.unwrap(ko.unwrap($gridViewDataSet).EditRowId) === ko.unwrap($data[ko.unwrap(ko.unwrap($gridViewDataSet).PrimaryKeyPropertyName)])");
                    CreateTemplates(context, placeholderEdit, true);
                    Children.Add(placeholderEdit);
                    placeholderEdit.Render(writer, context);
                    writer.WriteKnockoutDataBindEndComment();
                }
                else
                {
                    var placeholder = new DataItemContainer { DataContext = null };
                    placeholder.SetValue(Internal.PathFragmentProperty, JavascriptCompilationHelper.AddIndexerToViewModel(GetPathFragmentExpression(), "$index"));
                    placeholder.SetValue(Internal.ClientIDFragmentProperty, "$index()");
                    CreateRowWithCells(context, placeholder);
                    Children.Add(placeholder);
                    placeholder.Render(writer, context);

                }
            }

            writer.RenderEndTag();
        }
Beispiel #38
0
        /// <summary>
        /// Renders the contents inside the control begin and end tags.
        /// </summary>
        protected override void RenderContents(IHtmlWriter writer, RenderContext context)
        {
            var dataSourceBinding = GetDataSourceBinding();

            if (RenderOnServer)
            {
                // render on server
                var index = 0;
                foreach (var child in Children)
                {
                    Children[index].Render(writer, context);
                    index++;
                }
            }
            else
            {
                // render on client
                var placeholder = new DataItemContainer() { DataContext = null };
                placeholder.SetValue(Internal.PathFragmentProperty, JavascriptCompilationHelper.AddIndexerToViewModel(dataSourceBinding.GetKnockoutBindingExpression(), "$index"));
                Children.Add(placeholder);
                ItemTemplate.BuildContent(context.RequestContext, placeholder);

                placeholder.Render(writer, context);
            }
        }