Remember active language tab.

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
13 years ago
I found it annoying that the nop will always display "Standard" tab as default when saving. So if you edit another language this can become a pain. This modification will via Jquery add onclick events etc to all language tabs. This means that you do not have to go into every control and correct it. Works like a charm :)

I have modified the BaseNopAdministrationMasterPage.cs with this:


#region Language Tab Memory By PostMe


        //remember which language is active throughout session.
        public int SelectedTabLanguageId
        {
            get
            {
                if (Session["SelectedTabLanguageId"] != null)
                    return (int)Session["SelectedTabLanguageId"];
                return 0;
            }
            set { Session["SelectedTabLanguageId"] = value; }
        }


        /// <summary>
        /// This method will add scripts that automatically add extra onclick functionality to idTabs.
        /// Further more it will add a "selected" class to one of the idtabs, making it the active. This is done via the property SelectedTabLanguageId
        /// Please remember that the masterpage which uses this function must have the following hidden field:
        ///
        /// <asp:HiddenField ID="hiddenLanguageTabID" runat="server"  OnValueChanged="hiddenLanguageTabID_Changed"/>    
        ///
        /// The code for hiddenLanguageTabID_Changed looks like this:
        ///
        ///      protected void hiddenLanguageTabID_Changed(object sender, EventArgs e)
        ///     {
        ///          SelectedTabLanguageId = (string.IsNullOrEmpty(hiddenLanguageTabID.Value)) ? 0 : int.Parse(hiddenLanguageTabID.Value);
        ///     }
        ///
        /// </summary>
        public void BindRememberLanguageTabFunctinality()
        {
            var languages = LanguageManager.GetAllLanguages(true);

            if (languages != null && languages.Count > 0)
            {

                StringBuilder sb = new StringBuilder();

                HiddenField hiddenLanguageTabID = this.Page.Master.FindControl("hiddenLanguageTabID") as HiddenField;

                if (hiddenLanguageTabID == null)
                {
                    throw new NopException("You have not added hidden language memory field to the master page source. Please do so and insert \"SelectedTabLanguageId = (string.IsNullOrEmpty(hiddenLanguageTabID.Value)) ? 0 : int.Parse(hiddenLanguageTabID.Value);\"  in the onchanged event of hidden field: <asp:HiddenField ID=\"hiddenLanguageTabID\" runat=\"server\"  OnValueChanged=\"hiddenLanguageTabID_Changed\"/>");
                }

                sb.AppendLine("<script type=\"text/javascript\" >");

                //make sure that the whole document has loaded before hooking up javascript events
                sb.AppendLine("$(document).ready(function() {");

                //first tab1 is always standard tab
                sb.AppendLine("     $(\"a[href='#idTab_Info1']\").click(function() { ");
                sb.AppendLine("     document.getElementById('" + hiddenLanguageTabID.ClientID + "').value = '0';");
                sb.AppendLine("     });");

                int langCounter = 2; //language tabs always begin at 2 (1 being the standard...that is selected by default)m

                foreach (Language language in languages)
                {
                    //hook up onclick events to idtab href's

                    sb.AppendLine("     $(\"a[href='#idTab_Info" + langCounter + "']\").click(function() { ");
                    sb.AppendLine("     document.getElementById('" + hiddenLanguageTabID.ClientID + "').value = '" + language.LanguageId + "';");
                    sb.AppendLine("     });");


                    //if the language is selected, we add a "selected" class to the idtab href, making it active

                    if (language.LanguageId == SelectedTabLanguageId)
                    {
                        sb.AppendLine("     $(\"a[href='#idTab_Info" + langCounter + "']\").addClass(' selected');");
                    }



                    langCounter++;
                }

                sb.AppendLine("});");
                sb.AppendLine("</script>");



                if (this.Page.Header != null)
                {
                    //we have a header
                    if (HttpContext.Current.Items["RememberLanguageTabFunctinality"] == null ||
                        !Convert.ToBoolean(HttpContext.Current.Items["RememberLanguageTabFunctinality"]))
                    {
                        Literal script = new Literal() { Text = sb.ToString() };
                        Control control = this.Page.Header.FindControl("SCRIPTS");
                        if (control != null)
                            control.Controls.Add(script);
                        else
                            this.Page.Header.Controls.Add(script);
                    }
                    HttpContext.Current.Items["RememberLanguageTabFunctinality"] = true;
                }



            }

        }
        #endregion


Further More you need to add the following to the "main.master" in admin:

<asp:HiddenField ID="hiddenLanguageTabID" runat="server"  OnValueChanged="hiddenLanguageTabID_Changed"/>  

In the code for main admin master add this:

protected void Page_Load(object sender, EventArgs e)
        {

            BindRememberLanguageTabFunctinality();
......


AND



protected void hiddenLanguageTabID_Changed(object sender, EventArgs e)
        {

            SelectedTabLanguageId = (string.IsNullOrEmpty(hiddenLanguageTabID.Value)) ? 0 : int.Parse(hiddenLanguageTabID.Value);
                
        }

13 years ago
Thanks for this contribution. Just created a work item for this task
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.