Stephen2

Sitefinity - HierarchicalTaxonSelector in designer

Mar 4th, 2014
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.67 KB | None | 0 0
  1. DESIGNER.ascx:
  2. --------------
  3. <sitefinity:HierarchicalTaxonField ID="FilterCategory" runat="server" DisplayMode="Write" Expanded="false" ExpandText="Add Category" ShowDoneSelectingButton="true" AllowMultipleSelection="false" BindOnServer="false" TaxonomyMetafieldName="Category" WebServiceUrl="~/Sitefinity/Services/Taxonomies/HierarchicalTaxon.svc" TaxonomyId="E5CD6D69-1543-427B-AD62-688A99F5E7D4" />
  4.  
  5.  
  6.  
  7. DESIGNER.cs:
  8. ------------
  9. descriptor.AddComponentProperty("FilterCategory", this.FilterCategory.ClientID);
  10.  
  11.  
  12.  
  13. DESIGNER.js:
  14. ------------
  15. // Called when the designer window gets opened and here is place to "bind" your designer to the control properties
  16. refreshUI: function () {
  17.     var controlData = this._propertyEditor.get_control(); // JavaScript clone of your control - all the control properties will be properties of the controlData too
  18.     controlData.CategoryIdsStr && this.get_FilterCategory().set_value(controlData.CategoryIdsStr.split(','));
  19. },
  20. // Called when the "save" button is clicked. Here you can transfer the settings from the designer to the control
  21. applyChanges: function () {
  22.     var controlData = this._propertyEditor.get_control();
  23.     controlData.CategoryIdsStr = this.get_FilterCategory().get_value() ? this.get_FilterCategory().get_value().join() : "";
  24. },
  25.  
  26.  
  27. CONTROL.cs:
  28. -----------
  29. public string CategoryIdsStr { get; set; }
  30. private List<Guid> categoryIds {
  31.     get {
  32.         return CategoryIdsStr.IsNullOrWhitespace() ? new List<Guid>() : CategoryIdsStr.Split(',').Where(o => o.IsGuid()).Select(o => o.ToGuid()).ToList();
  33.     }
  34. }
  35. private string categoryIdFilterClause {
  36.     get {
  37.         var clauses = categoryIds.Select(o => string.Format("Category.Contains(({0}))", o));
  38.         return string.Join(" OR ", clauses);
  39.     }
  40. }
  41.  
  42. protected override void InitializeControls(GenericContainer container) {
  43.     //NB: uses dynamic LINQ expression as "Intersect" is not implemented on database...
  44.     //This way all filtering is done on DB for performance
  45.     var staffMembers = SitefinityFunctions.GetDynamicContentItems("StaffMembers.Staff");
  46.  
  47.     if (!categoryIdFilterClause.IsNullOrWhitespace()) {
  48.         staffMembers = staffMembers.Where(categoryIdFilterClause);
  49.     }
  50. }
  51.  
  52.  
  53. HELPERS.cs:
  54. -----------
  55. public static IQueryable<DynamicContent> GetDynamicContentItems(string type) {
  56.     var itemType = TypeResolutionService.ResolveType(string.Concat("Telerik.Sitefinity.DynamicTypes.Model.", type));
  57.     return DynamicModuleManager.GetManager().GetDataItems(itemType).Where(o => o.Visible && o.Status == ContentLifecycleStatus.Live);
  58. }
  59.  
  60. public static Guid ToGuid(this string str) {
  61.     var g = Guid.Empty;
  62.     Guid.TryParse(str, out g);
  63.     return g;
  64. }
Advertisement
Add Comment
Please, Sign In to add comment