RATreeNode.m 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. //The MIT License (MIT)
  2. //
  3. //Copyright (c) 2014 Rafał Augustyniak
  4. //
  5. //Permission is hereby granted, free of charge, to any person obtaining a copy of
  6. //this software and associated documentation files (the "Software"), to deal in
  7. //the Software without restriction, including without limitation the rights to
  8. //use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
  9. //the Software, and to permit persons to whom the Software is furnished to do so,
  10. //subject to the following conditions:
  11. //
  12. //THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. //IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
  14. //FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
  15. //COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
  16. //IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  17. //CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  18. //
  19. #import "RATreeNode.h"
  20. #import "RATreeNodeItem.h"
  21. @interface RATreeNode () {
  22. BOOL _expanded;
  23. }
  24. @property (nonatomic) BOOL expanded;
  25. @property (nonatomic, strong) RATreeNodeItem *lazyItem;
  26. @property (nonatomic, copy) BOOL (^expandedBlock)(id);
  27. @end
  28. @implementation RATreeNode
  29. - (id)initWithLazyItem:(RATreeNodeItem *)item expandedBlock:(BOOL (^)(id))expandedBlock;
  30. {
  31. self = [super init];
  32. if (self) {
  33. _lazyItem = item;
  34. _expandedBlock = expandedBlock;
  35. }
  36. return self;
  37. }
  38. #pragma mark -
  39. - (RATreeNodeItem *)item
  40. {
  41. return self.lazyItem.item;
  42. }
  43. - (BOOL)expanded
  44. {
  45. if (self.expandedBlock) {
  46. _expanded = self.expandedBlock(self.item);
  47. self.expandedBlock = nil;
  48. }
  49. return _expanded;
  50. }
  51. - (void)setExpanded:(BOOL)expanded
  52. {
  53. self.expandedBlock = nil;
  54. _expanded = expanded;
  55. }
  56. @end