Exemplo n.º 1
0
        public static OSDataGridTable FromTable(OSDataGrid parent, System.Web.UI.WebControls.Table table, ArrayList rowIds, ArrayList itemIndexes)
        {
            OSDataGridTable osTable = new OSDataGridTable(parent.ClientID);

            // copy everything from table
            // Properties
            osTable.BackImageUrl    = table.BackImageUrl;
            osTable.Caption         = table.Caption;
            osTable.CaptionAlign    = table.CaptionAlign;
            osTable.CellPadding     = table.CellPadding;
            osTable.CellSpacing     = table.CellSpacing;
            osTable.GridLines       = table.GridLines;
            osTable.HorizontalAlign = table.HorizontalAlign;

            // Rows
            TableRow[] tableRows = new TableRow[table.Rows.Count];
            table.Rows.CopyTo(tableRows, 0);

            int i = 0;

            foreach (OSDataGridItem row in tableRows)
            {
                try {
                    // restore the original id of this row, if set
                    if (rowIds != null)
                    {
                        row.ID = OSDataGridItem.FormatId((int)rowIds[i], parent.EnableLegacyRendering);
                    }
                    else
                    {
                        row.ID = OSDataGridItem.FormatId(i + 1, parent.EnableLegacyRendering);
                    }

                    // set the item index, if set
                    if (itemIndexes != null)
                    {
                        row.SetItemIndex((int)itemIndexes[i]);
                    }
                } catch (ArgumentOutOfRangeException) {
                    throw new HEMessageException(MR.GetMessage(MessageId.AjaxListModified, "Table Records", parent.ClientID));
                }

                osTable.Rows.Add(row);
                i++;
            }

            return(osTable);
        }
        /// <summary>
        /// Gets a control in the current selected row of a record widget, providing the record widget and the control id
        /// </summary>
        /// <param name="recordWidgetControl"></param>
        /// <param name="childID"></param>
        /// <returns></returns>
        public static Control getSelectedControlInRecordWidget(Control recordWidgetControl, string childID)
        {
            Control    selectedItemControl = null;
            OSDataGrid dg = null;

            //TODO TCS Put this in an interface like IWidgetWithIterator?
            if (recordWidgetControl is CustomWidget)
            {
                foreach (var control in recordWidgetControl.Controls)
                {
                    if (control is Iterator)
                    {
                        recordWidgetControl = (Control)control;
                    }
                }
            }

            if (recordWidgetControl is Iterator)
            {
                Iterator it = recordWidgetControl as Iterator;
                selectedItemControl = it.SelectedItemControl;
            }
            else
            {
                dg = recordWidgetControl as OSDataGrid;
                selectedItemControl = dg.SelectedItemControl;
            }

            Control foundControl = null;

            // try to find the control in the selected item control, if any
            if (selectedItemControl != null)
            {
                foundControl = FindControlRecursive(selectedItemControl, childID);
            }
            // #116141 - if the record widget control is a datagrid and the control is not yet found, try to find it in the header item
            if (foundControl == null && dg != null && dg.HeaderItemControl != null)
            {
                foundControl = FindControlRecursive(dg.HeaderItemControl, childID);
            }
            return(foundControl);
        }
        /// <summary>
        /// Gets a control in the last row of a record widget, providing the record widget and the control id
        /// </summary>
        /// <param name="recordWidgetControl"></param>
        /// <param name="childID"></param>
        /// <returns></returns>
        public static Control getControlInRecordWidget(Control recordWidgetControl, string childID)
        {
            //TODO TCS Put this in an interface like IWidgetWithIterator?
            if (recordWidgetControl is CustomWidget)
            {
                foreach (Control child in recordWidgetControl.Controls)
                {
                    // TODO JMN this decision should be done in compile time but currently its not easy
                    if (child is Iterator)
                    {
                        recordWidgetControl = child;
                        break;
                    }
                    else
                    {
                        var childFound = FindControlRecursive(child, childID);
                        if (childFound != null)
                        {
                            return(childFound);
                        }
                    }
                }
            }

            ControlCollection lines = null;
            Control           line;
            int index = 0;

            OSDataGrid dg = null;

            if (recordWidgetControl is Iterator)
            {
                Iterator it = recordWidgetControl as Iterator;
                // if the iterator has some selected index, go to the current control
                if (it.SelectedIndex != -1)
                {
                    return(getSelectedControlInRecordWidget(recordWidgetControl, childID));
                }
                else
                {
                    // Must be databinding the iterator, otherwise the control was requested from outside of the iterator */
                    if (it.IsDataBinding)
                    {
                        /* NOTE: The last control is always an Invisible Item, so we must
                         * use -2 to get the previous one that is a Repeater Item and
                         * corresponds to the last line */
                        lines = it.Controls;
                        index = lines.Count - 2;
                    }
                }
            }
            else
            {
                /* The table record widget is a DataGrid. */
                dg = recordWidgetControl as OSDataGrid;
                // if the datagrid has some selected index, go to the current control
                if (dg.SelectedIndex != -1)
                {
                    return(getSelectedControlInRecordWidget(recordWidgetControl, childID));
                }
                else
                {
                    // Must be databinding the datagrid, otherwise the control was requested from outside of the datagrid */
                    if (dg.IsDataBinding)
                    {
                        /* The datagrid always contains a child control table.
                        * This child control is a collection of lines, and each one of the line
                        * is a collection of columns containing the widget. */
                        lines = recordWidgetControl.Controls[0].Controls;
                        index = lines.Count - 1;
                    }
                }
            }


            Control ctrl = null;

            if (lines != null)
            {
                line = lines[index];
                // Find the control inside the line
                ctrl = FindControlRecursive(line, childID);
            }

            // #116141 - if the record widget control is a datagrid and the control is not yet found try to find in the widget in the header item
            if (ctrl == null && dg != null && dg.HeaderItemControl != null)
            {
                ctrl = FindControlRecursive(dg.HeaderItemControl, childID);
            }

            // Were we able to find the control ?
            if (ctrl == null)
            {
                // the control is not accessible in the current context, so return ""
                ctrl = new EmptyControl();
            }

            return(ctrl);
        }
        public void AjaxRefresh(Control c, int rowIndex, string animationName, string listOperation, bool isTableRecord, object dataItem, StoreViewStateDelegate storeViewStateDelegate, string clientIdOverride)
        {
            // don't do nothing if not in a partial request or if the control is not accessible (e.g.: refreshing widgets inside table records from outside)
            if (!IsAjaxRequest || c == null)
            {
                return;
            }

            // mark that we are refreshing a widget, so that possible queued javascripts happening in the databind are queued
            // to be sent immediately after the control json (for web block preparations or user defined functions execution)
            _isRefreshingWidget = true;

            try {
                LocalState stackBackup = null;
#if JAVA
                HashSet <IAbstractTopLevelComponentWithMandatory> previousVisibleComponentsWithValidation = null,
                                                                  newVisibleComponentsWithValidation      = null;
#endif


                if (c is OSUserControl)
                {
                    OSUserControl blk = (OSUserControl)c;
                    stackBackup = ((IWebScreen)blk.Page).PushStack();
                }
                else
                {
                    stackBackup = ((IWebScreen)c.Page).PushStack();
                }

                if (listOperation == "")
                {
#if JAVA
                    previousVisibleComponentsWithValidation = GetVisibleComponentsWithValidation(c);
#endif

                    // databind the control
                    c.DataBind();

#if JAVA
                    newVisibleComponentsWithValidation = GetVisibleComponentsWithValidation(c);
#endif
                }
                else
                {
                    // some list operation

                    if (!isTableRecord)
                    {
                        // list records require the prerender phase before list append, insert and remove operations, since prerender creates the current child controls hierarchy
                        MethodInfo preRenderRecursiveInternal = typeof(Page).GetMethod("PreRenderRecursiveInternal", BindingFlags.NonPublic | BindingFlags.Instance);
                        preRenderRecursiveInternal.Invoke(c, null);
                    }

                    IListRefresh listWidget = (IListRefresh)c;
                    // execute the list refresh databind to restore LineCount and StartIndex runtime properties in the table / list record
                    listWidget.DoListRefreshDataBind();
                    // execute the operation in the list
                    switch (listOperation)
                    {
                    case "Append":
                        listWidget.AppendItem(dataItem);
                        break;

                    case "Insert":
                        listWidget.InsertItem(rowIndex, dataItem);
                        break;

                    case "Remove":
                        listWidget.RemoveItem(rowIndex);
                        break;

                    case "Refresh":
                        listWidget.RefreshItem(rowIndex);
                        break;

                    default:
                        System.Diagnostics.Debug.Assert(false, "unknown list operation: " + listOperation);
                        break;
                    }
                }

                // call store viewstate in the calling page / block

                storeViewStateDelegate();

                if (stackBackup != null)
                {
                    Debugger.Pop(stackBackup, true);
                }

                // save partial viewstate and get modified buckets

                Hashtable modifiedBuckets = PartialSaveAllState();

                OSJSONResponse resp = CreateJSONResponse();

                // build JSON response with the control rendering and modified buckets

                foreach (string bucketName in modifiedBuckets.Keys)
                {
                    resp.AddToHidden(bucketName, modifiedBuckets[bucketName].ToString());
                }

                string html = "";

                if (listOperation == "")
                {
                    // regular ajax refresh
                    // render control and add it to outers
                    resp.AddToOuter(c.ClientID, RenderControl(c));

#if JAVA
                    ProcessComponentsWithValidationTurnedInvisible(previousVisibleComponentsWithValidation,
                                                                   newVisibleComponentsWithValidation);
#endif
                }
                else
                {
                    // implicit ajax refresh via ListAppend, ListInsert or ListRemove
                    if (listOperation == "Append")
                    {
                        html = RenderAjaxListOperationRow(c);
                    }
                    else if (listOperation == "Insert")
                    {
#if JAVA
                        html = RenderAjaxListOperationRow(c, rowIndex);
#else
                        html = RenderAjaxListOperationRow(c);
#endif
                    }
                    else if (listOperation == "Remove")
                    {
                        // need to render the empty message?
                        if (((IListRefresh)c).IsEmpty)
                        {
                            html = RenderAjaxListOperationRow(c, true);
                        }
                    }
                    else if (listOperation == "Refresh")
                    {
                        // row refresh operation
                        html = RenderAjaxListOperationRow(c, rowIndex);
                    }

                    string oddLineStyle  = "";
                    string evenLineStyle = "";
                    bool   useBullets    = false;

                    if (c is OSDataGrid)
                    {
                        // get table record information to be sent to the client js
                        OSDataGrid dg = (OSDataGrid)c;
                        oddLineStyle  = dg.OddLineStyle ?? oddLineStyle;
                        evenLineStyle = dg.EvenLineStyle ?? evenLineStyle;
                    }
                    else
                    {
                        // get list record information to be sent to the client js
                        Iterator it = (Iterator)c;
                        useBullets = it.UseBullets;
                    }

                    resp.AddToList(clientIdOverride ?? c.ClientID, html, listOperation, rowIndex, isTableRecord, oddLineStyle, evenLineStyle, useBullets);
                }


                // Add call to animation registration
                if (animationName != null && animationName != "None")
                {
                    if (listOperation == "Remove" && animationName == "Highlight")
                    {
                        animationName = "ListRemoveHighlight";
                    }
                    resp.AddToJs("OsRegisterEffect" + animationName.Replace(" ", "") + "();");
                }

                // write and flush the response
                WriteJavascriptResponse(FormatJSONResponseString(resp), /*flush*/ true);

                // send all the queued javascript gathered in this refresh
                if (_javascriptQueueResponse.Js.Count > 0)
                {
                    resp = CreateJSONResponse();
                    resp.AddToJs("outsystems.internal.$(document).ready(function() {" + FormatJSONResponseString(_javascriptQueueResponse) + "});");
                    // clear it for next executions
                    _javascriptQueueResponse.ClearJs();
                    // write the response
                    WriteJavascriptResponse(FormatJSONResponseString(resp), /*flush*/ true);
                }

                // cleanup JavaScript includes so the next refresh will not re-include it
                BlocksJavaScript.CleanupAlreadyIncludedBlocksWithJavaScript();
            } finally {
                // we're no longer refreshing a widget
                _isRefreshingWidget = false;
            }
        }