2012-06-01 3 views

답변

12

Sitecore에서, Sitecore.Client 어셈블리에 Sitecore.Shell.Applications.WebEdit.Commands.ChangeLanguageSitecore.Shell.Applications.WebEdit.Commands.SetLanguage 그것을 어떻게하는지 당신은 볼 수 있습니다.

이렇게하려면 두 가지 명령을 만들어야합니다. 하나의 명령은 버튼과 연관되어 있으며, 하나의 명령은 하위 항목이 선택 될 때 실행됩니다. 이 예제는 국가 별 쿠키를 변경하는 시나리오를 기반으로합니다.

ChangeCountry 명령

첫째, 명령 메뉴를 표시합니다. 동적 옵션을 사용하여 Menu을 표시하는 것을 볼 수 있습니다. GetHeaderGetIcon을 재정의하면 버튼 자체가 사용자의 현재 선택에 따라 동적으로 바뀝니다.

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using Sitecore.Shell.Applications.WebEdit.Commands; 
using Sitecore.Diagnostics; 
using Sitecore.Data.Items; 
using Sitecore.Web.UI.Sheer; 
using Sitecore.Web.UI.HtmlControls; 
using Sitecore.StringExtensions; 
using System.Web; 

namespace Prototype.Commands 
{ 
    public class ChangeCountry : WebEditCommand 
    { 
     protected Dictionary<string, CountryOption> _countries = new Dictionary<string, CountryOption> 
     { 
      {"US", new CountryOption { 
       ID = "US", 
       Name = "United States", 
       Icon = "Flags/32x32/flag_usa.png" 
      }}, 
      {"CA", new CountryOption { 
       ID = "CA", 
       Name = "Canada", 
       Icon = "Flags/32x32/flag_canada.png" 
      }}, 
      {"MX", new CountryOption { 
       ID = "MX", 
       Name = "Mexico", 
       Icon = "Flags/32x32/flag_mexico.png" 
      }}, 
      {"DE", new CountryOption { 
       ID = "DE", 
       Name = "Germany", 
       Icon = "Flags/32x32/flag_germany.png" 
      }} 
     }; 

     public override void Execute(Sitecore.Shell.Framework.Commands.CommandContext context) 
     { 
      Assert.ArgumentNotNull(context, "context"); 
      if (context.Items.Length == 1) 
      { 
       Item item = context.Items[0]; 
       SheerResponse.DisableOutput(); 
       Menu control = new Menu(); 
       //replace with lookup and loop of available values 
       foreach (var key in _countries.Keys) 
       { 
        var country = _countries[key]; 
        string id = country.ID; 
        string header = country.Name; 
        string icon = country.Icon; 
        string click = "prototype:setcountry(country={0})".FormatWith(key); 
        control.Add(id, header, icon, string.Empty, click, false, string.Empty, MenuItemType.Normal); 
       } 
       SheerResponse.EnableOutput(); 
       SheerResponse.ShowPopup("ChangeCountryButton", "below", control); 
      } 
     } 

     public override string GetHeader(Sitecore.Shell.Framework.Commands.CommandContext context, string header) 
     { 
      HttpCookie country = HttpContext.Current.Request.Cookies["country"]; 
      if (country != null && _countries.ContainsKey(country.Value)) 
      { 
       return _countries[country.Value].Name; 
      } 
      return base.GetHeader(context, header); 
     } 

     public override string GetIcon(Sitecore.Shell.Framework.Commands.CommandContext context, string icon) 
     { 
      HttpCookie country = HttpContext.Current.Request.Cookies["country"]; 
      if (country != null && _countries.ContainsKey(country.Value)) 
      { 
       return _countries[country.Value].Icon; 
      } 
      return base.GetIcon(context, icon); 
     } 

     protected class CountryOption 
     { 
      public string ID { get; set; } 
      public string Name { get; set; } 
      public string Icon { get; set; } 
     } 
    } 
} 

Commands.config 또는 포함 파일에서 새 명령을 등록하십시오.

<command name="prototype:changecountry" type="Prototype.Commands.ChangeCountry,Prototype" /> 

국가 변경 버튼

/sitecore/content/Applications/WebEdit/Ribbons/WebEdit/Experience에 새 청크와 만들기 버튼을 클릭합니다. 이 리본 스트립은 미리보기 모드에서도 참조/복제됩니다.

Ribbon Button

클릭 필드는 명령의 이름과 일치해야하며, ID 필드 위의 SheerResponse.ShowPopup 전화에서 제공되는 요소 ID와 일치해야합니다 :이 버튼은 다음과 같은 속성을 사용합니다.

SetCountry 명령

다음 메뉴/드롭에있는 항목이 선택 될 때 호출되는 명령입니다.

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using Sitecore.Shell.Applications.WebEdit.Commands; 
using System.Net; 
using Sitecore.Diagnostics; 
using Sitecore.Web.UI.Sheer; 
using System.Web; 

namespace Prototype.Commands 
{ 
    public class SetCountry : WebEditCommand 
    { 
     public override void Execute(Sitecore.Shell.Framework.Commands.CommandContext context) 
     { 
      Assert.ArgumentNotNull(context, "context"); 
      var country = context.Parameters["country"]; 
      Assert.IsNotNullOrEmpty(country, "Country not found"); 
      HttpCookie cookie = new HttpCookie("country", country); 
      HttpContext.Current.Response.Cookies.Add(cookie); 
      WebEditCommand.Reload(WebEditCommand.GetUrl()); 
     } 
    } 
} 

이 예에서는 선택한 값을 기반으로 쿠키를 설정하고 페이지를 다시로드합니다. 전달 된 값은 ChangeCountry에있는 메뉴 항목과 연관된 클릭 이벤트를 기반으로합니다. 마찬가지로 구성 할 때 명령 이름은 ChangeCountry 클릭 이벤트에서 사용 된 것과 일치해야합니다.

<command name="prototype:setcountry" type="Prototype.Commands.SetCountry,Prototype" /> 
관련 문제