Exemplo n.º 1
0
    private CMSButtonAction GetControlAction(HeaderAction headerAction)
    {
        var controlAction = new CMSButtonAction();

        controlAction.Text    = headerAction.Text;
        controlAction.Enabled = headerAction.Enabled && Enabled;
        controlAction.ToolTip = headerAction.Tooltip;

        // Register script only when action is active
        if (Enabled && headerAction.Enabled && !headerAction.Inactive)
        {
            // Wrap script from OnClick property into anonymous function so it won't cancel the following script in case this property scipt returns true.
            // The execution of following script is canceled only when anonymous function returns false.
            if (!String.IsNullOrEmpty(headerAction.OnClientClick))
            {
                string onClickScript = "var onClickWrapper = function(sender) { " + headerAction.OnClientClick + "}; if (onClickWrapper(this) === false) { return false; }";
                controlAction.OnClientClick = onClickScript;
            }

            string commandName = !string.IsNullOrEmpty(headerAction.CommandName) ? headerAction.CommandName : headerAction.EventName;

            // Perform post-back
            if (!String.IsNullOrEmpty(commandName) || !String.IsNullOrEmpty(headerAction.CommandArgument))
            {
                string argument = string.Join(";", new[]
                {
                    commandName,
                    headerAction.CommandArgument
                });

                var opt = new PostBackOptions(this, argument)
                {
                    PerformValidation = true,
                    ValidationGroup   = headerAction.ValidationGroup
                };

                string postbackScript = ControlsHelper.GetPostBackEventReference(this, opt, false, !PerformFullPostBack);
                controlAction.OnClientClick += postbackScript + ";";
            }
            else
            {
                // Use URL only for standard link
                if (!String.IsNullOrEmpty(headerAction.RedirectUrl))
                {
                    var target = headerAction.Target ?? "_self";

                    var url = ScriptHelper.ResolveUrl(headerAction.RedirectUrl);

                    if (headerAction.OpenInDialog)
                    {
                        url = URLHelper.AddParameterToUrl(url, "dialog", "1");
                        url = UIContextHelper.AppendDialogHash(url);

                        ScriptHelper.RegisterDialogScript(Page);

                        controlAction.OnClientClick = ScriptHelper.GetModalDialogScript(url, "action" + headerAction.Index, headerAction.DialogWidth, headerAction.DialogHeight, false);
                    }
                    else
                    {
                        controlAction.OnClientClick += "window.open('" + url + "','" + target + "');";
                    }
                }
            }

            // Stop automatic postback rendered by asp button
            controlAction.OnClientClick += " return false;";
        }

        return(controlAction);
    }
    private CMSButtonAction GetControlAction(HeaderAction headerAction)
    {
        var controlAction = new CMSButtonAction();

        controlAction.Text = headerAction.Text;
        controlAction.Enabled = headerAction.Enabled && Enabled;
        controlAction.ToolTip = headerAction.Tooltip;

        // Register script only when action is active
        if (Enabled && headerAction.Enabled && !headerAction.Inactive)
        {
            // Wrap script from OnClick property into anonymous function so it won't cancel the following script in case this property scipt returns true.
            // The execution of following script is canceled only when anonymous function returns false.
            if (!String.IsNullOrEmpty(headerAction.OnClientClick))
            {
                string onClickScript = "var onClickWrapper = function(sender) { " + headerAction.OnClientClick + "}; if (onClickWrapper(this) === false) { return false; }";
                controlAction.OnClientClick = onClickScript;
            }

            string commandName = !string.IsNullOrEmpty(headerAction.CommandName) ? headerAction.CommandName : headerAction.EventName;

            // Perform post-back
            if (!String.IsNullOrEmpty(commandName) || !String.IsNullOrEmpty(headerAction.CommandArgument))
            {
                string argument = string.Join(";", new[]
                {
                    commandName,
                    headerAction.CommandArgument
                });

                var opt = new PostBackOptions(this, argument)
                {
                    PerformValidation = true,
                    ValidationGroup = headerAction.ValidationGroup
                };

                string postbackScript = ControlsHelper.GetPostBackEventReference(this, opt, false, !PerformFullPostBack);
                controlAction.OnClientClick += postbackScript + ";";
            }
            else
            {
                // Use URL only for standard link
                if (!String.IsNullOrEmpty(headerAction.RedirectUrl))
                {
                    var target = headerAction.Target ?? "_self";

                    var url = ScriptHelper.ResolveUrl(headerAction.RedirectUrl);

                    if (headerAction.OpenInDialog)
                    {
                        url = URLHelper.AddParameterToUrl(url, "dialog", "1");
                        url = UIContextHelper.AppendDialogHash(url);

                        ScriptHelper.RegisterDialogScript(Page);

                        controlAction.OnClientClick = ScriptHelper.GetModalDialogScript(url, "action" + headerAction.Index, headerAction.DialogWidth, headerAction.DialogHeight, false);
                    }
                    else
                    {
                        controlAction.OnClientClick += "window.open('" + url + "','" + target + "');";
                    }
                }
            }

            // Stop automatic postback rendered by asp button
            controlAction.OnClientClick += " return false;";
        }

        return controlAction;
    }
    /// <summary>
    /// Loads device profile menu.
    /// </summary>
    private void LoadDevicesMenu()
    {
        if (UseSmallButton)
        {
            plcSmallButton.Visible = true;
        }
        else
        {
            plcBigButton.Visible = true;
        }

        string        defaultString    = HTMLHelper.HTMLEncode(GetString("deviceselector.default", ResourceCulture));
        StringBuilder sbDeviceProfiles = new StringBuilder();
        MenuItem      devMenuItem      = null;

        if (!UseSmallButton)
        {
            devMenuItem = new MenuItem
            {
                Text    = defaultString,
                Tooltip = defaultString,
            };

            // Display icon in On-site editing
            if ((Page is PortalPage) || (Page is TemplatePage))
            {
                devMenuItem.IconClass = "icon-monitor-smartphone";
            }

            SetStyles(devMenuItem);
            buttons.Buttons.Add(devMenuItem);
        }

        // Load enabled profiles
        InfoDataSet <DeviceProfileInfo> ds = DeviceProfileInfoProvider.GetDeviceProfiles()
                                             .WhereEquals("ProfileEnabled", 1)
                                             .OrderBy("ProfileOrder")
                                             .TypedResult;

        if (!DataHelper.DataSourceIsEmpty(ds))
        {
            // Create default item
            SubMenuItem defaultMenuItem = new SubMenuItem
            {
                Text          = defaultString,
                Tooltip       = defaultString,
                OnClientClick = "ChangeDevice('');",
            };

            if (UseSmallButton)
            {
                // Insert the current device profile to the button
                btnProfilesSelector.Actions.Add(new CMSButtonAction()
                {
                    OnClientClick = String.Format("ChangeDevice({0}); return false;", ScriptHelper.GetString(defaultString)),
                    Text          = defaultString
                });
            }
            else
            {
                // Insert the current device profile to the context menu
                devMenuItem.SubItems.Add(defaultMenuItem);
            }

            // Load the profiles list
            foreach (DeviceProfileInfo profileInfo in ds.Items)
            {
                string          profileName  = GetString(profileInfo.ProfileDisplayName, ResourceCulture);
                CMSButtonAction deviceButton = null;

                if (UseSmallButton)
                {
                    deviceButton = new CMSButtonAction()
                    {
                        OnClientClick = String.Format("ChangeDevice({0}); return false;", ScriptHelper.GetString(profileInfo.ProfileName)),
                        Text          = profileName,
                        Name          = profileName
                    };

                    btnProfilesSelector.Actions.Add(deviceButton);
                }
                else
                {
                    SubMenuItem menuItem = new SubMenuItem
                    {
                        Text          = HTMLHelper.HTMLEncode(profileName),
                        Tooltip       = HTMLHelper.HTMLEncode(profileName),
                        OnClientClick = String.Format("ChangeDevice({0});", ScriptHelper.GetString(profileInfo.ProfileName))
                    };
                    devMenuItem.SubItems.Add(menuItem);
                }

                // Update main button if current device profile is equal currently processed profile
                if ((currentDevice != null) && (currentDevice.ProfileName.CompareToCSafe(profileInfo.ProfileName, true) == 0))
                {
                    if (UseSmallButton)
                    {
                        btnProfilesSelector.SelectedActionName = profileName;
                    }
                    else
                    {
                        devMenuItem.Text    = HTMLHelper.HTMLEncode(profileName);
                        devMenuItem.Tooltip = HTMLHelper.HTMLEncode(profileName);
                    }
                }
            }
        }
    }
    /// <summary>
    /// Loads device profile menu.
    /// </summary>
    private void LoadDevicesMenu()
    {
        if (UseSmallButton)
        {
            plcSmallButton.Visible = true;
        }
        else
        {
            plcBigButton.Visible = true;
        }

        string defaultString = HTMLHelper.HTMLEncode(GetString("deviceselector.default", ResourceCulture));
        StringBuilder sbDeviceProfiles = new StringBuilder();
        MenuItem devMenuItem = null;

        if (!UseSmallButton)
        {
            devMenuItem = new MenuItem
            {
                Text = defaultString,
                Tooltip = defaultString,
            };

            // Display icon in On-site editing
            if ((Page is PortalPage) || (Page is TemplatePage))
            {
                devMenuItem.IconClass = "icon-monitor-smartphone";
            }

            SetStyles(devMenuItem);
            buttons.Buttons.Add(devMenuItem);
        }

        // Load enabled profiles
        InfoDataSet<DeviceProfileInfo> ds = DeviceProfileInfoProvider.GetDeviceProfiles()
            .WhereEquals("ProfileEnabled", 1)
            .OrderBy("ProfileOrder")
            .TypedResult;

        if (!DataHelper.DataSourceIsEmpty(ds))
        {
            // Create default item
            SubMenuItem defaultMenuItem = new SubMenuItem
                                              {
                                                  Text = defaultString,
                                                  Tooltip = defaultString,
                                                  OnClientClick = "ChangeDevice('');",
                                              };

            if (UseSmallButton)
            {
                // Insert the current device profile to the button
                btnProfilesSelector.Actions.Add(new CMSButtonAction()
                {
                    OnClientClick = String.Format("ChangeDevice({0}); return false;", ScriptHelper.GetString(defaultString)),
                    Text = defaultString
                });
            }
            else
            {
                // Insert the current device profile to the context menu
                devMenuItem.SubItems.Add(defaultMenuItem);
            }

            // Load the profiles list
            foreach (DeviceProfileInfo profileInfo in ds.Items)
            {
                string profileName = GetString(profileInfo.ProfileDisplayName, ResourceCulture);
                CMSButtonAction deviceButton = null;

                if (UseSmallButton)
                {
                    deviceButton = new CMSButtonAction()
                    {
                        OnClientClick = String.Format("ChangeDevice({0}); return false;", ScriptHelper.GetString(profileInfo.ProfileName)),
                        Text = profileName,
                        Name = profileName
                    };

                    btnProfilesSelector.Actions.Add(deviceButton);
                }
                else
                {
                    SubMenuItem menuItem = new SubMenuItem
                                               {
                                                   Text = HTMLHelper.HTMLEncode(profileName),
                                                   Tooltip = HTMLHelper.HTMLEncode(profileName),
                                                   OnClientClick = String.Format("ChangeDevice({0});", ScriptHelper.GetString(profileInfo.ProfileName))
                                               };
                    devMenuItem.SubItems.Add(menuItem);
                }

                // Update main button if current device profile is equal currently processed profile
                if ((currentDevice != null) && (currentDevice.ProfileName.CompareToCSafe(profileInfo.ProfileName, true) == 0))
                {
                    if (UseSmallButton)
                    {
                        btnProfilesSelector.SelectedActionName = profileName;
                    }
                    else
                    {
                        devMenuItem.Text = HTMLHelper.HTMLEncode(profileName);
                        devMenuItem.Tooltip = HTMLHelper.HTMLEncode(profileName);
                    }
                }

            }
        }
    }