/Serenity.Script.UI/FilterPanel/FilterStore.cs

https://gitlab.com/pgksunilkumar/Serenity
C# | 137 lines | 111 code | 26 blank | 0 comment | 22 complexity | 207bd0ca8a1cac62cb7a0dd7b9f9bf1b MD5 | raw file
  1. using Serenity.Data;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. namespace Serenity
  6. {
  7. public class FilterStore
  8. {
  9. private EventHandler changed;
  10. private string displayText;
  11. public FilterStore(IEnumerable<PropertyItem> fields)
  12. {
  13. Items = new List<FilterLine>();
  14. if (fields == null)
  15. throw new ArgumentNullException("source");
  16. this.Fields = fields.ToList();
  17. this.Fields.Sort((x, y) => Q.Externals.TurkishLocaleCompare(
  18. Q.TryGetText(x.Title) ?? x.Title ?? x.Name,
  19. Q.TryGetText(y.Title) ?? y.Title ?? y.Name));
  20. this.FieldByName = new JsDictionary<string,PropertyItem>();
  21. foreach (var field in fields)
  22. FieldByName[field.Name] = field;
  23. }
  24. public List<PropertyItem> Fields { get; private set; }
  25. public JsDictionary<string, PropertyItem> FieldByName { get; private set; }
  26. public List<FilterLine> Items { get; private set; }
  27. public void RaiseChanged()
  28. {
  29. displayText = null;
  30. if (changed != null)
  31. changed(this, EventArgs.Empty);
  32. }
  33. public event EventHandler Changed
  34. {
  35. add { changed += value; }
  36. remove { changed -= value; }
  37. }
  38. public BaseCriteria ActiveCriteria
  39. {
  40. get
  41. {
  42. bool inParens = false;
  43. BaseCriteria currentBlock = Criteria.Empty;
  44. bool isBlockOr = false;
  45. BaseCriteria activeCriteria = Criteria.Empty;
  46. for (int i = 0; i < Items.Count; i++)
  47. {
  48. var line = Items[i];
  49. if (inParens && (line.RightParen || line.LeftParen))
  50. {
  51. if (!currentBlock.IsEmpty)
  52. {
  53. if (isBlockOr)
  54. activeCriteria |= ~(currentBlock);
  55. else
  56. activeCriteria &= ~(currentBlock);
  57. currentBlock = Criteria.Empty;
  58. }
  59. inParens = false;
  60. }
  61. if (line.LeftParen)
  62. {
  63. isBlockOr = line.IsOr;
  64. inParens = true;
  65. }
  66. if (line.IsOr)
  67. currentBlock |= line.Criteria;
  68. else
  69. currentBlock &= line.Criteria;
  70. }
  71. if (!currentBlock.IsEmpty)
  72. {
  73. if (isBlockOr)
  74. activeCriteria |= ~(currentBlock);
  75. else
  76. activeCriteria &= ~(currentBlock);
  77. }
  78. return activeCriteria;
  79. }
  80. }
  81. public string DisplayText
  82. {
  83. get
  84. {
  85. if (displayText == null)
  86. {
  87. bool inParens = false;
  88. displayText = "";
  89. for (int i = 0; i < Items.Count; i++)
  90. {
  91. var line = Items[i];
  92. if (inParens && (line.RightParen || line.LeftParen))
  93. {
  94. displayText += ")";
  95. inParens = false;
  96. }
  97. if (displayText.Length > 0)
  98. displayText += " " + Q.Text("Controls.FilterPanel." + (line.IsOr ? "Or" : "And")) + " ";
  99. if (line.LeftParen)
  100. {
  101. displayText += "(";
  102. inParens = true;
  103. }
  104. displayText += line.DisplayText;
  105. }
  106. }
  107. return displayText;
  108. }
  109. }
  110. }
  111. }